:root {
  --bg: #0a0a0f;
  --bg-2: #14141d;
  --bg-3: #1d1d2a;
  --fg: #f4f4fa;
  --muted: #9aa0b4;
  --accent: #ffb547;
  --accent-2: #ff8a3d;
  --border: #2a2a3a;
  --hit: 48px;
  --header-h: 76px;
  --toolbar-h: 76px;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Inter, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
}

/* ---------- Header ---------- */
.brand {
  background: rgba(15, 15, 25, 0.55);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  border-bottom: 1px solid var(--border);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: var(--header-h);
  position: relative;
  z-index: 10;
}
.brand-left {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}
.brand-logo {
  height: 50px;
  width: auto;
  display: block;
  /* PNG is already pure white with alpha; keep it crisp on dark bg. */
}
.brand-logo-link {
  display: block;
  border-radius: 4px;
}
.brand-logo-link:focus { outline: none; }
.brand-logo-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
}
.brand-app {
  display: flex;
  flex-direction: column;
  gap: 2px;
  line-height: 1.2;
}
.brand-app-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--fg);
  white-space: nowrap;
}
.brand-app-sub {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}
.brand-app-sub a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: color 120ms, border-color 120ms;
}
.brand-app-sub a:hover {
  color: var(--fg);
  border-bottom-color: currentColor;
}
.brand-app-sub a:focus { outline: none; }
.brand-app-sub a:focus-visible {
  color: var(--fg);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-bottom-color: transparent;
}
.brand-right {
  display: flex;
  flex-direction: column;
  gap: 2px;
  line-height: 1.2;
  text-align: right;
  min-width: 0;
}
.brand .series { color: var(--muted); font-size: 13px; letter-spacing: 0.02em; }
.brand .title {
  font-size: 17px;
  font-weight: 600;
  color: var(--fg);
  text-decoration: none;
}
.brand .title:hover { text-decoration: underline; text-underline-offset: 3px; }
.brand .title:focus { outline: none; }
.brand .title:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}
.brand .speaker { color: var(--muted); font-size: 13px; }

@media (max-width: 900px) {
  .brand-app-title { font-size: 16px; }
  .brand-logo { height: 40px; }
  .brand .title { font-size: 14px; }
  .brand .series, .brand .speaker { font-size: 11px; }
}
@media (max-width: 600px) {
  :root { --header-h: 100px; }
  .brand { flex-direction: column; align-items: flex-start; gap: 4px; padding: 8px 12px; }
  .brand-right { text-align: left; }
}

/* ---------- Stage ---------- */
.stage {
  position: relative;
  overflow: hidden;
  background: transparent;
}
/* Canvas spans the entire viewport so the translucent header/toolbar/panels
   blur the live simulation behind them. */
canvas#gl {
  display: block;
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  z-index: 0;
  touch-action: none;
  cursor: grab;
}
canvas#gl.drawing { cursor: crosshair; }
canvas#gl:active { cursor: grabbing; }

