/* ── Reset & tokens ─────────────────────────────────────────────
 * Canonical tokens (colours, font, type scale, spacing, radii, durations) come
 * from the CDN base.css linked in index.html. Only what base.css does not
 * define is declared here, plus the two per-site knobs. This file styles the
 * engine's own surfaces (library, round header, track, feedback panel,
 * results, errors, embed bar); each game module in js/games/ installs the
 * stylesheet for what it renders (qz- classes), so nothing here reaches into
 * an option, a tile or a chip.
 */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --accent: #fb923c;
  --accent-bright: #fdba74;
  --danger: #e11d48;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --control-height: 44px;
  --control-height-sm: 36px;
  --keycap: 28px;
}

[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

html { height: 100%; }
body {
  font-family: var(--font); background: var(--bg); color: var(--text-primary);
  min-height: 100%; display: flex; flex-direction: column;
  line-height: 1.55; letter-spacing: 0.01em; -webkit-font-smoothing: antialiased;
}
main#main { flex: 1; display: flex; flex-direction: column; width: 100%; min-height: 0; }

/* ── The surface: one column, no card ──────────────────────────── */
.q-surface {
  width: 100%; max-width: 640px; margin: 0 auto; flex: 1;
  padding: var(--space-6) var(--space-4) var(--space-8);
  display: flex; flex-direction: column; gap: var(--space-6);
}
#quiz-root { display: flex; flex-direction: column; gap: var(--space-4); min-width: 0; }

/* ── Buttons ───────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--control-height); padding: 0 var(--space-4); border-radius: var(--radius-sm);
  font: inherit; font-size: var(--text-sm); font-weight: 600; line-height: 1.2; text-decoration: none;
  cursor: pointer; border: 1px solid transparent; background: transparent; color: var(--text-primary);
  transition: background-color var(--dur), color var(--dur), border-color var(--dur), transform var(--dur-fast);
  -webkit-tap-highlight-color: transparent;
}
.btn:disabled, .btn[aria-disabled="true"] { opacity: .45; cursor: not-allowed; pointer-events: none; }
.btn:active { transform: translateY(1px); }
.btn--sm { min-height: var(--control-height-sm); padding: 0 var(--space-3); font-size: var(--text-xs); }
.btn--primary { background: var(--accent); color: var(--bg); }
.btn--primary:hover { background: var(--accent-bright); }
.btn--ghost { border-color: var(--border-strong); }
.btn--ghost:hover { background: var(--surface-2); }
.btn--ghost[aria-pressed="true"] { background: var(--surface-2); border-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent-bright); outline-offset: 2px; }

.skip-link {
  position: absolute; top: 0; left: var(--space-4); z-index: 1000; padding: var(--space-2) var(--space-4);
  background: var(--accent-bright); color: var(--bg); font-weight: 600; font-size: var(--text-sm);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm); text-decoration: none;
  transform: translateY(-100%); transition: transform var(--dur) var(--ease-out);
}
.skip-link:focus { transform: none; }

/* ── Embed mode ────────────────────────────────────────────────── */
.q-embed-bar {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  padding: 6px var(--space-4); border-bottom: 1px solid var(--border); background: var(--surface-1);
  font-size: var(--text-xs); color: var(--text-muted);
}
.q-embed-bar a { color: var(--accent-bright); text-decoration: none; white-space: nowrap; }
.q-embed-bar a:hover { text-decoration: underline; }
.q-embed-note { padding: var(--space-2) var(--space-4) 0; font-size: var(--text-xs); color: var(--text-muted); }
body.is-embed .header-bar, body.is-embed .neo-footer, body.is-embed .neo-beacon, body.is-embed .skip-link { display: none; }
body.is-embed .q-surface { padding: var(--space-4); }

/* ── Library ───────────────────────────────────────────────────── */
.q-library { display: flex; flex-direction: column; gap: var(--space-4); }
.q-library__head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3) var(--space-6); }
.q-library__title { font-size: var(--text-2xl); font-weight: 600; line-height: 1.2; }
.q-games { list-style: none; display: flex; flex-direction: column; }
.q-game { display: grid; gap: var(--space-4); padding: var(--space-6) 0; border-top: 1px solid var(--border-subtle); }
.q-game:last-child { border-bottom: 1px solid var(--border-subtle); }
.q-game__info { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.q-game__name { font-size: var(--text-xl); font-weight: 600; line-height: 1.2; }
.q-game__describe { color: var(--text-secondary); max-width: 44ch; }
.q-game__controls { display: grid; gap: var(--space-3); align-content: start; }
.q-field { display: grid; gap: var(--space-1); font-size: var(--text-sm); color: var(--text-secondary); }
.q-select {
  width: 100%; min-height: var(--control-height); padding: 0 var(--space-3);
  font: inherit; font-size: var(--text-sm); color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
}
.q-select:disabled { opacity: .5; }
.q-sizes { display: flex; align-items: center; gap: var(--space-2); }
.q-sizes__label { font-size: var(--text-sm); color: var(--text-secondary); margin-right: var(--space-1); }
.q-sizes .btn { min-width: var(--control-height-sm); padding-inline: var(--space-2); font-variant-numeric: tabular-nums; }
.q-game__meta { font-size: var(--text-sm); color: var(--text-muted); font-variant-numeric: tabular-nums; }
.q-game__play { justify-self: start; min-width: 128px; }
@media (min-width: 940px) {
  .q-game { grid-template-columns: minmax(0, 1fr) 300px; gap: var(--space-8); }
}

/* ── Skeleton, while a set fetches ─────────────────────────────── */
@keyframes shimmer { to { background-position: -200% 0; } }
.skeleton {
  background: linear-gradient(90deg, var(--surface-1) 25%, var(--surface-2) 50%, var(--surface-1) 75%);
  background-size: 200% 100%; animation: shimmer 1.5s ease-in-out infinite; border-radius: var(--radius);
}
.q-skeleton { display: flex; flex-direction: column; gap: var(--space-3); }
.q-skeleton__head { display: flex; justify-content: space-between; gap: var(--space-4); }
.q-skeleton__head .skeleton { height: 16px; width: 28%; border-radius: var(--radius-sm); }
.q-skeleton__prompt { height: 72px; width: 56%; margin: var(--space-4) auto var(--space-6); }
.q-skeleton__row { height: var(--control-height); }

/* ── Round header, track, streak, quit ─────────────────────────── */
.q-round-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: var(--space-3);
  font-size: var(--text-sm); font-weight: 500; color: var(--text-secondary);
}
.q-round-head__progress { font-variant-numeric: tabular-nums; }
.q-round-head__streak { display: flex; justify-content: flex-end; min-height: 20px; }
.q-streak { display: inline-flex; align-items: center; gap: var(--space-2); color: var(--accent-bright); font-variant-numeric: tabular-nums; }
.q-streak__ring { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.q-streak__ring.is-pulse { animation: q-pulse 200ms var(--ease-out); }
@keyframes q-pulse { 50% { transform: scale(1.15); } }
.q-track-slot { margin-top: calc(-1 * var(--space-2)); }
.q-track { display: flex; gap: 2px; height: 4px; }
.q-track__seg { flex: 1; background: var(--surface-2); border-radius: 2px; transition: background-color var(--dur); }
.q-track__seg.is-hit { background: var(--accent); }
.q-track__seg.is-miss { background: var(--danger); opacity: .6; }
.q-track--replay .q-track__seg.is-hit, .q-track--replay .q-track__seg.is-miss {
  animation: q-fade 200ms var(--ease-out) both; animation-delay: calc(var(--i, 0) * 40ms);
}
.q-quit {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3);
  padding: var(--space-3) var(--space-4); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-1);
}
.q-quit__title { font-weight: 500; }
.q-quit__actions { display: flex; gap: var(--space-2); }

