/* test.css -- the hero screen. "Keycap & Switch": calm, premium base with a
   rewarding layer on top (combo streak, clean-word pop, speed tiers, finish
   burst). Everything that animates per-keystroke animates `transform` or
   `opacity` only, so it stays on the compositor and never forces layout --
   the previous version's caret animation read `offsetWidth` on every key,
   which forced a full synchronous reflow of the whole word list. */

.test-page {
  display: flex;
  flex-direction: column;
  gap: var(--s-6);
  max-width: 860px;
  margin: 4vh auto 0;
}

/* ---------- mode selector ---------- */

.test-page__modes {
  display: flex;
  gap: var(--s-8);
  justify-content: center;
  align-items: center;
  font-family: var(--f-mono);
  font-size: var(--t-sm);
  background: var(--plate);
  border-radius: var(--r-lg);
  padding: var(--s-3) var(--s-6);
  box-shadow: var(--e-1);
}

.mode-group {
  display: flex;
  align-items: center;
  gap: var(--s-2);
}

.mode-group__label {
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: var(--t-xs);
  margin-right: var(--s-1);
}

.mode-group button {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: var(--s-1) var(--s-3);
  border-radius: var(--r-sm);
  font-family: inherit;
  font-size: inherit;
  transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}

.mode-group button:hover {
  color: var(--legend);
  background: var(--plate-hi);
}

.mode-group button.active {
  color: var(--housing);
  background: var(--hit);
}

.mode-group select {
  background: none;
  border: none;
  color: var(--muted);
  font-family: inherit;
  font-size: inherit;
  padding: var(--s-1) var(--s-2);
  border-radius: var(--r-sm);
}

.mode-group select:focus-visible { color: var(--legend); }