.hud-top {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 8px;
  pointer-events: none;
}
.hud-stat {
  background: rgba(20, 20, 30, 0.7);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 6px 10px;
  border-radius: 8px;
  font-variant-numeric: tabular-nums;
  font-size: 15px;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: rgba(20, 20, 30, 0.55);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  min-height: var(--toolbar-h);
  scrollbar-width: thin;
  position: relative;
  z-index: 10;
}
.toolbar > button,
.toolbar .seg,
.toolbar .slider,
.toolbar label.select {
  flex: 0 0 auto;
}
.toolbar button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: var(--hit);
  min-width: var(--hit);
  padding: 0 14px;
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 120ms, transform 80ms;
  white-space: nowrap;
}
.toolbar button:hover:not(.active):not(:disabled) { background: #262636; }
.toolbar button:focus { outline: none; }
.toolbar button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.toolbar button:active:not(:disabled) {
  transform: scale(0.97);
  background: var(--accent);
  color: #1a0e00;
  border-color: var(--accent-2);
}
.toolbar button.primary:active { background: var(--accent-2); }
/* Active toggle buttons (contrast, fullscreen, mode toggles) glow orange and
   ignore hover — no more dark-interior-with-orange-outline combo. */
.toolbar button.active {
  background: var(--accent);
  color: #1a0e00;
  border-color: var(--accent-2);
  outline: none;
}
.toolbar button.active:hover { background: var(--accent); }
/* Segmented buttons get the same immediate-press feedback. */
.seg-btn:focus { outline: none; }
.seg-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.seg-btn:active:not(:disabled) { background: var(--accent); color: #1a0e00; }
.toolbar button.primary {
  background: var(--accent);
  color: #1a0e00;
  border-color: var(--accent-2);
}
.toolbar button.primary:hover { background: #ffc26b; }
.toolbar button.active { outline: 2px solid var(--accent); outline-offset: -2px; }
.toolbar button .ico { font-size: 18px; line-height: 1; }
.toolbar button.icon-only {
  padding: 0;
  width: var(--hit);
  justify-content: center;
}
.toolbar button.icon-only .ico {
  font-size: 22px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}
.toolbar .spacer {
  flex: 1 1 auto;
  min-width: 0;
}
.toolbar.compact-labels button .lbl,
.toolbar.compact-labels .seg-lbl { display: none; }
.toolbar.compact-labels .seg-btn {
  width: var(--hit);
  padding: 0;
  justify-content: center;
}

.slider {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px 10px;
  min-width: 160px;
}
.slider label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.slider label span { color: var(--fg); font-variant-numeric: tabular-nums; }
.slider input[type=range] {
  width: 100%;
  accent-color: var(--accent);
  height: 28px;
}

.toolbar label.select {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
}
.toolbar label.select select {
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  min-height: 36px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
}

@media (max-width: 1180px) {
  .toolbar button .lbl,
  .toolbar .seg-lbl { display: none; }
  .toolbar .seg-btn {
    width: var(--hit);
    padding: 0;
    justify-content: center;
  }
  .slider { min-width: 130px; }
}

/* ---------- Side Panels ---------- */
.panel {
  position: fixed;
  top: var(--header-h);
  bottom: var(--toolbar-h);
  right: 0;
  width: min(420px, 92vw);
  background: rgba(20, 20, 30, 0.7);
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  border-left: 1px solid var(--border);
  box-shadow: -10px 0 40px rgba(0,0,0,0.5);
  transform: translateX(100%);
  transition: transform 220ms ease;
  display: flex;
  flex-direction: column;
  z-index: 20;
}
.panel[aria-hidden="false"] { transform: translateX(0); }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { margin: 0; font-size: 18px; }
.panel-head .close {
  width: var(--hit); height: var(--hit);
  background: transparent; color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
}
.panel-body {
  overflow-y: auto;
  padding: 10px 12px 24px;
  flex: 1;
}

.cat-group { margin-bottom: 14px; }
.cat-group h3 {
  font-size: 13px;
  margin: 12px 4px 6px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}
.tile {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  color: var(--fg);
  font-size: 13px;
  min-height: 88px;
  transition: background 120ms;
}
.tile:hover { background: #262636; }
.tile .tile-name { font-weight: 600; font-size: 14px; }
.tile .tile-desc { color: var(--muted); font-size: 11px; line-height: 1.35; }
.tile canvas { width: 100%; height: 64px; image-rendering: pixelated; background: #08080d; border-radius: 6px; }

/* Preset tiles span wider */
#preset-list .tile { grid-column: span 1; }
#preset-list .tile-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

/* ---------- Stamp banner ---------- */
.stamp-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20,20,30,0.95);
  backdrop-filter: blur(8px);
  border: 1px solid var(--accent);
  color: var(--fg);
  padding: 8px 12px;
  border-radius: 999px;
  align-items: center;
  gap: 10px;
  z-index: 50;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
  pointer-events: auto;
  display: none;
}
.stamp-banner[data-active="true"] {
  display: flex;
}
.stamp-banner #stamp-banner-name { font-weight: 600; color: var(--accent); }
.stamp-banner .hint { color: var(--muted); font-size: 12px; }
.stamp-banner button {
  width: 40px; height: 40px;
  padding: 0;
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  font-family: inherit;
  pointer-events: auto;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.stamp-banner button:hover { background: #2c2c3e; }
.stamp-banner button:active { transform: scale(0.95); }

.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  /* Make sure buttons inside the segmented control don't carry the
     individual rounded corners they'd otherwise inherit from .toolbar button. */
}
.toolbar .seg-btn,
.seg-btn {
  background: var(--bg-3);
  color: var(--muted);
  border: none;
  border-right: 1px solid var(--border);
  border-radius: 0;
  padding: 0 14px;
  min-height: var(--hit);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background 120ms;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
}
.seg-btn:last-child { border-right: none; }
.seg-btn:hover:not(:disabled):not(.active) { background: #2c2c3e; color: var(--fg); }
.seg-btn.active { background: var(--accent); color: #1a0e00; font-weight: 600; }
.seg-btn:disabled { opacity: 0.45; cursor: not-allowed; }
@media (max-width: 600px) {
  .stamp-banner .hint { display: none; }
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .panel { transition: none; }
}

/* ---------- HUD stats: clickable variant + graph overlay ---------- */
.hud-top { z-index: 5; }
.hud-top .hud-stat { pointer-events: auto; }
button.hud-stat-btn {
  /* Reset the user-agent button so it matches the sibling .hud-stat divs exactly. */
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  font-weight: inherit;
  /* font-size intentionally left to .hud-stat so all three pills match. */
  line-height: inherit;
  letter-spacing: inherit;
  margin: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button.hud-stat-btn:focus { outline: none; }
button.hud-stat-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.hud-stat-btn[aria-expanded="true"] { border-color: var(--accent); }
.hud-stat-ico {
  font-size: 13px;
  line-height: 1;
  opacity: 0.85;
}

.pop-graph {
  position: absolute;
  top: 56px;
  left: 12px;
  width: min(360px, 80vw);
  height: 160px;
  background: rgba(20, 20, 30, 0.78);
  backdrop-filter: blur(8px) saturate(140%);
  -webkit-backdrop-filter: blur(8px) saturate(140%);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px 6px;
  z-index: 6;
  display: none;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}
.pop-graph[aria-hidden="false"] { display: block; }
.pop-graph canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* ---------- Tutorial modal ---------- */
.tutorial-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: none;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.48);
  backdrop-filter: blur(8px) saturate(115%);
  -webkit-backdrop-filter: blur(8px) saturate(115%);
}
.tutorial-overlay[aria-hidden="false"] { display: flex; }
.tutorial-dialog {
  width: min(760px, 96vw);
  max-height: min(760px, calc(100dvh - 32px));
  overflow: hidden;
  background: rgba(20, 20, 30, 0.94);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 18px 70px rgba(0, 0, 0, 0.62);
  display: flex;
  flex-direction: column;
}
.tutorial-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.tutorial-head h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 650;
}
.tutorial-close {
  width: 42px;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-3);
  color: var(--fg);
  font: inherit;
  font-size: 18px;
  cursor: pointer;
}
.tutorial-close:hover { background: #2c2c3e; }
.tutorial-close:focus { outline: none; }
.tutorial-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tutorial-body {
  overflow-y: auto;
  padding: 16px;
}
.tutorial-lead {
  margin: 0 0 14px;
  color: var(--fg);
  line-height: 1.45;
  font-size: 15px;
}
.rule-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(128px, 1fr));
  gap: 10px;
}
.rule-card {
  background: rgba(29, 29, 42, 0.8);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 8px;
}
.rule-card h3 {
  margin: 0;
  font-size: 14px;
  color: var(--fg);
}
.rule-card p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}
.rule-board,
.blinker-demo {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  width: 72px;
  aspect-ratio: 1;
  padding: 5px;
  border-radius: 8px;
  background: #08080d;
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.rule-board span,
.blinker-demo span {
  aspect-ratio: 1;
  border-radius: 3px;
  background: #161620;
  border: 1px solid rgba(255, 255, 255, 0.05);
}
.rule-board .alive,
.blinker-demo .blink-h,
.blinker-demo .blink-v,
.blinker-demo .blink-center {
  background: var(--accent);
  box-shadow: 0 0 10px rgba(255, 181, 71, 0.32);
}
.rule-board .next {
  animation: bornPulse 1800ms ease-in-out infinite;
}
.rule-board .fades {
  animation: deathFade 1800ms ease-in-out infinite;
}
.tutorial-example {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid rgba(255, 181, 71, 0.32);
  border-radius: 8px;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 12px;
  background: rgba(255, 181, 71, 0.08);
}
.tutorial-example p {
  margin: 0;
  color: var(--fg);
  font-size: 13px;
  line-height: 1.42;
}
.blinker-demo .blink-v { animation: blinkV 1600ms steps(1, end) infinite; }
.blinker-demo .blink-h { animation: blinkH 1600ms steps(1, end) infinite; }
.blinker-demo .blink-center { animation: none; }

@keyframes bornPulse {
  0%, 45% { background: #161620; box-shadow: none; }
  55%, 100% { background: var(--accent); box-shadow: 0 0 10px rgba(255, 181, 71, 0.34); }
}
@keyframes deathFade {
  0%, 45% { background: var(--accent); box-shadow: 0 0 10px rgba(255, 181, 71, 0.34); }
  55%, 100% { background: #161620; box-shadow: none; }
}
@keyframes blinkH {
  0%, 49% { background: var(--accent); box-shadow: 0 0 10px rgba(255, 181, 71, 0.34); }
  50%, 100% { background: #161620; box-shadow: none; }
}
@keyframes blinkV {
  0%, 49% { background: #161620; box-shadow: none; }
  50%, 100% { background: var(--accent); box-shadow: 0 0 10px rgba(255, 181, 71, 0.34); }
}

@media (max-width: 720px) {
  .tutorial-dialog {
    width: min(520px, 96vw);
    max-height: calc(100dvh - 24px);
  }
  .rule-grid { grid-template-columns: repeat(2, minmax(128px, 1fr)); }
}
@media (max-width: 420px) {
  .tutorial-overlay { padding-inline: 10px; }
  .tutorial-head { padding: 12px; }
  .tutorial-head h2 { font-size: 18px; }
  .tutorial-body { padding: 12px; }
  .rule-grid { grid-template-columns: 1fr; }
  .rule-card {
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    align-items: center;
  }
  .rule-card p { grid-column: 2; }
  .tutorial-example {
    grid-template-columns: 1fr;
    justify-items: start;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rule-board .next,
  .rule-board .fades,
  .blinker-demo .blink-v,
  .blinker-demo .blink-h,
  .blinker-demo .blink-center {
    animation: none;
  }
  .rule-board .next,
  .blinker-demo .blink-v,
  .blinker-demo .blink-h,
  .blinker-demo .blink-center {
    background: var(--accent);
    box-shadow: 0 0 10px rgba(255, 181, 71, 0.34);
  }
  .rule-board .fades {
    background: #161620;
    box-shadow: none;
  }
}
