/* Chess. Loads after base.css, which supplies the tokens, reset, background and
   card chrome. */

:root {
  /* --sq is the measured side of one square in px, written by the script; the
     value here is only what the very first paint uses. Everything on the board
     — glyph size, dot radius, coordinate labels — is derived from it, so the
     board scales as one piece. */
  --sq: 52px;

  /* The two square colours. Both are washes over the dark card rather than
     opaque fills, so the blobs drifting behind the glass still show through. */
  --sq-light: rgba(226, 232, 240, 0.13);
  --sq-dark: rgba(226, 232, 240, 0.045);

  /* Piece fills. The same solid glyphs are used for both sides and separated by
     colour plus an outline — the outline "white" glyphs (♔♕♖) render at wildly
     different weights across platforms, and half of them turn into emoji. */
  --piece-white: #f1f5f9;
  --piece-black: #0a0b10;

  --hl-last: rgba(168, 85, 247, 0.2);
  --hl-last-light: rgba(168, 85, 247, 0.3);
  --hl-check: rgba(244, 63, 94, 0.5);
}

/* Board plus the side panel; wider than the other games for that reason. */
.card {
  max-width: 800px;
}

.card__body {
  padding: 26px 26px 22px;
}

.card__foot {
  padding: 13px 26px;
}

/* Header: title + counters */
.head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.title {
  position: relative;
  margin: 0;
  font-family: var(--font-mono);
  font-size: clamp(1.6rem, 6.4vw, 2.4rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.03em;
  background: linear-gradient(
    100deg,
    var(--accent-cyan) 0%,
    #818cf8 45%,
    var(--accent-violet) 100%
  );
  background-size: 200% auto;
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  animation:
    rise 700ms var(--ease-smooth) 120ms both,
    shimmer 8s linear infinite;
}

.scores {
  display: flex;
  gap: 8px;
  animation: fade-in 700ms var(--ease-smooth) 240ms both;
}

.score {
  position: relative;
  min-width: 74px;
  padding: 6px 10px 7px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  text-align: center;
}

.score__label {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-dim);
  line-height: 1.4;
}

.score__value {
  display: block;
  font-family: var(--font-mono);
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* Hint line */
.hint {
  margin: 16px 0 14px;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  animation: fade-in 600ms var(--ease-smooth) 340ms both;
}

.hint__sigil {
  color: var(--accent-lime);
  margin-right: 8px;
}

.hint__touch { display: none; }

/* Coarse pointers get the tap wording instead of the mouse one. */
@media (hover: none) and (pointer: coarse) {
  .hint__desktop { display: none; }
  .hint__touch { display: inline; }
}

/* Board and panel */
.game {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 236px;
  gap: 18px;
  align-items: start;
  animation: rise 700ms var(--ease-smooth) 400ms both;
}

.boardwrap {
  position: relative;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 8px;
  border: 1px solid var(--border-color);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.025);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  border-radius: 7px;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  /* Dragging a piece must not pop the iOS callout or start a text selection,
     and must not scroll the page under the finger. */
  -webkit-touch-callout: none;
  touch-action: none;
}

/* Rows exist for assistive tech only; they must not break the single grid. */
.row { display: contents; }

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 0;
  border-radius: 0;
  background-color: var(--sq-dark);
  background-repeat: no-repeat;
  background-position: center;
  cursor: default;
  transition: background-color var(--duration-short) var(--ease-smooth);
}

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

/* Only a piece the side to move owns is worth reaching for. */
.sq--mine { cursor: grab; }

.sq:focus-visible {
  outline: 2px solid var(--accent-cyan);
  outline-offset: -2px;
  z-index: 3;
}

/* Coordinates ride in the corners of the edge squares, the way a real board
   prints them. The script decides which squares are on an edge, so the labels
   follow the orientation when the board flips. */
