/* Tokens from docs/spec/03-design-system.md (sections 2, 3, 4, 5), as in study/recorder. No other values are used below. */
:root {
  --paper: #FBF7F1;
  --surface: #FFFFFF;
  --surface-sunken: #F3EDE4;
  --line: #D9D0C4;
  --ink: #17130F;
  --ink-soft: #5C544B;
  --ink-faint: #736A60;
  --ember: #D43C15;
  --ember-pressed: #B93210;
  --ember-tint: #FBE3DA;
  --on-ember: #FFFFFF;
  --leaf: #1B8553;
  --on-leaf: #FFFFFF;
  --leaf-ink: #156F45;
  --danger: #B3261E;
  --on-danger: #FFFFFF;
  --amber-ink: #9A5B00;
  --shadow-float: 0 6px 20px rgba(23, 19, 15, 0.12);
  /* The voice ring's outline: none on light paper, where emberTint already shows. */
  --ring-outline: transparent;

  /* Type: rem so the phone's text-size setting scales everything. */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --lesson-size: 1.75rem; --lesson-line: 2.25rem;
  --display-size: 2.125rem; --display-line: 2.5rem;
  --title-size: 1.5rem; --title-line: 1.875rem;
  --heading-size: 1.1875rem; --heading-line: 1.625rem;
  --body-size: 1.0625rem; --body-line: 1.625rem;
  --label-size: 1rem; --label-line: 1.25rem;
  --caption-size: 0.8125rem; --caption-line: 1.125rem;

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 24px; --space-6: 32px; --space-7: 48px; --space-8: 64px;
  --margin: 20px;
  --target: 48px;
  --target-primary: 56px;
  --mic: 96px;
  --icon: 24px;
  --glyph: 40px;
  --radius-small: 4px;
  --radius-control: 14px;
  --radius-card: 20px;
  --radius-full: 999px;
  --hairline: 1px;
  --outline: 2px;

  --fast: 120ms;
  --base: 220ms;
  --pulse: 2400ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #121110;
    --surface: #1D1B19;
    --surface-sunken: #282522;
    --line: #3A3632;
    --ink: #F4EEE6;
    --ink-soft: #B5ACA1;
    --ink-faint: #968C80;
    --ember: #FF7A52;
    --ember-pressed: #E86A44;
    --ember-tint: #3A2219;
    --on-ember: #121110;
    --leaf: #4CC38A;
    --on-leaf: #121110;
    --leaf-ink: #4CC38A;
    --danger: #FF8A80;
    --on-danger: #121110;
    --amber-ink: #E5A13A;
    --shadow-float: none;
    --ring-outline: var(--ember);
    color-scheme: dark;
  }
}

/* Layout constants derived from the tokens above. */
:root {
  --dock-height: calc(var(--mic) + var(--space-4) * 2);
  --tabbar-height: calc(var(--target-primary) + var(--space-2));
  --content-max: 560px;
  --chart-height: calc(var(--space-8) * 2);
  --melody-height: calc(var(--space-8) * 2);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--body-size);
  line-height: var(--body-line);
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
h1, h2, h3, p, ul, ol { margin: 0; }
p, li, h1, h2, h3, span { overflow-wrap: anywhere; }

#app { min-height: 100vh; min-height: 100dvh; }
.screen-loading { padding: var(--space-7) var(--margin); color: var(--ink-soft); text-align: center; }

/* A page: a top bar, then content; room is left at the bottom for whatever is fixed there. */
.page {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-4) var(--margin) calc(var(--tabbar-height) + var(--space-6) + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: var(--space-5);
  min-width: 0;
}
.page.with-dock { padding-bottom: calc(var(--dock-height) + var(--body-line) + var(--space-7) + env(safe-area-inset-bottom)); }
.topbar { display: flex; align-items: center; gap: var(--space-3); min-height: var(--target); min-width: 0; }
.topbar .grow { flex: 1; min-width: 0; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: var(--target); height: var(--target);
  border: 0; border-radius: var(--radius-full); background: transparent; color: var(--ink);
  cursor: pointer; touch-action: manipulation;
}
.icon-btn:focus-visible, .btn:focus-visible, .tile:focus-visible, .mic:focus-visible, .play:focus-visible, .row:focus-visible, .tab:focus-visible { outline: var(--outline) solid var(--ink); outline-offset: var(--outline); }

