:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --board: #0b1324;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent-hover: #4ade80;
  --accent-ink: #052e16;
  --border: rgba(148, 163, 184, 0.18);
  --radius: 12px;
  --shadow: 0 12px 32px rgba(2, 6, 23, 0.45), 0 2px 6px rgba(2, 6, 23, 0.3);
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --fast: 150ms;
  --slow: 250ms;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  /* Height taken by everything except the board; the board gets the rest. */
  --reserve: 236px;
  --col: min(600px, max(280px, calc(100vh - var(--reserve))));
  color-scheme: dark;
}

@supports (height: 100svh) {
  :root {
    --col: min(600px, max(280px, calc(100svh - var(--reserve))));
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  background:
    radial-gradient(ellipse 80% 45% at 50% -8%, rgba(34, 197, 94, 0.13), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: calc(var(--col) + 32px);
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: 8px 16px calc(4px + env(safe-area-inset-bottom));
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  flex: none;
  width: 32px;
  height: 32px;
  filter: drop-shadow(0 4px 10px rgba(34, 197, 94, 0.25));
}

h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

/* Buttons */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 40px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(2, 6, 23, 0.3);
  transition:
    background-color var(--fast) var(--ease),
    border-color var(--fast) var(--ease),
    transform var(--fast) var(--ease),
    opacity var(--fast) var(--ease);
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  border-color: rgba(148, 163, 184, 0.32);
  background: var(--surface-2);
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}

.btn-square {
  width: 40px;
  padding: 0;
}

.btn-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover {
  border-color: transparent;
  background: var(--accent-hover);
}

.btn-wide {
  width: 100%;
  min-height: 44px;
  font-size: 1rem;
}

.btn:focus-visible,
.segmented button:focus-visible,
.dpad-btn:focus-visible,
.footer a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-play {
  fill: currentColor;
  stroke-width: 2;
}

#pause .icon-play,
#pause.is-paused .icon-pause {
  display: none;
}

#pause.is-paused .icon-play {
  display: block;
}

/* Score bar */
.stage {
  display: flex;
  flex-direction: column;
}

.hud {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.stat {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 72px;
  min-height: 48px;
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(2, 6, 23, 0.3);
}

.stat-label,
.field-label {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stat-value {
  display: inline-block;
  font-size: 1.2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  transform-origin: left center;
}

.chip {
  min-width: 0;
  margin-left: auto;
  padding: 6px 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(30, 41, 59, 0.6);
  color: var(--muted);
  font-size: 0.85rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Board */
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border: 2px solid rgba(34, 197, 94, 0.3);
  border-radius: var(--radius);
  background: var(--board);
  box-shadow: var(--shadow);
  outline: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  transition: border-color var(--slow) var(--ease);
}

.board[data-mode="wrap"] {
  border-style: dashed;
  border-color: rgba(34, 197, 94, 0.5);
}

.board canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 12px;
  background: rgba(11, 19, 36, 0.72);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--slow) var(--ease), visibility var(--slow) var(--ease);
}

.overlay.is-visible {
  opacity: 1;
  visibility: visible;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(100%, 340px);
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(30, 41, 59, 0.94);
  box-shadow: var(--shadow);
  text-align: center;
  transform: translateY(6px) scale(0.98);
  transition: transform var(--slow) var(--ease);
}

.overlay.is-visible .card {
  transform: none;
}

.card-title {
  margin: 0;
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}

.segmented-2 {
  grid-template-columns: repeat(2, 1fr);
}

.segmented button {
  appearance: none;
  min-width: 0;
  min-height: 40px;
  padding: 0 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--fast) var(--ease), color var(--fast) var(--ease);
  -webkit-tap-highlight-color: transparent;
}

.segmented button:hover {
  background: rgba(148, 163, 184, 0.1);
  color: var(--text);
}

.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.muted {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.hint-touch {
  display: none;
}

.results {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.results .stat {
  align-items: center;
  background: var(--bg);
  box-shadow: none;
}

.badge {
  width: fit-content;
  margin: 0 auto;
  padding: 4px 12px;
  border-radius: var(--radius);
  background: rgba(34, 197, 94, 0.15);
  color: var(--accent-hover);
  font-size: 0.85rem;
  font-weight: 700;
}

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

/* Touch arrow pad */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 64px);
  grid-template-rows: repeat(2, 48px);
  grid-template-areas:
    ". up ."
    "left down right";
  gap: 6px;
  justify-content: center;
  margin-top: 12px;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.dpad-btn {
  appearance: none;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background-color var(--fast) var(--ease), transform var(--fast) var(--ease);
  -webkit-tap-highlight-color: transparent;
}

.dpad-btn:active {
  background: var(--accent);
  color: var(--accent-ink);
  transform: scale(0.95);
}

.dpad-up { grid-area: up; }
.dpad-down { grid-area: down; }
.dpad-left { grid-area: left; }
.dpad-right { grid-area: right; }
.dpad-down .icon { transform: rotate(180deg); }
.dpad-left .icon { transform: rotate(-90deg); }
.dpad-right .icon { transform: rotate(90deg); }

.board-hint {
  margin-top: 12px;
  text-align: center;
}

/* Footer */
.footer {
  margin-top: auto;
  padding-top: 8px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.footer a {
  display: inline-block;
  padding: 10px 4px;
  color: var(--text);
  line-height: 20px;
  text-decoration-color: rgba(34, 197, 94, 0.6);
  text-underline-offset: 3px;
  transition: color var(--fast) var(--ease);
}

.footer a:hover {
  color: var(--accent-hover);
}

/* Touch devices: show the arrow pad and touch hints instead of keyboard hints. */
@media (hover: none) and (pointer: coarse) {
  :root {
    --reserve: 316px;
  }

  .dpad {
    display: grid;
  }

  .hint-keys {
    display: none;
  }

  .hint-touch {
    display: block;
  }
}

.is-touch {
  --reserve: 316px;
}

.is-touch .dpad {
  display: grid;
}

.is-touch .hint-keys {
  display: none;
}

.is-touch .hint-touch {
  display: block;
}

/* The first overlay appears without a fade; main.js removes this class after one frame. */
.preload *,
.preload *::before,
.preload *::after {
  transition: none !important;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
