:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f3eee7;
  --text: #1f1a17;
  --muted: #6b625b;
  --border: #e6ded4;
  --accent: #b5452b;
  --accent-text: #ffffff;
  --accent-soft: #f6e3dc;
  --listen: #2f6f8f;
  --speak: #b5452b;
  --grade: #6a5a9e;
  --ok: #2e7d4f;
  --warn-bg: #fff3d6;
  --warn-text: #6b4a00;
  --radius: 14px;
  --tabbar-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171412;
    --surface: #221e1b;
    --surface-2: #2b2622;
    --text: #f1ebe4;
    --muted: #a99f96;
    --border: #3a332e;
    --accent: #e0714f;
    --accent-text: #1a0f0b;
    --accent-soft: #3d2620;
    --listen: #6fb3d4;
    --speak: #e0714f;
    --grade: #a998e0;
    --ok: #6cc893;
    --warn-bg: #3a2f14;
    --warn-text: #f3d68a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main {
  max-width: 560px;
  margin: 0 auto;
  padding: 20px 16px calc(var(--tabbar-h) + 28px + env(safe-area-inset-bottom));
}
h1 { font-size: 1.6rem; line-height: 1.2; margin: 4px 0 16px; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; margin: 0 0 8px; }
p { margin: 8px 0; }
a { color: var(--accent); }
code { font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.center { text-align: center; }
.view[hidden] { display: none; }

/* ---------- building blocks ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 12px 0;
}
.card-title { font-size: 1rem; font-weight: 600; margin: 0 0 8px; }
summary { cursor: pointer; font-weight: 600; }
details[open] > summary { margin-bottom: 8px; }
details.small summary { font-weight: 500; color: var(--accent); }

button {
  font: inherit;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}
button:hover { border-color: var(--muted); }
button:active { transform: scale(0.98); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--accent); }
button.big { flex: 1; padding: 16px; font-weight: 600; }
button.link {
  min-height: 0; padding: 4px 2px; border: none; background: none;
  color: var(--accent); text-decoration: underline; text-underline-offset: 3px;
}
button.link:active { transform: none; }
:focus-visible { outline: 3px solid var(--listen); outline-offset: 2px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.row.tight { margin-top: 8px; gap: 16px; }
kbd { font: 0.75em ui-monospace, monospace; border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }

.field { display: block; margin: 14px 0; }
.field output { color: var(--muted); float: right; }
.field select, .field input[type=range] { display: block; width: 100%; margin-top: 6px; }
input[type=text], input[type=search], input[type=password], select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; min-height: 44px;
}
.field input[type=text] { display: block; width: 100%; margin-top: 6px; }
.row input[type=password] { flex: 1; min-width: 0; }
.check { display: flex; gap: 10px; align-items: center; margin: 12px 0; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.warn { background: var(--warn-bg); color: var(--warn-text); padding: 10px 12px; border-radius: 10px; }

/* ---------- setup ---------- */
.brand { text-align: center; margin: 24px 0 8px; }
.brand h1 { margin-bottom: 4px; }
.stack-buttons { display: grid; gap: 8px; }
.divider-label { color: var(--muted); font-size: 0.875rem; text-align: center; margin: 16px 0 0; }

/* ---------- today ---------- */
.eyebrow { color: var(--muted); font-size: 0.875rem; margin: 8px 0 0; }
.next-title { font-size: 1.5rem; margin: 4px 0 18px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats div { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 6px; text-align: center; }
.stats b { display: block; font-size: 1.6rem; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stats span { font-size: 0.78rem; color: var(--muted); }
.start { display: block; width: 100%; margin-top: 18px; padding: 20px; font-size: 1.2rem; border-radius: 16px; }
.chip {
  display: block; margin: 16px auto 0; min-height: 36px; padding: 6px 14px;
  border-radius: 999px; font-size: 0.85rem; color: var(--muted); background: var(--surface);
}
.chip:empty { display: none; }

/* ---------- progress ---------- */
.unit-row { padding: 10px 0; border-top: 1px solid var(--border); }
.unit-row:first-child { border-top: none; }
.unit-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; font-size: 0.92rem; }
.unit-head span:first-child { flex: 1; }
.unit-head .link { font-size: 0.82rem; }
.stack { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--surface-2); margin-top: 6px; }
.stack i, .legend i { display: block; height: 100%; }
.mastered { background: var(--ok); }
.known { background: var(--listen); }
.learning { background: var(--speak); opacity: 0.7; }
.legend { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--muted); margin-top: 0; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-left: 6px; }
.activity { display: flex; align-items: flex-end; gap: 4px; height: 64px; margin-top: 4px; }
.activity .day { flex: 1; background: var(--ok); border-radius: 3px 3px 0 0; }
.activity .day.empty { background: var(--surface-2); }

/* ---------- words ---------- */
#vocabSearch { display: block; width: 100%; }
#vocabList details { border-top: 1px solid var(--border); padding: 10px 0; }
#vocabList summary { display: flex; align-items: center; gap: 8px; list-style: none; }
#vocabList summary::-webkit-details-marker { display: none; }
#vocabList summary::before { content: "▸"; color: var(--muted); width: 12px; }
#vocabList details[open] summary::before { content: "▾"; }
#vocabList summary .unit-name { flex: 1; }
.study-next { min-height: 32px; padding: 4px 10px; font-size: 0.8rem; font-weight: 500; border-radius: 999px; }
.study-next.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.vocab-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; }
.vocab-row .play { width: 44px; height: 44px; min-height: 44px; padding: 0; border-radius: 50%; flex: none; }
.vocab-text { flex: 1; min-width: 0; }
.vocab-rom { display: block; font-style: italic; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--surface-2); border: 1px solid var(--border); }
.dot.mastered, .dot.known, .dot.learning { border: none; }