.sq::before,
.sq::after {
  position: absolute;
  font-family: var(--font-mono);
  font-size: calc(var(--sq) * 0.2);
  line-height: 1;
  color: var(--text-dim);
  pointer-events: none;
}

.sq[data-coord-rank]::before {
  content: attr(data-coord-rank);
  top: calc(var(--sq) * 0.07);
  left: calc(var(--sq) * 0.08);
}

.sq[data-coord-file]::after {
  content: attr(data-coord-file);
  right: calc(var(--sq) * 0.08);
  bottom: calc(var(--sq) * 0.06);
}

/* The two squares of the move just played. Light squares take a stronger wash
   so both halves of the pair read as the same colour. */
.sq--last { background-color: var(--hl-last); }
.sq--last.sq--light { background-color: var(--hl-last-light); }

/* Where a legal move lands: a dot on an empty square, a ring on a piece. */
.sq--move {
  background-image: radial-gradient(
    circle at center,
    rgba(34, 211, 238, 0.55) 0 calc(var(--sq) * 0.15),
    transparent calc(var(--sq) * 0.155)
  );
}

.sq--capture {
  box-shadow: inset 0 0 0 3px rgba(34, 211, 238, 0.55);
  z-index: 1;
}

.sq--selected {
  background-color: rgba(34, 211, 238, 0.28);
  box-shadow: inset 0 0 0 2px var(--accent-cyan);
  z-index: 2;
}

/* The king of whoever is to move, when they are in check. */
.sq--check {
  background-image: radial-gradient(circle at center, var(--hl-check) 0, transparent 72%);
}

.sq--hover:not(.sq--selected) {
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.55);
  z-index: 2;
}

.sq__piece {
  /* A stack that prefers a text-presentation symbol font; the U+FE0E the script
     appends to each glyph keeps the rest of them out of emoji rendering. */
  font-family: 'Segoe UI Symbol', 'Apple Symbols', 'Noto Sans Symbols 2',
    'DejaVu Sans', serif;
  font-size: calc(var(--sq) * 0.78);
  line-height: 1;
  pointer-events: none;
  /* The glyph's own box sits high in the em; nudge it back onto the square. */
  transform: translateY(calc(var(--sq) * -0.03));
}

.sq__piece--w {
  color: var(--piece-white);
  -webkit-text-stroke: calc(var(--sq) * 0.022) rgba(5, 5, 7, 0.85);
  text-shadow: 0 calc(var(--sq) * 0.02) calc(var(--sq) * 0.05) rgba(0, 0, 0, 0.55);
}

/* Black pieces would vanish into a dark square without the light outline; the
   text-shadow is there for the rare engine that ignores text-stroke. */
.sq__piece--b {
  color: var(--piece-black);
  -webkit-text-stroke: calc(var(--sq) * 0.022) rgba(226, 232, 240, 0.9);
  text-shadow: 0 0 calc(var(--sq) * 0.03) rgba(226, 232, 240, 0.5);
}

/* The square a piece was lifted from keeps its highlight but loses the glyph,
   which is now following the pointer. */
.sq--dragging .sq__piece { opacity: 0; }

/* The glyph under the pointer mid-drag. Lives on <body> so no overflow or
   stacking context on the board can clip it. */
.ghost {
  position: fixed;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  transform: translate(-50%, -50%);
}

/* Side panel */
.panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.tray {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-height: 30px;
  padding: 5px 9px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
}

