:root {
  --bg: #0f1218;
  --panel: #171c26;
  --text: #e8eaef;
  --muted: #8b93a6;
  --accent: #6ee7b7;
  --accent-dim: #34d399;
  --danger: #f87171;
  --light-square: #c9d1dc;
  --dark-square: #5c6b82;
  /* Nearly black; tiny contrast so the board mesh reads as a faint checkerboard */
  --fog-light: #14151a;
  --fog-dark: #0a0b0e;
  --selected: rgba(110, 231, 183, 0.45);
  --check: rgba(248, 113, 113, 0.35);
  font-family: 'Segoe UI', system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(ellipse 120% 80% at 50% -20%, #1e293b 0%, var(--bg) 55%);
  color: var(--text);
  line-height: 1.5;
}

#app {
  max-width: 52rem;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
}

.header h1 {
  margin: 0 0 0.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-size: 1.85rem;
}

.tagline {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  max-width: 40rem;
}

.panel {
  margin-top: 1.75rem;
  padding: 1.25rem 1.35rem;
  background: var(--panel);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 1.15rem;
}

/* Section toggles only — never use class `hidden` on chess cells (those use .fog) */
#gate.hidden,
#lobby-section.hidden,
#game-section.hidden,
#challenge-banner.hidden,
#return-lobby.hidden,
#promotion-modal.hidden,
#gate-error[hidden],
#move-error[hidden] {
  display: none !important;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.field span {
  font-size: 0.85rem;
  color: var(--muted);
}

input[type='text'] {
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  max-width: 20rem;
}

.btn {
  padding: 0.55rem 1rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: #2a3142;
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

.btn:hover {
  background: #343c50;
}

.btn.primary {
  background: linear-gradient(135deg, #059669, var(--accent-dim));
  border-color: transparent;
  color: #042f23;
  font-weight: 600;
}

.btn.danger {
  border-color: rgba(248, 113, 113, 0.55);
  color: #fecaca;
}

.btn.danger:hover {
  background: rgba(248, 113, 113, 0.15);
  border-color: rgba(248, 113, 113, 0.75);
}

.game-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.btn.primary:hover {
  filter: brightness(1.06);
}

.error {
  color: var(--danger);
  margin: 0.5rem 0 0;
  font-size: 0.9rem;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.hint {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0.25rem 0 1rem;
}

.lobby-bot-row {
  margin: 0.25rem 0 1rem;
}

.lobby-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lobby-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.lobby-list li:last-child {
  border-bottom: none;
}

.banner {
  margin-top: 1rem;
  padding: 1rem 1.25rem;
  background: rgba(110, 231, 183, 0.12);
  border: 1px solid rgba(110, 231, 183, 0.35);
  border-radius: 12px;
}

.banner .row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.game-head {
  margin-bottom: 1rem;
}

.game-head p {
  margin: 0.15rem 0;
}

.game-tc {
  font-size: 0.9rem;
  margin: 0.25rem 0 0.5rem;
}

.time-control-field {
  margin-bottom: 1rem;
}

.time-control-select {
  max-width: 12rem;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}

.clocks {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 0.5rem 0 0.75rem;
}

.clocks.hidden {
  display: none;
}

.clock {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.35rem 0.65rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.clock-active {
  border-color: rgba(110, 231, 183, 0.45);
  box-shadow: 0 0 0 1px rgba(110, 231, 183, 0.2);
}

.clock-label {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.clock-time {
  font-variant-numeric: tabular-nums;
  font-size: 1.15rem;
  font-weight: 600;
}

.capture-trays {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}

.capture-tray {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.capture-label {
  color: var(--muted);
  min-width: 8rem;
}

.capture-pieces {
  letter-spacing: 0.05em;
}

.replay-panel {
  margin-top: 1rem;
  padding: 1rem;
  border-radius: 12px;
  border: 1px solid rgba(110, 231, 183, 0.25);
  background: rgba(110, 231, 183, 0.06);
}

.replay-panel.hidden {
  display: none;
}

.replay-title {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.replay-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}

#replay-slider {
  flex: 1 1 8rem;
  min-width: 6rem;
}

.replay-san {
  margin: 0;
  font-size: 0.85rem;
}

#game-status {
  color: var(--muted);
  font-size: 0.95rem;
}

#board-wrap {
  display: flex;
  justify-content: center;
}

.board {
  display: flex;
  flex-direction: column;
  width: min(100%, 26rem);
  aspect-ratio: 1;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

/* One chess rank per row: top = far side, bottom = your side */
.board-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  flex: 1 1 0;
  min-height: 0;
}

@media (max-width: 400px) {
  .board {
    width: 100%;
  }
}

.square {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.5rem, 5vw, 2rem);
  line-height: 1;
  cursor: default;
  user-select: none;
  position: relative;
  box-sizing: border-box;
  border: 1px solid rgba(0, 0, 0, 0.42);
}

/* Visible squares only — use .fog — not .hidden — global `.hidden { display:none }` would kill fog squares */
.square.light:not(.fog) {
  background-color: var(--light-square);
  color: #111;
}

.square.dark:not(.fog) {
  background-color: var(--dark-square);
  color: #111;
}

/* Fog: very dark checkerboard; white borders keep the grid visible */
.square.fog {
  cursor: default;
  border-color: rgba(255, 255, 255, 0.7);
}

.square.fog.light {
  background-color: var(--fog-light);
}

.square.fog.dark {
  background-color: var(--fog-dark);
}

.square.selectable {
  cursor: pointer;
}

/* Server-validated destinations (including fog) */
.square.legal-target {
  cursor: pointer;
  box-shadow: inset 0 0 0 3px rgba(110, 231, 183, 0.85);
}

.square.fog.legal-target {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.5), inset 0 0 0 3px rgba(110, 231, 183, 0.9);
}

/* Dot on empty or fog destinations (squares with a visible piece use the ring only) */
.square.legal-target.fog::after,
.square.legal-target:not(.fog):empty::after {
  content: '';
  position: absolute;
  z-index: 1;
  width: min(30%, 1rem);
  height: min(30%, 1rem);
  border-radius: 50%;
  background: rgba(110, 231, 183, 0.65);
  pointer-events: none;
}

.square.selected::after {
  content: '';
  position: absolute;
  inset: 2px;
  border: 3px solid var(--accent);
  border-radius: 2px;
  pointer-events: none;
}

.square.in-check::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--check);
  pointer-events: none;
}

