/* ===========================================================================
   Read-Along Coach — "Schoolbook" visual system
   ---------------------------------------------------------------------------
   The canonical spec is the UI guide (Design canvas, "UI Guide — Schoolbook"):
   colour, word states, type, components, motion and — section 07 — layout &
   placement. Region letters in the comments below (A–G) refer to that section.

   Rules this file encodes:
     1. The passage is the hero: it owns the centre column and the most space.
     2. Patience is visible: the status pill mirrors the patience machine.
     3. Unclear is never wrong: "couldn't hear" is grey italic, never red.
   Every word state also carries a shape cue (tint, underbar, wavy underline,
   strike + dashed outline, italic + dotted rule, caption) so it survives colour
   blindness, projectors and phones in sunlight.

   Type: Fraunces for headings and big numbers; Atkinson Hyperlegible for all
   text a reader reads (Braille Institute design: b/d, I/l/1, O/0 never blur).
   =========================================================================== */

:root {
  /* neutrals */
  --paper:      #F6F1E7;   /* page ground */
  --card:       #FFFDF8;   /* reading surface, cards */
  --well:       #F0EADC;   /* inset wells, tracks */
  --rule:       #E3DACA;   /* borders, dividers */
  --rule-strong:#CFC4AE;   /* form control borders */
  --ink:        #1B2330;   /* text, app bar */
  --ink-soft:   #4A5261;   /* secondary text */
  --ink-faint:  #5B6270;   /* captions (5.4:1 on paper) */
  --bar-ink:    #C9CDD6;

  /* one action colour + one highlighter */
  --action:      #3346C9;
  --action-dark: #26359C;
  --action-tint: #E7EAFB;
  --hi:          #FFE27A;  /* "read next" — nothing else uses it */
  --hi-edge:     #D9B32C;

  /* word states */
  --ok:        #0B6B63;  --ok-bg:   #DCF0EA;  --ok-soft:  #B8DDD3;
  --sub:       #8A4300;  --sub-bg:  #FFE5C4;  --sub-line: #D97706; --sub-soft: #F2C98F;
  --skip:      #8C2F1C;  --skip-line: #C2533A;
  --unk:       #5B6270;  --unk-line:  #8A909B;

  --r-chip: 8px;
  --r-ctl:  14px;
  --r-card: 20px;
  --r-hero: 24px;

  --shadow-card: 0 1px 2px rgba(27, 35, 48, .05), 0 24px 48px -32px rgba(27, 35, 48, .45);

  --font-ui: 'Atkinson Hyperlegible', ui-sans-serif, system-ui, sans-serif;
  --font-display: 'Fraunces', Georgia, 'Times New Roman', serif;
}

* { box-sizing: border-box; }
html { color-scheme: light; -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--paper);
}

h1, h2, h3 { margin: 0; }
button, select, input { font: inherit; color: inherit; }

.skip-link {
  position: absolute; left: 16px; top: -60px; z-index: 50;
  padding: 10px 16px; border-radius: 10px;
  background: var(--hi); color: var(--ink); font-weight: 700; text-decoration: none;
}
.skip-link:focus { top: 12px; }

:focus-visible { outline: 3px solid var(--action); outline-offset: 3px; }

/* ═══ A · App bar ══════════════════════════════════════════════════════════ */

.appbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20;
  background: var(--ink); color: var(--card);
}
.appbar-inner {
  max-width: 1440px; margin: 0 auto;
  min-height: 72px; padding: 0 40px;
  display: flex; align-items: center; gap: 20px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 38px; height: 38px; flex-shrink: 0; border-radius: 11px;
  background: var(--hi); color: var(--ink);
  display: grid; place-items: center;
}
.brand-name { font-family: var(--font-display); font-size: 23px; font-weight: 600; white-space: nowrap; }
.brand-sub { font-size: 14px; color: var(--bar-ink); padding-left: 14px; border-left: 1px solid #3A4456; }
.tech-badge {
  margin-left: auto; display: inline-flex; align-items: center; gap: 10px;
  padding: 8px 14px; border-radius: 999px; border: 1px solid #3A4456;
  font-size: 13px; font-weight: 700; letter-spacing: .06em; color: var(--bar-ink); white-space: nowrap;
}
.tech-dot { width: 8px; height: 8px; border-radius: 50%; background: #5FD3B5; }

/* ═══ Layout: B rail + stage (C–G) ═════════════════════════════════════════ */

.layout {
  max-width: 1440px; margin: 0 auto;
  padding: 32px 40px 40px;
  display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 32px;
  align-items: start;
}

.card {
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: var(--r-card);
}

/* ── B · Rail ─────────────────────────────────────────────────────────────── */

.rail { display: flex; flex-direction: column; gap: 16px; }
/* Pin the rail only when the screen can show all of it: a sticky element
   taller than the viewport leaves its bottom cards unreachable. */
@media (min-width: 1100px) and (min-height: 1180px) {
  .rail { position: sticky; top: calc(72px + 24px); }
}
.panel { padding: 22px 24px; display: flex; flex-direction: column; gap: 12px; }
.panel-title { font-family: var(--font-display); font-size: 22px; font-weight: 600; line-height: 1.2; }
.field-label {
  font-size: 13px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft);
  margin-bottom: -4px;
}
select {
  width: 100%; height: 48px; padding: 0 40px 0 14px;
  border: 1px solid var(--rule-strong); border-radius: 12px;
  background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%231B2330' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 14px center;
  appearance: none; -webkit-appearance: none; font-size: 16px; cursor: pointer;
}
.hint { margin: 0; font-size: 14px; line-height: 1.5; color: var(--ink-soft); }
.audio-diagnostic { color: var(--ink-faint); font-size: 13px; }

.check { display: flex; align-items: center; gap: 12px; min-height: 44px; font-weight: 700; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--action); margin: 0; }

.how-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 10px; font-size: 14px; line-height: 1.5; color: var(--ink-soft); }
.how-list b { color: var(--ink); }
.panel-how { background: transparent; border-style: dashed; border-color: var(--rule-strong); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */

.btn {
  min-height: 52px; padding: 0 20px; border-radius: var(--r-ctl);
  border: 2px solid transparent; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  font-size: 17px; font-weight: 700; line-height: 1.1; text-align: center;
  transition: transform .08s ease, background-color .15s ease, box-shadow .15s ease;
}
.btn:active:not(:disabled) { transform: translateY(2px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--action); color: #fff; box-shadow: 0 3px 0 var(--action-dark); }
.btn-primary:hover:not(:disabled) { background: #2B3DB5; }
.btn-primary:active:not(:disabled) { box-shadow: 0 1px 0 var(--action-dark); }
.btn-ink { background: var(--ink); color: var(--card); }
.btn-ink:hover:not(:disabled) { background: #2A3446; }
.btn-outline { background: var(--card); color: var(--ink); border-color: var(--ink); }
.btn-quiet { background: transparent; color: var(--ink); text-decoration: underline; text-underline-offset: 4px; min-height: 44px; }
.btn-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.btn-row .btn { padding: 0 12px; font-size: 16px; }

/* ── C · Stage header: title left, patience pill right ──────────────────── */

.stage { min-width: 0; display: flex; flex-direction: column; gap: 18px; }
.stage-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; flex-wrap: wrap; }
.stage-title { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.eyebrow { font-size: 13px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-soft); }
.stage-title h1 { font-family: var(--font-display); font-size: clamp(28px, 2.4vw, 38px); font-weight: 600; line-height: 1.1; }

/* The pill: app.js writes #status text and sets data-tone. */
#status.pill {
  min-height: 48px; max-width: 100%; padding: 10px 18px 10px 16px; border-radius: 999px;
  display: inline-flex; align-items: center; gap: 12px; flex-wrap: wrap;
  font-weight: 700; font-size: 16px;
  background: var(--well); color: var(--ink);
  transition: background-color .25s ease, color .25s ease;
}
#status.pill::before {
  content: ""; width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
  background: currentColor;
}
#status[data-tone="listen"] { background: var(--ok-bg); color: var(--ok); }
#status[data-tone="work"]   { background: var(--sub-bg); color: var(--sub); }
#status[data-tone="help"]   { background: var(--action-tint); color: var(--action-dark); }
#status[data-tone="done"]   { background: var(--ok-bg); color: var(--ok); }
#status[data-tone="error"]  { background: #F8DCD3; color: var(--skip); }
#status[data-tone="work"]::before { animation: breathe 1.6s ease-in-out infinite; }

/* "▶ Playing…" badge app.js prepends during a demo replay */
#status .playing {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px 3px 8px; border-radius: 999px;
  background: var(--ink); color: var(--card);
  font-size: 12px; letter-spacing: .04em; white-space: nowrap;
}
#status .playing .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--hi); animation: breathe 1.1s ease-in-out infinite; }

