* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  color-scheme: light;
  --paper: #d6d1c7;
  --surface: #ebe7df;
  --ink: #252521;
  --muted: #66635d;
  --rule: #9d988f;
  --accent: #a94737;
  --accent-dark: #87362a;
  --accent-contrast: #fffaf2;
  --player-two: #48677f;
  --shadow: #272722;
  --overlay-backdrop: rgba(214, 209, 199, 0.97);
  --key-surface: #dfdad1;
  --board-rule: #3d3d38;
  --font-display: "Arial Black", "Helvetica Neue", Arial, sans-serif;
  --font-body: "Helvetica Neue", Arial, sans-serif;
  --font-mono: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #1c1d1b;
  --surface: #272825;
  --ink: #dedbd2;
  --muted: #aaa69d;
  --rule: #55554f;
  --accent: #d07864;
  --accent-dark: #b86453;
  --accent-contrast: #171816;
  --player-two: #86a0ba;
  --shadow: #0e0f0e;
  --overlay-backdrop: rgba(28, 29, 27, 0.97);
  --key-surface: #30312e;
  --board-rule: #4a4b46;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
}

body::after {
  content: "STACK / FIELD 01";
  position: fixed;
  right: 18px;
  bottom: 22px;
  z-index: 2;
  color: var(--muted);
  font: 700 0.62rem var(--font-mono);
  letter-spacing: 0.16em;
  pointer-events: none;
  writing-mode: vertical-rl;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  isolation: isolate;
}

#bgCanvas {
  position: fixed;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.theme-toggle {
  position: fixed;
  top: 18px;
  right: 20px;
  z-index: 200;
  display: inline-flex;
  min-height: 38px;
  align-items: center;
  gap: 10px;
  padding: 0 11px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--surface);
  box-shadow: 2px 2px 0 var(--shadow);
  color: var(--ink);
  font: 700 0.63rem var(--font-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, box-shadow 120ms ease;
}

.theme-toggle:hover {
  background: var(--ink);
  color: var(--surface);
}

.theme-toggle:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.theme-toggle-track {
  position: relative;
  display: block;
  width: 30px;
  height: 16px;
  border: 1px solid currentColor;
  background: var(--key-surface);
}

.theme-toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 10px;
  height: 10px;
  background: var(--accent);
  transition: transform 140ms ease;
}

:root[data-theme="dark"] .theme-toggle-thumb {
  transform: translateX(14px);
}

.hidden {
  display: none !important;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 28px;
  background: var(--overlay-backdrop);
  animation: appear 140ms ease-out both;
}

.overlay.active {
  display: flex;
}

.overlay-content {
  width: min(620px, 100%);
  padding: 30px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--surface);
  box-shadow: 5px 5px 0 var(--shadow);
  text-align: left;
}

