/* One chess game's viewer (chess-game.js): a slot at the top of an event page that slides open,
   or a dock at the side of the window (a sheet from the bottom on phones) */

/* The slot, under the event's head (in one block with it). Shut, it takes no room; open, it
   keeps the page's gap above it (.sp-chess, 24px, 20px on phones). */
.cg-slot { display: grid; grid-template-rows: 0fr; margin-top: 0; transition: grid-template-rows 260ms cubic-bezier(.25, 1, .5, 1), margin-top 260ms cubic-bezier(.25, 1, .5, 1); }
.cg-slot.cg-open { grid-template-rows: 1fr; margin-top: 24px; }
.cg-slot.cg-still { transition: none; }
.cg-inner { min-height: 0; overflow: hidden; }
.cg-slot .cgv { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }

/* The dock: over the page at the window's right, nearly its full height, scrolling on its own */
.cg-dock {
  position: fixed;
  z-index: 50;
  top: 8px;
  right: 8px;
  bottom: 8px;
  width: clamp(420px, 36vw, 480px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  transform: translateX(calc(100% + 12px));
  transition: transform 260ms cubic-bezier(.25, 1, .5, 1);
}
.cg-dock.cg-open { transform: none; }
.cg-dock.cg-still { transition: none; }

/* The viewer: the game's name and Close over its panel */
.cgv { background: var(--surface-2); }
.cgv-bar { display: flex; align-items: center; gap: 12px; padding: 10px 12px 0 16px; }
.cgv-name { display: flex; align-items: baseline; gap: 12px; min-width: 0; margin: 0; font: 600 19px/1.2 var(--display); color: var(--ink); }
/* The players' names link to their pages (chess-players.js): room inside the clip for a name's
   focus ring, given back outside it so nothing moves. The same for the lines by the board. */
.cgv-pl { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 3px; margin: -3px; }
.cgv-res { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--muted); }
.cgv-x { flex: none; margin-left: auto; width: 32px; }
.cgv-link { display: flex; flex: none; margin-left: auto; }
.cgv-link:empty { display: none; }
.cgv-link:not(:empty) + .cgv-x { margin-left: -8px; }
.cg-loading { padding: 14px 16px; margin: 0; }
.cg-faint { color: var(--faint); }

