:root {
  --paper: #EEF2EE;
  --sheet: #F8FAF7;
  --ink: #1F2A2E;
  --ink-soft: #55656A;
  --line: #D3DCD6;
  --cinnabar: #B83A2E;
  --cinnabar-soft: rgba(184, 58, 46, .38);
  --jade: #2F7D6B;
  --amber: #B7791F;
  --hanzi: "Noto Serif SC", "Noto Serif CJK SC", "Source Han Serif SC", "Songti SC", "Noto Sans CJK SC", serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --tabs-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #172024;
    --sheet: #1E292E;
    --ink: #E4EBE7;
    --ink-soft: #9AABA9;
    --line: #34444A;
    --cinnabar: #DD6A5C;
    --cinnabar-soft: rgba(221, 106, 92, .42);
    --jade: #5DB39E;
    --amber: #D9A24A;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.5 var(--ui);
  min-height: 100dvh;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
main { max-width: 560px; margin: 0 auto; padding: 20px 18px 28px; outline: none; }
h1 { font-size: 1.375rem; line-height: 1.25; margin: 0 0 4px; font-weight: 650; }
h2 { font-size: 1.0625rem; margin: 28px 0 10px; font-weight: 650; }
p { margin: 0 0 12px; }
.muted { color: var(--ink-soft); }
.small { font-size: .875rem; }
.hz { font-family: var(--hanzi); }
:focus-visible { outline: 2px solid var(--cinnabar); outline-offset: 2px; }

/* ---------- buttons ---------- */
button, .btn {
  font: inherit; color: inherit; cursor: pointer;
  border: 1px solid var(--line); background: var(--sheet);
  border-radius: 12px; padding: 12px 16px; min-height: 48px;
}
button:active { transform: translateY(1px); }
.btn-primary { background: var(--ink); color: var(--paper); border-color: var(--ink); font-weight: 600; }
.btn-wide { width: 100%; }
.icon-btn {
  min-height: 44px; min-width: 44px; padding: 0 14px; border-radius: 22px;
  display: inline-flex; align-items: center; gap: 6px; font-size: .9375rem;
}
.icon-btn svg { width: 18px; height: 18px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; inset: auto 0 0 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--sheet); border-top: 1px solid var(--line);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  color: var(--ink-soft); text-decoration: none; font-size: .75rem;
}
.tab-glyph { font-family: var(--hanzi); font-size: 1.3rem; line-height: 1.2; }
.tabs a[aria-current="page"] { color: var(--ink); }
.tabs a[aria-current="page"] .tab-glyph { color: var(--cinnabar); }

/* ---------- the tianzige square (the one bold element) ---------- */
.tzg {
  position: relative; aspect-ratio: 1; width: min(72vw, 300px); margin: 0 auto;
  border: 2px solid var(--cinnabar); background: var(--sheet);
  display: grid; place-items: center;
  container-type: inline-size;
  transition: width .2s ease-out;
}
.tzg.compact { width: min(40vw, 160px); }
.tzg::before, .tzg::after {
  content: ""; position: absolute; pointer-events: none;
}
.tzg::before { left: 0; right: 0; top: 50%; border-top: 1.5px dashed var(--cinnabar-soft); }
.tzg::after  { top: 0; bottom: 0; left: 50%; border-left: 1.5px dashed var(--cinnabar-soft); }
.tzg .glyph { font-family: var(--hanzi); font-weight: 600; line-height: 1; position: relative; z-index: 1; color: var(--ink); }
.tzg.sm { width: 56px; flex: 0 0 56px; margin: 0; border-width: 1.5px; }
.tzg.sm::before, .tzg.sm::after { border-width: 1px; }

