:root {
  color-scheme: light;
  font-family: "Segoe UI", Arial, sans-serif;
  color: #263144;
  background: #e8e9ef;
  --ink: #263144;
  --muted: #536076;
  --rule: #7d8b9b55;
  --accent: #46557e;
  --move-fill: #505f80;
  --move-ink: #fbfaff;
  --standard-jump-fill: #a33d4e;
  --long-jump-fill: #744e98;
  --jump-stripe: #f9efff33;
  --path-fill: #286e77;
  --sightline: #ffc4dc;
  --tile-trail-color: #a04775;
  --grid-line: #ffffff40;
  --wrap-line: #6d6d8a;
  --crease-line: color-mix(in srgb, var(--wrap-line) 76%, transparent);
  --light-square: #aabacf;
  --dark-square: #859ab3;
  --selected-sightline: #931449;
  --capture-ring: #ffcab5;
  --selection-edge: #885d53;
  --selection-fill: #b4939a;
  --white-turn-dot: #fff9ed;
  /* Recolor white source artwork to porcelain (#fff9ed) in light mode. */
  --white-piece-tint: brightness(94%) sepia(19%);
  --action-motion: #425476;
  --action-support: #365e60;
  --action-impact: #74465e;
  --action-disassemble: #9d3441;
  --action-ink: #fff9f2;
}