/* The game shown: its row in Rounds and its results in the crosstable */
.chess-g.chess-on td { background: #f8edeb; }
.chess-xg.chess-on, .chess-xg.chess-on:hover { background: #B33A30; color: #fff; }

/* About 70/30: the board column takes 70% (keeping 264px for the moves, and short enough that the
   whole viewer, eval graph and captured-piece rows included, fits the window), the moves the rest. */
.cg { display: grid; grid-template-columns: minmax(0, min(70%, 100% - 264px, max(420px, 100vh - 284px))) minmax(0, 1fr); gap: 14px 24px; padding: 12px 16px 16px; background: var(--surface-2); text-align: left; }

/* Board column: players above and below, the eval bar beside the board */
.cg-left { min-width: 0; }
.cg-player { display: flex; align-items: baseline; gap: 8px; margin: 0; padding: 4px 0 4px 32px; font-size: 14px; white-space: nowrap; }
.cg-pname { overflow: hidden; text-overflow: ellipsis; font-weight: 600; padding: 3px; margin: -3px; }
.cg-title { color: var(--muted); font-weight: 700; font-size: 12px; }
.cg-elo { color: var(--muted); font-variant-numeric: tabular-nums; }
/* Clocks: tabular figures in a box as wide as 1:59:59, so a ticking clock (or one dropping under
   the hour) never moves anything. A live game's running clock is drawn dark (chess-game.js). Its
   border is taken back from the line's height, so the clocks showing from the first move on don't
   push the board down. */
.cg-clk { flex: none; margin: -1px 0 -1px auto; min-width: 7ch; padding: 0 6px; border: 1px solid transparent; border-radius: 3px; font-weight: 600; font-variant-numeric: tabular-nums; text-align: center; color: var(--ink); }
.cg-clk:empty { display: none; }
.cg-clk.cg-run { background: #1A1410; border-color: #1A1410; color: #fff; }
/* Captured pieces under each player's name (chess-game.js capRow): the side's label, the pieces it
   took and its score. One line of fixed height, there from the start position, so the board never
   moves as pieces come off. Short of room the pieces fan out: each but the last narrows, its piece
   overhanging the next, while the label and score keep their full width. */
.cg-cap { display: flex; align-items: center; gap: 8px; height: 22px; margin: 0; padding: 0 0 2px 32px; font-size: 12px; white-space: nowrap; color: var(--muted); }
.cg-cap-h, .cg-cap-sc { flex: none; }
.cg-cap-ps { display: flex; flex: 0 1 auto; min-width: 0; }
.cg-cap-i { flex: 0 1 18px; min-width: 0; }
.cg-cap-i:last-child { flex-shrink: 0; }
.cg-cap-i svg { display: block; width: 18px; height: 18px; overflow: visible; }
.cg-cap-sc { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--ink); }
.cg-boardrow { display: grid; grid-template-columns: 24px minmax(0, 1fr); gap: 8px; }
.cg-bar { position: relative; overflow: hidden; background: #1A1410; border: 1px solid #1A1410; cursor: default; }
.cg-fill { position: absolute; left: 0; right: 0; bottom: 0; height: 50%; background: #fff; transition: height 300ms ease; }
.cg-bar.cg-flip .cg-fill { top: 0; bottom: auto; }
/* The eval at the end of the side ahead: dark on White's fill, white on Black's */
.cg-evn { position: absolute; left: 0; right: 0; bottom: 4px; font: 600 11px/1 var(--display); font-variant-numeric: tabular-nums; text-align: center; white-space: nowrap; color: #1A1410; pointer-events: none; }
.cg-evn.cg-evb { top: 4px; bottom: auto; color: #fff; }
.cg-bar.cg-flip .cg-evn { top: 4px; bottom: auto; }
.cg-bar.cg-flip .cg-evn.cg-evb { top: auto; bottom: 4px; }
.cg-board { display: block; width: 100%; aspect-ratio: 1; background: #fff; border: 1px solid #1A1410; }
.cg-board use { transition: transform 170ms cubic-bezier(.25, 1, .5, 1); }
.cg-board.cg-still use { transition: none; }
.cg-hl rect { fill: rgba(255, 199, 46, .5); }
/* Playing on: the picked piece's square, its legal squares, a dragged piece, the engine's arrow */
.cg-board { cursor: pointer; touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
.cg-board.cg-drag { cursor: grabbing; }
.cg-board use.cg-lift { transition: none; }
.cg-sel { fill: rgba(20, 160, 150, .35); }
.cg-dots { pointer-events: none; }
.cg-dots circle { fill: rgba(26, 20, 16, .28); }
.cg-dots .cg-ring { fill: none; stroke: rgba(26, 20, 16, .28); stroke-width: .08; }
.cg-arrow { fill: rgba(20, 160, 150, .8); pointer-events: none; }
.cg-coords text { font: 600 0.2px var(--body, sans-serif); user-select: none; }
.cg-on-light { fill: #B33A30; }
.cg-on-dark { fill: #fff; }

/* Opening, the move shown, its eval and note, controls, the moves */
.cg-right { display: flex; flex-direction: column; min-width: 0; gap: 10px; }
.cg-opening { margin: 0; font-family: var(--display); font-size: 19px; font-weight: 600; line-height: 1.2; color: var(--ink); }
.cg-now { display: flex; align-items: baseline; gap: 12px; margin: 0; font-size: 15px; font-weight: 600; }
.cg-note { margin: 0; min-height: 1.3em; font-size: 14px; color: var(--muted); }
.cg-ctl { display: flex; gap: 6px; }
.cg-btn { display: inline-grid; place-items: center; width: 40px; height: 32px; padding: 0; border: 1px solid var(--line-strong); border-radius: 4px; background: var(--surface); color: var(--ink); cursor: pointer; }
.cg-btn:hover:not(:disabled) { background: var(--surface-2); }
.cg-btn:disabled { color: var(--faint); cursor: default; }
.cg-btn[data-go="flip"] { margin-left: auto; }
/* The engine: its eval, depth and best move after the move shown; its toggle and the arrow's */
.cg-now { flex-wrap: wrap; row-gap: 2px; }
.cg-now > span:empty { display: none; }
.cg-ev { font-weight: 700; font-variant-numeric: tabular-nums; color: var(--ink); }
.cg-ed, .cg-eb { font-size: 14px; font-weight: 500; font-variant-numeric: tabular-nums; color: var(--muted); }
.cg-eng { display: flex; gap: 6px; }
.cg-tog { height: 28px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 4px; background: var(--surface); color: var(--ink); font: 500 13px var(--body); cursor: pointer; }
.cg-tog:hover:not(:disabled) { background: var(--surface-2); }
.cg-tog[aria-pressed="true"] { background: #1A1410; border-color: #1A1410; color: #fff; }
.cg-tog:disabled { cursor: default; }
/* Where a game opens, at the row's end: it names the choice, so it has no pressed state */
.cg-openat { margin-left: auto; }
/* A live game has its state there instead: Live while the board follows it, else Back to live.
   One height either way, so the switch moves nothing. */
.cg-lv { display: flex; align-items: center; height: 28px; margin-left: auto; }
.cg-lv[hidden] { display: none; }
.cg-onair, .cg-golive { display: inline-flex; align-items: center; gap: 6px; }
.cg-onair { padding: 0 2px; color: var(--loss); font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; }
.cg-onair::before, .cg-golive::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.cg-onair::before { animation: pulse 1.6s ease-in-out infinite; }
.cg-tog.cg-golive { border-color: var(--loss); color: var(--loss); font-weight: 600; }
.cg-tog.cg-golive:hover { background: var(--loss-bg); }
@media (prefers-reduced-motion: reduce) {
  .cg-onair::before { animation: none; }
}
/* The viewer's line */
.cg-var { border: 1px solid var(--line); background: var(--surface); padding: 8px 10px; }
.cg-var-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cg-var-h { font-family: var(--display); font-size: 17px; font-weight: 600; color: var(--ink); }
.cg-var-mv { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px; margin: 6px 0 0; font-size: 14px; }
.cg-vn { margin-left: 4px; color: var(--faint); font-variant-numeric: tabular-nums; }
.cg-vn:first-child { margin-left: 0; }
.cg-vm { padding: 2px 5px; border: 0; background: none; font: inherit; color: var(--ink); cursor: pointer; }
.cg-vm:hover { background: var(--surface-2); }
.cg-vm[aria-current] { background: #B33A30; color: #fff; }
.cg-mv.cg-from { outline: 1px solid #B33A30; outline-offset: -1px; }
/* The moves fill the column down to the board's foot: no height of their own, so they never make
   the panel taller than the board. */
.cg-moves { flex: 1 1 0; height: 0; min-height: 160px; list-style: none; margin: 0; padding: 0; overflow-y: auto; position: relative; border: 1px solid var(--line); background: var(--surface); font-size: 14px; }
.cg-moves li { display: grid; grid-template-columns: 40px 1fr 1fr; }
.cg-moves li + li { border-top: 1px solid var(--line); }
.cg-n { padding: 4px 8px; color: var(--faint); font-variant-numeric: tabular-nums; text-align: right; }
.cg-mv { padding: 4px 8px; border: 0; background: none; font: inherit; color: var(--ink); text-align: left; cursor: pointer; }
.cg-mv:hover:not(:disabled) { background: var(--surface-2); }
.cg-mv[aria-current] { background: #B33A30; color: #fff; }
.cg-mv:disabled { color: var(--faint); cursor: default; }
.cg-glyph { margin-left: 1px; font-weight: 700; }
.cg-inacc .cg-glyph { color: #2f7fb8; }
.cg-mistake .cg-glyph { color: #c27c00; }
.cg-blunder .cg-glyph { color: #c0392b; }
.cg-mv[aria-current] .cg-glyph { color: #fff; }

/* Docked: one column, the board the dock's width, the moves filling the rest of its height */
@media (min-width: 761px) {
  .cg-dock .cgv { display: flex; flex-direction: column; min-height: 100%; }
  .cg-dock .cgv-body, .cg-dock .cg-game { display: flex; flex-direction: column; flex: 1 0 auto; }
  .cg-dock .cg { display: flex; flex-direction: column; flex: 1 0 auto; gap: 14px; padding: 10px 14px 14px; }
  .cg-dock .cg-right { flex: 1 0 auto; }
  .cg-dock .cg-moves { min-height: 140px; }
}

@media (max-width: 760px) {
  .cg { grid-template-columns: minmax(0, 1fr); padding: 10px 10px 12px; }
  .cg-moves { flex: none; height: auto; min-height: 0; max-height: 220px; }
  .cgv-bar { padding: 8px 10px 0 12px; }
  /* The dock is a sheet from the bottom, the page's top still showing above it */
  .cg-dock {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: calc(100vh - 56px);
    border-radius: 12px 12px 0 0;
    transform: translateY(calc(100% + 12px));
  }
}
@supports (height: 100dvh) {
  @media (max-width: 760px) { .cg-dock { max-height: calc(100dvh - 56px); } }
}
@media (max-width: 600px) {
  .cg-slot.cg-open { margin-top: 20px; }
}
