:root {
  --bg: #07111c;
  --panel: #0b1724;
  --panel-2: #0a1420;
  --line: #1a2b3e;
  --line-2: #22374d;
  --text: #e8f1fa;
  --muted: #92a3b8;
  --accent: #22d3ee;
  --accent-2: #38bdf8;
  --accent-ink: #04202a;
  --radius: 12px;
  --wide: min(1200px, calc(100vw - 32px));
  --board-h: clamp(320px, calc(100dvh - 392px), 900px);
  font-family: "Segoe UI", "Segoe UI Variable", system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(1200px 700px at 50% 30%, #0c1d2e 0%, var(--bg) 65%) fixed, var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-user-select: none;
  user-select: none;
}

svg { width: 20px; height: 20px; flex: none; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

h1, h2, h3, p { margin: 0; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Header (matches Brick Breaker) ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  padding: 0 clamp(16px, 3vw, 48px);
  border-bottom: 1px solid #163049;
  background: rgba(6, 17, 29, .6);
  backdrop-filter: blur(8px);
}

.brand { display: flex; align-items: center; gap: 14px; text-decoration: none; color: var(--text); min-width: 0; }
.brand-home, .brand-logo-link { display: flex; border-radius: 6px; }
.brand-logo-link:hover .brand-logo { filter: brightness(1.15); }
.brand-home:focus-visible, .brand-logo-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.brand-icon { width: 40px; height: 40px; }
.brand-logo { width: 110px; height: auto; }
.brand-sep { width: 1px; height: 28px; background: var(--line-2); margin: 0 10px 0 6px; }
.brand-title {
  color: #0ee6f5;
  font-weight: 600;
  font-size: 14px;
  letter-spacing: .5em;
  text-transform: uppercase;
  white-space: nowrap;
}

.topbar-actions { display: flex; align-items: center; gap: 16px; }
.divider { width: 1px; height: 24px; background: var(--line-2); }

.link-btn, .icon-btn {
  display: inline-flex; align-items: center; gap: 10px;
  background: none; border: 0; padding: 6px; border-radius: 8px;
  color: #c6d6e4; font-size: 16px;
}
.link-btn svg, .icon-btn svg { width: 24px; height: 24px; color: var(--accent); }
.icon-btn svg { color: inherit; }
.link-btn:hover, .icon-btn:hover { color: var(--accent); }
.icon-btn[hidden] { display: none; }
.icon-btn[aria-pressed="false"] { color: var(--muted); }
.link-btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); }
.check input, .cb-ghost input { position: absolute; opacity: 0; pointer-events: none; }

.switch {
  position: relative;
  width: 44px; height: 24px;
  border-radius: 999px;
  background: var(--line-2);
  transition: background .2s;
  flex: none;
}
.switch::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff;
  transition: transform .2s;
}
.switch--lg { width: 56px; height: 30px; }
.switch--lg::after { width: 24px; height: 24px; }
input:checked + .switch { background: var(--accent); }
input:checked + .switch::after { transform: translateX(20px); }
input:checked + .switch--lg::after { transform: translateX(26px); }
input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 22px 16px 8px;
}

