:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface-2: #f3efe9;
  --text: #1f1d1a;
  --muted: #6b665e;
  --border: #e6e0d6;
  --accent: #d93a3f;
  --accent-soft: #fde9e8;
  --good: #2f8a4c;
  --good-soft: #e3f4e8;
  --bad: #c43535;
  --bad-soft: #fbe5e5;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px;
  --jp: 'Noto Sans JP', 'Hiragino Sans', 'Yu Gothic', sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161514; --surface: #201f1d; --surface-2: #2a2826; --text: #ece8e2; --muted: #a39d93;
    --border: #36332f; --accent: #ff6b6b; --accent-soft: #3a2324; --good: #5fc77f; --good-soft: #1f3326;
    --bad: #ff7676; --bad-soft: #3b2222; --shadow: 0 1px 2px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  --bg: #161514; --surface: #201f1d; --surface-2: #2a2826; --text: #ece8e2; --muted: #a39d93;
  --border: #36332f; --accent: #ff6b6b; --accent-soft: #3a2324; --good: #5fc77f; --good-soft: #1f3326;
  --bad: #ff7676; --bad-soft: #3b2222; --shadow: 0 1px 2px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: 'Noto Sans KR', var(--jp), system-ui, sans-serif;
  line-height: 1.6; -webkit-font-smoothing: antialiased;
}
.jp, ruby, .kana-big, .kanji-big { font-family: var(--jp); }
a { color: inherit; }
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .5em; }
h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); }
h2 { font-size: 1.35rem; }
.muted { color: var(--muted); }

