:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --line: #e2ddd2;
  --text: #1f2421;
  --muted: #69706b;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d8efec;
  --warn: #b45309;
  --warn-soft: #fdf0dc;
  --good: #15803d;
  --bad: #b91c1c;
  --teacher: #ffffff;
  --learner: #dff1ee;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 16px rgb(0 0 0 / 0.05);
  /* tones: colour plus a contour icon, so colour is never the only cue */
  --tone-mid: #5b6470;
  --tone-low: #1d5fd1;
  --tone-falling: #c62828;
  --tone-high: #0b7d4f;
  --tone-rising: #7b3fd6;
  --radius: 14px;
  --thai: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Thonburi", sans-serif;
  --ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121614; --surface: #1b201e; --surface-2: #232927; --line: #313835; --text: #e9ece9; --muted: #9aa49f;
    --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #163a36; --warn: #f59e0b; --warn-soft: #3a2a10;
    --good: #4ade80; --bad: #f87171; --teacher: #1b201e; --learner: #173430;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    --tone-mid: #aab3bd; --tone-low: #6ea8ff; --tone-falling: #ff7a7a; --tone-high: #4fd69c; --tone-rising: #c29bff;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121614; --surface: #1b201e; --surface-2: #232927; --line: #313835; --text: #e9ece9; --muted: #9aa49f;
  --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #163a36; --warn: #f59e0b; --warn-soft: #3a2a10;
  --good: #4ade80; --bad: #f87171; --teacher: #1b201e; --learner: #173430;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  --tone-mid: #aab3bd; --tone-low: #6ea8ff; --tone-falling: #ff7a7a; --tone-high: #4fd69c; --tone-rising: #c29bff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--ui); -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