@keyframes breathe { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* ── D · Reading surface ─────────────────────────────────────────────────── */

.reader {
  border-radius: var(--r-hero);
  box-shadow: var(--shadow-card);
  min-height: 420px;
  display: flex; flex-direction: column;
}

#passage {
  flex-grow: 1;
  padding: 44px 52px 28px;
  font-size: clamp(26px, 2.2vw, 32px);
  line-height: 1.9;
  word-spacing: .08em;
  overflow-wrap: break-word;
}

.reader-foot {
  padding: 0 52px 28px;
  display: flex; flex-direction: column; gap: 16px;
}

.read-progress { display: flex; align-items: center; gap: 16px; }
.read-progress-track { flex-grow: 1; height: 10px; border-radius: 5px; background: var(--well); overflow: hidden; }
.read-progress-fill { height: 100%; width: 0; border-radius: 5px; background: var(--ink); transition: width .3s ease; }
#readProgressText { font-size: 14px; font-weight: 700; color: var(--ink-soft); white-space: nowrap; }

/* Coach bubble (D2): docks inside the surface, under the words. */
#coach:empty { display: none; }
#coach:not(:empty) {
  align-self: flex-start; max-width: 100%;
  position: relative; margin-left: 58px;
  padding: 14px 20px; border-radius: 4px 20px 20px 20px;
  background: var(--ink); color: var(--card);
  font-size: 20px; line-height: 1.45;
  animation: pop .28s cubic-bezier(.34, 1.56, .64, 1) both;
}
#coach:not(:empty)::before {
  content: ""; position: absolute; left: -58px; top: 0;
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--ink) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='22' height='22' viewBox='0 0 24 24' fill='none' stroke='%23FFE27A' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M11 5 6 9H2v6h4l5 4z'/%3E%3Cpath d='M15.5 8.5a5 5 0 0 1 0 7'/%3E%3Cpath d='M19 5a10 10 0 0 1 0 14'/%3E%3C/svg%3E") no-repeat center;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.97); } to { opacity: 1; transform: none; } }

/* ── Word states (passage + legend) ──────────────────────────────────────── */

.w {
  padding: .04em .2em;
  border-radius: var(--r-chip);
  border: 2px solid transparent;
  transition: background-color .2s ease, color .2s ease, box-shadow .2s ease, opacity .2s ease;
}
.w.correct     { color: var(--ok); background: var(--ok-bg); }
.w.next        { color: var(--ink); background: var(--hi); box-shadow: inset 0 -4px 0 var(--hi-edge); }
.w.supplied    { color: var(--action-dark); background: var(--action-tint); }
.w.substituted { color: var(--sub); background: var(--sub-bg); text-decoration: underline wavy var(--sub-line); text-decoration-thickness: 2px; text-underline-offset: 5px; }
.w.skipped     { color: var(--skip); background: transparent; border: 2px dashed var(--skip-line); text-decoration: line-through; text-decoration-thickness: 2px; }
.w.unscorable  { color: var(--unk); background: transparent; font-style: italic; border-bottom: 2px dotted var(--unk-line); border-radius: 0; }
/* A still-revisable ASR guess: honest about the leading edge, not an error. */
.w.provisional { opacity: .6; }
/* Supplied wins over whatever verdict the aligner later gives the same word. */
.w.supplied.correct, .w.supplied.substituted, .w.supplied.skipped {
  color: var(--action-dark); background: var(--action-tint); border-color: transparent; text-decoration: none;
}

/* ── E · Legend ──────────────────────────────────────────────────────────── */

.legend-bar { display: flex; flex-wrap: wrap; gap: 8px 20px; font-size: 14px; color: var(--ink-soft); }
.legend-bar .key { display: inline-flex; align-items: center; gap: 8px; }
.legend-bar .w { font-weight: 700; font-size: 14px; padding: 0 6px; }