/* ── The item host: the game renders into it ───────────────────── */
.q-host { display: flex; flex-direction: column; gap: var(--space-6); margin-top: var(--space-2); transition: opacity 150ms var(--ease-out); }
.q-host.is-leaving { opacity: 0; }
.q-host.is-entering { animation: q-rise 200ms var(--ease-out) both; }
@keyframes q-fade { from { opacity: 0; } }

/* ── Keycaps: a separate box beside the option, never text in it ── */
.q-keycap {
  display: inline-grid; place-items: center; width: var(--keycap); height: var(--keycap);
  font-size: var(--text-xs); font-weight: 600; font-variant-numeric: tabular-nums; color: var(--text-secondary);
  background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
}
.q-keycap--wide { width: auto; padding: 0 var(--space-2); }
.q-keycap-wrap { display: inline-flex; }

/* ── The feedback panel ────────────────────────────────────────── */
.q-feedback {
  display: flex; flex-direction: column; gap: var(--space-4); padding: var(--space-4) var(--space-6);
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius-lg);
  animation: q-rise 200ms var(--ease-out) both;
}
@keyframes q-rise { from { opacity: 0; transform: translateY(8px); } }
.q-feedback__title { font-size: var(--text-xl); font-weight: 600; }
/* Programmatic focus lands on these headings; the ring hugs the words, not the column. */
.q-feedback__title, .q-results__title, .q-error__title { width: fit-content; }
.q-miss { display: flex; flex-direction: column; gap: var(--space-3); }
.q-miss + .q-miss { padding-top: var(--space-4); border-top: 1px solid var(--border-subtle); }
.q-miss__what { color: var(--text-secondary); }
.q-miss__expected { font-size: var(--text-lg); font-weight: 600; color: var(--text-primary); }
/* Kana-only answers break only at the spaces between pieces, never inside one. */
.q-miss__expected, .q-miss-list__expected, .q-miss-list__prompt { word-break: keep-all; line-break: strict; overflow-wrap: anywhere; }
.q-miss__why { display: flex; flex-direction: column; gap: var(--space-3); max-width: 65ch; }
.q-feedback__continue { align-self: flex-start; }

