/* ============================= base / theme ============================= */

:root {
  --bg: #0f1220;
  --bg2: #1a1f35;
  --panel: rgba(255, 255, 255, 0.05);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #e7eaf3;
  --muted: #9aa3b8;
  --accent: #22d3ee;
  --accent2: #a78bfa;
  --danger: #f87171;
  --gold: #fbbf24;
  --sq-light: #dee3ea;
  --sq-dark: #5d7290;
  --piece-font: "Segoe UI Symbol", "Noto Sans Symbols 2", "DejaVu Sans", sans-serif;
}

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

html, body { height: 100%; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: radial-gradient(120% 120% at 20% 0%, var(--bg2) 0%, var(--bg) 60%);
  color: var(--text);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

#app { height: 100%; display: flex; align-items: center; justify-content: center; }

.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 24px;
  width: 100%;
  max-width: 640px;
  max-height: 100%;
  overflow-y: auto;
}
.screen.active { display: flex; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; font-size: 14px; }

h1.title { font-size: clamp(34px, 7vw, 56px); letter-spacing: 1px; text-align: center; }
h1.title .knight { color: var(--accent); text-shadow: 0 0 24px rgba(34, 211, 238, 0.6); }
.tagline { color: var(--muted); font-size: 18px; text-align: center; }
h2 { font-size: 28px; }

/* ============================= buttons / cards ========================== */

.btn {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 18px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.25); }
.btn:active { transform: scale(0.97); }
.btn.big { font-size: 20px; padding: 14px 28px; min-width: 220px; }
.btn.small { font-size: 14px; padding: 6px 14px; }
.btn.primary {
  background: linear-gradient(135deg, rgba(34, 211, 238, 0.25), rgba(167, 139, 250, 0.25));
  border-color: rgba(34, 211, 238, 0.5);
}
.btn.primary:hover { border-color: var(--accent); }
.btn.back { border: none; background: none; color: var(--muted); }
.btn.choice.selected {
  border-color: var(--accent);
  background: rgba(34, 211, 238, 0.15);
  box-shadow: 0 0 12px rgba(34, 211, 238, 0.25);
}

.menu-buttons { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.choice-row { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

.card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 20px 24px;
  width: 100%;
  max-width: 440px;
}
.rules h3 { margin-bottom: 10px; color: var(--accent); }
.rules li { margin: 6px 0 6px 18px; color: var(--muted); }
.rules li b { color: var(--text); }

/* =========================== multiplayer setup ========================== */

.mp-box { display: flex; flex-direction: column; gap: 12px; align-items: center; text-align: center; }
.room-code {
  font-size: 44px;
  font-weight: 700;
  letter-spacing: 12px;
  padding-left: 12px; /* offset letter-spacing */
  color: var(--gold);
  text-shadow: 0 0 20px rgba(251, 191, 36, 0.4);
}
.join-row { display: flex; gap: 10px; }
#join-code {
  font: inherit;
  font-size: 22px;
  letter-spacing: 8px;
  text-transform: uppercase;
  text-align: center;
  width: 140px;
  padding: 8px;
  color: var(--text);
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  outline: none;
}
#join-code:focus { border-color: var(--accent); }
.pulse-text { animation: pulse-text 1.6s ease-in-out infinite; }
@keyframes pulse-text { 50% { opacity: 0.45; } }

/* ================================ game hud ============================== */

#screen-game { gap: 10px; padding: 12px; max-width: none; height: 100%; justify-content: center; position: relative; }

