/*
 * KingTrap styles.
 *
 * The board is a CSS grid of 64 buttons rather than one SVG: hit-testing,
 * focus rings and keyboard access all come free, and the pieces are inline SVG
 * either way, so nothing is lost in crispness. Board appearance is driven
 * entirely by custom properties on [data-board-theme], so adding a theme is a
 * block of variables and nothing else.
 */

:root {
  --bg: #14171c;
  --panel: #1b1f26;
  --panel-2: #222731;
  --line: #2e3541;
  --text: #e6e9ee;
  --muted: #97a1b0;
  --accent: #5b9dd9;
  --accent-warm: #d9a05b;
  --danger: #d9534f;
  --good: #5cb85c;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, monospace;
}

/* ── Board themes ─────────────────────────────────────────────────────── */

[data-board-theme="wood"] {
  --light-sq: #e8d3ac;
  --dark-sq: #b07c4c;
  --board-edge: #6b4423;
  --coord: #6b4423;
  --coord-alt: #f0e0c4;
  --piece-fill: #fffdf7;
  --piece-line: #2b2118;
  --piece-fill-dark: #35291f;
  --piece-line-dark: #f3e6d2;
}

[data-board-theme="marble"] {
  --light-sq: #e9e9e4;
  --dark-sq: #8f9aa3;
  --board-edge: #5d666e;
  --coord: #55606a;
  --coord-alt: #f2f4f6;
  --piece-fill: #ffffff;
  --piece-line: #2b3138;
  --piece-fill-dark: #2f353c;
  --piece-line-dark: #eef1f4;
}

[data-board-theme="tournament"] {
  --light-sq: #eeeed2;
  --dark-sq: #769656;
  --board-edge: #4a6135;
  --coord: #4a6135;
  --coord-alt: #eeeed2;
  --piece-fill: #ffffff;
  --piece-line: #262421;
  --piece-fill-dark: #262421;
  --piece-line-dark: #f4f4ec;
}

[data-board-theme="slate"] {
  --light-sq: #6f7885;
  --dark-sq: #414954;
  --board-edge: #2a303a;
  --coord: #2a303a;
  --coord-alt: #cbd3de;
  --piece-fill: #f2f5f9;
  --piece-line: #1d2229;
  --piece-fill-dark: #1d2229;
  --piece-line-dark: #dfe6ef;
}

/* A nod to the Smalltalk V original: phosphor green on black. */
[data-board-theme="crt"] {
  --light-sq: #0d2a10;
  --dark-sq: #061806;
  --board-edge: #103c14;
  --coord: #3ddc5b;
  --coord-alt: #3ddc5b;
  --piece-fill: none;
  --piece-line: #6bff8a;
  --piece-fill-dark: none;
  --piece-line-dark: #2f9b45;
}

/* ── Shell ────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--accent); }

header.top {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

header.top h1 {
  margin: 0;
  font-size: 19px;
  letter-spacing: 0.5px;
  font-weight: 650;
}

header.top h1 span { color: var(--accent-warm); }

header.top nav { margin-left: auto; display: flex; gap: 14px; }
header.top nav a { text-decoration: none; color: var(--muted); font-size: 14px; }
header.top nav a:hover, header.top nav a.active { color: var(--text); }

main {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 360px;
  gap: 22px;
  padding: 22px;
  max-width: 1220px;
  margin: 0 auto;
  align-items: start;
}

@media (max-width: 900px) {
  main { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 16px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 650;
}

/* ── Board ────────────────────────────────────────────────────────────── */

.board-area { display: flex; flex-direction: column; align-items: center; gap: 14px; }

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  /* Rows must be explicit. Left to auto, a row holding pieces takes its height
     from the piece SVG and grows taller than the empty rows, so the board stops
     being a uniform 8x8 and the hint dots turn into ovals. */
  grid-template-rows: repeat(8, 1fr);
  width: min(74vh, 100%);
  aspect-ratio: 1;
  border: 10px solid var(--board-edge);
  border-radius: 6px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 45%);
  overflow: hidden;
  user-select: none;
}

.sq {
  position: relative;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  background: var(--dark-sq);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.12s ease;
}

.sq.light { background: var(--light-sq); }
.sq:disabled { cursor: default; }
.sq:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; z-index: 4; }

/* Rank and file labels, printed into the edge squares like a real board.
   Real elements, not pseudo-content -- see the note in board.js. */
.coord {
  position: absolute;
  font-size: clamp(8px, 1.5vh, 11px);
  font-weight: 700;
  opacity: 0.75;
  pointer-events: none;
  z-index: 3;
}
.coord.file { right: 3px; bottom: 1px; }
.coord.rank { left: 3px; top: 1px; }
.sq.light .coord { color: var(--coord); }
.sq:not(.light) .coord { color: var(--coord-alt); }

.piece-slot {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 2;
}

.piece {
  width: 88%;
  height: 88%;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgb(0 0 0 / 28%));
}

.pc-fill { fill: var(--piece-fill); stroke: var(--piece-line); stroke-width: 1.5; stroke-linejoin: round; }
.pc-line { fill: none; stroke: var(--piece-line); stroke-width: 2.6; }
.pc-line-solid { fill: none; stroke: var(--piece-line); stroke-width: 1.5; }
.pc-outline { fill: none; stroke: var(--piece-line); stroke-width: 2.1; stroke-linejoin: round; }

.piece-black .pc-fill { fill: var(--piece-fill-dark); stroke: var(--piece-line-dark); }
.piece-black .pc-line,
.piece-black .pc-line-solid,
.piece-black .pc-outline { stroke: var(--piece-line-dark); }

