/* Walnut theme, after the chess.com app: dark wood UI, a tan wooden board,
   chess.com's green for primary actions. X moves first, so it plays the
   part of white (ivory with a dark outline, like a chess piece) and O of
   black (ebony) — the eval bar follows suit. */
:root {
  --bg: #2f251e;          /* walnut */
  --panel: #3d3027;       /* oak panels */
  --panel-2: #4b3b2f;
  --border: #5f4a39;
  --text: #f2e8da;        /* cream */
  --muted: #c4b19b;
  --accent: #81b64c;      /* chess.com green */
  --accent-hover: #93c45d;
  --accent-edge: #5a8a33; /* the 3-D bottom edge of primary buttons */
  --green: #81b64c;
  --red: #e0604a;
  --gold: #f2c45a;

  /* The board */
  --board-frame: #5a3b25; /* outer frame + gaps between boards */
  --sub-board: #8b5e3c;   /* gaps between cells */
  --cell: #e6c89c;        /* tan squares */
  --cell-hover: #efd8b4;
  --cell-last: #eedb7e;   /* chess.com-style last-move highlight */
  --hint: #3f7a22;        /* dark green that reads on tan */

  /* The pieces */
  --x-color: #fbf7ef;     /* ivory */
  --o-color: #2a201a;     /* ebony */
  --piece-outline: #1e1712;

  --radius: 12px;
  font-size: 16px;
}

* { box-sizing: border-box; }

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

main {
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  padding: 0 16px 32px;
  flex: 1;
}

/* ---- Header ---- */
.topbar {
  max-width: 620px;
  width: 100%;
  margin: 0 auto;
  padding: 20px 16px 8px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.topbar h1 {
  margin: 0;
  font-size: 1.55rem;
  letter-spacing: 0.5px;
}
.subtitle {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}
.stats {
  color: var(--muted);
  font-size: 0.8rem;
  text-align: right;
  white-space: nowrap;
}
.icon-btn {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1.1rem;
  padding: 6px 10px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--panel-2); }

.settings-panel {
  max-width: 620px;
  margin: 0 auto 8px;
  padding: 12px 16px;
  width: calc(100% - 32px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.setting-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  cursor: pointer;
}

/* ---- Ladder ---- */
.ladder {
  list-style: none;
  display: flex;
  gap: 4px;
  padding: 0;
  margin: 12px 0;
}
.ladder li {
  flex: 1;
  text-align: center;
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 7px 2px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
  position: relative;
  overflow: hidden;
}
.ladder li.beaten {
  background: rgba(129, 182, 76, 0.18);
  border-color: var(--green);
  color: var(--green);
}
.ladder li.current {
  background: rgba(242, 196, 90, 0.16);
  border-color: var(--gold);
  color: var(--gold);
  font-weight: 700;
}
.ladder li.fell {
  background: rgba(224, 96, 74, 0.16);
  border-color: var(--red);
  color: var(--red);
}

/* ---- Status ---- */
.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 28px;
  margin-bottom: 8px;
}
.status { font-size: 0.95rem; }
.mode-badge {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--gold);
  color: var(--gold);
}

/* ---- Eval bar: X (ivory) fills from the left, O (ebony) is the rest ---- */
.eval-wrap { margin-bottom: 10px; }
.eval-bar {
  height: 20px;
  border-radius: 10px;
  background: var(--o-color);
  border: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}
.eval-fill {
  height: 100%;
  width: 50%;
  background: var(--x-color);
  transition: width 0.5s ease;
}
/* The label sits in a small pill so it reads over either color */
.eval-label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 1px 9px;
  border-radius: 999px;
  background: rgba(47, 37, 30, 0.88);
  color: var(--text);
  font-size: 0.68rem;
  white-space: nowrap;
  pointer-events: none;
}
.eval-label:empty { display: none; }

/* ---- Board ---- */
.board-wrap { position: relative; }
.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  background: var(--board-frame);
  padding: 6px;
  border-radius: var(--radius);
  aspect-ratio: 1;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
.sub-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  background: var(--sub-board);
  border-radius: 6px;
  padding: 3px;
  position: relative;
  transition: box-shadow 0.25s ease;
  min-width: 0;
  min-height: 0;
}
.sub-board.active { box-shadow: 0 0 0 3px var(--accent); }
.cell {
  border: none;
  background: var(--cell);
  border-radius: 3px;
  font-size: clamp(0.9rem, 4vw, 1.5rem);
  font-weight: 800;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
  padding: 0;
  min-width: 0;
  min-height: 0;
  font-family: inherit;
}
.cell.playable { cursor: pointer; }
.cell.playable:hover { background: var(--cell-hover); }
.cell.x {
  color: var(--x-color);
  -webkit-text-stroke: 1.4px var(--piece-outline);
  paint-order: stroke fill;
}
.cell.o { color: var(--o-color); }
.cell.last-move { background: var(--cell-last); }
.cell.hint {
  outline: 2px dashed var(--hint);
  outline-offset: -3px;
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { background: var(--cell); }
  50% { background: rgba(129, 182, 76, 0.6); }
}
/* Analysis runner-up candidates: a small rank number in the empty cell */
.cell.cand { outline: 1px dashed var(--hint); outline-offset: -3px; }
.cell.cand:empty::after {
  content: attr(data-label);
  font-size: 0.7em;
  font-weight: 700;
  color: var(--hint);
}
/* A decided small board: tan wash over its cells, big mark on top */
.sub-board .macro-mark {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2rem, 12vw, 4rem);
  font-weight: 800;
  border-radius: 6px;
  background: rgba(230, 200, 156, 0.86);
  pointer-events: none;
}
.macro-mark.x {
  color: var(--x-color);
  -webkit-text-stroke: 2.5px var(--piece-outline);
  paint-order: stroke fill;
}
.macro-mark.o { color: var(--o-color); }
.macro-mark.draw { color: var(--sub-board); }

