:root {
  --paper: #f7f2ea;
  --ink:   #141414;
  --ink-soft: rgba(20, 20, 20, .55);
  --hairline: rgba(28, 26, 23, .13);
  --serif: 'Newsreader', Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
}

/* One column in normal flow — nothing is fixed, nothing can overlap. */
#stage {
  box-sizing: border-box;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 12px;
}

/* Board flexes to absorb leftover height; it shrinks before anything
   overlaps. Once it hits its cap, the spacers take the extra space so
   the whole group stays vertically centered and the cards hug the board. */
#stage::before, #stage::after { content: ''; flex: 1 1 0; }
#boardArea {
  position: relative;   /* anchors the controls beside the board */
  flex: 999 1 auto;
  min-height: 240px;
  max-height: 560px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Controls: mute sits on the board's fourth row from the bottom —
   same height as pause + mute on the landing page. Resign is the
   word above it, at the exact seat "play Stockfish" occupies there,
   so the transition between pages doesn't jump. */
#ctrls {
  position: absolute;
  left: calc(75% + var(--bw, 480px) / 4);
  /* Identical to index.html: White's 4th rank, center at (4 + 0.5)/8
     down from the board top (board itself is vertically centered).
     Only mute is in-flow, so this centers the icon on that row. */
  top: calc(50% - var(--bw, 480px) / 2 + var(--bw, 480px) * 4.5 / 8);
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  /* Above chessground pieces (resting 2, anim 8, dragging 11) and the
     plaque (z-index 12): without this, a piece on the h-file steals the
     hover and the hand cursor never appears on the buttons. */
  z-index: 13;
  cursor: pointer;
}
#ctrls.no-room { display: none; }
.ctrl {
  background: none;
  border: 0;
  padding: 6px;
  color: #000;
  cursor: pointer;
  outline: none;  /* no focus ring: keyboard use after clicking a ctrl drew one */
}
.ctrl, .ctrl * { cursor: pointer; }
.ctrl svg { display: block; }
/* Resign: same placement and voice as #playSf on the landing page. */
#resignBtn {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  font-family: var(--serif);
  font-size: 13px;
  line-height: 1;
  color: var(--ink-soft);
  transition: color 300ms ease;
}
#resignBtn:hover { color: var(--ink); }
#resignBtn:disabled { color: rgba(20, 20, 20, .25); }
#resignBtn:disabled, #resignBtn:disabled * { cursor: default; }
#mute .slash { display: none; }
#mute.muted .waves { display: none; }
#mute.muted .slash { display: initial; }
#board { /* sized in px by fitBoard() */ }

/* Board colours: sand light squares, venetian clay dark squares. */
cg-board {
  background-color: #e9d8bf;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8' shape-rendering='crispEdges'%3E%3Cdefs%3E%3Cpattern id='p' width='2' height='2' patternUnits='userSpaceOnUse'%3E%3Crect width='2' height='2' fill='%23e9d8bf'/%3E%3Crect x='1' width='1' height='1' fill='%23a3564a'/%3E%3Crect y='1' width='1' height='1' fill='%23a3564a'/%3E%3C/pattern%3E%3C/defs%3E%3Crect width='8' height='8' fill='url(%23p)'/%3E%3C/svg%3E");
}
cg-board square.last-move { background-color: rgba(196, 145, 62, .32); }
/* Chessground hands the whole board a pointer cursor; the hand belongs
   only over a piece that can actually be picked up. Those squares are
   marked by setPlayable() through highlight.custom.
   pointer-events:auto on the square is for the desktop cursor only —
   on touch it recreates chessground's old "highlighted squares eat
   the gesture" bug (lichess-org/chessground#82), so keep it off when
   there is no fine pointer. */
.cg-wrap.manipulable cg-board { cursor: default; }
@media (hover: hover) and (pointer: fine) {
  cg-board square.movable { pointer-events: auto; cursor: pointer; }
}
/* Chessground's own theme paints these lichess green; restate them in
   the page's ink so a playable board still reads as this board. */
cg-board square.move-dest {
  background: radial-gradient(rgba(28, 26, 23, .26) 21%, rgba(28, 26, 23, .26) 0,
                              rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 0);
}
cg-board square.oc.move-dest {
  background: radial-gradient(transparent 0%, transparent 79%, rgba(28, 26, 23, .26) 79%);
}
cg-board square.move-dest:hover { background: rgba(196, 145, 62, .3); }
cg-board square.oc.move-dest:hover { background: rgba(196, 145, 62, .3); }
cg-board square.selected { background-color: rgba(196, 145, 62, .45); }

/* ---- player cards: unboxed text blocks — a masthead hairline under
   the name row is the only structure ---- */
.player {
  box-sizing: border-box;
  width: var(--bw, min(94vw, 480px));
  padding: 2px 2px 0;
}

.player .row {
  display: flex; align-items: center; gap: 9px;
  padding-bottom: 5px;
  border-bottom: 1px solid var(--hairline);
}

