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

:root {
  color-scheme: light dark;
  accent-color: var(--focus);
  --bg-1: light-dark(#f7f4ed, #2c2c35);
  --bg-2: light-dark(#ddd5c6, #131318);
  --text: light-dark(#232119, #e8e3da);
  --muted: light-dark(#5f5a4e, #cfc8bb);
  --surface: light-dark(rgb(0 0 0 / 0.05), rgb(255 255 255 / 0.07));
  --surface-hover: light-dark(rgb(0 0 0 / 0.1), rgb(255 255 255 / 0.14));
  --border: light-dark(rgb(0 0 0 / 0.16), rgb(255 255 255 / 0.14));
  --focus: light-dark(#2563eb, #7fb2ff);
  --overlay: light-dark(rgb(250 248 243 / 0.8), rgb(24 24 30 / 0.72));
  --dialog-bg: light-dark(#fbf8f1, #1b1b22);
  --danger: light-dark(#b3261e, #ff8a80);
}

/* The inline head script writes the saved override into the meta tag.
   No attribute means "light dark", so the system preference still wins. */
:root:has(> head > meta[name="color-scheme"][content="dark"]) {
  color-scheme: dark;
}

:root:has(> head > meta[name="color-scheme"][content="light"]) {
  color-scheme: light;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: flex;
  padding: 12px;
  background: radial-gradient(
    circle at 30% 15%,
    var(--bg-1),
    var(--bg-2) 70%
  );
  color: var(--text);
  font-family: ui-sans-serif, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

#stage {
  position: relative;
  flex: 1;
  width: 100%;
  min-height: 0;
}

/* Frame stays a fixed square and scrolls when the board is larger. The
   canvas is only as big as this frame; the spacer behind it is a capped
   virtual range, so even a 94,000,000x94,000,000 board stays under browser
   scroll limits and costs the same bitmap as a 20x20 one. */
#board-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: auto;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

/* Zero-size pin holding the canvas. Sticky in both axes, so the canvas stays
   glued to the frame while the spacer scrolls underneath it. The browser
   moves it, not a scroll handler, so a fast scroll never exposes the
   background before the next repaint. */
#board-pin {
  position: sticky;
  top: 0;
  left: 0;
  flex: 0 0 auto;
  z-index: 1;
  width: 0;
  height: 0;
}

#board-spacer {
  flex: 0 0 auto;
  margin: auto;
  pointer-events: none;
}

#board-wrap::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

#board-wrap::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}

#board-wrap::-webkit-scrollbar-corner {
  background: transparent;
}

/* Canvas gets an explicit pixel size from JS: exactly the visible frame. */
#board {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  border-radius: 10px;
  background: linear-gradient(160deg, #e2c288, #d3a967 55%, #c99f5a);
  box-shadow:
    0 18px 44px rgb(0 0 0 / 0.35),
    inset 0 0 0 1px rgb(0 0 0 / 0.18);
  cursor: crosshair;
  touch-action: pan-x pan-y pinch-zoom;
  user-select: none;
}

/* Cursor coordinate: also the jump-to fields. */
#coord {
  position: absolute;
  left: 20px;
  bottom: 20px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--overlay);
  color: var(--muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

#coord label {
  color: var(--muted);
}

#coord input {
  /* Fallback width: fits the widest coordinate the 94,000,000 cap can
     produce (-47,000,000, nine characters). startGame narrows it per board. */
  width: 9.5ch;
  padding: 0;
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: center;
  appearance: textfield;
  -moz-appearance: textfield;
}

#coord input::-webkit-outer-spin-button,
#coord input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#coord input:focus-visible {
  outline: 1px solid var(--focus);
  outline-offset: 1px;
}

.coord-sep {
  color: var(--muted);
}

/* HUD: two fixed-width panels in one row. Each panel collapses to its
   header plus the divider line and unfolds on hover (or tap). */
#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 2;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.panel {
  overflow: hidden;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--overlay);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.22);
  pointer-events: auto;
}

/* Collapsed is literally the header: the box ends at the divider line.
   Hover (or tap) brings the body back. No partial heights. */
.panel-inner {
  display: none;
}

.panel.open .panel-inner {
  display: flex;
}

/* Typing in a field pins the panel open too. */
.panel.keep-open .panel-inner {
  display: flex;
}

@media (hover: hover) {
  .panel:hover .panel-inner {
    display: flex;
  }
}

#controls {
  width: min(320px, 100%);
}

#scores {
  width: min(240px, 100%);
}

/* Move history sits in the bottom-right corner, out of the HUD row. */
#history {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 3;
  width: min(270px, calc(100% - 24px));
}