@keyframes check-flash-pulse {
  0%,
  100% {
    box-shadow: inset 0 0 0 0 transparent;
  }
  25%,
  75% {
    box-shadow: inset 0 0 0 4px rgba(248, 113, 113, 0.95);
  }
  50% {
    box-shadow: inset 0 0 0 2px rgba(248, 113, 113, 0.65);
  }
}

.square.check-flash {
  animation: check-flash-pulse 0.4s ease-in-out 3;
  z-index: 2;
}

.knight-pick-bar {
  margin: 0.5rem 0;
  padding: 0.75rem 1rem;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.knight-pick-bar.hidden {
  display: none;
}

.knight-pick-hint {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.knight-pick-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.btn.knight-pick-a {
  border-color: rgba(250, 204, 21, 0.55);
  color: #fef08a;
}

.btn.knight-pick-b {
  border-color: rgba(34, 197, 94, 0.55);
  color: #86efac;
}

@keyframes knight-flash-a {
  0%,
  100% {
    box-shadow: inset 0 0 0 0 transparent;
  }
  50% {
    box-shadow: inset 0 0 0 4px rgba(250, 204, 21, 0.95);
  }
}

@keyframes knight-flash-b {
  0%,
  100% {
    box-shadow: inset 0 0 0 0 transparent;
  }
  50% {
    box-shadow: inset 0 0 0 4px rgba(34, 197, 94, 0.95);
  }
}

.square.knight-route-flash-a {
  animation: knight-flash-a 0.45s ease-in-out 2;
  z-index: 2;
}

.square.knight-route-flash-b {
  animation: knight-flash-b 0.45s ease-in-out 2;
  z-index: 2;
}

.square.knight-route-a {
  box-shadow: inset 0 0 0 3px rgba(250, 204, 21, 0.9);
  z-index: 2;
}

.square.knight-route-b {
  box-shadow: inset 0 0 0 3px rgba(34, 197, 94, 0.9);
  z-index: 2;
}

.square.knight-route-a.knight-route-b {
  box-shadow:
    inset 3px 0 0 2px rgba(250, 204, 21, 0.95),
    inset -3px 0 0 2px rgba(34, 197, 94, 0.95);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-inner {
  background: var(--panel);
  padding: 1.5rem;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  max-width: 22rem;
}

.modal-inner h3 {
  margin: 0 0 0.5rem;
}

.promo-buttons {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}

.piece-btn {
  font-size: 1.75rem;
  padding: 0.35rem 0.65rem;
  min-width: 3rem;
}
