/* ========================================
   Chess Game — Styles
   Full-viewport layout, responsive
   ======================================== */

/* ── Variables ───────────────────────── */
.chess-page {
  --nav-h: 48px;
  --pad: 16px;
  --gap: 20px;
  --sidebar-w: 260px;
  --board-border: 2px;

  /* Square size: constrained by BOTH height and width so nothing overflows */
  --sq: min(
    calc((100dvh - var(--nav-h) - var(--pad) * 2 - var(--board-border) * 2) / 8),
    calc((100vw - var(--sidebar-w) - var(--gap) - var(--pad) * 2 - var(--board-border) * 2) / 8)
  );
  --board-outer: calc(var(--sq) * 8 + var(--board-border) * 2);

  /* Board palette */
  --board-light: #c8d0e4;
  --board-dark: #5d6b92;
  --board-selected: rgba(99, 102, 241, 0.55);
  --board-lastmove-light: color-mix(in srgb, #c8d0e4 70%, #ffd54f 30%);
  --board-lastmove-dark: color-mix(in srgb, #5d6b92 70%, #ffd54f 30%);
  --board-check: rgba(239, 68, 68, 0.55);
  --board-legal: rgba(99, 102, 241, 0.35);
}

/* ── Page layout ─────────────────────── */
.chess-page {
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  line-height: 1.4;
}

/* ── Compact navigation ──────────────── */
.chess-nav {
  display: flex;
  align-items: center;
  height: var(--nav-h);
  padding: 0 20px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  gap: 16px;
}

.chess-nav-logo {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--color-accent) !important;
  letter-spacing: -0.5px;
}

.chess-nav-title {
  font-weight: 600;
  font-size: 0.8rem;
  color: var(--color-text-muted);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.chess-nav-back {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--color-text-muted) !important;
  transition: color 0.2s;
}
.chess-nav-back:hover {
  color: var(--color-accent) !important;
}

/* ── Main container ──────────────────── */
.chess-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--gap);
  padding: var(--pad);
  min-height: 0;
  overflow: hidden;
}

/* ── Board ───────────────────────────── */
.chess-board-wrapper {
  flex-shrink: 0;
}

.chess-board {
  display: grid;
  grid-template-columns: repeat(8, var(--sq));
  grid-template-rows: repeat(8, var(--sq));
  border: var(--board-border) solid rgba(255,255,255,0.08);
  border-radius: 4px;
  box-shadow:
    0 0 0 1px rgba(0,0,0,0.3),
    0 8px 32px rgba(0,0,0,0.4),
    0 2px 8px rgba(0,0,0,0.2);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.square {
  width: var(--sq);
  height: var(--sq);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--sq) * 0.72);
  cursor: pointer;
  position: relative;
  transition: background 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}

.square.light { background: var(--board-light); }
.square.dark  { background: var(--board-dark); }

.square.selected { background: var(--board-selected) !important; }

.square.last-move-light { background: var(--board-lastmove-light); }
.square.last-move-dark  { background: var(--board-lastmove-dark); }

.square.check { background: var(--board-check) !important; }

/* Legal move indicators */
.square.legal-move::after {
  content: '';
  position: absolute;
  width: 26%;
  height: 26%;
  background: var(--board-legal);
  border-radius: 50%;
  pointer-events: none;
}

.square.legal-capture::after {
  content: '';
  position: absolute;
  width: 82%;
  height: 82%;
  border: 3.5px solid var(--board-legal);
  border-radius: 50%;
  background: transparent;
  pointer-events: none;
}

.square:hover:not(.selected) {
  filter: brightness(1.08);
}

/* Piece */
.piece {
  line-height: 1;
  text-shadow: 0 1px 4px rgba(0,0,0,0.35);
  pointer-events: none;
  z-index: 1;
}

/* Board coordinates */
.coord-rank,
.coord-file {
  position: absolute;
  font-size: max(9px, calc(var(--sq) * 0.14));
  font-weight: 700;
  opacity: 0.55;
  pointer-events: none;
  font-family: var(--font-sans);
}

.coord-rank { top: 2px; left: 3px; }
.coord-file { bottom: 1px; right: 3px; }

.square.light .coord-rank,
.square.light .coord-file { color: var(--board-dark); }
.square.dark .coord-rank,
.square.dark .coord-file { color: var(--board-light); }

