/* ============================================================================
   Memory Quiz — style.css (Game 08, plan/games/08-memory-quiz.md §4)
   Mobile-first, one-thumb layout; the play screen fits 360 px with the banner
   + grid + HUD visible and no scroll. Grid cells are buttons sized
   min(92vw / cols, 88px); the emoji glyph fills the cell via clamp().
   `touch-action: manipulation` on the grid only. Theme follows the site's
   `ai-quiz-theme` setting (resolved to [data-theme="dark"] / "light" by an
   inline script in index.html). Modern CSS (dvh, backdrop-filter) always has
   a plain fallback declared first (browser floor).
   ========================================================================== */

:root,
[data-theme='light'] {
  --bg-a: #e8e4f3;
  --bg-b: #d4c5e8;
  --card: #ffffff;
  --card-border: rgba(148, 163, 184, 0.35);
  --text: #1e293b;
  --muted: #64748b;
  --socket: #cbd5e1;
  --tile: #f8fafc;
  --tile-hover: #eef2ff;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --primary-a: #2563eb;
  --primary-b: #4f46e5;
  --gold: #b45309;
  --gold-bg: #fef3c7;
  --shadow: 0 10px 30px rgba(30, 27, 75, 0.12);
}

[data-theme='dark'] {
  --bg-a: #1e1b4a;
  --bg-b: #3f1229;
  --card: #1e293b;
  --card-border: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --socket: #475569;
  --tile: #16202f;
  --tile-hover: #1c2a3f;
  --good: #4ade80;
  --good-bg: rgba(74, 222, 128, 0.16);
  --bad: #f87171;
  --bad-bg: rgba(248, 113, 113, 0.16);
  --primary-a: #3b82f6;
  --primary-b: #6366f1;
  --gold: #fbbf24;
  --gold-bg: rgba(251, 191, 36, 0.14);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    'Helvetica Neue',
    Arial,
    sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, var(--bg-a), var(--bg-b)) fixed;
  -webkit-tap-highlight-color: transparent;
}

.app {
  min-height: 100vh;
  min-height: 100dvh; /* fallback declared first above — browser floor */
  max-width: none; /* BUG-016 sweep: full-screen per owner request */
  margin: 0 auto;
  padding: 0.75rem 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.back-link {
  align-self: flex-start;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  padding: 0.25rem 0.5rem;
  margin-left: -0.5rem;
  border-radius: 8px;
}

.back-link:hover,
.back-link:focus-visible {
  color: var(--text);
  background: var(--tile-hover);
}

.hidden {
  display: none !important;
}

/* Visually hidden but exposed to assistive tech (live region). */
.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;
}

/* ---- screens -------------------------------------------------------------- */

.screen {
  display: none;
  flex-direction: column;
  gap: 0.75rem;
}

.screen--active {
  display: flex;
}

/* ---- menu ----------------------------------------------------------------- */

.menu-title {
  margin: 0.5rem 0 0;
  text-align: center;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.9375rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.card h2 {
  margin: 0;
  font-size: 0.8125rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.chips {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.chips button {
  border: 1.5px solid var(--card-border);
  background: var(--tile);
  color: var(--text);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
}

.chips button[aria-checked='true'] {
  border-color: var(--primary-a);
  background: var(--tile-hover);
  box-shadow: inset 0 0 0 1px var(--primary-a);
}

.best-line {
  margin: 0;
  font-size: 0.9375rem;
  color: var(--muted);
}

/* ---- mode chips reuse .chips; the campaign level grid ------------------------- */

.level-picker-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.levels {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(3.6rem, 1fr));
  gap: 0.45rem;
}

.levels .level-tile {
  border: 1.5px solid var(--card-border);
  background: var(--tile);
  color: var(--text);
  border-radius: 12px;
  padding: 0.4rem 0.2rem 0.3rem;
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  line-height: 1.2;
}

.levels .level-tile .lv-stars {
  font-size: 0.625rem;
  letter-spacing: 0.05em;
  color: var(--gold);
}

.levels .level-tile .lv-lock {
  font-size: 0.75rem;
  color: var(--muted);
}

.levels .level-tile[aria-checked='true'] {
  background: linear-gradient(135deg, var(--primary-a), var(--primary-b));
  border-color: transparent;
  color: #fff;
  box-shadow: var(--shadow);
}

.levels .level-tile[aria-checked='true'] .lv-stars {
  color: #fde68a;
}

.levels .level-tile:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.9375rem;
  cursor: pointer;
}

.toggle-row input {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--primary-a);
}