/* ── Top bar ── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; text-decoration: none; white-space: nowrap; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-family: var(--jp); font-size: 18px;
}
.nav { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; }
.nav a { padding: 6px 12px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 500; font-size: .95rem; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.theme-btn, .menu-btn { background: none; border: 1px solid var(--border); border-radius: 10px; width: 36px; height: 36px; flex: none; }
.menu-btn { display: none; margin-left: auto; }
@media (max-width: 860px) {
  .menu-btn { display: block; }
  .nav { display: none; position: absolute; top: 100%; left: 0; right: 0; flex-direction: column;
    background: var(--surface); border-bottom: 1px solid var(--border); padding: 8px 16px; }
  .nav.open { display: flex; }
  .topbar { padding: 10px 16px; }
}

.container { max-width: 1040px; margin: 0 auto; padding: 28px 16px 60px; min-height: 70vh; }
.footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 24px 16px; border-top: 1px solid var(--border); }

/* ── Generic UI ── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 500; text-decoration: none; transition: transform .08s, background .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.small { padding: 6px 12px; font-size: .9rem; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 16px 0; }
.tab { padding: 7px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-size: .92rem; }
.tab.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); font-size: .8rem; color: var(--muted); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.speak { background: none; border: none; font-size: 1.1rem; padding: 4px; border-radius: 8px; line-height: 1; }
.speak:hover { background: var(--surface-2); }
.section-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.input { padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; min-width: 0; }

/* ── Home ── */
.hero { padding: 24px 0 28px; }
.hero p { color: var(--muted); max-width: 620px; margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin: 22px 0 30px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat b { display: block; font-size: 1.6rem; }
.stat span { color: var(--muted); font-size: .85rem; }
.module { text-decoration: none; display: flex; flex-direction: column; gap: 8px; transition: transform .12s, border-color .12s; }
.module:hover { transform: translateY(-2px); border-color: var(--accent); }
.module .glyph { font-family: var(--jp); font-size: 2.2rem; font-weight: 700; color: var(--accent); }
.module h3 { margin: 0; }
.module p { margin: 0; color: var(--muted); font-size: .92rem; }
.path { counter-reset: step; display: grid; gap: 10px; margin-top: 10px; }
.path li { list-style: none; display: flex; gap: 12px; align-items: baseline; }
.path li::before { counter-increment: step; content: counter(step); flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: .85rem; }
.path ul, ul.path { padding: 0; margin: 0; }

/* ── Kana chart ── */
.kana-table { display: grid; grid-template-columns: 56px repeat(5, 1fr); gap: 8px; max-width: 640px; }
.kana-table.yoon { grid-template-columns: 56px repeat(3, 1fr); max-width: 420px; }
.kana-rowlabel { font-family: var(--jp); color: var(--muted); font-size: .85rem; display: grid; place-items: center; }
.kana-cell {
  aspect-ratio: 1 / 1; background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; position: relative;
  transition: border-color .12s, transform .08s;
}
.kana-cell:hover { border-color: var(--accent); }
.kana-cell:active { transform: scale(.96); }
.kana-cell.empty { background: transparent; border: 1px dashed var(--border); cursor: default; }
.kana-cell .ch { font-family: var(--jp); font-size: clamp(1.3rem, 4vw, 2rem); font-weight: 500; line-height: 1.1; }
.kana-cell .ro { font-size: .78rem; color: var(--muted); }
.kana-cell.learned::after { content: ''; position: absolute; top: 7px; right: 7px; width: 7px; height: 7px; border-radius: 50%; background: var(--good); }
.kana-table.hide-romaji .ro { visibility: hidden; }
@media (max-width: 520px) { .kana-table { grid-template-columns: 34px repeat(5, 1fr); gap: 4px; } .kana-table.yoon { grid-template-columns: 34px repeat(3, 1fr); } .kana-rowlabel { font-size: .7rem; } }
.tips { margin: 0; padding-left: 18px; color: var(--muted); }
.tips li { margin: 4px 0; }

/* ── Quiz ── */
.quiz { max-width: 620px; margin: 0 auto; }
.quiz-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-size: .9rem; color: var(--muted); }
.quiz-prompt { text-align: center; padding: 30px 16px 22px; }
.quiz-prompt .big { font-family: var(--jp); font-size: clamp(2.6rem, 10vw, 4.4rem); font-weight: 700; line-height: 1.15; }
.quiz-prompt .mid { font-family: var(--jp); font-size: clamp(1.3rem, 4.5vw, 1.8rem); font-weight: 500; }
.quiz-prompt .hint { color: var(--muted); margin-top: 8px; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choices.single { grid-template-columns: 1fr; }
.choice { padding: 14px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); font-size: 1.05rem; text-align: center; }
.choice:hover:not(:disabled) { border-color: var(--accent); }
.choice.correct { background: var(--good-soft); border-color: var(--good); color: var(--good); font-weight: 700; }
.choice.wrong { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.choice:disabled { cursor: default; }
.feedback { min-height: 56px; margin-top: 14px; padding: 12px 14px; border-radius: 12px; font-size: .95rem; }
.feedback.good { background: var(--good-soft); color: var(--good); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.feedback:empty { background: none; }
.type-input { width: 100%; text-align: center; font-size: 1.4rem; padding: 12px; }
.result { text-align: center; padding: 30px 10px; }
.result .score { font-size: 3rem; font-weight: 700; color: var(--accent); }

/* ── Flashcard ── */
.flash { perspective: 1200px; max-width: 520px; margin: 0 auto; }
.flash-card { position: relative; height: 300px; transform-style: preserve-3d; transition: transform .45s; cursor: pointer; }
.flash-card.flipped { transform: rotateY(180deg); }
.flash-face { position: absolute; inset: 0; backface-visibility: hidden; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; text-align: center; padding: 20px; }
.flash-back { transform: rotateY(180deg); }
.flash-face .kanji-big { font-size: clamp(3rem, 12vw, 5rem); font-weight: 700; line-height: 1.1; }
.flash-face .reading { font-family: var(--jp); font-size: 1.3rem; color: var(--accent); }
.flash-face .meaning { font-size: 1.25rem; font-weight: 500; }
.flash-controls { display: flex; justify-content: center; gap: 10px; margin-top: 16px; flex-wrap: wrap; }

/* ── Kanji ── */
.kanji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.kanji-tile { aspect-ratio: 1; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; }
.kanji-tile:hover { border-color: var(--accent); }
.kanji-tile .k { font-family: var(--jp); font-size: 2rem; font-weight: 500; line-height: 1.1; }
.kanji-tile .m { font-size: .72rem; color: var(--muted); }
.kanji-tile.learned::after { content: ''; position: absolute; top: 7px; right: 7px; width: 7px; height: 7px; border-radius: 50%; background: var(--good); }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; animation: fade .15s; }
.modal { width: min(520px, 100%); max-height: 90vh; overflow: auto; position: relative; }
.modal .close { position: absolute; top: 12px; right: 12px; background: none; border: none; font-size: 1.3rem; }
@keyframes fade { from { opacity: 0; } }
.kanji-detail { display: flex; gap: 20px; align-items: center; margin-bottom: 14px; }
.kanji-detail .kanji-big { font-size: 5rem; line-height: 1; font-weight: 700; }
.readings { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: .95rem; }
.readings dt { color: var(--muted); }
.readings dd { margin: 0; font-family: var(--jp); }

/* ── Vocab / Sentences list ── */
.list { display: grid; gap: 8px; }
.row { display: flex; align-items: center; gap: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; }
.row .main { flex: 1; min-width: 0; }
.row .jp-word { font-family: var(--jp); font-size: 1.2rem; font-weight: 500; }
.row .sub { color: var(--muted); font-size: .9rem; }
.row .sentence { font-family: var(--jp); font-size: 1.15rem; }
.row.learned { border-left: 4px solid var(--good); }
ruby rt { font-size: .55em; color: var(--muted); }
.hide-reading .reading-line { filter: blur(5px); transition: filter .2s; }
.hide-reading .reading-line:hover { filter: none; }
.hide-meaning .meaning-line { filter: blur(5px); transition: filter .2s; }
.hide-meaning .meaning-line:hover { filter: none; }

/* ── Sentence builder ── */
.builder-answer { min-height: 64px; border: 2px dashed var(--border); border-radius: 12px; padding: 10px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 12px 0; }
.builder-pool { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; min-height: 48px; }
.chip { font-family: var(--jp); padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); font-size: 1.1rem; box-shadow: var(--shadow); }
.chip:hover { border-color: var(--accent); }

