:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #16202c;
  --muted: #5b6878;
  --line: #d9dee6;
  --primary: #1f4e79;
  --primary-ink: #ffffff;
  --accent: #e8590c;
  --accent-soft: #fff1e8;
  --ok: #1f8a4c;
  --ok-soft: #e5f6ec;
  --warn: #b7791f;
  --warn-soft: #fdf4e1;
  --bad: #c92a2a;
  --bad-soft: #fde8e8;
  --header: #1f2a37;
  --header-ink: #f5f7fa;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --radius: 14px;
  --font-vi: "Be Vietnam Pro", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --font-zh: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", "Noto Sans CJK TC", "Source Han Sans TC", "Microsoft YaHei", sans-serif;
  --zh-scale: 1;
  --bottomnav-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f151c; --surface: #18212b; --surface-2: #212c38; --text: #e8edf3; --muted: #9aa8b8;
    --line: #2c3947; --primary: #5b9bd5; --primary-ink: #0b1118; --accent: #ff8a3d; --accent-soft: #3a2416;
    --ok: #4cc983; --ok-soft: #15301f; --warn: #f0b44c; --warn-soft: #362a12; --bad: #ff6b6b; --bad-soft: #3a1a1a;
    --header: #0b1016; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f151c; --surface: #18212b; --surface-2: #212c38; --text: #e8edf3; --muted: #9aa8b8;
  --line: #2c3947; --primary: #5b9bd5; --primary-ink: #0b1118; --accent: #ff8a3d; --accent-soft: #3a2416;
  --ok: #4cc983; --ok-soft: #15301f; --warn: #f0b44c; --warn-soft: #362a12; --bad: #ff6b6b; --bad-soft: #3a1a1a;
  --header: #0b1016; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-vi); font-size: 16px; line-height: 1.55;
  overflow-x: hidden; min-height: 100vh; min-height: 100dvh;
  padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
.zh, [lang^="zh"] { font-family: var(--font-zh); }
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
img, svg { max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .75rem; }
.noscript { padding: 1rem; text-align: center; }

/* ---------- Header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--header); color: var(--header-ink);
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; min-width: 0; flex: 1; }
.brand-logo {
  flex: none; width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, #ff8a3d, #c2410c); color: #fff; font-family: var(--font-zh); font-weight: 700; font-size: 21px;
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-title { font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-size: .78rem; opacity: .75; }
.topnav { display: none; gap: 4px; }
.topnav a, .icon-btn { color: var(--header-ink); text-decoration: none; }
.topnav a { padding: 8px 12px; border-radius: 10px; font-weight: 500; font-size: .95rem; white-space: nowrap; }
.topnav a:hover { background: rgba(255,255,255,.08); }
.topnav a.active { background: rgba(255,255,255,.14); }
.icon-btn { flex: none; width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; }
.icon-btn:hover, .icon-btn.active { background: rgba(255,255,255,.12); }

/* ---------- Bottom nav (mobile) ---------- */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .7rem; font-weight: 500; min-width: 0;
}
.bottomnav a span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; padding: 0 2px; }
.bottomnav svg { width: 24px; height: 24px; fill: currentColor; }
.bottomnav a.active { color: var(--accent); }

@media (min-width: 900px) {
  .topnav { display: flex; }
  .bottomnav { display: none; }
  body { padding-bottom: 0; }
}

/* ---------- Layout ---------- */
main { display: block; max-width: 1080px; margin: 0 auto; padding: 16px 16px 32px; outline: none; }
@media (min-width: 900px) { main { padding: 24px 24px 48px; } }
.page-head { margin: 4px 0 16px; }
.page-head p { color: var(--muted); margin: 0; }
.banner { margin: 0; padding: 10px 16px; background: var(--warn-soft); color: var(--text); font-size: .9rem; border-bottom: 1px solid var(--line); }
.banner a { font-weight: 600; }

.grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .grid.cols-2 { grid-template-columns: repeat(2, 1fr); } .grid.cols-3 { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 900px) { .grid.cols-3 { grid-template-columns: repeat(3, 1fr); } }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.card.link { display: block; color: inherit; text-decoration: none; transition: transform .12s ease, border-color .12s; }
.card.link:hover { border-color: var(--primary); }
.card.link:active { transform: scale(.99); }
.card .meta { color: var(--muted); font-size: .88rem; }
.feature { display: flex; gap: 14px; align-items: flex-start; }
.feature .ico {
  flex: none; width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.feature .ico svg { width: 26px; height: 26px; fill: currentColor; }
.feature h3 { margin-bottom: 2px; }
.feature p { margin: 0; color: var(--muted); font-size: .92rem; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; text-align: center; min-width: 0; }
.stat b { display: block; font-size: 1.4rem; color: var(--primary); font-variant-numeric: tabular-nums; }
.stat span { font-size: .78rem; color: var(--muted); }

.hero {
  border-radius: var(--radius); padding: 18px; margin-bottom: 16px; color: #fff;
  background: linear-gradient(135deg, #1f4e79 0%, #243b53 60%, #7c2d12 130%);
}
.hero h1 { font-size: 1.35rem; }
.hero p { opacity: .9; margin: 0; }
.hero .zh { font-size: 1.1rem; opacity: .9; }

.section-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 22px 0 10px; flex-wrap: wrap; }
.section-title h2 { margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-weight: 600; font-size: .95rem; cursor: pointer;
  text-decoration: none; user-select: none; -webkit-user-select: none; touch-action: manipulation; white-space: nowrap;
}
.btn:hover { border-color: var(--primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 20px; height: 20px; fill: currentColor; flex: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 4px 10px; font-size: .85rem; border-radius: 10px; }
.btn.round { width: 44px; padding: 0; border-radius: 50%; }
.btn.round.small { width: 36px; }
.btn.recording { background: var(--bad); border-color: var(--bad); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(201,42,42,.45); } 50% { box-shadow: 0 0 0 10px rgba(201,42,42,0); } }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.btn-row.center { justify-content: center; }
.btn.mic-big { width: 84px; height: 84px; border-radius: 50%; padding: 0; background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.mic-big svg { width: 38px; height: 38px; }

/* ---------- Chips / tabs ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 10px; scrollbar-width: thin; -webkit-overflow-scrolling: touch; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 7px 14px; font-size: .88rem; font-weight: 500; cursor: pointer; white-space: nowrap; min-height: 38px;
}
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.chip small { opacity: .75; margin-left: 4px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; min-width: 0; }
.field label, .field .label { font-size: .88rem; color: var(--muted); font-weight: 500; }
.input, select.input, textarea.input {
  width: 100%; min-height: 44px; border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px;
  background: var(--surface); color: var(--text); font-size: 16px;
}
textarea.input { min-height: 80px; resize: vertical; font-family: var(--font-zh); }
.input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1 1 180px; min-width: 0; }
.switch { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--primary); flex: none; }
input[type="range"] { width: 100%; accent-color: var(--accent); }

/* ---------- Chinese text ---------- */
.hanzi { font-family: var(--font-zh); font-size: calc(1.35rem * var(--zh-scale)); line-height: 1.5; font-weight: 500; word-break: break-all; overflow-wrap: anywhere; }
.hanzi.xl { font-size: calc(2.2rem * var(--zh-scale)); }
.hanzi.xxl { font-size: calc(3.2rem * var(--zh-scale)); line-height: 1.3; }
.pinyin { color: var(--primary); font-size: .98rem; overflow-wrap: anywhere; }
.viet { color: var(--muted); font-size: .95rem; overflow-wrap: anywhere; }
.hide-pinyin .pinyin, .hide-viet .viet { display: none; }
.blur-hanzi .line .hanzi { filter: blur(7px); transition: filter .2s; }
.blur-hanzi .line:hover .hanzi, .blur-hanzi .line.reveal .hanzi { filter: none; }

/* ---------- Lesson lines ---------- */
.toolbar {
  position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 10;
  background: var(--bg); padding: 8px 0 10px; margin-bottom: 6px; border-bottom: 1px solid var(--line);
}
.toolbar [data-status]:not(:empty) { margin-top: 6px; }
details.opts { padding: 10px 14px; }
details.opts summary { font-size: .92rem; }
.lines { display: flex; flex-direction: column; gap: 10px; }
.line { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; }
.line.playing { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.line .who {
  grid-row: span 3; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: .85rem; background: var(--surface-2); color: var(--muted);
}
.line .who.B { background: var(--accent-soft); color: var(--accent); }
.line .who.none { visibility: hidden; width: 0; }
.line .actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.line .result { grid-column: 1 / -1; }

/* ---------- Feedback ---------- */
.feedback { margin-top: 10px; padding: 12px; border-radius: 12px; background: var(--surface-2); }
.feedback .score { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.feedback .score.good { color: var(--ok); }
.feedback .score.mid { color: var(--warn); }
.feedback .score.low { color: var(--bad); }
.diff { font-family: var(--font-zh); font-size: calc(1.5rem * var(--zh-scale)); line-height: 2.1; display: flex; flex-wrap: wrap; gap: 2px; }
.diff .c { display: inline-flex; flex-direction: column; align-items: center; padding: 0 3px; border-radius: 6px; min-width: 1.6em; }
.diff .c small { font-family: var(--font-vi); font-size: .62rem; line-height: 1.2; color: var(--muted); }
.diff .ok { background: var(--ok-soft); color: var(--ok); }
.diff .tone { background: var(--warn-soft); color: var(--warn); }
.diff .wrong { background: var(--bad-soft); color: var(--bad); }
.diff .miss { background: var(--bad-soft); color: var(--bad); opacity: .65; text-decoration: line-through; }
.diff .punct { color: var(--muted); min-width: auto; }
.heard { font-family: var(--font-zh); font-size: 1.1rem; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: .78rem; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.tips { margin: 8px 0 0; padding-left: 18px; font-size: .9rem; }
.tips li { margin-bottom: 4px; }
.listening-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--bad); margin-right: 6px; animation: pulse 1.2s infinite; }

/* ---------- Vocab ---------- */
.vocab-list { display: grid; gap: 8px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .vocab-list { grid-template-columns: 1fr 1fr; } }
.vitem { display: flex; gap: 10px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.vitem .body { flex: 1; min-width: 0; }
.vitem .hanzi { font-size: calc(1.25rem * var(--zh-scale)); }
.vitem .btns { display: flex; gap: 4px; flex: none; }
.vitem.known { border-left: 4px solid var(--ok); }
.star { color: var(--muted); }
.star.on { color: var(--warn); }

.flash {
  min-height: 260px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  cursor: pointer; padding: 24px 16px; gap: 6px; user-select: none; -webkit-user-select: none;
}
.flash .hint { font-size: .8rem; color: var(--muted); margin-top: 10px; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 8px 0 14px; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .25s; }

/* ---------- Quiz ---------- */
.options { display: grid; gap: 10px; grid-template-columns: 1fr; margin-top: 14px; }
@media (min-width: 600px) { .options { grid-template-columns: 1fr 1fr; } }
.option {
  text-align: left; padding: 12px 14px; min-height: 54px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: .98rem; overflow-wrap: anywhere;
}
.option:hover { border-color: var(--primary); }
.option.correct { background: var(--ok-soft); border-color: var(--ok); }
.option.incorrect { background: var(--bad-soft); border-color: var(--bad); }

/* ---------- Audio player ---------- */
.audio-list { display: grid; gap: 8px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .audio-list { grid-template-columns: 1fr 1fr; } }
@media (min-width: 900px) { .audio-list { grid-template-columns: 1fr 1fr 1fr; } }
.aitem { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; text-align: left; width: 100%; min-height: 56px; }
.aitem.active { border-color: var(--accent); background: var(--accent-soft); }
.aitem .t { font-weight: 600; }
.aitem .s { font-size: .85rem; color: var(--muted); }
.player { position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 9; margin-bottom: 14px; }
.player .now { font-weight: 600; margin-bottom: 6px; overflow-wrap: anywhere; }
.player .time { font-variant-numeric: tabular-nums; font-size: .85rem; color: var(--muted); min-width: 92px; text-align: right; }
.ab-badge { font-size: .8rem; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); }

/* ---------- Misc ---------- */
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.notice { padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); font-size: .92rem; margin-bottom: 12px; }
.notice.info { background: var(--surface-2); }
.notice.bad { background: var(--bad-soft); }
.badge { display: inline-block; font-size: .72rem; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-weight: 600; }
.badge.lv1 { background: var(--ok-soft); color: var(--ok); }
.badge.lv2 { background: var(--warn-soft); color: var(--warn); }
.badge.lv3 { background: var(--bad-soft); color: var(--bad); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.center { text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--bottomnav-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #1f2a37; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: .9rem; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (min-width: 900px) { .toast { bottom: 24px; } }
.mistake { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); flex-wrap: wrap; }
.mistake .body { flex: 1 1 200px; min-width: 0; }
details.card summary { cursor: pointer; font-weight: 600; min-height: 32px; }
kbd { font-family: inherit; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: .85em; }

@media (max-width: 480px) {
  .line { padding: 10px 12px; }
  .line .actions { display: grid; grid-template-columns: auto auto 1fr auto; }
  .line .actions .btn:not(.accent) span { display: none; }
  .line .actions .btn:not(.accent) { width: 40px; padding: 0; }
  .line .actions .badge { grid-column: 1 / -1; justify-self: start; }
  .toolbar .btn-row { flex-wrap: nowrap; }
  .toolbar .btn { padding: 8px 10px; flex: none; }
  .toolbar .btn.primary { flex: 1 1 auto; min-width: 0; }
  .toolbar .btn.primary span { overflow: hidden; text-overflow: ellipsis; }
  .toolbar .btn.iconish span { display: none; }
  .toolbar .btn.iconish { width: 44px; padding: 0; }
  .bottomnav a { font-size: .66rem; }
}
@media (max-width: 380px) {
  .brand-sub { display: none; }
  .stat b { font-size: 1.2rem; }
  .btn { padding: 8px 12px; }
  .hanzi.xxl { font-size: calc(2.6rem * var(--zh-scale)); }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