.primary {
  border: 0;
  border-radius: 14px;
  padding: 0.9rem 1rem;
  font-size: 1.0625rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--primary-a), var(--primary-b));
  cursor: pointer;
  box-shadow: var(--shadow);
}

.daily {
  border: 1.5px solid var(--card-border);
  border-radius: 14px;
  padding: 0.7rem 1rem;
  font: inherit;
  color: var(--text);
  background: var(--card);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.daily-title {
  font-weight: 700;
}

.daily-sub {
  font-size: 0.8125rem;
  color: var(--muted);
}

button:focus-visible {
  outline: 2px solid var(--primary-a);
  outline-offset: 2px;
}

/* ---- play screen ----------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.hud {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  justify-content: center;
  flex: 1;
}

.chip {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 0.3rem 0.65rem;
  font-size: 0.8125rem;
  font-weight: 700;
  white-space: nowrap;
}

.chip-icon {
  margin-right: 0.15rem;
}

.ghost {
  border: 1.5px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 0.4rem 0.7rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* ---- banner + countdown ring ------------------------------------------------ */

.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  min-height: 2.4rem;
}

.ring {
  width: 1.7rem;
  height: 1.7rem;
  transform: rotate(-90deg);
  flex: none;
}

.ring circle {
  fill: none;
  stroke-width: 3;
  stroke-linecap: round;
}

.ring-track {
  stroke: var(--socket);
}

.ring-fill {
  stroke: var(--primary-a);
  stroke-dasharray: 62.83;
  stroke-dashoffset: 0;
}

.banner-text {
  font-size: 1.0625rem;
  font-weight: 700;
  text-align: center;
}

/* ---- grid (blocks are big: cap 112px; grids are fully filled) ---------------- */

.board-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), 1fr);
  gap: 8px;
  width: min(
    92vw,
    calc(var(--cols, 2) * 160px + (var(--cols, 2) - 1) * 8px)
  ); /* BUG-016 sweep: bigger cells on large screens */
  margin: 0 auto;
  touch-action: manipulation; /* plan §4: on the grid only */
}

.cell {
  aspect-ratio: 1;
  border: 1.5px solid var(--card-border);
  border-radius: 16px;
  background: var(--tile);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2rem, 11vw, 3.6rem);
  line-height: 1;
  color: var(--text);
  transition:
    background 120ms ease,
    transform 120ms ease;
}

.cell:hover {
  background: var(--tile-hover);
}

.cell:active {
  transform: scale(0.94);
}

/* The removed item's socket during a missing question (plan §7.3). */
.cell--removed {
  background: var(--socket);
  border-style: dashed;
}

/* Wrong answer: the truth pulses once (plan §4/§10). */
.cell--pulse {
  animation: cell-pulse 900ms ease-in-out;
}

@keyframes cell-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(37, 99, 235, 0);
  }
  30% {
    box-shadow: 0 0 0 4px var(--primary-a);
    background: var(--tile-hover);
  }
}

/* Blank beat between boards (plan §2: 250 ms blank). */
.grid--blank .cell span {
  visibility: hidden;
}

/* Question phase: the grid structure stays visible and tappable, but the
   items hide — answers come from memory. Reveals punch through per cell. */
.grid--hidden .cell .glyph {
  visibility: hidden;
}

.grid--hidden .cell--reveal .glyph {
  visibility: visible;
}

/* Ancient-device glyph fallback: first letter, styled plain (plan §7.7). */
.cell .glyph-fallback {
  font-size: clamp(1.1rem, 5vw, 2rem);
  font-weight: 800;
}

/* Kids mode: the item's name rides under the glyph and hides with it. */
.cell .glyph small {
  display: block;
  font-size: 0.55rem;
  font-weight: 600;
  line-height: 1.1;
}

/* Stars line on the level-clear card. */
.stars {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.15em;
}

/* ---- candidate row (missing questions) --------------------------------------- */

.candidates {
  display: flex;
  justify-content: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.candidates button {
  min-width: 4.2rem;
  min-height: 3.4rem;
  border: 1.5px solid var(--card-border);
  border-radius: 14px;
  background: var(--card);
  cursor: pointer;
  padding: 0.35rem 0.6rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.1rem;
  color: var(--text);
  transition: transform 120ms ease;
}

.candidates button:hover {
  background: var(--tile-hover);
}

.candidates button:active {
  transform: scale(0.94);
}

.candidates .cand-emoji {
  font-size: 1.6rem;
  line-height: 1;
}

.candidates .cand-name {
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--muted);
}

