:root {
  --bg: #FFF8EE;
  --surface: #FFFFFF;
  --ink: #3B2B20;
  --muted: #7D6E62;
  --line: #EEDFCB;
  --accent: #F5A623;
  --ok: #2EA66A;
  --ng: #E2483D;
  --shadow: 0 4px 0 rgba(59, 43, 32, .12);
  --radius: 18px;
  --on-accent: #3B2B20;
  --header-bg: rgba(255, 248, 238, .92);
  --soft: #F6EEE2;
  --hover: #FFF0D2;
  --chip: #FFF3D6;
  --mode-bg: #FFFBF4;
  --ok-bg: #E3F7EC;
  --ng-bg: #FDE7E5;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1F1914; --surface: #2B231C; --ink: #F3E6D6; --muted: #B5A595; --line: #43372C;
    --shadow: 0 4px 0 rgba(0, 0, 0, .35);
    --header-bg: rgba(31, 25, 20, .92); --soft: #3A3027; --hover: #44372A; --chip: #4A3B24;
    --mode-bg: #30271F; --ok-bg: #1E3A2B; --ng-bg: #44241F;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1F1914; --surface: #2B231C; --ink: #F3E6D6; --muted: #B5A595; --line: #43372C;
  --shadow: 0 4px 0 rgba(0, 0, 0, .35);
  --header-bg: rgba(31, 25, 20, .92); --soft: #3A3027; --hover: #44372A; --chip: #4A3B24;
  --mode-bg: #30271F; --ok-bg: #1E3A2B; --ng-bg: #44241F;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "M PLUS Rounded 1c", "Hiragino Maru Gothic ProN", "Meiryo", sans-serif;
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- ヘッダー ---------- */
.site-header {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 10px;
  padding-left: max(20px, env(safe-area-inset-left, 0px));
  padding-right: max(20px, env(safe-area-inset-right, 0px));
  background: var(--header-bg);
  backdrop-filter: blur(6px);
  border-bottom: 2px solid var(--line);
}
.logo {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 0; padding: 0;
  font-size: 1.2rem; font-weight: 800;
}
.logo b { color: var(--accent); }
.logo-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 50%;
  background: #FFD45C; border: 3px solid var(--ink); font-size: 1.2rem; line-height: 1;
}
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav button {
  background: none; border: 0; padding: 6px 12px; border-radius: 999px; font-weight: 700;
}
.nav button:hover, .nav button.active { background: var(--hover); }

main { flex: 1; width: 100%; max-width: 1040px; margin: 0 auto; padding-block: 24px 48px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }

.site-footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 20px; }

/* ---------- 共通 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 22px; border-radius: 999px;
  border: 3px solid var(--ink); background: var(--surface);
  font-weight: 800; box-shadow: var(--shadow);
  transition: transform .08s;
}
.btn:active { transform: translateY(3px); box-shadow: none; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.small { padding: 6px 14px; font-size: .9rem; }

h1, h2, h3 { line-height: 1.3; margin: 0; }
.section-title { font-size: 1.4rem; margin: 32px 0 14px; }
.lead { color: var(--muted); margin: 6px 0 0; }

.face svg { display: block; width: 100%; height: auto; }

/* ---------- ホーム ---------- */
.hero {
  display: grid; grid-template-columns: 1.3fr 1fr; gap: 24px; align-items: center;
  background: var(--surface); border: 3px solid var(--ink); border-radius: 28px;
  padding: 28px; box-shadow: var(--shadow);
}
.hero h1 { font-size: clamp(1.6rem, 4vw, 2.4rem); }
.hero h1 span { color: var(--accent); }
.hero .actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 20px; }
.hero-faces { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.hero-faces .face { animation: bob 3s ease-in-out infinite; }
.hero-faces .face:nth-child(2n) { animation-delay: -1.5s; }
@keyframes bob { 50% { transform: translateY(-6px); } }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.cat-card {
  background: var(--surface); border: 3px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column;
}
.cat-card .top {
  display: flex; align-items: center; gap: 12px; padding: 14px;
  background: var(--cat); color: #fff;
}
.cat-card .top .face { width: 64px; flex: none; background: rgba(255,255,255,.25); border-radius: 50%; }
.cat-card .top h3 { font-size: 1.25rem; }
.cat-card .top small { opacity: .9; font-weight: 700; }
.cat-card .body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 10px; flex: 1; }
.word-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.word-chips span { font-size: .8rem; background: var(--soft); border-radius: 999px; padding: 1px 9px; }
.cat-card .best { font-size: .85rem; color: var(--muted); margin-top: auto; }
.cat-card .row { display: flex; gap: 8px; }
.cat-card .row .btn { flex: 1; padding: 8px 10px; font-size: .9rem; }