h1, .title { font-size: var(--title-size); line-height: var(--title-line); font-weight: 700; text-wrap: balance; }
h2, .heading { font-size: var(--heading-size); line-height: var(--heading-line); font-weight: 600; text-wrap: balance; }
.soft { color: var(--ink-soft); }
.faint { color: var(--ink-faint); }
.caption { font-size: var(--caption-size); line-height: var(--caption-line); font-weight: 500; color: var(--ink-faint); }
.label { font-size: var(--label-size); line-height: var(--label-line); font-weight: 600; }
.display { font-size: var(--display-size); line-height: var(--display-line); font-weight: 700; font-variant-numeric: tabular-nums; }
.num { font-variant-numeric: tabular-nums; }
.icon { width: var(--icon); height: var(--icon); flex: none; }
.stack { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; }
.stack-tight { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.inline { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; min-width: 0; }
.section { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; }

/* The lesson: always the largest text on a learning screen. */
.lesson { font-size: var(--lesson-size); line-height: var(--lesson-line); font-weight: 400; text-wrap: pretty; }
.lesson-small { font-size: var(--heading-size); line-height: var(--heading-line); }
.lesson .mk-focus { font-weight: 600; }
.lesson .mk-stress { font-weight: 700; }
.lesson .mk-weak { color: var(--ink-faint); }
.lesson .mk-pause { color: var(--ink-faint); font-weight: 400; padding: 0 var(--space-1); }
.lesson .mk-link, .lesson .mk-dot { color: var(--ink-faint); font-weight: 400; }
.lesson .mk-link { margin: 0 calc(var(--space-1) * -1); }
.lesson .w { border-radius: var(--radius-small); transition: color var(--base) var(--ease); }
.lesson .w-heard { color: var(--leaf-ink); }
.lesson .w-missed { color: var(--amber-ink); text-decoration: underline dotted; text-decoration-thickness: var(--outline); text-underline-offset: var(--space-1); }
.lesson .w-cant-tell { color: var(--ink-soft); }
.is-now-line { background: var(--ember-tint); color: var(--ink); border-radius: var(--radius-small); }
.cue { font-size: var(--heading-size); line-height: var(--heading-line); color: var(--ink-soft); }
.lines { display: flex; flex-direction: column; gap: var(--space-4); }
.lines .is-later { color: var(--ink-soft); }
.turn { display: flex; flex-direction: column; gap: var(--space-1); }
.fade-in { animation: fade var(--base) var(--ease); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--target);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-control);
  border: 0;
  font-size: var(--label-size); line-height: var(--label-line); font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: transform var(--fast) var(--ease), background-color var(--fast) var(--ease);
  touch-action: manipulation;
  min-width: 0;
}
.btn:active { transform: scale(0.97); }
.btn[disabled] { cursor: default; opacity: 0.5; transform: none; }
.btn-primary { min-height: var(--target-primary); background: var(--ember); color: var(--on-ember); width: 100%; }
.btn-primary:active { background: var(--ember-pressed); }
.btn-outline { background: var(--surface); color: var(--ink); border: var(--outline) solid var(--ink); }
.btn-quiet { background: transparent; color: var(--ink); }
.btn-good { background: var(--leaf); color: var(--on-leaf); }
.btn-danger { background: var(--danger); color: var(--on-danger); }
.btn-wide { width: 100%; }
.btn[aria-pressed="true"] { background: var(--ember-tint); color: var(--ink); }
.btn-row { display: flex; gap: var(--space-2); flex-wrap: wrap; min-width: 0; }
.btn-row > .btn { flex: 1 1 auto; }

/* Rows (sittings, lists) */
.rows { display: flex; flex-direction: column; gap: var(--space-2); }
.row {
  display: flex; align-items: center; gap: var(--space-3);
  min-height: var(--target-primary);
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-control);
  text-align: left; width: 100%;
  cursor: pointer; touch-action: manipulation;
  min-width: 0;
}
.row:active { transform: scale(0.99); }
.row .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row .state { flex: none; display: inline-flex; align-items: center; gap: var(--space-1); color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.row .state.is-done { color: var(--leaf-ink); }
.row.is-next { border: var(--outline) solid var(--ink); }
.row.static { cursor: default; }
.row.static:active { transform: none; }

/* Tiles (choices) */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); }
.tile {
  display: flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: calc(var(--target-primary) + var(--space-4));
  padding: var(--space-3);
  background: var(--surface);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-card);
  font-size: var(--heading-size); line-height: var(--heading-line); font-weight: 600;
  cursor: pointer; touch-action: manipulation; min-width: 0;
}
.tile[aria-pressed="true"] { background: var(--ember-tint); border: var(--outline) solid var(--ink); }
.tile.is-right { border: var(--outline) solid var(--leaf-ink); color: var(--leaf-ink); background: var(--surface); }
.tile.is-try { border: var(--outline) dashed var(--amber-ink); color: var(--amber-ink); background: var(--surface); }