/* ── Results ───────────────────────────────────────────────────── */
.q-results { display: flex; flex-direction: column; gap: var(--space-3); }
.q-results__title { font-size: var(--text-2xl); font-weight: 600; line-height: 1.2; }
.q-results__score { font-size: var(--text-xl); font-weight: 500; }
.q-results__meta { font-size: var(--text-sm); color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.q-results .q-track { margin: var(--space-2) 0 var(--space-4); }
.q-results__misses { margin-top: var(--space-2); font-size: var(--text-xs); font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); }
.q-miss-list { list-style: none; display: flex; flex-direction: column; }
.q-miss-list__row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: var(--space-1) var(--space-4); align-items: baseline;
  padding: var(--space-3) 0; border-top: 1px solid var(--border-subtle);
}
.q-miss-list__row:last-child { border-bottom: 1px solid var(--border-subtle); }
.q-miss-list__prompt { font-size: var(--text-lg); font-weight: 500; }
.q-miss-list__expected { font-weight: 600; color: var(--accent-bright); font-variant-numeric: tabular-nums; }
.q-miss-list__why { grid-column: 1 / -1; font-size: var(--text-sm); color: var(--text-secondary); max-width: 65ch; }
.q-results__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-top: var(--space-4); }

/* ── Named errors, never a blank ───────────────────────────────── */
.q-error { display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-3); padding: var(--space-6) 0; }
.q-error__title { font-size: var(--text-xl); font-weight: 600; }
.q-error__message { max-width: 65ch; }
.q-error__detail {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--text-sm); color: var(--text-muted);
  max-width: 65ch; overflow-wrap: anywhere;
}

/* ── The honesty line, once per screen ─────────────────────────── */
.q-lang-note { font-size: var(--text-sm); color: var(--text-secondary); }

/* ── Attribution, when the licence says screen: "required" ─────── */
.q-attrib { border-top: 1px solid var(--border-subtle); padding-top: var(--space-4); color: var(--text-muted); font-size: var(--text-xs); line-height: 1.6; max-width: 80ch; }
.q-attrib a { color: var(--text-secondary); }
.q-attrib__line { display: flex; align-items: center; gap: var(--space-3); min-height: var(--control-height-sm); cursor: pointer; list-style: none; }
.q-attrib__line::-webkit-details-marker { display: none; }
.q-attrib__first { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.q-attrib__more { flex: none; color: var(--text-secondary); text-decoration: underline; text-underline-offset: 2px; }
.q-attrib__details[open] .q-attrib__first { white-space: normal; }
.q-attrib__body { padding-top: var(--space-2); }

/* ── Reduced motion: every entry becomes a 100ms opacity change ── */
@media (prefers-reduced-motion: reduce) {
  .q-host { transition: opacity 100ms linear; }
  .q-host.is-entering, .q-feedback, .q-track--replay .q-track__seg.is-hit, .q-track--replay .q-track__seg.is-miss {
    animation: q-fade 100ms linear both; animation-delay: 0ms;
  }
  .q-streak__ring.is-pulse, .skeleton { animation: none; }
  .btn, .q-track__seg { transition-duration: 100ms; }
}

/* ── Responsive ────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .q-surface { padding: var(--space-4) var(--space-3); }
  :root { --keycap: 24px; --tile: 56px; }
  .q-feedback { padding: var(--space-4); }
  /* One thumb free: the panel's one action spans the column. */
  .q-feedback__continue { align-self: stretch; }
  .q-sizes { width: 100%; }
  /* A miss row stacks: the prompt line, then the expected under it, then the why. */
  .q-miss-list__row { grid-template-columns: minmax(0, 1fr); }
}