#app { display: grid; grid-template-rows: auto 1fr auto; height: 100dvh; max-width: 1100px; margin: 0 auto; }
.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand-mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); font: 600 13px var(--thai); }
.top h1 { flex: 1; margin: 0; font-size: 15px; font-weight: 500; color: var(--muted); text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 10px; background: transparent; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.tabs { display: flex; justify-content: space-around; border-top: 1px solid var(--line); background: var(--surface); padding-bottom: env(safe-area-inset-bottom); order: 3; }
.tab { position: relative; flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 6px; border: 0; background: none; color: var(--muted); font-size: 11px; font-weight: 500; }
.tab[aria-current="page"] { color: var(--accent); }
.badge { position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--warn); color: #fff; font-size: 11px; line-height: 18px; }

#screen { overflow-y: auto; padding: 4px 16px 24px; outline: none; }
#screen.flush { padding: 0; overflow: hidden; }

@media (min-width: 900px) {
  #app { grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr; }
  .top { grid-column: 1 / -1; }
  .tabs { order: 0; flex-direction: column; justify-content: flex-start; gap: 4px; border: 0; background: transparent; padding: 8px; }
  .tab { flex: 0; flex-direction: row; gap: 10px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
  .tab[aria-current="page"] { background: var(--accent-soft); }
  .badge { position: static; margin-left: auto; }
  #screen { padding: 4px 24px 32px; }
  #screen.flush { padding: 0; }
}

/* building blocks */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stack { display: grid; gap: 12px; }
.stack > * { min-width: 0; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
h2 { font-size: 20px; margin: 8px 0 4px; }
h3 { font-size: 15px; margin: 0 0 6px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); font-weight: 500; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn svg { width: 18px; height: 18px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-size: 13px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 12px; font-size: 13px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.error { color: var(--bad); }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.field input, label.field select { width: 100%; min-width: 0; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); font-size: 15px; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }

/* Thai text: each written syllable is coloured by its tone */
.th { font-family: var(--thai); }
.thai-line { font-family: var(--thai); font-size: 23px; line-height: 1.55; }
.thai-line.big { font-size: 34px; }
.thai-line.mid { font-size: 19px; }
.w { cursor: pointer; border-radius: 4px; padding: 0 1px; }
.w:hover { background: var(--surface-2); }
.notone .w .s { color: inherit !important; }
.s.mid { color: var(--tone-mid); } .s.low { color: var(--tone-low); } .s.falling { color: var(--tone-falling); }
.s.high { color: var(--tone-high); } .s.rising { color: var(--tone-rising); }
.roman { color: var(--muted); font-size: 14px; letter-spacing: 0.01em; }
.english { font-size: 15px; }
.reveal { border: 0; background: none; padding: 0; color: var(--accent); font-size: 13px; }
.note { font-size: 13px; color: var(--muted); }
.tone-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }
.tone-icon { width: 28px; height: 18px; stroke-width: 2.2; }
.t-mid { color: var(--tone-mid); } .t-low { color: var(--tone-low); } .t-falling { color: var(--tone-falling); }
.t-high { color: var(--tone-high); } .t-rising { color: var(--tone-rising); }

/* talk */
.talk { display: grid; grid-template-rows: auto 1fr auto; height: 100%; min-height: 0; }
.talk .messages { min-height: 0; }
.talk-head { display: flex; align-items: center; gap: 8px; padding: 6px 16px 8px; border-bottom: 1px solid var(--line); }
.talk-head .title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; scroll-behavior: smooth; }
.msg { max-width: 640px; }
.msg.teacher { align-self: flex-start; width: min(100%, 640px); }
.msg.learner { align-self: flex-end; background: var(--learner); border-radius: var(--radius) var(--radius) 4px var(--radius); padding: 10px 14px; }
.bubble { background: var(--teacher); border: 1px solid var(--line); border-radius: 4px var(--radius) var(--radius) var(--radius); padding: 12px 14px; box-shadow: var(--shadow); display: grid; gap: 6px; }
.who { font-size: 12px; color: var(--muted); margin: 0 0 4px 2px; }
.play-row { display: flex; gap: 6px; align-items: center; }
.play { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); color: var(--accent); }
.play svg { width: 16px; height: 16px; fill: currentColor; stroke: none; }
.play.slow svg { fill: none; stroke: currentColor; }
.play.playing { background: var(--accent); color: var(--accent-ink); }
.explain { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.feedback { border-radius: 10px; padding: 8px 10px; display: grid; gap: 4px; font-size: 15px; background: var(--surface-2); border-left: 4px solid var(--muted); }
.feedback .verdict { font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; }
.feedback.v-good { border-color: var(--good); } .feedback.v-good .verdict { color: var(--good); }
.feedback.v-almost { border-color: var(--warn); } .feedback.v-almost .verdict { color: var(--warn); }
.feedback.v-wrong { border-color: var(--bad); } .feedback.v-wrong .verdict { color: var(--bad); }
.feedback.v-english { border-color: var(--accent); } .feedback.v-english .verdict { color: var(--accent); }
.feedback .play, .explain .play { width: 28px; height: 28px; flex: none; }
.task { border: 1px dashed var(--accent); border-radius: 10px; padding: 8px 10px; background: var(--accent-soft); }
.task .label { font-size: 12px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: 0.04em; }
.lesson-done { border-radius: 10px; padding: 10px; background: color-mix(in srgb, var(--good) 14%, var(--surface)); display: grid; gap: 8px; }
.teach { padding: 8px 0 4px; border-bottom: 1px solid var(--line); }
.teach:last-child { border-bottom: 0; }
.teach h4 { margin: 4px 0; font-size: 15px; }
.bar.stacked { display: flex; }
.bar.stacked > i.good { background: var(--good); } .bar.stacked > i.almost { background: var(--warn); } .bar.stacked > i.wrong { background: var(--bad); }
.correction { border-left: 3px solid var(--warn); background: var(--warn-soft); border-radius: 0 10px 10px 0; padding: 8px 10px; display: grid; gap: 4px; }
.correction .label { font-size: 12px; font-weight: 600; color: var(--warn); text-transform: uppercase; letter-spacing: 0.04em; }
.suggest { display: flex; flex-wrap: wrap; gap: 6px; }
.suggest button { display: grid; text-align: left; gap: 0; padding: 6px 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); max-width: 100%; }
.suggest button .th { font-size: 16px; }
.suggest button .small { color: var(--muted); }
.words { display: flex; flex-wrap: wrap; gap: 6px; }
.typing { display: inline-flex; gap: 4px; padding: 14px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; } .typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.dock { border-top: 1px solid var(--line); background: var(--surface); padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); display: grid; gap: 8px; }
.dock-row { display: flex; align-items: flex-end; gap: 8px; }
.dock textarea { flex: 1; resize: none; min-height: 44px; max-height: 140px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); font: 16px/1.4 var(--thai); }
.mic { position: relative; display: grid; place-items: center; width: 52px; height: 52px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); flex: none; }
.mic svg { width: 24px; height: 24px; }
.mic.listening { background: var(--bad); color: #fff; }
.mic.listening::after { content: ""; position: absolute; inset: -5px; border-radius: 50%; border: 2px solid var(--bad); animation: pulse 1.2s infinite; }
@keyframes pulse { from { transform: scale(0.9); opacity: 1; } to { transform: scale(1.25); opacity: 0; } }
.send { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); display: grid; place-items: center; color: var(--accent); flex: none; }
.dock-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.heard { font-family: var(--thai); color: var(--muted); font-size: 14px; }

.starts { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.start { text-align: left; display: grid; gap: 4px; padding: 16px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow); }
.start:hover { border-color: var(--accent); }
.start strong { font-size: 16px; }
.list { display: grid; gap: 8px; }
.list-item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-align: left; width: 100%; }