/* Status lines: always an icon and words. */
.status { display: flex; align-items: flex-start; gap: var(--space-2); color: var(--ink-soft); min-width: 0; }
.status .icon { margin-top: calc((var(--body-line) - var(--icon)) / 2); }
.status-good { color: var(--leaf-ink); font-weight: 600; }
.status-try { color: var(--amber-ink); font-weight: 600; }
.status-system { color: var(--ink-soft); font-weight: 600; }
.chip {
  display: inline-flex; align-items: center; gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
  color: var(--ink-soft);
  font-size: var(--label-size); line-height: var(--label-line); font-weight: 600;
}

/* Sitting chrome: close, the rail of steps, the counter. */
.rail { display: flex; gap: var(--space-1); flex: 1; min-width: 0; align-items: center; }
.rail span { flex: 1; height: var(--space-1); border-radius: var(--radius-full); background: var(--line); min-width: var(--space-1); }
.rail span.is-done { background: var(--ink-soft); }
.rail span.is-now { background: var(--ink); height: var(--space-2); }
.counter { font-size: var(--label-size); line-height: var(--label-line); font-weight: 600; color: var(--ink-soft); font-variant-numeric: tabular-nums; flex: none; }
.step-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); min-width: 0; }

/* The dock: fixed at the bottom; the centre control is always in the same place. */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--paper);
  border-top: var(--hairline) solid var(--line);
  padding: var(--space-3) var(--margin) calc(var(--space-4) + env(safe-area-inset-bottom));
}
.dock-inner {
  max-width: var(--content-max); margin: 0 auto;
  display: grid; grid-template-columns: minmax(0, 1fr) var(--mic) minmax(0, 1fr); align-items: center; gap: var(--space-3);
}
.dock-side { display: flex; min-width: 0; container-type: inline-size; }
.dock-side .btn span { overflow-wrap: normal; white-space: nowrap; }
/* A narrow screen or large text: the side buttons keep their icon; the label stays for screen readers. */
@container (max-width: 6rem) {
  .dock-side .btn span { position: absolute; width: var(--hairline); height: var(--hairline); overflow: hidden; clip-path: inset(50%); }
}
.dock-side.left { justify-content: flex-start; }
.dock-side.right { justify-content: flex-end; }
.dock-side .btn { padding-left: var(--space-2); padding-right: var(--space-2); }
.dock-status { min-height: var(--body-line); max-width: var(--content-max); margin: 0 auto var(--space-2); display: flex; justify-content: center; text-align: center; }

.mic {
  position: relative; isolation: isolate;
  width: var(--mic); height: var(--mic);
  border-radius: var(--radius-full); border: 0; background: transparent;
  color: var(--on-ember);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: manipulation;
  transition: transform var(--fast) var(--ease);
}
.mic::after { content: ''; position: absolute; inset: 0; border-radius: var(--radius-full); background: var(--ember); box-shadow: var(--shadow-float); z-index: -1; }
.mic:active { transform: scale(0.97); }
.mic .glyph, .play .glyph { width: var(--glyph); height: var(--glyph); }
.mic .ring { position: absolute; inset: 0; border-radius: var(--radius-full); background: var(--ember-tint); box-shadow: 0 0 0 var(--outline) var(--ring-outline); z-index: -1; transform: scale(1); transition: transform var(--fast) linear; }
.mic.is-ready .ring { animation: pulse var(--pulse) var(--ease) infinite; }
.mic.is-recording .ring { transform: scale(calc(1.16 + var(--level, 0) * 0.4)); }
.mic.is-busy { cursor: default; opacity: 0.5; }
.mic.is-busy .ring { animation: none; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.14); } }
.play {
  width: var(--mic); height: var(--mic);
  border-radius: var(--radius-full);
  border: var(--outline) solid var(--ink);
  background: var(--surface); color: var(--ink);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: manipulation;
  transition: transform var(--fast) var(--ease), background-color var(--base) var(--ease);
}
.play:active { transform: scale(0.97); }
.play.is-playing { background: var(--ember-tint); }
.play[disabled] { cursor: default; color: var(--ink-faint); border-color: var(--line); }
.centre-gap { width: var(--mic); height: var(--mic); }

