@font-face {
  font-family: 'Space Grotesk';
  src: url('assets/fonts/space-grotesk.ttf') format('truetype');
  font-weight: 300 700;
  font-display: swap;
}

:root {
  color-scheme: dark;
  --background: hsl(0 0% 9%);
  --foreground: hsl(0 0% 95%);
  --muted: hsl(0 0% 13%);
  --muted-foreground: hsl(0 0% 63%);
  --popover: hsl(0 0% 7%);
  --popover-foreground: hsl(0 0% 95%);
  --card: hsl(0 0% 7%);
  --card-foreground: hsl(0 0% 95%);
  --border: hsl(0 0% 14.5%);
  --input: hsl(0 0% 14.5%);
  --primary: hsl(256 35% 45%);
  --primary-foreground: hsl(0 0% 100%);
  --secondary: hsl(0 0% 13%);
  --secondary-foreground: hsl(0 0% 80%);
  --accent: hsl(256 30% 20%);
  --accent-foreground: hsl(256 30% 80%);
  --destructive: hsl(0 70% 45%);
  --destructive-foreground: hsl(0 0% 100%);
  --ring: hsl(256 35% 45%);
  --radius: 0.5rem;
  --gray-800: oklch(27.8% 0.033 256.848);
  --green: oklch(72.3% 0.219 149.579);
  --red: oklch(63.7% 0.237 25.331);
  font-family: 'Space Grotesk', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-synthesis: none;
  line-height: 1.5;
  color: var(--foreground);
  background: var(--background);
}

