:root {
  --bg: #312e2b;
  --surface: #262421;
  --surface-2: #383531;
  --border: #45413c;
  --text: #f3f3f2;
  --muted: #a6a39e;
  --accent: #81b64c;
  --accent-hover: #96c95d;
  --primary: #4b7399;
  --primary-hover: #5b86af;
  --sq-light: #eeeed2;
  --sq-dark: #769656;
  --sq-selected: rgba(255, 255, 51, 0.52);
  --sq-last: rgba(246, 246, 105, 0.58);
  --sq-check: rgba(235, 97, 80, 0.85);
}

body.theme-wood {
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
}

body.theme-slate {
  --sq-light: #dee3e6;
  --sq-dark: #8ca2ad;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

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

.app {
  max-width: 1540px;
  margin: 0 auto;
  padding: 10px 18px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 28px; line-height: 1; color: var(--accent); }
.brand h1 { font-size: 1.1rem; font-weight: 700; }
.subtitle { font-size: 0.76rem; color: var(--muted); }

.topbar-right { display: flex; align-items: center; gap: 10px; }

.theme-select, .engine-status {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 0.8rem;
}
.icon-btn { font-size: 0.95rem; line-height: 1; padding: 5px 9px; }

.engine-status {
  display: flex;
  align-items: center;
  gap: 7px;
}
.engine-status .dot { width: 8px; height: 8px; border-radius: 50%; background: #f59e0b; }
.engine-status.ready .dot { background: var(--accent); }
.engine-status.error .dot { background: #ef4444; }

.workspace {
  display: grid;
  grid-template-columns: auto minmax(540px, 700px);
  gap: 18px;
  align-items: start;
  justify-content: center;
}

.board-section { display: flex; gap: 10px; align-items: stretch; }

.eval-bar-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 34px;
  padding: 32px 0;
  gap: 5px;
}
.eval-bar-wrap.flipped { flex-direction: column-reverse; }
.eval-bar-wrap.flipped .eval-bar { justify-content: flex-start; }
.eval-bar-wrap.eval-hidden { opacity: 0.15; pointer-events: none; }

.eval-bar {
  flex: 1;
  width: 22px;
  background: #403d39;
  border: 1px solid #1f1d1b;
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.eval-fill-white {
  background: #ffffff;
  width: 100%;
  transition: height 0.25s ease;
}

.eval-score-top, .eval-score-bot {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--muted);
  min-height: 15px;
}

.board-container { display: flex; flex-direction: column; gap: 6px; }

.player-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 10px;
  background: var(--surface);
  border-radius: 5px;
  font-size: 0.88rem;
  font-weight: 600;
}
.player-left { display: flex; align-items: center; gap: 8px; }
.captured-pieces { font-size: 0.95rem; letter-spacing: 1px; color: var(--muted); }

.clock-badge {
  font-family: monospace;
  font-size: 0.95rem;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--bg);
}
.clock-badge.active { background: #ffffff; color: #1a1917; }
.clock-badge.low { background: #ca3431; color: white; }

.board-wrapper {
  position: relative;
  width: min(76vh, 580px);
  height: min(76vh, 580px);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  user-select: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.square.light { background: var(--sq-light); color: #769656; }
.square.dark { background: var(--sq-dark); color: #eeeed2; }
.square.last-move::after { content: ''; position: absolute; inset: 0; background: var(--sq-last); pointer-events: none; }
.square.selected::after { content: ''; position: absolute; inset: 0; background: var(--sq-selected); pointer-events: none; }
.square.in-check { background: var(--sq-check) !important; }
.square.premove { background: rgba(235, 97, 80, 0.52) !important; }
.square.user-mark { background: rgba(235, 97, 80, 0.72) !important; }

.piece {
  position: relative;
  z-index: 2;
  width: 85%;
  height: 85%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.32));
}
.piece svg { width: 100%; height: 100%; }

.legal-dot {
  position: relative;
  z-index: 3;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.22);
  pointer-events: none;
}
.square.has-piece .legal-dot {
  position: absolute;
  inset: 5%;
  width: auto;
  height: auto;
  background: transparent;
  border: 4px solid rgba(0, 0, 0, 0.24);
}

.sq-badge {
  position: absolute;
  top: 3px;
  right: 3px;
  z-index: 5;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.45);
  pointer-events: none;
}

.coord-rank { position: absolute; top: 2px; left: 4px; font-size: 10px; font-weight: 700; z-index: 1; }
.coord-file { position: absolute; bottom: 2px; right: 4px; font-size: 10px; font-weight: 700; z-index: 1; }

.arrow-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 4; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: calc(100vh - 82px);
  overflow-y: auto;
  padding-right: 2px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}