/* ---------- more ---------- */
.qr { background: #fff; padding: 8px; border-radius: 10px; width: min(260px, 100%); margin: 8px 0; }
.qr svg { display: block; width: 100%; height: auto; }

/* ---------- session ---------- */
#session:not([hidden]) { display: flex; flex-direction: column; min-height: calc(100dvh - 48px); }
.session-bar { display: flex; align-items: center; gap: 10px; }
button.icon { min-width: 44px; padding: 8px 10px; border-radius: 12px; font-size: 1.05rem; line-height: 1; }
button.icon.wide { font-size: 0.85rem; white-space: nowrap; }
.progress { flex: 1; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
#bar { height: 100%; width: 0; background: var(--ok); transition: width 0.4s; }
#unit { margin: 10px 0 0; min-height: 1.3em; }
.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 8px 0; }
#orb {
  --c: var(--listen);
  width: 180px; height: 180px;
  border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  border: 3px solid var(--c); color: var(--c);
  font-weight: 600; text-align: center; padding: 16px;
  transition: border-color 0.3s, color 0.3s;
}
#orb[data-stage="speak"] { --c: var(--speak); animation: pulse 1.4s ease-in-out infinite; }
#orb[data-stage="grade"] { --c: var(--grade); }
#orb[data-stage="paused"] { --c: var(--muted); }
#orb.mic { border-style: dashed; }
#orb .icon::before { font-size: 2.4rem; content: "👂"; }
#orb[data-stage="speak"] .icon::before { content: "🗣️"; }
#orb[data-stage="grade"] .icon::before { content: "⭐"; }
#orb[data-stage="paused"] .icon::before { content: "⏸️"; }
@keyframes pulse { 50% { transform: scale(1.04); } }
@media (prefers-reduced-motion: reduce) { #orb { animation: none !important; } }
.prompt { text-align: center; font-size: 1.35rem; min-height: 1.9em; margin: 18px 0 0; }
.peek { text-align: center; color: var(--muted); font-style: italic; min-height: 1.5em; margin: 2px 0; }
.feedback { text-align: center; min-height: 1.5em; margin: 2px 0; font-weight: 600; }
.links { display: flex; gap: 16px; align-items: center; justify-content: center; color: var(--muted); margin: 4px 0; }
.bottombar {
  position: sticky; bottom: 0;
  display: grid; gap: 8px;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
}
.bottombar[hidden] { display: none; }
.bottombar button { min-height: 60px; font-size: 1.05rem; border-radius: 16px; }
.grades { grid-template-columns: 1fr 1.4fr 1fr; }
.row.split > button { flex: 1; }
#sessionWordsBox { margin-top: 8px; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar[hidden] { display: none; }
.tabbar button {
  flex: 1; max-width: 140px; height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: none; border-radius: 0; background: none; color: var(--muted); font-size: 0.75rem;
}
.tabbar button span { font-size: 1.3rem; line-height: 1; }
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tabbar button:active { transform: none; }
button.icon.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.section-title { margin-top: 24px; }
.lesson-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.lesson-row:first-child { border-top: none; }
.lesson-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.lesson-row.done b { color: var(--muted); font-weight: 500; }

.lyrics { margin-top: 12px; }
.section-name { margin: 12px 0 2px; }
.lyric-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; }
.lyric-row .play { width: 44px; height: 44px; min-height: 44px; padding: 0; border-radius: 50%; flex: none; }
.lyric-text { flex: 1; min-width: 0; }
.lyric-text .thai { font-size: 1.15rem; }
.video { position: relative; width: 100%; aspect-ratio: 16 / 9; margin: 12px 0; border-radius: 12px; overflow: hidden; background: #000; }
.video iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.link-btn { display: inline-flex; align-items: center; min-height: 44px; padding: 10px 16px; border: 1px solid var(--border); border-radius: 12px; text-decoration: none; }
.song details { margin-top: 12px; }

.level-head { margin: 18px 0 4px; font-size: 0.8rem; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); }

/* A2 material sits on a slightly lighter background (90% of the page colour). */
:root { --bg-a2: color-mix(in srgb, var(--bg) 90%, #fff); }
#vocabList details.level-A2, .lesson-row.level-A2 { background: var(--bg-a2); border-radius: 12px; padding-left: 10px; padding-right: 10px; }
#vocabList details.level-A2 { margin: 6px 0; border-top: none; }
.lesson-row.level-A2 { margin: 6px 0; border-top: none; }
body[data-level="A2"] { background: var(--bg-a2); }
body[data-level="A2"] .bottombar { background: var(--bg-a2); }


button.link.who { color: inherit; font-size: inherit; text-decoration: none; border-bottom: 1px dashed var(--muted); padding: 0; }
button.link.who::after { content: ' ▾'; color: var(--muted); }

.my-link { font-size: 1.05rem; word-break: break-all; margin: 8px 0 0; }
#backupHelp { margin-top: 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; }
#backupHelp ol { padding-left: 20px; }
