:root, [data-theme="dark"] {
  --bg: #161512;
  --panel: #262421;
  --panel-2: #302e2c;
  --text: #bababa;
  --bright: #f0f0f0;
  --muted: #7a776f;
  --accent: #629924;
  --accent-2: #759900;
  --danger: #c33;
  --topbar-bg: #21201d;
  --topbar-border: #3c3935;
  --border: #3c3935;
  --input-bg: #1b1a18;
  --board-w: min(62vh, calc(100vw - 420px), 640px);
  --radius: 8px;
  --panel-pad: 10px 12px;
  --layout-pad: 16px 18px 24px;
  --layout-gap: 16px;
  font-family: "Noto Sans", "Segoe UI", system-ui, sans-serif;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #eceae3;
  --panel: #ffffff;
  --panel-2: #f3f1ea;
  --text: #3c3934;
  --bright: #1a1917;
  --muted: #7a776f;
  --accent: #5a8a1a;
  --accent-2: #6a9a22;
  --danger: #c33;
  --topbar-bg: #f7f5ef;
  --topbar-border: #d5d2c8;
  --border: #d5d2c8;
  --input-bg: #f7f5ef;
  color-scheme: light;
}


/* Board square schemes (chessboard.js class names) */
[data-board="classic"] #board .white-1e1d7,
[data-board="classic"] #setup-board .white-1e1d7 { background-color: #f0d9b5; color: #b58863; }
[data-board="classic"] #board .black-3c85d,
[data-board="classic"] #setup-board .black-3c85d { background-color: #b58863; color: #f0d9b5; }

[data-board="blue"] #board .white-1e1d7,
[data-board="blue"] #setup-board .white-1e1d7 { background-color: #eceff1; color: #6d8a96; }
[data-board="blue"] #board .black-3c85d,
[data-board="blue"] #setup-board .black-3c85d { background-color: #6d8a96; color: #eceff1; }

[data-board="green"] #board .white-1e1d7,
[data-board="green"] #setup-board .white-1e1d7 { background-color: #eeeed2; color: #769656; }
[data-board="green"] #board .black-3c85d,
[data-board="green"] #setup-board .black-3c85d { background-color: #769656; color: #eeeed2; }

[data-board="walnut"] #board .white-1e1d7,
[data-board="walnut"] #setup-board .white-1e1d7 { background-color: #e3c29f; color: #8b5a2b; }
[data-board="walnut"] #board .black-3c85d,
[data-board="walnut"] #setup-board .black-3c85d { background-color: #8b5a2b; color: #e3c29f; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; color: inherit; }

.app { min-height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 8px 14px;
  background: var(--topbar-bg); border-bottom: 1px solid var(--topbar-border);
  flex-wrap: nowrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 18px; color: var(--bright); font-weight: 700; letter-spacing: .02em; }
.tag { margin: 0; font-size: 11px; color: var(--muted); }
.knight { font-size: 28px; line-height: 1; color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.inline { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.inline input[type=range] { width: 110px; accent-color: var(--accent); }
.inline select {
  background: var(--panel-2); border: 1px solid #4a4742; border-radius: 4px; padding: 2px 6px;
}

.layout {
  flex: 1; display: grid;
  grid-template-columns: var(--board-w) minmax(280px, 1fr);
  gap: var(--layout-gap); padding: var(--layout-pad); align-items: start;
}

.eval-col { display: flex; justify-content: center; height: var(--board-w); }
.eval-bar {
  width: 22px; height: 100%; background: #1a1a1a; border-radius: 4px;
  overflow: hidden; position: relative; border: 1px solid var(--border);
  display: flex; flex-direction: column; justify-content: flex-end;
}
.eval-white { background: #f0f0f0; height: 50%; width: 100%; transition: height .18s ease; }
.eval-label {
  position: absolute; left: 50%; top: 8px; transform: translateX(-50%);
  writing-mode: vertical-rl; transform: translateX(-50%) rotate(180deg);
  font-size: 11px; font-weight: 700; color: #111; text-shadow: 0 0 3px #fff;
  pointer-events: none;
}

.board-col { width: var(--board-w); }
#board { width: 100%; touch-action: none; }
.board-tools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.status { margin-top: 8px; font-size: 13px; color: var(--muted); min-height: 1.3em; }

.side-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; max-height: calc(100vh - 90px); }
.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--panel-pad);
}
.panel.grow { flex: 1; min-height: 140px; display: flex; flex-direction: column; }
.panel-h {
  display: flex; justify-content: flex-start; align-items: baseline; gap: 10px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); margin-bottom: 8px;
}
#engine-meta {
  flex: 1;
  min-width: 0;
  text-align: left;
  text-transform: none;
  letter-spacing: normal;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.muted { color: var(--muted); }