.tray__label {
  flex: none;
  font-family: var(--font-mono);
  font-size: 0.58rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.tray__pieces {
  font-family: 'Segoe UI Symbol', 'Apple Symbols', 'Noto Sans Symbols 2',
    'DejaVu Sans', serif;
  font-size: 0.95rem;
  line-height: 1.2;
  word-break: break-all;
}

.tray__pieces .w { color: var(--piece-white); }

/* At tray size the outline trick is too fine to read, so captured black pieces
   are simply drawn in a dimmer grey instead. */
.tray__pieces .b { color: #64748b; }

.moves {
  flex: 1;
  min-height: 190px;
  max-height: 340px;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0;
  padding: 6px;
  list-style: none;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  font-family: var(--font-mono);
  font-size: 0.76rem;
}

.moves:empty::after {
  content: 'no moves yet';
  display: block;
  padding: 4px 4px 0;
  color: var(--text-dim);
  font-size: 0.72rem;
}

.move {
  display: grid;
  grid-template-columns: 2.2em 1fr 1fr;
  gap: 4px;
  padding: 2px 3px;
  border-radius: 5px;
}

.move__no {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.move__ply {
  color: var(--text-secondary);
  white-space: nowrap;
}

/* The ply on the board right now. */
.move__ply--now {
  color: var(--accent-cyan);
  font-weight: 700;
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 8px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 7px;
  background: rgba(5, 5, 7, 0.84);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  text-align: center;
  animation: fade-in 320ms var(--ease-smooth) both;
}

.overlay[hidden] { display: none; }

.overlay__title {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(100deg, var(--accent-cyan), var(--accent-violet));
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}

.overlay__text {
  margin: 0 0 12px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.overlay__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

/* Promotion picker */
.promo {
  display: flex;
  gap: 8px;
}

.promo__pick {
  display: flex;
  align-items: center;
  justify-content: center;
  width: calc(var(--sq) * 1.1);
  height: calc(var(--sq) * 1.1);
  padding: 0;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  font-family: 'Segoe UI Symbol', 'Apple Symbols', 'Noto Sans Symbols 2',
    'DejaVu Sans', serif;
  font-size: calc(var(--sq) * 0.72);
  line-height: 1;
  cursor: pointer;
  transition:
    border-color var(--duration-short) var(--ease-smooth),
    background var(--duration-short) var(--ease-smooth);
}

.promo__pick:hover,
.promo__pick:focus-visible {
  border-color: var(--border-hover);
  background: rgba(34, 211, 238, 0.12);
  outline: none;
}

/* Buttons */
.controls {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.btn {
  flex: 1;
  padding: 9px 14px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-chip);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition:
    color var(--duration-short) var(--ease-smooth),
    border-color var(--duration-short) var(--ease-smooth),
    background var(--duration-short) var(--ease-smooth);
}

.btn:hover:not(:disabled),
.btn:focus-visible {
  color: var(--accent-cyan);
  border-color: var(--border-hover);
  outline: none;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn--primary {
  color: var(--text-primary);
  border-color: rgba(34, 211, 238, 0.25);
  background: rgba(34, 211, 238, 0.1);
}

.btn[aria-pressed="true"] {
  color: var(--accent-lime);
  border-color: rgba(74, 222, 128, 0.35);
  background: rgba(74, 222, 128, 0.1);
}

/* Square glyph-only buttons, so five controls still fit a narrow phone. */
.btn--icon {
  flex: none;
  min-width: 44px;
  padding: 9px 0;
  font-size: 0.95rem;
}

.btn--icon[aria-pressed="false"]#sound {
  color: var(--text-dim);
  text-decoration: line-through;
}

.overlay .btn {
  flex: none;
  min-width: 116px;
}

/* Responsive Design */
@media (max-width: 780px) {
  .game {
    grid-template-columns: minmax(0, 1fr);
  }

  .panel { max-width: 480px; width: 100%; margin: 0 auto; }

  .moves { min-height: 0; max-height: 150px; }
}

@media (max-width: 660px) {
  .stage { padding: 14px; }

  .card__body { padding: 20px 16px 18px; }

  .card__foot { padding: 12px 16px; }

  .head { gap: 12px; }

  .scores { width: 100%; }

  .score { flex: 1; min-width: 0; }

  .boardwrap { padding: 6px; }

  .overlay { inset: 6px; }

  .controls { flex-wrap: wrap; }

  .btn { min-width: 0; }
}

/* Accessibility - Respect user's motion preferences */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-delay: 0ms !important;
  }
}
