/* html-games shared design system.
   Design tokens, base layout, overlays, buttons, panels.
   Game-specific styles (canvas wrapper, touch buttons) live in each game's index.html.
*/

:root {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1c2030;
  --line: #232838;
  --ink: #e7eaf3;
  --ink-2: #a8aebd;
  /* 5.12:1 on --bg, 4.82:1 on --panel — passes WCAG 2.2 AA 1.4.3 for body text */
  --ink-3: #7d859a;
  --accent: #7cf2c4;
  --accent-2: #8ab4ff;
  --danger: #ff6b8a;
  --shadow: 0 1px 0 rgba(255,255,255,0.04), 0 8px 30px rgba(0,0,0,0.35);
  --r-lg: 16px;
  --r-md: 10px;
  --r-sm: 6px;
  --t-fast: 120ms cubic-bezier(.2,.7,.2,1);
  --t-med: 220ms cubic-bezier(.2,.7,.2,1);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(1100px 600px at 80% -10%, rgba(122,180,255,0.10), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(124,242,196,0.08), transparent 60%),
    var(--bg);
  color: var(--ink);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
/* Gameplay surfaces swallow drags so canvas gestures don't select text.
   The catalog is plain content — leave its text selectable. */
.app, .overlay, .touch, .feel-toggle {
  user-select: none;
  -webkit-user-select: none;
}
/* Screen-reader-only text must stay selectable (WCAG 2.1.1). */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
  user-select: text;
  -webkit-user-select: text;
}

/* Visible focus for every interactive element (WCAG 2.4.7).
   The UA outline is suppressed by the dark theme, so we own it here. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}
.feel-toggle button:focus-visible,
.menu-action:focus-visible {
  outline-offset: 3px;
}

.app {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.stage {
  display: grid;
  gap: 14px;
  align-items: start;
}

.board-wrap {
  background: linear-gradient(180deg, var(--panel) 0%, var(--panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 14px;
  box-shadow: var(--shadow);
}

.board-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px 10px 4px;
}
.wordmark {
  font-weight: 800;
  letter-spacing: -0.01em;
  font-size: 18px;
  color: var(--ink);
}
.wordmark .dot { color: var(--accent); }

.side {
  display: grid;
  gap: 14px;
}

.panel {
  background: linear-gradient(180deg, var(--panel) 0%, var(--panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 14px;
  box-shadow: var(--shadow);
}

.panel h3 {
  margin: 0 0 10px 0;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-3);
  font-weight: 600;
}

.stat {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 6px 0;
  border-bottom: 1px dashed var(--line);
}
.stat:last-child { border-bottom: none; }
.stat .k { color: var(--ink-2); font-size: 13px; }
.stat .v {
  font-family: 'JetBrains Mono', ui-monospace, Menlo, monospace;
  font-weight: 700;
  font-size: 16px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.stat .v.accent { color: var(--accent); }

.preview {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: var(--r-md);
  background: #0a0c12;
  border: 1px solid var(--line);
  padding: 6px;
  display: grid;
  place-items: center;
}

.btn-row {
  margin-top: 14px;
  display: flex;
  gap: 10px;
  justify-content: center;
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #11141d;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  padding: 10px 16px;
  border-radius: var(--r-md);
  cursor: pointer;
  transition: transform var(--t-fast), background var(--t-fast), border-color var(--t-fast), color var(--t-fast);
  letter-spacing: 0.02em;
}
.btn:hover { background: #161a25; border-color: #2c3247; }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(180deg, #1f2638, #161c2a);
  border-color: #2c3247;
}
.btn.primary:hover {
  background: linear-gradient(180deg, #243049, #1a2133);
  border-color: #3b4567;
}
.btn.accent {
  background: linear-gradient(180deg, rgba(124,242,196,0.18), rgba(124,242,196,0.08));
  border-color: rgba(124,242,196,0.35);
  color: var(--accent);
}
.btn.accent:hover {
  background: linear-gradient(180deg, rgba(124,242,196,0.25), rgba(124,242,196,0.12));
  border-color: rgba(124,242,196,0.55);
}
.btn .ico { margin-right: 6px; }

/* ===== Overlay menu actions =====
   Standardized button row used in every game's start / pause / game-over
   overlay. Same shape and size — only the icon and color differ.
   Variants: .resume (mint accent), .restart (danger pink), .catalog (blue accent-2). */