/* ── Grammar ── */
.lesson-list { display: grid; gap: 10px; }
.lesson-item { display: flex; align-items: center; gap: 14px; text-decoration: none; }
.lesson-item .num { flex: none; width: 36px; height: 36px; border-radius: 10px; background: var(--surface-2); display: grid; place-items: center; font-weight: 700; color: var(--muted); }
.lesson-item.done .num { background: var(--good-soft); color: var(--good); }
.lesson-item .t { font-family: var(--jp); font-weight: 700; }
.lesson-item .s { color: var(--muted); font-size: .9rem; }
.lesson-body { font-size: 1rem; }
.lesson-body p { margin: .5em 0; }
.lesson-body ul { padding-left: 20px; }
.lesson-body li { margin: 4px 0; }
.lesson-body b { color: var(--accent); font-family: var(--jp); }
.gtable { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: .95rem; }
.gtable th, .gtable td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; }
.gtable th { background: var(--surface-2); }
.gtable td { font-family: var(--jp); }
.table-wrap { overflow-x: auto; }
.example { padding: 12px 0; border-bottom: 1px solid var(--border); }
.example:last-child { border-bottom: none; }
.example .jp { font-size: 1.15rem; }
.example .r { color: var(--muted); font-size: .88rem; font-family: var(--jp); }

.empty-state { text-align: center; padding: 40px 16px; color: var(--muted); }
.mt { margin-top: 24px; }
.mb { margin-bottom: 16px; }