/* ── Sidebar ─────────────────────────── */
.chess-sidebar {
  width: var(--sidebar-w);
  height: var(--board-outer);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  flex-shrink: 0;
}

/* Status bar */
.game-status {
  text-align: center;
  font-weight: 600;
  font-size: 0.88rem;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.game-status .turn-indicator {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
  border: 2px solid #555;
}
.turn-indicator.white { background: #fff; }
.turn-indicator.black { background: #222; }

/* Captured pieces */
.captured-pieces {
  padding: 2px 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
  min-height: 24px;
}

.captured-row {
  font-size: 1.05rem;
  letter-spacing: 1px;
  line-height: 1.2;
}

.material-score {
  font-size: 0.72rem;
  color: var(--color-accent);
  font-weight: 600;
  margin-left: auto;
}

/* Move list */
.move-list-wrapper {
  background: rgba(0,0,0,0.2);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.move-list-header {
  padding: 8px 14px;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  font-weight: 600;
  font-size: 0.7rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 1.5px;
  flex-shrink: 0;
}

.move-list {
  padding: 6px 10px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.7;
  flex: 1;
  min-height: 0;
}

.move-list::-webkit-scrollbar {
  width: 5px;
}
.move-list::-webkit-scrollbar-track {
  background: transparent;
}
.move-list::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
}

.move-row {
  display: flex;
  gap: 4px;
  padding: 1px 4px;
  border-radius: 4px;
}
.move-row:hover {
  background: rgba(255,255,255,0.03);
}

.move-number {
  color: var(--color-text-muted);
  min-width: 28px;
  text-align: right;
  margin-right: 6px;
  opacity: 0.5;
}

.move-white, .move-black {
  min-width: 50px;
  padding: 0 4px;
  border-radius: 3px;
  cursor: default;
}

.move-white.current, .move-black.current {
  background: var(--color-accent);
  color: #fff;
}

/* Buttons */
.chess-controls {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.chess-btn {
  flex: 1;
  padding: 8px 4px;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 6px;
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
  white-space: nowrap;
}

.chess-btn:hover {
  background: rgba(255,255,255,0.08);
  border-color: var(--color-accent);
}
.chess-btn:active {
  transform: scale(0.97);
}

.chess-btn.primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}
.chess-btn.primary:hover {
  background: var(--color-accent-hover);
}

.chess-btn.danger {
  border-color: rgba(239, 68, 68, 0.3);
  color: #ef4444;
}
.chess-btn.danger:hover {
  background: rgba(239, 68, 68, 0.1);
  border-color: #ef4444;
}

/* Promotion modal */
.promo-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.promo-dialog {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  padding: 20px 24px;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0,0,0,0.5);
}

.promo-dialog h3 {
  margin-bottom: 14px;
  font-size: 0.9rem;
  color: var(--color-text-muted);
  font-weight: 500;
}

.promo-pieces {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.promo-piece {
  width: 56px;
  height: 56px;
  font-size: 2.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface-2);
  border: 2px solid var(--color-border);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.promo-piece:hover {
  border-color: var(--color-accent);
  background: var(--color-accent-subtle);
  transform: scale(1.08);
}

/* ── Responsive — Mobile ─────────────── */
@media (max-width: 768px) {
  .chess-page {
    --sq: calc((100vw - var(--pad) * 2 - var(--board-border) * 2) / 8);
    height: auto;
    overflow: auto;
  }

  .chess-container {
    flex-direction: column;
    align-items: center;
    padding: 12px;
    height: auto;
    overflow: visible;
  }

  .chess-sidebar {
    width: var(--board-outer);
    max-width: 100%;
    height: auto;
  }

  .move-list {
    max-height: 180px;
  }

  .chess-nav-title {
    display: none;
  }
}

@media (max-width: 480px) {
  .chess-btn {
    font-size: 0.7rem;
    padding: 7px 2px;
  }

  .promo-piece {
    width: 48px;
    height: 48px;
    font-size: 1.8rem;
  }
}

/* Very short viewports — allow scrolling */
@media (max-height: 500px) {
  .chess-page {
    height: auto;
    overflow: auto;
  }
  .chess-container {
    height: auto;
  }
}