/* The bottom card mirrors the top one around the board: thought line
   nearest the board, hairline, name row at the outer edge. */
#pBot { display: flex; flex-direction: column-reverse; }
#pBot .row {
  border-bottom: none;
  border-top: 1px solid var(--hairline);
  padding-bottom: 0;
  padding-top: 5px;
}
#pBot .status { margin-top: 0; margin-bottom: 4px; }
.player .name {
  font-family: var(--serif);
  font-variant-caps: all-small-caps;
  font-size: 17px;
  font-weight: 500;
  letter-spacing: .05em;
}
/* Each seat in its own voice: the human side takes the flowing hand the
   replay page gives Leela, against Stockfish's machined one. */
.player.You .name {
  font-family: 'Great Vibes', cursive;
  font-variant-caps: normal;
  font-size: 25px;
  font-weight: 400;
  letter-spacing: 0;     /* scripts connect; never letterspace them */
  line-height: 1;
}
.player.Stockfish .name {
  font-family: 'Chakra Petch', var(--sans);
  font-variant-caps: normal;
  font-size: 16.5px;
  font-weight: 600;
  letter-spacing: .02em;
}

/* ---- first-load name entrance ------------------------------------
   Stockfish: each glyph slammed down L→R like a rubber stamp.
   You: Leela's starry fade-in — the name resolves out of a soft
   glow-blur while small stars twinkle around it. ---- */
.player.Stockfish .name .letter {
  display: inline-block;
  transform-origin: 50% 85%;
  opacity: 0;
  will-change: transform, opacity;
  animation: stamp-in .32s cubic-bezier(.1, .85, .2, 1.05) forwards;
  animation-delay: calc(var(--i) * 68ms + 40ms);
}
@keyframes stamp-in {
  0% {
    opacity: 0;
    transform: scale(1.65) translateY(-11px) rotate(calc(var(--r, -4) * 1deg));
  }
  58% {
    opacity: 1;
    transform: scale(.9) translateY(1.5px) rotate(calc(var(--r, -4) * .2deg));
  }
  78% {
    transform: scale(1.05) translateY(0) rotate(calc(var(--r, -4) * .12deg));
  }
  100% {
    opacity: 1;
    /* A hair of residual tilt — the stamp never lands perfectly square. */
    transform: scale(1) translateY(0) rotate(calc(var(--r, -4) * .18deg));
  }
}
.player.You .name.enter-starry {
  position: relative;
  display: inline-block;
  opacity: 0;
  filter: blur(5px) brightness(1.6);
  will-change: opacity, filter;
  animation: you-starlight 1.1s ease-out forwards;
  animation-delay: 120ms;
}
@keyframes you-starlight {
  0% { opacity: 0; filter: blur(5px) brightness(1.6); }
  55% { opacity: .92; filter: blur(1px) brightness(1.15); }
  100% { opacity: 1; filter: blur(0) brightness(1); }
}
.player.You .name .spark {
  position: absolute;
  pointer-events: none;
  font-size: var(--sz, 9px);
  line-height: 1;
  color: #fff7e6;
  text-shadow:
    0 0 3px #fff2cf,
    0 0 7px rgba(255, 214, 130, .95),
    0 0 13px rgba(255, 190, 90, .7);
  opacity: 0;
  will-change: transform, opacity;
  animation: spark-twinkle 950ms ease-out forwards;
}
@keyframes spark-twinkle {
  0% { opacity: 0; transform: scale(.15) rotate(0deg); }
  30% { opacity: 1; transform: scale(1.3) rotate(25deg); }
  55% { opacity: .95; transform: scale(.95) rotate(5deg); }
  100% { opacity: 0; transform: scale(.3) rotate(-18deg); }
}
@media (prefers-reduced-motion: reduce) {
  .player.Stockfish .name .letter {
    animation: none;
    opacity: 1;
    transform: none;
  }
  .player.You .name.enter-starry {
    animation: none;
    opacity: 1;
    filter: none;
  }
  .player.You .name .spark { display: none; }
}

/* Confidence: Stockfish's own sense of how he is doing. The human
   side keeps no bar — a player is not an evaluation function. */
