:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f1eee7;
  --text: #22201c;
  --muted: #6f6a60;
  --line: #e3ded3;
  --accent: #c8102e;
  --accent-2: #a50d26;
  --accent-soft: #fbe7ea;
  --ok: #2e7d32;
  --ok-soft: #e5f3e6;
  --warn: #b26a00;
  --warn-soft: #fff1dc;
  --info: #1565c0;
  --info-soft: #e3eefb;
  --t1: #d32f2f; --t2: #2e7d32; --t3: #1565c0; --t4: #7b1fa2; --t5: #757575;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 20px rgba(0,0,0,.06);
  --hanzi-font: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Source Han Sans SC", "Microsoft YaHei", "Heiti SC", sans-serif;
  --kai-font: "Kaiti SC", "STKaiti", "KaiTi", "Noto Serif SC", "Songti SC", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15140f; --surface: #1f1d18; --surface-2: #2a2721; --text: #f1ede4; --muted: #a59f92; --line: #38342c;
    --accent: #ff5a6e; --accent-2: #ff7d8d; --accent-soft: #3a1d22; --ok: #7bd389; --ok-soft: #1d3320;
    --warn: #ffb74d; --warn-soft: #3a2c14; --info: #7fb5ff; --info-soft: #1a2a40;
    --t1: #ff6b6b; --t2: #7bd389; --t3: #7fb5ff; --t4: #d29bff; --t5: #a59f92;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15140f; --surface: #1f1d18; --surface-2: #2a2721; --text: #f1ede4; --muted: #a59f92; --line: #38342c;
  --accent: #ff5a6e; --accent-2: #ff7d8d; --accent-soft: #3a1d22; --ok: #7bd389; --ok-soft: #1d3320;
  --warn: #ffb74d; --warn-soft: #3a2c14; --info: #7fb5ff; --info-soft: #1a2a40;
  --t1: #ff6b6b; --t2: #7bd389; --t3: #7fb5ff; --t4: #d29bff; --t5: #a59f92;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
.hz { font-family: var(--hanzi-font); }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; padding: 20px 14px;
  background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-family: var(--hanzi-font); font-size: 22px; font-weight: 700;
}
.brand b { display: block; font-size: 16px; }
.brand small { color: var(--muted); font-size: 12px; }
.nav-btn {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 11px 12px; border: 0; border-radius: 12px; background: transparent;
  cursor: pointer; text-align: left; font-weight: 500; color: var(--muted);
}
.nav-btn svg { width: 20px; height: 20px; flex: none; }
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.sidebar .spacer { flex: 1; }
.streak-pill {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 12px;
  background: var(--warn-soft); color: var(--warn); font-weight: 600; font-size: 14px;
}
main { padding: 28px 32px 80px; max-width: 1100px; width: 100%; min-width: 0; }
.grid > *, .stack > * { min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 26px; letter-spacing: -.01em; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

/* bottom nav on mobile */
.bottom-nav { display: none; }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  main { padding: 16px 16px 96px; }
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: var(--surface); border-top: 1px solid var(--line);
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .bottom-nav .nav-btn { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; text-align: center; justify-content: center; }
  .page-head h1 { font-size: 22px; }
}

/* ---------- Cards & UI ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 12px; font-size: 17px; }
.card h3 { margin: 0 0 8px; font-size: 15px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 860px) { .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 520px) { .grid-2 { grid-template-columns: 1fr; } }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.mt { margin-top: 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600; min-height: 44px;
  transition: transform .06s ease, background .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 6px 12px; min-height: 36px; font-size: 14px; border-radius: 10px; }
.btn.lg { padding: 14px 26px; font-size: 17px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; }
.icon-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; }

.seg { display: inline-flex; background: var(--surface-2); padding: 4px; border-radius: 12px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted); min-height: 38px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

select, input[type="text"], input[type="search"], input[type="number"] {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); min-height: 44px;
}
input[type="search"] { width: 100%; }
label.switch { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
label.switch:last-child { border-bottom: 0; }
label.switch input { width: 22px; height: 22px; accent-color: var(--accent); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 650; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.new { background: var(--surface-2); color: var(--muted); }
.badge.learning { background: var(--warn-soft); color: var(--warn); }
.badge.mastered { background: var(--ok-soft); color: var(--ok); }
.badge.lvl { background: var(--accent-soft); color: var(--accent); }

.progress { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; display: flex; }
.progress > span { display: block; height: 100%; }
.progress .p-m { background: var(--ok); }
.progress .p-l { background: #f0b34a; }

.stat { display: flex; flex-direction: column; gap: 2px; }
.stat b { font-size: 28px; line-height: 1.1; letter-spacing: -.02em; }
.stat span { color: var(--muted); font-size: 13px; }

/* tones */
.tone1 { color: var(--t1); } .tone2 { color: var(--t2); } .tone3 { color: var(--t3); } .tone4 { color: var(--t4); } .tone5 { color: var(--t5); }