.candidates .glyph-fallback {
  font-size: 1.3rem;
  font-weight: 800;
}

/* ---- overlays ----------------------------------------------------------------- */

.overlay {
  /* fixed, not absolute: the pause/game-over cards are taller than the grid
     on the small tiers — they center in the viewport, not the board wrap */
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 18px;
  z-index: 5;
}

/* Plain translucent backdrop first; the blur is a progressive enhancement. */
.overlay--pause,
.overlay--gameover {
  background: var(--bg-a);
  background: color-mix(in srgb, var(--bg-a) 82%, transparent);
  backdrop-filter: blur(6px);
}

.overlay--feedback {
  background: transparent;
  pointer-events: none; /* transient — never blocks the board */
}

.overlay-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 1.25rem 1.1rem;
  width: min(88%, 20rem);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  max-height: 100%;
  overflow: auto;
}

.overlay-emoji {
  font-size: 2rem;
}

.overlay-title {
  margin: 0;
  font-size: 1.3rem;
}

.overlay-sub {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
}

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.ghost-row {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

.win-stats {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.badges {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  flex-wrap: wrap;
  min-height: 0;
}

.badge-new,
.badge-top {
  border-radius: 999px;
  padding: 0.25rem 0.7rem;
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.06em;
}

.badge-new {
  color: var(--gold);
  background: var(--gold-bg);
}

.badge-top {
  color: var(--primary-a);
  background: var(--tile-hover);
}

/* feedback: a small floating chip, ✓ points or ✗ the truth (plan §4) */
.feedback-card {
  border-radius: 999px;
  padding: 0.55rem 1.1rem;
  font-size: 1.0625rem;
  font-weight: 800;
  box-shadow: var(--shadow);
  animation: feedback-pop 400ms ease-out;
  margin: 0 auto;
  max-width: 92%;
  text-align: center;
}

.feedback-card--hit {
  color: var(--good);
  background: var(--good-bg);
  border: 1.5px solid var(--good);
}

.feedback-card--miss {
  color: var(--bad);
  background: var(--bad-bg);
  border: 1.5px solid var(--bad);
}

@keyframes feedback-pop {
  from {
    transform: scale(0.7);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* ---- toast (clipboard fallback) ------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translate(-50%, 200%);
  /* parked fully out of sight — the % translate resolves against the content
     box, so the padded pill otherwise peeks above the bottom edge */
  opacity: 0;
  pointer-events: none;
  background: var(--text);
  color: var(--bg-a);
  border-radius: 999px;
  padding: 0.55rem 1rem;
  font-size: 0.875rem;
  font-weight: 600;
  transition:
    transform 220ms ease,
    opacity 220ms ease;
  z-index: 50;
}

.toast--in {
  transform: translate(-50%, 0);
  opacity: 1;
  pointer-events: auto;
}

@media (prefers-reduced-motion: reduce) {
  .cell,
  .candidates button,
  .feedback-card,
  .toast {
    transition: none;
    animation: none;
  }
}

/* ---------- Mystery Mix (suggestion 08 task 2) ---------- */

.shuffle-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.6rem;
  width: 100%;
  max-width: 420px;
  margin: 0 auto 1rem;
}

@media (max-width: 360px) {
  .shuffle-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.shuffle-card {
  aspect-ratio: 3 / 4;
  border: 1.5px dashed var(--card-border);
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-size: 2rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  padding: 0.5rem;
}

button.shuffle-card:focus-visible {
  outline: 2px solid var(--primary);
}

.shuffle-card--open {
  border-style: solid;
  cursor: default;
  gap: 0.3rem;
}

.shuffle-card--open .sc-level {
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1;
}

.shuffle-card--open .sc-mode {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
}

.shuffle-card--open .sc-stars {
  font-size: 0.8rem;
  letter-spacing: 0.08em;
}

.shuffle-card--open .sc-play {
  margin-top: 0.35rem;
  font-size: 0.85rem;
  padding: 0.4rem 0.9rem;
}

#screen-shuffle .ghost {
  margin-top: auto;
}

/* ---- BUG-021: level-clear gallery + unanswered gate ----------------------- */

.clear-items {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  margin: 0.75rem 0 0.25rem;
}

.clear-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.6rem;
  height: 2.6rem;
  font-size: 1.5rem;
  border-radius: 12px;
  background: rgba(148, 163, 184, 0.14);
}

.timeout-note {
  color: #fbbf24;
  font-weight: 600;
}
