:root {
  --bg: #f7f3ec;
  --surface: #ffffff;
  --surface-2: #f0ebe1;
  --text: #1f1d1a;
  --muted: #6f6a62;
  --border: #e5dfd3;
  --accent: #c8102e;          /* Chinese red */
  --accent-soft: #fbe9e7;
  --me: #2f6fde;
  --me-text: #ffffff;
  --them: #ffffff;
  --teacher: #fff8e6;
  --sel: #f5c518;
  --ok: #1f9d55;
  --mid: #d98a00;
  --bad: #d64545;
  --t1: #d62828;  /* high flat */
  --t2: #1f9d55;  /* rising */
  --t3: #2f6fde;  /* low */
  --t4: #8e44ad;  /* falling */
  --t0: #8a8a8a;  /* neutral */
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17161a; --surface: #211f24; --surface-2: #2a272e; --text: #ecebee; --muted: #a29ea8;
    --border: #35313a; --accent: #ff5a6e; --accent-soft: #3a2228; --me: #3a6fd8; --them: #262429;
    --teacher: #2e2a20; --sel: #f2cf4a; --ok: #4cc77f; --mid: #f0a93a; --bad: #ff7676;
    --t1: #ff6b6b; --t2: #4cd18a; --t3: #74a6ff; --t4: #c58af0; --t0: #9a9a9a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17161a; --surface: #211f24; --surface-2: #2a272e; --text: #ecebee; --muted: #a29ea8;
  --border: #35313a; --accent: #ff5a6e; --accent-soft: #3a2228; --me: #3a6fd8; --them: #262429;
  --teacher: #2e2a20; --sel: #f2cf4a; --ok: #4cc77f; --mid: #f0a93a; --bad: #ff7676;
  --t1: #ff6b6b; --t2: #4cd18a; --t3: #74a6ff; --t4: #c58af0; --t0: #9a9a9a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "PingFang SC", "Noto Sans SC", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.topbar-title { flex: 1; min-width: 0; color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; font-size: 22px; padding: 4px 6px; cursor: pointer; color: var(--muted); }
.container { max-width: 760px; margin: 0 auto; padding: 16px 16px 90px; }

/* ---------- pinyin tone colours ---------- */
.pinyin .t1, .t1 { color: var(--t1); }
.pinyin .t2, .t2 { color: var(--t2); }
.pinyin .t3, .t3 { color: var(--t3); }
.pinyin .t4, .t4 { color: var(--t4); }
.pinyin .t0, .t0 { color: var(--t0); }
.no-tones .pinyin span { color: inherit; }
.pinyin { font-weight: 600; letter-spacing: .01em; }
.pychip { background: var(--surface-2); border-radius: 6px; padding: 0 5px; white-space: nowrap; }
.tone-key { display: flex; gap: 12px; font-weight: 700; font-size: 14px; }

/* ---------- Home ---------- */
.hero h1 { font-size: 28px; line-height: 1.2; margin: 8px 0 6px; }
.hero p { margin: 0 0 14px; color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; font-size: 13px; color: var(--muted); }
.stat b { font-size: 18px; display: block; color: var(--text); }
.stat.link:active { transform: scale(.98); }
.continue {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--accent); color: #fff; border-radius: 14px; padding: 14px 18px; font-weight: 700; font-size: 18px; margin-bottom: 8px;
}
.category { margin-top: 24px; }
.category h2 { font-size: 20px; margin: 0 0 2px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.category h2 small { font-size: 13px; color: var(--muted); font-weight: 500; }
.category > p { margin: 0 0 10px; color: var(--muted); font-size: 15px; }
.cards { display: grid; gap: 8px; }
.card {
  display: flex; align-items: center; gap: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px; color: var(--text);
}
.card:active { transform: scale(.99); }
.card.next { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card.missing { opacity: .55; }
.card .num { width: 30px; height: 30px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); font-size: 13px; font-weight: 700; color: var(--muted); }
.card.done .num { background: var(--ok); color: #fff; }
.card .body { flex: 1; min-width: 0; }
.card .title { font-weight: 650; }
.card .meta { font-size: 14px; color: var(--muted); line-height: 1.35; }

/* ---------- Lesson ---------- */
.convo-head h1 { font-size: 26px; line-height: 1.2; margin: 4px 0 4px; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 700; margin-top: 10px; }
.sub { margin: 0 0 8px; }
.goals { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 10px 14px; font-size: 15px; margin: 8px 0; }
.goals ul { margin: 4px 0 0; padding-left: 20px; }
.lesson-thread { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }

/* teacher messages */
.msg { display: flex; gap: 8px; align-items: flex-end; max-width: 92%; }
.msg.cont { margin-top: -6px; }
.avatar { width: 30px; height: 30px; flex: none; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 15px; font-weight: 700; }
.avatar.ghost { background: none; }
.tbubble { background: var(--teacher); border: 1px solid var(--border); border-radius: var(--radius); border-bottom-left-radius: 6px; padding: 9px 14px; }

.tip { display: flex; gap: 10px; background: var(--surface); border: 1px dashed var(--border); border-radius: 14px; padding: 10px 14px; font-size: 15px; }
.tip-icon { font-size: 20px; line-height: 1.3; }
.tip-title { display: block; }

.card-block { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 12px 14px; }
.card-block h3 { margin: 0 0 8px; font-size: 16px; }
.tablewrap { overflow-x: auto; }
table.ref, table.vt { border-collapse: collapse; width: 100%; font-size: 15px; }
table.ref th, table.ref td, table.vt th, table.vt td { border-bottom: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
table.ref th, table.vt th { font-size: 13px; color: var(--muted); font-weight: 600; }

/* sound card */
.sound-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; flex-wrap: wrap; }
.sound-label { font-size: 26px; font-weight: 800; color: var(--accent); }
.sound-body { display: grid; grid-template-columns: 150px 1fr; gap: 12px; align-items: start; }
@media (max-width: 520px) { .sound-body { grid-template-columns: 1fr; } .sound-body .mouth { max-width: 220px; margin: 0 auto; } }
.sound dl { margin: 0; display: grid; gap: 6px; }
.srow { display: grid; grid-template-columns: 112px 1fr; gap: 8px; font-size: 15px; }
@media (max-width: 400px) { .srow { grid-template-columns: 1fr; gap: 0; } }
.srow dt { font-weight: 650; color: var(--muted); font-size: 14px; }
.srow dd { margin: 0; }
.mouth { width: 100%; height: auto; background: var(--surface-2); border-radius: 12px; }
.mouth .face { fill: none; stroke: var(--muted); stroke-width: 2.5; stroke-linecap: round; }
.mouth .roof { fill: none; stroke: var(--text); stroke-width: 3; stroke-linecap: round; }
.mouth .velum-line { fill: none; stroke: var(--muted); stroke-width: 3; stroke-dasharray: 4 3; }
.mouth .teeth { fill: var(--surface); stroke: var(--muted); stroke-width: 1.5; }
.mouth .tongue { fill: #e57373; opacity: .85; }
.mouth .contact { fill: none; stroke: var(--sel); stroke-width: 3; animation: pulse 1.4s ease-in-out infinite; }
.mouth .mlabel { font-size: 10px; fill: var(--muted); }
@keyframes pulse { 50% { stroke-width: 6; opacity: .6; } }

/* tone chart */
.tonechart { width: 100%; max-width: 520px; display: block; margin: 0 auto; }
.tonechart .grid { stroke: var(--border); }
.tonechart .axis { font-size: 11px; fill: var(--muted); text-anchor: end; }
.tonechart .tlabel { font-size: 15px; font-weight: 700; text-anchor: middle; }
.tlabel.t1 { fill: var(--t1); } .tlabel.t2 { fill: var(--t2); } .tlabel.t3 { fill: var(--t3); } .tlabel.t4 { fill: var(--t4); }
.tline { fill: none; stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; }
.tline.t1 { stroke: var(--t1); } .tline.t2 { stroke: var(--t2); } .tline.t3 { stroke: var(--t3); } .tline.t4 { stroke: var(--t4); }
.tline.half { stroke-dasharray: 6 6; stroke-width: 3; opacity: .7; }
.tonelegend { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 8px 0; }
.tonebtn b { color: inherit; }
.tonehint { width: 100%; text-align: center; font-size: 13px; color: var(--muted); }
table.vt td:first-child { font-weight: 650; white-space: nowrap; }

/* blocks of items */
.block { margin-top: 6px; }
.block-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.block-head h3 { font-size: 18px; margin: 8px 0 4px; }
.block-note { margin: 0 0 8px; color: var(--muted); font-size: 15px; }
.items { display: grid; gap: 8px; }
.item {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 9px 40px 9px 14px;
  cursor: pointer; -webkit-tap-highlight-color: transparent; transition: box-shadow .12s;
}
.item:active { transform: scale(.99); }
.item.sel, .bubble.sel, .bubble.playing { box-shadow: 0 0 0 3px var(--sel); }
.item .badge { position: absolute; right: 12px; top: 10px; color: var(--ok); font-weight: 800; }
.py-line { font-size: 21px; line-height: 1.35; }
.py-line.big { font-size: 25px; }
.hz { font-size: 15px; color: var(--muted); font-family: "PingFang SC", "Noto Sans SC", sans-serif; }
.no-hz .item .hz, .no-hz .b-hz { display: none; }
.meta-line { font-size: 15px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.meta-line .vi { color: var(--muted); }
.meta-line .vi::before { content: "🇻🇳 "; font-size: 12px; }
.meta-line .hv { font-size: 13px; color: var(--accent); background: var(--accent-soft); border-radius: 6px; padding: 0 6px; }
.no-en .item:not(.recall) .en, .no-en .b-en { display: none; }
.no-vi .meta-line .vi, .no-vi .meta-line .hv, .no-vi .vi-line { display: none; }
.lit, .note { font-size: 14px; color: var(--muted); }
.item.recall .prompt { font-size: 18px; font-weight: 600; }
.vi-line { font-size: 14px; color: var(--muted); }
.from { font-size: 12px; display: inline-block; margin-top: 2px; }
.slot { width: 100%; }

.pairs { display: grid; gap: 8px; }
.pair-row { display: grid; grid-template-columns: 1fr auto 1fr; gap: 6px; align-items: stretch; }
.pair-row .item { padding-right: 30px; }
.pair-row .item .py-line { font-size: 19px; }
.vs { align-self: center; font-size: 12px; color: var(--muted); font-weight: 700; }

.pattern .frame { background: var(--accent-soft); border-radius: 12px; padding: 8px 12px; margin: 2px 0 8px; font-size: 20px; }
.frame-en { font-size: 15px; color: var(--muted); }

/* ---------- dialogue ---------- */
.setting { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; font-size: 15px; color: var(--muted); margin: 4px 0 8px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 12px; }
.thread { display: flex; flex-direction: column; gap: 4px; }
.turn { display: flex; flex-direction: column; align-items: flex-start; }
.turn.right { align-items: flex-end; }
.who { font-size: 12px; color: var(--muted); margin: 8px 12px 2px; }
.bubble {
  max-width: 88%; background: var(--them); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius); border-bottom-left-radius: 6px; padding: 8px 14px; cursor: pointer;
  -webkit-tap-highlight-color: transparent; transition: box-shadow .12s;
}
.turn.right .bubble { background: color-mix(in srgb, var(--me) 14%, var(--surface)); color: var(--text); border-color: color-mix(in srgb, var(--me) 45%, transparent); border-radius: var(--radius); border-bottom-right-radius: 6px; }
.b-py { font-size: 19px; line-height: 1.35; }
.b-hz { font-size: 14px; opacity: .7; }
.b-en { font-size: 14px; opacity: .8; margin-top: 2px; }
.turn.right .pinyin { text-shadow: none; }
.b-note { max-width: 88%; font-size: 13px; color: var(--muted); margin: 2px 8px; }
.b-hidden { display: none; }
.roleplay .turn.right .b-py, .roleplay .turn.right .b-hz, .roleplay .turn.right .b-en { display: none; }
.roleplay .turn.right .b-hidden { display: block; }
.b-prompt { font-weight: 600; }
.btn.on { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ---------- practice panel ---------- */
.practice {
  width: 100%; margin: 6px 0 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 10px; box-shadow: 0 4px 18px rgba(0,0,0,.08); color: var(--text); text-align: left;
}
.answer { display: none; padding: 2px 4px 6px; }
.answer.show { display: block; }
.turn .practice .answer, .item-wrap .practice .answer:not(.recall-a) { }
.practice textarea {
  width: 100%; min-height: 56px; resize: vertical; font: inherit; font-size: 20px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; margin-top: 8px;
}
.practice textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.practice .row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.practice .spacer { flex: 1; }
.reveal-row { margin-bottom: 6px; }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 7px 13px; font-size: 15px;
  cursor: pointer; white-space: nowrap; color: var(--text);
}
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.big { padding: 10px 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; }
.btn.danger { color: var(--bad); }
.btn.mic { font-weight: 650; }
.btn.mic.rec { background: var(--bad); border-color: var(--bad); color: #fff; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .7; } }
.btn:active { transform: scale(.97); }
.btn.done-on { background: var(--ok); border-color: var(--ok); color: #fff; }

.result { margin-top: 8px; display: grid; gap: 6px; }
.result:empty { display: none; }
.score { font-size: 16px; }
.score b { font-size: 20px; }
.score.sok b { color: var(--ok); } .score.smid b { color: var(--mid); } .score.slow b { color: var(--bad); }
.syls { display: flex; flex-wrap: wrap; gap: 4px; }
.sylchip { display: inline-flex; flex-direction: column; align-items: center; border-radius: 8px; padding: 2px 8px; font-weight: 700; font-size: 17px; line-height: 1.2; }
.sylchip sub { font-size: 11px; font-weight: 500; }
.sylchip.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.sylchip.tone { background: color-mix(in srgb, var(--mid) 20%, transparent); color: var(--mid); }
.sylchip.miss { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.heard { font-size: 14px; display: flex; flex-wrap: wrap; gap: 2px 6px; align-items: baseline; }
.heardsyl { display: inline-flex; flex-direction: column; align-items: center; line-height: 1.15; }
.heardsyl .hpy { font-weight: 600; }
.heardsyl .hhz { font-size: 12px; color: var(--muted); }
.heardsyl.extra { color: var(--muted); text-decoration: line-through; }
.no-hz .heardsyl .hhz { display: none; }

/* ---------- misc ---------- */
.search { width: 100%; font: inherit; font-size: 17px; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text); margin: 8px 0 12px; }
.convo-foot { display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; align-items: center; margin-top: 30px; padding-top: 16px; border-top: 1px solid var(--border); }

dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text); padding: 0; width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.settings-form { padding: 18px; display: grid; gap: 12px; }
.settings-form h2 { margin: 0; font-size: 20px; }
.settings-form label { display: grid; gap: 6px; font-size: 15px; }
.settings-form label.check { grid-template-columns: auto 1fr; align-items: start; gap: 10px; }
.settings-form select { font: inherit; font-size: 16px; padding: 6px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); max-width: 100%; }
.settings-actions { display: flex; justify-content: space-between; gap: 8px; }
