/* =========================================================================
   Typing Master — Stylesheet
   -------------------------------------------------------------------------
   1. Tokens (dark + light)      5. Lesson panel
   2. Reset and base             6. Results + review
   3. Top bar and controls       7. Footer
   4. Practice screen            8. Responsive · motion · print
   ========================================================================= */

/* ------------------------------------------------------------ 1. tokens */
:root {
  --bg: #0e1116;
  --bg-raised: #141922;
  --panel: #161c25;
  --line: rgba(255, 255, 255, 0.10);
  --line-strong: rgba(255, 255, 255, 0.18);
  --text: #e8ecf2;
  --text-dim: #9aa5b4;
  --text-faint: #6a7482;
  --accent: #63d3a3;
  --accent-ink: #06251a;
  --correct: #86d7ae;
  --wrong: #ff7b72;
  --wrong-soft: rgba(255, 123, 114, 0.16);
  --focus: #7aa2ff;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.45);
  --radius: 12px;

  --passage-size: 1.375rem;
  --passage-line: 2.05;
  --passage-lines: 3;

  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", "Courier New", monospace;

  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

:root[data-theme="light"] {
  --bg: #fbfbfa;
  --bg-raised: #ffffff;
  --panel: #ffffff;
  --line: rgba(18, 24, 33, 0.12);
  --line-strong: rgba(18, 24, 33, 0.22);
  --text: #18202b;
  --text-dim: #5a6572;
  --text-faint: #8b95a2;
  --accent: #12855f;
  --accent-ink: #ffffff;
  --correct: #14795a;
  --wrong: #c0362c;
  --wrong-soft: rgba(192, 54, 44, 0.12);
  --focus: #2f6bdc;
  --shadow: 0 18px 48px rgba(18, 24, 33, 0.14);
}

/* ----------------------------------------------------- 2. reset + base */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

h1, h2, h3, p, dl, dd, ul { margin: 0; }
ul { padding: 0; list-style: none; }
button, select, input { font: inherit; color: inherit; }

:where(a, button, select, input, [tabindex]):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 6px;
}

.skip-link {
  position: absolute;
  left: 12px;
  top: -60px;
  z-index: 50;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  transition: top 120ms var(--ease);
}
.skip-link:focus { top: 12px; }

.noscript {
  margin: 24px auto;
  max-width: 40rem;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text-dim);
}

/* -------------------------------------------------- 3. bar + controls */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(16px, 4vw, 32px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }

/* The logo doubles as the home button: it returns you to your place in the
   course. Padded out and pulled back by the same amount so the mark stays
   optically flush with the page edge. */
.brand-btn {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  margin: -5px;
  padding: 5px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  transition: background-color 120ms var(--ease), border-color 120ms var(--ease);
}
.brand-btn:hover { background: var(--bg-raised); border-color: var(--line); }
.brand-btn:active { background: var(--panel); }

.brand-mark {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 26px; height: 26px;
  border-radius: 7px;
  background: var(--accent);
  color: var(--accent-ink);         /* the mark is drawn in currentColor */
}
.brand-mark svg { display: block; }

.brand-name {
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.field { display: inline-flex; align-items: center; gap: 6px; }
.field[hidden] { display: none; }

.field-label {
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
}

select {
  appearance: none;
  padding: 6px 26px 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: var(--bg-raised);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right 12px center, right 7px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  color: var(--text);
  font-size: 0.83rem;
  cursor: pointer;
  transition: border-color 120ms var(--ease), background-color 120ms var(--ease);
}
select:hover { border-color: var(--line-strong); }

.btn {
  padding: 6px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.83rem;
  cursor: pointer;
  transition: color 120ms var(--ease), border-color 120ms var(--ease), background-color 120ms var(--ease);
}
.btn:hover { color: var(--text); border-color: var(--line-strong); background: var(--bg-raised); }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent); color: var(--accent-ink); filter: brightness(1.06); }

.btn-download {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  white-space: nowrap;
}
.btn-download:hover { filter: brightness(1.06); background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Hide the download button when running as the desktop app */
html[data-desktop="1"] .btn-download { display: none; }

/* Reload button is only relevant in the desktop app */
.btn-reload { display: none; }
html[data-desktop="1"] .btn-reload { display: inline-block; }



.btn-icon {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  padding: 0;
  font-size: 1.05rem;
  line-height: 1;
}

.theme-glyph {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--text-dim);
  box-shadow: 0 0 0 2px transparent, inset -4px -2px 0 0 var(--bg-raised);
}
:root[data-theme="light"] .theme-glyph { box-shadow: inset -4px -2px 0 0 var(--bg-raised); }