/* Feedback: a sheet that sits above the dock, in the flow of the page. */
.sheet {
  background: var(--surface);
  border: var(--hairline) solid var(--line);
  border-radius: var(--radius-card);
  padding: var(--space-4);
  display: flex; flex-direction: column; gap: var(--space-3);
  min-width: 0;
}
.verdict { display: flex; align-items: flex-start; gap: var(--space-2); font-size: var(--heading-size); line-height: var(--heading-line); font-weight: 600; }
.verdict .icon { margin-top: calc((var(--heading-line) - var(--icon)) / 2); }
.verdict.good { color: var(--leaf-ink); }
.verdict.try, .verdict.unsure { color: var(--amber-ink); }
.verdict.neutral { color: var(--ink-soft); }
.measures { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: var(--space-2) var(--space-4); align-items: baseline; min-width: 0; margin: 0; }
.measures dt { color: var(--ink-soft); }
.measures dd { margin: 0; font-weight: 600; min-width: 0; font-variant-numeric: tabular-nums; }
.measures dd.try { color: var(--amber-ink); }
.measures dd.good { color: var(--leaf-ink); }
.measures dd.neutral { color: var(--ink-soft); font-weight: 400; }
.melody { width: 100%; height: var(--melody-height); display: block; }
.legend { display: flex; gap: var(--space-4); flex-wrap: wrap; font-size: var(--label-size); line-height: var(--label-line); }
.legend span { display: inline-flex; align-items: center; gap: var(--space-2); }
.legend i { display: inline-block; width: var(--space-5); height: var(--outline); background: currentColor; border-radius: var(--radius-full); }
.legend .model { color: var(--ink-faint); }
.legend .model i { height: calc(var(--outline) * 2); }
.legend .you { color: var(--ink); }

/* Bottom tabs */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--paper);
  border-top: var(--hairline) solid var(--line);
  padding: var(--space-1) var(--space-2) calc(var(--space-1) + env(safe-area-inset-bottom));
}
.tabbar-inner { max-width: var(--content-max); margin: 0 auto; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: var(--target-primary);
  border: 0; background: transparent; color: var(--ink-soft);
  font-size: var(--caption-size); line-height: var(--caption-line); font-weight: 600;
  cursor: pointer; touch-action: manipulation; min-width: 0;
}
.tab { container-type: inline-size; }
.tab span { max-width: 100%; overflow: hidden; white-space: nowrap; }
/* Too narrow for the words (small screen, large text): icons only; each tab keeps its name for screen readers. */
@container (max-width: 5rem) {
  .tab span { position: absolute; width: var(--hairline); height: var(--hairline); clip-path: inset(50%); }
}
.tab[aria-current="page"] { color: var(--ink); }

/* Passphrase */
.gate { max-width: var(--content-max); margin: 0 auto; min-height: 100vh; min-height: 100dvh; padding: var(--space-7) var(--margin); display: flex; flex-direction: column; justify-content: center; gap: var(--space-5); }
.field {
  width: 100%; min-height: var(--target-primary);
  padding: var(--space-3) var(--space-4);
  border: var(--outline) solid var(--ink-soft); border-radius: var(--radius-control);
  background: var(--surface); color: var(--ink);
  font-size: var(--body-size);
}
.field:focus { outline: none; border-color: var(--ink); }

/* Progress */
.chart { width: 100%; height: var(--chart-height); display: block; color: var(--ink); }
.chart .axis { stroke: var(--line); stroke-width: var(--hairline); }
.chart .series { fill: none; stroke: currentColor; stroke-width: var(--outline); vector-effect: non-scaling-stroke; }
.chart .dot-line { stroke-width: calc(var(--outline) * 3); stroke-linecap: round; }
.chart-card { background: var(--surface); border: var(--hairline) solid var(--line); border-radius: var(--radius-card); padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.chart-head { display: flex; justify-content: space-between; gap: var(--space-3); align-items: baseline; flex-wrap: wrap; min-width: 0; }
.counts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 5.5rem), 1fr)); gap: var(--space-2); }
.count .caption { overflow-wrap: normal; }
.count { background: var(--surface); border: var(--hairline) solid var(--line); border-radius: var(--radius-control); padding: var(--space-3); display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.compare { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--space-3); }
.compare .stack { align-items: stretch; }

/* Settings */
.choice-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-2); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation: none !important; }
  .mic.is-recording .ring { transform: scale(1.16); opacity: calc(0.5 + var(--level, 0) * 0.5); }
}