.compact-card { padding: 8px 10px; }

.mode-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 8px; }
.tab-btn {
  padding: 6px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
}
.tab-btn.active { background: var(--accent); color: #16220b; border-color: var(--accent); }

.bot-settings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  padding: 7px 10px;
  margin-bottom: 8px;
  background: var(--bg);
  border-radius: 5px;
  font-size: 0.78rem;
}
.setting-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.setting-row select { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: 3px 6px; }
.checkbox-row label { display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 0.78rem; }

.action-grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }

.btn {
  padding: 6px 10px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
  font-size: 0.8rem;
  cursor: pointer;
}
.btn:hover { background: #47433e; }
.btn.primary { background: var(--primary); border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.accent { background: var(--accent); border-color: var(--accent); color: #16220b; }
.btn.accent:hover { background: var(--accent-hover); }
.btn.small { padding: 4px 8px; font-size: 0.76rem; }
.btn.toggle-on { background: var(--accent); border-color: var(--accent); color: #16220b; }
.btn.toggle-off { background: var(--surface-2); border-color: var(--border); color: var(--muted); }

.panel-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.panel-title { font-size: 0.95rem; font-weight: 700; }
.header-actions { display: flex; align-items: center; gap: 6px; }

.eval-pill {
  font-size: 0.85rem;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: 4px;
  background: #ffffff;
  color: #1a1917;
}

.eval-graph-wrap {
  background: #3d3a36;
  border-radius: 4px;
  height: 46px;
  overflow: hidden;
  margin-bottom: 6px;
  border: 1px solid var(--border);
}
#evalGraph { width: 100%; height: 100%; display: block; cursor: pointer; }

.pv-list { display: flex; flex-direction: column; gap: 3px; font-size: 0.78rem; margin-bottom: 8px; }
.pv-line {
  background: var(--bg);
  padding: 4px 7px;
  border-radius: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pv-score {
  display: inline-block;
  min-width: 42px;
  text-align: center;
  font-weight: 800;
  background: #ffffff;
  color: #1a1917;
  border-radius: 3px;
  padding: 0 4px;
  margin-right: 6px;
}
.pv-score.neg { background: #1a1917; color: #f3f3f2; }
.pv-empty { color: var(--muted); font-style: italic; padding: 4px 0; }

.opening-depth-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
  font-size: 0.78rem;
}
.opening-left { display: flex; align-items: center; gap: 6px; overflow: hidden; }
.eco-code {
  font-size: 0.72rem; font-weight: 800;
  background: var(--surface-2); color: var(--accent);
  padding: 1px 5px; border-radius: 3px;
}
.opening-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.depth-badge { color: var(--muted); white-space: nowrap; }

.engine-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 0.74rem;
  color: var(--muted);
}
.eng-ctrl { display: flex; flex-direction: column; gap: 2px; }
.eng-ctrl strong { color: var(--text); }
.eng-ctrl input[type="range"] { width: 100%; accent-color: var(--accent); }

.move-coach {
  background: var(--bg);
  border-left: 3px solid var(--accent);
  padding: 8px 10px;
  border-radius: 4px;
  font-size: 0.78rem;
  margin-bottom: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.coach-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.coach-title { font-weight: 700; font-size: 0.82rem; display: flex; align-items: center; gap: 6px; }
.coach-eval-shift {
  font-family: monospace;
  font-size: 0.74rem;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--text);
}
.coach-desc { color: #e2e0dc; line-height: 1.38; }
.coach-pv { font-family: monospace; font-size: 0.74rem; color: var(--accent); background: var(--surface); padding: 4px 6px; border-radius: 4px; }
.coach-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.coach-retry-msg { font-weight: 700; padding: 4px 7px; border-radius: 4px; }
.coach-retry-msg.ok { background: rgba(129, 182, 76, 0.2); color: #a3e635; }
.coach-retry-msg.bad { background: rgba(202, 52, 49, 0.22); color: #fca5a5; }

.moves-inline {
  background: var(--bg);
  border-radius: 4px;
  padding: 5px 8px;
  max-height: 92px;
  overflow-y: auto;
  font-size: 0.8rem;
  line-height: 1.55;
  margin-bottom: 6px;
}
.mv-num { color: var(--muted); margin-right: 3px; }
.mv-item {
  display: inline-block;
  padding: 0 4px;
  border-radius: 3px;
  cursor: pointer;
  margin-right: 4px;
  font-weight: 600;
}
.mv-item:hover { background: var(--surface-2); }
.mv-item.active { background: #5a5650; color: #ffffff; }

.q-book { color: #d4a373; }
.q-brilliant { color: #26c6da; }
.q-great { color: #5c9ce6; }
.q-best { color: #81b64c; }
.q-good { color: #96bc4b; }
.q-inacc { color: #f0c15c; }
.q-mistake { color: #e58f2a; }
.q-blunder { color: #ca3431; }

.bg-book { background: #a88865; color: #ffffff; }
.bg-brilliant { background: #1baca6; color: #ffffff; }
.bg-great { background: #5c8bb0; color: #ffffff; }
.bg-best { background: #81b64c; color: #ffffff; }
.bg-good { background: #96bc4b; color: #ffffff; }
.bg-inacc { background: #f0c15c; color: #1a1917; }
.bg-mistake { background: #e58f2a; color: #ffffff; }
.bg-blunder { background: #ca3431; color: #ffffff; }

.review-box {
  background: var(--bg);
  border-radius: 5px;
  padding: 7px 9px;
  margin-bottom: 6px;
  display: grid;
  grid-template-columns: 185px 1fr;
  gap: 10px;
  align-items: center;
}
.accuracy-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 5px;
  margin-bottom: 0;
}
.acc-card {
  text-align: center;
  padding: 5px 8px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
}
.acc-card strong { font-size: 1.05rem; }
.acc-card span { font-size: 0.7rem; opacity: 0.85; }
.white-acc { background: #ffffff; color: #1a1917; }
.black-acc { background: #383531; color: #f3f3f2; border: 1px solid var(--border); }
.result-mid { font-size: 0.85rem; font-weight: 700; color: var(--muted); }

.breakdown-table { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 0.76rem; }
.bd-row {
  display: grid;
  grid-template-columns: 36px 1fr 36px;
  align-items: center;
  text-align: center;
}
.bd-label { display: flex; align-items: center; justify-content: center; gap: 5px; font-weight: 600; }
.bd-dot { width: 15px; height: 15px; border-radius: 50%; font-size: 9px; font-weight: 900; display: inline-flex; align-items: center; justify-content: center; }

.nav-review-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.nav-row { display: flex; gap: 4px; flex: 1; }
.nav-row .btn { flex: 1; }

.analysis-progress { margin-bottom: 6px; font-size: 0.76rem; color: var(--muted); }
.progress-bar { height: 5px; background: var(--bg); border-radius: 3px; overflow: hidden; margin-top: 3px; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); transition: width 0.2s; }

.pgn-row { display: flex; gap: 5px; }
.pgn-row input {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 0.78rem;
}
.turn-status-line { font-size: 0.75rem; color: var(--muted); margin-top: 5px; text-align: right; }

.hidden { display: none !important; }

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 16px; text-align: center;
}
.promo-choices { display: flex; gap: 10px; margin-top: 12px; }
.promo-btn {
  font-size: 36px; padding: 8px 14px; border-radius: 6px;
  background: var(--surface-2); border: 1px solid var(--border); cursor: pointer;
}
.promo-btn:hover { background: var(--primary); }

.game-over-box { min-width: 300px; max-width: 360px; padding: 20px; }
.game-over-icon { font-size: 40px; margin-bottom: 6px; }
.game-over-box h2 { font-size: 1.3rem; margin-bottom: 4px; }
.game-over-sub { color: var(--muted); font-size: 0.9rem; margin-bottom: 14px; }
.game-over-actions { display: flex; flex-direction: column; gap: 7px; }

@media (max-width: 980px) {
  .app { padding: 8px 10px; }
  .topbar { flex-wrap: wrap; gap: 8px; }
  .workspace { grid-template-columns: 1fr; gap: 12px; }
  .board-section { justify-content: center; gap: 6px; }
  .eval-bar-wrap { width: 24px; padding: 28px 0; }
  .eval-bar { width: 16px; }
  .board-wrapper { width: min(calc(100vw - 56px), 480px); height: min(calc(100vw - 56px), 480px); }
  .sidebar { max-height: none; overflow-y: visible; }
  .board, .square { touch-action: manipulation; }
}

@media (max-width: 600px) {
  .brand h1 { font-size: 0.98rem; }
  .topbar-right { width: 100%; justify-content: space-between; }
  .bot-settings { grid-template-columns: 1fr; }
  .review-box { grid-template-columns: 1fr; }
  .breakdown-table { grid-template-columns: 1fr 1fr; }
  .nav-review-row { flex-direction: column; align-items: stretch; }
  .btn { padding: 8px 10px; }
}
