:root {
  color-scheme: dark;
  --bg: #101418;
  --panel: #171d22;
  --panel-2: #20272d;
  --line: #303a43;
  --text: #f2f6f8;
  --muted: #aab6bd;
  --accent: #86d672;
  --accent-2: #f2c94c;
  --danger: #ff6b66;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

html {
  /* No pull-to-refresh / rubber-band bounce; no double-tap zoom. */
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(134, 214, 114, 0.12), transparent 30rem),
    linear-gradient(135deg, #101418 0%, #14191e 55%, #0d1114 100%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

button,
select {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 6px;
  touch-action: manipulation;
}

button:hover,
button:focus-visible,
select:hover,
select:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.shell {
  --pad: 16px;
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  /* Top-aligned (not vertically centred) so the arena sizing in game.js is stable. */
  align-content: start;
  justify-items: center;
  padding:
    max(var(--pad), env(safe-area-inset-top))
    max(var(--pad), env(safe-area-inset-right))
    max(var(--pad), env(safe-area-inset-bottom))
    max(var(--pad), env(safe-area-inset-left));
}

.game-panel {
  width: min(100%, 980px);
  display: grid;
  gap: 12px;
}

.top-bar {
  align-items: end;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.title-block h1 {
  font-size: clamp(1.6rem, 4vw, 2.2rem);
  line-height: 1;
}

.eyebrow {
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  margin: 0 0 4px;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

.scoreboard {
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(2, minmax(70px, 1fr));
}

.top-links {
  align-items: center;
  align-self: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.developer-link {
  align-items: center;
  align-self: center;
  background: rgba(134, 214, 114, 0.12);
  border: 1px solid rgba(134, 214, 114, 0.55);
  border-radius: 999px;
  color: var(--accent);
  display: inline-flex;
  font-size: 0.9rem;
  font-weight: 800;
  justify-content: center;
  min-height: 40px;
  padding: 0 14px;
  text-decoration: none;
  white-space: nowrap;
}

.developer-link:hover,
.developer-link:focus-visible {
  background: var(--accent);
  color: #102015;
  outline: none;
}

.scoreboard div {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  text-align: right;
}

.scoreboard span {
  color: var(--muted);
  display: block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
}

.scoreboard strong {
  display: block;
  font-size: 1.35rem;
}

.scoreboard strong.best-flash {
  color: var(--accent-2);
}

.toolbar {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
}

.controls {
  display: grid;
  flex: 1;
  gap: 8px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  min-width: min(100%, 420px);
}

.difficulty {
  align-items: center;
  color: var(--muted);
  display: inline-flex;
  font-size: 0.85rem;
  font-weight: 700;
  gap: 8px;
  text-transform: uppercase;
}

.difficulty select {
  text-transform: none;
}

.difficulty select:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.difficulty-label {
  cursor: pointer;
}

/* Small round (i) button that opens the difficulty tooltip (tap on phones). */
.info-btn {
  align-items: center;
  border-radius: 999px;
  color: var(--accent);
  display: inline-flex;
  flex: none;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 0.95rem;
  font-style: italic;
  font-weight: 800;
  height: 30px;
  justify-content: center;
  min-height: 30px;
  padding: 0;
  text-transform: none;
  width: 30px;
}

.info-btn[aria-expanded="true"] {
  background: rgba(134, 214, 114, 0.18);
  border-color: var(--accent);
}

/* Positioned (fixed) by game.js above the dropdown and clamped to the viewport. */
.difficulty-tip {
  background: #161c21;
  border: 1px solid var(--accent);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  left: 0;
  letter-spacing: 0;
  line-height: 1.3;
  max-width: calc(100vw - 16px);
  padding: 8px 10px;
  pointer-events: auto;
  position: fixed;
  text-transform: none;
  top: 0;
  width: max-content;
  z-index: 50;
}

.difficulty-tip[hidden] {
  display: none;
}

.difficulty-tip::after {
  border: 7px solid transparent;
  border-top-color: var(--accent);
  content: "";
  left: var(--arrow-x, 50%);
  position: absolute;
  top: 100%;
  transform: translateX(-50%);
}

.difficulty-tip.is-below::after {
  border-bottom-color: var(--accent);
  border-top-color: transparent;
  bottom: 100%;
  top: auto;
}

.difficulty-tip .tip-title {
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  margin: 0 0 5px;
  text-transform: uppercase;
}

.difficulty-tip ul {
  display: grid;
  gap: 3px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.difficulty-tip li {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 3px 6px;
}

.difficulty-tip li strong {
  color: var(--accent);
}

.difficulty-tip li.is-selected {
  background: rgba(134, 214, 114, 0.14);
  border-color: rgba(134, 214, 114, 0.55);
}

.difficulty-tip li.is-selected strong {
  color: var(--accent-2);
}

.help {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

.help kbd {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--text);
  display: inline-block;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 1px 6px;
}

/* Width/height are set in px by layoutArena() in game.js. */
.canvas-wrap {
  background: #0c1013;
  border-radius: 8px;
  box-shadow: 0 0 0 1px var(--line), 0 18px 48px rgba(0, 0, 0, 0.35);
  flex: none;
  height: 320px;
  overflow: hidden;
  position: relative;
  width: 320px;
}

canvas {
  display: block;
  height: 100%;
  width: 100%;
}

canvas:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.overlay {
  align-items: center;
  background: rgba(12, 16, 19, 0.78);
  display: flex;
  flex-direction: column;
  gap: 10px;
  inset: 0;
  justify-content: center;
  padding: 20px;
  pointer-events: none;
  position: absolute;
  text-align: center;
}

.overlay.hidden {
  display: none;
}

.overlay h2 {
  font-size: clamp(1.5rem, 4vw, 2rem);
}

.overlay p {
  color: var(--muted);
  max-width: 28ch;
}

.overlay .new-best {
  color: var(--accent-2);
  font-weight: 800;
}

.overlay button {
  background: var(--accent);
  color: #102015;
  pointer-events: auto;
}

#start {
  background: var(--accent);
  color: #102015;
}

#end-game {
  color: var(--accent-2);
}

#mute[aria-pressed="true"] {
  color: var(--muted);
}

/* Settings: gear icon + label. Opens the display settings dialog. */
.settings-btn {
  align-items: center;
  display: inline-flex;
  -webkit-tap-highlight-color: transparent;
  gap: 7px;
  justify-content: center;
}

.fullscreen-btn {
  align-items: center;
  display: inline-flex;
  -webkit-tap-highlight-color: transparent;
  gap: 7px;
  justify-content: center;
}

.fullscreen-btn svg,
.fs-icon-btn svg {
  fill: none;
  flex: none;
  height: 1.15em;
  pointer-events: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
  width: 1.15em;
}

.settings-btn svg {
  fill: none;
  flex: none;
  height: 1.15em;
  pointer-events: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
  width: 1.15em;
}

.settings-btn[aria-expanded="true"] {
  border-color: var(--accent);
}

/* ---------- Settings dialog ---------- */
.modal-backdrop {
  align-items: center;
  background: rgba(4, 7, 9, 0.72);
  display: flex;
  inset: 0;
  justify-content: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  position: fixed;
  z-index: 100;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55);
  display: grid;
  gap: 14px;
  max-height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 18px;
  width: min(440px, 100%);
}

.modal h2 {
  font-size: 1.25rem;
  margin: 0;
}

.modal-desc {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.4;
  margin: 0;
}

.switch-list {
  display: grid;
  gap: 10px;
}

.switch-row {
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  display: flex;
  gap: 14px;
  justify-content: space-between;
  padding: 10px 12px;
}

.switch-row label {
  cursor: pointer;
  display: grid;
  gap: 3px;
}

.switch-title {
  font-weight: 700;
}

.switch-hint {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.3;
}

/* Toggle switch: a native checkbox (role="switch") restyled. */
input.switch {
  appearance: none;
  -webkit-appearance: none;
  background: #3a444d;
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  flex: none;
  height: 28px;
  margin: 0;
  position: relative;
  transition: background-color 0.15s ease;
  width: 48px;
}

input.switch::before {
  background: #dfe6ea;
  border-radius: 50%;
  content: "";
  height: 20px;
  left: 3px;
  position: absolute;
  top: 3px;
  transition: transform 0.15s ease;
  width: 20px;
}

input.switch:checked {
  background: var(--accent);
  border-color: var(--accent);
}

input.switch:checked::before {
  background: #102015;
  transform: translateX(20px);
}

input.switch:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.choice-group {
  border: 1px solid var(--line);
  border-radius: 10px;
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 8px 12px 12px;
}

.choice-group legend {
  font-weight: 700;
  padding: 0 4px;
}

.choice {
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  gap: 12px;
  padding: 8px 10px;
}

.choice:has(input:checked) {
  border-color: var(--accent);
}

.choice input {
  accent-color: var(--accent);
  flex: none;
  height: 20px;
  margin: 0;
  width: 20px;
}

.choice input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.choice-text {
  display: grid;
  gap: 2px;
}

/* Reset best score (Settings). */
.reset-best {
  display: grid;
  gap: 6px;
}

.reset-row,
.reset-confirm {
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  display: flex;
  gap: 12px;
  justify-content: space-between;
  padding: 10px 12px;
}

.reset-row[hidden],
.reset-confirm[hidden] {
  display: none;
}

.reset-text {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.reset-confirm {
  border-color: rgba(255, 107, 107, 0.6);
  flex-wrap: wrap;
}

.reset-confirm p {
  flex: 1 1 220px;
  font-size: 0.88rem;
  line-height: 1.35;
  margin: 0;
}

.reset-confirm-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.danger-btn {
  background: transparent;
  border: 1px solid rgba(255, 107, 107, 0.7);
  color: #ff8f8f;
  flex: none;
}

.reset-confirm .danger-btn {
  background: #d9534f;
  border-color: #d9534f;
  color: #fff;
}

.danger-btn:disabled {
  border-color: var(--line);
  color: var(--muted);
}

.reset-result {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
  margin: 0;
}

.reset-result:empty {
  display: none;
}

.reset-result[data-tone="ok"] {
  color: var(--accent);
}

.reset-result[data-tone="error"] {
  color: #ff8f8f;
}

.modal-actions {
  background: var(--panel);
  bottom: -18px;
  display: flex;
  justify-content: flex-end;
  /* Close stays reachable even if a short screen makes the dialog scroll. */
  margin: 0 -18px -18px;
  padding: 0 18px 18px;
  position: sticky;
}

/* Short screens (e.g. 360x740 phones): tighter spacing so the whole dialog,
   including the Reset best score confirmation, fits without scrolling. */
@media (max-height: 780px) {
  .modal {
    gap: 10px;
    padding: 14px;
  }

  .modal-actions {
    bottom: -14px;
    margin: 0 -14px -14px;
    padding: 0 14px 14px;
  }

  .modal-desc {
    font-size: 0.82rem;
  }

  .switch-list {
    gap: 8px;
  }

  .switch-row,
  .reset-row,
  .reset-confirm {
    padding: 8px 10px;
  }

  .choice-group {
    gap: 6px;
    padding: 6px 10px 10px;
  }

  .choice {
    padding: 6px 10px;
  }

  .switch-hint {
    font-size: 0.78rem;
  }

  .modal button {
    min-height: 40px;
  }
}

#settings-close {
  background: var(--accent);
  color: #102015;
  min-width: 104px;
}

html.modal-open,
html.modal-open body {
  overflow: hidden;
}

.stage {
  display: grid;
  gap: 10px;
  grid-template-areas:
    "arena arena"
    "left right";
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  min-width: 0;
}

.arena-slot {
  display: flex;
  grid-area: arena;
  justify-content: center;
  min-width: 0;
}

.turn-btn {
  align-items: center;
  display: flex;
  font-size: 1.15rem;
  gap: 10px;
  justify-content: center;
  min-height: 56px;
  padding: 0 12px;
  /* Instant, gesture-free taps: no scroll/zoom, selection, callout or highlight. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.turn-btn svg {
  fill: none;
  flex: none;
  height: 1.6em;
  pointer-events: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.4;
  width: 1.6em;
}

.turn-btn .turn-label {
  pointer-events: none;
}

/* Buttons line up under the arena rather than spanning a wide column. */
.turn-left {
  grid-area: left;
  justify-self: end;
  width: min(100%, calc(var(--arena-size, 640px) / 2 - 5px));
}

.turn-right {
  grid-area: right;
  justify-self: start;
  width: min(100%, calc(var(--arena-size, 640px) / 2 - 5px));
}

/* No sticky hover/focus ring after a tap; only the pressed state lights up. */
.turn-btn:hover,
.turn-btn:focus,
.turn-btn:focus-visible {
  border-color: var(--line);
}

.turn-btn:active,
.turn-btn.is-pressed {
  background: var(--accent);
  border-color: var(--accent);
  color: #102015;
}

.sr-only {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}


.player-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 8px;
  padding: 10px 14px;
}

.player-row {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.player-name {
  display: grid;
  flex: 0 1 320px;
  gap: 6px;
  min-width: 0;
}

.player-name span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
}

.player-name input {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 16px; /* >= 16px stops iOS zooming in on focus */
  min-height: 44px;
  padding: 0 12px;
  width: 100%;
}

.player-name input:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.player-name input.invalid {
  border-color: var(--danger);
}

.name-hint {
  color: var(--muted);
  font-size: 0.85rem;
}

.name-hint.error {
  color: var(--danger);
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#save-settings {
  background: var(--accent);
  color: #102015;
}

button:disabled,
#overlay-start:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.title-snake {
  font-size: 0.85em;
}

.play-layout {
  align-items: stretch;
  display: grid;
  gap: 14px;
  grid-template-columns: minmax(0, 1fr) minmax(220px, 280px);
}


.arcade-board {
  background:
    linear-gradient(160deg, rgba(134, 214, 114, 0.08), transparent 42%),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: inset 0 0 0 1px rgba(134, 214, 114, 0.08);
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  overflow: hidden;
  padding: 14px;
}

.board-header h2 {
  color: var(--accent);
  font-size: 1.05rem;
  letter-spacing: 0.02em;
}

.board-meta {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  margin-top: 4px;
  text-transform: uppercase;
}

.board-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 6px;
  list-style: none;
  margin: 0;
  max-height: min(48dvh, 460px);
  overflow: auto;
  padding: 0;
}

