:root {
  --light: #eeeed2;
  --dark: #769656;
  --sel: #f6f669;
  --bg: #2b2b2b;
  --panel: #363636;
  --text: #e8e8e8;
  --muted: #a8a8a8;
  --accent: #4a90d9;
  --black-piece: #58a6ff;
  --black-piece-stroke: #041326;
  --warn: #d9534f;
  --square: clamp(38px, calc((100vw - 40px) / 8), 60px);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  padding: 16px 24px;
  border-bottom: 1px solid #444;
}
header h1 { margin: 0 0 4px; }
.tagline { margin: 0; color: var(--muted); }
.banner { padding: 8px 12px; border-radius: 6px; margin-top: 10px; }
.banner.warn { background: #4a2c2c; color: #ffd6d6; border: 1px solid var(--warn); }
code { background: #1f1f1f; padding: 1px 5px; border-radius: 4px; }

main {
  display: flex;
  flex-wrap: wrap;
  gap: 28px;
  padding: 24px;
  align-items: flex-start;
}

/* Board */
.board {
  display: grid;
  grid-template-columns: repeat(8, var(--square));
  grid-template-rows: repeat(8, var(--square));
  border: 3px solid #1f1f1f;
  width: calc(var(--square) * 8);
  height: calc(var(--square) * 8);
  position: relative;
}
.square {
  width: var(--square);
  height: var(--square);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--square) * 0.72);
  line-height: 1;
  cursor: pointer;
  user-select: none;
  position: relative;
  z-index: 1;
}
.square.light { background: var(--light); }
.square.dark { background: var(--dark); }
.square.hover-target:hover { outline: 3px solid var(--sel); outline-offset: -3px; }
.square .coord {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 10px;
  color: rgba(0,0,0,0.4);
}

/* Pieces: solid glyphs recolored by side for contrast */
.piece { pointer-events: none; }
.piece.w { color: #ffffff; text-shadow: 0 0 2px #000, 0 0 2px #000, 0 1px 2px #000; }
.piece.b {
  color: var(--black-piece);
  text-shadow: 0 0 2px var(--black-piece-stroke), 0 1px 2px #000;
}

.board-arrow-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 4;
  overflow: visible;
}
.engine-arrow {
  fill: none;
  stroke-linecap: round;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.55));
}
.engine-arrow.white { stroke: #58a6ff; }
.engine-arrow.black { stroke: #ff9f43; }
.engine-arrow-label {
  font-size: 10px;
  font-weight: 800;
  paint-order: stroke;
  stroke: #111;
  stroke-width: 3px;
  stroke-linejoin: round;
}
.engine-arrow-label.white { fill: #b9dcff; }
.engine-arrow-label.black { fill: #ffd8a8; }

.board-controls { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }
.orientation, .castling { margin-top: 12px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.castling label, .orientation label { color: var(--text); }

button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid #555;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); font-weight: 600; padding: 10px 18px; }
button.link { background: none; border: none; color: var(--accent); padding: 2px 4px; }

/* Side panel */
.side-panel { flex: 1; min-width: 320px; max-width: 560px; }
.palette { display: flex; flex-direction: column; gap: 6px; background: var(--panel); padding: 10px; border-radius: 8px; }
.palette-row { display: flex; gap: 6px; }
.pal-btn {
  width: 48px; height: 48px;
  font-size: 30px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  background: #2d2d2d;
}
.pal-btn.eraser { width: auto; font-size: 14px; }
.pal-btn.selected { outline: 3px solid var(--sel); outline-offset: -1px; }
.hint { color: var(--muted); font-size: 13px; }

.status { min-height: 20px; margin: 10px 0; color: var(--muted); }
.status.error { color: #ffb0b0; }

.analysis-tools {
  display: grid;
  gap: 10px;
  margin: 10px 0 16px;
}
.arrow-legend {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 13px;
}
.swatch {
  display: inline-block;
  width: 14px;
  height: 4px;
  border-radius: 999px;
  margin-right: 5px;
  vertical-align: middle;
}
.swatch.white { background: #58a6ff; }
.swatch.black { background: #ff9f43; }
.history {
  background: rgba(0,0,0,0.18);
  border: 1px solid #454545;
  border-radius: 8px;
  padding: 10px 12px;
}
.history h2 {
  font-size: 14px;
  margin: 0 0 6px;
}
.history .sub { color: var(--muted); font-weight: 400; font-size: 12px; }
.history-list { margin: 0; padding-left: 20px; color: var(--muted); font-size: 13px; }
.history-list li { margin: 4px 0; cursor: pointer; }
.history-list li:hover { color: var(--text); }
.history-list .history-main { color: var(--text); }
.history-list .history-time { color: var(--muted); font-size: 12px; }

.results { display: flex; gap: 20px; flex-wrap: wrap; }
.result-col { flex: 1; min-width: 230px; }
.result-col h2 { font-size: 16px; margin-bottom: 8px; }
.result-col .sub { color: var(--muted); font-weight: 400; font-size: 13px; }
.moves { margin: 0; padding-left: 22px; }
.moves li { margin-bottom: 8px; }
.moves .san { font-weight: 700; font-size: 16px; }
.moves .uci { color: var(--muted); }
.moves .eval { float: right; font-variant-numeric: tabular-nums; }
.moves .eval.good { color: #8bd17c; }
.moves .eval.bad { color: #e08585; }
.moves .pv { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.note { color: var(--muted); font-style: italic; }

/* Upload + reference */
.upload-box { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.file-label { cursor: pointer; }
.upload-btn { width: auto; font-size: 14px; padding: 8px 14px; }
.hint.inline { margin: 0; }
.reference { margin: 0 0 14px; }
.reference img { max-width: 320px; width: 100%; border: 2px solid #1f1f1f; border-radius: 4px; display: block; }
.reference figcaption { color: var(--muted); font-size: 13px; margin-top: 4px; }

/* Occupancy hint dot (no-ML fallback) */
.square.occupied::after {
  content: "";
  position: absolute;
  width: 22%;
  height: 22%;
  border-radius: 50%;
  background: rgba(74, 144, 217, 0.55);
  box-shadow: 0 0 0 2px rgba(0,0,0,0.25);
}

@media (max-width: 760px) {
  header {
    padding: 12px 14px;
  }
  header h1 {
    font-size: 22px;
  }
  main {
    display: block;
    padding: 12px;
  }
  .board-area,
  .side-panel {
    width: 100%;
    max-width: none;
    min-width: 0;
  }
  .board {
    margin: 0 auto;
  }
  .board-controls,
  .orientation,
  .castling {
    justify-content: center;
    gap: 8px;
    font-size: 13px;
  }
  .board-controls button {
    flex: 1 1 30%;
    padding: 8px 6px;
  }
  .side-panel {
    margin-top: 16px;
  }
  .upload-box {
    align-items: stretch;
  }
  .upload-btn,
  button.primary {
    width: 100%;
    justify-content: center;
  }
  .reference {
    display: grid;
    grid-template-columns: 112px 1fr;
    gap: 10px;
    align-items: center;
    background: rgba(0,0,0,0.16);
    padding: 8px;
    border-radius: 8px;
  }
  .reference[hidden] { display: none; }
  .reference img {
    max-width: 112px;
  }
  .reference figcaption {
    margin: 0;
  }
  .palette {
    padding: 8px;
  }
  .palette-row {
    flex-wrap: wrap;
  }
  .pal-btn {
    width: 42px;
    height: 42px;
    font-size: 26px;
  }
  .results {
    gap: 10px;
  }
  .result-col {
    min-width: 100%;
  }
}
