/* Theme system: every color the UI (and the SVG board) uses comes from the
   custom properties below, keyed off data-theme on <html>. "system" is
   resolved to light/dark before first paint by the inline script in the
   page-head template block, so no flash of the wrong theme. */

:root,
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #191b1f;
  --muted: #667085;
  --border: #e0e4e9;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-text: #ffffff;
  --danger: #b42318;
  --win: #067647;
  --loss: #b42318;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.06);

  --board-light: #eeeed2;
  --board-dark: #769656;
  --board-highlight: #f6c453;
  --board-lastmove: rgba(246, 196, 83, 0.45);
  --piece-white: #ffffff;
  --piece-white-outline: #3d3d3d;
  --piece-black: #1f1f1f;
  --piece-black-outline: rgba(255, 255, 255, 0.55);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16181d;
  --surface: #1f2229;
  --surface-2: #262a33;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --border: #333845;
  --accent: #818cf8;
  --accent-hover: #a5b0ff;
  --accent-text: #14161b;
  --danger: #f97066;
  --win: #4ade80;
  --loss: #f97066;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);

  --board-light: #f0d9b5;
  --board-dark: #b58863;
  --board-highlight: #ffd75e;
  --board-lastmove: rgba(255, 215, 94, 0.4);
  --piece-white: #f8f8f8;
  --piece-white-outline: #2b2b2b;
  --piece-black: #1c1c1c;
  --piece-black-outline: rgba(255, 255, 255, 0.6);
}

:root[data-theme="midnight"] {
  color-scheme: dark;
  --bg: #0b0e14;
  --surface: #12161f;
  --surface-2: #1a1f2b;
  --text: #dbe4f0;
  --muted: #8494ab;
  --border: #232b3b;
  --accent: #38bdf8;
  --accent-hover: #7dd3fc;
  --accent-text: #06121a;
  --danger: #fb7185;
  --win: #34d399;
  --loss: #fb7185;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.5);

  --board-light: #8fa3bd;
  --board-dark: #45556e;
  --board-highlight: #38bdf8;
  --board-lastmove: rgba(56, 189, 248, 0.32);
  --piece-white: #f2f6fb;
  --piece-white-outline: #1c2430;
  --piece-black: #14181f;
  --piece-black-outline: rgba(226, 236, 248, 0.65);
}

:root[data-theme="forest"] {
  color-scheme: dark;
  --bg: #101710;
  --surface: #182118;
  --surface-2: #1f2b1f;
  --text: #e2ecdf;
  --muted: #93a58f;
  --border: #2b3a2a;
  --accent: #6ee7a0;
  --accent-hover: #9af0bd;
  --accent-text: #0b160d;
  --danger: #f4a26a;
  --win: #6ee7a0;
  --loss: #f4a26a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.45), 0 6px 20px rgba(0, 0, 0, 0.4);

  --board-light: #d9e4c8;
  --board-dark: #5b7c4f;
  --board-highlight: #f2d15c;
  --board-lastmove: rgba(242, 209, 92, 0.42);
  --piece-white: #fbfcf8;
  --piece-white-outline: #26301f;
  --piece-black: #1a2016;
  --piece-black-outline: rgba(240, 248, 232, 0.6);
}

:root[data-theme="ocean"] {
  color-scheme: dark;
  --bg: #0c1524;
  --surface: #122036;
  --surface-2: #182a45;
  --text: #dde9f7;
  --muted: #85a0c0;
  --border: #223a5c;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-text: #081221;
  --danger: #fda4af;
  --win: #5eead4;
  --loss: #fda4af;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.45), 0 8px 24px rgba(2, 8, 20, 0.5);

  --board-light: #cfdde9;
  --board-dark: #6688aa;
  --board-highlight: #ffd75e;
  --board-lastmove: rgba(255, 215, 94, 0.4);
  --piece-white: #f5f9fd;
  --piece-white-outline: #1d2b3d;
  --piece-black: #131c28;
  --piece-black-outline: rgba(222, 236, 250, 0.6);
}

/* --- board styles --- */

/* User-selectable board palette, independent of the UI theme, via data-board
   on <html> (persisted as chess_board in localStorage). "Auto" = attribute
   absent, keeping the theme's own board colors above. These blocks sit after
   the theme blocks so the equal-specificity override wins the cascade. */

:root[data-board="green"] {
  --board-light: #eeeed2;
  --board-dark: #769656;
}

:root[data-board="walnut"] {
  --board-light: #f0d9b5;
  --board-dark: #b58863;
}

