/* ============== THEME ============== */
:root {
  --bg-0: #0a0a14;
  --bg-1: #11111d;
  --bg-2: #1a1a2e;
  --panel: rgba(20, 22, 38, 0.75);
  --panel-border: rgba(255, 255, 255, 0.08);
  --text: #e8e8f0;
  --text-dim: #8a8aa3;
  --accent: #f5b942;        /* warm gold */
  --accent-glow: #f5b94266;
  --neon-cyan: #00e5ff;
  --neon-cyan-glow: #00e5ff55;
  --light-sq: #ead7b4;
  --light-sq-hi: #f5e9c8;
  --dark-sq: #6b4a2b;
  --dark-sq-hi: #8a5e36;
  --move-dot: rgba(0, 229, 255, 0.55);
  --capture-ring: rgba(245, 185, 66, 0.85);
  --check-glow: rgba(255, 70, 90, 0.9);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  --radius: 14px;
  --board-size: min(560px, 92vmin, 78vh);
}

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

html, body {
  height: 100%;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text);
  background: radial-gradient(ellipse at top, var(--bg-2) 0%, var(--bg-0) 70%);
  overflow-x: hidden;
  user-select: none;
}

.bg-glow {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(circle at 20% 20%, rgba(245, 185, 66, 0.08), transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(0, 229, 255, 0.06), transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* ============== TOPBAR ============== */
.topbar {
  position: relative;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 32px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(10, 10, 20, 0.6);
  backdrop-filter: blur(8px);
}

.logo {
  font-family: 'Cinzel', serif;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 4px;
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent-glow);
}
.logo span { color: var(--text-dim); font-weight: 500; }
.logo .logo-accent {
  color: var(--accent);
  text-shadow: 0 0 18px var(--accent-glow);
}

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

.status-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 100px;
  font-size: 13px;
  letter-spacing: 1px;
}
.status-pill .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--neon-cyan);
  box-shadow: 0 0 12px var(--neon-cyan-glow);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ============== STAGE ============== */
.stage {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 24px;
  padding: 24px 32px;
  max-width: 1400px;
  margin: 0 auto;
  align-items: start;
}

/* ============== RESPONSIVE ============== */
/* Touch-friendly: no tap-highlight, no double-tap zoom delay */
.square, .mini-btn, .big-btn, .setup-btn, .promo-piece {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Tablet & small laptop (≤1000px) ---- */
@media (max-width: 1000px) {
  .stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 16px;
  }
  .board-wrap { order: 1; }
  .side-panel { order: 2; width: 100%; max-height: none !important; }
  /* side panel splits into 2 columns: captured pieces | move history */
  .side-panel.left {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    align-items: start;
  }
  .side-panel.left .panel-label { grid-column: 1 / -1; }
  .side-panel.left .moves-box {
    grid-column: 2;
    grid-row: 2 / -1;
    margin-top: 0;
  }
  .moves-list { max-height: 150px; }
}

/* ---- Smartphone (≤640px) ---- */
@media (max-width: 640px) {
  .topbar {
    padding: 10px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .logo { font-size: 20px; letter-spacing: 2px; }
  .topbar-right { gap: 8px; }
  .status-pill { font-size: 11px; padding: 6px 10px; gap: 6px; }
  .mini-btn { width: 36px; height: 32px; font-size: 14px; border-radius: 8px; }
  .board-frame { padding: 10px; border-radius: 12px; }
  .board-coords-files { padding: 0 10px; }
  .stage { padding: 12px 8px; gap: 12px; }
  .side-panel { padding: 12px; border-radius: 12px; }
  .side-panel.left {
    grid-template-columns: 1fr;
  }
  .side-panel.left .moves-box { grid-column: auto; grid-row: auto; margin-top: 12px; }
  .moves-list { max-height: 120px; }
  .board-legend { font-size: 10px; gap: 12px; }
  .captured-row { font-size: 18px; }
  .captured-row svg { width: 18px; height: 18px; }
  .rank-label, .file-label { font-size: 8px; }
  /* setup & endgame screens */
  .setup-card { padding: 26px 16px; }
  .setup-logo { font-size: 32px; letter-spacing: 2px; }
  .setup-tagline { font-size: 12px; margin-bottom: 20px; }
  .setup-btn { font-size: 12px; padding: 10px 4px; }
  .setup-section { margin-bottom: 16px; }
  .endgame-card { padding: 28px 24px; }
  .endgame-title { font-size: 26px; letter-spacing: 2px; }
  .endgame-emoji { font-size: 56px; }
}

/* ============== PANELS ============== */
.side-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 18px;
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
  max-height: 640px;
  overflow-y: auto;
}

.panel-label {
  font-family: 'Cinzel', serif;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--accent);
  text-transform: uppercase;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--panel-border);
}

.sub-label {
  font-size: 10px;
  color: var(--text-dim);
  letter-spacing: 1.5px;
  text-transform: uppercase;
  margin: 14px 0 8px;
}