/* ═══ F · Report, G · Progress ═════════════════════════════════════════════ */

.results { display: grid; gap: 16px; margin-top: 8px; }

/* app.js toggles these with inline style.display = 'block' | 'none'. */
#report, #progress { display: none; padding: 26px 28px; animation: rise .35s ease-out both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.card-head h2 { font-family: var(--font-display); font-size: 26px; font-weight: 600; }
.badge {
  padding: 5px 12px; border-radius: 999px; background: var(--well);
  font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-soft);
}

.stat-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.stat {
  padding: 20px 22px; border-radius: 18px; background: var(--well);
  display: flex; flex-direction: column; gap: 8px;
}
.stat b { font-family: var(--font-display); font-size: 56px; font-weight: 700; line-height: 1; }
.stat .label { font-size: 13px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
.stat-hero { background: var(--ink); color: var(--card); }
.stat-hero .label { color: var(--hi); }
.stat-help b { color: var(--action-dark); }

.practice {
  margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--rule);
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 16px;
}
.practice-label { font-size: 13px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
.practice-words { font-size: 22px; font-weight: 700; }
.legend { margin: 14px 0 0; font-size: 14px; color: var(--ink-soft); }

.spark-line { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin: 0 0 14px; }
.table-wrap { overflow-x: auto; }
#progress table { width: 100%; border-collapse: collapse; font-size: 15px; }
#progress th, #progress td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--rule); white-space: nowrap; }
#progress th { font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
#progress td:nth-child(2) { white-space: normal; min-width: 8rem; }
#progress tbody tr:first-child td { font-weight: 700; }

.empty-state {
  padding: 22px 26px; background: transparent; border-style: dashed; border-color: var(--rule-strong);
  display: flex; flex-direction: column; gap: 6px;
}
.empty-state .panel-title { font-size: 20px; }
#report[style*="block"] + [data-empty-for="report"],
#progress[style*="block"] + [data-empty-for="progress"] { display: none; }

.foot {
  max-width: 1440px; margin: 0 auto; padding: 24px 40px 40px;
  border-top: 1px solid var(--rule); font-size: 14px; color: var(--ink-faint);
}

/* ═══ Breakpoints (guide §07) ══════════════════════════════════════════════ */

/* Tablet · 720–1099px: rail folds into a row of control cards above the stage;
   the explainer moves to the very bottom. */
@media (max-width: 1099px) {
  .appbar-inner, .layout, .foot { padding-left: 24px; padding-right: 24px; }
  .brand-sub { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .rail { position: static; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
  .panel-how { grid-column: 1 / -1; order: 99; }
  .layout > .rail { order: 1; }
  .layout > .stage { order: 2; }
}

/* Phone · under 720px: one column, reading first. */
@media (max-width: 719px) {
  .appbar-inner { min-height: 60px; padding: 0 16px; gap: 12px; }
  .brand-name { font-size: 20px; }
  .tech-badge { display: none; }
  .layout { padding: 16px 16px 28px; gap: 20px; }
  /* Reading first, then controls, then results (guide §07). The stage's
     children join the layout grid directly so the rail can slot between the
     passage and the report. */
  .layout > .stage { display: contents; }
  .stage-head { order: 1; }
  .reader { order: 2; }
  .legend-bar { order: 3; }
  .layout > .rail { order: 4; grid-template-columns: minmax(0, 1fr); }
  .results { order: 5; margin-top: 0; }
  .empty-state { display: none; }
  .stage-head { align-items: flex-start; flex-direction: column; gap: 12px; }
  #status.pill { font-size: 15px; }
  .reader { min-height: 0; border-radius: 22px; }
  #passage { padding: 24px 20px 16px; font-size: 24px; line-height: 1.85; }
  .reader-foot { padding: 0 20px 20px; }
  #coach:not(:empty) { font-size: 18px; margin-left: 0; }
  #coach:not(:empty)::before { display: none; }
  .stat-row { grid-template-columns: minmax(0, 1fr); }
  .stat b { font-size: 44px; }
  #report, #progress { padding: 20px; }
  .foot { padding: 20px 16px 32px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
