:root {
  --bg: #07111c;
  --panel: #0c1826;
  --panel-2: #0a1420;
  --line: #1a2b3e;
  --line-2: #22374d;
  --text: #e8f1fa;
  --muted: #92a3b8;
  --accent: #22d3ee;
  --accent-2: #38bdf8;
  --accent-ink: #04202a;
  --radius: 14px;
  --board-h: clamp(360px, calc(100dvh - 290px), 820px);
  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 600px at 50% -10%, #0d2034 0%, var(--bg) 60%) 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; }

/* ---------- Header ---------- */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: 64px;
  padding: 0 clamp(16px, 3.5vw, 56px);
  border-bottom: 1px solid var(--line);
  background: rgba(7, 17, 28, .6);
  backdrop-filter: blur(8px);
}

.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; }
.brand-icon { width: 44px; height: 44px; }
.brand-logo { width: 110px; height: auto; }

.topbar-title {
  color: var(--accent);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: .38em;
  text-transform: uppercase;
  padding-left: .38em;
}

.topbar-actions { display: flex; align-items: center; justify-content: flex-end; gap: 14px; }
.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: var(--text); font-size: 15px;
}
.link-btn:hover, .icon-btn:hover { color: var(--accent); }
.icon-btn svg { width: 22px; height: 22px; }
#settings-top-btn { color: var(--accent); }

.sound-toggle { display: inline-flex; align-items: center; gap: 12px; cursor: pointer; }
.sound-toggle input, .check input { position: absolute; opacity: 0; pointer-events: none; }

.switch {
  position: relative;
  width: 42px; 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;
}
input:checked + .switch { background: var(--accent); }
input:checked + .switch::after { transform: translateX(18px); }
input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: auto minmax(340px, 400px);
  grid-template-rows: auto auto;
  grid-template-areas:
    "title ."
    "board panel";
  justify-content: center;
  align-content: center;
  column-gap: 16px;
  row-gap: 14px;
  padding: 18px clamp(16px, 3vw, 40px) 16px;
}

.board-col { display: contents; }

.board-title { grid-area: title; display: flex; align-items: baseline; gap: 18px; flex-wrap: wrap; }
.board-title h1 { font-size: 34px; font-weight: 800; letter-spacing: -.01em; line-height: 1.1; }
.board-title h1 em { font-style: normal; color: var(--accent); }
.board-title p { color: var(--muted); font-size: 16px; }

.board-frame {
  grid-area: board;
  position: relative;
  height: var(--board-h);
  aspect-ratio: 1 / 2;
  --glow: 34, 211, 238;
  border: 2px solid rgba(var(--glow), .55);
  border-radius: 10px;
  background: #081320;
  box-shadow: 0 0 22px rgba(var(--glow), .22), inset 0 0 18px rgba(var(--glow), .06);
  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; }

/* ---------- Side panel ---------- */
.panel {
  grid-area: panel;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px 14px 16px;
  background: linear-gradient(180deg, #0d1a29 0%, #0a1522 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 0;
}

.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 0 2px 0 10px; }

.status { display: inline-flex; align-items: center; gap: 10px; font-size: 15px; color: var(--muted); }
.status i { width: 11px; height: 11px; 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; }

.card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 14px;
}
.card h2 {
  font-size: 14px; font-weight: 700; letter-spacing: .08em;
  text-transform: uppercase; margin-bottom: 12px;
}

.slot {
  background: #08111b;
  border: 1px solid var(--line);
  border-radius: 10px;
  min-height: 0;
}
.slot { position: relative; }
.slot canvas { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }

.eyebrow {
  color: var(--accent); font-size: 13px; font-weight: 700;
  letter-spacing: .22em; text-transform: uppercase;
}

