:root {
  --bg: #faf6ef;
  --surface: #ffffff;
  --ink: #1f2430;
  --muted: #646a78;
  --border: #e8e1d5;
  --track: #e6dfd2;
  --seg-track: #f1ebe0;
  --seg-on: #ffffff;
  --dot: #1f2430;
  --p0: #e8553f;
  --p0-soft: rgba(232, 85, 63, 0.16);
  --p1: #17998a;
  --p1-soft: rgba(23, 153, 138, 0.16);
  --focus: #3b6fe0;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(31, 36, 48, 0.05), 0 10px 30px rgba(31, 36, 48, 0.07);
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161b;
    --surface: #1d2027;
    --ink: #f1efe9;
    --muted: #a0a6b3;
    --border: #2c3039;
    --track: #31353f;
  --seg-track: #14161b;
  --seg-on: #343843;
    --dot: #f1efe9;
    --p0: #ff7a66;
    --p0-soft: rgba(255, 122, 102, 0.2);
    --p1: #35c2b1;
    --p1-soft: rgba(53, 194, 177, 0.2);
    --focus: #7da2ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14161b;
  --surface: #1d2027;
  --ink: #f1efe9;
  --muted: #a0a6b3;
  --border: #2c3039;
  --track: #31353f;
  --seg-track: #14161b;
  --seg-on: #343843;
  --dot: #f1efe9;
  --p0: #ff7a66;
  --p0-soft: rgba(255, 122, 102, 0.2);
  --p1: #35c2b1;
  --p1-soft: rgba(53, 194, 177, 0.2);
  --focus: #7da2ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

/* Header */

.brand h1 {
  margin: 0 0 20px;
  font-size: clamp(30px, 8vw, 40px);
  font-weight: 800;
  letter-spacing: -0.02em;
  text-align: center;
}

.brand h1 span[aria-hidden] {
  color: var(--p0);
}

/* Cards and buttons */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 650;
  padding: 12px 20px;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.15s ease;
}

.btn:hover {
  background: color-mix(in srgb, var(--surface) 88%, var(--ink));
}

.btn:active {
  transform: scale(0.97);
}

.btn.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}

.btn.primary:hover {
  background: color-mix(in srgb, var(--ink) 85%, var(--bg));
}

.btn:focus-visible,
.segmented input:focus-visible + span {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* Setup */

.setup {
  display: grid;
  gap: 18px;
  padding: 22px;
}

.field {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

.field legend {
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 650;
  color: var(--muted);
}

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: var(--seg-track);
}

.segmented label {
  flex: 1;
  position: relative;
}

.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
}

.segmented span {
  display: block;
  padding: 10px 8px;
  border-radius: 999px;
  text-align: center;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.segmented input:checked + span {
  background: var(--seg-on);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(31, 36, 48, 0.12);
}

.setup .btn.primary {
  padding: 15px 20px;
  font-size: 17px;
}

.howto {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

/* Scoreboard */

.scoreboard {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.player {
  --colour: var(--p0);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.2s ease, transform 0.2s ease;
}

.player.p1 {
  --colour: var(--p1);
}

.player.active {
  border-color: var(--colour);
  transform: translateY(-2px);
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 5px;
  background: var(--colour);
  flex: none;
}

.player .name {
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player .score {
  margin-left: auto;
  font-size: 26px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--colour);
}

.status {
  min-height: 1.5em;
  margin: 14px 0;
  text-align: center;
  font-weight: 600;
  color: var(--muted);
}

/* Board */

.board-wrap {
  padding: 8px;
}

.board {
  --turn: var(--p0);
  display: block;
  width: 100%;
  height: auto;
  max-height: 72vh;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.board.turn-p1 {
  --turn: var(--p1);
}

.dots circle {
  fill: var(--dot);
  pointer-events: none;
}

.line {
  outline: none;
  cursor: pointer;
}

.line .track {
  stroke: var(--track);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 2 10;
  pointer-events: none;
}

.line .stroke {
  stroke: transparent;
  stroke-width: 10;
  stroke-linecap: round;
  pointer-events: none;
}

.line .hit {
  stroke: transparent;
  stroke-width: 44;
}

.line.drawn {
  cursor: default;
}

.line.drawn .track {
  display: none;
}

.line.drawn .hit {
  pointer-events: none;
}

.line.drawn.p0 .stroke {
  stroke: var(--p0);
}

.line.drawn.p1 .stroke {
  stroke: var(--p1);
}

.line.drawn .stroke {
  stroke-dasharray: 1;
  animation: draw 0.28s ease-out both;
}

.line.last .stroke {
  stroke-width: 12;
}

@media (hover: hover) {
  .board:not(.locked) .line:not(.drawn):hover .stroke {
    stroke: var(--turn);
    opacity: 0.4;
  }
}

.board:not(.locked) .line:not(.drawn):focus-visible .stroke {
  stroke: var(--turn);
  opacity: 0.55;
}

.board:not(.locked) .line:not(.drawn):focus-visible .track {
  stroke: var(--focus);
  stroke-dasharray: none;
}

.board.locked .line {
  cursor: default;
}

.board.locked .line .hit {
  pointer-events: none;
}

.box rect {
  fill: transparent;
  transition: fill 0.2s ease;
}

.box text {
  font-family: var(--font);
  font-size: 22px;
  font-weight: 800;
  fill: transparent;
}

.box.claimed rect {
  animation: pop 0.32s cubic-bezier(0.2, 0.9, 0.3, 1.4) both;
  transform-box: fill-box;
  transform-origin: center;
}

.box.claimed.p0 rect {
  fill: var(--p0-soft);
}

.box.claimed.p1 rect {
  fill: var(--p1-soft);
}

.box.claimed.p0 text {
  fill: var(--p0);
}

.box.claimed.p1 text {
  fill: var(--p1);
}

@keyframes draw {
  from {
    stroke-dashoffset: 1;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes pop {
  from {
    transform: scale(0.4);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}

/* End of game */

.result {
  width: min(360px, calc(100vw - 32px));
  padding: 28px 22px 22px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  text-align: center;
}

.result::backdrop {
  background: rgba(20, 22, 27, 0.45);
  backdrop-filter: blur(2px);
}

.result[open] {
  animation: pop 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.3) both;
}

.result h2 {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 800;
}

.result-score {
  margin: 0;
  color: var(--muted);
  font-weight: 600;
}

.result .actions {
  margin-top: 22px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
