/* Mobile-first. Board scales to the smaller of viewport width/height. */
:root {
  --light: #eeeed2;
  --dark: #262942;
  --sel: #f6f669;
  --last: rgba(72, 133, 214, 0.45);
  --check: rgba(214, 46, 46, 0.86);
  --draw: rgba(120, 124, 140, 0.62);
  --bg: #312e2b;
  --panel: #3b3936;
  --text: #f2f2f2;
  --pixel-font: "Press Start 2P", "Courier New", monospace;
}

/* bundled retro pixel font (self-contained, no CDN needed) */
@font-face {
  font-family: "Press Start 2P";
  src: url("../fonts/PressStart2P.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  touch-action: manipulation;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 10px 10px 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#title {
  font-size: 1.4rem;
  font-weight: 600;
  margin: 6px 0 8px;
  letter-spacing: 0.5px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 0.95rem;
}
.topbar select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid #555;
  border-radius: 8px;
  padding: 8px 10px;
  font-family: var(--pixel-font);
  font-size: 0.6rem;
}

.status {
  font-size: 1.05rem;
  min-height: 1.4em;
  text-align: center;
  font-weight: 500;
}
.status.win { color: #ffd166; font-weight: 700; }

.conn {
  font-size: 0.78rem;
  opacity: 0.65;
  margin: 2px 0 8px;
  text-align: center;
}

.powers {
  width: min(96vw, 560px);
  background: var(--panel);
  border: 1px solid #4a4744;
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 10px;
  font-size: 0.85rem;
  line-height: 1.5;
}

#board-wrap {
  width: min(96vw, 96vh, 560px);
}

.board {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 3px solid #20201d;
  border-radius: 4px;
  overflow: hidden;
  user-select: none;
  touch-action: none; /* let us drag pieces without the page scrolling */
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.sq.light { background: var(--light); }
.sq.dark  { background: var(--dark); }

.sq.sel { background: var(--sel) !important; }
.sq.last::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--last);
  pointer-events: none;
}
/* king in check: the whole square goes red (the piece sits on top of it) */
.sq.check::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--check);
  pointer-events: none;
}
/* drawn game (stalemate / threefold / insufficient): grey both kings' squares */
.sq.draw::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--draw);
  pointer-events: none;
}
/* the enemy piece delivering check gets an orange frame */
.sq.checker::before {
  content: "";
  position: absolute;
  inset: 0;                 /* flush to the square edges */
  border: 5px solid #ff9d2e;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.5);
  z-index: 3;
  pointer-events: none;
}
/* the square currently under a dragged piece gets a bright inset ring */
.sq.dragover {
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.92),
              inset 0 0 0 6px rgba(0, 0, 0, 0.32);
}

/* the piece: a square sprite that fills its square */
.piece {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}
.piece .pcimg {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  image-rendering: pixelated;            /* keep pixel art crisp at any size */
  image-rendering: -webkit-optimize-contrast; /* Safari fallback */
}

/* legal-move indicator: a square frame inset around the square's perimeter.
   White for a quiet move, red for a capture. Drawn above the piece sprite so
   capture frames show over the enemy piece. The thin dark shadow keeps the
   white frame visible on the light (cream) squares too. */
.sq .movehint {
  position: absolute;
  inset: 0;                 /* flush to the square edges, like a thick cell border */
  border: 5px solid #ffffff;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.5);
  z-index: 3;
  pointer-events: none;
}
.sq .movehint.capture { border-color: #ff2e2e; }

/* a checkmated king topples over */
.piece.fallen .pcimg { transform: rotate(90deg); }

/* coordinate labels in corners */
.sq .coord {
  position: absolute;
  font-size: 9px;
  font-weight: 700;
  opacity: 0.6;
  z-index: 1;
}
.sq .coord.file { right: 3px; bottom: 1px; }
.sq .coord.rank { left: 3px; top: 1px; }
.sq.light .coord { color: var(--dark); }
.sq.dark .coord { color: var(--light); }

.controls {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
  justify-content: center;
}
button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid #555;
  border-radius: 8px;
  padding: 12px 13px;
  font-family: var(--pixel-font);
  font-size: 0.6rem;
  line-height: 1.3;
  letter-spacing: 0.5px;
  cursor: pointer;
}
button:active { background: #4a4744; }
button.danger { border-color: #7a4040; color: #ffb3b3; }
button.danger:active { background: #5a3030; }

.captured {
  margin-top: 12px;
  width: min(96vw, 560px);
  font-size: 24px;
  min-height: 64px;
}
.cap-row { min-height: 30px; line-height: 30px; letter-spacing: 2px; }

.hidden { display: none !important; }

/* small piece icon, used in the captured tray and promotion picker */
.swatch { display: inline-block; width: 28px; height: 28px; vertical-align: middle; }
.swatch .pcimg { width: 100%; height: 100%; object-fit: contain; display: block; }
.promo .swatch { width: 52px; height: 52px; }

/* the floating piece while dragging (shares .piece, so reset its layout) */
.drag-ghost {
  position: fixed;
  inset: auto;
  z-index: 200;
  transform: translate(-50%, -60%) scale(1.12);
  transform-origin: center;
  pointer-events: none;
  filter: drop-shadow(0 6px 6px rgba(0,0,0,0.5));
}
.drag-ghost .pcimg { width: 100%; height: 100%; object-fit: contain; display: block; }

/* promotion picker overlay */
.promo {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.promo-inner {
  background: var(--panel);
  padding: 18px;
  border-radius: 12px;
  text-align: center;
}
.promo-label { margin-bottom: 14px; font-family: var(--pixel-font); font-size: 0.72rem; }
.promo-choices { display: flex; gap: 6px; }
.promo-choices button {
  font-size: 40px;
  width: 64px;
  height: 64px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
