:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --fg: #f8fafc;
  --muted: #94a3b8;
  --line: #334155;
  --accent: #38bdf8;

  --idle: #64748b;
  --busy: #38bdf8;
  --searching: #f59e0b;
  --tracking: #22c55e;
  --error: #f87171;

  --off: #f87171;
  --close: #f59e0b;
  --correct: #22c55e;
}

* { box-sizing: border-box; }

/* removes the ~300ms tap delay + double-tap-to-zoom gesture browsers reserve
   on clickable elements by default — this app has no use for either, and a
   camera-driven UI needs taps to register immediately. */
button, [role="button"] { touch-action: manipulation; }

/* author `display` on .reference / .learn / .mode etc. otherwise beats the
   UA [hidden] rule (equal specificity) — make the attribute reliable */
[hidden] { display: none !important; }

html {
  /* soft depth behind the animated bg canvas so it never reads as flat black */
  background:
    radial-gradient(1200px 760px at 50% -12%, #1b2b47, rgba(15, 23, 42, 0) 62%),
    radial-gradient(900px 600px at 100% 110%, #17233c, rgba(15, 23, 42, 0) 55%),
    var(--bg);
  background-attachment: fixed;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--fg);
  -webkit-text-size-adjust: 100%;
  background: transparent; /* the animated <canvas> from js/bg.js shows through */
  /* the app owns exactly one screen: header + workspace + controls, no page
     scroll on any device. dvh tracks mobile browser chrome. */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- top bar: title + letter picker ------------------------------- */

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.6rem 1rem;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
}
.topbar h1 { margin: 0; font-size: 1rem; font-weight: 700; flex: none; }
.topbar-link {
  margin-left: auto;
  flex: none;
  font: 700 0.8rem system-ui, sans-serif;
  color: var(--muted);
  text-decoration: none;
  padding: 0.3rem 0.6rem;
  border-radius: 8px;
}
.topbar-link:hover { color: var(--fg); background: #0b1220; }
/* two right-aligned topbar items (progress + About) — only the first needs the
   auto margin that pushes the group right; .topbar's own `gap` spaces the rest */
.topbar-link ~ .topbar-link { margin-left: 0; }
.progress-btn {
  background: none;
  /* was `1px solid transparent`, only visible on :hover — on touch there's
     no hover, so it read as plain text, not a button. */
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
}
@media (hover: hover) and (pointer: fine) {
  .progress-btn:hover { border-color: var(--accent); }
}

/* ---- practice progress: mastery grid + streak --------------- */

.progress-overlay {
  position: fixed;
  inset: 0;
  z-index: 130;
  display: grid;
  place-items: center;
  padding: 1.2rem;
  background: rgba(2, 6, 23, 0.86);
  backdrop-filter: blur(6px);
}
.progress-inner {
  max-width: 26rem;
  width: 100%;
  background: linear-gradient(180deg, #1e293b, #16202f);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.2rem 1.4rem 1.4rem;
  box-shadow: 0 30px 80px -30px rgba(0, 0, 0, 0.8);
}
.progress-head { display: flex; align-items: center; justify-content: space-between; }
.progress-head h2 { margin: 0; font-size: 1.1rem; }
.progress-streak { margin: 0.2rem 0 0.9rem; color: var(--muted); font-size: 0.85rem; }
.progress-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.4rem;
  margin-bottom: 0.8rem;
}
.pg-cell {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font: 700 0.85rem system-ui, sans-serif;
  background: #1f2b3d;
  color: var(--muted);
  border: 1px solid rgba(148, 163, 184, 0.18);
}
.pg-cell.started { background: rgba(56, 189, 248, 0.28); color: #e0f2fe; border-color: rgba(56, 189, 248, 0.4); }
.pg-cell.mastered { background: #22c55e; color: #04140a; border-color: #22c55e; }
.progress-legend { margin: 0; font-size: 0.78rem; color: var(--muted); display: flex; align-items: center; gap: 0.3rem; flex-wrap: wrap; }
.pg-dot { display: inline-block; width: 0.7rem; height: 0.7rem; border-radius: 3px; margin-left: 0.6rem; }
.pg-dot:first-child { margin-left: 0; }
.pg-dot.pg-mastered { background: #22c55e; }
.pg-dot.pg-started { background: rgba(56, 189, 248, 0.5); }
.pg-dot.pg-none { background: #1f2b3d; border: 1px solid rgba(148, 163, 184, 0.18); }
@media (max-width: 520px) { .topbar-link { margin-left: 0; } }

/* segmented toggles — Practice/Challenge (header) and Free pick/A→Z (learn bar) */
.mode,
.submode {
  flex: none;
  display: inline-flex;
  flex-wrap: wrap;
  padding: 3px;
  border-radius: 11px;
  background: #0b1220;
  border: 1px solid var(--line);
  box-shadow: 0 2px 10px -4px rgba(0, 0, 0, 0.5);
}
.mode-btn,
.submode button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 700 0.85rem system-ui, sans-serif;
  padding: 0.4rem 0.95rem;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.submode button { font-size: 0.8rem; padding: 0.36rem 0.8rem; }
.mode-btn.on,
.submode button.on {
  background: linear-gradient(180deg, #7dd3fc, var(--accent));
  color: #0b1220;
  box-shadow: 0 4px 14px -4px rgba(56, 189, 248, 0.6);
}
@media (max-width: 520px) {
  .topbar h1 { font-size: 0.9rem; width: 100%; }
}
@media (max-width: 400px) {
  .mode-btn { padding: 0.36rem 0.62rem; font-size: 0.8rem; }
}

.learn {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.45rem 0.6rem;
  flex-wrap: wrap;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 1rem 0.5rem;
}
.chips {
  /* was `flex: 1` (= flex-basis: 0%), which never wraps onto its own line —
     it just grows into whatever's left beside .submode. On a 360px phone
     that left ~81px for 26 chips: 2/row, 13 rows, ~494px tall on first load,
     before a letter is even picked. flex-basis:100% forces its own row;
     the grid + max-height bounds it instead of letting it own the screen. */
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(2.6rem, 1fr));
  gap: 0.3rem;
  flex: 1 1 100%;
  min-width: 0;
  max-height: 30svh;
  overflow-x: hidden; /* setting only overflow-y makes some browsers auto-enable x too */
  overflow-y: auto;
  overscroll-behavior: contain;
}
/* once a letter is chosen the grid collapses to a compact strip */
.learn-current { display: none; font-size: 1.25rem; font-weight: 800; color: var(--accent); min-width: 1.2em; text-align: center; }
.step-btn {
  display: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--fg);
  border-radius: 7px;
  width: 1.9rem;
  height: 1.9rem;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}
.step-btn:active { transform: scale(0.92); }
.learn.compact .chips { display: none; }
.learn.compact .learn-current { display: inline-block; }
.learn.compact .step-btn { display: inline-block; }
.learn.compact[data-run="on"] .step-btn { display: none; }
.az-progress {
  flex: none;
  font: 800 0.85rem system-ui, sans-serif;
  color: var(--accent);
  letter-spacing: 0.02em;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.14);
}
/* only shown during a bounded run (A→Z or Review) — free-pick is meant to
   teach, so hiding help there would defeat its point. */
.blind-toggle {
  flex: none;
  font-size: 0.78rem;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
}
.learn[data-run="on"] .chip.done {
  background: linear-gradient(180deg, #22c55e, #15803d);
  border-color: #22c55e;
  color: #dcfce7;
}
.learn[data-run="on"] .chip.clear { display: none; }
.chip.clear {
  width: auto;
  padding: 0 0.6rem;
  font-size: 0.72rem;
  font-weight: 700;
}
.chip {
  flex: none;
  /* was a fixed 2.1rem (33.6px) — under the 44px touch-target guideline.
     width:100% fills its grid cell (.chips is now a grid, see above);
     .chip.clear overrides width to auto so it's unaffected here. */
  width: 100%;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #223048, #1a2434);
  color: var(--fg);
  font: 700 0.88rem system-ui, sans-serif;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.14s ease, background 0.14s, color 0.14s;
}
@media (hover: hover) and (pointer: fine) {
  .chip:hover { border-color: var(--accent); transform: translateY(-2px); }
}
.chip:active { transform: translateY(0) scale(0.92); }
.chip.on {
  background: linear-gradient(180deg, #7dd3fc, var(--accent));
  border-color: #7dd3fc;
  color: #0b1220;
  transform: scale(1.12);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.28), 0 6px 16px -4px rgba(56, 189, 248, 0.5);
}
.chip.clear {
  border-color: var(--line);
  color: var(--muted);
  background: var(--panel);
  height: 1.7rem;
  align-self: center;
}

/* ---- workspace: camera + reference panel ------------------------ */
/* fills the space between header and controls on ANY screen. the camera box
   flexes to whatever room is left; the video + overlay canvas both cover it,
   so the device / camera aspect ratio never forces a scroll or a letterbox. */

.workspace {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0.2rem 1rem 0;
  display: flex;
  gap: 0.9rem;
  /* default (portrait / narrow): camera stacked over the reference strip */
  flex-direction: column;
}

/* landscape / desktop: camera left, reference card right, both centred in the
   row. the camera keeps a natural 4:3 shape (height drives it) so a normal
   webcam isn't cropped to a near-square — it just gets as big as the row
   allows, then the whole group is centred instead of stretched edge to edge. */
@media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
  .workspace {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding-bottom: 0.2rem;
  }
}

.viewport {
  position: relative;
  flex: 1 1 auto;
  /* was 0 — with .reference now allowed to shrink (above) rather than being
     `flex:none`, the camera needs its own floor or a tall .reference could
     still take everything. 34svh keeps a usable camera view on any phone. */
  min-height: 34svh;
  min-width: 0;
  --hold: 0;
  background: #020617;
  border-radius: 18px;
  overflow: hidden;
  box-shadow:
    inset 0 0 0 3px transparent,
    0 20px 50px -24px rgba(0, 0, 0, 0.7),
    0 0 0 1px rgba(148, 163, 184, 0.08);
  transition: box-shadow 0.22s ease, transform 0.22s ease;
}

/* landscape / desktop: a true 4:3 camera box, driven by the room the row has
   after the header + learn bar + controls, capped so it never sprawls and
   shrinks on smaller windows. MUST come after the base `.viewport` rule —
   equal specificity, source order decides. */
@media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
  .viewport {
    flex: 0 1 auto;
    width: min(100%, calc((100dvh - 150px) * 4 / 3), 900px);
    height: auto;
    max-height: calc(100dvh - 130px);
    aspect-ratio: 4 / 3;
  }
}
/* fallback for browsers without dvh (older Safari): plain vh */
@supports not (height: 100dvh) {
  @media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
    .viewport { width: min(100%, calc((100vh - 150px) * 4 / 3), 900px); max-height: calc(100vh - 130px); }
  }
}
/* "charge" bar along the bottom while you hold a completed sign — fills over
   HOLD_MS, then the reward fires. Invisible at --hold: 0. */
.viewport::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  height: 4px;
  width: calc(var(--hold) * 100%);
  background: linear-gradient(90deg, #22c55e, #86efac);
  box-shadow: 0 0 12px 2px rgba(34, 197, 94, 0.7);
  z-index: 40;
  pointer-events: none;
  transition: width 0.1s linear;
}
/* gentle life before the camera is even on */
.viewport[data-state="idle"] {
  animation: vp-idle-breathe 4.4s ease-in-out infinite;
}
@keyframes vp-idle-breathe {
  0%, 100% {
    box-shadow:
      inset 0 0 0 3px rgba(56, 189, 248, 0.06),
      0 20px 50px -24px rgba(0, 0, 0, 0.7),
      0 0 34px -18px rgba(56, 189, 248, 0.35);
  }
  50% {
    box-shadow:
      inset 0 0 0 3px rgba(56, 189, 248, 0.14),
      0 20px 50px -24px rgba(0, 0, 0, 0.7),
      0 0 60px -12px rgba(56, 189, 248, 0.55);
  }
}
.viewport[data-match="off"] {
  box-shadow: inset 0 0 0 4px var(--off), 0 0 34px -6px rgba(248, 113, 113, 0.45);
}
.viewport[data-match="close"] {
  box-shadow: inset 0 0 0 5px var(--close), 0 0 40px -4px rgba(245, 158, 11, 0.5);
}
.viewport[data-match="correct"] {
  box-shadow: inset 0 0 0 6px var(--correct), 0 0 55px 0 rgba(34, 197, 94, 0.6);
}
.viewport.celebrate {
  animation: pop-scale 0.5s cubic-bezier(0.2, 1.6, 0.4, 1);
}
.viewport.celebrate::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(circle at 50% 50%, rgba(34, 197, 94, 0.35), transparent 62%);
  animation: flash-out 0.6s ease-out forwards;
  pointer-events: none;
}
@keyframes pop-scale {
  0% { transform: scale(1); }
  45% { transform: scale(1.02); }
  100% { transform: scale(1); }
}
@keyframes flash-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* selfie (front) camera is mirrored; the back camera shows the world as-is */
.stage { position: absolute; inset: 0; transform: scaleX(-1); }
.viewport[data-facing="environment"] .stage { transform: none; }
#camera, #overlay { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.hud { position: absolute; inset: 0; pointer-events: none; }

.pill {
  position: absolute;
  top: 0.7rem;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  max-width: calc(100% - 1.2rem);
  padding: 0.34rem 0.75rem;
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(6px);
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pill-dot { flex: none; width: 0.55rem; height: 0.55rem; border-radius: 50%; background: var(--idle); }
[data-state="requesting"] .pill-dot,
[data-state="loading"] .pill-dot { background: var(--busy); animation: pulse 1s ease-in-out infinite; }
[data-state="searching"] .pill-dot { background: var(--searching); animation: pulse 1.4s ease-in-out infinite; }
[data-state="tracking"] .pill-dot { background: var(--tracking); }
[data-state="error"] .pill-dot { background: var(--error); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .pill-dot { animation: none !important; } }

/* recognized letter — small corner badge, not a giant overlay */
.letter-badge {
  position: absolute;
  left: 0.7rem;
  bottom: 0.7rem;
  min-width: 2.5rem;
  height: 2.5rem;
  padding: 0 0.55rem;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: rgba(2, 6, 23, 0.8);
  backdrop-filter: blur(6px);
  box-shadow: 0 0 0 1px rgba(148, 163, 184, 0.15);
  font: 800 1.7rem system-ui, sans-serif;
  transition: transform 0.15s ease;
}
.letter-badge.pop {
  animation: badge-pop 0.55s cubic-bezier(0.2, 1.7, 0.4, 1);
}
@keyframes badge-pop {
  0% { transform: scale(1) rotate(0); }
  35% { transform: scale(1.55) rotate(-7deg); color: var(--correct);
        box-shadow: 0 0 0 2px var(--correct), 0 0 26px rgba(34, 197, 94, 0.7); }
  70% { transform: scale(0.95) rotate(4deg); }
  100% { transform: scale(1) rotate(0); }
}

.stats {
  position: absolute;
  right: 0.6rem;
  bottom: 0.5rem;
  margin: 0;
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
  background: rgba(2, 6, 23, 0.55);
  color: rgba(248, 250, 252, 0.55);
  font: 0.62rem/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.curtain {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1.1rem;
  background: linear-gradient(rgba(2, 6, 23, 0.3), rgba(2, 6, 23, 0.55));
  pointer-events: auto;
}
.curtain-inner { text-align: center; max-width: 20rem; }
[data-state="requesting"] .curtain,
[data-state="loading"] .curtain,
[data-state="searching"] .curtain,
[data-state="tracking"] .curtain { display: none; }
.cta {
  background: linear-gradient(180deg, #7dd3fc, var(--accent));
  color: #0b1220; border: 0; border-radius: 12px;
  padding: 0.85rem 1.5rem; font: 800 1.05rem system-ui, sans-serif; cursor: pointer;
  box-shadow: 0 10px 30px -10px rgba(56, 189, 248, 0.7);
  animation: cta-breathe 2.6s ease-in-out infinite;
  transition: transform 0.14s ease, box-shadow 0.14s ease;
}
@media (hover: hover) and (pointer: fine) {
  .cta:hover { transform: translateY(-2px); box-shadow: 0 14px 34px -8px rgba(56, 189, 248, 0.85); }
}
.cta:active { transform: translateY(0) scale(0.97); }
@keyframes cta-breathe {
  0%, 100% { box-shadow: 0 10px 30px -10px rgba(56, 189, 248, 0.6); }
  50% { box-shadow: 0 10px 40px -6px rgba(56, 189, 248, 0.95); }
}
.cta-sub { margin: 0.65rem 0 0; color: var(--fg); font-size: 0.8rem; line-height: 1.5; opacity: 0.85; }
[data-state="error"] .cta-sub { color: var(--error); opacity: 1; }

.pick-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0.7rem 1.15rem;
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(148, 163, 184, 0.2);
  font: 700 0.95rem system-ui, sans-serif;
  color: #e2e8f0;
  /* was `white-space: nowrap` with no max-width — the actual string ("👆 Pick
     a letter above to start learning") renders ~355px wide and got clipped
     on both sides inside a ~328px viewport. */
  max-width: min(90%, 22rem);
  text-align: center;
  text-wrap: balance;
  animation: pick-bob 2.2s ease-in-out infinite;
}
.pick-hint span { font-size: 1.1rem; }
@keyframes pick-bob {
  0%, 100% { transform: translate(-50%, -50%); }
  50% { transform: translate(-50%, calc(-50% - 6px)); }
}

/* ---- challenge HUD ---------------------------------------- */

.timebar {
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 6px;
  background: rgba(2, 6, 23, 0.5);
  z-index: 45;
}
.timebar { transition: height 0.15s ease; }
.timebar i {
  display: block;
  height: 100%;
  width: calc(var(--time, 1) * 100%);
  background: linear-gradient(90deg, #ef4444, #f59e0b 45%, #22c55e);
  background-size: 100vw 100%;
  background-position: right;
  transition: width 0.12s linear;
}
.timebar.low { height: 9px; }
.timebar.low i { animation: time-pulse 0.5s ease-in-out infinite; }
@keyframes time-pulse { 0%, 100% { filter: brightness(1); } 50% { filter: brightness(1.8); } }

.score-badge {
  position: absolute;
  right: 0.7rem;
  top: 0.9rem;
  min-width: 2.2rem;
  padding: 0.25rem 0.6rem;
  border-radius: 12px;
  background: rgba(2, 6, 23, 0.8);
  backdrop-filter: blur(6px);
  font: 800 1.15rem system-ui, sans-serif;
  text-align: center;
  box-shadow: 0 0 0 1px rgba(148, 163, 184, 0.15);
  transition: transform 0.15s ease;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.score-badge.pop { animation: badge-pop 0.5s cubic-bezier(0.2, 1.7, 0.4, 1); }
.score-badge .streak {
  font-size: 0.8rem;
  font-weight: 800;
  color: #fdba74;
}
.ch-gain {
  position: absolute;
  right: 1rem;
  top: 3rem;
  font: 900 1.1rem system-ui, sans-serif;
  color: var(--correct);
  text-shadow: 0 2px 10px rgba(2, 6, 23, 0.8);
  pointer-events: none;
  animation: gain-float 0.7s ease-out forwards;
}
@keyframes gain-float {
  0% { opacity: 0; transform: translateY(6px); }
  25% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-18px); }
}
.ch-banner {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  font: 900 clamp(4rem, 22vh, 9rem) system-ui, sans-serif;
  color: rgba(248, 250, 252, 0.92);
  text-shadow: 0 6px 30px rgba(2, 6, 23, 0.8);
  pointer-events: none;
  letter-spacing: 0.02em;
}
.ch-banner.win { color: var(--correct); }
.ch-banner.go { color: #7dd3fc; animation: go-flash 0.5s ease-out; }
@keyframes go-flash {
  0% { transform: translate(-50%, -50%) scale(0.6); opacity: 0; }
  40% { opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1.15); opacity: 0.9; }
}
.ch-seeing {
  position: absolute;
  left: 50%;
  bottom: 0.8rem;
  transform: translateX(-50%);
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.7);
  backdrop-filter: blur(4px);
  font: 700 0.8rem system-ui, sans-serif;
  color: var(--muted);
  pointer-events: none;
  white-space: nowrap;
}
.ch-seeing b { color: #e2e8f0; }
.ch-card {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1.1rem;
  background: linear-gradient(rgba(2, 6, 23, 0.55), rgba(2, 6, 23, 0.8));
  z-index: 50;
  pointer-events: auto; /* .hud is pointer-events:none — re-enable for the button */
}
.ch-card-inner { text-align: center; max-width: 22rem; }
.ch-card-title { margin: 0 0 0.35rem; font: 800 1.3rem system-ui, sans-serif; }
.ch-card-sub { margin: 0 0 1rem; color: var(--muted); font-size: 0.85rem; line-height: 1.5; }
/* in challenge mode the ch-card owns the "turn on camera" moment */
.viewport[data-mode="challenge"] .curtain { display: none; }

/* ---- reference panel ---------------------------------------- */

.reference {
  /* was `flex: none` at a fixed ~358px in portrait — the only other flex
     item is .viewport (flex:1), so the camera got crushed toward 0. Letting
     this shrink (and scroll if its content still doesn't fit) means the
     camera keeps a real minimum instead of losing a fight it can't win. */
  flex: 0 1 auto;
  min-height: 0;
  max-height: 42svh;
  overflow-x: hidden; /* setting only overflow-y makes some browsers auto-enable x too */
  overflow-y: auto;
  overscroll-behavior: contain;
  background: linear-gradient(180deg, #1e293b, #16202f);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 0.8rem 0.95rem;
  display: grid;
  gap: 0.5rem 0.75rem;
  box-shadow: 0 16px 40px -22px rgba(0, 0, 0, 0.6);
  /* mobile/portrait: photo + demo stacked above the match readout, not
     squeezed into one cramped row — the old 3-column strip made both the
     images and the text too small to actually read on a phone. */
  grid-template-columns: 1fr 1fr;
  grid-template-areas:
    "head  head"
    "desc  desc"
    "photo canvas"
    "match match";
  align-items: center;
}
.ref-head  { grid-area: head;  font-size: 0.82rem; color: var(--muted); }
.ref-head b { color: var(--fg); font-size: 1.05rem; }
.ref-hand { margin-left: 0.4rem; font-size: 0.72rem; opacity: 0.8; text-transform: capitalize; }
/* lefty on camera -> show the reference as a left hand */
.reference.mirror .ref-photo-wrap,
.reference.mirror .ref-canvas-wrap { transform: scaleX(-1); }
.ref-desc {
  grid-area: desc;
  margin: 0;
  font-size: 0.86rem;
  line-height: 1.4;
  color: #cbd5e1;
}
.ref-desc:empty { display: none; }
.ref-photo-wrap { grid-area: photo; }
.ref-canvas-wrap { grid-area: canvas; }
.ref-match  { grid-area: match; min-width: 0; }
.ref-photo-wrap,
.ref-canvas-wrap {
  /* was a fixed 72px squeezed into a 3-up row — too small to actually read
     finger position on a phone screen. Now each gets its own half-width
     column (see grid-template-areas above), sized off that. No min-width
     floor: a hard px floor on two side-by-side items forces the whole grid
     (including the full-width desc text row) wider than .reference whenever
     the panel itself gets flex-shrunk below ~200px — overflow-x:hidden then
     clips live text instead of just letting the images get a bit smaller. */
  width: 100%;
  max-width: 160px;
  min-width: 0;
  justify-self: center;
  aspect-ratio: 1;
  border-radius: 12px;
}
.ref-photo-wrap,
.ref-canvas-wrap {
  position: relative;
  display: block;
  padding: 0;
  border: 1px solid rgba(148, 163, 184, 0.12);
  background: #0b1220;
  cursor: zoom-in;
  overflow: hidden;
}
.ref-photo { display: block; width: 100%; height: 100%; object-fit: cover; background: #020617; }
.ref-canvas { display: block; width: 100%; height: 100%; }
.zoom-icon {
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: 0.8rem;
  color: rgba(226, 232, 240, 0.6);
  pointer-events: none;
}

.ref-match-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.reco { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.72rem; font-weight: 700; color: var(--muted); }
.reco i { width: 0.55rem; height: 0.55rem; border-radius: 50%; background: #475569; transition: background 0.2s; }
.reco.match i { background: var(--correct); box-shadow: 0 0 8px 1px rgba(34, 197, 94, 0.7); }
.reco.match { color: #86efac; }
.letter-stat { margin: 0; font-size: 0.75rem; color: var(--muted); }
.letter-stat:empty { display: none; }
.reference.nudge { animation: ref-nudge 1.5s ease-in-out infinite; }
@keyframes ref-nudge {
  0%, 100% { box-shadow: 0 16px 40px -22px rgba(0, 0, 0, 0.6); }
  50% { box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.5), 0 16px 40px -22px rgba(0, 0, 0, 0.6); }
}

/* PORTRAIT: keep the description to 3 lines so the strip stays short */
@media (max-width: 759px), (orientation: portrait) {
  .ref-desc {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-size: 0.82rem;
  }
}

/* LANDSCAPE + roomy: a full-height column beside the camera. Grid areas
   already match the mobile layout above — this just widens the card. */
@media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
  .reference {
    width: clamp(230px, 26vw, 330px);
    align-self: center; /* compact card, balanced beside the camera */
    align-content: start;
    align-items: stretch;
    gap: 0.6rem 0.7rem;
  }
  .ref-photo-wrap,
  .ref-canvas-wrap {
    max-width: 168px;
  }
}
.ref-match { display: grid; gap: 0.35rem; align-content: center; }
.meter {
  position: relative;
  height: 0.7rem;
  border-radius: 999px;
  background: #0b1220;
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}
.meter-fill {
  height: 100%; width: 0%; border-radius: 999px;
  background: var(--off);
  transition: width 0.12s linear, background-color 0.2s, box-shadow 0.2s;
}
.meter-fill.correct {
  box-shadow: 0 0 14px 1px rgba(34, 197, 94, 0.85);
  animation: meter-shimmer 1.1s ease-in-out infinite;
}
@keyframes meter-shimmer {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.35); }
}
.meter-label {
  font-size: 0.85rem; font-weight: 800; letter-spacing: 0.01em; color: var(--muted);
  transition: color 0.2s;
}
.meter-label.correct { text-shadow: 0 0 12px rgba(34, 197, 94, 0.6); }
.ref-hint {
  margin: 0;
  min-height: 1.3em;
  font-size: 0.9rem;
  line-height: 1.3;
  font-weight: 600;
  color: var(--fg);
}
.ref-hint:empty { display: none; }

/* landscape column has the room for a taller hint; portrait strip keeps it to
   two lines so the strip stays short and the camera keeps the screen */
@media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
  .ref-hint { min-height: 2.4em; font-size: 0.92rem; }
}
@media (max-width: 759px), (orientation: portrait) {
  .ref-hint {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}

/* ---- spell mode: continuous fingerspelling -> text -------- */

@property --p {
  syntax: "<number>";
  inherits: false;
  initial-value: 0;
}

.speller {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.85rem 1rem;
  background: linear-gradient(180deg, #1e293b, #16202f);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 16px 40px -22px rgba(0, 0, 0, 0.6);
  /* the help + chart can be long — keep the panel inside one screen and let it
     scroll internally rather than pushing the layout */
  max-height: 46vh;
  overflow-y: auto;
}
.sp-top {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  padding-bottom: 0.55rem;
  background: linear-gradient(180deg, #1e293b 82%, transparent);
}
.sp-live { display: flex; align-items: center; gap: 0.7rem; min-height: 2.6rem; }
.sp-ring {
  --p: 0;
  flex: none;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background:
    radial-gradient(closest-side, #0b1220 79%, transparent 80% 100%),
    conic-gradient(var(--accent) calc(var(--p) * 360deg), #33415580 0);
  transition: --p 0.1s linear;
}
.sp-ring b {
  font: 800 1.15rem system-ui, sans-serif;
  color: var(--fg);
}
.sp-ring b:empty::after,
.sp-ring b { color: var(--fg); }
.sp-hint { font-size: 0.82rem; color: var(--muted); line-height: 1.35; }
.sp-text {
  min-height: 3.2rem;
  max-height: 8rem;
  overflow-y: auto;
  padding: 0.6rem 0.7rem;
  border-radius: 12px;
  background: #0b1220;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
  font: 700 1.35rem/1.4 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
  color: #e2e8f0;
  white-space: pre-wrap;
  word-break: break-word;
}
.sp-text:empty::before {
  content: "…";
  color: #475569;
}
/* the word being spelled right now — not yet committed */
.sp-pending {
  color: #64748b;
  border-bottom: 2px dashed #475569;
  padding-bottom: 1px;
}
.sp-controls { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.sp-controls button {
  flex: 1 1 auto;
  min-width: 4rem;
  padding: 0.5rem 0.8rem;
  background: var(--panel);
  color: var(--fg);
  border: 1px solid #475569;
  border-radius: 9px;
  font: 700 0.88rem system-ui, sans-serif;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.14s, background 0.14s;
}
@media (hover: hover) and (pointer: fine) {
  .sp-controls button:hover { border-color: var(--accent); transform: translateY(-1px); }
}
.sp-controls button:active { transform: translateY(0) scale(0.96); }
#spClear { border-color: #7f1d1d; }

/* collapsible help + alphabet chart */
.sp-fold {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0b1220;
  font-size: 0.82rem;
}
.sp-fold > summary {
  cursor: pointer;
  padding: 0.5rem 0.7rem;
  font-weight: 700;
  color: var(--fg);
  list-style: none;
}
.sp-fold > summary::-webkit-details-marker { display: none; }
.sp-fold > summary::before {
  content: "▸ ";
  color: var(--accent);
}
.sp-fold[open] > summary::before { content: "▾ "; }
.sp-steps {
  margin: 0;
  padding: 0 0.9rem 0.7rem 1.6rem;
  display: grid;
  gap: 0.4rem;
  color: #cbd5e1;
  line-height: 1.4;
}
.sp-steps b { color: var(--fg); }

/* animated gesture demos */
.g-list {
  display: grid;
  gap: 0.55rem;
  padding: 0.4rem 0.7rem 0.7rem;
  color: #cbd5e1;
  line-height: 1.35;
}
.g-row { display: flex; align-items: center; gap: 0.6rem; }
.g-row b { color: var(--fg); }
.g-anim {
  position: relative;
  flex: none;
  width: 88px;
  height: 52px;
  border-radius: 8px;
  background: #0b1220;
  border: 1px solid rgba(148, 163, 184, 0.14);
  overflow: hidden;
  display: grid;
  place-items: center;
}
.g-anim .h {
  position: absolute;
  font-size: 24px;
  line-height: 1;
  filter: grayscale(0.15);
}
.g-track {
  position: absolute;
  left: 8px;
  right: 8px;
  height: 2px;
  background: repeating-linear-gradient(90deg, #33415580 0 4px, transparent 4px 8px);
}
.g-delete .h { animation: g-swipe 1.8s ease-in-out infinite; }
@keyframes g-swipe {
  0%        { transform: translateX(26px); opacity: 0; }
  12%       { opacity: 1; }
  50%       { transform: translateX(-26px); opacity: 1; }
  62%, 100% { transform: translateX(-26px); opacity: 0; }
}
/* add a letter: hand still, a ring fills around it */
.g-hold .g-ring {
  --p: 0;
  position: absolute;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background:
    radial-gradient(closest-side, #0b1220 76%, transparent 78%),
    conic-gradient(var(--accent) calc(var(--p) * 360deg), #33415577 0);
  animation: g-fill 2s ease-in-out infinite;
}
@keyframes g-fill { 0% { --p: 0; } 65%, 100% { --p: 1; } }
/* space: hand drops, a gap mark appears */
.g-space .h { animation: g-drop 2s ease-in-out infinite; }
@keyframes g-drop {
  0%, 12%  { transform: translateY(-6px); opacity: 1; }
  42%      { transform: translateY(13px); opacity: 1; }
  56%      { transform: translateY(13px); opacity: 0; }
  100%     { transform: translateY(-6px); opacity: 0; }
}
.g-space .g-gap {
  position: absolute;
  bottom: 7px;
  font: 800 15px ui-monospace, monospace;
  color: var(--accent);
  opacity: 0;
  animation: g-gap 2s ease-in-out infinite;
}
@keyframes g-gap { 0%, 44% { opacity: 0; } 58%, 88% { opacity: 1; } 100% { opacity: 0; } }
.g-copy .h1  { animation: g-in-l 2.1s ease-in-out infinite; }
.g-copy .h2  { animation: g-in-r 2.1s ease-in-out infinite; }
.g-paste .h1 { animation: g-out-l 2.1s ease-in-out infinite; }
.g-paste .h2 { animation: g-out-r 2.1s ease-in-out infinite; }
@keyframes g-in-l  { 0%, 100% { transform: translateX(-22px); } 45%, 68% { transform: translateX(-3px); } }
@keyframes g-in-r  { 0%, 100% { transform: translateX(22px); }  45%, 68% { transform: translateX(3px); } }
@keyframes g-out-l { 0%, 100% { transform: translateX(-3px); }  45%, 68% { transform: translateX(-22px); } }
@keyframes g-out-r { 0%, 100% { transform: translateX(3px); }   45%, 68% { transform: translateX(22px); } }
.g-note { margin: 0.1rem 0 0; font-size: 0.78rem; color: var(--muted); }
.g-note b { color: #cbd5e1; }
@media (prefers-reduced-motion: reduce) {
  .g-anim .h, .g-ring, .g-gap { animation: none !important; }
  .g-delete .h { transform: translateX(-14px); opacity: 1; }
  .g-copy .h1  { transform: translateX(-3px); }
  .g-copy .h2  { transform: translateX(3px); }
  .g-paste .h1 { transform: translateX(-20px); }
  .g-paste .h2 { transform: translateX(20px); }
  .g-hold .g-ring { --p: 0.6; }
  .g-space .h { transform: translateY(6px); opacity: 1; }
  .g-space .g-gap { opacity: 1; }
}

.sp-metrics {
  margin: 0.1rem 0 0;
  font: 500 0.72rem ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--muted);
}
.sp-metrics:empty { display: none; }

/* fluid mode: the decoded, speakable sentence + speak button */
.sp-decoded {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.65rem;
  border-radius: 10px;
  background: #0b1220;
  border: 1px solid rgba(56, 189, 248, 0.25);
}
.sp-decoded-label {
  font: 600 0.62rem ui-monospace, monospace;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.sp-decoded-text {
  flex: 1 1 8rem;
  min-width: 0;
  font: 700 1.05rem/1.35 system-ui, sans-serif;
  color: #7dd3fc;
}
.sp-decoded-text[data-fallback="1"] { color: #cbd5e1; font-style: italic; }
#spSpeak {
  flex: none;
  padding: 0.4rem 0.7rem;
  background: var(--accent, #38bdf8);
  color: #0f172a;
  border: 0;
  border-radius: 8px;
  font: 700 0.82rem system-ui, sans-serif;
  cursor: pointer;
}
#spSpeak:active { transform: scale(0.96); }
.sp-fluid {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
}
.sp-fluid input { margin: 0; }
.sp-beta {
  font: 700 0.6rem ui-monospace, monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #38bdf8;
  border: 1px solid rgba(56, 189, 248, 0.5);
  border-radius: 3px;
  padding: 0.05rem 0.3rem;
}
.sp-fluid-hint { flex-basis: 100%; color: #475569; font-size: 0.74rem; }
.sp-autospeak { display: inline-flex; align-items: center; gap: 0.25rem; font-size: 0.72rem; color: var(--muted); cursor: pointer; }
.sp-autospeak input { margin: 0; }

/* ---- spell-mode word drill (B4) --------------------------- */
.sp-drill-toggle {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
}
.sp-drill-toggle input { margin: 0; }
.sp-drill-toggle select {
  background: #1e293b;
  color: inherit;
  border: 1px solid #334155;
  border-radius: 6px;
  padding: 2px 4px;
  font: inherit;
  cursor: pointer;
}
.sp-drill {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #0b1220;
}
.sp-drill-label {
  font: 700 0.62rem ui-monospace, monospace;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.sp-drill-word {
  font: 800 1.15rem ui-monospace, monospace;
  letter-spacing: 0.16em;
  display: flex;
  gap: 0.05em;
}
.sp-drill-word .ltr { color: #475569; transition: color 0.15s ease; }
.sp-drill-word .ltr.hit { color: #4ade80; }
.sp-drill-word .ltr.miss { color: #f87171; }
.sp-drill-word.solved .ltr { color: #4ade80; }
#spDrillSkip {
  font: 600 0.72rem system-ui, sans-serif;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #1e293b;
  color: var(--muted);
  cursor: pointer;
}
#spDrillSkip:hover { border-color: var(--accent); }
.sp-drill-score { margin-left: auto; font: 700 0.8rem ui-monospace, monospace; color: var(--fg); white-space: nowrap; }

/* ---- read mode (receptive practice) ----------------------- */
.reader {
  flex: 0 1 auto;
  align-self: center;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  padding: 0.9rem 1rem;
  background: linear-gradient(180deg, #1e293b, #16202f);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 16px 40px -22px rgba(0, 0, 0, 0.6);
  max-width: 460px;
  margin: 0 auto;
  width: 100%;
  /* was unbounded — on a short phone screen the Check button could be pushed
     past body{overflow:hidden} with no way to scroll to it (Course mode,
     with the lesson path + header, was the worst case). */
  max-height: 100%;
  overflow-x: hidden; /* setting only overflow-y makes some browsers auto-enable x too */
  overflow-y: auto;
  overscroll-behavior: contain;
}
.rd-top { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.rd-cats { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.rd-cat {
  font: 600 0.72rem system-ui, sans-serif;
  padding: 0.22rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #0b1220;
  color: var(--muted);
  cursor: pointer;
  text-transform: capitalize;
}
.rd-cat.on { border-color: var(--accent, #38bdf8); color: #7dd3fc; background: #10263a; }
.rd-score { font: 800 1rem ui-monospace, monospace; color: var(--fg); white-space: nowrap; }

/* --- Course: teaching-order lessons with progress gating --- */
.rd-modes { display: flex; gap: 0.35rem; }
.rd-mode {
  font: 700 0.76rem system-ui, sans-serif;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #0b1220;
  color: var(--muted);
  cursor: pointer;
}
.rd-mode.on { border-color: var(--accent, #38bdf8); color: #0f172a; background: var(--accent, #38bdf8); }
.rd-course { display: flex; flex-direction: column; gap: 0.55rem; }
.rd-path { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.rd-step {
  font: 700 0.72rem system-ui, sans-serif;
  padding: 0.24rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #0b1220;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.3rem;
}
.rd-step.active { border-color: var(--accent, #38bdf8); color: #7dd3fc; background: #10263a; }
.rd-step.done { color: #4ade80; border-color: #1f7a46; }
.rd-step.locked { cursor: not-allowed; opacity: 0.5; }
.rd-step .tick { font-size: 0.85em; }
.rd-lesson-head { display: flex; align-items: baseline; justify-content: space-between; gap: 0.5rem; }
.rd-lesson-head strong { font: 800 0.95rem system-ui, sans-serif; color: var(--fg); }
.rd-lesson-prog { font: 700 0.8rem ui-monospace, monospace; color: var(--muted); white-space: nowrap; }
.rd-lesson-blurb { margin: 0; font: 500 0.8rem system-ui, sans-serif; color: var(--muted); }
.rd-bar { height: 6px; border-radius: 999px; background: #0b1220; border: 1px solid var(--line); overflow: hidden; }
.rd-bar > i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent, #38bdf8), #4ade80);
  transition: width 0.4s ease;
}
.rd-stage {
  flex: 0 1 auto;
  display: grid;
  place-items: center;
  background: #0b1220;
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: 12px;
  padding: 0.5rem;
}
#rdCanvas { width: min(100%, 260px); height: auto; aspect-ratio: 1; display: block; }
.rd-playbar { display: flex; align-items: center; gap: 0.7rem; font-size: 0.8rem; color: var(--muted); }
#rdPlay {
  background: var(--panel); color: var(--fg); border: 1px solid #475569;
  border-radius: 9px; padding: 0.4rem 0.8rem; font: 700 0.85rem system-ui, sans-serif; cursor: pointer;
}
#rdSpeed { background: #1e293b; color: inherit; border: 1px solid #334155; border-radius: 6px; padding: 2px 4px; }
.rd-len { margin-left: auto; font: 700 1rem ui-monospace, monospace; letter-spacing: 0.15em; color: #475569; }
.rd-answer {
  display: flex; gap: 0.5rem; flex-wrap: wrap;
  /* .reader can now scroll (see above) — pin Check/Reveal to the bottom of
     that scroll area so they're never the part that gets scrolled away. */
  position: sticky; bottom: 0;
  background: linear-gradient(180deg, #1e293b, #16202f);
  padding-top: 0.4rem; margin-top: -0.4rem;
}
#rdInput {
  flex: 1 1 8rem; min-width: 0;
  padding: 0.55rem 0.7rem;
  background: #0b1220; color: var(--fg);
  border: 1px solid #334155; border-radius: 9px;
  font: 600 1rem system-ui, sans-serif;
}
#rdInput:focus { outline: 2px solid var(--accent, #38bdf8); outline-offset: 1px; }
.rd-answer button {
  padding: 0.55rem 0.9rem; border: 0; border-radius: 9px; cursor: pointer;
  font: 700 0.88rem system-ui, sans-serif;
}
.rd-answer button[type="submit"] { background: var(--accent, #38bdf8); color: #0f172a; }
#rdReveal { background: var(--panel); color: var(--muted); border: 1px solid #475569; }
.rd-feedback { margin: 0; min-height: 1.4em; font: 700 0.95rem system-ui, sans-serif; }
.rd-feedback.good { color: #4ade80; }
.rd-feedback.bad { color: #f87171; }
.sp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 0.3rem;
  padding: 0.3rem 0.6rem 0.7rem;
  max-height: 40vh;
  overflow-y: auto;
}
.sp-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: 8px;
  background: #111c2e;
  color: inherit;
  font: inherit;
  cursor: zoom-in;
}
.sp-cell:hover { border-color: var(--accent); }
.sp-cell img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 5px;
  background: #020617;
}
.sp-cell b { font-size: 0.8rem; color: var(--fg); }

@media (min-width: 700px) and (min-height: 420px) and (orientation: landscape) {
  .speller {
    width: clamp(260px, 32vw, 400px);
    align-self: center;
    max-height: 100%;
  }
  .sp-text { max-height: 12rem; }
}

/* ---- controls --------------------------------------------- */

.controls {
  flex: none;
  width: calc(100% - 1.6rem);
  max-width: 1180px;
  margin: 0.5rem auto 0.6rem;
  padding: 0.4rem 0.7rem;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-wrap: wrap;
  background: linear-gradient(180deg, #1b2637, #151f2e);
  border: 1px solid var(--line);
  border-radius: 12px;
}
#stopBtn, #flipBtn {
  background: var(--panel); color: var(--fg); border: 1px solid var(--line);
  border-radius: 9px; padding: 0.5rem 0.95rem; font: 600 0.9rem system-ui, sans-serif; cursor: pointer;
  transition: transform 0.12s ease, border-color 0.14s;
}
#stopBtn { border-color: #475569; }
@media (hover: hover) and (pointer: fine) {
  #stopBtn:hover, #flipBtn:hover { border-color: var(--accent); transform: translateY(-1px); }
}
#stopBtn:active, #flipBtn:active { transform: translateY(0) scale(0.96); }
button:disabled { opacity: 0.5; cursor: default; }
.ghost-toggle {
  font-size: 0.78rem; color: var(--muted);
  display: inline-flex; align-items: center; gap: 0.35rem; cursor: pointer;
}
.hand-pick {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.75rem;
  color: var(--muted);
}
.hand-pick span { font-weight: 700; }
.hand-pick button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  border-radius: 7px;
  padding: 0.28rem 0.55rem;
  font: 700 0.75rem system-ui, sans-serif;
  cursor: pointer;
  transition: background 0.14s, color 0.14s, border-color 0.14s;
}
.hand-pick button.on {
  background: linear-gradient(180deg, #7dd3fc, var(--accent));
  border-color: #7dd3fc;
  color: #0b1220;
}
.icon-btn {
  margin-left: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 9px;
  width: 2.2rem; height: 2.2rem; font-size: 1rem; cursor: pointer; line-height: 1;
  transition: transform 0.12s ease, border-color 0.14s;
}
@media (hover: hover) and (pointer: fine) {
  .icon-btn:hover { border-color: var(--accent); transform: translateY(-1px); }
}
.icon-btn:active { transform: scale(0.9); }
.icon-btn.muted { opacity: 0.55; }

/* ---- reward toast ---------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 2.2rem;
  transform: translate(-50%, 140%);
  padding: 0.7rem 1.3rem;
  border-radius: 999px;
  background: linear-gradient(180deg, #16241a, #0d1a12);
  border: 1px solid rgba(34, 197, 94, 0.5);
  box-shadow: 0 12px 40px -10px rgba(34, 197, 94, 0.5);
  color: #dcfce7;
  font: 800 1rem system-ui, sans-serif;
  letter-spacing: 0.01em;
  opacity: 0;
  pointer-events: none;
  z-index: 70;
  transition: transform 0.45s cubic-bezier(0.2, 1.5, 0.4, 1), opacity 0.3s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---- harder letters: tint the grid chips ------------------- */
.chip.hard {
  border-color: #b45309;
  box-shadow: inset 0 0 0 1px rgba(245, 158, 11, 0.35);
}
.chip.hard::after {
  content: "";
  position: absolute;
  top: 3px; right: 3px;
  width: 4px; height: 4px;
  border-radius: 50%;
  background: var(--close);
}
.chip { position: relative; }
.chip.hard.on { box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.28), 0 6px 16px -4px rgba(56, 189, 248, 0.5); }

/* motion letters (J, Z) — traced, not held */
.chip.motion {
  border-color: #7c3aed;
  box-shadow: inset 0 0 0 1px rgba(167, 139, 250, 0.4);
}
.chip.motion::after {
  content: "↻";
  position: absolute;
  top: -1px; right: 2px;
  font-size: 0.6rem;
  line-height: 1;
  color: #a78bfa;
}
.chip.motion.on::after { color: #0b1220; }

/* ---- challenge: lives + skip ------------------------------- */
.score-badge .lives { font-size: 0.85rem; letter-spacing: 1px; }
.ch-skip {
  position: absolute;
  right: 0.8rem;
  bottom: 3.2rem;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  border: 1px solid rgba(248, 113, 113, 0.5);
  background: rgba(2, 6, 23, 0.8);
  backdrop-filter: blur(6px);
  color: #fecaca;
  font: 700 0.78rem system-ui, sans-serif;
  cursor: pointer;
  pointer-events: auto;
  z-index: 46;
}
.ch-skip:active { transform: scale(0.95); }
.ch-banner.miss { color: var(--off); }

/* ---- A→Z run "next letter" bridge ------------------------- */
.az-next {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  padding: 0.9rem 1.6rem;
  border-radius: 16px;
  background: rgba(2, 6, 23, 0.82);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(56, 189, 248, 0.4);
  font: 800 1.2rem system-ui, sans-serif;
  color: #e2e8f0;
  pointer-events: none;
  animation: go-flash 0.4s ease-out;
}
.az-next b { color: var(--accent); font-size: 1.5rem; }

/* ---- enlarged demo -------------------------------------- */
.demo-zoom {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: grid;
  place-items: center;
  gap: 0.8rem;
  background: rgba(2, 6, 23, 0.88);
  backdrop-filter: blur(4px);
  cursor: zoom-out;
}
.demo-zoom canvas,
.demo-zoom-img {
  width: min(80vw, 80vh, 460px);
  height: min(80vw, 80vh, 460px);
  background: #0b1220;
  border-radius: 20px;
  border: 1px solid var(--line);
}
.demo-zoom-img { object-fit: cover; }
.demo-zoom p { margin: 0; color: var(--muted); font-size: 0.85rem; }

/* ---- A→Z results + first-visit intro (shared card look) --- */
.run-card, .intro {
  position: fixed;
  inset: 0;
  z-index: 130;
  display: grid;
  place-items: center;
  padding: 1.2rem;
  background: rgba(2, 6, 23, 0.86);
  backdrop-filter: blur(6px);
}
.run-card-inner, .intro-inner {
  max-width: 26rem;
  width: 100%;
  background: linear-gradient(180deg, #1e293b, #16202f);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.4rem 1.5rem;
  box-shadow: 0 30px 80px -30px rgba(0, 0, 0, 0.8);
  text-align: center;
}
.run-card-title { margin: 0 0 0.5rem; font: 800 1.3rem system-ui, sans-serif; }
.run-card-body { margin: 0 0 1.1rem; color: #cbd5e1; font-size: 0.9rem; line-height: 1.6; }
.intro-inner { text-align: left; }
.intro-privacy {
  margin: 0 0 1rem;
  padding: 0.65rem 0.8rem;
  background: rgba(34, 197, 94, 0.1);
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: 10px;
  font-size: 0.85rem;
  line-height: 1.45;
  color: #d1fae5;
}
.intro-privacy b { color: #86efac; }
.intro-inner h2 { margin: 0 0 0.8rem; font-size: 1.25rem; text-align: center; }
.intro-inner .cta:disabled { opacity: 0.55; cursor: default; }
#introCloseTimer { opacity: 0.75; font-weight: 400; }
.intro-inner ol { margin: 0 0 1.2rem; padding-left: 1.2rem; }
.intro-inner li { margin: 0 0 0.65rem; font-size: 0.9rem; line-height: 1.5; color: #cbd5e1; }
.intro-inner li b { color: var(--fg); }
.intro-inner .cta { display: block; width: 100%; }

@media (prefers-reduced-motion: reduce) {
  .cta { animation: none; }
  .chip, .cta, .icon-btn, #stopBtn, #flipBtn, .viewport, .letter-badge, .toast {
    transition: opacity 0.15s;
  }
  .viewport.celebrate, .letter-badge.pop, .meter-fill.correct,
  .viewport[data-state="idle"] { animation: none; }
  .chip.on { transform: none; }
  .toast.show { transform: translate(-50%, 0); }
  /* new (Spell/Read) surfaces */
  .sp-ring, .g-anim .h, .g-ring, .g-gap, .meter-fill, .rd-bar > i, .sp-drill-word .ltr { animation: none !important; transition: none !important; }
}

/* ---- accessibility --------------------------------------- */

/* one consistent, high-visibility keyboard focus ring everywhere */
a:focus-visible,
button:focus-visible,
select:focus-visible,
input:focus-visible,
summary:focus-visible,
[tabindex]:focus-visible,
.chip:focus-visible,
.mode-btn:focus-visible,
.rd-cat:focus-visible,
.rd-mode:focus-visible,
.rd-step:focus-visible,
.sp-cell:focus-visible {
  outline: 3px solid #fde047;
  outline-offset: 2px;
  border-radius: 6px;
}
.mode-btn:focus-visible { outline-offset: -1px; }

/* stronger palette when the OS asks for more contrast */
/* High contrast: was OS-preference-only (prefers-contrast: more). Now also a
   manual in-app toggle (#contrastBtn) — main.js applies/removes this same
   class on <html> based on either signal, so there's one set of overrides
   instead of two copies to keep in sync. */
:root.high-contrast {
  --line: #64748b;
  --muted: #cbd5e1;
}
.high-contrast .chip, .high-contrast .mode-btn, .high-contrast .rd-cat,
.high-contrast .sp-controls button, .high-contrast .sp-fold, .high-contrast .sp-decoded,
.high-contrast #stopBtn, .high-contrast #flipBtn, .high-contrast .controls,
.high-contrast .reference, .high-contrast .speller, .high-contrast .reader {
  border-color: #94a3b8;
}
.high-contrast .sp-pending { color: #cbd5e1; border-bottom-color: #94a3b8; }
.high-contrast .rd-len { color: #94a3b8; }
.high-contrast .rd-mode, .high-contrast .rd-step { border-color: #94a3b8; }
.high-contrast .sp-drill, .high-contrast .sp-drill-toggle select { border-color: #94a3b8; }
.high-contrast .sp-drill-word .ltr { color: #cbd5e1; }
.high-contrast .dim, .high-contrast .g-note, .high-contrast .sp-fluid-hint,
.high-contrast .sp-decoded-label, .high-contrast .sp-drill-label { color: #cbd5e1 !important; }

.contrast-btn[aria-pressed="true"] { color: var(--accent); }

/* a visually-hidden helper for screen-reader-only text */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