/* ---------- Stat bar ---------- */
.statbar {
  width: var(--wide);
  display: grid;
  grid-template-columns: repeat(4, 1fr) .85fr;
  align-items: center;
  padding: 8px 0;
  background: linear-gradient(180deg, rgba(13, 26, 41, .9), rgba(10, 21, 34, .9));
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
}
.statbar-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px 12px;
}
.statbar-item + .statbar-item { border-left: 1px solid var(--line-2); }
.statbar-item > span {
  color: var(--muted); font-size: 13px; font-weight: 600;
  letter-spacing: .1em; text-transform: uppercase;
}
.statbar-item strong { font-size: 26px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.statbar-level strong { line-height: 1.05; }
.statbar-level .bar { width: min(180px, 80%); margin-top: 6px; }

.bar { height: 6px; border-radius: 999px; background: var(--line-2); overflow: hidden; }
.bar div {
  height: 100%; width: 0; border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  box-shadow: 0 0 10px rgba(34, 211, 238, .5);
  transition: width .3s ease;
}

.status { display: inline-flex; align-items: center; gap: 12px; font-size: 16px; color: var(--muted); }
.status i { width: 14px; height: 14px; border-radius: 50%; background: #64748b; }
.status[data-state="playing"] { color: var(--text); }
.status[data-state="playing"] i { background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.status[data-state="paused"] i { background: #fbbf24; }
.status[data-state="over"] i { background: #f87171; }

/* ---------- Play row ---------- */
/* Side columns use the same widths as the Snake and Memory games */
.play-row { display: flex; align-items: flex-start; justify-content: center; gap: clamp(14px, 1.6vw, 36px); }

.side { margin-top: 20px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.side-hold, .side-next { width: clamp(190px, 16vw, 300px); }
.side .card { width: 100%; }
@media (min-width: 1600px) {
  .play-row { gap: 28px; }
  .side-hold, .side-next { width: 290px; }
}

.card {
  background: linear-gradient(180deg, #0d1a29 0%, #0a1522 100%);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  overflow: hidden;
}
.card h2 {
  font-size: 15px; font-weight: 600; letter-spacing: .02em;
  text-transform: uppercase;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}

.slot { position: relative; min-height: 0; }
.slot canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }
.hold-slot {
  height: 120px; margin: 12px 16px 16px;
  background: #08111b; border: 1px solid var(--line); border-radius: 8px;
}
.next-list { display: flex; flex-direction: column; padding: 8px 12px 14px; }
.next-list .slot { height: 82px; }

.side-hint { color: var(--muted); font-size: 12px; letter-spacing: .14em; }

.board-frame {
  position: relative;
  height: var(--board-h);
  aspect-ratio: 1 / 2;
  --glow: 34, 211, 238;
  border: 2px solid rgba(var(--glow), .7);
  border-radius: 8px;
  background: #081320;
  box-shadow: 0 0 26px rgba(var(--glow), .3), inset 0 0 18px rgba(var(--glow), .07);
  overflow: hidden;
  touch-action: none;
  transition: border-color .5s ease, box-shadow .5s ease;
}
.board-frame canvas { display: block; width: 100%; height: 100%; }

/* Stack-height warning (set from JS) */
.board-frame[data-danger="warn"] { --glow: 251, 191, 36; }
.board-frame[data-danger="danger"] {
  --glow: 239, 68, 68;
  border-color: rgba(var(--glow), .85);
  animation: danger-pulse 1.1s ease-in-out infinite;
}
@keyframes danger-pulse {
  0%, 100% { box-shadow: 0 0 18px rgba(239, 68, 68, .25), inset 0 0 14px rgba(239, 68, 68, .06); }
  50% { box-shadow: 0 0 34px rgba(239, 68, 68, .55), inset 0 0 26px rgba(239, 68, 68, .14); }
}
@media (prefers-reduced-motion: reduce) {
  .board-frame[data-danger="danger"] { animation: none; box-shadow: 0 0 28px rgba(239, 68, 68, .45); }
}

.board-overlay {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 20px; text-align: center;
  background: rgba(6, 14, 24, .78);
  backdrop-filter: blur(3px);
}
.board-overlay[hidden] { display: none; }
.board-overlay h3 { font-size: 30px; font-weight: 800; }
.board-overlay p { color: var(--muted); margin-bottom: 8px; line-height: 1.5; }
.board-overlay p b { color: var(--accent); font-size: 1.15em; }

/* ---------- Control bar ---------- */
.controlbar {
  width: var(--wide);
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  padding: 16px 22px;
  background: linear-gradient(180deg, rgba(13, 26, 41, .9), rgba(10, 21, 34, .9));
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
}
.cb-group { display: flex; align-items: center; gap: 12px; padding: 0 34px; }
.cb-group:first-child { padding-left: 0; }
.cb-group:last-child { padding-right: 0; }
.cb-group + .cb-group { border-left: 1px solid var(--line-2); }
.cb-label { font-size: 16px; white-space: nowrap; }
.cb-style { gap: 18px; }
.cb-ghost { gap: 40px; cursor: pointer; }

.primary-btn, .outline-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 12px;
  height: 44px; padding: 0 30px;
  border-radius: 6px;
  font-size: 16px; font-weight: 600;
}
.primary-btn {
  background: var(--accent);
  color: var(--accent-ink); border: 0;
  box-shadow: 0 4px 18px rgba(34, 211, 238, .25);
}
.primary-btn:hover { filter: brightness(1.07); }
.outline-btn { background: transparent; border: 1.5px solid #dbe6f2; color: var(--text); padding: 0 36px; }
.outline-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.outline-btn:disabled { opacity: .45; cursor: default; }
.primary-btn svg, .outline-btn svg { width: 18px; height: 18px; }
.cb-actions .primary-btn { min-width: 168px; }
.cb-actions .outline-btn { min-width: 150px; }

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 112px);
  border: 1px solid var(--line-2);
  border-radius: 6px;
  overflow: hidden;
  background: rgba(8, 17, 27, .6);
}
.segmented button {
  height: 42px;
  background: transparent;
  border: 0;
  font-size: 15px;
}
.segmented button + button { border-left: 1px solid var(--line-2); }
.segmented button:hover { background: rgba(34, 211, 238, .08); }
.segmented button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.segmented button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- Key hints ---------- */
.key-hints { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; margin-top: 6px; }
.hint { display: inline-flex; align-items: center; gap: 6px; padding: 0 28px; }
.hint + .hint { border-left: 1px solid var(--line-2); }
.hint em { font-style: normal; color: var(--muted); font-size: 14px; margin-left: 10px; }

kbd {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 40px; height: 38px; padding: 0 10px;
  background: #0d1a29;
  border: 1px solid #3a4f66;
  border-radius: 6px;
  font: 600 14px/1 inherit;
  font-family: inherit;
  color: var(--text);
}
kbd.wide { min-width: 112px; }
kbd svg { width: 16px; height: 16px; }

.credit { text-align: center; color: var(--muted); font-size: 12px; letter-spacing: .2em; padding: 12px 0 16px; }
.credit a { color: var(--accent); font-weight: 700; text-decoration: none; }
.credit a:hover { text-decoration: underline; }

/* ---------- Touch controls ---------- */
.touch-controls { display: none; }

/* ---------- Modals ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  background: rgba(3, 9, 16, .72);
  backdrop-filter: blur(6px);
  overflow-y: auto;
}
.modal[hidden] { display: none; }
.modal-box {
  position: relative;
  width: min(880px, 100%);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 30px 32px;
  background: linear-gradient(180deg, #0f1e2f, #0a1522);
  border: 1px solid var(--line-2);
  border-radius: 18px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .5);
}
.modal-box--narrow { width: min(640px, 100%); }
.modal-box h2 { font-size: 30px; font-weight: 800; margin: 6px 0 8px; }
.modal-box h3 { font-size: 15px; letter-spacing: .06em; text-transform: uppercase; margin: 20px 0 10px; color: var(--accent); }
.modal-sub { color: var(--muted); line-height: 1.55; }
.modal-close { position: absolute; top: 14px; right: 14px; }
.modal-close[hidden] { display: none; }
.eyebrow { color: var(--accent); font-size: 13px; font-weight: 700; letter-spacing: .22em; text-transform: uppercase; }

/* ---------- Start screen (inside the board) ---------- */
.start-screen {
  position: absolute; inset: 0; z-index: 2;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 14px;
  text-align: center;
  background: rgba(6, 14, 24, .88);
  backdrop-filter: blur(3px);
}
.start-screen[hidden] { display: none; }
.start-screen h3 { font-size: 22px; font-weight: 800; line-height: 1.15; margin-top: -4px; }
.start-styles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; width: 100%; }
.style-option {
  display: flex; flex-direction: column; gap: 4px;
  padding: 6px 6px 7px;
  background: var(--panel-2);
  border: 2px solid var(--line-2);
  border-radius: 10px;
  font-size: 13px; font-weight: 700;
  transition: border-color .15s, box-shadow .15s;
}
.style-option:hover { border-color: #3b5874; }
.style-option[aria-checked="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 211, 238, .12), 0 0 18px rgba(34, 211, 238, .22);
}
.style-option:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.style-option canvas { width: 100%; aspect-ratio: 1; border-radius: 6px; background: #081320; }
.start-screen .check { font-size: 13px; gap: 10px; }
.start-screen .primary-btn { width: 100%; height: 40px; padding: 0 12px; }
.check { display: inline-flex; align-items: center; gap: 12px; color: var(--muted); cursor: pointer; }

.modal-actions { display: flex; justify-content: flex-end; margin-top: 22px; }
.modal-actions .primary-btn { min-width: 200px; }

.howto-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.howto-cols ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; color: var(--muted); }
.howto-cols li { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.howto-cols li b { color: var(--text); }
.howto-cols kbd { min-width: 30px; height: 28px; font-size: 13px; }
.howto-cols kbd.wide { min-width: 64px; }

/* ---------- Medium widths: tighten the control bar ---------- */
@media (max-width: 1240px) and (min-width: 901px) {
  .cb-group { padding: 0 20px; }
  .segmented { grid-template-columns: repeat(4, 88px); }
  .cb-actions .primary-btn, .cb-actions .outline-btn { min-width: 0; padding: 0 20px; }
  .cb-ghost { gap: 16px; }
  .hint { padding: 0 16px; }
}
@media (max-width: 1080px) and (min-width: 901px) {
  .controlbar { row-gap: 12px; }
  .cb-ghost { border-left: 0 !important; }
}

/* ---------- Shorter desktop screens ---------- */
@media (max-height: 860px) and (min-width: 901px) {
  :root { --board-h: clamp(320px, calc(100dvh - 308px), 900px); }
  .layout { padding-top: 14px; gap: 10px; }
  .statbar { padding: 8px 0; }
  .statbar-item strong { font-size: 22px; }
  .controlbar { padding: 10px 18px; }
  .key-hints { display: none; }
  .credit { padding: 8px 0 10px; }
  .next-list .slot { height: 70px; }
}

/* ---------- Mobile / portrait tablet ---------- */
@media (max-width: 900px) {
  :root { --board-h: min(calc(100dvh - 292px), calc((100vw - 32px - 128px - 16px) * 2)); }

  .topbar { height: 56px; }
  .brand { gap: 10px; }
  .brand-icon { width: 32px; height: 32px; }
  .brand-logo { width: 84px; }
  .brand-sep { margin: 0 2px; height: 22px; }
  .brand-title { font-size: 12px; letter-spacing: .3em; }
  .topbar-actions { gap: 8px; }
  .link-btn span, .divider { display: none; }

  .layout { gap: 8px; padding: 8px 16px 6px; }

  .statbar { grid-template-columns: repeat(4, 1fr); padding: 6px 0; border-radius: 10px; }
  .statbar-status { display: none; }
  .statbar-item { padding: 2px 4px; }
  .statbar-item > span { font-size: 10px; letter-spacing: .06em; }
  .statbar-item strong { font-size: 18px; }
  .statbar-level .bar { height: 4px; margin-top: 3px; }

  .play-row { gap: 8px; }
  .side { margin-top: 0; }
  .side-hold, .side-next { width: 64px; }
  .side-hint { display: none; }
  .card h2 { font-size: 10px; padding: 6px 4px; text-align: center; letter-spacing: .06em; }
  .hold-slot { height: 52px; margin: 6px; }
  .next-list { padding: 4px 4px 6px; }
  .next-list .slot { height: 50px; }

  .controlbar { padding: 6px; background: none; border: 0; }
  .cb-actions, .cb-ghost { display: none; }
  .cb-style { padding: 0; border: 0; width: 100%; }
  .cb-style .cb-label { display: none; }
  .segmented { width: 100%; grid-template-columns: repeat(4, 1fr); }
  .segmented button { height: 34px; font-size: 13px; }
  .key-hints { display: none; }

  .touch-controls {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 8px;
    padding: 4px 16px 6px;
  }
  .touch-controls button {
    height: 56px;
    display: flex; align-items: center; justify-content: center;
    background: var(--panel);
    border: 1px solid var(--line-2);
    border-bottom-width: 3px;
    border-radius: 12px;
    font-size: 13px; font-weight: 700;
  }
  .touch-controls button svg { width: 24px; height: 24px; }
  .touch-controls button:active, .touch-controls button.is-down { background: var(--line-2); transform: translateY(1px); }
  .touch-controls .accent { background: var(--accent); color: var(--accent-ink); border-color: #139bb3; }
  .touch-controls .accent:active, .touch-controls .accent.is-down { background: #1bb5d0; }

  .credit { font-size: 10px; letter-spacing: .12em; padding: 0 0 max(8px, env(safe-area-inset-bottom)); }

  .modal-box { padding: 24px 18px; }
  .modal-box h2 { font-size: 24px; }
  .start-screen { gap: 6px; padding: 8px; }
  .start-screen .eyebrow { display: none; }
  .start-screen h3 { font-size: 16px; margin: 0; }
  .start-styles { gap: 6px; }
  .style-option { padding: 4px 4px 5px; font-size: 11px; border-radius: 8px; }
  .start-screen .check { font-size: 11px; }
  .start-screen .primary-btn { height: 36px; font-size: 14px; }
  .modal-actions .primary-btn { width: 100%; }
  .howto-cols { grid-template-columns: 1fr; gap: 0; }
}

/* Touch-capable large screens (e.g. tablets in landscape) also get on-screen buttons */
@media (pointer: coarse) and (min-width: 901px) {
  .key-hints, .side-hint { display: none; }
  .touch-controls {
    display: grid; grid-template-columns: repeat(7, 72px); gap: 10px;
    justify-content: center; padding: 4px 16px 12px;
  }
  .touch-controls button {
    height: 60px; display: flex; align-items: center; justify-content: center;
    background: var(--panel); border: 1px solid var(--line-2); border-bottom-width: 3px;
    border-radius: 12px; font-weight: 700;
  }
  .touch-controls .accent { background: var(--accent); color: var(--accent-ink); }
}

@media (max-width: 560px) {
  .brand-logo-link, .brand-sep { display: none; }
}