[data-board-theme="crt"] .piece { filter: drop-shadow(0 0 3px currentColor); }

/* ── Square overlays ──────────────────────────────────────────────────── */

.sq.last::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgb(255 214 92 / 32%);
  pointer-events: none;
}

.sq.selected::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgb(91 157 217 / 42%);
  pointer-events: none;
}

/* Move hints: a dot on an empty square, a ring around an occupied one. The
   ring case only arises for a slider blocked by nothing -- kept for clarity. */
.sq.hint::after {
  content: "";
  position: absolute;
  /* Centred explicitly. An absolutely-positioned pseudo-element with no
     offsets falls back to its static position, which inside a flex container
     is not reliably the centre. */
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgb(20 24 30 / 34%);
  pointer-events: none;
}
.sq.light.hint::after { background: rgb(20 24 30 / 26%); }
.sq.hint:hover::after { background: rgb(91 157 217 / 70%); width: 38%; height: 38%; }

.sq.check {
  background: radial-gradient(circle, rgb(217 83 79 / 85%) 12%, transparent 72%), var(--dark-sq);
}
.sq.light.check {
  background: radial-gradient(circle, rgb(217 83 79 / 85%) 12%, transparent 72%), var(--light-sq);
}

.sq.trapped {
  background: radial-gradient(circle, rgb(217 83 79 / 95%) 30%, rgb(120 20 18 / 80%) 75%), var(--dark-sq);
  animation: trapped-pulse 1.3s ease-in-out infinite;
}
@keyframes trapped-pulse { 50% { filter: brightness(1.35); } }

/* The forbidden backtrack square. Visible in normal play, not just in debug:
   it is the one rule a chess player has no intuition for, and a king that
   cannot step back needs to be able to see why. */
.sq.backtrack::before {
  content: "";
  position: absolute;
  inset: 8%;
  border: 2.5px dashed rgb(217 83 79 / 85%);
  border-radius: 4px;
  pointer-events: none;
}
.sq.backtrack::after {
  content: "\00d7";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 46%;
  font-weight: 700;
  color: rgb(217 83 79 / 95%);
  pointer-events: none;
  line-height: 1;
}

/* Debug overlay: why a king cannot go somewhere. */
.sq.att-white { box-shadow: inset 0 0 0 3px rgb(240 240 240 / 38%); }
.sq.att-black { box-shadow: inset 0 0 0 3px rgb(20 20 20 / 42%); }
.sq.att-white.att-black { box-shadow: inset 0 0 0 3px rgb(217 83 79 / 55%); }

/* ── Status bar ───────────────────────────────────────────────────────── */

.status {
  display: flex;
  align-items: center;
  gap: 12px;
  width: min(74vh, 100%);
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 48px;
}

.turn-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: #fff;
  flex: none;
}
.turn-dot.black { background: #23282f; }

.status .who { font-weight: 600; }
.status .detail { color: var(--muted); font-size: 13px; margin-left: auto; text-align: right; }
.status.over { border-color: var(--accent-warm); }
.status.over .who { color: var(--accent-warm); }

/* Live count of each king's escape squares -- an approaching trap made legible. */
.mobility {
  display: flex;
  gap: 10px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
}
.mobility b { color: var(--text); font-weight: 600; }
.mobility .low { color: var(--accent-warm); }
.mobility .critical { color: var(--danger); font-weight: 700; }

/* ── Controls ─────────────────────────────────────────────────────────── */

.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }

select, input[type="text"], input[type="number"] {
  width: 100%;
  padding: 7px 9px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  font-family: inherit;
  font-size: 14px;
}

button.btn {
  padding: 8px 14px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}
button.btn:hover { border-color: var(--accent); }
button.btn.primary { background: var(--accent); border-color: var(--accent); color: #0d1116; font-weight: 600; }
button.btn.primary:hover { filter: brightness(1.08); }
button.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 5px 0;
  cursor: pointer;
  font-size: 14px;
}
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }
.toggle .hint-text { color: var(--muted); font-size: 12px; display: block; }

/* ── Move list ────────────────────────────────────────────────────────── */

.moves {
  max-height: 230px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 13px;
}
.moves table { width: 100%; border-collapse: collapse; }
.moves td { padding: 2px 5px; }
.moves td.num { color: var(--muted); width: 30px; text-align: right; }
.moves td.mv { cursor: default; }
.moves tr:last-child td.mv:last-child, .moves tr:last-child td.mv { color: var(--accent-warm); }
.moves .empty { color: var(--muted); font-family: var(--font); }

/* ── Analysis ─────────────────────────────────────────────────────────── */

.evalbar {
  height: 10px;
  border-radius: 5px;
  background: #23282f;
  overflow: hidden;
  margin-bottom: 10px;
  border: 1px solid var(--line);
}
.evalbar > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--good)); transition: width 0.3s ease; }

.features { font-size: 12.5px; font-family: var(--mono); }
.features div { display: flex; justify-content: space-between; gap: 8px; padding: 1.5px 0; }
.features .name { color: var(--muted); }
.features .pos { color: var(--good); }
.features .neg { color: var(--danger); }
.pv { font-family: var(--mono); font-size: 12.5px; color: var(--muted); margin-top: 8px; word-spacing: 4px; }

.error {
  background: rgb(217 83 79 / 14%);
  border: 1px solid rgb(217 83 79 / 45%);
  color: #ffb3b1;
  padding: 8px 11px;
  border-radius: 7px;
  font-size: 13px;
  margin-bottom: 10px;
}

.muted { color: var(--muted); font-size: 13px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 11.5px; color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 5px; }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