.captured-section { margin-bottom: 10px; }
.cap-label { font-size: 11px; color: var(--text-dim); margin-bottom: 4px; }
.captured-row {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  min-height: 28px;
  padding: 6px;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 8px;
  font-size: 22px;
  line-height: 1;
}
.captured-row svg { width: 22px; height: 22px; }

.material-adv {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  padding: 6px;
  margin: 6px 0;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
}
.material-adv.pos { color: #4ade80; }
.material-adv.neg { color: #f87171; }

.moves-box { margin-top: 16px; }
.moves-list {
  list-style: none;
  max-height: 220px;
  overflow-y: auto;
  font-size: 12px;
  padding-right: 4px;
}
.moves-list li {
  display: flex;
  justify-content: space-between;
  padding: 4px 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  color: var(--text-dim);
}
.moves-list li.current {
  background: rgba(245, 185, 66, 0.12);
  color: var(--accent);
  font-weight: 600;
}
.moves-list::-webkit-scrollbar { width: 4px; }
.moves-list::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 2px; }

/* ============== BUTTONS ============== */
.big-btn {
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  background: linear-gradient(135deg, var(--accent), #d49a2a);
  border: none;
  color: #1a1a2e;
  font-family: 'Cinzel', serif;
  font-weight: 700;
  letter-spacing: 1.5px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 4px 20px var(--accent-glow);
}
.big-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 24px var(--accent-glow); }
.big-btn:active { transform: translateY(0); }

/* ============== BOARD ============== */
.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.board-frame {
  position: relative;
  padding: 18px;
  background: linear-gradient(145deg, #2a1f15, #4a3520, #2a1f15);
  border-radius: 16px;
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.7),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  border: 2px solid #1a1208;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: var(--board-size);
  height: var(--board-size);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.3);
}

.board.flipped { transform: rotate(180deg); }
.board.flipped .square { transform: rotate(180deg); }

/* rank/file labels */
.board-coords-files {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  width: var(--board-size);
  padding: 0 18px;
  font-size: 11px;
  color: var(--text-dim);
  font-weight: 600;
}
.board-coords-files span { text-align: center; }

.rank-label {
  position: absolute;
  font-size: 10px;
  font-weight: 700;
  color: rgba(0, 0, 0, 0.4);
  pointer-events: none;
  top: 3px; left: 4px;
}
.board.flipped .rank-label { transform: rotate(180deg); }
.file-label {
  position: absolute;
  font-size: 10px;
  font-weight: 700;
  color: rgba(0, 0, 0, 0.4);
  pointer-events: none;
  bottom: 3px; right: 4px;
}
.board.flipped .file-label { transform: rotate(180deg); }
.board-wrap.flipped .board-coords-files {
  transform: rotate(180deg);
}

/* ============== SQUARES ============== */
.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}
.square.light {
  background:
    linear-gradient(135deg, var(--light-sq) 0%, var(--light-sq-hi) 100%);
}
.square.dark {
  background:
    linear-gradient(135deg, var(--dark-sq) 0%, var(--dark-sq-hi) 100%);
}
.square.selected {
  background:
    radial-gradient(circle, rgba(245, 185, 66, 0.6), transparent 70%) !important;
}
.square.last-move::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(245, 185, 66, 0.32);
  pointer-events: none;
}
.square.check::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, var(--check-glow), transparent 60%);
  animation: checkPulse 1.4s ease-in-out infinite;
}
@keyframes checkPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.square .move-dot {
  position: absolute;
  width: 26%; height: 26%;
  border-radius: 50%;
  background: var(--move-dot);
  box-shadow: 0 0 12px var(--move-dot);
  pointer-events: none;
  animation: dotIn 0.18s ease-out;
}
.square .capture-ring {
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: 3px solid var(--capture-ring);
  background: rgba(245, 185, 66, 0.12);
  pointer-events: none;
  animation: ringIn 0.18s ease-out;
}
@keyframes dotIn {
  from { transform: scale(0); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes ringIn {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* ============== PIECES ============== */
.piece {
  width: 86%;
  height: 86%;
  cursor: grab;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.5));
  transition: transform 0.15s;
  pointer-events: auto;
  z-index: 2;
}
.piece:active { cursor: grabbing; }
.piece:hover { transform: scale(1.05); }
.piece.dragging { opacity: 0.5; }

.square .piece.moving {
  position: absolute;
  z-index: 100;
  transition: left 0.22s cubic-bezier(0.4, 0, 0.2, 1), top 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
}
.square .piece.captured {
  animation: capturePop 0.3s ease-out forwards;
}
@keyframes capturePop {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); }
  100% { transform: scale(0.7); opacity: 0; }
}