@keyframes appear {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.menu-box {
  position: relative;
}

.menu-box::before {
  content: "01";
  position: absolute;
  top: 26px;
  right: 28px;
  color: var(--accent);
  font: 900 2.1rem/1 var(--font-display);
}

.eyebrow {
  display: flex;
  width: calc(100% - 70px);
  align-items: center;
  justify-content: space-between;
  padding-bottom: 9px;
  border-bottom: 1px solid var(--ink);
  color: var(--muted);
  font: 700 0.62rem/1 var(--font-mono);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.game-title {
  margin: 24px 0 2px;
  color: var(--ink);
  font: 900 clamp(4.4rem, 11vw, 7.2rem)/0.78 var(--font-display);
  letter-spacing: -0.085em;
  text-transform: uppercase;
}

.game-title::after {
  content: ".";
  color: var(--accent);
}

.menu-subtitle {
  max-width: 340px;
  margin: 20px 0 28px;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.45;
}

.menu-buttons {
  display: grid;
  grid-template-areas:
    "solo versus"
    "controls controls"
    "speed speed"
    "help help";
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

#btnSolo { grid-area: solo; }
#btnVersus { grid-area: versus; }
#soloControlSelect { grid-area: controls; }
.speed-control { grid-area: speed; }
#btnControls { grid-area: help; }

.btn {
  min-height: 48px;
  padding: 12px 18px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--surface);
  color: var(--ink);
  font: 800 0.75rem var(--font-body);
  letter-spacing: 0.11em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, transform 120ms ease, box-shadow 120ms ease;
}

.btn:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.btn:hover {
  background: var(--ink);
  color: var(--surface);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 3px 3px 0 var(--shadow);
}

.btn-primary:hover {
  background: var(--accent-dark);
  box-shadow: 1px 1px 0 var(--shadow);
  transform: translate(2px, 2px);
}

.btn-secondary,
.btn-outline {
  background: var(--surface);
  color: var(--ink);
}

.btn-small {
  min-height: 38px;
  padding: 8px 18px;
  background: var(--surface);
  font-size: 0.65rem;
}

.control-select {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px 18px;
  padding: 12px 0 4px;
}

.control-option {
  display: flex;
  align-items: center;
  gap: 7px;
}

.control-option input[type="radio"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
}

.control-option label {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 3px;
  color: var(--muted);
  font-size: 0.74rem;
  cursor: pointer;
}

.key {
  display: inline-block;
  min-width: 40px;
  padding: 3px 6px;
  margin-right: 5px;
  border: 1px solid var(--ink);
  border-radius: 1px;
  background: var(--key-surface);
  color: var(--ink);
  font: 700 0.67rem var(--font-mono);
  text-align: center;
}

.control-option label .key {
  min-width: auto;
  padding: 2px 5px;
  margin-right: 0;
  font-size: 0.6rem;
}

.control-shared {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-left: auto;
  padding-left: 18px;
  border-left: 1px solid var(--rule);
  color: var(--muted);
  font-size: 0.72rem;
}

.control-shared .key {
  min-width: auto;
  margin-right: 0;
  padding: 2px 5px;
  font-size: 0.6rem;
}

.control-shared-label {
  margin-right: 3px;
}

.control-or {
  color: var(--faint);
  font-size: 0.65rem;
}

.speed-control {
  display: grid;
  grid-template-columns: 90px 1fr 48px;
  align-items: center;
  gap: 12px;
  padding: 13px 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
}

.speed-label {
  color: var(--muted);
  font: 700 0.62rem var(--font-mono);
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

.speed-row {
  display: flex;
  align-items: center;
  gap: 9px;
}

.speed-val {
  min-width: 28px;
  color: var(--muted);
  font-size: 0.65rem;
}

.speed-current {
  color: var(--accent);
  font: 800 0.78rem var(--font-mono);
  text-align: right;
}

#speedSlider {
  flex: 1;
  height: 3px;
  appearance: none;
  border-radius: 0;
  background: var(--ink);
  outline: none;
}

#speedSlider::-webkit-slider-thumb {
  width: 14px;
  height: 18px;
  appearance: none;
  border: 2px solid var(--ink);
  border-radius: 0;
  background: var(--accent);
  cursor: pointer;
}

#speedSlider::-moz-range-thumb {
  width: 12px;
  height: 16px;
  border: 2px solid var(--ink);
  border-radius: 0;
  background: var(--accent);
  cursor: pointer;
}

.menu-note {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  color: var(--muted);
  font: 700 0.55rem var(--font-mono);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.controls-box {
  max-width: 680px;
}

.controls-box h2,
.pause-box h2,
.gameover-box h2 {
  margin-bottom: 24px;
  font: 900 2.6rem/1 var(--font-display);
  letter-spacing: -0.055em;
  text-transform: uppercase;
}

.controls-box h2::after,
.pause-box h2::after,
.gameover-box h2::after {
  content: ".";
  color: var(--accent);
}

.controls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
  margin-bottom: 16px;
  text-align: left;
}

.controls-col {
  padding-top: 12px;
  border-top: 2px solid var(--ink);
}

.controls-col h3 {
  margin-bottom: 10px;
  color: var(--accent);
  font: 800 0.68rem var(--font-mono);
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

.key-row {
  padding: 5px 0;
  color: var(--muted);
  font-size: 0.81rem;
}

.key-row-spaced {
  margin-top: 8px;
}

.controls-pause-row {
  margin: 12px 0 18px;
  text-align: center;
}

#gameArea {
  position: relative;
  z-index: 10;
  display: flex;
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.game-layout {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 16px;
}

.board-container {
  position: relative;
  padding: 6px;
  border: 2px solid var(--ink);
  border-radius: 0;
  background: var(--surface);
  box-shadow: 5px 5px 0 var(--shadow);
  animation: board-in 160ms ease-out both;
}

.solo-layout .board-container canvas,
.player-area .board-container canvas {
  display: block;
  border-radius: 0;
}

.board-border {
  position: absolute;
  inset: 6px;
  border: 1px solid var(--board-rule);
  border-radius: 0;
  pointer-events: none;
}

@keyframes board-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.side-panel {
  display: flex;
  min-width: 126px;
  flex-direction: column;
  gap: 12px;
}

.panel-box {
  padding: 11px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--surface);
}

