@font-face {
  font-family: 'Instrument Sans';
  src: url('fonts/instrument-sans.woff2') format('woff2');
  font-weight: 400 700;
  font-display: swap;
}

:root {
  --sq-light: #D3DFDB;
  --sq-dark: #7A9A94;
  --butter: #F2CF63;
  --ember: #D4533E;
  --board: min(calc(100vw - 32px), calc(100dvh - 248px), 640px);
  --radius: 10px;
  color-scheme: light;
}

body[data-field='light'] {
  --field: #E4E9E7;
  --ink: #17262A;
  --ink-2: #5B6D6E;
  --line: rgba(23, 38, 42, 0.12);
  --chip: rgba(23, 38, 42, 0.065);
  --chip-strong: rgba(23, 38, 42, 0.12);
  --sheet: #F3F6F5;
  --sheet-raised: #FFFFFF;
  --focus: #3E6A64;
}

body[data-field='dark'] {
  --field: #1B2A2D;
  --ink: #E2EBE9;
  --ink-2: #8FA4A2;
  --line: rgba(226, 235, 233, 0.13);
  --chip: rgba(226, 235, 233, 0.075);
  --chip-strong: rgba(226, 235, 233, 0.15);
  --sheet: #243538;
  --sheet-raised: #34494E;
  --focus: #F2CF63;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--field);
  color: var(--ink);
  font-family: 'Instrument Sans', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  transition: background-color 0.5s ease, color 0.5s ease;
}

button { font: inherit; color: inherit; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- layout ---------- */

.app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: var(--board);
  grid-template-areas: 'top' 'stage' 'bottom' 'moves' 'actions';
  justify-content: center;
  align-content: center;
  row-gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 16px max(10px, env(safe-area-inset-bottom));
}

#bar-top { grid-area: top; }
#bar-bottom { grid-area: bottom; }

@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  :root { --board: min(calc(100dvh - 64px), calc(100vw - 420px), 860px); }
  .app {
    grid-template-columns: var(--board) 300px;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      'stage top'
      'stage moves'
      'stage bottom'
      'stage actions';
    column-gap: 36px;
    row-gap: 10px;
  }
  .stage { align-self: center; }
}

/* ---------- players ---------- */

.player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 2px;
  transition: opacity 0.3s ease;
}

body[data-view='table'] #bar-top.black { transform: rotate(180deg); }

.disc {
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px var(--ink);
}
.disc.w { background: #F7F8F6; }
.disc.b { background: #263234; }

.name { font-weight: 600; }
.player:not(.active) .name { color: var(--ink-2); font-weight: 500; }
.status { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.status.check { color: var(--ember); font-weight: 600; }
body[data-field='dark'] .status.check { color: #F08A75; }

.caps {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 0;
  overflow: hidden;
}
.caps .pc { width: 20px; height: 20px; margin-right: -7px; flex: none; }
.caps .gap { width: 10px; flex: none; }
.caps .diff { margin-left: 12px; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

.clock {
  flex: none;
  min-width: 78px;
  border: 0;
  padding: 5px 10px;
  border-radius: 9px;
  background: var(--chip);
  font-size: 21px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
  text-align: center;
  cursor: pointer;
}
.clock.running { background: var(--ink); color: var(--field); }
.clock.low { color: var(--ember); }
.clock.running.low { background: var(--ember); color: #fff; }
.clock.paused { opacity: 0.55; }

/* ---------- board ---------- */

.stage {
  grid-area: stage;
  position: relative;
  width: var(--board);
  height: var(--board);
}
/* The shadow lives on the stage so it stays put while the board turns. */
.stage::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(10, 25, 25, 0.22), 0 22px 44px -22px rgba(10, 25, 25, 0.5);
}

.board {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  cursor: default;
}
.board .row { display: contents; }

.sq { position: relative; outline: none; }
.sq.l { background: var(--sq-light); }
.sq.d { background: var(--sq-dark); }
.sq.last::before, .sq.sel::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(242, 207, 99, 0.5);
}
.sq.sel::before { background: rgba(242, 207, 99, 0.78); }
.sq.check::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, rgba(222, 72, 52, 0.95) 0%, rgba(222, 72, 52, 0.55) 38%, rgba(222, 72, 52, 0) 70%);
}
.sq.move::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28%;
  height: 28%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(18, 44, 44, 0.3);
}
.sq.capture::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, transparent 0 60%, rgba(18, 44, 44, 0.3) 61% 72%, transparent 73%);
}
.sq.hover { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.6); }
.sq:focus-visible { box-shadow: inset 0 0 0 3px var(--butter); z-index: 1; }