/* lessons */
.units { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.unit-card { text-align: left; display: grid; gap: 4px; padding: 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
.unit-card:hover { border-color: var(--accent); }
.unit-num { font-size: 12px; color: var(--muted); font-weight: 600; }
.done { color: var(--good); font-size: 13px; font-weight: 600; }
.phrase { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.phrase:last-child { border-bottom: 0; }
.phrase .actions { display: flex; gap: 6px; align-items: flex-start; }
.word-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.word { padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); display: grid; gap: 2px; }

/* quiz, review, tones */
.big-card { text-align: center; display: grid; gap: 12px; justify-items: center; padding: 24px 16px; }
.options { display: grid; gap: 8px; width: 100%; max-width: 480px; }
.option { padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-align: left; }
.option.right { border-color: var(--good); background: color-mix(in srgb, var(--good) 14%, var(--surface)); }
.option.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--surface)); }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; width: 100%; max-width: 480px; }
.grades .btn { flex-direction: column; gap: 0; font-size: 14px; }
.grades .btn small { font-size: 11px; color: var(--muted); font-weight: 400; }
.tone-cards { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.tone-card { display: grid; gap: 4px; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); }
.tone-card .name { font-weight: 600; display: flex; align-items: center; gap: 6px; text-transform: capitalize; }
.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.progress-strip { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; width: 100%; max-width: 480px; }
.progress-strip > i { display: block; height: 100%; background: var(--accent); transition: width 0.3s; }

/* today */
.today-head { display: flex; align-items: center; gap: 16px; }
.ring-box { position: relative; width: 112px; height: 112px; flex: none; }
.ring { width: 112px; height: 112px; }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 12; }
.ring-fill { fill: none; stroke: var(--accent); stroke-width: 12; stroke-linecap: round; transition: stroke-dasharray 0.6s; }
.ring-label { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; line-height: 1.1; }
.ring-label b { font-size: 28px; }
.ring-label span { font-size: 12px; color: var(--muted); }
.streak { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.plan { display: grid; gap: 6px; }
.plan-item { display: flex; align-items: center; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-align: left; width: 100%; }
.plan-item:hover { border-color: var(--accent); }
.plan-item .spacer { display: grid; gap: 2px; }
.plan-icon { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); flex: none; }
.plan-item.done { opacity: 0.7; }
.plan-item.done .plan-icon { background: var(--good); color: #fff; }
.plan-item.done b { text-decoration: line-through; text-decoration-thickness: 1px; }
.level { display: grid; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.level:last-child { border-bottom: 0; }
.level-tag { display: inline-grid; place-items: center; min-width: 34px; height: 24px; padding: 0 6px; border-radius: 6px; background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700; }
.level-head { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.path { display: flex; flex-wrap: wrap; gap: 8px; }
.stop { position: relative; width: 36px; height: 36px; border-radius: 50%; border: 2px solid var(--line); background: var(--surface); color: var(--muted); font-weight: 600; font-size: 13px; }
.stop.taught { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.stop.next { border-color: var(--accent); color: var(--accent); animation: glow 1.8s infinite; }
@keyframes glow { 0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 40%, transparent); } 50% { box-shadow: 0 0 0 6px transparent; } }
.badge-dot { position: absolute; right: -2px; top: -2px; width: 11px; height: 11px; border-radius: 50%; background: var(--warn); border: 2px solid var(--surface); }
.memory { padding: 4px 0; border-bottom: 1px solid var(--line); }
.burst { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.burst i { position: absolute; top: -12px; width: 8px; height: 14px; border-radius: 2px; animation: fall 2s ease-in forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(540deg); opacity: 0.6; } }
@media (prefers-reduced-motion: reduce) { .burst { display: none; } .stop.next { animation: none; } }

/* progress */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.stat b { display: block; font-size: 26px; line-height: 1.1; }
.stat span { font-size: 13px; color: var(--muted); }
.chart svg { width: 100%; height: 150px; }
.chart text { font-size: 10px; fill: var(--muted); stroke: none; }
.report { display: grid; gap: 8px; }
.report ul { margin: 0; padding-left: 20px; }

/* sheet (word details, settings) */
.backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / 0.35); z-index: 10; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 85dvh; overflow-y: auto; background: var(--surface); border-radius: 18px 18px 0 0; padding: 18px 18px max(18px, env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgb(0 0 0 / 0.2); }
@media (min-width: 700px) { .sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 460px; border-radius: 18px; } }
.sheet-close { position: absolute; top: 10px; right: 10px; }
.sheet > :nth-child(2) { padding-right: 40px; }
.syllables { display: flex; flex-wrap: wrap; gap: 8px; }
.syl { display: grid; justify-items: center; gap: 2px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); min-width: 64px; }
.syl .th { font-size: 22px; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 20; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); font-size: 14px; box-shadow: var(--shadow); }