.sound-toggle {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-left: auto;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  font-size: var(--t-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: var(--s-1) var(--s-2);
  border-radius: var(--r-sm);
}

.sound-toggle:hover { color: var(--legend); }

.sound-toggle__icon {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.sound-toggle[aria-pressed="true"] { color: var(--pace); }

.sound-toggle[aria-pressed="true"] .sound-toggle__icon {
  background: var(--pace);
  box-shadow: 0 0 8px var(--pace);
}

/* ---------- surface ---------- */

.test-surface {
  position: relative;
  background: var(--plate);
  border-radius: var(--r-lg);
  padding: var(--s-8);
  box-shadow: var(--e-1);
  transition: box-shadow var(--dur) var(--ease);
}

/* Speed tiers: the surface picks up a progressively warmer rim as live wpm
   climbs. Set as a data attribute by the UI on each metrics tick. */
.test-surface[data-tier="1"] { box-shadow: var(--e-1), inset 0 0 0 1px rgb(94 224 192 / 0.25); }
.test-surface[data-tier="2"] { box-shadow: var(--e-1), inset 0 0 0 1px rgb(94 224 192 / 0.5); }
.test-surface[data-tier="3"] { box-shadow: var(--e-1), inset 0 0 0 1px rgb(242 179 61 / 0.6); }

.test-surface__hud {
  display: flex;
  align-items: baseline;
  gap: var(--s-6);
  margin-bottom: var(--s-6);
  font-family: var(--f-display);
}

.hud-stat {
  display: flex;
  align-items: baseline;
  gap: var(--s-1);
  color: var(--muted);
  font-size: var(--t-sm);
}

.hud-stat span {
  font-size: var(--t-lg);
  color: var(--legend);
  font-variant-numeric: tabular-nums;
}

.hud-stat--primary span {
  font-size: var(--t-2xl);
  color: var(--pace);
  line-height: 1;
}

.hud-stat--timer { margin-left: auto; }

.hud-stat--timer span { color: var(--hit); }

/* Timer pulses in the last 5 seconds. */
.hud-stat--timer.is-urgent span {
  color: var(--miss);
  animation: timer-pulse 1s var(--ease) infinite;
}

@keyframes timer-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

/* ---------- combo streak ---------- */

/* Sits in the HUD flex row, not absolutely positioned -- an earlier version
   pinned it to the surface's top-right corner where it printed straight on
   top of the countdown timer. Reserves its width even when hidden so the
   timer doesn't shift sideways when a streak starts. */
.combo {
  display: flex;
  align-items: baseline;
  gap: var(--s-2);
  font-family: var(--f-display);
  opacity: 0;
  transform: scale(0.9);
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
  pointer-events: none;
}

.combo.is-visible {
  opacity: 1;
  transform: scale(1);
}

.combo__count {
  font-size: var(--t-xl);
  color: var(--hit);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.combo__label {
  font-size: var(--t-xs);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* Milestone bump every 10 -- scales up and settles. */
.combo.is-milestone .combo__count {
  animation: combo-bump 380ms var(--ease);
  text-shadow: 0 0 16px rgb(242 179 61 / 0.7);
}

@keyframes combo-bump {
  0% { transform: scale(1); }
  45% { transform: scale(1.45); }
  100% { transform: scale(1); }
}

/* ---------- words ---------- */

.test-surface__viewport {
  position: relative;
  height: calc(3 * 1.9em);
  overflow: hidden;
  font-family: var(--f-mono);
  font-size: var(--t-xl);
  line-height: 1.9;
}

/* Top and bottom lines fade out so the eye stays on the active line. */
.test-surface__viewport::before,
.test-surface__viewport::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 1.9em;
  pointer-events: none;
  z-index: 2;
}

.test-surface__viewport::before {
  top: 0;
  background: linear-gradient(var(--plate), rgb(27 34 44 / 0));
  opacity: 0.75;
}

.test-surface__viewport::after {
  bottom: 0;
  background: linear-gradient(rgb(27 34 44 / 0), var(--plate));
  opacity: 0.75;
}

.test-surface__track {
  position: relative;
  color: var(--muted);
  user-select: none;
  transition: transform 240ms var(--ease);
  will-change: transform;
}

.word {
  display: inline-block;
  margin-right: 0.45em;
  position: relative;
}

.char {
  color: var(--muted);
  transition: color var(--dur-fast) linear;
}

.char--correct { color: var(--legend); }

.char--incorrect {
  color: var(--miss);
  text-decoration: underline;
  text-decoration-color: var(--miss);
  text-underline-offset: 3px;
}

.char--extra {
  color: var(--miss);
  opacity: 0.75;
  text-decoration: underline;
  text-decoration-color: var(--miss);
}

.word--incorrect {
  text-decoration: underline;
  text-decoration-color: rgb(255 90 78 / 0.7);
  text-underline-offset: 6px;
}

/* The last word of a fixed-length passage -- a quiet cue that the finish
   line is here, before a student gets stuck on it wondering what commits it. */
.word--last {
  text-decoration: underline;
  text-decoration-color: var(--pace);
  text-decoration-style: dashed;
  text-underline-offset: 6px;
}

/* Clean word committed: a quick lift. Transform-only, so it composites. */
.word--pop { animation: word-pop 300ms var(--ease); }

@keyframes word-pop {
  0% { transform: translateY(0) scale(1); }
  40% { transform: translateY(-3px) scale(1.04); }
  100% { transform: translateY(0) scale(1); }
}

/* Wrong key: the word nudges rather than the whole surface. */
.word--shake { animation: word-shake 160ms var(--ease); }

@keyframes word-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

/* ---------- caret ---------- */

/* Positioned absolutely and moved with `transform` only -- it is never
   inserted into the character flow, which is what previously made it
   impossible to move (insertBefore against a stale child index) and forced
   a relayout of every following word. */
/* Two transform layers on purpose: the outer .caret owns POSITION (glides
   between characters), the inner .caret__key owns the keycap DEPRESS. If
   both lived on one element the depress animation would clobber the
   position transform mid-glide and the caret would snap to the origin. */
.caret {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  transform: translate(0, 0);
  transition: transform 90ms var(--ease);
  will-change: transform;
}

/* Height and vertical offset are set from the measured glyph box by
   WordsView.fitCaretToGlyph(). The em fallbacks only apply before the first
   render. Do not reintroduce a fixed margin-top here: the caret is already
   positioned at the character's offsetTop, which includes half-leading. */
.caret__key {
  display: block;
  width: 2px;
  height: var(--caret-h, 1.25em);
  margin-top: var(--caret-top, 0);
  background: var(--hit);
  border-radius: 1px;
  transform-origin: center top;
  transition: background var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
  animation: caret-blink 1.1s steps(1) infinite;
}

/* Blinking only while idle -- a blinking caret under active typing reads as
   flicker. The UI removes .is-idle on the first keystroke of each word. */
.caret:not(.is-idle) .caret__key { animation: none; }

@keyframes caret-blink {
  0%, 45% { opacity: 1; }
  50%, 95% { opacity: 0.25; }
  100% { opacity: 1; }
}

.caret.is-depressed .caret__key { animation: caret-depress var(--dur-fast) var(--ease); }

@keyframes caret-depress {
  0% { transform: translateY(0) scaleY(1); }
  40% { transform: translateY(2px) scaleY(0.82); }
  100% { transform: translateY(0) scaleY(1); }
}

.test-surface[data-tier="2"] .caret__key { background: var(--pace); }
.test-surface[data-tier="3"] .caret__key { background: var(--hit); box-shadow: 0 0 10px var(--hit); }

/* ---------- travel bar ---------- */

.travel-bar {
  margin-top: var(--s-6);
  height: 4px;
  background: var(--housing);
  border-radius: 2px;
  overflow: hidden;
}

.travel-bar__fill {
  height: 100%;
  width: 100%;
  background: var(--hit);
  border-radius: 2px;
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform var(--dur) var(--ease);
}

/* ---------- overlay / input / finish ---------- */

.test-surface__overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgb(16 20 26 / 0.8);
  backdrop-filter: blur(3px);
  color: var(--muted);
  border-radius: var(--r-lg);
  cursor: text;
  font-size: var(--t-sm);
  z-index: 4;
}

.test-surface__overlay--visible { display: flex; }

.focus-prompt {
  padding: var(--s-3) var(--s-6);
  border-radius: var(--r-md);
  background: var(--plate-hi);
  box-shadow: var(--e-key);
}

.test-surface__input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
}

.finish-burst {
  position: absolute;
  inset: 0;
  border-radius: var(--r-lg);
  pointer-events: none;
  opacity: 0;
  z-index: 5;
  background: radial-gradient(circle at 50% 55%, rgb(242 179 61 / 0.35), rgb(242 179 61 / 0) 60%);
}

.finish-burst.is-firing { animation: finish-burst 620ms var(--ease); }

@keyframes finish-burst {
  0% { opacity: 0; transform: scale(0.9); }
  35% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.05); }
}

