:root {
  --bg: #161512;
  --panel: #262421;
  --panel-2: #302e2b;
  --text: #e8e6e3;
  --muted: #9b9893;
  --accent: #81b64c;
  --accent-2: #5d9a2e;
  --light: #f0d9b5;
  --dark: #b58863;
  --last: rgba(255, 221, 64, 0.42);
  --select: rgba(20, 85, 30, 0.5);
  --board: min(640px, calc(100vh - 210px), calc(100vw - 60px));
}

body.theme-green { --light: #eeeed2; --dark: #769656; }
body.theme-blue { --light: #dee3e6; --dark: #8ca2ad; }
body.theme-marble { --light: #e9e4dc; --dark: #9a9087; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: radial-gradient(ellipse at top, #24221e 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  min-height: 100vh;
}

button, select, input { font: inherit; color: inherit; }

button {
  background: var(--panel-2);
  border: 1px solid #3d3a36;
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
button:hover { background: #3a3733; }
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); border-color: var(--accent-2); color: #fff; font-weight: 600; }
button.primary:hover { background: #8fc55a; }
button:disabled { opacity: 0.45; cursor: default; }

select, input[type="text"] {
  background: #1d1c1a;
  border: 1px solid #3d3a36;
  border-radius: 6px;
  padding: 6px 8px;
}

/* ------------------------------------------------------------ header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 22px;
  border-bottom: 1px solid #2c2a27;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo { font-size: 40px; line-height: 1; color: var(--accent); font-family: "Segoe UI Symbol", "DejaVu Sans", serif; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: 0.3px; }
.brand p { margin: 0; font-size: 12px; color: var(--muted); }
.status {
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  min-width: 260px;
  text-align: center;
}
.status.thinking::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 8px;
  animation: pulse 1s infinite;
}
@keyframes pulse { 50% { opacity: 0.2; } }

/* ------------------------------------------------------------ layout */
.layout {
  display: flex;
  gap: 22px;
  padding: 16px 22px;
  justify-content: center;
  align-items: flex-start;
}
.board-area { display: flex; flex-direction: column; gap: 8px; }
.board-wrap { display: flex; gap: 10px; }

.side {
  width: 400px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(100vh - 100px);
}

@media (max-width: 1100px) {
  .layout { flex-direction: column; align-items: center; }
  .side { width: min(640px, 100%); max-height: none; }
}

/* ------------------------------------------------------------ board */
.board-container { position: relative; width: var(--board); height: var(--board); }

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
  user-select: none;
  touch-action: none;
}

.sq { position: relative; display: flex; align-items: center; justify-content: center; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.last::after, .sq.selected::after, .sq.hint::after, .sq.check::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
}
.sq.last::after { background: var(--last); }
.sq.selected::after { background: var(--select); }
.sq.hint::after { background: rgba(38, 194, 163, 0.45); animation: pulse 1.2s infinite; }
.sq.check::after { background: radial-gradient(circle, rgba(255, 0, 0, 0.9) 0%, rgba(231, 0, 0, 0.5) 40%, rgba(169, 0, 0, 0) 75%); }

.coord {
  position: absolute;
  font-size: calc(var(--board) * 0.021);
  font-weight: 700;
  pointer-events: none;
  z-index: 1;
}
.coord.rank { top: 3px; left: 4px; }
.coord.file { bottom: 2px; right: 5px; }
.sq.light .coord { color: var(--dark); }
.sq.dark .coord { color: var(--light); }

.piece {
  font-family: "Segoe UI Symbol", "DejaVu Sans", "Noto Sans Symbols 2", "Arial Unicode MS", serif;
  font-size: calc(var(--board) * 0.098);
  line-height: 1;
  z-index: 2;
  cursor: grab;
  transition: transform 0.08s;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.35));
}
.piece.white { color: #fdfdfd; -webkit-text-stroke: 1.4px #2a2a2a; paint-order: stroke fill; }
.piece.black { color: #1c1b1a; -webkit-text-stroke: 1px #000; }
.piece.dragging-src { opacity: 0.25; }
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%) scale(1.12);
  cursor: grabbing;
}

.dot {
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(20, 85, 30, 0.45);
  pointer-events: none;
  z-index: 3;
}
.ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: calc(var(--board) * 0.009) solid rgba(20, 85, 30, 0.5);
  pointer-events: none;
  z-index: 3;
}

.arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