.score-card { display: flex; align-items: center; gap: 22px; padding: 16px 20px; }
.score-main { min-width: 0; }
.score-card strong {
  display: block; margin-top: 2px;
  font-size: clamp(40px, 3.6vw, 52px); font-weight: 700; line-height: 1.05;
  color: var(--accent); font-variant-numeric: tabular-nums;
  text-shadow: 0 0 28px rgba(34, 211, 238, .25);
}
.best {
  display: flex; align-items: center; gap: 16px;
  padding-left: 22px;
  border-left: 1px solid var(--line-2);
  min-height: 62px;
}
.best svg { width: 28px; height: 28px; color: var(--accent); }
.best div { display: flex; flex-direction: column; gap: 4px; }
.best span { color: var(--muted); font-size: 15px; }
.best b { color: var(--text); font-size: 22px; font-variant-numeric: tabular-nums; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.stat {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 14px;
  display: flex; flex-direction: column; gap: 2px;
}
.stat span { color: var(--muted); font-size: 15px; }
.stat strong { font-size: 25px; font-weight: 700; font-variant-numeric: tabular-nums; }

.hold-next {
  flex: 1;
  min-height: 150px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.hold-card, .next-card { display: flex; flex-direction: column; min-height: 0; }
.hold-card .slot { flex: 1; }
.next-card { gap: 6px; }
.next-card h2 { margin-bottom: 6px; }
.next-card .slot { flex: 1; min-height: 28px; }

.progress { padding: 0 2px; }
.progress-row { display: flex; justify-content: space-between; font-size: 15px; color: var(--muted); margin-bottom: 8px; }
.progress-row span:last-child { color: var(--text); }
.bar { height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; }
.bar div {
  height: 100%; width: 0; border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  box-shadow: 0 0 12px rgba(34, 211, 238, .5);
  transition: width .3s ease;
}

.style-switch { padding: 0 2px; }
.style-switch > span { display: block; color: var(--text); font-size: 17px; font-weight: 600; margin-bottom: 10px; }
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.style-tile {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 4px 9px;
  background: linear-gradient(180deg, #0d1a29 0%, #0a1522 100%);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  font-size: 14px; font-weight: 600;
  transition: border-color .15s, box-shadow .15s;
}
.style-tile canvas { width: 36px; height: 36px; }
.style-tile:hover { border-color: #3b5874; }
.style-tile[aria-checked="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 0 18px rgba(34, 211, 238, .25), inset 0 0 16px rgba(34, 211, 238, .06);
}
.style-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.actions { display: grid; grid-template-columns: 1.6fr 1fr; gap: 12px; }

.ghost-btn, .outline-btn, .primary-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  border-radius: 8px;
}
.ghost-btn {
  padding: 8px 18px;
  background: transparent;
  border: 1px solid var(--line-2);
  font-size: 14px;
}
.ghost-btn svg { width: 16px; height: 16px; }
.ghost-btn:hover { border-color: var(--accent); color: var(--accent); }
.ghost-btn:disabled { opacity: .45; cursor: default; }

.primary-btn {
  padding: 11px 20px;
  background: linear-gradient(180deg, #3ee0f5, #1ec2e0);
  color: var(--accent-ink); border: 0;
  font-size: 17px; font-weight: 700;
  box-shadow: 0 6px 24px rgba(34, 211, 238, .25);
}
.primary-btn svg { width: 18px; height: 18px; }
.primary-btn:hover { filter: brightness(1.07); }
.outline-btn {
  padding: 10px 18px;
  background: transparent; border: 1px solid #5a6f86;
  font-size: 17px; font-weight: 600;
}
.outline-btn:hover { border-color: var(--accent); }

/* ---------- Controls bar ---------- */
.controls-bar {
  align-self: center;
  display: flex; align-items: center; flex-wrap: wrap; justify-content: center;
  margin: 0 16px;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 14px;
}
.controls-bar > * { padding: 2px 18px; }
.controls-bar > * + * { border-left: 1px solid var(--line-2); }
.group { display: inline-flex; align-items: center; gap: 8px; }
.group em { font-style: normal; color: var(--muted); margin-left: 8px; }

kbd {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 36px; height: 34px; padding: 0 10px;
  background: #0f1d2d;
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 7px;
  font: 600 14px/1 inherit;
  font-family: inherit;
  color: var(--text);
}
kbd.wide { min-width: 92px; }
kbd svg { width: 16px; height: 16px; }

.credit { text-align: center; color: var(--muted); font-size: 13px; padding: 14px 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; }

.style-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin: 24px 0 20px;
}
.style-option {
  display: flex; flex-direction: column; gap: 10px;
  padding: 10px 10px 14px;
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: 14px;
  text-align: center;
  font-size: 16px; font-weight: 700;
  transition: border-color .15s, transform .15s;
}
.style-option:hover { border-color: #3b5874; transform: translateY(-2px); }
.style-option[aria-checked="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(34, 211, 238, .12), 0 0 24px rgba(34, 211, 238, .18);
}
.style-option canvas { width: 100%; aspect-ratio: 5 / 8; border-radius: 8px; background: #081320; }

.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; }

/* ---------- Short desktop screens ---------- */
@media (max-height: 860px) and (min-width: 901px) {
  :root { --board-h: clamp(360px, calc(100dvh - 250px), 820px); }
  .layout { padding-top: 12px; row-gap: 10px; }
  .board-title h1 { font-size: 28px; }
  .score-card strong { font-size: 42px; }
  .stat { padding: 8px 12px; }
  .stat strong { font-size: 22px; }
  .card { padding: 10px 12px; }
  .credit { padding: 10px 0 12px; }
}

@media (max-height: 800px) and (min-width: 901px) {
  :root { --board-h: clamp(360px, calc(100dvh - 186px), 820px); }
  .controls-bar { display: none; }
  .panel { gap: 8px; padding-top: 10px; padding-bottom: 12px; }
  .score-card strong { font-size: 36px; }
  .best b { font-size: 19px; }
  .style-switch > span { display: none; }
  .style-tile { padding: 6px 4px; gap: 3px; font-size: 13px; }
  .style-tile canvas { width: 28px; height: 28px; }
  .hold-next { min-height: 140px; }
  .card h2 { margin-bottom: 8px; }
}

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

  .topbar { height: 54px; grid-template-columns: 1fr auto; }
  .topbar-title, .link-btn span { display: none; }
  .brand-icon { width: 34px; height: 34px; }
  .brand-logo { width: 84px; }
  .topbar-actions { gap: 8px; }

  .layout {
    grid-template-columns: 78px 1fr 78px;
    grid-template-areas:
      "title title title"
      "hold score next"
      "board board board";
    gap: 8px 10px;
    padding: 8px 16px 8px;
    align-items: start;
  }
  .panel, .hold-next { display: contents; }
  .panel-head, .best, .progress, .style-switch, .actions, .controls-bar, .board-title p { display: none; }

  .board-title { justify-content: center; }
  .board-title h1 { font-size: 18px; }
  .board-frame { justify-self: center; }

  .card { padding: 8px; border-radius: 10px; }
  .card h2 { font-size: 11px; margin-bottom: 6px; text-align: center; }
  .hold-card { grid-area: hold; }
  .hold-card .slot { height: 58px; flex: none; }
  .next-card { grid-area: next; gap: 0; }
  .next-card .slot { height: 58px; flex: none; }
  .next-card .slot + .slot { display: none; }

  .score-card, .stats { grid-area: score; }
  .score-card { display: block; border: 0; padding: 0; background: none; text-align: center; align-self: start; }
  .score-card .eyebrow { font-size: 11px; }
  .score-card strong { font-size: 30px; margin: 0; }
  .stats { align-self: end; gap: 6px; }
  .stat { padding: 4px 6px; align-items: center; gap: 0; border-radius: 8px; }
  .stat span { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; }
  .stat strong { font-size: 15px; }

  .touch-controls {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 8px;
    padding: 4px 16px 6px;
  }
  .touch-controls button {
    height: 58px;
    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: 11px; padding: 0 0 max(8px, env(safe-area-inset-bottom)); }

  .modal-box { padding: 24px 18px; }
  .modal-box h2 { font-size: 24px; }
  .style-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 18px 0 16px; }
  .style-option canvas { aspect-ratio: 5 / 6; }
  .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) {
  .controls-bar { 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); }
}