.menu-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}
.menu-action {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: rgba(20,24,36,0.6);
  color: var(--ink);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition:
    background var(--t-fast),
    border-color var(--t-fast),
    transform var(--t-fast),
    color var(--t-fast);
}
.menu-action:hover { transform: translateY(-1px); }
.menu-action:active { transform: translateY(0); }
.menu-action .ico {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  line-height: 1;
  letter-spacing: 0;
}
.menu-action.resume {
  border-color: rgba(124,242,196,0.45);
  color: var(--accent);
  background: rgba(124,242,196,0.06);
}
.menu-action.resume:hover {
  border-color: rgba(124,242,196,0.7);
  background: rgba(124,242,196,0.12);
}
.menu-action.restart {
  border-color: rgba(255,107,138,0.45);
  color: var(--danger);
  background: rgba(255,107,138,0.06);
}
.menu-action.restart:hover {
  border-color: rgba(255,107,138,0.7);
  background: rgba(255,107,138,0.12);
}
.menu-action.catalog {
  border-color: rgba(255,184,107,0.45);
  color: #ffb86b;
  background: rgba(255,184,107,0.06);
}
.menu-action.catalog:visited {
  color: #ffb86b;
  border-color: rgba(255,184,107,0.45);
  background: rgba(255,184,107,0.06);
}
.menu-action.catalog:hover {
  border-color: rgba(255,184,107,0.7);
  color: #ffc78a;
  background: rgba(255,184,107,0.12);
}
@media (prefers-reduced-motion: reduce) {
  .menu-action { transition: none; }
  .menu-action:hover { transform: none; }
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10,12,18,0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  opacity: 0;
  pointer-events: none;
  /* `visibility` (not just opacity) keeps hidden overlay buttons out of the
     tab order while the fade still plays. WCAG 2.4.3. */
  visibility: hidden;
  transition: opacity var(--t-med), visibility var(--t-med);
  z-index: 10;
}
.overlay.show { opacity: 1; pointer-events: auto; visibility: visible; }
.card {
  background: linear-gradient(180deg, var(--panel) 0%, var(--panel-2) 100%);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 36px;
  min-width: min(320px, 92vw);
  max-width: 92vw;
  text-align: center;
  box-shadow: 0 24px 80px rgba(0,0,0,0.5);
  transform: translateY(8px) scale(0.98);
  transition: transform var(--t-med);
}
.overlay.show .card { transform: translateY(0) scale(1); }

.card .glyph {
  width: 56px; height: 56px;
  margin: 0 auto 14px auto;
  border-radius: 14px;
  display: grid; place-items: center;
  background: linear-gradient(180deg, rgba(124,242,196,0.16), rgba(124,242,196,0.06));
  border: 1px solid rgba(124,242,196,0.35);
  color: var(--accent);
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 22px;
}
.card .glyph.warn {
  color: var(--accent-2);
  border-color: rgba(138,180,255,0.35);
  background: linear-gradient(180deg, rgba(138,180,255,0.16), rgba(138,180,255,0.06));
}
.card .glyph.danger {
  color: var(--danger);
  border-color: rgba(255,107,138,0.35);
  background: linear-gradient(180deg, rgba(255,107,138,0.16), rgba(255,107,138,0.06));
}
.card h1 {
  margin: 0 0 6px 0;
  font-size: 28px;
  letter-spacing: -0.02em;
  font-weight: 800;
}
.card p {
  margin: 0 0 18px 0;
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.5;
}
.card .actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}
.card .meta {
  margin-top: 16px;
  display: flex;
  gap: 18px;
  justify-content: center;
  color: var(--ink-3);
  font-size: 12px;
}
.card .meta b {
  color: var(--ink);
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
}

.flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: rgba(124,242,196,0.0);
  transition: background 120ms ease-out;
  z-index: 5;
}
.flash.go { background: rgba(124,242,196,0.12); }
.flash.multi { background: rgba(124,242,196,0.20); }
.flash.over { background: rgba(255,107,138,0.18); }

.toast {
  position: absolute;
  top: 24px;
  left: 50%;
  transform: translate(-50%, -8px);
  background: rgba(20,24,36,0.85);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-med), transform var(--t-med);
  z-index: 9;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bonus { color: var(--accent); border-color: rgba(124,242,196,0.35); }

/* ===== Catalog back-link ===== */
/* Floating pill in the top-left of any game page. Lives above the .app
   container so it doesn't fight with the centered game stage. */
.back-link {
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 50;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 8px 12px;
  background: rgba(15, 17, 21, 0.78);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink-2);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: -0.005em;
  transition:
    color var(--t-fast),
    border-color var(--t-fast),
    transform var(--t-fast),
    background var(--t-fast);
}
.back-link:hover {
  color: var(--ink);
  border-color: var(--accent);
  background: rgba(15, 17, 21, 0.92);
  transform: translateX(-2px);
}
.back-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.back-link .arrow {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  color: var(--accent);
  transition: transform var(--t-fast);
}
.back-link:hover .arrow { transform: translateX(-2px); }

@media (prefers-reduced-motion: reduce) {
  .back-link, .back-link .arrow { transition: none; }
}

