/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242836;
  --border: #2e3348;
  --text: #e4e6f0;
  --text-dim: #8b8fa3;
  --accent: #6c8cff;
  --accent-hover: #8ba4ff;
  --light-sq: #e8dcc8;
  --dark-sq: #a0845c;
  --highlight: rgba(108, 140, 255, 0.35);
  --move-hint: rgba(108, 140, 255, 0.25);
  --capture-hint: rgba(239, 83, 80, 0.35);
  --last-move: rgba(255, 230, 100, 0.3);
  --check: rgba(239, 83, 80, 0.5);
  --sq-size: min(calc((100vh - 220px) / 8), calc((100vw - 400px) / 8), 80px);
  --radius: 8px;
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  align-items: center;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  max-width: 1100px;
  padding: 12px 16px;
  gap: 10px;
}

/* ── Header ── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}
header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.controls {
  display: flex;
  gap: 8px;
  align-items: center;
}
.controls button, .controls select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 0.82rem;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.controls button:hover, .controls select:hover {
  background: var(--surface2);
  border-color: var(--accent);
}
.controls button:active { transform: scale(0.97); }

/* ── Main Layout ── */
main {
  display: flex;
  gap: 16px;
  flex: 1;
  min-height: 0;
  align-items: flex-start;
  justify-content: center;
}

/* ── Board ── */
.board-container {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.player-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--surface);
  border-radius: var(--radius);
  min-height: 36px;
}
.player-bar .player-name {
  font-weight: 600;
  font-size: 0.85rem;
}
.captured-pieces {
  font-size: 0.95rem;
  letter-spacing: 1px;
  opacity: 0.8;
}

.board-wrapper {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: auto 1fr auto;
  gap: 0;
}

.file-labels {
  display: flex;
  grid-column: 2;
}
.file-labels.top { grid-row: 1; }
.file-labels.bottom { grid-row: 3; }
.file-labels span {
  width: var(--sq-size);
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-dim);
  padding: 2px 0;
}

.rank-labels {
  display: flex;
  flex-direction: column;
  grid-row: 2;
}
.rank-labels.left { grid-column: 1; }
.rank-labels.right { grid-column: 3; }
.rank-labels span {
  height: var(--sq-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  color: var(--text-dim);
  width: 20px;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, var(--sq-size));
  grid-template-rows: repeat(8, var(--sq-size));
  border: 2px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  user-select: none;
  grid-column: 2;
  grid-row: 2;
  position: relative;
}

/* ── Squares ── */
.square {
  width: var(--sq-size);
  height: var(--sq-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--sq-size) * 0.78);
  cursor: pointer;
  position: relative;
  transition: background 0.08s;
}
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }
.square.selected { background: var(--highlight) !important; }
.square.last-move { background: var(--last-move) !important; }
.square.check { background: var(--check) !important; }
.square.drag-over { box-shadow: inset 0 0 0 3px var(--accent); }

/* Move hints (dots) */
.square.hint::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  background: var(--move-hint);
  border-radius: 50%;
  pointer-events: none;
}
.square.capture-hint::after {
  content: '';
  position: absolute;
  width: 90%;
  height: 90%;
  border: 4px solid var(--capture-hint);
  border-radius: 50%;
  pointer-events: none;
}

/* ── Piece dragging ── */
.piece-dragging {
  position: fixed;
  font-size: calc(var(--sq-size) * 0.85);
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.4));
}

/* ── Side Panel ── */
.side-panel {
  width: 260px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  height: calc(var(--sq-size) * 8 + 80px);
}

.status-bar {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 14px;
  text-align: center;
}
.status {
  font-weight: 600;
  font-size: 0.95rem;
}
.status.checkmate { color: #ef5350; }
.status.draw { color: #ffb74d; }
.status.stalemate { color: #ffb74d; }

.thinking {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--accent);
}
.thinking.hidden { display: none; }
.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.move-history {
  flex: 1;
  background: var(--surface);
  border-radius: var(--radius);
  overflow-y: auto;
  padding: 6px 0;
}
.move-history table {
  width: 100%;
  border-collapse: collapse;
}
.move-history td {
  padding: 4px 10px;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background 0.1s;
}
.move-history td:first-child {
  width: 32px;
  text-align: center;
  color: var(--text-dim);
  cursor: default;
}
.move-history td:hover { background: var(--surface2); }
.move-history td.active { background: var(--highlight); font-weight: 600; }

.pgn-section {
  padding-top: 4px;
}
.pgn-section button {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  font-size: 0.82rem;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}
.pgn-section button:hover { background: var(--surface2); }

/* ── Promotion Modal ── */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 28px;
  text-align: center;
}
.modal-content h3 {
  margin-bottom: 16px;
  font-size: 1rem;
}
.promo-pieces {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.promo-piece {
  font-size: 48px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: background 0.15s, transform 0.1s;
}
.promo-piece:hover {
  background: var(--highlight);
  transform: scale(1.1);
}

/* ── Responsive ── */
@media (max-width: 700px) {
  :root {
    --sq-size: min(calc((100vw - 32px) / 8), 56px);
  }
  main {
    flex-direction: column;
    align-items: center;
  }
  .side-panel {
    width: 100%;
    max-width: calc(var(--sq-size) * 8 + 40px);
    height: 200px;
  }
  .controls {
    flex-wrap: wrap;
  }
}