.lines { font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 13px; line-height: 1.45; }
.lines.stale { opacity: 0.5; }
.lines.stale .line { pointer-events: none; }
.lines { min-height: 6.5em; }
.line { display: grid; grid-template-columns: 54px 1fr; gap: 8px; padding: 5px 4px; border-radius: 4px; cursor: pointer; }
.line:hover { background: var(--panel-2); }
.line .score { font-weight: 700; color: var(--bright); }
.line .score.good { color: #9bcf5a; }
.line .score.bad { color: #e57373; }
.line .pv { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.moves {
  flex: 1; overflow: auto; font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 14px; line-height: 1.7;
}
.moves .num { color: var(--muted); margin-right: 4px; }
.moves .san {
  display: inline-block; padding: 0 5px; border-radius: 3px; cursor: pointer; color: var(--bright);
}
.moves .san:hover { background: #3d5a1f; }
.moves .san.active { background: var(--accent); color: #fff; }
.moves .var { color: var(--muted); }
.moves .var .san { color: var(--text); }
.moves .var .san.active { color: #fff; }

textarea, .fen {
  width: 100%; background: #1b1a18; border: 1px solid var(--border); border-radius: 4px;
  padding: 8px; resize: vertical; color: var(--text);
}
.fen { margin-top: 8px; font-family: ui-monospace, monospace; font-size: 12px; }
.row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

.btn {
  background: var(--panel-2); border: 1px solid #4a4742; color: var(--bright);
  border-radius: 4px; padding: 6px 10px; cursor: pointer;
}
.btn:hover { background: #3a3834; }
.btn.primary { background: #3d5c16; border-color: #5a8a22; }
.btn.primary:hover { background: #4a701c; }
.btn.off { opacity: .55; }
.btn-icon {
  padding: 6px 10px; font-size: 18px; line-height: 1; min-width: 36px;
}
#btn-play.playing { background: #3d5c16; border-color: #5a8a22; color: #fff; }
[data-theme="light"] #btn-play.playing { background: #6a9a22; border-color: #5a8a1a; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; z-index: 20;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel); padding: 18px 20px; border-radius: 10px; text-align: center;
  border: 1px solid #4a4742;
}
.promo-row { display: flex; gap: 8px; margin-top: 10px; }
.promo-row button { padding: 8px 12px; }

/* chessboard.js square highlight */
.highlight-move { box-shadow: inset 0 0 0 4px rgba(155, 199, 0, .55); }
.highlight-check { box-shadow: inset 0 0 0 4px rgba(224, 80, 80, .7); }
.highlight-sel { box-shadow: inset 0 0 0 4px rgba(80, 160, 255, .7); }
.highlight-dest { box-shadow: inset 0 0 0 3px rgba(80, 160, 255, .35); }
.highlight-dest.highlight-move { box-shadow: inset 0 0 0 4px rgba(155, 199, 0, .55); }




/* Settings */
.settings-wrap { position: relative; }
.settings-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
  min-width: 230px; padding: 12px 14px;
  background: var(--panel); border: 1px solid #4a4742; border-radius: var(--radius, 8px);
  box-shadow: 0 8px 24px rgba(0,0,0,.45);
}
.settings-panel.hidden { display: none; }
.settings-row {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--bright, #f0f0f0); cursor: pointer;
}
.settings-hint { margin: 10px 0 0; font-size: 11px; color: var(--muted); line-height: 1.4; }

/* Eval bar toggle: reclaim space when hidden */
.layout {
  grid-template-columns: var(--board-w) minmax(280px, 1fr);
}
.layout.show-eval {
  grid-template-columns: 28px var(--board-w) minmax(280px, 1fr);
}
.eval-col.hidden { display: none; }

/* Setup modal */
.setup-card {
  text-align: left;
  width: min(560px, 96vw);
  max-height: 96vh;
  overflow: auto;
}
.setup-header h2 {
  margin: 0 0 6px;
  font-size: 18px;
  color: var(--bright, #f0f0f0);
}
.setup-header p { margin: 0 0 12px; font-size: 12px; line-height: 1.4; }
#setup-board { width: min(420px, 86vw); margin: 0 auto; }
.setup-controls { margin-top: 12px; }
.setup-side, .setup-castle { gap: 12px; }
.setup-fen-label { display: block; margin-top: 10px; font-size: 12px; }
.setup-fen-label .fen { margin-top: 4px; }
.setup-error { margin: 8px 0 0; color: #e57373; font-size: 12px; }
.setup-error.hidden { display: none; }
.setup-actions { justify-content: flex-end; margin-top: 14px; }
.setup-card .spare-pieces-7492f { background: transparent; }



/* Play vs engine modal */
.play-card {
  text-align: left;
  width: min(420px, 94vw);
}
.play-header h2 {
  margin: 0 0 6px;
  font-size: 18px;
  color: var(--bright, #f0f0f0);
}
.play-header p { margin: 0 0 14px; font-size: 12px; line-height: 1.4; }
.play-modal-body { display: flex; flex-direction: column; gap: 12px; }
.play-side, .play-limit-group { gap: 12px; align-items: center; }
.play-modal-body .tight { gap: 4px; }
.play-modal-body input[type=radio] { accent-color: var(--accent); margin: 0; }
.play-modal-body select {
  background: var(--panel-2, #302e2c); border: 1px solid #4a4742; border-radius: 4px; padding: 4px 8px;
}
#play-depth-wrap.hidden, #play-time-wrap.hidden { display: none; }
.play-actions { justify-content: flex-end; margin-top: 6px; }
#btn-play.playing { background: #3d5c16; border-color: #5a8a22; }

/* Hide analysis spoilers during play-vs-engine */
body.play-no-spoilers #lines .line { display: none; }
body.play-no-spoilers #eval-col,
body.play-no-spoilers .eval-col {
  visibility: hidden;
}
body.play-no-spoilers #engine-meta {
  opacity: 0.85;
}

/* Setup: click-selected spare piece */
#setup-board .spare-piece-selected {
  outline: 2px solid var(--accent, #629924);
  outline-offset: 2px;
  border-radius: 4px;
  filter: drop-shadow(0 0 4px rgba(98, 153, 36, 0.85));
  cursor: pointer;
}
#setup-board .spare-pieces-7492f .piece-417db {
  cursor: pointer;
}
#setup-board .board-b72b1 .square-55d63 {
  cursor: cell;
}

#setup-board.setup-painting .board-b72b1 .square-55d63 {
  cursor: copy;
}


.settings-label { font-size: 13px; color: var(--muted); margin-right: 4px; }
.settings-theme { flex-wrap: wrap; margin-top: 10px; }
.settings-theme .inline { color: var(--bright); }

/* —— Mobile / narrow screens —— */
@media (max-width: 900px) {
  :root, [data-theme="dark"], [data-theme="light"] {
    --board-w: 100vw;
    --layout-pad: 0;
    --layout-gap: 6px;
    --panel-pad: 6px 8px;
  }

  .topbar {
    gap: 8px;
    padding: 6px 8px;
    min-height: 44px;
  }
  .brand { gap: 6px; min-width: 0; flex: 1; }
  .brand h1 { font-size: 15px; }
  .brand .tag {
    font-size: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 46vw;
  }
  .knight { font-size: 22px; }
  .top-actions {
    gap: 8px;
    flex-wrap: nowrap;
    flex-shrink: 0;
  }
  .top-actions .inline { font-size: 12px; gap: 4px; }
  .top-actions .inline select { padding: 2px 4px; max-width: 52px; }
  #btn-engine { padding: 5px 8px; font-size: 12px; }

  .layout {
    grid-template-columns: 1fr;
    gap: var(--layout-gap);
    padding: var(--layout-pad);
    align-items: stretch;
  }
  .layout.show-eval {
    grid-template-columns: 18px 1fr;
    gap: 4px;
  }

  .board-col {
    width: 100%;
    max-width: 100vw;
    /* leftover px after 8-square snap stay even on both sides */
  }
  #board {
    width: 100%;
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
    touch-action: none;
  }
  /* content-box + 2px border was eating a few px; outline keeps an edge */
  #board .board-b72b1 {
    border: none !important;
    box-shadow: 0 0 0 1px #404040;
  }

  .eval-col { height: var(--board-w); width: 18px; }
  .eval-bar { width: 16px; border-radius: 0; border-left: none; border-right: none; }

  .board-tools {
    margin-top: 6px;
    padding: 0 6px;
    gap: 4px;
  }
  .board-tools .btn { padding: 5px 8px; font-size: 12px; }
  .status { margin: 4px 6px 2px; font-size: 12px; min-height: 1.2em; }

  .side-col {
    max-height: none;
    gap: 6px;
    padding: 0 0 12px;
  }
  .panel {
    padding: var(--panel-pad);
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  .panel-h {
    margin-bottom: 4px;
    font-size: 11px;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }
  #engine-meta {
    width: 100%;
    max-width: 100%;
    text-align: left;
    font-size: 11px;
  }
  .brand-text {
    min-width: 0;
    flex: 1;
    text-align: left;
  }
  .brand .tag {
    text-align: left;
    margin: 0;
  }
  .lines { font-size: 12px; line-height: 1.25; min-height: 8.5em; }
  .line { padding: 2px 4px; gap: 6px; }
  .moves { font-size: 13px; line-height: 1.55; min-height: 5em; }
  textarea { rows: 3; min-height: 64px; padding: 6px; }
}

@media (max-width: 420px) {
  .brand h1 { display: none; }
  .brand .tag { max-width: 38vw; }
  #btn-engine { max-width: 88px; overflow: hidden; text-overflow: ellipsis; }
}

[data-theme="light"] .btn {
  background: var(--panel-2); border-color: #c8c4b8; color: var(--bright);
}
[data-theme="light"] .btn:hover { background: #e7e4da; }
[data-theme="light"] .btn.primary { background: #5a8a1a; border-color: #4a7a12; color: #fff; }
[data-theme="light"] textarea, [data-theme="light"] .fen {
  background: var(--input-bg); border-color: var(--border); color: var(--bright);
}
[data-theme="light"] .inline select {
  background: var(--panel-2); border-color: #c8c4b8; color: var(--bright);
}
[data-theme="light"] .settings-panel {
  background: var(--panel); border-color: var(--border); box-shadow: 0 8px 24px rgba(0,0,0,.12);
}

/* Figurine algebraic notation — prefer symbol fonts over emoji */
body.notation-figurine .san,
body.notation-figurine .lines .pv {
  font-family: "Segoe UI Symbol", "Apple Symbols", "Noto Sans Symbols 2", "DejaVu Sans", sans-serif;
  font-size: 1.05em;
}