/* ---------- study ---------- */
.study-top { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 18px; }
.counter { font-variant-numeric: tabular-nums; color: var(--ink-soft); font-size: .9375rem; }
.counter b { color: var(--ink); font-weight: 650; }
.card-meta { display: flex; justify-content: space-between; align-items: center; width: min(72vw, 300px); margin: 0 auto 10px; font-size: .875rem; color: var(--ink-soft); }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .8125rem; border: 1px solid currentColor; }
.pill.vazia { color: var(--amber); }
.pill.cheia { color: var(--jade); }
.pill.new { color: var(--cinnabar); }
.front-pinyin { text-align: center; font-size: 1.25rem; margin-top: 14px; color: var(--ink-soft); }
.reveal { margin-top: 18px; }
.answer { text-align: center; margin-top: 18px; animation: rise .22s ease-out; }
.answer .py { font-size: 1.75rem; letter-spacing: .01em; }
.answer .meaning { font-size: 1.125rem; margin-top: 2px; }
.answer .cls { color: var(--ink-soft); font-size: .875rem; margin-top: 2px; }
.sentence {
  margin-top: 18px; text-align: left; padding: 14px 16px; background: var(--sheet);
  border-left: 3px solid var(--cinnabar); border-radius: 0 10px 10px 0;
}
.sentence .s-hz { font-family: var(--hanzi); font-size: 1.375rem; line-height: 1.5; }
.sentence mark { background: none; color: var(--cinnabar); }
.sentence .s-py { color: var(--ink-soft); margin-top: 2px; }
.sentence .s-pt { margin-top: 6px; }
.sentence-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.grade { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 18px; }
.grade button { padding: 10px 6px; display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
.grade small { color: var(--ink-soft); font-size: .75rem; margin-top: 2px; }
.grade .g-again { border-color: var(--cinnabar); color: var(--cinnabar); }
.grade .g-good { border-color: var(--jade); color: var(--jade); }
.center-row { display: flex; justify-content: center; gap: 10px; margin-top: 14px; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.empty { text-align: center; padding-top: 24px; }
.empty .tzg { width: 140px; margin-bottom: 18px; }
.empty .tzg .glyph { font-size: 66cqw; }

/* ---------- quiz ---------- */
.modes { display: grid; gap: 10px; margin-top: 16px; }
.mode { text-align: left; display: flex; align-items: center; gap: 14px; padding: 14px 16px; }
.mode .tzg.sm .glyph { font-size: 60cqw; }
.mode b { display: block; font-weight: 650; }
.mode span.muted { font-size: .875rem; }
.q-prompt { text-align: center; margin: 8px 0 18px; }
.q-prompt .q-text { font-size: 1.25rem; }
.options { display: grid; gap: 10px; }
.options.hz-opts { grid-template-columns: 1fr 1fr; }
.options button { text-align: left; min-height: 54px; }
.options.hz-opts button { text-align: center; font-family: var(--hanzi); font-size: 2rem; padding: 10px; }
.options button.right { border-color: var(--jade); background: color-mix(in srgb, var(--jade) 14%, var(--sheet)); }
.options button.wrong { border-color: var(--cinnabar); background: color-mix(in srgb, var(--cinnabar) 12%, var(--sheet)); }
.progress-line { height: 4px; background: var(--line); border-radius: 2px; margin: 4px 0 18px; overflow: hidden; }
.progress-line i { display: block; height: 100%; background: var(--cinnabar); transition: width .25s; }
.score { text-align: center; padding-top: 12px; }
.score .big { font-size: 3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.missed { margin-top: 18px; text-align: left; }

/* ---------- list ---------- */
.search { width: 100%; font: inherit; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--sheet); color: var(--ink); min-height: 48px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chips button { min-height: 36px; padding: 4px 14px; border-radius: 18px; white-space: nowrap; font-size: .875rem; }
.chips button[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.rows { list-style: none; margin: 8px 0 0; padding: 0; }
.rows li button {
  width: 100%; display: grid; grid-template-columns: 36px 84px 1fr auto; align-items: center; gap: 10px;
  text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: none; padding: 10px 2px; min-height: 60px;
}
.rows .rk { color: var(--ink-soft); font-variant-numeric: tabular-nums; font-size: .8125rem; }
.rows .w { font-family: var(--hanzi); font-size: 1.5rem; white-space: nowrap; }
.rows .w.long { font-size: 1.125rem; }
.rows .pm { line-height: 1.3; min-width: 0; }
.rows .pm span { display: block; color: var(--ink-soft); font-size: .875rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; border: 1.5px solid var(--line); }
.dot.learning { background: var(--amber); border-color: var(--amber); }
.dot.known { background: var(--jade); border-color: var(--jade); }
.list-count { color: var(--ink-soft); font-size: .875rem; margin-top: 8px; }

/* ---------- detail dialog ---------- */
dialog {
  border: 0; padding: 0; width: min(560px, 100vw); max-width: 100vw; max-height: 88dvh;
  margin: auto auto 0; border-radius: 18px 18px 0 0; background: var(--paper); color: var(--ink);
}
dialog::backdrop { background: rgba(10, 16, 18, .45); }
dialog[open] { animation: sheet .2s ease-out; }
@keyframes sheet { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-body { padding: 18px 18px 28px; overflow-y: auto; max-height: 88dvh; }
.sheet-body .tzg { width: 150px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

/* ---------- progress ---------- */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 16px; }
.stat { background: var(--sheet); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.stat b { display: block; font-size: 1.625rem; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat span { color: var(--ink-soft); font-size: .875rem; }
.map { display: grid; grid-template-columns: repeat(40, 1fr); gap: 2px; margin-top: 8px; }
.map i { aspect-ratio: 1; background: var(--line); border-radius: 1px; }
.map i.learning { background: var(--amber); }
.map i.known { background: var(--jade); }
.legend { display: flex; gap: 14px; margin-top: 8px; font-size: .8125rem; color: var(--ink-soft); }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; background: var(--line); }
.legend .l-learning::before { background: var(--amber); }
.legend .l-known::before { background: var(--jade); }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting label { flex: 1; }
.setting select, .setting input[type=range] { font: inherit; color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: 10px; padding: 8px; min-height: 44px; }
.setting input[type=checkbox] { width: 24px; height: 24px; accent-color: var(--cinnabar); }
.setting input[type=range] { accent-color: var(--cinnabar); padding: 0; border: 0; background: none; }
.voice-status { padding: 12px 14px; border-radius: 12px; background: var(--sheet); border: 1px solid var(--line); margin-top: 8px; }
.voice-status.bad { border-color: var(--cinnabar); }
.voice-status ol { margin: 8px 0 0; padding-left: 20px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.danger { color: var(--cinnabar); border-color: var(--cinnabar); }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: 10px; font-size: .9375rem;
  opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; z-index: 10;
}
#toast.show { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