/* ---------- Dashboard ---------- */
.hero {
  display: grid; grid-template-columns: 1fr auto; gap: 20px; align-items: center;
  background: linear-gradient(135deg, var(--accent) 0%, #8e0b21 100%); color: #fff; border: 0;
}
.hero h2 { color: #fff; font-size: 22px; margin-bottom: 6px; }
.hero p { margin: 0; opacity: .9; }
.hero .btn { background: #fff; color: #8e0b21; border-color: #fff; }
.hero .where { display: inline-block; margin-top: 12px; background: rgba(255,255,255,.16); padding: 6px 12px; border-radius: 10px; font-weight: 600; }
.hero-hz { font-family: var(--kai-font); font-size: 92px; line-height: 1; opacity: .9; }
@media (max-width: 600px) { .hero { grid-template-columns: 1fr; } .hero-hz { display: none; } }

.level-row { display: grid; grid-template-columns: 74px 1fr 96px; gap: 14px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.level-row:last-child { border-bottom: 0; }
.level-row .lv { font-weight: 750; }
.level-row .nums { text-align: right; font-size: 13px; color: var(--muted); }

.heatmap { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 14px); gap: 3px; overflow-x: auto; padding-bottom: 4px; }
.heatmap i { width: 14px; height: 14px; border-radius: 3px; background: var(--surface-2); display: block; }
.heatmap i.h1 { background: #f6c6cd; } .heatmap i.h2 { background: #ec8a98; } .heatmap i.h3 { background: #dc4359; } .heatmap i.h4 { background: #a50d26; }

/* ---------- Units ---------- */
.units { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.unit {
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; cursor: pointer; background: var(--surface);
  text-align: left; display: flex; flex-direction: column; gap: 6px;
}
.unit:hover { border-color: var(--accent); }
.unit.current { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.unit b { font-size: 14px; }
.unit .progress { height: 6px; }
.unit small { color: var(--muted); font-size: 12px; }
.unit .hzs { font-family: var(--hanzi-font); font-size: 15px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Flashcards ---------- */
.study-wrap { max-width: 680px; margin: 0 auto; }
.study-top { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.study-top .progress { flex: 1; }
.flash {
  position: relative; min-height: 420px; padding: 28px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px;
  cursor: pointer; user-select: none;
}
.flash .big-hz { font-family: var(--hanzi-font); font-size: clamp(64px, 14vw, 120px); line-height: 1.1; font-weight: 500; }
.flash .big-id { font-size: clamp(24px, 4vw, 32px); font-weight: 650; }
.flash .pinyin { font-size: 26px; font-weight: 600; letter-spacing: .02em; }
.flash .arti { font-size: 20px; }
.flash .hint { color: var(--muted); font-size: 14px; }
.flash .illus { width: 128px; height: 128px; }
.flash .illus svg { width: 100%; height: 100%; }
.flash .corner { position: absolute; top: 14px; display: flex; gap: 6px; }
.flash .corner.l { left: 14px; } .flash .corner.r { right: 14px; }
.flash .divider { width: 60%; height: 1px; background: var(--line); margin: 6px 0; }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 14px; }
.grades .btn { flex-direction: column; gap: 0; padding: 10px 6px; min-height: 64px; }
.grades .btn small { font-weight: 500; color: var(--muted); font-size: 12px; }
.grades .g0 { border-color: #e57373; color: #c62828; }
.grades .g1 { border-color: #ffb74d; color: var(--warn); }
.grades .g2 { border-color: #81c784; color: var(--ok); }
.grades .g3 { border-color: #64b5f6; color: var(--info); }
.kbd { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 0 5px; margin-left: 4px; }

.done-box { text-align: center; padding: 40px 20px; }
.done-box .big { font-size: 56px; }

/* ---------- Quiz ---------- */
.quiz-q { text-align: center; padding: 26px 20px; }
.quiz-q .big-hz { font-family: var(--hanzi-font); font-size: clamp(56px, 12vw, 96px); line-height: 1.15; }
.quiz-q .prompt { font-size: 22px; font-weight: 650; }
.options { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 16px; }
@media (max-width: 520px) { .options { grid-template-columns: 1fr; } }
.opt {
  padding: 16px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); cursor: pointer; min-height: 64px;
  font-size: 17px; font-weight: 600; text-align: center; display: flex; align-items: center; justify-content: center;
}
.opt.hz { font-size: 30px; font-weight: 500; }
.opt:hover { border-color: var(--muted); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); color: var(--ok); }
.opt.wrong { border-color: #e57373; background: var(--accent-soft); color: #c62828; }
.feedback { margin-top: 14px; padding: 14px 16px; border-radius: 12px; display: none; }
.feedback.show { display: block; }
.feedback.ok { background: var(--ok-soft); color: var(--ok); }
.feedback.no { background: var(--accent-soft); color: var(--accent); }

/* ---------- Writing ---------- */
.write-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
@media (max-width: 900px) { .write-layout { grid-template-columns: 1fr; } }
.write-stage { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.write-box {
  position: relative; width: min(100%, 520px); aspect-ratio: 1; border-radius: 18px; background: var(--surface);
  border: 2px solid var(--line); touch-action: none; overflow: hidden;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.write-box > svg.grid-bg, .write-box > .grid-bg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.write-box > .writer { position: absolute; inset: 0; }
.write-box canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.write-box .ghost { position: absolute; inset: 0; display: grid; place-items: center; font-family: var(--kai-font); color: var(--text); opacity: .12; pointer-events: none; line-height: 1; }
.word-strip { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.word-strip button { font-family: var(--hanzi-font); font-size: 28px; width: 56px; height: 56px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); cursor: pointer; }
.word-strip button.on { border-color: var(--accent); color: var(--accent); }
.word-strip button.done { background: var(--ok-soft); border-color: var(--ok); }
.write-msg { min-height: 26px; font-weight: 600; text-align: center; }
.write-msg.ok { color: var(--ok); } .write-msg.no { color: var(--accent); }

/* ---------- Dictionary ---------- */
.dict-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.word-item {
  display: grid; grid-template-columns: 52px 1fr; gap: 10px; align-items: center; text-align: left;
  border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px; background: var(--surface); cursor: pointer; min-height: 72px;
}
.word-item:hover { border-color: var(--accent); }
.word-item .wi-pic { width: 52px; height: 52px; display: grid; place-items: center; font-family: var(--hanzi-font); font-size: 22px; background: var(--surface-2); border-radius: 10px; }
.word-item .wi-pic svg { width: 44px; height: 44px; }
.word-item .wi-h { font-family: var(--hanzi-font); font-size: 20px; font-weight: 600; }
.word-item .wi-p { font-size: 13px; }
.word-item .wi-a { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 100; display: none; align-items: center; justify-content: center; padding: 16px; }
.modal-bg.show { display: flex; }
.modal { width: min(560px, 100%); max-height: 92vh; overflow: auto; background: var(--surface); border-radius: 20px; padding: 24px; box-shadow: 0 20px 60px rgba(0,0,0,.3); position: relative; }
.modal .close { position: absolute; top: 12px; right: 12px; }
.detail-head { display: flex; gap: 18px; align-items: center; }
.detail-head .illus { width: 110px; height: 110px; flex: none; background: var(--surface-2); border-radius: 16px; display: grid; place-items: center; }
.detail-head .illus svg { width: 92px; height: 92px; }
.detail-head .dh { font-family: var(--hanzi-font); font-size: 56px; line-height: 1.1; }
.stroke-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.stroke-cell { width: 110px; height: 110px; border: 1px solid var(--line); border-radius: 12px; position: relative; background: var(--surface); }
.stroke-cell svg.grid-bg { position: absolute; inset: 0; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600; opacity: 0; transition: all .25s; z-index: 200; pointer-events: none; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.empty { text-align: center; color: var(--muted); padding: 30px; }
.tag-note { background: var(--info-soft); color: var(--info); padding: 10px 14px; border-radius: 12px; font-size: 14px; }
hr.sep { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }

/* ---------- Course / lessons ---------- */
.lesson-list { display: grid; gap: 10px; }
.lesson-tile {
  display: grid; grid-template-columns: 110px 1fr auto; gap: 16px; align-items: center; text-align: left;
  padding: 14px 18px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.lesson-tile:hover { border-color: var(--accent); }
.lesson-tile.current { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.lesson-tile.locked { opacity: .55; }
.lt-num { font-family: var(--hanzi-font); font-weight: 700; font-size: 18px; color: var(--accent); }
.lt-num small { display: block; font-family: system-ui, sans-serif; font-size: 12px; color: var(--muted); font-weight: 500; }
.lt-t { font-size: 20px; font-weight: 650; }
@media (max-width: 600px) { .lesson-tile { grid-template-columns: 1fr; gap: 6px; } }
.lesson-tabs { margin-top: 4px; }
.dialog h2 { font-size: 16px; }
.dl-line { display: grid; grid-template-columns: 70px 1fr 44px; gap: 10px; align-items: center; padding: 10px 6px; border-radius: 12px; }
.gram .dl-line { grid-template-columns: 1fr 44px; }
.dl-line.speaking { background: var(--warn-soft); }
.dl-sp { font-weight: 700; color: var(--accent); font-size: 15px; }
.dl-zh { font-size: 24px; line-height: 2.1; }
.dl-tr { color: var(--muted); font-size: 15px; }
.no-tr .dl-tr { display: none; }
.no-py rt { visibility: hidden; }
ruby.tk { ruby-position: over; padding: 0 1px; border-radius: 4px; cursor: pointer; }
ruby.tk:not(.p):hover { background: var(--accent-soft); }
ruby.tk.p { cursor: default; }
rt { font-size: 12px; color: var(--muted); font-family: system-ui, sans-serif; letter-spacing: 0; }
@media (max-width: 600px) { .dl-line { grid-template-columns: 1fr 44px; } .dl-sp { grid-column: 1 / -1; } .dl-zh { font-size: 21px; } }
.gnum { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 14px; margin-right: 6px; }
.vocab-table { display: flex; flex-direction: column; }
.vrow { display: grid; grid-template-columns: 40px 90px 130px 1fr auto; gap: 12px; align-items: center; text-align: left; padding: 8px 6px; border: 0; border-bottom: 1px solid var(--line); background: transparent; cursor: pointer; }
.vrow:hover { background: var(--surface-2); }
.vrow .v-pic svg { width: 36px; height: 36px; display: block; }
.vrow .v-h { font-size: 22px; }
@media (max-width: 600px) { .vrow { grid-template-columns: 36px 70px 1fr; } .vrow .v-a { grid-column: 2 / -1; } .vrow > span:last-child { display: none; } }
.ex-sec { font-family: var(--hanzi-font); font-weight: 700; color: var(--accent); margin-bottom: 6px; }
.ex-ins { font-weight: 600; margin: 6px 0 10px; }
.ex-sent { font-size: 26px; margin: 8px 0; line-height: 2.1; }
.ex-pic { width: 140px; height: 140px; margin: 10px auto; }
.ex-pic svg { width: 100%; height: 100%; }
.opt.txt { font-size: 15px; font-weight: 500; }
.blank { color: var(--accent); }
.order-ans { min-height: 64px; border: 2px dashed var(--line); border-radius: 14px; padding: 10px; margin: 12px 0; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; }
.order-pool { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 14px; }
.chip { font-size: 22px; padding: 8px 14px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); cursor: pointer; min-height: 48px; }
.chip.used { opacity: .3; }
.order-ans .chip { border-color: var(--accent); }