.touch {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 12px;
  padding: 0 12px;
  z-index: 8;
}
.touch .row { display: flex; justify-content: space-between; gap: 10px; margin-top: 8px; }
.touch button {
  flex: 1;
  appearance: none;
  background: rgba(20,24,36,0.7);
  border: 1px solid var(--line);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  padding: 14px;
  border-radius: 12px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.touch button:active { background: rgba(40,48,70,0.9); }

@media (hover: none) and (pointer: coarse) {
  .touch { display: block; }
  /* Mobile: hide floating back-link — nav lives in pause/game-over menu. */
  .back-link { display: none; }
}

/* ===== Responsive game canvas =====
   Mobile / narrow viewports: scale canvas to fit container width, keeping
   aspect ratio. Combined with `image-rendering: pixelated` (set per-game),
   pixel art scales without subpixel blur. */
@media (max-width: 900px) {
  .stage canvas,
  .board-host canvas,
  .board-wrap canvas {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 0 auto;
  }
  .board-host,
  .board-wrap {
    max-width: 100%;
    overflow: hidden;
  }
}

/* ===== Mobile: maximize play area =====
   Strip padding, borders and head chrome that would steal pixels from the
   game canvas on narrow viewports. Applies to every game. */
@media (max-width: 900px) {
  .app {
    padding: 8px 8px 0 8px;
  }
  .stage {
    gap: 8px;
  }
  .board-head {
    display: none;  /* hide "<game>." wordmark + v1.0 — redundant on mobile */
  }
  .board-host {
    padding: 0;
    border: 0;
  }
  .btn-row {
    display: none;  /* desktop Pause/Restart replaced by .touch UI */
  }
  /* Compact side panels on mobile (Stats + Controls). */
  .panel {
    padding: 10px;
  }
  .panel h3 {
    margin: 0 0 6px 0;
    font-size: 10px;
  }
  .stat {
    padding: 4px 0;
  }
  .stat .k { font-size: 12px; }
  .stat .v { font-size: 14px; }
  .controls { gap: 6px; margin-top: 4px; }
  .key { padding: 6px 8px; font-size: 11px; }
  .key kbd { font-size: 10px; padding: 1px 5px; }
}

/* ===== Universal mobile touch UI =====
   Standard layout: pause (far left) · secondary action column · main 2×2 grid.
   Same shape / colors / sizing as Tetris touch UI (see tetris/index.html).
   Visibility controlled by the coarse-pointer media query below. */
.touch {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 8px;
  padding: 0 12px;
  z-index: 8;
  gap: 8px;
  align-items: stretch;
}
.touch button {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(20,24,36,0.7);
  color: var(--ink);
  font-family: 'Inter', system-ui, sans-serif;
  font-weight: 600;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  min-height: 48px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.touch button:active { transform: translateY(1px); }
.touch .pause-btn {
  flex: 0 0 56px;
  width: 56px;
  font-size: 22px;
  color: var(--ink-2);
}
.touch .pause-btn:active { background: rgba(40,48,70,0.9); }
.touch .secondary,
.touch .main {
  display: grid;
  gap: 6px;
}
.touch .secondary {
  flex: 1;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}
.touch .secondary button {
  font-size: 11px;
  padding: 4px;
  letter-spacing: 0.04em;
}
.touch .main {
  flex: 2;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}
.touch .main button {
  font-size: 24px;
  color: var(--accent);
  border-color: rgba(124,242,196,0.55);
  background: rgba(124,242,196,0.18);
}
.touch .main button:active {
  background: rgba(124,242,196,0.32);
  border-color: var(--accent);
}
/* Variants: row (1 row × 2 cols, for games with 2 actions), single (1 cell). */
.touch .main.row {
  grid-template-rows: 1fr;
}
.touch .main.single {
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
}
.touch .main.single button {
  font-size: 20px;
  letter-spacing: 0.06em;
}
/* Sudoku-style numeric pad: 3×3 number grid (override main 2×2 layout). */
.touch .main.numpad {
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
}
.touch .main.numpad button {
  font-size: 20px;
  font-family: 'JetBrains Mono', monospace;
}
@media (hover: none) and (pointer: coarse) {
  .touch { display: flex; }
  .back-link { display: none; }  /* navigation lives in pause overlay */
}

@media (max-width: 900px) {
  .app { padding: 12px; padding-bottom: 180px; }
}

/* ===== Particle layer =====
   A single absolute-positioned layer over the .app where particle bursts
   are emitted. Particles are short-lived (≤700ms) and self-cleaning. */
.particle-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 6;
}
.particle {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  pointer-events: none;
  transform: translate(0, 0) scale(1);
  opacity: 1;
  animation: particle-burst 600ms cubic-bezier(.2,.7,.2,1) forwards;
  will-change: transform, opacity;
}
@keyframes particle-burst {
  0%   { transform: translate(0, 0) scale(1); opacity: 1; }
  60%  { opacity: 1; }
  100% { transform: translate(var(--dx, 0), var(--dy, 0)) scale(0); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .particle-layer { display: none; }
}

/* ===== Settings toolbar (mute / haptics toggle) =====
   Small icon buttons in the top-right of any overlay card. */
.feel-toggle {
  position: absolute;
  top: 14px;
  right: 14px;
  display: inline-flex;
  gap: 6px;
  z-index: 1;
}
.feel-toggle button {
  appearance: none;
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(20,24,36,0.6);
  color: var(--ink-2);
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background var(--t-fast), border-color var(--t-fast), color var(--t-fast);
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}
.feel-toggle button:hover {
  background: rgba(40,48,70,0.9);
  border-color: #2c3247;
}
.feel-toggle button[aria-pressed="false"] {
  /* The slash is the only non-text cue for "off" — it has to clear 3:1 (1.4.11). */
  color: var(--ink-2);
  opacity: 0.9;
}
.feel-toggle button[aria-pressed="false"]::after {
  content: '';
  position: absolute;
  width: 22px;
  height: 1px;
  background: currentColor;
  transform: rotate(-45deg);
}
/* ===== Global reduced-motion fallback (WCAG 2.3.3) =====
   Per-game canvas animations gate themselves through
   Feel.prefersReducedMotion(); this neutralises every CSS-driven
   animation and transition on the page. */
@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;
  }
}
