:root {
  --bg: #1b1b1f;
  --panel: #26262c;
  --panel-border: #38383f;
  --text: #e9e9ee;
  --text-dim: #9a9aa5;
  --accent: #d4af37;
  --light-sq: #ecdab9;
  --dark-sq: #7c5a3a;
  --sel-sq: rgba(76, 175, 80, 0.55);
  --target-dot: rgba(30, 30, 30, 0.55);
  --check-glow: rgba(220, 60, 60, 0.65);
  --white-piece: #fdfdfd;
  --black-piece: #17171a;
  --piece-outline: #000;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--panel-border);
  background: var(--panel);
  flex-wrap: wrap;
  gap: 10px;
}

header h1 {
  font-size: 1.3em;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
header h1 .crown { color: var(--accent); }

.header-controls { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }

select, button {
  font-family: inherit;
  font-size: 0.95em;
  background: #33333b;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
button:hover, select:hover { background: #3d3d47; }
button.primary { background: var(--accent); color: #1b1b1f; font-weight: 600; border: none; }
button.primary:hover { background: #e2bd4a; }
button:disabled { opacity: 0.5; cursor: default; }

main {
  display: flex;
  gap: 24px;
  padding: 20px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

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

#status-banner {
  min-height: 1.4em;
  font-size: 1.05em;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  text-align: center;
}
#status-banner.check { color: #ff8a80; }
#status-banner.over { color: var(--accent); }

#board {
  display: grid;
  grid-template-columns: repeat(8, 56px);
  grid-template-rows: repeat(8, 56px);
  border: 4px solid #4a3623;
  border-radius: 4px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

.square {
  position: relative;
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  user-select: none;
  cursor: pointer;
}
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }
.square.selected::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--sel-sq);
}
.square.target::before {
  content: '';
  position: absolute;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--target-dot);
  z-index: 1;
}
.square.target.has-piece::before {
  width: 100%; height: 100%;
  border-radius: 0;
  background: transparent;
  border: 4px solid var(--target-dot);
}
.square.last-move {
  box-shadow: inset 0 0 0 3px rgba(255, 235, 59, 0.7);
  background-image: linear-gradient(rgba(255, 235, 59, 0.32), rgba(255, 235, 59, 0.32));
}
.square.in-check { box-shadow: inset 0 0 12px 4px var(--check-glow); }
.square.setup-pickable { box-shadow: inset 0 0 0 3px rgba(212, 175, 55, 0.65); }

.piece { position: relative; z-index: 2; line-height: 1; }
.piece.white { color: var(--white-piece); text-shadow: 0 0 2px #000, 0 0 1px #000; }
.piece.black { color: var(--black-piece); text-shadow: 0 0 2px #fff8, 0 0 1px #fff8; }

.crown-badge {
  position: absolute;
  top: 1px;
  right: 3px;
  font-size: 15px;
  z-index: 3;
  filter: drop-shadow(0 0 2px #000);
}

.file-labels, .rank-labels { display: flex; color: var(--text-dim); font-size: 0.75em; }
.file-labels { justify-content: space-around; width: 448px; }
.board-row { display: flex; align-items: center; gap: 4px; position: relative; }

.side-panel {
  width: 300px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-box {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 12px;
}
.panel-box h3 { margin: 0 0 8px 0; font-size: 0.95em; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.online-controls { display: flex; gap: 8px; }
.online-controls button { flex: 1; }

#history-list {
  max-height: 220px;
  overflow-y: auto;
  font-family: 'Consolas', monospace;
  font-size: 0.9em;
  display: grid;
  grid-template-columns: 2em 1fr 1fr;
  gap: 2px 8px;
}
#history-list .move-num { color: var(--text-dim); }

.tray { display: flex; flex-wrap: wrap; gap: 2px; min-height: 28px; font-size: 22px; }
.tray .piece { font-size: 22px; }

#turn-indicator { font-size: 0.95em; color: var(--text-dim); }
#turn-indicator strong { color: var(--text); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.hidden { display: none !important; }
.modal-overlay.hidden { display: none; }
/* The hidden-queen instruction card lives inside .board-row (not the usual
   viewport-fixed .modal-overlay) and is positioned absolutely against the
   board itself — anchored to the board's own top edge, over the opponent's
   pieces, rather than a fixed pixel offset from the viewport. A fixed
   offset only happened to work for whatever header/nameplate height it was
   tuned against; anchoring to the board is correct at any header height or
   viewport size. Your own pieces are always drawn on the bottom two ranks
   regardless of which color you're playing, so this keeps the card clear
   of them. It's also fully click-through — no buttons live inside it, the
   player picks their hidden queen by clicking the board underneath. */
.board-hint {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex; justify-content: center;
  pointer-events: none;
  z-index: 10;
}
.modal-box {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 24px;
  max-width: 480px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
}
.modal-box h2 { margin-top: 0; }
.modal-box p { line-height: 1.5; color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.modal-checkbox { display: flex; align-items: center; gap: 6px; font-size: 0.9em; color: var(--text-dim); margin-top: 10px; }

.promo-choices { display: flex; gap: 10px; justify-content: center; margin-top: 12px; }
.promo-choices button { font-size: 30px; padding: 8px 14px; }

/* ---------- Toasts ---------- */
#toast-container {
  position: fixed;
  bottom: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 200;
}
.toast {
  background: #2f2a1a;
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  max-width: 320px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
  animation: fade-in 0.25s ease-out;
}
@keyframes fade-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

#confirm-banner {
  text-align: center;
  font-size: 1.1em;
  padding: 10px;
}
#confirm-banner .crown { color: var(--accent); }

.bot-thinking { color: var(--text-dim); font-style: italic; }

/* ---------- Rating / nameplates ---------- */
.rating-summary { font-size: 0.9em; color: var(--text-dim); }
.rating-summary strong { color: var(--accent); }

.nameplates {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.95em;
}
.nameplate {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 4px 12px;
}
.nameplate .elo, .nameplate .name { font-weight: 600; }
.nameplate.active-turn { border-color: var(--accent); }
/* Online chess clock — monospace + tabular digits so the text doesn't jitter as it ticks. */
.nameplate .clock {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.07);
  font-family: ui-monospace, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  min-width: 3.6em;
  text-align: center;
}
.nameplate.active-turn .clock { background: rgba(212, 175, 55, 0.22); }
.nameplate .clock.low { color: #ff6b6b; }
.nameplate .clock.expired { color: #ff6b6b; opacity: 0.7; }
.nameplate-vs { color: var(--text-dim); font-size: 0.85em; }

.rating-change-line {
  font-size: 0.95em;
  padding: 4px 12px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
}
.rating-change-line.hidden { display: none; }
.rating-change-line .delta-up { color: #6fcf6f; font-weight: 700; }
.rating-change-line .delta-down { color: #ff8a80; font-weight: 700; }
.rating-change-line .delta-flat { color: var(--text-dim); font-weight: 700; }

.rating-caption { font-size: 0.85em; color: var(--text-dim); }

button.danger { border-color: #a3312f; color: #ff8a80; }
button.danger:hover { background: #a3312f; color: #fff; }

/* ---------- Bot picker ---------- */
.modal-box.wide { max-width: 640px; width: 92vw; }

.bot-roster-list {
  max-height: 60vh;
  overflow-y: auto;
  margin-top: 12px;
}
.bot-tier-header {
  font-size: 0.78em;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin: 16px 0 6px 0;
  border-bottom: 1px solid var(--panel-border);
  padding-bottom: 4px;
}
.bot-tier-header:first-child { margin-top: 0; }

.bot-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  margin-bottom: 6px;
  background: #2c2c33;
}
.bot-card:hover { background: #35353d; border-color: var(--accent); }
.bot-card .bot-name-row { display: flex; align-items: baseline; gap: 8px; }
.bot-card .bot-name { font-weight: 700; }
.bot-card .bot-elo { color: var(--accent); font-variant-numeric: tabular-nums; }
.bot-card .bot-blurb { display: block; font-size: 0.85em; color: var(--text-dim); margin-top: 2px; font-weight: 400; }
.bot-card .recommended-badge {
  font-size: 0.72em;
  background: var(--accent);
  color: #1b1b1f;
  border-radius: 10px;
  padding: 2px 8px;
  font-weight: 700;
  white-space: nowrap;
}

/* ---------- Match history ---------- */
.match-history-list {
  max-height: 50vh;
  overflow-y: auto;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.match-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #2c2c33;
  border-radius: 6px;
  font-size: 0.9em;
}
.match-row .match-result-win { color: #6fcf6f; font-weight: 700; }
.match-row .match-result-loss { color: #ff8a80; font-weight: 700; }
.match-row .match-result-draw { color: var(--text-dim); font-weight: 700; }
.match-row .match-delta { font-variant-numeric: tabular-nums; font-weight: 700; }
.match-empty { color: var(--text-dim); font-size: 0.9em; text-align: center; padding: 20px; }

/* ---------- Premoves ---------- */
.square.premove-highlight { box-shadow: inset 0 0 0 3px rgba(90, 150, 220, 0.85); }
.premove-badge {
  position: absolute;
  bottom: 1px;
  left: 3px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: rgba(60, 110, 200, 0.9);
  border-radius: 50%;
  width: 15px; height: 15px;
  display: flex; align-items: center; justify-content: center;
  z-index: 3;
}
.premove-list { display: flex; flex-direction: column; gap: 4px; font-family: 'Consolas', monospace; font-size: 0.9em; min-height: 20px; margin-bottom: 8px; }
.premove-list .premove-empty { color: var(--text-dim); font-family: inherit; font-size: 0.9em; }
button.small-btn { font-size: 0.82em; padding: 4px 8px; width: 100%; }

/* ---------- Mode picker ---------- */
.mode-choices { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.mode-choice-btn {
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px;
  padding: 14px 16px; text-align: left; width: 100%;
}
.mode-choice-title { font-weight: 700; font-size: 1.05em; }
.mode-choice-sub { font-size: 0.85em; color: var(--text-dim); font-weight: 400; }
button.mode-choice-btn.primary .mode-choice-sub { color: #1b1b1f; opacity: 0.75; }

/* ---------- Online play ---------- */
.room-code-input {
  display: block;
  width: 100%;
  font-family: 'Consolas', monospace;
  font-size: 1.6em;
  letter-spacing: 0.25em;
  text-align: center;
  text-transform: uppercase;
  padding: 10px;
  margin-top: 10px;
  background: #33333b;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
}
.room-code-display {
  font-family: 'Consolas', monospace;
  font-size: 2em;
  letter-spacing: 0.3em;
  text-align: center;
  color: var(--accent);
  background: #33333b;
  border-radius: 8px;
  padding: 14px;
  margin: 10px 0;
}
.room-error { color: #ff8a80; font-size: 0.9em; margin-top: 6px; }
.online-indicator { color: var(--text-dim); font-style: italic; }

/* ---------- Accounts ---------- */
.auth-status { font-size: 0.9em; color: var(--text-dim); }
.auth-input {
  font-family: inherit; font-size: 1em; letter-spacing: normal; text-align: left; text-transform: none;
}
.auth-input + .auth-input { margin-top: 8px; }

/* ---------- Leaderboard ---------- */
.leaderboard-tabs { display: flex; gap: 6px; margin: 14px 0 10px; }
.leaderboard-tab-btn { flex: 1; padding: 8px; font-size: 0.9em; }
.leaderboard-tab-btn.active { background: var(--accent); color: #1b1b1f; border-color: var(--accent); }
.leaderboard-list { display: flex; flex-direction: column; gap: 4px; max-height: 50vh; overflow-y: auto; }
.leaderboard-row {
  display: grid; grid-template-columns: 2em 1fr auto auto; align-items: center; gap: 10px;
  padding: 8px 10px; background: #2f2f36; border-radius: 6px;
}
.leaderboard-rank { color: var(--text-dim); font-weight: 700; }
.leaderboard-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-games { color: var(--text-dim); font-size: 0.85em; }
.leaderboard-rating { color: var(--accent); font-weight: 700; font-family: 'Consolas', monospace; }
.leaderboard-empty, .leaderboard-loading { color: var(--text-dim); text-align: center; padding: 20px; }

.rematch-note { margin: 8px 0 0 0; font-size: 0.85em; color: var(--text-dim); }

/* ---------- Profile ---------- */
.profile-since { margin: -8px 0 12px 0; color: var(--text-dim); font-size: 0.9em; }
.profile-ratings { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.profile-chip {
  flex: 1; min-width: 110px; padding: 8px 10px; border-radius: 8px;
  background: rgba(255, 255, 255, 0.05); border: 1px solid var(--panel-border); text-align: center;
}
.profile-chip .label { display: block; font-size: 0.8em; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.profile-chip .value { display: block; font-size: 1.4em; font-weight: 700; color: var(--accent); }
.profile-chip .sub { display: block; font-size: 0.75em; color: var(--text-dim); }
.profile-record { margin: 0 0 12px 0; font-weight: 600; }
.profile-section-title { margin: 12px 0 6px 0; font-size: 0.9em; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; }
.profile-game {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 6px;
  border-bottom: 1px solid var(--panel-border); font-size: 0.92em;
}
.profile-game .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-game .meta { color: var(--text-dim); font-size: 0.85em; white-space: nowrap; }
.profile-game .badge { font-weight: 700; min-width: 3.2em; text-align: center; }
.profile-game .badge.win { color: #7bd88f; }
.profile-game .badge.loss { color: #ff8a80; }
.profile-game .badge.draw { color: var(--text-dim); }
.profile-game button { padding: 3px 9px; font-size: 0.85em; }
.profile-empty { color: var(--text-dim); padding: 8px 0; }
.leaderboard-name.clickable { cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }
.leaderboard-name.clickable:hover { color: var(--accent); }
.auth-status.clickable { cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; }

/* ---------- Replay ---------- */
.replay-box { max-width: 420px; }
.replay-sub { margin: -8px 0 10px 0; color: var(--text-dim); font-size: 0.9em; }
.replay-board {
  display: grid;
  grid-template-columns: repeat(8, 40px);
  grid-template-rows: repeat(8, 40px);
  border: 3px solid #4a3623; border-radius: 4px; width: max-content; margin: 0 auto;
}
.replay-board .square { width: 40px; height: 40px; font-size: 28px; cursor: default; }
.replay-board .crown-badge { font-size: 11px; top: 0; right: 1px; }
.replay-move-text { text-align: center; min-height: 1.6em; margin: 10px 0 6px 0; font-weight: 600; }
.replay-move-text .reveal { color: var(--accent); }
.replay-controls { display: flex; gap: 6px; justify-content: center; }
.replay-controls button { min-width: 44px; }
.replay-legend { font-size: 0.8em; color: var(--text-dim); text-align: center; margin: 10px 0 0 0; }

/* ---------- Recovery code / forgot password ---------- */
.recovery-code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 1.5em;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-align: center;
  padding: 14px 10px;
  margin: 10px 0;
  border-radius: 8px;
  background: rgba(212, 175, 55, 0.12);
  border: 1px dashed var(--accent);
  color: var(--accent);
  user-select: all;
  word-break: break-all;
}
.link-btn {
  background: none; border: none; padding: 2px 0; margin: 2px 0 6px 0;
  color: var(--text-dim); text-decoration: underline; cursor: pointer; font-size: 0.85em;
}
.link-btn:hover { color: var(--accent); background: none; }