.test-hint {
  text-align: center;
  color: var(--muted);
  font-size: var(--t-xs);
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  .caret,
  .test-surface__track,
  .travel-bar__fill { transition: none; }

  .caret__key,
  .caret.is-depressed .caret__key,
  .word--pop,
  .word--shake,
  .combo.is-milestone .combo__count,
  .hud-stat--timer.is-urgent span,
  .finish-burst.is-firing { animation: none; }
}

/* ---------------------------------------------------------------------------
   Sprint progress and deadline

   A sprint quest can be thousands of words long. The travel bar fills across
   the CURRENT WORD, which at that scale is full and empty again every second
   and tells a student nothing about how far there is to go. This is progress
   through the passage, next to a clock that actually ends the round.
   --------------------------------------------------------------------------- */

.sprint-progress {
  margin: 0 0 var(--s-4);
}

.sprint-progress__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--s-3);
  margin-bottom: var(--s-1);
  font-size: var(--t-xs);
  color: var(--muted);
}

.sprint-progress__pct {
  font-family: var(--f-mono);
  color: var(--pace);
}

.sprint-progress__track {
  height: 0.45rem;
  border-radius: var(--r-sm);
  background: var(--plate-hi);
  overflow: hidden;
}

.sprint-progress__fill {
  height: 100%;
  width: 0;
  background: var(--pace);
  transition: width var(--dur-fast) var(--ease);
}

/* The clock is the one thing on the HUD that can end the run, so in the last
   half minute it stops being a quiet readout. */
.hud-stat--timer.is-urgent {
  color: var(--miss);
}

.drill-result__headline--timeup {
  color: var(--legend);
}