/* ------------------------------------------------ 4. practice screen */
.stage {
  flex: 1;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: clamp(28px, 6vh, 64px) clamp(16px, 5vw, 32px) 64px;
}

.session-head { margin-bottom: 22px; }

.lesson-label {
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}

.lesson-title {
  margin: 6px 0 8px;
  font-size: clamp(1.3rem, 3.4vw, 1.65rem);
  font-weight: 600;
  letter-spacing: -0.015em;
}

.objective {
  max-width: 62ch;
  color: var(--text-dim);
  font-size: 0.92rem;
}

.key-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }

.key-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-width: 24px;
  height: 22px;
  padding: 0 7px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--bg-raised);
  font-family: var(--mono);
  font-size: 0.74rem;
  color: var(--text-dim);
}
.key-chip.is-new {
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
  color: var(--accent);
}
.key-chip.is-new::after {
  content: "new";
  margin-left: 5px;
  font-family: var(--sans);
  font-size: 0.6rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.8;
}

.statbar {
  display: flex;
  gap: clamp(18px, 5vw, 34px);
  padding-bottom: 16px;
  margin-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.stat { display: flex; align-items: baseline; gap: 6px; }

.stat-value {
  font-family: var(--mono);
  font-size: 1.18rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.stat-label {
  font-size: 0.72rem;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.statbar.is-hidden { visibility: hidden; }

.text-frame { position: relative; margin-top: 22px; }

.text-viewport {
  position: relative;
  height: calc(var(--passage-line) * var(--passage-lines) * 1rem * 1.4);
  overflow: hidden;
  border-radius: 10px;
  transition: box-shadow 140ms var(--ease);
}

/* A quiet ring appears only while the typing field is not focused, so it is
   obvious that clicking or typing brings it back. */
.text-viewport.is-idle { box-shadow: inset 0 0 0 1px var(--line); }

/* Short passages sit in the middle of the window instead of clinging to the
   top of it, so the screen does not look half empty. */
.text-viewport.is-short {
  display: flex;
  align-items: center;
}

.text {
  position: relative;          /* containing block for caret offsetTop maths */
  font-family: var(--mono);
  font-size: var(--passage-size);
  line-height: var(--passage-line);
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text-faint);
  transform: translateY(0);
  transition: transform 110ms var(--ease);
  will-change: transform;
}

.tm-ch { color: var(--text-faint); }
.tm-ch.is-correct { color: var(--correct); }
.tm-ch.is-wrong { color: var(--wrong); background: var(--wrong-soft); border-radius: 3px; }
/* Colour changes ease out over a few frames instead of snapping: the
   passage reads as fluid while each keystroke still lands instantly. */
.tm-ch {
  transition: color 70ms linear, background-color 70ms linear;
}
@media (prefers-reduced-motion: reduce) {
  .tm-ch { transition: none; }
}

/* The newline marker: a faint ↵ that keeps line breaks legible. */
.tm-ch.is-nl { color: var(--text-faint); opacity: 0.45; }
.tm-ch.is-nl.is-correct { color: var(--correct); opacity: 0.25; }

/* Caret styles, chosen in Settings.
   The highlight lives on a floating element that glides between characters
   and wraps between lines; .is-current only lifts the character colour. */
.text[data-caret="block"] .tm-ch.is-current {
  color: var(--text);
}
.text[data-caret="line"] .tm-ch.is-current {
  color: var(--text);
}
.text[data-caret="underline"] .tm-ch.is-current {
  color: var(--text);
}
.text[data-caret="none"] .tm-ch.is-current { color: var(--text-dim); }

/* The gliding caret itself. Size and position come from the app's caret maths
   (it knows each character's grid line); CSS only animates the journey. */
.float-caret {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  pointer-events: none;
  background: var(--accent);
  opacity: 0.92;
  border-radius: 2px;
  transition: transform 90ms cubic-bezier(0.22, 0.61, 0.36, 1);
  will-change: transform;
}
.text[data-caret="block"] .float-caret {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 45%, transparent);
  border-radius: 3px;
  opacity: 1;
}
@media (prefers-reduced-motion: reduce) {
  .float-caret { transition: none; }
}

/* The blinking block reads as "type here" without any layout movement. */
.text[data-caret="block"] .tm-ch.is-current::after,
.text[data-caret="none"] .tm-ch.is-current::after { content: none; }

.caret-blink { animation: caret-blink 1.1s steps(2, start) infinite; }
@keyframes caret-blink { 50% { opacity: 0.45; } }

/* Invisible real input sitting over the passage. */
.capture {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  border: 0;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  font-size: 16px;            /* stops iOS zooming when the keyboard opens */
  line-height: var(--passage-line);
  opacity: 0;
  outline: none;
  touch-action: manipulation;
}
.capture::selection { background: transparent; }

.text-viewport.is-idle .capture { cursor: text; }

/* A wrong keystroke nudges the passage sideways for a blink — felt, not seen.
   Uses `translate`, so it composes with the scroll transform on .text. */
@keyframes tm-wobble {
  0% { translate: 0 0; }
  30% { translate: -1px 0; }
  65% { translate: 1px 0; }
  100% { translate: 0 0; }
}
.text-viewport.is-wobble { animation: tm-wobble 150ms linear; }
@media (prefers-reduced-motion: reduce) {
  .text-viewport.is-wobble { animation: none; }
}

.text-hint {
  margin-top: 14px;
  font-size: 0.78rem;
  color: var(--text-faint);
}

.caps-hint {
  margin-top: 12px;
  padding: 6px 10px;
  border: 1px solid color-mix(in srgb, var(--wrong) 40%, transparent);
  border-radius: 8px;
  background: var(--wrong-soft);
  color: var(--wrong);
  font-size: 0.78rem;
}
.caps-hint[hidden] { display: none; }

.progress {
  height: 3px;
  margin-top: 22px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 140ms var(--ease);
}

.status-line {
  min-height: 1.4em;
  margin-top: 12px;
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* ------------------------------------------------------ 5. lesson panel */
dialog {
  padding: 0;
  border: none;
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgba(6, 9, 13, 0.62);
  animation: backdrop-in 220ms ease;
}

/* Dialogs arrive with a small, quick motion: the results card rises into
   place, the lesson panel slides in from the right. `translate` is used so
   the animation never fights a transform used for layout scrolling. */
dialog[open] { animation: dialog-in 220ms cubic-bezier(0.22, 0.61, 0.36, 1); }
@keyframes dialog-in {
  from { opacity: 0; translate: 0 14px; }
}
.panel[open] { animation-name: panel-in; }
@keyframes panel-in {
  from { opacity: 0; translate: 24px 0; }
}
@keyframes backdrop-in {
  from { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  dialog[open], dialog::backdrop { animation: none; }
}

.panel {
  position: fixed;
  inset: 0 0 0 auto;
  margin: 0;
  width: min(430px, 100vw);
  max-width: 100vw;
  height: 100%;
  max-height: 100%;
  border-left: 1px solid var(--line);
}

.panel-inner { display: flex; flex-direction: column; height: 100%; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 12px;
}

.panel-title { font-size: 1.02rem; font-weight: 600; }

.panel-tabs {
  display: flex;
  gap: 4px;
  padding: 0 20px 12px;
  border-bottom: 1px solid var(--line);
}

.tab {
  padding: 6px 10px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text-faint);
  font-size: 0.8rem;
  cursor: pointer;
}
.tab.is-active { color: var(--text); border-color: var(--line); background: var(--bg-raised); }

.panel-view { flex: 1; overflow-y: auto; padding: 16px 20px 28px; }
.panel-view[hidden] { display: none; }

.panel-subtitle {
  margin: 22px 0 8px;
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.stage-block + .stage-block { margin-top: 18px; }

.stage-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}
.stage-head h3 { font-size: 0.9rem; font-weight: 600; }
.stage-count { font-size: 0.74rem; color: var(--text-faint); font-variant-numeric: tabular-nums; }

.stage-summary { font-size: 0.76rem; color: var(--text-faint); margin-bottom: 10px; }

.lesson-list { display: flex; flex-direction: column; gap: 4px; }

.lesson-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: transparent;
  color: var(--text-dim);
  text-align: left;
  cursor: pointer;
  transition: background-color 110ms var(--ease), border-color 110ms var(--ease), color 110ms var(--ease);
}
.lesson-row:hover { background: var(--bg-raised); color: var(--text); border-color: var(--line); }
.lesson-row.is-current { border-color: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--text); }

