:root {
  --bg: #f7f4ef;
  --panel: #fffdf9;
  --ink: #1f1d1b;
  --muted: #857e75;
  --faint: #b3aca2;
  --line: #e6dfd4;
  --accent: #d6453d;
  --good: #2f8a5b;
  --ok: #c18512;
  --low: #c8442f;
  --ui: 'DM Sans', system-ui, sans-serif;
  --ipa: 'Gentium Book Plus', 'Charis SIL', 'Noto Serif', 'Times New Roman', serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 var(--ui); }
button { font: inherit; color: inherit; cursor: pointer; }
.ipa { font-family: var(--ipa); }
[hidden] { display: none !important; }

/* top bar */
.top { position: sticky; top: 0; z-index: 5; height: 56px; display: flex; align-items: center; justify-content: space-between; padding: 0 20px; background: var(--bg); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; letter-spacing: .02em; }
.grader { display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 6px 14px; font-size: 13px; font-weight: 500; }
.grader-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.grader.on .grader-dot { background: var(--good); }
.grader.off { border-color: var(--ink); background: var(--ink); color: #fff; }
.grader.off .grader-dot { background: var(--accent); }

/* layout */
.layout { display: grid; grid-template-columns: 200px minmax(0, 1fr) 320px; min-height: calc(100vh - 56px); }
.rail { border-right: 1px solid var(--line); padding: 16px 10px; position: sticky; top: 56px; height: calc(100vh - 56px); overflow-y: auto; }
.rail-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 8px 10px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.link { border: 0; background: none; padding: 0; font-size: 12px; color: var(--muted); text-decoration: underline; text-underline-offset: 2px; text-transform: none; letter-spacing: 0; font-weight: 500; }
.link.active { color: var(--ink); }
.sound-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.sound-btn { width: 100%; display: grid; grid-template-columns: 34px minmax(0, 1fr) auto; align-items: center; gap: 6px; border: 0; background: none; text-align: left; padding: 7px 8px; border-radius: 10px; }
.sound-btn:hover { background: #efe9e0; }
.sound-btn.active { background: var(--panel); box-shadow: 0 0 0 1px var(--line); }
.sound-glyph { font-family: var(--ipa); font-size: 24px; line-height: 1; text-align: center; }
.sound-name { font-size: 13px; font-weight: 600; line-height: 1.25; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sound-from { display: block; font-size: 11.5px; color: var(--muted); font-weight: 400; overflow: hidden; text-overflow: ellipsis; }
.badge { min-width: 30px; text-align: center; font-size: 12px; font-weight: 700; border-radius: 999px; padding: 2px 7px; background: #ece6dc; color: var(--muted); }
.badge.good { background: #e3f1e9; color: var(--good); }
.badge.ok { background: #f7ecd6; color: var(--ok); }
.badge.low { background: #f8e3df; color: var(--low); }

/* lesson */
.main { padding: 22px 28px 60px; min-width: 0; }
.lesson-head { display: flex; align-items: center; gap: 18px; margin-bottom: 10px; }
.glyph { font-size: 64px; line-height: 1; min-width: 72px; text-align: center; }
h1 { margin: 0; font-size: 26px; line-height: 1.15; letter-spacing: -.01em; }
.sub { margin: 4px 0 0; color: var(--muted); }
.lesson-body { display: grid; grid-template-columns: minmax(300px, 580px) minmax(210px, 1fr); gap: 24px; align-items: start; }
.stage { margin: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 12px 12px 14px; }
.stage svg { width: 100%; display: block; }
.mouth-host { position: relative; width: 100%; cursor: grab; border-radius: 12px; overflow: hidden; background: radial-gradient(120% 90% at 40% 45%, #fffdf9 0%, #f4eee6 100%); }
.mouth-host:active { cursor: grabbing; }
.mouth-host canvas { display: block; }
.mouth-host.flat { cursor: default; background: none; }
.mouth-host.flat .drag-hint { display: none; }
.drag-hint { position: absolute; right: 10px; bottom: 8px; font-size: 11px; color: var(--faint); pointer-events: none; z-index: 1; }
.m3d-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.m3d-overlay .lbl text { font: 600 11.5px var(--ui); fill: #6d655c; paint-order: stroke; stroke: rgba(255, 253, 249, .92); stroke-width: 4px; stroke-linejoin: round; }
.m3d-overlay .lbl line { stroke: #b9b0a5; stroke-width: 1; }
.m3d-overlay .lbl circle { fill: #6d655c; }
.m3d-overlay .lbl text.tongue { fill: #fff; stroke: none; font-weight: 700; letter-spacing: .08em; text-anchor: middle; opacity: .85; }
.m3d-overlay .arw-line { fill: none; stroke: var(--accent); stroke-width: 3; stroke-linecap: round; }
.m3d-overlay .ghost { fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 3 3; }
.m3d-overlay .arw-text { font: 700 13px var(--ui); fill: var(--accent); paint-order: stroke; stroke: rgba(255, 253, 249, .95); stroke-width: 5px; stroke-linejoin: round; }
.m3d-overlay .pulse { fill: none; stroke: var(--accent); stroke-width: 2; transform-box: fill-box; transform-origin: center; animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.35); opacity: .35; } }
.trick { margin: 18px 0 0; padding: 12px 14px; background: #f6efe6; border-radius: 12px; font-size: 15.5px; line-height: 1.45; }
.trick b { color: var(--accent); }
.steps { display: flex; gap: 6px; margin: 4px 2px 10px; }
.step { flex: 1; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 7px 10px; text-align: left; font-size: 13px; line-height: 1.3; transition: background .12s, border-color .12s, color .12s; }
.step b { color: var(--accent); }
.step small { display: block; color: var(--muted); font-size: 11.5px; }
.step.active { background: var(--ink); border-color: var(--ink); color: #fff; }
.step.active small { color: #cfc8bf; }
.step.active b { color: #ff9d95; }
.stage-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 4px 12px; font-size: 12.5px; font-weight: 500; }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.voice-chip { margin-left: auto; font-size: 12.5px; color: var(--muted); }
.voice-chip.on { color: var(--accent); font-weight: 600; }
.eyebrow { margin: 0 0 10px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.hint-inline { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 6px; }
.cues ol { margin: 0; padding: 0; list-style: none; counter-reset: cue; display: grid; gap: 14px; }
.cues li { counter-increment: cue; display: grid; grid-template-columns: 28px 1fr; font-size: 16.5px; line-height: 1.45; }
.cues li::before { content: counter(cue); font-weight: 700; color: var(--accent); font-size: 14px; padding-top: 2px; }
.real { display: inline-block; margin-top: 20px; font-size: 13px; color: var(--muted); }

/* practice list */
.practice { margin-top: 30px; }
.group-label { font-size: 13px; color: var(--muted); margin: 18px 0 8px; }
.pairs { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 8px; }
.pair { display: grid; grid-template-columns: 1fr auto 1fr; align-items: stretch; gap: 4px; }
.pair-vs { align-self: center; font-size: 11px; color: var(--faint); }
.words { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.item { position: relative; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 9px 12px; text-align: left; transition: border-color .15s, background .15s; }
.item:hover { border-color: #cfc6b9; }
.item.active { border-color: var(--ink); box-shadow: 0 0 0 1px var(--ink); }
.item-text { display: block; font-weight: 600; font-size: 15px; }
.item-ipa { display: block; font-family: var(--ipa); color: var(--muted); font-size: 15px; }
.item-score { position: absolute; top: 8px; right: 10px; font-size: 11px; font-weight: 700; }
.item.sentence { width: 100%; }
.item.sentence .item-text { font-weight: 500; font-size: 16px; }

/* coach */
.coach { border-left: 1px solid var(--line); background: var(--panel); padding: 24px 22px; position: sticky; top: 56px; height: calc(100vh - 56px); overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.coach-word { font-size: 34px; font-weight: 700; line-height: 1.15; letter-spacing: -.01em; overflow-wrap: anywhere; }
.coach-word.long { font-size: 21px; font-weight: 600; }
.coach-ipa { font-size: 22px; color: var(--muted); min-height: 1em; }
.coach-row { display: flex; gap: 8px; }
.btn { border: 1px solid var(--ink); background: var(--ink); color: #fff; border-radius: 12px; padding: 9px 14px; font-weight: 600; font-size: 14px; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn:disabled { opacity: .45; cursor: default; }
.rec { display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%; border: 0; border-radius: 16px; padding: 16px; background: var(--accent); color: #fff; font-weight: 700; font-size: 17px; }
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: #fff; }
.rec.live { background: var(--ink); }
.rec.live .rec-dot { background: var(--accent); animation: beat 1s ease-in-out infinite; }
.rec.busy { background: #8d867d; cursor: progress; }
@keyframes beat { 50% { transform: scale(.6); } }
.meter { height: 4px; border-radius: 4px; background: #eee8df; overflow: hidden; }
#meterFill { height: 100%; width: 0; background: var(--accent); transition: width .08s linear; }
.keys { margin: auto 0 0; font-size: 11.5px; color: var(--faint); }

/* result */
.result:empty { display: none; }
.result { display: grid; gap: 12px; }
.score-row { display: flex; align-items: center; gap: 14px; }
.big-score { font-size: 44px; font-weight: 700; line-height: 1; min-width: 70px; }
.big-score.good { color: var(--good); } .big-score.ok { color: var(--ok); } .big-score.low { color: var(--low); }
.score-meta { font-size: 13px; color: var(--muted); line-height: 1.35; }
.score-meta b { color: var(--ink); font-weight: 600; }
.word-block { display: grid; gap: 6px; }
.word-head { font-size: 13px; color: var(--muted); }
.word-head b { color: var(--ink); }
.word-head.bad b { color: var(--low); }
.tiles { display: flex; flex-wrap: wrap; gap: 4px; }
.tile { min-width: 32px; text-align: center; padding: 3px 5px 2px; border-radius: 8px; background: #f3eee6; border-bottom: 3px solid var(--faint); }
.tile .t-ipa { font-family: var(--ipa); font-size: 21px; line-height: 1.1; display: block; }
.tile .t-score { font-size: 10.5px; font-weight: 700; color: var(--muted); display: block; }
.tile .t-heard { font-size: 10.5px; color: var(--low); display: block; font-family: var(--ipa); }
.tile.good { border-bottom-color: var(--good); } .tile.ok { border-bottom-color: var(--ok); } .tile.low { border-bottom-color: var(--low); }
.tile.target { background: #fff; box-shadow: 0 0 0 1.5px var(--ink) inset; }
.tile.dim { opacity: .55; }
.note { font-size: 13.5px; line-height: 1.45; margin: 0; padding: 10px 12px; border-radius: 10px; background: #f6f1ea; }
.note .ipa { font-size: 16px; }
.note.warn { background: #fbecea; }
.stats { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); }
.stats b { color: var(--ink); }

/* setup dialog */
.setup { border: 0; border-radius: 18px; padding: 0; max-width: 520px; width: calc(100% - 32px); color: var(--ink); background: var(--panel); box-shadow: 0 20px 60px rgba(30, 20, 10, .25); }
.setup::backdrop { background: rgba(31, 29, 27, .35); }
.setup form { padding: 24px 26px; display: grid; gap: 12px; }
.setup h2 { margin: 0; font-size: 22px; }
.setup-lead { margin: 0; color: var(--muted); font-size: 14px; }
.how { margin: 0; padding-left: 20px; font-size: 14px; display: grid; gap: 6px; }
.how a { color: var(--ink); }
.setup label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.setup input { font: 15px var(--ui); padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.setup input:focus { outline: 2px solid var(--ink); outline-offset: 1px; }
.setup-err { margin: 0; color: var(--low); font-size: 13.5px; min-height: 1em; }
.setup-err:empty { display: none; }
.setup-note { margin: 0; font-size: 12px; color: var(--muted); }
.setup-row { display: flex; justify-content: flex-end; gap: 8px; }

/* narrower screens */
/* mid widths: sounds become a strip on top, the practice panel stays beside the lesson */
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(0, 1fr) 320px; }
  .rail { grid-column: 1 / -1; position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 8px 16px; overflow-x: auto; }
  .rail-head { display: none; }
  .sound-list { display: flex; gap: 4px; }
  .sound-btn { grid-template-columns: auto auto; min-width: max-content; padding: 6px 10px; }
  .sound-name { display: none; }
  .lesson-body { grid-template-columns: 1fr; }
  .stage { max-width: 580px; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .main { padding: 18px 16px 40px; }
  .glyph { font-size: 48px; min-width: 52px; }
  .coach { position: static; height: auto; border-left: 0; border-top: 1px solid var(--line); padding: 20px 16px; }
}