.board-row {
  align-items: center;
  background: rgba(32, 39, 45, 0.85);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 8px;
  grid-template-columns: 2.2rem minmax(0, 1fr) auto auto;
  padding: 8px 10px;
}

.board-row.is-you {
  border-color: rgba(134, 214, 114, 0.65);
  box-shadow: 0 0 0 1px rgba(134, 214, 114, 0.25);
}

.board-rank {
  font-size: 0.85rem;
  font-weight: 800;
  text-align: center;
}

.board-name {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-diff {
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.board-diff:not(:empty) {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 6px;
}

.board-diff[data-difficulty="hard"] {
  border-color: rgba(242, 201, 76, 0.55);
  color: var(--accent-2);
}

.board-best {
  color: var(--accent-2);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
}

.board-status {
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  font-size: 0.8rem;
  gap: 6px 10px;
  line-height: 1.35;
  padding: 6px 10px;
}

.board-status[hidden] {
  display: none;
}

.board-status[data-tone="ok"] {
  border-color: rgba(134, 214, 114, 0.45);
  color: var(--accent);
}

.board-status[data-tone="error"] {
  border-color: rgba(255, 107, 107, 0.6);
  color: #ff8f8f;
}

.board-retry {
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 999px;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  min-height: 28px;
  padding: 2px 12px;
}

.board-empty {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
  padding: 12px 4px;
}

.board-foot {
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.35;
}

.player-chip {
  color: var(--accent);
  font-size: 0.95rem;
  font-weight: 800;
}

@media (min-width: 861px) {
  /* Board takes the arena column's height instead of stretching the page. */
  .arcade-board {
    contain: size;
  }

  .board-list {
    max-height: none;
    min-height: 0;
  }
}

/* ---------- Phones / small tablets ---------- */
@media (max-width: 860px) {
  .shell {
    --pad: 8px;
  }

  .game-panel {
    gap: 8px;
  }

  .play-layout {
    gap: 12px;
    grid-template-columns: 1fr;
  }

  .arcade-board {
    order: 2;
  }

  .board-list {
    max-height: 220px;
  }

  .top-bar {
    align-items: center;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px 10px;
  }

  .eyebrow,
  .help {
    display: none;
  }

  .title-block h1 {
    font-size: 1.3rem;
  }

  .top-links {
    gap: 6px;
  }

  .developer-link {
    align-self: center;
    font-size: 0.8rem;
    min-height: 32px;
    padding: 0 12px;
  }

  .scoreboard {
    flex: 1 0 100%;
    gap: 6px;
    grid-template-columns: repeat(2, 1fr);
  }

  .scoreboard div {
    align-items: baseline;
    display: flex;
    justify-content: space-between;
    padding: 4px 10px;
    text-align: left;
  }

  .scoreboard strong {
    font-size: 1.1rem;
  }

  .player-panel {
    gap: 4px;
    padding: 8px 10px;
  }

  .player-name {
    flex: 1 1 160px;
  }

  .player-name span {
    display: none;
  }

  .name-hint {
    font-size: 0.78rem;
  }

  /* Two rows on phones: Difficulty and Settings, then Start / Pause /
     End game / Sound (so the difficulty tooltip, which opens above the
     dropdown, never covers Start). In play only Pause is left: one row. */
  .toolbar {
    flex-wrap: wrap;
    gap: 6px;
  }

  .toolbar .difficulty {
    order: -2;
  }

  .controls {
    flex: 1 0 100%;
    gap: 6px;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    min-width: 0;
  }

  .settings-btn {
    font-size: 0.85rem;
    margin-left: auto;
    order: -1;
    padding: 0 12px;
  }

  .fullscreen-btn {
    font-size: 0.85rem;
    order: -1;
    padding: 0 12px;
  }

  .controls button,
  .difficulty select {
    font-size: 0.85rem;
    padding: 0 6px;
  }

  .difficulty {
    gap: 6px;
  }

  .difficulty-label {
    display: none;
  }

  .turn-btn {
    font-size: 1.25rem;
    min-height: clamp(64px, 11dvh, 110px);
  }

  /* Before Start / paused the turn buttons do nothing, so they give up a
     little height to the extra toolbar row (keeps the 24x24 board). In play
     they grow into the spare height again (min 64px). */
  html:not(.is-playing) .turn-btn {
    min-height: clamp(52px, 8dvh, 100px);
  }

  /* While a run is live, collapse the setup UI so the arena + buttons dominate.
     Pause brings everything back. */
  .is-playing .player-panel,
  .is-playing .arcade-board,
  .is-playing .top-links,
  .is-playing #start,
  .is-playing #end-game,
  .is-playing #mute,
  .is-playing .settings-btn,
  .is-playing .fullscreen-btn,
  .is-playing .difficulty {
    display: none;
  }

  .is-playing .controls {
    grid-template-columns: 1fr;
  }

  /* In play the panel fills the screen exactly; the turn buttons sit at the
     bottom (thumb reach) and grow into any spare height. */
  .is-playing .game-panel {
    display: flex;
    flex-direction: column;
    height: calc(100dvh - max(var(--pad), env(safe-area-inset-top)) - max(var(--pad), env(safe-area-inset-bottom)));
  }

  .is-playing .play-layout {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-height: 0;
  }

  .is-playing .stage {
    flex: 1;
    grid-template-rows: auto 1fr;
    min-height: 0;
  }

  .is-playing .turn-btn {
    align-self: end;
    height: clamp(64px, 100%, 160px);
  }

  html.is-playing,
  html.is-playing body {
    overflow: hidden;
  }

  .is-playing .stage {
    touch-action: none;
  }
}

/* Narrow phones: Difficulty, Settings and Full screen still share one row. */
@media (max-width: 389px) {
  .settings-btn,
  .fullscreen-btn {
    gap: 5px;
    padding: 0 8px;
  }
}

/* Phone held sideways: buttons flank the arena. */
@media (orientation: landscape) and (max-height: 540px) {
  .stage {
    align-items: stretch;
    grid-template-areas: "left arena right";
    grid-template-columns: minmax(88px, 1fr) minmax(0, 3fr) minmax(88px, 1fr);
  }

  .turn-btn,
  .is-playing .turn-btn {
    align-self: stretch;
    flex-direction: column;
    height: auto;
    justify-self: stretch;
    min-height: 0;
    width: auto;
  }

  .is-playing .title-block {
    display: none;
  }
}

/* ---------- Full screen ----------
   Only the current score, the board and the Left / Right buttons (plus a
   small Pause / Exit pair). html.is-fullscreen is set both for the real
   Fullscreen API (the stage is the full-screen element) and for the CSS
   "immersive" fallback, where the stage is pinned over the whole viewport
   and everything else on the page is hidden (not just covered). */
.fs-score,
.fs-controls {
  display: none;
}

html.is-fullscreen,
html.is-fullscreen body {
  overflow: hidden;
}

html.is-fullscreen .shell {
  visibility: hidden;
}

html.is-fullscreen .stage,
.stage:fullscreen {
  background: var(--bg);
}

html.is-fullscreen .stage {
  align-items: stretch;
  box-sizing: border-box;
  gap: 8px;
  grid-template-areas:
    "score ctl"
    "arena arena"
    "left right";
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) clamp(72px, 20dvh, 200px);
  height: 100%;
  height: 100dvh;
  inset: 0;
  margin: 0;
  overscroll-behavior: none;
  padding:
    max(8px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
  position: fixed;
  touch-action: none;
  visibility: visible;
  width: 100%;
  z-index: 40;
}

html.is-fullscreen .fs-score {
  align-items: baseline;
  align-self: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: inline-flex;
  gap: 8px;
  grid-area: score;
  justify-self: start;
  padding: 4px 12px;
}

html.is-fullscreen .fs-score span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

html.is-fullscreen .fs-score strong {
  font-size: 1.35rem;
  font-variant-numeric: tabular-nums;
}

html.is-fullscreen .fs-controls {
  align-items: center;
  align-self: start;
  display: flex;
  gap: 6px;
  grid-area: ctl;
  justify-content: flex-end;
}

.fs-icon-btn {
  align-items: center;
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
  display: inline-flex;
  height: 44px;
  justify-content: center;
  min-height: 44px;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  width: 44px;
}

.fs-icon-btn[hidden] {
  display: none;
}

.fs-icon-btn:hover,
.fs-icon-btn:focus-visible {
  color: var(--text, #fff);
}

.fs-icon-btn .icon-play,
.fs-icon-btn.is-paused .icon-pause {
  display: none;
}

.fs-icon-btn.is-paused .icon-play {
  display: block;
}

/* Portrait: the board is width-limited (24 rows, square cells), so the spare
   height goes above it; the board sits right over the turn buttons. */
html.is-fullscreen .arena-slot {
  align-items: flex-end;
  min-height: 0;
  overflow: hidden;
}

html.is-fullscreen .canvas-wrap {
  box-shadow: 0 0 0 1px var(--line);
}

html.is-fullscreen .turn-btn {
  align-self: stretch;
  height: auto;
  justify-self: stretch;
  min-height: 0;
  width: auto;
}

/* Sideways (and desktop): score and controls on top of the side columns,
   the buttons fill the rest of each side, the board takes the middle. */
@media (orientation: landscape) {
  html.is-fullscreen .stage {
    grid-template-areas:
      "score arena ctl"
      "left arena right";
    grid-template-columns: minmax(96px, 1fr) minmax(0, 4fr) minmax(96px, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
  }

  html.is-fullscreen .arena-slot {
    align-items: center;
  }

  html.is-fullscreen .fs-score {
    align-items: center;
    align-self: start;
    flex-direction: column;
    gap: 0;
    justify-self: stretch;
  }

  html.is-fullscreen .turn-btn {
    flex-direction: column;
  }
}