/* ---------- 単語帳 ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: 8px; margin: 18px 0; }
.tab {
  border: 3px solid var(--ink); background: var(--surface); border-radius: 999px;
  padding: 4px 14px; font-weight: 700;
}
.tab.active { background: var(--cat, var(--accent)); color: #fff; }

.study-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px; }
.flip { perspective: 900px; height: 330px; background: none; border: 0; padding: 0; text-align: left; }
.flip-inner {
  position: relative; width: 100%; height: 100%;
  transition: transform .5s; transform-style: preserve-3d;
}
.flip.flipped .flip-inner { transform: rotateY(180deg); }
.side {
  position: absolute; inset: 0; backface-visibility: hidden;
  background: var(--surface); border: 3px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
}
.side.back { transform: rotateY(180deg); justify-content: flex-start; gap: 6px; }
.side .face { width: 170px; }
.side .word { font-size: 1.7rem; font-weight: 800; }
.side .hint { color: var(--muted); font-size: .8rem; }
.side.back .face { width: 96px; }
.side.back .ja { font-weight: 800; color: var(--cat); font-size: 1.1rem; }
.side.back .ex { font-size: .92rem; margin: 6px 0 0; }
.side.back .exja { font-size: .8rem; color: var(--muted); margin: 0; }
.speak {
  border: 2px solid var(--ink); background: var(--chip); border-radius: 999px;
  padding: 2px 10px; font-size: .85rem; font-weight: 700;
}

/* ---------- クイズ設定 ---------- */
.setup {
  background: var(--surface); border: 3px solid var(--ink); border-radius: 24px;
  padding: 24px; box-shadow: var(--shadow); max-width: 720px; margin: 0 auto;
}
.mode-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-top: 18px; }
.mode {
  border: 3px solid var(--ink); border-radius: 16px; background: var(--mode-bg);
  padding: 14px 12px; text-align: center; box-shadow: var(--shadow);
}
.mode:hover { background: var(--hover); }
.mode .icon { font-size: 1.8rem; }
.mode b { display: block; margin-top: 4px; }
.mode small { color: var(--muted); }
.cat-select { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

/* ---------- クイズ ---------- */
.quiz { max-width: 640px; margin: 0 auto; }
.quiz-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.progress { flex: 1; height: 14px; border: 3px solid var(--ink); border-radius: 999px; background: var(--surface); overflow: hidden; }
.progress div { height: 100%; background: var(--accent); transition: width .3s; }
.quiz-count { font-weight: 800; white-space: nowrap; }
.quit { background: none; border: 0; color: var(--muted); font-weight: 700; }

.q-card {
  background: var(--surface); border: 3px solid var(--ink); border-radius: 24px;
  box-shadow: var(--shadow); padding: 20px; text-align: center;
}
.q-card .face { width: 190px; margin: 0 auto; }
.q-label { color: var(--muted); font-weight: 700; font-size: .9rem; }
.q-main { font-size: 2rem; font-weight: 800; margin: 4px 0; }
.q-main.ja { font-size: 1.5rem; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.choice {
  border: 3px solid var(--ink); border-radius: 16px; background: var(--surface);
  padding: 14px 10px; font-weight: 800; font-size: 1.1rem; box-shadow: var(--shadow);
}
.choice:hover:not(:disabled) { background: var(--hover); }
.choice:disabled { cursor: default; }
.choice.correct { background: var(--ok); color: #fff; }
.choice.wrong { background: var(--ng); color: #fff; }
.choice.dim { opacity: .45; }

.spell-form { display: flex; gap: 8px; margin-top: 16px; }
.spell-form input {
  flex: 1; min-width: 0; border: 3px solid var(--ink); border-radius: 14px;
  padding: 12px 14px; font: inherit; font-size: 1.2rem; font-weight: 700;
}
.spell-hint { color: var(--muted); font-size: .9rem; margin-top: 6px; letter-spacing: .15em; }

.feedback {
  margin-top: 16px; border: 3px solid var(--ink); border-radius: 18px; padding: 14px 16px;
  text-align: left; animation: pop .25s ease-out;
}
.feedback.ok { background: var(--ok-bg); }
.feedback.ng { background: var(--ng-bg); }
.feedback .verdict { font-size: 1.2rem; font-weight: 800; }
.feedback.ok .verdict { color: var(--ok); }
.feedback.ng .verdict { color: var(--ng); }
.feedback p { margin: 4px 0; }
.feedback .next { margin-top: 10px; width: 100%; }
@keyframes pop { from { transform: scale(.96); opacity: 0; } }

/* ---------- 結果 ---------- */
.result { max-width: 640px; margin: 0 auto; text-align: center; }
.result .big { font-size: 3.4rem; font-weight: 800; }
.result .face { width: 150px; margin: 0 auto; }
.result .actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin: 18px 0; }
.review { text-align: left; display: grid; gap: 10px; }
.review-item {
  display: flex; gap: 12px; align-items: center;
  background: var(--surface); border: 3px solid var(--ink); border-radius: 16px; padding: 8px 12px;
}
.review-item .face { width: 56px; flex: none; margin: 0; }
.review-item b { font-size: 1.1rem; }
.review-item small { color: var(--muted); display: block; }

/* ---------- スマホ ---------- */
@media (max-width: 640px) {
  .site-header { padding: 8px 16px; }
  .logo > span:last-child { display: none; }
  .hero { grid-template-columns: 1fr; padding: 20px; }
  .hero-faces { order: -1; max-width: 320px; margin: 0 auto; }
  .choices { grid-template-columns: 1fr; }
  .q-card .face { width: 150px; }
}

body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 10; background: var(--bg); }
.logo-mark { color: var(--on-accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- ログイン ---------- */
.auth { display: flex; align-items: center; gap: 8px; }
.auth:empty { display: none; }
.btn.google { background: var(--surface); gap: 8px; }
.btn.google .g {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: #4285F4; color: #fff; font-family: Arial, sans-serif; font-weight: 700; font-size: .8rem;
}
.user-chip { display: flex; align-items: center; gap: 6px; font-weight: 700; max-width: 180px; }
.user-chip img, .user-chip .avatar {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--ink); flex: none;
}
.user-chip .avatar { display: grid; place-items: center; background: var(--accent); color: var(--on-accent); }
.user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-btn { background: none; border: 0; padding: 4px 6px; color: var(--muted); font-weight: 700; font-size: .85rem; }
.link-btn:hover { color: var(--ink); text-decoration: underline; }

.toast {
  position: fixed; z-index: 20; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  max-width: min(520px, calc(100% - 32px));
  background: var(--ink); color: var(--bg); border-radius: 14px; padding: 10px 16px;
  font-weight: 700; font-size: .9rem; box-shadow: var(--shadow);
}

/* ---------- 覚えた単語の進捗 ---------- */
.meter { height: 12px; border: 2px solid var(--ink); border-radius: 999px; background: var(--soft); overflow: hidden; }
.meter div { height: 100%; background: var(--ok); transition: width .4s; }
.progress-panel {
  margin-top: 20px; background: var(--surface); border: 3px solid var(--ink); border-radius: var(--radius);
  padding: 16px 20px; box-shadow: var(--shadow); display: grid; gap: 10px;
}
.progress-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.eyebrow { font-size: .8rem; font-weight: 800; color: var(--muted); letter-spacing: .08em; }
.progress-panel .count { font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.progress-panel .count b { font-size: 2rem; color: var(--ok); }
.note { margin: 0; color: var(--muted); font-size: .85rem; }

.cat-progress { display: grid; gap: 4px; font-size: .85rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.word-chips span.on { background: var(--ok-bg); color: var(--ok); font-weight: 700; }

.study-count { font-weight: 800; color: var(--ok); margin: -4px 0 14px; font-variant-numeric: tabular-nums; }
.learned-badge {
  display: none; position: absolute; top: 12px; right: 12px;
  background: var(--ok); color: #fff; border-radius: 999px; padding: 1px 10px; font-size: .8rem; font-weight: 800;
}
.flip.is-learned .learned-badge { display: inline-block; }
.flip.is-learned .side { border-color: var(--ok); }
.card-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: auto; }
.learn-toggle {
  border: 2px solid var(--ink); border-radius: 999px; padding: 2px 10px;
  font-size: .85rem; font-weight: 800; background: var(--surface); cursor: pointer;
}
.learn-toggle.on { background: var(--ok); border-color: var(--ok); color: #fff; }

.learned-summary {
  display: grid; gap: 8px; text-align: left; margin: 16px 0;
  background: var(--surface); border: 3px solid var(--ink); border-radius: var(--radius); padding: 14px 16px;
  font-variant-numeric: tabular-nums;
}
.learned-summary b { color: var(--ok); font-size: 1.15rem; }

@media (max-width: 640px) {
  .wide-only, .user-name { display: none; }
  .site-header { gap: 6px; }
  .nav { gap: 0; }
  .nav button { padding: 6px 7px; font-size: .9rem; }
  .auth .btn.google { padding: 5px 10px; gap: 5px; }
}
.nav button, .btn, .link-btn { white-space: nowrap; }


/* ---------- 卒業（アカウント削除） ---------- */
:root { --danger: #C93A30; }
.graduate-row { display: flex; justify-content: flex-end; }
.link-btn.danger { color: var(--danger); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.dialog {
  width: min(460px, calc(100% - 32px)); border: 3px solid var(--ink); border-radius: 22px;
  background: var(--surface); color: var(--ink); padding: 22px; box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(20, 14, 10, .55); }
.dialog h2 { font-size: 1.3rem; margin-bottom: 8px; }
.dialog p { margin: 8px 0; }
.dialog-note { color: var(--muted); font-size: .88rem; }
.dialog-error { color: var(--danger); font-weight: 700; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 16px; }