.panel-label {
  display: block;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--rule);
  color: var(--ink);
  font: 700 0.65rem var(--font-mono);
  letter-spacing: 0.14em;
  text-align: left;
  text-transform: uppercase;
}

.panel-box canvas {
  display: block;
  margin: 0 auto;
}

.stats-box {
  padding: 10px 12px;
  text-align: left;
}

.stat {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--rule);
}

.stat:last-child {
  border-bottom: 0;
}

.stat-label {
  color: var(--muted);
  font: 700 0.61rem var(--font-mono);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.stat-value {
  color: var(--accent);
  font: 800 0.98rem var(--font-mono);
}

.player-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
}

.player-label {
  align-self: stretch;
  padding: 5px 7px;
  background: var(--ink);
  color: var(--surface);
  font: 700 0.64rem var(--font-mono);
  letter-spacing: 0.17em;
  text-align: center;
  text-transform: uppercase;
}

.p1-color {
  color: var(--accent);
}

.p2-color {
  color: var(--player-two);
}

.player-label.p1-color,
.player-label.p2-color {
  color: var(--surface);
}

.player-area {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.versus-layout {
  gap: 0;
}

.versus-layout .side-panel {
  min-width: 100px;
}

.vs-divider {
  display: flex;
  min-height: 500px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
}

.vs-text {
  width: 44px;
  height: 44px;
  padding-top: 13px;
  background: var(--accent);
  color: var(--surface);
  font: 900 0.72rem var(--font-mono);
  letter-spacing: 0.1em;
  text-align: center;
}

.win-display {
  margin-top: 13px;
  font: 900 1.85rem var(--font-mono);
}

.win-sep {
  margin: 0 5px;
  color: var(--muted);
}

.game-buttons {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

.pause-box,
.gameover-box {
  max-width: 480px;
}

.pause-box .btn,
.gameover-box .btn {
  width: 100%;
  margin-top: 9px;
}

.gameover-stats {
  margin-bottom: 20px;
  padding: 14px 0;
  border-top: 2px solid var(--ink);
  border-bottom: 1px solid var(--rule);
  color: var(--muted);
  font: 0.86rem/1.9 var(--font-mono);
  text-align: left;
}

.gameover-stats strong {
  color: var(--ink);
}

.match-result {
  color: var(--accent);
  font-weight: 800;
}

.match-result-large {
  font-size: 1.25rem;
}

body,
.overlay,
.overlay-content,
.board-container,
.panel-box,
.key {
  transition: background-color 180ms ease, color 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
}

@media (max-width: 1050px) {
  body::after { display: none; }
  .game-layout { transform: scale(0.82); transform-origin: top center; }
  .versus-layout { transform: scale(0.72); }
  .vs-divider { min-height: 430px; padding: 0 10px; }
}

@media (max-width: 700px) {
  .theme-toggle { top: 12px; right: 12px; }
  .overlay { align-items: flex-start; padding: 62px 14px 14px; }
  .overlay-content { padding: 22px; box-shadow: 4px 4px 0 var(--shadow); }
  .menu-box::before { top: 20px; right: 20px; font-size: 1.5rem; }
  .game-title { font-size: clamp(3.6rem, 20vw, 5.5rem); }
  .menu-buttons {
    grid-template-areas:
      "solo"
      "versus"
      "controls"
      "speed"
      "help";
    grid-template-columns: 1fr;
  }
  .control-select { flex-direction: column; align-items: flex-start; gap: 8px; }
  .control-shared {
    width: 100%;
    margin-left: 0;
    padding: 9px 0 0;
    border-top: 1px solid var(--rule);
    border-left: 0;
  }
  .speed-control { grid-template-columns: 1fr 44px; }
  .speed-label { grid-column: 1 / -1; }
  .controls-grid { grid-template-columns: 1fr; gap: 18px; }
  .menu-note { gap: 12px; }
  .game-layout { transform: scale(0.58); transform-origin: top center; }
  .versus-layout { transform: scale(0.46); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
}