* { box-sizing: border-box; border-color: var(--border); scrollbar-width: thin; }
body { margin: 0; }
::selection { background: var(--primary); color: var(--primary-foreground); }
::-webkit-scrollbar { width: 0.5rem; }
::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 9999px; }
::-webkit-scrollbar-track { background: transparent; }
[hidden] { display: none !important; }
button, textarea { font: inherit; }
button { cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: not-allowed; }
main { max-width: 42rem; margin: 16px auto; padding: 16px; }
.intro, .result { text-align: center; max-width: 520px; margin: 72px auto 0; }
h1 { font-size: 30px; line-height: 36px; font-weight: 600; letter-spacing: -0.025em; margin: 0 0 16px; }
.small { font-size: 14px; line-height: 20px; color: var(--muted-foreground); }
.intro { max-width: 440px; }
.intro h1 { font-size: clamp(32px, 8vw, 40px); line-height: 1.15; margin-bottom: 28px; }
.intro-actions { margin-top: 36px; }
.intro #start { width: 100%; height: 64px; gap: 12px; border-radius: var(--radius); font-size: 20px; font-weight: 600; box-shadow: 0 4px 16px rgb(0 0 0 / 0.2); }
.intro #start svg { width: 24px; height: 24px; fill: currentColor; }
.intro #start:active { transform: scale(0.98); }
#load-status { margin: 12px 0 0; }
.intro-instruction { font-size: 20px; line-height: 28px; font-weight: 500; color: var(--foreground); margin: 20px 0; }
.primary, .secondary {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 8px 16px;
  border: 0;
  border-radius: calc(var(--radius) - 4px);
  font-size: 14px;
  line-height: 20px;
  font-weight: 500;
  white-space: nowrap;
  outline: none;
  transition: all 150ms;
  box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
}
.primary { background: var(--primary); color: var(--primary-foreground); }
.primary:hover { background: color-mix(in srgb, var(--primary) 90%, transparent); }
.secondary { background: var(--secondary); color: var(--secondary-foreground); }
.secondary:hover { background: color-mix(in srgb, var(--secondary) 80%, transparent); }
.primary:disabled, .secondary:disabled { opacity: 0.5; pointer-events: none; }
button:focus-visible, textarea:focus-visible {
  outline: none;
  border-color: var(--ring);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 50%, transparent);
}
.icon { width: 40px; padding: 0; }
.icon svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.stats-card, .timer-card, .options {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  background: var(--card);
  color: var(--card-foreground);
  box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
}
.stats-card, .timer-card { margin-bottom: 16px; }
.game-top { display: flex; justify-content: space-between; align-items: center; font-size: 14px; line-height: 20px; }
.eyebrow { color: var(--muted-foreground); }
.total { color: var(--foreground); }
.total b { font-weight: 500; }
.progress { display: flex; gap: 6px; margin-top: 8px; }
.progress span { height: 8px; flex: 1; border-radius: 9999px; background: var(--muted); }
.progress .current { background: var(--primary); }
.progress .right { background: var(--green); }
.progress .wrong { background: var(--red); }
.timer-label { display: flex; justify-content: space-between; font-size: 14px; line-height: 20px; color: var(--muted-foreground); margin-bottom: 8px; }
.timer-label b { width: 96px; text-align: right; font-weight: 500; color: var(--foreground); font-variant-numeric: tabular-nums; }
.timer-row { display: flex; align-items: center; gap: 16px; }
.timer-track { background: var(--secondary); height: 8px; flex: 1; border-radius: 9999px; overflow: hidden; }
#timer-bar { background: var(--primary); width: 100%; height: 100%; transform-origin: left; }
#time { font-size: 14px; line-height: 20px; font-weight: 500; color: var(--foreground); font-variant-numeric: tabular-nums; width: 48px; text-align: right; white-space: nowrap; }
.options { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.option {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  background: var(--gray-800);
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  overflow: hidden;
  transition: all 150ms;
}
.option img { width: 100%; height: 100%; object-fit: contain; display: block; }
.option:not(:disabled):hover { box-shadow: 0 0 0 2px var(--muted-foreground); transform: scale(1.02); }
.option.correct { box-shadow: 0 0 0 4px var(--green); background: color-mix(in oklab, var(--green) 50%, transparent); z-index: 10; }
.option.incorrect { box-shadow: 0 0 0 4px var(--red); background: color-mix(in oklab, var(--red) 50%, transparent); z-index: 10; }
.option.dimmed { opacity: 0.4; }
.badge { position: absolute; right: 6px; bottom: 6px; background: var(--green); color: var(--foreground); border-radius: 9999px; width: 24px; height: 24px; display: grid; place-items: center; }
.incorrect .badge { background: var(--red); }
.feedback { min-height: 56px; padding: 16px 0 8px; font-size: 14px; line-height: 20px; }
.feedback b { font-weight: 500; }
.game-controls { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.advance-track { height: 2px; background: var(--secondary); margin-top: 16px; overflow: hidden; border-radius: 9999px; }
#advance-bar { height: 100%; background: var(--primary); transform-origin: left; transform: scaleX(0); }
.final-score { font-size: 72px; font-weight: 500; letter-spacing: -0.025em; margin: 20px 0 8px; }
.squares { display: flex; justify-content: center; gap: 6px; margin: 24px 0; }
.squares span { width: 12px; height: 12px; border-radius: 4px; background: var(--red); }
.squares .right { background: var(--green); }
textarea { font-size: 14px; line-height: 20px; background: color-mix(in srgb, var(--input) 30%, transparent); color: var(--foreground); width: 100%; resize: vertical; border: 1px solid var(--input); border-radius: calc(var(--radius) - 4px); min-height: 120px; padding: 12px; display: block; box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05); }
.result-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 12px; margin-top: 24px; }
#listening:empty, #load-status:empty, #copy-status:empty { display: none; }
textarea.copy-flash { border-color: var(--ring); animation: copy-flash .8s ease-out; }
@keyframes copy-flash { from { box-shadow: 0 0 0 5px color-mix(in srgb, var(--ring) 70%, transparent); } to { box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 50%, transparent); } }
@media (max-width: 480px) { main { margin: 0 auto; } .final-score { font-size: 60px; } }
@media (max-width: 480px) {
  .options { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .intro { margin: 0 auto; min-height: calc(100svh - 32px); display: flex; flex-direction: column; padding: 0 8px max(16px, env(safe-area-inset-bottom)); }
  .intro-copy { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 32px 0; }
  .intro-instruction { margin: 10px 0; }
  .intro-actions { margin-top: 16px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

#game:focus { outline: none; }

@media (max-width: 600px), (max-width: 1000px) and (max-height: 500px) {
  main {
    --screen-top: max(12px, env(safe-area-inset-top));
    --screen-bottom: max(12px, env(safe-area-inset-bottom));
    margin: 0 auto;
    padding: var(--screen-top) max(12px, env(safe-area-inset-right)) var(--screen-bottom) max(12px, env(safe-area-inset-left));
  }
  #game {
    height: calc(100dvh - var(--screen-top) - var(--screen-bottom));
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  #game > :not(.options-space) { flex-shrink: 0; }
  .stats-card, .timer-card { margin: 0; padding: 10px 12px; }
  #listening { margin: 0; }
  .options-space {
    flex: 1;
    min-height: 0;
    container-type: size;
    display: grid;
    place-items: center;
  }
  .options {
    --tile-size: min(calc((100cqw - 26px) / 2), calc((100cqh - 34px) / 3));
    width: calc(var(--tile-size) * 2 + 26px);
    height: calc(var(--tile-size) * 3 + 34px);
    padding: 8px;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(3, minmax(0, 1fr));
  }
  .option { min-width: 0; min-height: 0; aspect-ratio: 1; }
  .option img { min-width: 0; min-height: 0; }
  .feedback { min-height: 40px; padding: 0; overflow-wrap: anywhere; }
  .game-controls { flex-wrap: nowrap; }
  .advance-track { margin: 0; height: 2px; }
  #advance-track[hidden] { display: block !important; visibility: hidden; }
  .intro {
    margin: 0 auto;
    min-height: 0;
    height: calc(100dvh - var(--screen-top) - var(--screen-bottom));
    display: flex;
    flex-direction: column;
    padding: 0 8px;
  }
  .intro-copy { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 12px 0; }
  .intro-actions { margin-top: 12px; }
  .result {
    height: calc(100dvh - var(--screen-top) - var(--screen-bottom));
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 12px;
  }
  .result h1, .result p, .result .squares, .result-actions, .final-score { margin: 0; }
  .final-score { font-size: clamp(36px, 8dvh, 60px); line-height: 1.2; }
  .result textarea { resize: none; min-height: 90px; height: 108px; }
}
@media (max-width: 1000px) and (max-height: 500px) {
  #game { gap: 6px; }
  .stats-card, .timer-card { padding: 6px 10px; }
  .progress, .timer-label { margin-top: 0; margin-bottom: 4px; }
  .feedback { min-height: 20px; }
  .intro h1 { margin-bottom: 12px; }
  .intro-instruction { margin: 6px 0; font-size: 18px; line-height: 24px; }
  .intro #start { height: 52px; }
  .result { gap: 8px; }
  .result h1 { font-size: 24px; line-height: 28px; }
}

.stats-card .timer-card { border: 0; border-radius: 0; box-shadow: none; background: transparent; padding: 12px 0 0; margin: 0; }
@media (max-width: 1000px) and (max-height: 500px) {
  .stats-card .timer-card { padding-top: 6px; }
}

@media (max-width: 1000px) and (max-height: 500px) and (orientation: landscape) {
  .options {
    --tile-size: min(calc((100cqw - 34px) / 3), calc((100cqh - 26px) / 2));
    width: calc(var(--tile-size) * 3 + 34px);
    height: calc(var(--tile-size) * 2 + 26px);
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
}

.stats-card { display: grid; }
.round-info, .answer-feedback { grid-area: 1 / 1; }
.stats-card:has(#feedback:not(:empty)) .round-info { visibility: hidden; }
.answer-feedback { display: flex; flex-direction: column; justify-content: center; min-width: 0; }
.answer-feedback:has(#feedback:empty) { display: none; }
.answer-feedback .feedback { min-height: 0; padding: 0; text-align: center; color: var(--muted-foreground); overflow-wrap: anywhere; }
.answer-feedback .feedback b { display: block; font-size: 22px; line-height: 28px; font-weight: 500; color: var(--foreground); }
.answer-feedback .feedback[data-correct=true] b { color: var(--green); }
.answer-feedback #advance-track { margin-top: 6px; }
.answer-feedback #advance-track[hidden] { display: none !important; }
@media (max-width: 600px), (max-width: 1000px) and (max-height: 500px) {
  .game-controls { display: none; }
  .game-controls:has(#retry-audio:not([hidden])) { display: flex; justify-content: center; }
  #automatic, #next { display: none; }
}
@media (max-width: 1000px) and (max-height: 500px) {
  .answer-feedback .feedback { font-size: 13px; line-height: 18px; }
  .answer-feedback .feedback b { font-size: 20px; line-height: 24px; }
}