:root[data-board="slate"] {
  --board-light: #8fa3bd;
  --board-dark: #45556e;
}

:root[data-board="forest"] {
  --board-light: #d9e4c8;
  --board-dark: #5b7c4f;
}

:root[data-board="ice"] {
  --board-light: #cfdde9;
  --board-dark: #6688aa;
}

:root[data-board="purple"] {
  --board-light: #e6e0ec;
  --board-dark: #9c8ab5;
}

/* --- base --- */

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.wrap {
  max-width: 40rem;
  margin: 0 auto;
  padding: 1.25rem 1rem 3rem;
}

h1 { font-size: 1.45rem; margin: 0 0 0.35rem; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; margin: 0 0 0.5rem; }
p.lede { color: var(--muted); margin: 0 0 1.25rem; font-size: 0.95rem; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- topbar --- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar .brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.01em;
}
.topbar .brand:hover { color: var(--accent); }

.theme-picker,
.board-picker {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}
.theme-picker select,
.board-picker select {
  width: auto;
  margin-top: 0;
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
}
.board-picker { margin-left: auto; }

/* --- cards & forms --- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-top: 1.25rem;
}

label { display: block; margin-top: 0.85rem; font-weight: 600; font-size: 0.875rem; }

input, select {
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.5rem 0.65rem;
  margin-top: 0.3rem;
  width: 100%;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

button {
  font-size: 0.95rem;
  font-family: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.5rem 1rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover { border-color: var(--accent); }
button:active { transform: translateY(1px); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); }

/* --- tables --- */

table { border-collapse: collapse; width: 100%; margin-top: 0.5rem; }
th, td { text-align: left; padding: 0.5rem 0.65rem; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: none; }
th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.win { color: var(--win); font-weight: 600; }
.loss { color: var(--loss); font-weight: 600; }
.draw { color: var(--muted); }

/* --- lobby --- */

#mm-status { margin-top: 0.85rem; font-weight: 600; min-height: 1.5rem; }

/* --- bot picker --- */

.bot-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(7.5rem, 1fr));
  gap: 0.5rem;
  margin-top: 1rem;
}

.bot-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.65rem 0.5rem;
  text-align: center;
  line-height: 1.2;
}

.bot-card:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-2));
}

.bot-card:disabled {
  opacity: 0.5;
  cursor: wait;
}

.bot-elo {
  font-size: 1.15rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.bot-name {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
}

#bot-status { margin-top: 0.85rem; font-weight: 600; min-height: 1.5rem; }
#links { margin-top: 1.25rem; padding: 1rem; background: var(--surface-2); border-radius: 0.5rem; display: none; }
#links a { display: block; margin-top: 0.5rem; word-break: break-all; }

/* --- play page --- */

.play-wrap { max-width: 34rem; }

#clocks {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  font-variant-numeric: tabular-nums;
  font-size: 1.15rem;
  margin: 0.75rem 0;
}
#clocks > div {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  padding: 0.4rem 0.75rem;
  color: var(--muted);
}
#clocks .active { color: var(--win); font-weight: 700; }

#board {
  display: block;
  width: 100%;
  max-width: 480px;
  height: auto;
  border-radius: 0.5rem;
  box-shadow: var(--shadow);
  touch-action: none;
  user-select: none;
}
#board .square { cursor: pointer; }
#board .square.sq-light { fill: var(--board-light); }
#board .square.sq-dark { fill: var(--board-dark); }
#board .sel-highlight { stroke: var(--board-highlight); }
#board .last-move { fill: var(--board-lastmove); pointer-events: none; }
#board .piece { cursor: grab; }
#board .piece.dragging { cursor: grabbing; }
#board .piece-w { fill: var(--piece-white); stroke: var(--piece-white-outline); stroke-width: 1.1; paint-order: stroke; }
#board .piece-b { fill: var(--piece-black); stroke: var(--piece-black-outline); stroke-width: 1.1; paint-order: stroke; }
#board .promo-cell { fill: var(--surface); stroke: var(--border); }

#status { font-weight: 600; margin: 0.65rem 0; min-height: 1.5rem; }

#controls { margin-top: 0.5rem; display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }

details { margin-top: 0.75rem; }
details summary { cursor: pointer; color: var(--muted); font-size: 0.875rem; }
#move-form { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
#move-form input { margin-top: 0; flex: 1; }

#moves {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.875rem;
  white-space: pre-wrap;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 0.65rem 0.75rem;
  border-radius: 0.5rem;
  max-height: 8rem;
  overflow-y: auto;
  margin-top: 0.75rem;
  min-height: 2.5rem;
}