/* ============== BOARD TOOLS & LEGEND ============== */
.board-tools {
  display: flex;
  gap: 8px;
}
.mini-btn {
  width: 40px;
  height: 36px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--text);
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.2s;
}
.mini-btn:hover {
  border-color: var(--accent);
  background: rgba(245, 185, 66, 0.1);
  transform: translateY(-2px);
}
.mini-btn.gold {
  background: linear-gradient(135deg, var(--accent), #d49a2a);
  color: #1a1a2e;
  border: none;
  box-shadow: 0 0 14px var(--accent-glow);
}
.mini-btn.gold:hover { background: linear-gradient(135deg, #ffc95e, #d49a2a); }

.board-legend {
  display: flex;
  gap: 16px;
  margin-top: 10px;
  font-size: 11px;
  color: var(--text-dim);
}
.board-legend .dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 5px;
  background: var(--move-dot);
  vertical-align: middle;
}
.board-legend .dot.capture { background: var(--capture-ring); }
.board-legend .dot.check { background: var(--check-glow); }

/* ============== PROMOTION MODAL ============== */
.promotion-modal {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  z-index: 50;
  backdrop-filter: blur(6px);
}
.promotion-modal.show { display: flex; }
.promo-card {
  background: linear-gradient(145deg, #1a1a2e, #2a2a44);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 0 30px var(--accent-glow);
  text-align: center;
}
.promo-title {
  font-family: 'Cinzel', serif;
  color: var(--accent);
  margin-bottom: 14px;
  letter-spacing: 2px;
}
.promo-pieces {
  display: flex;
  gap: 8px;
}
.promo-piece {
  width: 64px; height: 64px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
.promo-piece:hover {
  background: rgba(245, 185, 66, 0.2);
  border-color: var(--accent);
  transform: translateY(-2px);
}
.promo-piece svg { width: 80%; height: 80%; }

/* ============== SETUP SCREEN ============== */
.setup-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at top, var(--bg-2) 0%, var(--bg-0) 70%);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
  overflow-y: auto;
}
.setup-overlay.show { display: flex; }

.setup-card {
  background: linear-gradient(145deg, #1a1a2e, #23233c);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 36px 44px;
  width: min(460px, 100%);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7), 0 0 60px var(--accent-glow);
  text-align: center;
  animation: popIn 0.45s cubic-bezier(0.3, 1.5, 0.5, 1);
}

.setup-logo {
  font-family: 'Cinzel', serif;
  font-size: 42px;
  font-weight: 700;
  letter-spacing: 4px;
  color: var(--accent);
  text-shadow: 0 0 24px var(--accent-glow);
  margin-bottom: 6px;
}
.setup-logo .logo-accent {
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent-glow);
}
.setup-tagline {
  color: var(--text-dim);
  font-size: 13px;
  letter-spacing: 1px;
  margin-bottom: 26px;
}

.setup-section { margin-bottom: 22px; }
.setup-label {
  font-family: 'Cinzel', serif;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--text-dim);
  text-transform: uppercase;
  margin-bottom: 10px;
}
.setup-options {
  display: flex;
  gap: 8px;
  background: rgba(0, 0, 0, 0.3);
  padding: 5px;
  border-radius: 12px;
}
.setup-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-family: inherit;
  font-size: 13px;
  padding: 11px 6px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}
.setup-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }
.setup-btn.active {
  background: linear-gradient(135deg, var(--accent), #d49a2a);
  color: #1a1a2e;
  font-weight: 600;
  box-shadow: 0 0 18px var(--accent-glow);
}

.setup-start {
  margin-top: 8px;
  padding: 14px;
  font-size: 15px;
  letter-spacing: 2px;
}

/* ============== ONLINE ============== */
.online-actions {
  display: flex;
  gap: 8px;
}
.online-join {
  display: flex;
  gap: 6px;
}
.online-input {
  flex: 1;
  min-width: 0;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  letter-spacing: 2px;
  padding: 10px 12px;
  border-radius: 8px;
  text-transform: uppercase;
  outline: none;
  transition: border-color 0.2s;
}
.online-input::placeholder {
  color: var(--text-dim);
  letter-spacing: 0.5px;
  font-size: 12px;
  text-transform: none;
}
.online-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 12px var(--accent-glow);
}
.setup-btn.ghost-btn {
  flex: none;
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
}
.setup-btn.ghost-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}
.online-code {
  margin-top: 10px;
  padding: 14px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px dashed var(--accent);
  border-radius: 12px;
}
.online-code-label {
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--text-dim);
  text-transform: uppercase;
  margin-bottom: 6px;
}
.online-code-value {
  font-family: 'Cinzel', serif;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 6px;
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent-glow);
  user-select: all;
}
.online-code-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 8px;
}
.online-status {
  min-height: 18px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
}

/* ============== ENDGAME ============== */
.endgame-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(10px);
  animation: fadeIn 0.3s ease-out;
}
.endgame-modal.show { display: flex; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.endgame-card {
  background: linear-gradient(145deg, #1a1a2e, #2a2a44);
  border: 2px solid var(--accent);
  border-radius: 18px;
  padding: 40px 56px;
  text-align: center;
  box-shadow: 0 0 60px var(--accent-glow);
  animation: popIn 0.4s cubic-bezier(0.3, 1.5, 0.5, 1);
}
@keyframes popIn {
  from { transform: scale(0.6); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.endgame-emoji {
  font-size: 72px;
  margin-bottom: 8px;
}
.endgame-title {
  font-family: 'Cinzel', serif;
  font-size: 32px;
  color: var(--accent);
  letter-spacing: 3px;
  margin-bottom: 4px;
}
.endgame-sub {
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 20px;
}