.coord {
  position: absolute;
  font-size: clamp(9px, calc(var(--board) / 50), 13px);
  font-weight: 600;
  line-height: 1;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
.sq.l .coord { color: var(--sq-dark); }
.sq.d .coord { color: var(--sq-light); }
.coord.rank { top: 4px; left: 4px; }
.coord.file { bottom: 3px; right: 4px; }
.turning .coord { opacity: 0; }
body.no-coords .coord { display: none; }

.pieces { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.piece {
  position: absolute;
  left: 0;
  top: 0;
  width: 12.5%;
  height: 12.5%;
  transition: transform 0.18s cubic-bezier(0.25, 0.8, 0.25, 1), opacity 0.18s ease;
  will-change: transform;
}
.piece svg { display: block; width: 100%; height: 100%; transition: transform 0.12s ease; }
.piece.dragging { transition: none; z-index: 3; }
.piece.dragging svg { transform: scale(1.14); filter: drop-shadow(0 8px 8px rgba(5, 20, 20, 0.28)); }
.piece.gone { opacity: 0; }
.piece.enter { animation: enter 0.2s ease-out; }
body[data-view='table'] .piece.b svg { transform: rotate(180deg); }
body[data-view='table'] .piece.b.dragging svg { transform: rotate(180deg) scale(1.14); }
.no-anim .piece, .no-anim .piece svg { transition: none !important; }

@keyframes enter { from { opacity: 0; transform: var(--to) scale(0.8); } }

/* piece colours */
.pc > * { stroke-width: 2.6; stroke-linejoin: round; stroke-linecap: round; }
.pc-w > * { fill: #F7F8F6; stroke: #22302F; }
.pc-b > * { fill: #263234; stroke: #111A1B; }
.pc .d { fill: none; }
.pc-w .d { stroke: #22302F; }
.pc-b .d { stroke: #A9BDB9; }
.pc .eye { stroke: none; }
.pc-w .eye { fill: #22302F; }
.pc-b .eye { fill: #A9BDB9; }

/* ---------- overlays on the board ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
}

.promo { background: rgba(12, 28, 28, 0.38); }
.promo-card {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 12px 12px 6px;
  border-radius: 16px;
  background: var(--sheet);
  box-shadow: 0 20px 50px -20px rgba(5, 15, 15, 0.6);
}
.promo-card.flip { transform: rotate(180deg); }
.promo-row { display: flex; gap: 6px; }
.promo-row button {
  width: min(calc(var(--board) / 6), 92px);
  aspect-ratio: 1;
  padding: 4px;
  border: 0;
  border-radius: 12px;
  background: var(--sq-light);
  cursor: pointer;
}
.promo-row button:nth-child(even) { background: var(--sq-dark); }
.promo-row button:hover { box-shadow: inset 0 0 0 3px var(--butter); }
.promo-row svg { width: 100%; height: 100%; display: block; }

.handoff {
  border: 0;
  padding: 0;
  background: var(--field);
  color: var(--ink);
  cursor: pointer;
  transition: background-color 0.5s ease, color 0.5s ease;
}
.handoff-inner { display: grid; justify-items: center; gap: 10px; }
.handoff-inner.flip { transform: rotate(180deg); }
.handoff-disc { width: 28px; height: 28px; border-radius: 50%; box-shadow: 0 0 0 2px var(--ink); }
.handoff-disc.w { background: #F7F8F6; }
.handoff-disc.b { background: #263234; }
.handoff-title { font-size: 24px; font-weight: 600; letter-spacing: -0.01em; }
.handoff-sub { color: var(--ink-2); }

.result { background: rgba(12, 28, 28, 0.26); }
.result-card {
  position: relative;
  width: min(300px, 82%);
  padding: 26px 22px 20px;
  border-radius: 18px;
  background: var(--sheet);
  text-align: center;
  box-shadow: 0 24px 60px -24px rgba(5, 15, 15, 0.6);
  animation: rise 0.32s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.result-title { margin: 0; font-size: 26px; font-weight: 600; letter-spacing: -0.015em; line-height: 1.15; }
.result-sub { margin: 6px 0 18px; color: var(--ink-2); }
.result-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.result-close { position: absolute; top: 8px; right: 8px; }

@keyframes rise { from { opacity: 0; transform: translateY(10px) scale(0.98); } }

/* ---------- moves ---------- */

.moves-wrap {
  grid-area: moves;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  min-height: 34px;
}

.moves {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
  font-variant-numeric: tabular-nums;
}
.moves::-webkit-scrollbar { display: none; }
.moves li { display: flex; align-items: center; }
.moves .num { color: var(--ink-2); font-size: 13px; margin-right: 1px; }
.moves button {
  border: 0;
  background: none;
  padding: 4px 6px;
  border-radius: 7px;
  cursor: pointer;
}
.moves button:hover { background: var(--chip); }
.moves button[aria-current='true'] { background: var(--chip-strong); font-weight: 600; }
.moves .empty { color: var(--ink-2); padding: 4px 2px; }

.live-btn {
  flex: none;
  border: 0;
  border-radius: 9px;
  padding: 6px 12px;
  background: var(--ink);
  color: var(--field);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  .moves-wrap { flex-direction: column; align-items: stretch; min-height: 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 8px 0; }
  .moves {
    display: grid;
    grid-template-columns: 2.6em 1fr 1fr;
    align-content: start;
    gap: 2px 4px;
    overflow-x: hidden;
    overflow-y: auto;
    white-space: normal;
    scrollbar-width: thin;
  }
  .moves li { display: contents; }
  .moves .num { align-self: center; text-align: right; padding-right: 6px; }
  .moves button { text-align: left; }
  .moves .empty { grid-column: 1 / -1; }
}

/* ---------- actions ---------- */

.actions {
  grid-area: actions;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
}

.act {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 4px 7px;
  border: 0;
  border-radius: 12px;
  background: none;
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
}
.act:hover:not(:disabled) { background: var(--chip); }
.act:disabled { opacity: 0.32; cursor: default; }
.act svg, .icon-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- buttons ---------- */

.btn {
  border: 0;
  border-radius: 11px;
  padding: 10px 16px;
  background: var(--chip);
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: var(--chip-strong); }
.btn.primary { background: var(--ink); color: var(--sheet); }
.btn.primary:hover { opacity: 0.9; }
.btn.danger { background: var(--ember); color: #fff; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 10px;
  background: none;
  cursor: pointer;
}
.icon-btn:hover { background: var(--chip); }
.icon-btn svg { width: 20px; height: 20px; }

.link {
  border: 0;
  background: none;
  padding: 6px 10px;
  color: var(--ink-2);
  font-size: 14px;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

/* ---------- dialogs ---------- */

dialog.sheet {
  width: min(400px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: 0 30px 80px -24px rgba(5, 15, 15, 0.55);
}
dialog.sheet::backdrop { background: rgba(10, 22, 22, 0.42); }
dialog.sheet.flip { transform: rotate(180deg); }
dialog.sheet[open] { animation: rise 0.22s cubic-bezier(0.2, 0.8, 0.2, 1); }
dialog.sheet.flip[open] { animation: none; }

.ask { padding: 22px; }
.ask-title { margin: 0 0 6px; font-size: 19px; font-weight: 600; letter-spacing: -0.01em; }
.ask-body { margin: 0 0 20px; color: var(--ink-2); }
.ask-body:empty { display: none; }
.ask-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }

.menu form { padding: 16px 20px 18px; }
.menu-head { display: flex; align-items: center; justify-content: space-between; margin-right: -8px; }
.menu-head h2 { margin: 0; font-size: 19px; font-weight: 600; letter-spacing: -0.01em; }
.menu-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 20px; }

.setting { margin: 0 0 18px; padding: 0; border: 0; min-width: 0; }
.setting legend { margin-bottom: 8px; padding: 0; font-size: 14px; font-weight: 600; }
.seg { display: flex; gap: 2px; padding: 3px; border-radius: 11px; background: var(--chip); }
.seg label { position: relative; flex: 1; }
.seg input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.seg span { display: block; padding: 7px 4px; border-radius: 8px; text-align: center; font-size: 14px; font-variant-numeric: tabular-nums; }
.seg input:checked + span { background: var(--sheet-raised); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); }
.seg-wrap { flex-wrap: wrap; }
.seg-wrap label { flex: 1 0 30%; }
.hint { margin: 7px 2px 0; font-size: 13px; color: var(--ink-2); }

.toggles { border-bottom: 1px solid var(--line); }
.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 0;
  border-top: 1px solid var(--line);
  cursor: pointer;
}
.toggle b { display: block; font-size: 14.5px; font-weight: 600; }
.toggle small { display: block; margin-top: 2px; font-size: 13px; color: var(--ink-2); }
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: none;
  width: 42px;
  height: 26px;
  margin: 0;
  border-radius: 13px;
  background: var(--chip-strong);
  cursor: pointer;
  transition: background-color 0.2s ease;
}
.toggle input::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s ease;
}
.toggle input:checked { background: var(--sq-dark); }
.toggle input:checked::before { transform: translateX(16px); }

.menu-foot { margin: 14px 2px 0; font-size: 13px; color: var(--ink-2); }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  z-index: 50;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--field);
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.4);
  animation: rise 0.2s ease-out;
}

.noscript { padding: 24px; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  body, .handoff { transition: none; }
  .piece, .piece svg { transition: none; }
  .result-card, dialog.sheet[open], .toast, .piece.enter { animation: none; }
}