.lesson-name { font-size: 0.86rem; }
.lesson-meta { font-size: 0.72rem; color: var(--text-faint); font-variant-numeric: tabular-nums; }

.empty { font-size: 0.78rem; color: var(--text-faint); }

.settings { display: flex; flex-direction: column; gap: 10px; }
.settings .field { justify-content: space-between; }
.settings .field-label { text-transform: none; letter-spacing: 0; font-size: 0.8rem; color: var(--text-dim); }
.field-check { gap: 10px; }
.field-check label { font-size: 0.8rem; color: var(--text-dim); cursor: pointer; }
.field-check input { accent-color: var(--accent); width: 15px; height: 15px; }

/* ------------------------------------------------ 6. results + review */
.results {
  width: min(560px, calc(100vw - 32px));
  max-width: none;
  margin: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--panel);
}

.results-body {
  padding: clamp(20px, 4vw, 30px);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

.results-head { position: relative; margin-bottom: 18px; }
.results-kicker {
  font-size: 0.7rem;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}
.results-title { margin-top: 4px; font-size: 1.2rem; font-weight: 600; }
.results-close { position: absolute; top: -6px; right: -6px; }

.results-primary {
  display: flex;
  gap: clamp(24px, 8vw, 56px);
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.big-stat { display: flex; flex-direction: column; }
.big-value {
  font-family: var(--mono);
  font-size: 2.5rem;
  font-weight: 600;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.big-label {
  font-size: 0.7rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.results-message { margin: 16px 0; color: var(--text-dim); font-size: 0.9rem; }

.results-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px 20px;
  padding: 16px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.results-grid dt { font-size: 0.72rem; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-faint); }
.results-grid dd { margin: 3px 0 0; font-family: var(--mono); font-size: 0.86rem; font-variant-numeric: tabular-nums; }

.results-mistakes { margin-top: 14px; }
.results-mistakes:not(:empty) { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.mistake-note { font-size: 0.76rem; color: var(--text-faint); margin-right: 4px; }
.mistake-chip {
  display: inline-flex;
  gap: 5px;
  padding: 3px 8px;
  border: 1px solid color-mix(in srgb, var(--wrong) 35%, transparent);
  border-radius: 6px;
  background: var(--wrong-soft);
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--wrong);
}

.results-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 22px; }

.review { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.review-title { font-size: 0.9rem; font-weight: 600; margin-bottom: 10px; }
.review-viewport { border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; background: var(--bg-raised); }
.review-list { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.review-list li {
  font-family: var(--mono);
  font-size: 0.76rem;
  color: var(--text-dim);
  overflow-wrap: anywhere;
}
.review-list mark { background: var(--wrong-soft); color: var(--wrong); border-radius: 3px; padding: 0 2px; }
.review #review-back { margin-top: 16px; }

/* ------------------------------------------------------------- footer */
.site-footer {
  flex-shrink: 0;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 0 clamp(16px, 5vw, 32px) clamp(24px, 5vh, 40px);
  text-align: center;
}

.footer-note {
  margin-bottom: 6px;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-faint);
}

.footer-link-row { font-size: 0.82rem; }

.footer-link {
  color: var(--text-dim);
  text-decoration: none;
  border-bottom: 1px solid var(--line-strong);
  padding-bottom: 1px;
  transition: color 120ms var(--ease), border-color 120ms var(--ease);
}
.footer-link:hover {
  color: var(--accent);
  border-bottom-color: color-mix(in srgb, var(--accent) 55%, transparent);
}

.footer-contact {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.footer-icon {
  flex-shrink: 0;
}

/* -------------------------------------------------------- 7. responsive */
@media (max-width: 780px) {
  :root { --passage-lines: 3; }
  .topbar { flex-wrap: wrap; gap: 10px; }
  .controls { width: 100%; justify-content: flex-start; }
  .statbar { gap: 20px; }
}

@media (max-width: 520px) {
  :root { --passage-size: 1.08rem; --passage-line: 1.95; }
  .brand-name { display: none; }
  .field-label { display: none; }
  .stage { padding-top: 20px; }
  .summary { grid-template-columns: 1fr 1fr; }
  .results-primary { gap: 22px; }
  .big-value { font-size: 2.1rem; }
  .results-grid { grid-template-columns: 1fr; gap: 10px; }
}

@media (pointer: coarse) {
  .text-hint::after {
    content: " A physical keyboard is recommended for touch typing.";
    color: var(--accent);
  }
}

/* ------------------------------------------------ 8. motion & printing */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

@media print {
  .topbar, .progress, .panel, .results, .site-footer { display: none !important; }
  body { background: #fff; color: #000; }
}