.hud { width: min(92vw, calc(100vh - 190px), 620px); display: flex; flex-direction: column; gap: 6px; }
.player-row { display: flex; align-items: center; gap: 10px; min-height: 26px; }
.pname { font-weight: 600; }
.dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.4); flex-shrink: 0; }
.dot.w { background: #f5f2e9; }
.dot.b { background: #1c1f2b; }
.captured { font-family: var(--piece-font); font-size: 20px; letter-spacing: 2px; margin-left: auto; opacity: 0.9; }
.captured .cw { color: #f5f2e9; text-shadow: 0 1px 2px rgba(0,0,0,0.7); }
.captured .cb { color: #2a2e3d; text-shadow: 0 1px 2px rgba(255,255,255,0.35); }

.cd-track {
  height: 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
.cd-fill {
  height: 100%;
  width: 100%;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  transform-origin: left;
  transform: scaleX(0);
}
.cd-fill.hot { background: linear-gradient(90deg, #f97316, var(--danger)); }

#status { min-height: 22px; font-size: 15px; text-align: center; }

/* ================================= board ================================ */

#board-wrap { position: relative; }

#board {
  position: relative;
  width: min(92vw, calc(100vh - 190px), 620px);
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.08);
  touch-action: none;
}

.square {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
}
.square.light { background: var(--sq-light); }
.square.dark { background: var(--sq-dark); }

/* last-move tint */
.square.last::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(251, 191, 36, 0.28);
}

/* selection */
.square.sel { box-shadow: inset 0 0 0 3px var(--gold); }

/* legal-move dot */
.square.hint::after {
  content: "";
  position: absolute;
  left: 50%; top: 50%;
  width: 28%; height: 28%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.35);
}
.square.light.hint::after { background: rgba(15, 23, 42, 0.22); }

/* capture ring */
.square.cap::after {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: 4px solid rgba(248, 113, 113, 0.75);
}

/* king in danger */
.square.check::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, rgba(248, 113, 113, 0.65) 0%, rgba(248, 113, 113, 0.1) 70%);
  animation: check-pulse 0.9s ease-in-out infinite;
}
@keyframes check-pulse { 50% { opacity: 0.35; } }

/* ================================= pieces =============================== */

.piece {
  position: absolute;
  left: 0; top: 0;
  width: 12.5%;
  height: 12.5%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--piece-font);
  font-size: calc(var(--sq, 64px) * 0.76);
  line-height: 1;
  cursor: grab;
  transition: transform 0.18s cubic-bezier(0.2, 0.8, 0.3, 1.1), opacity 0.25s ease, filter 0.2s ease;
  will-change: transform;
  z-index: 2;
}
.piece.w { color: #f7f4ea; text-shadow: 0 2px 3px rgba(0, 0, 0, 0.65), 0 0 1px #000; }
.piece.b { color: #23262f; text-shadow: 0 1px 2px rgba(255, 255, 255, 0.3), 0 0 1px rgba(255,255,255,0.4); }

/* NOTE: the enlarge effects live inside the inline `transform` (appended by
   board.js) rather than the standalone `scale` property — `scale` composes
   around the transform and would multiply the translate offsets, pushing the
   piece away from the cursor. */
.piece.drag { transition: none; z-index: 10; cursor: grabbing; }
.piece.preview { z-index: 5; }
.piece.captured {
  z-index: 1;
  opacity: 0;
  filter: blur(2px);
  pointer-events: none;
}
.piece.shake { animation: shake 0.25s ease; }
@keyframes shake {
  25% { margin-left: -4px; }
  75% { margin-left: 4px; }
}

/* dim the side that is cooling down */
#board.cool-w .piece.w, #board.cool-b .piece.b { filter: brightness(0.65) saturate(0.6); }

#board.locked .piece { cursor: default; }

/* ================================ overlay =============================== */

#overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 12, 20, 0.72);
  backdrop-filter: blur(6px);
  z-index: 50;
  animation: fade-in 0.35s ease;
}
@keyframes fade-in { from { opacity: 0; } }

.overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  background: var(--bg2);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 36px 48px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  animation: pop-in 0.35s cubic-bezier(0.2, 0.8, 0.3, 1.2);
  text-align: center;
}
@keyframes pop-in { from { transform: scale(0.8); opacity: 0; } }

#overlay-title { font-size: 36px; }
#overlay-title.win { color: var(--gold); text-shadow: 0 0 24px rgba(251, 191, 36, 0.5); }
#overlay-title.lose { color: var(--danger); }

/* ================================ mobile ================================ */

@media (max-width: 600px) {
  .screen { padding: 14px; gap: 14px; }
  .btn.big { min-width: 180px; font-size: 18px; }
  .hud, #board { width: min(96vw, calc(100vh - 170px)); }
  .overlay-card { padding: 26px 28px; }
}