.player .conf { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.player .pct {
  font-size: 10.5px;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.player .bar {
  position: relative;
  width: 128px; height: 5px;
  border-radius: 3px;
  background: rgba(28, 26, 23, .08);
  overflow: hidden;
}
.player .bar::after {   /* equality tick */
  content: ''; position: absolute; top: 0; bottom: 0; left: 50%;
  width: 1px; background: rgba(28, 26, 23, .18);
}
.player .bar i {
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 50%;
  border-radius: 3px;
  transition: width 700ms cubic-bezier(.4, 0, .2, 1);
}
.player .bar i { background: var(--ink); }

/* One quiet line, in the same voice and rhythm as the replay page. */
.player .status {
  font-family: var(--serif);
  font-style: italic;
  font-size: 13.5px;
  line-height: 1.5;   /* room for italic descenders inside overflow:hidden */
  min-height: 1.5em;
  margin-top: 4px;
  color: var(--ink-soft);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding-left: .25em; margin-left: -.25em;
  transition: opacity 700ms ease;
}
.player .status.faded { opacity: 0; }

/* ---- captured pieces: the material each side has taken, on the
   board-side of the card. Duplicates fan out with a card-hand
   overlap, so even a 15-piece haul stays a quiet little row. ---- */
.player .sub { display: flex; align-items: center; }
.player .sub .status { flex: 1 1 auto; min-width: 0; }
/* The tray owns its full height from ply one — the first capture must
   not nudge the layout — and lays pieces out as flex items so their
   sprites never enter line-box math. */
.player .caps {
  margin-left: auto; padding-left: 12px;
  display: flex; align-items: center;
  height: 17px;
}
.player .caps .grp { display: flex; }
.player .caps .grp + .grp { margin-left: 3.5px; }
.player .caps i {
  width: 17px; height: 17px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
.player .caps .grp i + i { margin-left: -10px; }

/* ---- verdict: the closing notice, a calling card laid on the board.
   The message is the page's, the name inside it is its owner's. ---- */
#verdict {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(.97);
  opacity: 0;
  pointer-events: none;
  z-index: 12;
  text-align: center;
  white-space: nowrap;
  transition: opacity 900ms ease, transform 900ms cubic-bezier(.2, .7, .2, 1);
  color: var(--ink);
  background: var(--paper);
  border: 1px solid rgba(28, 26, 23, .3);
  box-shadow: 0 0 0 4px var(--paper), 0 3px 18px rgba(20, 20, 20, .28);
  padding: 13px 34px 15px;
  font-family: var(--serif);
  font-size: 21px;
}
#verdict.show { opacity: 1; transform: translate(-50%, -50%) scale(1); pointer-events: auto; }
/* Only the name wears its owner's face, the way a trademark keeps its
   lettering; the words around it stay in the page's own hand. */
#verdict.Stockfish .who {
  font-family: 'Chakra Petch', var(--sans);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: .02em;
}
#verdict.You .who {
  font-family: 'Great Vibes', cursive;
  font-size: 28px;
  font-weight: 400;
  letter-spacing: 0;     /* scripts connect; never letterspace them */
}
/* Dismissal sits on the card's corner, over the paper ring. */
#verdictClose {
  position: absolute;
  top: -10px; right: -10px;
  width: 20px; height: 20px;
  padding: 0;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid rgba(28, 26, 23, .3);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  outline: none;
  transition: background-color 300ms ease, border-color 300ms ease;
}
#verdictClose:hover {
  background-color: rgba(28, 26, 23, .07);
  border-color: rgba(28, 26, 23, .5);
}
#verdictClose svg { display: block; }

/* ---- promotion: the same calling card, asking a question ---- */
#promo {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  z-index: 5;
  display: none;
  gap: 4px;
  padding: 9px 11px;
  background: var(--paper);
  border: 1px solid rgba(28, 26, 23, .3);
  box-shadow: 0 0 0 4px var(--paper), 0 3px 18px rgba(20, 20, 20, .28);
}
#promo.show { display: flex; }
#promo button {
  width: 46px; height: 46px;
  padding: 0;
  border: 0;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 40px 40px;
  cursor: pointer;
  outline: none;
  transition: background-color 200ms ease;
}
#promo button:hover { background-color: rgba(28, 26, 23, .07); }

/* Wordmark: fixed in the corner, outside the column. */
#logo {
  position: fixed;
  top: 14px; left: 18px;
  font-family: var(--serif);
  font-size: 15px;
  font-weight: 500;
  letter-spacing: .22em;
  color: var(--ink);
  user-select: none;
}
#credit {
  position: fixed;
  bottom: 14px; right: 18px;
  font: 13px/1 var(--serif);
  color: var(--ink-soft);
  text-decoration: none;
  transition: color 300ms ease;
}
#credit:hover { color: var(--ink); }

/* ---- progressive shedding: the board is king ---- */
/* Logo sheds on short height or narrow portrait. Ctrls are width-only
   via JS (.no-room) so resign/mute don't flip with height at one width. */
@media (max-height: 780px) {
  #logo { display: none; }
}
@media (max-width: 820px) and (orientation: portrait) {
  #logo, #credit { display: none; }
}
@media (max-height: 660px), (max-width: 540px) {
  #stage { padding: 10px 12px; gap: 8px; }
}
@media (max-height: 560px), (max-width: 460px) {
  .player { padding: 0 2px; }
  .player .name { font-size: 14px; }
  .player.You .name { font-size: 20px; }
  .player.Stockfish .name { font-size: 13.5px; }
  .player .bar { width: 88px; }
}
@media (max-height: 560px) {
  .player .sub { display: none; }
}

body.play-mode #board { pointer-events: auto; }
body.play-mode #pBot .conf { display: none; }