#history .panel-inner {
  flex-direction: column;
  max-height: min(45vh, 380px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.history-list li {
  display: grid;
  grid-template-columns: 9px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 3px 6px;
  border-radius: 6px;
  font-size: 12.5px;
}

/* Newest row is on top; tint it like the score leader. */
.history-list li:first-child {
  background: var(--surface);
}

.history-name {
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-coord {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* The truncation notice spans the whole row instead of the 9px dot column. */
.history-more {
  grid-column: 1 / -1;
  color: var(--muted);
}

/* The divider stays visible when the panel is collapsed: the box folds
   down to exactly this line. */
.tab {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 10px;
  font: inherit;
  font-size: 12.5px;
  text-align: left;
  white-space: nowrap;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.panel-inner {
  padding: 6px 8px 8px;
}

#controls .panel-inner {
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
}

#scores .panel-inner {
  flex-direction: column;
  gap: 2px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.settings {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}

.actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

#status {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-right: 2px;
  font-size: 13px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

#leader-line {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Leaderboard: rows stack and grow downward, never sideways. */
.scores-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.scores-list li {
  display: grid;
  grid-template-columns: 9px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: 6px;
  font-size: 12.5px;
}

.scores-list li.top {
  background: var(--surface);
}

.score-dot {
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--dot, var(--muted));
  box-shadow: 0 0 0 1px var(--border);
}

/* Cross-shaped players get an X marker in the leaderboard too. */
.score-dot.cross {
  position: relative;
  border-radius: 0;
  background: none;
  box-shadow: none;
}

.score-dot.cross::before,
.score-dot.cross::after {
  content: "";
  position: absolute;
  top: 50%;
  left: -12%;
  width: 124%;
  height: 2.4px;
  margin-top: -1.2px;
  border-radius: 2px;
  background: var(--dot, var(--muted));
}

.score-dot.cross::before {
  transform: rotate(45deg);
}

.score-dot.cross::after {
  transform: rotate(-45deg);
}

.score-name {
  color: var(--muted);
}

.score-points {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

#status::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--dot, #888);
  box-shadow: 0 0 0 1px var(--border);
}

/* Cross-shaped players get an X marker in the status bar too. */
#status.cross::before {
  border-radius: 0;
  background:
    linear-gradient(
      45deg,
      transparent 42%,
      var(--dot) 42% 58%,
      transparent 58%
    ),
    linear-gradient(
      -45deg,
      transparent 42%,
      var(--dot) 42% 58%,
      transparent 58%
    );
  box-shadow: none;
}

#board:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

select,
button {
  font: inherit;
  font-size: 12.5px;
  padding: 6px 9px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 120ms ease, opacity 120ms ease;
}

button {
  padding: 6px 12px;
}

/* Stepper: minus / number / plus. Native spinners are hidden so no arrow
   ever sits on top of the digits. */
.stepper {
  display: inline-flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  overflow: hidden;
}

.stepper button {
  padding: 0 9px;
  border: 0;
  border-radius: 0;
  background: transparent;
  font-size: 14px;
  line-height: 1;
}

.stepper input {
  /* Fits the largest board: 94,000,000 is eight digits. */
  width: 9ch;
  padding: 6px 2px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  text-align: center;
  cursor: text;
}

.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.stepper input[type="number"] {
  appearance: textfield;
  -moz-appearance: textfield;
}

.stepper:focus-within {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

select:hover,
.stepper button:hover:not(:disabled),
button:hover:not(:disabled) {
  background: var(--surface-hover);
}

button:disabled {
  opacity: 0.35;
  cursor: default;
}

select:focus-visible,
button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

@media (max-width: 560px) {
  /* Narrow screens: stack the two panels, still full width and always open. */
  #hud {
    flex-direction: column;
  }

  #controls,
  #scores {
    width: 100%;
  }

  .actions {
    width: 100%;
    justify-content: flex-end;
  }
}

/* On narrow phones the coordinate pill and the history header share the
   bottom edge. Lift the panel above the pill; it still opens upward. */
@media (max-width: 420px) {
  #history {
    bottom: 56px;
  }
}

/* ---- preferences dialog -------------------------------------------- */

dialog {
  width: min(460px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 16px 18px 18px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--dialog-bg);
  color: var(--text);
  box-shadow: 0 24px 64px rgb(0 0 0 / 0.45);
}

/* Only style the open state: a bare display would defeat the closed state. */
dialog[open] {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.45);
  overflow: hidden;
  overscroll-behavior: contain;
}

.prefs-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.prefs-head h1 {
  margin: 0;
  font-size: 15px;
  letter-spacing: 0.02em;
}

.prefs-close-form {
  margin: 0;
}

.prefs-close {
  padding: 0 8px;
  font-size: 18px;
  line-height: 1.4;
}

.prefs-group {
  margin: 0;
  padding: 8px 12px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.prefs-group legend {
  padding: 0 4px;
  color: var(--muted);
  font-size: 11.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.theme-options {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  font-size: 12.5px;
}

.theme-options label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.theme-note,
.prefs-hint,
.prefs-error {
  margin: 6px 0 0;
  font-size: 11.5px;
  color: var(--muted);
}

.prefs-error {
  min-height: 1em;
  color: var(--danger);
}

.prefs-rows {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 6px 12px;
  font-size: 12.5px;
}

.prefs-rows span {
  color: var(--muted);
}

.binding {
  min-width: 96px;
  padding: 4px 8px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.binding.capturing {
  color: var(--focus);
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.prefs-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.prefs-actions form {
  margin: 0;
}