* { box-sizing: border-box; }
body {
  margin: 0;
}
main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  min-height: 22rem;
  padding: 0 clamp(16px, 2vw, 32px) 16px;
}
button, select { color: inherit; font: inherit; cursor: pointer; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
[hidden] { display: none !important; }

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 21px 0 17px;
  border-bottom: 1px solid var(--rule);
}
.masthead-controls, .appearance-control { display: flex; align-items: center; }
.masthead-controls { margin-inline-start: auto; gap: clamp(12px, 2vw, 26px); }
.appearance-control {
  gap: 8px;
  color: var(--muted);
  font-size: .8rem;
  font-weight: 500;
}
.appearance-control select {
  min-height: 34px;
  padding: 4px 8px;
  border: 1px solid var(--control-edge, #8290a5);
  border-radius: 6px;
  background-color: var(--control-surface, #f6f5fa);
  color: var(--ink);
}
.appearance-control select:hover { border-color: var(--accent); }
h1, h2, p { margin: 0; }
h1, h2 { font-family: Baskerville, Georgia, serif; font-weight: 400; }
h1 {
  display: flex;
  align-items: center;
  gap: .28em;
  font-size: clamp(2rem, 4vw, 3rem);
  line-height: 1;
  letter-spacing: -.05em;
}
h1::before {
  content: "";
  width: .48em;
  height: .68em;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  background: conic-gradient(from 45deg, #677b98 0 25%, #b1c9c8 25% 50%, #e4ced7 50% 75%, #8d89a6 75%);
}
.dimensions {
  display: inline-flex;
  align-items: center;
  gap: .6em;
  color: var(--muted);
  font: 600 .78rem/1 Consolas, monospace;
  letter-spacing: .12em;
}
.dimensions::before {
  content: "";
  width: 12px;
  height: 12px;
  background: conic-gradient(#8794a8 25%, #e3e7ec 0 50%, #8794a8 0 75%, #e3e7ec 0) 0 0 / 6px 6px;
}

.workspace {
  display: grid;
  grid-template: "board tools" minmax(0, 1fr) / minmax(0, 1fr) clamp(260px, 23vw, 330px);
  gap: clamp(18px, 2vw, 34px);
  align-items: start;
  padding-top: 21px;
}
.board-area {
  grid-area: board;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  align-self: stretch;
  min-width: 0;
  min-height: 0;
}
.board-frame {
  position: relative;
  overflow: clip;
  outline: 1px solid #758395;
  border-radius: 5px;
  background-color: #c4cad5;
  box-shadow: 0 0 0 3px #ffffff80, 0 8px 26px #40536e24;
  touch-action: none;
  user-select: none;
  cursor: default;
}
.board-frame:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.board-grid {
  --cell-size: 34px;
  --crease-width: clamp(1px, calc(var(--cell-size) * .04), 2px);
  --action-band-height: calc(var(--cell-size) * .42);
  --grid-columns: 1;
  --grid-rows: 1;
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: top left;
  will-change: transform;
  cursor: default;
  width: calc(var(--cell-size) * var(--grid-columns));
  aspect-ratio: var(--grid-columns) / var(--grid-rows);
  display: grid;
  grid-template-rows: repeat(var(--grid-rows), minmax(0, 1fr));
}
.board-row {
  display: grid;
  grid-template-columns: repeat(var(--grid-columns), minmax(0, 1fr));
  contain: layout paint;
  overflow-clip-margin: 1px;
}
/* The overview can display thousands of cells. Keep each painted row
   together when an animated push changes pieces across many copies. */
.board-grid.overview .board-row { will-change: transform; }
.square {
  --edge-width: clamp(1px, calc(var(--cell-size) * .055), 3px);
  display: grid;
  place-items: center;
  padding: 0;
  position: relative;
  /* Artwork, frames and action controls share the grid-line centerlines. */
  border: 0;
  /* Each tile paints an equal half of every adjoining stroke. */
  background-origin: border-box;
  background-color: var(--square-face);
  background-image:
    linear-gradient(to right,
      color-mix(in srgb, var(--west-line, var(--grid-line)) var(--west-line-ink, 100%), transparent),
      transparent var(--west-width, 1px), transparent calc(100% - var(--east-width, 1px)),
      color-mix(in srgb, var(--east-line, var(--grid-line)) var(--east-line-ink, 100%), transparent)),
    linear-gradient(to bottom,
      color-mix(in srgb, var(--north-line, var(--grid-line)) var(--north-line-ink, 100%), transparent),
      transparent var(--north-width, 1px), transparent calc(100% - var(--south-width, 1px)),
      color-mix(in srgb, var(--south-line, var(--grid-line)) var(--south-line-ink, 100%), transparent)),
    var(--square-pattern, none);
  background-repeat: no-repeat;
  border-radius: 0;
  cursor: inherit;
  -webkit-tap-highlight-color: transparent;
}
.square.occupied, .square.destination, .square.special-target { cursor: pointer; }
.square.cannon-part, .motion-cannon { overflow: hidden; }
.square[data-trail-lit] {
  --pulse-face: color-mix(in srgb, var(--tile-trail-color) var(--tile-trail-strength), var(--square-face));
  background-color: var(--pulse-face);
}
/* Adjacent lit tiles underpaint each other where fractional compositing can expose the board frame. */
.square { --edge-paint: var(--frame-color, var(--pulse-face, var(--square-face))); }
.square:is(.join-east, [data-trail-east]) { box-shadow: 1px 0 var(--edge-paint); }
.square:is(.join-south, [data-trail-south]) { box-shadow: 0 1px var(--edge-paint); }
.square:is(.join-east, [data-trail-east]):is(.join-south, [data-trail-south]) {
  box-shadow: 1px 0 var(--edge-paint), 0 1px var(--edge-paint);
}
.impact-waves {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
}
.board-motion {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
  contain: strict;
  overflow: clip;
}
.motion-group, .board-motion .motion-piece { position: absolute; top: 0; left: 0; }
.motion-sheet { position: absolute; inset: 0; transform-origin: 0 0; will-change: transform; }
.board-motion .motion-piece {
  width: var(--cell-size);
  height: var(--cell-size);
  pointer-events: none;
}
.board-motion .motion-piece > img.piece { scale: var(--motion-size, 1); }
.pivot-line {
  position: absolute;
  left: 50%;
  top: 50%;
  height: max(2px, calc(var(--cell-size) * .067));
  border-radius: 100vmax;
  background: linear-gradient(to right, transparent, var(--selected-sightline) 33% 67%, transparent);
}
.motion-hidden > img.piece, .motion-hidden > .round-turns,
.motion-hidden:is(.ammo-pawn, .ammo-phalanx, .ammo-jester)::before { visibility: hidden; }
/* The charge sits on the carriage. Its glyph stays upright on every copy. */
.ammo-pawn::before, .ammo-phalanx::before, .ammo-jester::before {
  content: "";
  display: block;
  box-sizing: border-box;
  aspect-ratio: 1;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 74%;
  border: 1px solid;
  pointer-events: none;
}
.ammo-pawn::before {
  border-radius: 50%;
  border-color: #705229;
  background-color: #eed09a;
  background-image: url("../assets/pieces/pawn.svg#black");
}
.ammo-phalanx::before {
  border-radius: 10% 10% 50% 50% / 10% 10% 42% 42%;
  border-color: #b0d6ca;
  background-color: #2c5557;
  background-image: url("../assets/pieces/phalanx.svg#white");
  filter: brightness(93%) sepia(12%);
}
.ammo-jester::before {
  border-radius: 36% 36% 25% 25%;
  border-color: #edc4de;
  background-color: #754061;
  background-image: url("../assets/pieces/jester.svg#white");
  background-size: 80%;
}
:is(.square, .motion-piece):is(.ammo-pawn, .ammo-phalanx, .ammo-jester)::before {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: 61%;
  translate: -50% -50%;
  width: min(78%, max(13px, 63%));
}
.board-motion .motion-projectile::before {
  top: 50%;
  width: 42%;
  box-shadow: 0 0 0 1px #33282b80;
}
.round-turns {
  display: inline-grid;
  place-items: center;
  box-sizing: border-box;
  border: 1px solid #efddb6;
  border-radius: 3px;
  background: #32282b;
  color: #fff1cb;
  font: 700 11px/1 system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
:is(.square, .motion-piece) > .round-turns {
  position: absolute;
  z-index: 2;
  top: 2%;
  right: 2%;
  width: 38%;
  height: 38%;
  font-size: clamp(4px, calc(var(--cell-size) * .28), 21px);
}
.legend .round-turns { width: 15px; height: 15px; }
:is(.square, .motion-piece) > .round-turns.transferred-round {
  right: 1%;
  width: 40%;
  font-size: clamp(4px, calc(var(--cell-size) * .23), 19px);
}
.board-frame.is-panning, .board-frame.is-panning .square:hover { cursor: grabbing; }
.square.west-edge { --west-line: var(--wrap-line); --west-width: var(--edge-width); }
.square.east-edge { --east-line: var(--wrap-line); --east-width: var(--edge-width); }
.square.north-edge { --north-line: var(--wrap-line); --north-width: var(--edge-width); }
.square.south-edge { --south-line: var(--wrap-line); --south-width: var(--edge-width); }
.board-row.crease-above { --north-line: var(--crease-line); --north-width: var(--crease-width); }
.board-row.crease-below { --south-line: var(--crease-line); --south-width: var(--crease-width); }
.square.light { --square-face: var(--light-square); }
.square.dark { --square-face: var(--dark-square); }
.square:focus-visible { z-index: 2; outline: 2px solid #372d62; outline-offset: -2px; }
.coord {
  position: absolute;
  top: 3%;
  left: 5%;
  color: #25344a;
  font: 600 clamp(4px, calc(var(--cell-size) * .29), 20px)/1 Consolas, monospace;
  pointer-events: none;
}
.board-grid.hide-labels .coord { display: none; }
.square.destination { --square-face: var(--move-fill); }
:is(.show-learned-jumps, .legend) .knight-standard { --move-fill: var(--standard-jump-fill); }
:is(.square, .legend i).knight-rested {
  --move-fill: var(--long-jump-fill);
  --square-pattern: repeating-linear-gradient(135deg, transparent 0 5px, var(--jump-stripe) 5px 6px);
}
.legend i.knight-rested { background-image: var(--square-pattern); }
.square.destination .coord { color: var(--move-ink); }
.square.destination:focus-visible { outline: 2px dashed var(--move-ink); outline-offset: -2px; }
.square.on-path { --square-face: var(--path-fill); --frame-color: #b2eeea; }
.square > .sightlines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}
.sightlines path {
  fill: none;
  stroke: var(--sightline);
  stroke-width: clamp(1.5px, calc(var(--cell-size) * .075), 4px);
  stroke-dasharray: calc(var(--cell-size) * .18) calc(var(--cell-size) * .075);
  vector-effect: non-scaling-stroke;
}
.square.selected .sightlines path { stroke: var(--selected-sightline); }
.sightlines.ghost-trace path {
  stroke: var(--move-ink);
  stroke-linecap: round;
  stroke-dasharray: 1px calc(var(--cell-size) * .16);
}
.square.selected .ghost-trace path { stroke: var(--selection-edge); }
.sightlines:is(.enfilade-trace, .royal-skewer-trace, .check-trace) path {
  stroke-dasharray: none;
  stroke-linecap: round;
}
.sightlines:is(.enfilade-trace, .royal-skewer-trace) :is(path, circle) { stroke: var(--move-ink); }
.sightlines:is(.enfilade-trace, .royal-skewer-trace) path.capture-leg {
  stroke: var(--sightline);
  stroke-dasharray: 0 calc(var(--cell-size) * .25);
}
.square.selected :is(.enfilade-trace, .royal-skewer-trace) path { stroke: var(--selection-edge); }
.sightlines.check-trace :is(path, circle) { stroke: var(--check-line, #8d314c); }
.sightlines.check-trace path { filter: drop-shadow(0 0 1px var(--check-halo, #fff1df)); }
.sightlines circle {
  fill: var(--path-fill);
  stroke-width: 2px;
  vector-effect: non-scaling-stroke;
}
.sightlines.check-trace circle { fill: var(--check-halo, #fff1df); }
.square:has(> .sightlines) > :is(.coord, img.piece:not(.cannon-sprite)) { z-index: 1; }
.square.selected {
  --square-face: var(--selection-fill);
  --frame-color: var(--selection-edge);
  --frame-width: clamp(1px, calc(var(--cell-size) * .065), 3px);
}
.square.in-check { --square-face: #f1c5ce; --frame-color: #95425f; --frame-width: 2px; }
/* The outermost pixels carry the frame color even when the camera is between pixels. */
.square:is(.selected, .on-path, .in-check):not(.cannon-part.selected) {
  --inner-size: calc(100% - 2 * var(--frame-width, 2px));
  --inner-color: var(--pulse-face, var(--square-face));
  background-color: var(--frame-color);
  background-image: var(--square-pattern, none), linear-gradient(var(--inner-color), var(--inner-color));
  background-position: center;
  background-size: var(--inner-size) var(--inner-size);
}
.square.cannon-part.selected {
  background-image: none;
  box-shadow: none;
  outline: none;
}
/* Each rendered row clips its contents. Join the rails across rows and
   close just the two outside ends, even where the barrel crosses rank 18. */
.square.cannon-part.selected::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  border: 0 solid var(--selection-edge);
  border-inline-width: var(--edge-width);
}
.square.selected.cannon-cap-top::after { border-top-width: var(--edge-width); }
.square.selected.cannon-cap-bottom::after { border-bottom-width: var(--edge-width); }
.square.cannon-part.selected:focus-visible::after {
  background: radial-gradient(circle at 18% 82%, var(--selection-edge) 0 7%, transparent 8%);
}
.square.capture::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 83%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: var(--edge-width) solid var(--capture-ring);
  box-shadow: 0 0 0 1px #674e63;
  pointer-events: none;
}
:is(.square, .motion-piece) img.piece {
  position: absolute;
  left: 0;
  bottom: 3%;
  width: 100%;
  height: 77%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}
.hide-labels :is(.square, .motion-piece) img.piece { bottom: 7%; height: 86%; }
:is(.square, .motion-piece) img.piece.white,
[data-placement-side="white"] img { filter: var(--white-piece-tint); }
:is(.square, .motion-piece) img.cannon-sprite, .hide-labels :is(.square, .motion-piece) img.cannon-sprite {
  top: 0;
  bottom: auto;
  height: 300%;
  width: 100%;
  object-fit: fill;
}
:is(.square, .motion-piece) img.cannon-sprite.white { filter: brightness(0) invert(1) var(--white-piece-tint); }
:is(.square, .motion-piece) .cannon-sprite.phalanx { translate: 0 -33.333333%; }
:is(.square, .motion-piece):not(.faces-down) .cannon-sprite.warhorn, :is(.square, .motion-piece).faces-down .cannon-sprite.pawn { translate: 0 -66.666667%; }
:is(.square, .motion-piece).faces-down .cannon-sprite { transform: rotate(180deg); }
.square.cannon-part .coord { z-index: 1; }

/* Most controls use the whole tile height; the low band keeps its occupant visible. */
.board-grid .square.special-target:not(.cannon-part) img.piece {
  bottom: 41%;
  height: 54%;
}
.board-grid .square.special-target:is(.ammo-pawn, .ammo-phalanx, .ammo-jester)::before {
  top: 35%;
  width: 51%;
}
.special-target .coord { z-index: 2; }
.square.action-choices {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  place-items: stretch;
  --choice-divider: #fff8;
}
.action-glyph, .action-choice {
  z-index: 1;
  background: linear-gradient(to top, var(--action-band) var(--action-band-height), transparent var(--action-band-height));
  color: var(--action-ink);
}
.action-glyph {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.action-choice {
  position: relative;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  appearance: none;
  border: 0;
  border-radius: 0;
  cursor: inherit;
}
.action-choices .action-choice { --action-band-height: 42%; }
.action-choices .action-choice:nth-child(odd of .action-choice):nth-last-child(1 of .action-choice) {
  grid-column: 1 / -1;
}
.action-choices .action-choice:nth-child(even of .action-choice) { --split-x: 1px; }
.action-choices .action-choice:nth-child(n+3 of .action-choice) { --split-y: 1px; }
/* Center each divider on its hit boundary, including where four controls meet. */
.action-choices .action-choice::before {
  content: "";
  pointer-events: none;
  position: absolute;
  inset: 0 calc(-.5 * var(--split-x, 0px)) calc(-.5 * var(--split-y, 0px)) 0;
  translate: calc(-.5 * var(--split-x, 0px)) calc(-.5 * var(--split-y, 0px));
  border: 0 solid var(--choice-divider);
  border-width: var(--split-y, 0px) 0 0 var(--split-x, 0px);
}
.action-glyph::after, .action-choice::after, .action-symbol::after {
  content: "";
  display: block;
  background: currentColor;
  mask: var(--action-mask) center / contain no-repeat;
  rotate: var(--action-angle, 0deg);
  pointer-events: none;
}
.action-glyph::after, .action-choice::after {
  position: absolute;
  left: 50%;
  bottom: calc(var(--action-band-height) / 2);
  translate: -50% 50%;
  width: min(90%, calc(var(--cell-size) * .35));
  height: 35%;
}
.action-choice:is(:hover, :focus-visible) {
  background-color: color-mix(in srgb, var(--action-band) 25%, transparent);
}
.action-choice:focus-visible { z-index: 3; outline: 2px solid var(--action-focus, var(--action-band)); outline-offset: -2px; }
.rear-control .component-selection {
  position: absolute;
  inset: 0 0 var(--action-band-height);
  margin: 0;
  padding: 0;
  appearance: none;
  border: 0;
  background: transparent;
  cursor: inherit;
}
.rear-control .component-selection:focus-visible { z-index: 3; outline: 2px solid var(--selection-edge); outline-offset: -2px; }
.rear-control .action-choice {
  --action-focus: var(--action-ink);
  position: absolute;
  inset: auto 0 0;
  height: var(--action-band-height);
  background: var(--action-band);
}
/* Each copy already orients its cannon sprite. Keep the control on that copy's rear edge. */
.rear-control.faces-down .action-choice { inset: 0 0 auto; }
.rear-control.faces-down .component-selection { inset: var(--action-band-height) 0 0; }
.rear-control.faces-down .coord { top: auto; bottom: 3%; }
.rear-control .action-choice::after {
  top: 50%;
  bottom: auto;
  height: min(100%, calc(var(--cell-size) * .35));
  translate: -50% -50%;
}
.rear-control .action-choice:hover { background: color-mix(in srgb, var(--action-band) 86%, var(--action-ink)); }
.action-symbol {
  display: inline-grid;
  place-items: center;
  width: 1.3em;
  height: 1.3em;
  border-radius: 3px;
  color: var(--action-ink);
  background: var(--action-band);
}
.action-symbol::after { width: 79%; height: 79%; }

[data-action-kind] { --action-band: var(--action-motion); }
[data-action-kind="advance"], [data-action-kind="retreat"], [data-action-kind="sidestep"] { --action-mask: url("../assets/actions/advance.svg"); }
[data-action-kind="load"] { --action-mask: url("../assets/actions/load.svg"); --action-band: var(--action-impact); }
[data-action-kind="push"] { --action-mask: url("../assets/actions/push.svg"); }
[data-action-kind="ghost"] { --action-mask: url("../assets/actions/ghost.svg"); }
[data-action-kind="assemble"] { --action-mask: url("../assets/actions/assemble.svg"); --action-band: var(--action-support); }
[data-action-kind="disassemble"] { --action-mask: url("../assets/actions/disassemble.svg"); --action-band: var(--action-disassemble); }
[data-action-kind="muster"] { --action-mask: url("../assets/actions/muster.svg"); --action-band: var(--action-support); }
[data-action-kind="crown-pendragon"] { --action-mask: url("../assets/actions/crown-pendragon.svg"); --action-band: var(--action-support); }
[data-action-kind="fall-in-line"] { --action-mask: url("../assets/actions/fall-in-line.svg"); --action-band: var(--action-support); }
[data-action-kind="god-save"] { --action-mask: url("../assets/actions/god-save.svg"); --action-band: var(--action-support); }
[data-action-kind="double-thrust"] { --action-mask: url("../assets/actions/double-thrust.svg"); --action-band: var(--action-impact); }
[data-action-kind="en-passant"] { --action-mask: url("../assets/actions/en-passant.svg"); --action-band: var(--action-impact); }
[data-action-kind="convergence"] { --action-mask: url("../assets/actions/convergence.svg"); --action-band: var(--action-impact); }
[data-action-kind="enfilade"], [data-action-kind="royal-skewer"] {
  --action-band: var(--sightline);
  --action-ink: var(--selected-sightline);
}
[data-action-kind="enfilade"] { --action-mask: url("../assets/actions/enfilade.svg"); }
[data-action-kind="royal-skewer"] { --action-mask: url("../assets/actions/royal-skewer.svg"); }
[data-action-kind="enfilade"][data-turn="right"]::after { transform: scaleX(-1); }
[data-action-kind="move"] { --action-mask: url("../assets/actions/move.svg"); }
[data-action-kind="move"][data-icon="capture"] { --action-mask: url("../assets/actions/capture.svg"); }
[data-direction="up-right"] { --action-angle: 45deg; }
[data-direction="right"] { --action-angle: 90deg; }
[data-direction="down-right"] { --action-angle: 135deg; }
[data-direction="down"] { --action-angle: 180deg; }
[data-direction="down-left"] { --action-angle: 225deg; }
[data-direction="left"] { --action-angle: 270deg; }
[data-direction="up-left"] { --action-angle: 315deg; }
/* Keep the passed pawn on the side of the diagonal where the capture occurs. */
[data-action-kind="en-passant"]:is([data-direction="up-left"], [data-direction="down-right"])::after { scale: -1 1; }
@media (forced-colors: active) {
  .round-turns { color: CanvasText; background: Canvas; border-color: CanvasText; }
  .action-glyph, .action-choice, .action-symbol {
    --action-band: ButtonFace;
    --action-ink: ButtonText;
    forced-color-adjust: none;
  }
  .square.action-choices { --choice-divider: ButtonText; }
  .action-choice:focus-visible { outline-color: Highlight; }
}

.legend, .legend span { display: flex; align-items: center; }
.legend {
  justify-content: center;
  flex-wrap: wrap;
  gap: 4px 16px;
  min-height: 18px;
  margin-top: 9px;
  font-size: .76rem;
  color: var(--muted);
}
.legend span { gap: 6px; }
.legend span[hidden] { display: none; }
.legend i {
  display: inline-grid;
  place-items: center;
  width: 14px;
  height: 14px;
  border-radius: 2px;
  background: var(--move-fill);
}
.legend .ammo-key i { width: 17px; height: 17px; box-shadow: none; background: none; }
.legend .ammo-key i::before { width: 100%; }
.capture-mark::after { content: ""; width: 9px; height: 9px; border: 2px solid var(--capture-ring); border-radius: 50%; }

.tools {
  grid-area: tools;
  min-width: 0;
  align-self: start;
  padding: 18px clamp(12px, 1.1vw, 18px);
  border: 1px solid #ffffffd9;
  border-radius: 8px;
  background: #f4f4f8;
  box-shadow: 0 0 0 1px #697d981e, 0 5px 21px #50637910;
}
.tools-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 15px;
}
.panel-disclosure {
  margin-top: .5rem;
  padding-inline: .65rem;
  border: 1px solid var(--rule);
  border-radius: 6px;
}
.panel-disclosure[open] { padding-bottom: .65rem; }
.panel-disclosure > summary {
  padding: .55rem .65rem;
  margin-inline: -.65rem;
  border-radius: 5px;
  color: var(--ink);
  font-size: .9rem;
  font-weight: 600;
}
.panel-disclosure[open] > summary {
  border-radius: 5px 5px 0 0;
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}
.panel-disclosure summary { cursor: pointer; }
.panel-disclosure summary::marker { color: var(--accent); font-size: .78em; }
.panel-disclosure summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.panel-disclosure summary:hover { color: var(--accent); }
.settings-content {
  margin: .65rem 0 0 .4rem;
  padding-inline-start: .65rem;
  border-inline-start: 2px solid var(--rule);
}
.settings-group {
  padding: 0 0 0 .75rem;
  border-top: 1px solid var(--rule);
  font-size: .81rem;
}
.settings-group[open] { padding-bottom: .6rem; }
.settings-group > summary {
  margin-inline-start: -.75rem;
  padding-block: .48rem;
  color: var(--ink);
  font-size: .85rem;
  font-weight: 600;
}
#position-editor .turn-order-setting { font-size: .85rem; margin-bottom: .35rem; }
.match-help, #match-progress, #match-save-status, #match-history {
  color: var(--muted); font-size: .81rem; line-height: 1.45;
}
.match-help { margin: .55rem 0; }
.match-fields {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center;
  gap: .35rem .6rem; color: var(--muted); font-size: .81rem;
}
.match-fields select, .match-buttons button, .match-save {
  min-width: 0; min-height: 30px; padding: 3px 7px; border: 1px solid var(--control-edge, #8290a5);
  border-radius: 5px; background: var(--control-surface, #f6f5fa); color: var(--ink); font-size: .8rem;
}
.match-buttons { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) minmax(0, 1.2fr); gap: 5px; margin: .65rem 0 .3rem; }
.match-buttons button { padding-inline: 3px; }
#match-play { grid-column: span 2; color: var(--control-surface, #fafaff); background: var(--accent); }
#spectator button:disabled { opacity: .5; cursor: default; }
#match-progress:not([hidden]), #match-save-status:not([hidden]) { display: block; margin-top: .4rem; }
#match-save-status { overflow-wrap: anywhere; }
#match-history:not([hidden]) { margin: .35rem 0; padding-left: 2.1rem; max-height: 6.3rem; overflow-y: auto; }
.match-save { margin-top: .25rem; }
:where(.board-grid:not(.hide-preview-changes)) .square.engine-last:where(:not(:focus-visible)) { outline: 2px solid var(--accent); outline-offset: -2px; }
@media (pointer: coarse) {
  .match-fields select, .match-buttons button, .match-save { min-height: 44px; }
}
#position-editor #setup-side { margin-bottom: .5rem; }
h2 { font-size: clamp(1.25rem, 1.7vw, 1.55rem); line-height: 1.1; letter-spacing: -.035em; }
.turn-indicator, #setup-side {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 3px 8px;
  border: 1px solid #a8b3c0;
  border-radius: 14px;
  background: #ffffffe0;
  color: #374660;
  font: 600 .74rem/1.2 "Segoe UI", Arial, sans-serif;
}
.turn-indicator::before {
  content: "";
  width: 9px;
  height: 9px;
  border: 1px solid #61718a;
  border-radius: 50%;
  background: var(--white-turn-dot);
}
.turn-indicator[data-side="black"]::before { background: #263348; }
.turn-indicator[data-side="either"]::before { background: linear-gradient(90deg, var(--white-turn-dot) 50%, #263348 50%); }
.turn-indicator.in-check { background: #f8e4e7; color: #81334d; border-color: #bc8899; }
.tool-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.piece-picker {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: .85rem;
  color: var(--muted);
}
.piece-picker select {
  flex: 1;
  min-width: 0;
  min-height: 33px;
  padding: 4px 6px;
  border: 1px solid #b6bfcd;
  border-radius: 5px;
  font: inherit;
  color: var(--ink);
  background-color: #fbfbffcc;
}
.tool-grid button {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 9px;
  min-height: 41px;
  border: 1px solid #b6bfcd;
  border-radius: 5px;
  font-size: .85rem;
  line-height: 1.15;
  text-align: left;
  background: #fbfbff9e;
}
.tool-icon { font: 1.12rem/1 "Segoe UI Symbol", sans-serif; text-align: center; }
.tool-icon { width: 21px; flex: 0 0 21px; }
.placement-swatch {
  width: 23px;
  height: 23px;
  flex: 0 0 23px;
  padding: 2px;
  border-radius: 3px;
  background: #859ab3;
}
.placement-swatch img { display: block; width: 100%; height: 100%; object-fit: contain; }
.tool-grid button:hover, .zoom-controls button:hover:enabled { background: #fff; border-color: #838fa5; }
.tool-grid button[aria-pressed="true"] {
  border-color: #687c99;
  background: #dce2eb;
  box-shadow: inset 0 1px #fff9;
}
.actions { display: flex; align-items: center; flex-wrap: wrap; gap: 3px 5px; margin-top: 12px; }
.actions button, #home-view {
  color: var(--ink);
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 6px;
  font-size: .78rem;
  line-height: 1.2;
  background: transparent;
}
.actions button:hover, #home-view:hover { background: var(--control-hover, #fff9); }
.actions #reset { color: #fafaff; background: #465670; border-color: #404e65; }
.actions #reset:hover { background: #35455e; }
.view-controls, .zoom-controls, .label-toggle { display: flex; align-items: center; }
.view-controls { flex-wrap: wrap; gap: 2px 8px; padding-bottom: .45rem; color: var(--muted); font-size: .81rem; }
#home-view { color: var(--accent); text-decoration: underline; text-underline-offset: .18em; }
.zoom-controls { gap: 4px; }
.zoom-controls span { margin-right: 3px; }
.zoom-controls output { min-width: calc(4ch + 1em); color: var(--ink); font-variant-numeric: tabular-nums; text-align: center; }
.zoom-controls button {
  min-width: 28px;
  min-height: 28px;
  border: 1px solid #a7b4c2;
  border-radius: 4px;
  font: 1rem/1 "Segoe UI", sans-serif;
  background: #ffffff85;
}
.zoom-controls button:disabled { opacity: .45; cursor: default; }
.label-toggle { gap: 5px; min-height: 32px; cursor: pointer; }
.label-toggle input { margin: 0; accent-color: #56668b; }
.wave-finish { display: none; }
:root[data-theme="dark"] .wave-finish {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .7rem;
  margin-top: .65rem;
  font-weight: 600;
}
.wave-finish select {
  flex: 1;
  min-width: 0;
  min-height: 32px;
  padding: .2rem .4rem;
  border: 1px solid var(--control-edge);
  border-radius: 5px;
  color: var(--ink);
  background: var(--control-surface);
  font: inherit;
  font-weight: 400;
}
.wave-sliders, .sound-volume {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  margin: .75rem 0 .45rem;
}
.wave-sliders label, .sound-volume label { font-weight: 600; }
.wave-sliders output, .sound-volume output { color: var(--muted); font-variant-numeric: tabular-nums; }
.wave-sliders input, .sound-volume input {
  grid-column: 1 / -1;
  width: 100%;
  min-width: 0;
  height: 22px;
  margin: 0 0 .35rem;
  accent-color: var(--accent);
  cursor: pointer;
}
.sound-volume { margin: .25rem 0 0; }
.sound-volume input { margin-bottom: 0; }
.wave-actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.wave-actions button {
  min-height: 30px;
  padding: .25rem .55rem;
  background: var(--control-surface, #f6f5fa);
  color: var(--ink);
  border: 1px solid var(--control-edge, #8290a5);
  border-radius: 5px;
  font-size: inherit;
}
.wave-actions button:enabled:hover { background: var(--control-hover, #fff); border-color: var(--accent); }
.wave-actions button:disabled { opacity: .6; cursor: default; }
#wave-preview-reason, #wave-save-status { margin-top: .45rem; color: var(--muted); line-height: 1.4; }
#status {
  border-top: 1px solid var(--rule);
  margin-top: 15px;
  padding-top: 13px;
  line-height: 1.45;
  font-size: .92rem;
}
#due-shot, #enfilade-check { margin-top: .8rem; padding-left: .6rem; border-left: 3px solid var(--check-line, #8d4867); font-size: .85rem; }
#move-preview { display: flex; align-items: center; flex-wrap: wrap; gap: .4rem; margin-top: .85rem; }
#preview-label { flex-basis: 100%; color: var(--accent); font-size: .85rem; font-weight: 600; }
#move-preview .label-toggle { flex-basis: 100%; color: var(--muted); font-size: .82rem; }
#move-preview button {
  padding: .35rem .65rem; border: 1px solid var(--control-edge, #8290a5); border-radius: 5px;
  color: var(--ink); background: var(--control-surface, #f6f5fa); font-size: .82rem;
}
#move-preview #preview-accept { color: var(--control-surface, #fafaff); background: var(--accent); }
@media (prefers-reduced-motion: no-preference) and (forced-colors: none) {
  #preview-accept:enabled:not([hidden]) { animation: accept-reminder 8s ease-in-out infinite; }
  @keyframes accept-reminder {
    0%, 75%, 100% { background-color: var(--accent); box-shadow: 0 0 0 0 transparent; }
    87.5% {
      background-color: color-mix(in srgb, var(--accent) 86%, var(--control-surface, #fafaff));
      box-shadow: 0 0 3px 2px color-mix(in srgb, var(--accent) 25%, transparent);
    }
  }
}
#move-preview button:enabled:hover { border-color: var(--accent); }
#move-preview button:disabled { opacity: .55; }
#mapping-note { margin-top: 7px; color: var(--muted); font-size: .81rem; line-height: 1.5; }
#special-actions { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
#special-actions button { display: inline-flex; align-items: center; gap: .4em; padding: 6px 8px; border: 1px solid #a8afc3; border-radius: 4px; background: #f8f6fe; font-size: .81rem; }
#special-actions button:hover, #special-actions button:focus-visible { background: #dbece9; }
#piece-abilities {
  margin: 14px 0;
  padding: 12px 0 13px;
  border-block: 1px solid var(--rule);
}
#piece-abilities h3 {
  margin: 0 0 .55rem;
  color: var(--ink);
  font: 400 1.15rem/1.2 Baskerville, Georgia, serif;
}
#piece-abilities dl { margin: 0; }
#piece-abilities dt { color: var(--accent); font-size: .85rem; font-weight: 650; }
#piece-abilities dt:not(:first-child) { margin-top: .6rem; }
#piece-abilities dd { margin: .14rem 0 0; color: var(--ink); font-size: .85rem; line-height: 1.45; }
#cannon-ammunition h4 {
  margin: .85rem 0 .45rem;
  color: var(--ink);
  font: 600 1rem/1.2 Baskerville, Georgia, serif;
}
#cannon-ammunition dl {
  padding-inline-start: .8rem;
  border-inline-start: 2px solid var(--rule);
}
.diagrams { display: flex; flex-wrap: wrap; gap: 5px 14px; margin-top: 11px; }
.diagrams a { color: #46567b; font-size: .80rem; text-underline-offset: 3px; }
.rule-status { margin-top: 10px; color: var(--muted); font-size: .8rem; line-height: 1.45; }
.rule-status summary { cursor: pointer; }
.rule-status p { margin-top: 5px; }
.rule-status a { color: var(--accent); }

@media (min-width: 701px) {
  .tools { max-height: 100%; overflow-y: auto; scrollbar-width: thin; }
}
@media (pointer: coarse) {
  .appearance-control select { min-height: 44px; }
  .wave-sliders input, .sound-volume input, .wave-actions button, .wave-finish select,
  .panel-disclosure summary, #home-view { min-height: 44px; }
  .panel-disclosure summary { align-content: center; }
  .zoom-controls button { min-width: 40px; min-height: 40px; }
  .tool-grid button, .label-toggle, #move-preview button { min-height: 44px; }
}
@media (max-width: 700px) {
  main { min-height: 100dvh; height: auto; padding: 0 12px 13px; }
  .masthead {
    display: grid;
    grid-template: "title appearance" auto "dimensions appearance" auto / minmax(0, 1fr) auto;
    gap: 5px 12px;
    padding: 13px 0 10px;
  }
  .masthead-controls { display: contents; }
  .masthead .dimensions { grid-area: dimensions; align-self: start; font-size: .66rem; }
  .appearance-control { grid-area: appearance; flex-direction: column; align-items: flex-start; gap: 3px; font-size: .76rem; }
  h1 { grid-area: title; font-size: clamp(1.55rem, 7.7vw, 2rem); }
  /* Keep the board under the player's finger as the selected abilities change height. */
  .workspace { grid-template: "board" clamp(16rem, 57dvh, 36rem) "tools" auto / minmax(0, 1fr); gap: 12px; padding-top: 11px; }
  .tools { padding: 9px 11px; }
  .tools-heading { margin-bottom: 7px; }
  h2 { font-size: 1.28rem; }
  .tool-grid { gap: 4px; }
  .tool-grid button { justify-content: center; gap: 3px; padding: 5px 2px; font-size: .75rem; text-align: center; }
  .tool-icon { width: 14px; flex-basis: 14px; }
  .actions { margin-top: 5px; }
  #status { margin-top: 5px; padding-top: 6px; font-size: .86rem; }
  #mapping-note { margin-top: 4px; font-size: .77rem; }
  .diagrams { margin-top: 5px; }
}