/* ── Account button ── */
.acc-btn { display: inline-flex; align-items: center; gap: 8px; padding: 5px 12px 5px 6px; border: 1px solid var(--border); border-radius: 999px; text-decoration: none; font-size: .9rem; font-weight: 500; white-space: nowrap; flex: none; min-height: 36px; }
.acc-btn:not(:has(.avatar)) { padding: 5px 14px; }
.acc-btn.active, .acc-btn:hover { border-color: var(--accent); }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: .85rem; flex: none; }
.avatar.big { width: 56px; height: 56px; font-size: 1.5rem; }
.acc-name { max-width: 90px; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 520px) { .acc-name { display: none; } .acc-btn:has(.avatar) { padding: 4px; } .brand span:last-child { font-size: .95rem; } }

/* ── Course ── */
.continue { display: flex; align-items: center; gap: 16px; text-decoration: none; margin-bottom: 18px; border-left: 5px solid var(--lv, var(--accent)); }
.continue:hover { border-color: var(--lv, var(--accent)); }
@media (max-width: 520px) { .continue { flex-wrap: wrap; } .continue .btn { width: 100%; } }
.lv-badge { flex: none; width: 42px; height: 42px; border-radius: 12px; background: var(--lv, var(--accent)); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 1.15rem; }
.level-card { margin-bottom: 12px; border-left: 5px solid var(--lv); padding: 16px 18px; }
.level-card.locked { opacity: .62; }
.level-card.locked .lv-badge { background: var(--muted); }
.level-head { display: flex; align-items: center; gap: 14px; cursor: pointer; list-style: none; }
.level-head::-webkit-details-marker { display: none; }
.level-head::after { content: '▾'; color: var(--muted); transition: transform .2s; }
.level-card[open] .level-head::after { transform: rotate(180deg); }
.lv-name { font-weight: 700; font-size: 1.08rem; }
.lessons { display: grid; gap: 6px; }
.lesson-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); text-decoration: none; background: var(--bg); }
a.lesson-row:hover { border-color: var(--lv, var(--accent)); }
.lesson-row.current { border-color: var(--lv); box-shadow: 0 0 0 2px color-mix(in srgb, var(--lv) 25%, transparent); }
.lesson-row.is-locked { color: var(--muted); }
.lesson-row.test { background: var(--surface-2); font-weight: 500; }
.ls-icon { flex: none; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-size: .82rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.lesson-row.done .ls-icon { background: var(--good-soft); color: var(--good); }
.lesson-row.current .ls-icon { background: var(--lv); color: #fff; }
.ls-title { flex: 1; min-width: 0; font-family: var(--jp), 'Noto Sans KR', sans-serif; }
.ls-type { font-size: .75rem; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; flex: none; }
.stars { color: #e0a100; letter-spacing: 1px; white-space: nowrap; }
.stars .off { color: var(--border); }
.pill.good { background: var(--good-soft); color: var(--good); }
.steps { display: flex; align-items: center; gap: 8px; margin: 14px 0; flex-wrap: wrap; font-size: .9rem; }
.step { padding: 5px 12px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.step.on { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.step-arrow { color: var(--muted); }
.pass { font-size: 1.3rem; font-weight: 700; color: var(--good); }
.kcards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px; }
.kcard { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; background: var(--bg); }
.kc-top { display: flex; gap: 14px; align-items: center; margin-bottom: 6px; }
.kc-top .kanji-big { font-size: 3rem; line-height: 1; }
.kc-ex { font-size: .95rem; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

/* ── Auth ── */
.auth-wrap { max-width: 440px; margin: 0 auto; }
.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-size: .9rem; font-weight: 500; }
.notice { padding: 12px 14px; border-radius: 12px; margin: 12px 0; font-size: .95rem; }
.notice.good { background: var(--good-soft); color: var(--good); }
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.acc-head { display: flex; gap: 16px; align-items: center; }