/* ---- Overlay / modal ---- */
.board-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(24, 18, 13, 0.72);
  border-radius: var(--radius);
  z-index: 5;
}
.overlay-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 88%;
  width: 380px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.overlay-card h2 { margin: 0 0 6px; font-size: 1.25rem; }
.overlay-card p { color: var(--muted); font-size: 0.9rem; margin: 6px 0; }
.overlay-card .share-line {
  font-size: 0.8rem;
  color: var(--text);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px;
  margin: 10px 0;
  word-break: break-word;
  white-space: pre-line; /* the share string is two lines (title, link+result) */
}
.overlay-card canvas {
  width: 100%;
  height: 120px;
  margin: 8px 0;
  background: var(--panel-2);
  border-radius: 8px;
}
.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

/* ---- Buttons & controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
}
.btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  font-family: inherit;
}
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
/* chess.com's chunky green button */
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
  box-shadow: 0 3px 0 var(--accent-edge);
}
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.primary:active:not(:disabled) { box-shadow: 0 1px 0 var(--accent-edge); transform: translateY(2px); }
.side-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 12px 0 4px;
  font-size: 0.85rem;
  color: var(--muted);
}
.side-btn {
  padding: 4px 16px;
  font-weight: 700;
}
.side-btn.x.selected {
  background: var(--x-color);
  border-color: var(--x-color);
  color: var(--o-color);
}
.side-btn.o.selected {
  background: var(--o-color);
  border-color: var(--muted);
  color: var(--x-color);
}
.practice-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--muted);
}
.practice-controls select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 8px;
  font-family: inherit;
}

/* ---- Edition-specific content (html[data-edition], see js/config.js) ---- */
html[data-edition="public"] .personal-only,
html[data-edition="personal"] .public-only { display: none !important; }

/* ---- Mode tabs (personal edition) ---- */
.tabs {
  display: flex;
  gap: 4px;
  margin: 8px 0 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
}
.tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.9rem;
  padding: 8px 4px;
  border-radius: 7px;
  cursor: pointer;
}
.tab.selected { background: var(--panel-2); color: var(--text); font-weight: 700; }
.tab:disabled { opacity: 0.4; cursor: not-allowed; }
.difficulty-select,
.inline-setting select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-family: inherit;
}

/* ---- Analysis board ---- */
.analysis-options { margin-top: 8px; font-size: 0.85rem; color: var(--muted); }
.inline-setting { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.analysis-results {
  margin-top: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 0.88rem;
}
.analysis-results p { margin: 0; }
.best-line { margin-bottom: 8px; }
.best-line strong { color: var(--green); font-size: 1.05rem; }
.proof { color: var(--gold); }
.proof.lost { color: var(--red); font-weight: 600; }
.candidates { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.candidates th {
  text-align: left;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.75rem;
  padding: 2px 4px 4px;
  border-bottom: 1px solid var(--border);
}
.candidates td { padding: 4px; }
.candidates tr.best td { color: var(--green); font-weight: 600; }
.pv { margin-top: 10px; line-height: 1.6; }
.muted { color: var(--muted); }
.small { font-size: 0.75rem; margin-top: 8px; }
.move-list {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 4px;
  font-size: 0.85rem;
}
.mv-num { color: var(--muted); margin-left: 6px; font-variant-numeric: tabular-nums; }
.mv {
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 2px 5px;
  font: inherit;
  color: var(--text);
  cursor: pointer;
}
.mv.current { border-color: var(--gold); background: rgba(242, 196, 90, 0.12); }

.position-tools { margin-top: 8px; }
.small-btn { padding: 6px 10px; font-size: 0.82rem; }

/* ---- Position editor ---- */
.palette {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.brush { min-width: 52px; font-weight: 800; }
.brush.selected { box-shadow: 0 0 0 2px var(--accent); }
.brush.x.selected {
  background: var(--x-color);
  color: var(--o-color);
  border-color: var(--x-color);
}
.brush.o.selected {
  background: var(--o-color);
  color: var(--x-color);
  border-color: var(--muted);
}
.brush.erase.selected { background: var(--panel-2); }
/* Every square is editable, and decided boards stay see-through so the
   pieces under the big mark can still be changed */
.board.editing .cell { cursor: pointer; }
.board.editing .macro-mark {
  background: rgba(230, 200, 156, 0.35);
  opacity: 0.55;
}
.editor-info {
  margin-top: 8px;
  font-size: 0.9rem;
  color: var(--muted);
  min-height: 1.3em;
}
.editor-info.error { color: var(--red); }

/* ---- Misc ---- */
.howto {
  margin-top: 22px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 0.88rem;
  color: var(--muted);
}
.howto summary { cursor: pointer; color: var(--text); font-weight: 600; }
.howto p { line-height: 1.5; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 0.85rem;
  z-index: 50;
  transition: opacity 0.3s ease;
}

footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 16px;
}
footer a { color: var(--accent); }

.hidden { display: none !important; }

@media (max-width: 480px) {
  .topbar h1 { font-size: 1.25rem; }
  .stats { display: none; }
}
