@font-face {
  font-family: "xkcd Script";
  src: url("./assets/xkcd-script.ttf") format("truetype");
  font-display: swap;
}

:root {
  color-scheme: light;
  --ink: #2d2d2d;
  --muted: #6f6f6f;
  --line: #c9c9c9;
  --soft: #f1f1f1;
  --tile: #dcdcdc;
  --orange: #e46f2c;
  --red: #d52828;
  --yellow: #fff982;
  font-family: Arial, Helvetica, sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  overflow-x: hidden;
  background: #fff;
  color: var(--ink);
}

button, input { font: inherit; }

button, a { color: inherit; }

.app-shell {
  --viewport-padding: 28px;
  --vertical-reserve: 300px;
  width: min(
    760px,
    calc(100vw - var(--viewport-padding)),
    max(220px, calc(100dvh - var(--vertical-reserve)))
  );
  margin: 0 auto;
  padding: 24px 0 18px;
}

.page-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin: 0 0 18px;
}

.page-header h1 {
  flex: 1;
  margin: 0;
  font-size: clamp(1.15rem, 3vw, 1.55rem);
  font-weight: 600;
  letter-spacing: -.015em;
}

.page-header h1 span { font-weight: 400; }

.page-header a {
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.comic-shell { width: 100%; }

.board-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border: 3px solid #353535;
  background: #fff;
}