/* promotion */
.promo {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  z-index: 20;
  border-radius: 4px;
}
.promo button {
  width: calc(var(--board) / 8);
  height: calc(var(--board) / 8);
  border-radius: 50%;
  background: var(--light);
  display: flex; align-items: center; justify-content: center;
  padding: 0;
}
.promo button:hover { background: #fff; }
.promo .piece { font-size: calc(var(--board) * 0.085); }

/* eval bar */
.eval-bar {
  width: 26px;
  height: var(--board);
  background: #403d39;
  border-radius: 4px;
  position: relative;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.eval-fill {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 50%;
  background: #f4f4f4;
  transition: height 0.5s ease;
}
.eval-bar.flipped .eval-fill { bottom: auto; top: 0; }
.eval-text {
  position: absolute;
  left: 0; right: 0;
  text-align: center;
  font-size: 10px;
  font-weight: 700;
  bottom: 4px;
  color: #333;
}
.eval-text.top { bottom: auto; top: 4px; color: #eee; }
.eval-bar.off { visibility: hidden; }

/* player bars */
.player-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 36px;
  min-height: 34px;
}
.player-bar .name { font-weight: 600; }
.player-bar .tag { font-size: 12px; color: var(--muted); background: var(--panel); padding: 2px 8px; border-radius: 10px; }
.player-bar .captured { font-family: "Segoe UI Symbol", "DejaVu Sans", serif; font-size: 18px; color: var(--muted); letter-spacing: -3px; }
.player-bar .adv { font-size: 13px; color: var(--muted); }
.player-bar .turn-dot { width: 10px; height: 10px; border-radius: 50%; background: transparent; }
.player-bar.active .turn-dot { background: var(--accent); box-shadow: 0 0 8px var(--accent); }

.controls { display: flex; gap: 6px; padding-left: 36px; flex-wrap: wrap; }
.controls .spacer { flex: 1; }

/* ------------------------------------------------------------ side cards */
.card {
  background: var(--panel);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.card-title { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 8px; display: flex; justify-content: space-between; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); flex: 1; }
.row.toggles label { flex-direction: row; align-items: center; gap: 5px; font-size: 13px; color: var(--text); flex: none; }
.level { display: flex; flex-direction: column; gap: 4px; font-size: 13px; margin-bottom: 8px; }
.level b { color: var(--accent); }
input[type="range"] { accent-color: var(--accent); width: 100%; }
.note { font-size: 11px; color: var(--muted); margin: 0; }
.tools input[type="text"] { flex: 1; font-size: 12px; font-family: Consolas, monospace; }

/* coach */
.coach { display: flex; flex-direction: column; min-height: 200px; flex: 1 1 auto; overflow: hidden; }
.coach-head { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #81b64c, #3f7a1f);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800;
  color: #fff;
  box-shadow: 0 0 0 2px #1c1b19;
}
.coach-head .sub { font-size: 12px; color: var(--muted); }
.coach-feed { overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding-right: 4px; flex: 1; max-height: 320px; }
.msg {
  background: var(--panel-2);
  border-left: 4px solid #555;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13.5px;
  line-height: 1.45;
  animation: slide 0.25s ease;
}
@keyframes slide { from { opacity: 0; transform: translateY(6px); } }
.msg .badge {
  display: inline-block;
  font-weight: 800;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 8px;
  color: #fff;
  margin-right: 6px;
}
.msg .tip { margin-top: 6px; font-size: 12.5px; color: #cfd8b8; }
.msg .tip.warn { color: #f3c98b; }
.msg .line { display: block; margin-top: 4px; font-family: Consolas, monospace; font-size: 12px; color: var(--muted); }
.msg .eval-change { color: var(--muted); font-size: 12px; white-space: nowrap; }
.msg .show-link { background: none; border: none; color: var(--accent); padding: 0; margin-left: 4px; font-size: 12px; text-decoration: underline; }

/* engine line */
.pv { font-family: Consolas, monospace; font-size: 13px; line-height: 1.5; color: #d4d2cf; min-height: 20px; }
.pv .score { font-weight: 700; color: var(--accent); margin-right: 8px; }
#engineStats { text-transform: none; letter-spacing: 0; }

/* move list */
.moves { max-height: 240px; display: flex; flex-direction: column; }
.move-list { overflow-y: auto; display: grid; grid-template-columns: 36px 1fr 1fr; font-size: 14px; }
.move-list .num { color: var(--muted); padding: 3px 6px; background: #2b2926; }
.move-list .mv { padding: 3px 8px; cursor: pointer; border-radius: 3px; display: flex; align-items: center; gap: 6px; }
.move-list .mv:hover { background: #3a3733; }
.move-list .mv.current { background: #4b6b2d; }
.move-list .mv .b { font-size: 10px; font-weight: 800; padding: 0 4px; border-radius: 6px; color: #fff; }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal-box { background: var(--panel); border-radius: 12px; padding: 24px 28px; width: min(460px, 92vw); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6); }
.modal-box h2 { margin: 0 0 4px; }
.modal-box p { margin: 0 0 14px; color: var(--muted); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.summary-acc { font-size: 34px; font-weight: 800; color: var(--accent); }
.summary-grid { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; font-size: 14px; margin-top: 10px; }
.summary-grid .k { display: flex; align-items: center; gap: 8px; }
.summary-grid .sw { width: 10px; height: 10px; border-radius: 50%; }
.key-moments { margin-top: 12px; font-size: 13px; color: #d4d2cf; }
.key-moments li { margin-bottom: 3px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #3a3733; padding: 10px 18px; border-radius: 8px; z-index: 200;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

.hidden { display: none !important; }