.cg-wrap {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

cg-board {
  border-radius: 0 !important;
  background-color: #fff !important;
  background-image: conic-gradient(var(--tile) 25%, #fff 0 50%, var(--tile) 0 75%, #fff 0) !important;
  background-size: 25% 25% !important;
}

.xkcd-board piece.king.white { background-image: url('./assets/wK.svg'); }
.xkcd-board piece.queen.white { background-image: url('./assets/wQ.svg'); }
.xkcd-board piece.rook.white { background-image: url('./assets/wR.svg'); }
.xkcd-board piece.bishop.white { background-image: url('./assets/wB.svg'); }
.xkcd-board piece.knight.white { background-image: url('./assets/wN.svg'); }
.xkcd-board piece.pawn.white { background-image: url('./assets/wP.svg'); }
.xkcd-board piece.king.black { background-image: url('./assets/bK.svg'); }
.xkcd-board piece.queen.black { background-image: url('./assets/bQ.svg'); }
.xkcd-board piece.rook.black { background-image: url('./assets/bR.svg'); }
.xkcd-board piece.bishop.black { background-image: url('./assets/bB.svg'); }
.xkcd-board piece.knight.black { background-image: url('./assets/bN.svg'); }
.xkcd-board piece.pawn.black { background-image: url('./assets/bP.svg'); }

cg-board square.last-move { background-color: rgba(255, 246, 91, .63); }
cg-board square.selected { background-color: rgba(255, 236, 64, .55); }
cg-board square.move-dest { background: radial-gradient(rgba(42, 42, 42, .45) 18%, transparent 20%); }
cg-board square.move-dest:hover { background: rgba(255, 236, 64, .35); }
cg-board square.check { background: radial-gradient(circle, rgba(228, 111, 44, .75), transparent 70%); }
cg-board square.offside-rejected-square {
  background: radial-gradient(circle, rgba(213, 40, 40, .55), rgba(213, 40, 40, .14) 55%, transparent 74%);
}

.xkcd-board.white-in-check piece.king.white,
.xkcd-board.black-in-check piece.king.black {
  z-index: 14;
  filter:
    drop-shadow(0 0 4px rgba(228, 111, 44, .95))
    drop-shadow(0 0 11px rgba(228, 111, 44, .9));
  animation: check-glow .8s ease-in-out infinite alternate;
}

.xkcd-board piece.offside-rejected {
  z-index: 18;
  filter:
    drop-shadow(0 0 4px rgba(213, 40, 40, 1))
    drop-shadow(0 0 13px rgba(213, 40, 40, .95));
  animation: offside-piece-glow .9s ease-out both;
}

@keyframes check-glow {
  from {
    filter:
      drop-shadow(0 0 3px rgba(228, 111, 44, .8))
      drop-shadow(0 0 8px rgba(228, 111, 44, .7));
  }
  to {
    filter:
      drop-shadow(0 0 6px rgba(228, 111, 44, 1))
      drop-shadow(0 0 15px rgba(228, 111, 44, .95));
  }
}

@keyframes offside-piece-glow {
  0%, 62% {
    filter:
      drop-shadow(0 0 5px rgba(213, 40, 40, 1))
      drop-shadow(0 0 15px rgba(213, 40, 40, 1));
  }
  100% {
    filter:
      drop-shadow(0 0 2px rgba(213, 40, 40, .45))
      drop-shadow(0 0 7px rgba(213, 40, 40, .35));
  }
}

.offside-line {
  --line-top: 25%;
  position: absolute;
  z-index: 12;
  top: calc(var(--line-top) - 4px);
  left: -38px;
  right: -52px;
  height: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s ease;
}

.offside-line.show { opacity: 1; }

.offside-dashes {
  position: absolute;
  inset: 0;
  overflow: visible;
}

.offside-dashes line {
  fill: none;
  stroke: var(--red);
  stroke-dasharray: 30 18;
  stroke-linecap: round;
  stroke-width: 4;
}

.offside-flag {
  position: absolute;
  top: -47px;
  right: -16px;
  width: 48px;
  height: 48px;
  fill: none;
  stroke: var(--red);
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 3.5;
  transform: rotate(7deg);
  transform-origin: 10px 43px;
}

.offside-flag .flag-checks {
  fill: var(--red);
  stroke: none;
}

.offside-flag clipPath path {
  fill: #000;
  stroke: none;
}

.offside-message {
  margin: 15px 2px 8px;
  color: #171717;
  font-family: "xkcd Script", "Comic Sans MS", cursive;
  font-size: clamp(1.05rem, min(5.2vw, 4.5dvh), 2.25rem);
  line-height: 1.2;
  text-align: center;
  text-transform: uppercase;
}

.offside-message:empty { display: none; }

.board-status {
  margin: 12px 4px 0;
  font-family: "xkcd Script", "Comic Sans MS", cursive;
  font-size: clamp(1.2rem, 3.5vw, 1.8rem);
  line-height: 1.15;
  text-align: center;
  text-transform: uppercase;
}

.game-bar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 8px 16px;
  margin-top: 14px;
  padding: 11px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.controls {
  grid-column: 2;
  display: flex;
  flex: 0 1 auto;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
}

.controls button,
.primary-button,
.secondary-button {
  border: 1px solid #9c9c9c;
  border-radius: 4px;
  padding: 8px 11px;
  background: #fff;
  color: #333;
  cursor: pointer;
  font-weight: 600;
}

.controls button:hover:not(:disabled),
.secondary-button:hover { background: var(--soft); }

.controls button:disabled { opacity: .4; cursor: default; }

.controls .primary-control,
.primary-button {
  border-color: #333;
  background: #333;
  color: #fff;
}

.controls .primary-control:hover,
.primary-button:hover { background: #111; }

.switch-control {
  display: flex;
  align-items: center;
  gap: 7px;
  color: #4d4d4d;
  font-size: .78rem;
}

.switch-control {
  grid-column: 1;
  cursor: pointer;
  font-weight: 700;
}

.switch-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-track {
  position: relative;
  width: 34px;
  height: 20px;
  border: 1px solid #777;
  border-radius: 999px;
  background: #ddd;
  transition: background .12s ease;
}

.switch-track span {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .35);
  transition: transform .12s ease;
}

.switch-control input:checked + .switch-track { background: #333; }
.switch-control input:checked + .switch-track span { transform: translateX(14px); }
.switch-control input:focus-visible + .switch-track { outline: 3px solid #77a7e8; outline-offset: 2px; }

.history-strip {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-height: 38px;
  padding: 10px 0 0;
  overflow-x: auto;
  color: var(--muted);
  font-size: .78rem;
}

.history-label { flex: 0 0 auto; color: var(--ink); font-weight: 700; }
.empty-history { flex: 0 0 auto; }
.empty-history.hidden { display: none; }

.move-list {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 0;
  padding: 0 0 4px;
  list-style: none;
}

.move-row { display: flex; gap: 5px; white-space: nowrap; }
.move-number { color: #999; }
.move-san { color: #444; }
.move-san.latest { font-weight: 700; }

footer {
  margin-top: 15px;
  color: #888;
  text-align: center;
  font-size: .68rem;
}

footer a { text-underline-offset: 2px; }

.modal {
  width: min(460px, calc(100% - 28px));
  max-height: calc(100vh - 28px);
  overflow: auto;
  border: 2px solid #333;
  border-radius: 4px;
  padding: 0;
  background: #fff;
  color: var(--ink);
  box-shadow: 8px 8px 0 rgba(0, 0, 0, .18);
}

.modal::backdrop { background: rgba(255, 255, 255, .78); }
.modal form { position: relative; padding: 25px; }
.modal h2 { margin: 0 34px 22px 0; font-size: 1.45rem; }

.modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  border: 0;
  padding: 3px 7px;
  background: transparent;
  color: #555;
  cursor: pointer;
  font-size: 1.8rem;
  line-height: 1;
}

.modal fieldset {
  margin: 0 0 18px;
  border: 0;
  padding: 0;
}

.modal legend,
.number-field > span {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-size: .77rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  overflow: hidden;
  border: 1px solid #999;
  border-radius: 4px;
}

.segmented button,
.segmented label span {
  display: block;
  min-width: 0;
  border: 0;
  border-right: 1px solid #999;
  padding: 9px 8px;
  background: #fff;
  color: #555;
  cursor: pointer;
  text-align: center;
  font-weight: 700;
}

.segmented > :last-child,
.segmented > :last-child span { border-right: 0; }
.segmented button[aria-pressed="true"] { background: #333; color: #fff; }
.segmented label { position: relative; }
.segmented label input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label input:checked + span { background: #333; color: #fff; }
.segmented label input:focus-visible + span { outline: 3px solid #77a7e8; outline-offset: -3px; }

.rule-note {
  margin: 0 0 20px;
  padding: 12px;
  background: var(--soft);
  font-size: .85rem;
  line-height: 1.45;
}

.radio-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 5px;
  border-bottom: 1px solid #e1e1e1;
  cursor: pointer;
}

.radio-row:last-child { border-bottom: 0; }
.radio-row input { accent-color: #333; }
.radio-row span { display: grid; gap: 2px; }
.radio-row strong { font-size: .92rem; }
.radio-row small { color: var(--muted); }

.number-field { display: block; margin: 0 0 20px; }
.number-field input {
  width: 100%;
  border: 1px solid #999;
  border-radius: 4px;
  padding: 9px 10px;
  color: #222;
  background: #fff;
}

.number-field[hidden],
.modal fieldset[hidden] { display: none; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.promotion-modal { width: min(390px, calc(100% - 28px)); }
.promotion-modal form { text-align: center; }
.modal-heading { margin-bottom: 17px; font-size: 1.15rem; font-weight: 700; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 16px; }
.promotion-option {
  aspect-ratio: 1;
  border: 1px solid #777;
  border-radius: 3px;
  background: #eee;
  background-position: center;
  background-size: 82%;
  background-repeat: no-repeat;
  cursor: pointer;
}
.promotion-option:hover { background-color: var(--yellow); }

@media (max-width: 700px) {
  .app-shell {
    --viewport-padding: 4px;
    --vertical-reserve: 260px;
    padding-top: 14px;
  }
  .page-header { padding: 0 10px; margin-bottom: 12px; }
  .board-stage { border-width: 2px; }
  .offside-line {
    left: -24px;
    right: -38px;
  }
  .offside-dashes line { stroke-dasharray: 26 16; stroke-width: 3; }
  .offside-flag { right: -2px; top: -44px; width: 43px; height: 43px; }
  .offside-message { margin-inline: 10px; }
  .game-bar { align-items: flex-start; margin-inline: 10px; }
  .controls button { padding: 7px 8px; font-size: .75rem; }
  .history-strip { margin-inline: 10px; }
  footer { padding-inline: 10px; }
}

@media (max-width: 460px) {
  .page-header h1 span { display: block; margin-top: 2px; font-size: .82em; }
  .game-bar { grid-template-columns: auto minmax(0, 1fr); }
  .controls { grid-column: 2; min-width: 0; max-width: none; justify-content: flex-end; }
  .modal form { padding: 21px; }
}

@media (prefers-reduced-motion: reduce) {
  .xkcd-board.white-in-check piece.king.white,
  .xkcd-board.black-in-check piece.king.black,
  .xkcd-board piece.offside-rejected { animation: none; }
  .offside-line { transition: none; }
}
