/* Dots and Boxes, pencil on a notebook page: ruled paper, graphite dots, blue and red pencil. Fixed palette. */
:root {
  color-scheme: light;
  --paper: #fbf8f1;
  --rule: rgba(90, 135, 200, .16);
  --margin: rgba(214, 96, 90, .28);
  --card: #fffdf9;
  --ink: #2d2f36;
  --muted: #7b7d86;
  --line: #e6e1d6;
  --graphite: #3a3c43;
  --p0: #2e62c9;        /* Blue pencil */
  --p0-wash: rgba(46, 98, 201, .12);
  --p1: #d4483b;        /* Red pencil */
  --p1-wash: rgba(212, 72, 59, .12);
  /* tutorial kit (tutorial.css) */
  --tut-accent: #f2b84b;
  --tut-accent-ink: #2d2f36;
  --tut-bubble: #fffdf9;
  --tut-ink: #2d2f36;
  --tut-muted: #7b7d86;
  --tut-dim: rgba(28, 30, 38, .55);
  --margin-x: calc(max(14px, 4vw) + 18px);   /* notebook margin line */
}
/* On phones the board spans the width, so the margin line moves into the side gutter, clear of the dots. */
@media (max-width: 640px) { :root { --margin-x: max(5px, 4vw - 9px); } }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  margin: 0;
  background-color: var(--paper);
  background-image:
    linear-gradient(90deg, transparent 0 var(--margin-x), var(--margin) var(--margin-x) calc(var(--margin-x) + 2px), transparent calc(var(--margin-x) + 2px)),
    repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- bar ---------- */
.app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  max-width: 820px; margin: 0 auto;
  padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(14px, env(safe-area-inset-left));
}
.bar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 60px; }
.back { color: var(--muted); text-decoration: none; font-size: 14px; white-space: nowrap; padding: 12px 2px; }
.back:hover, .back:focus-visible { color: var(--ink); }
.bar-actions { display: flex; gap: 8px; }

.btn {
  min-height: 44px; min-width: 44px; padding: 0 18px; border-radius: 22px;
  border: 0; background: var(--card); color: var(--ink);
  font-weight: 650; cursor: pointer; box-shadow: 0 1px 2px rgba(45, 47, 54, .1), 0 0 0 1px var(--line);
  transition: transform .12s, background .15s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn.icon { padding: 0; width: 44px; font-size: 22px; line-height: 1; letter-spacing: .04em; }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.primary { background: var(--ink); color: var(--card); box-shadow: none; }
.btn.wide { width: 100%; min-height: 50px; font-size: 17px; }
.btn:disabled, .row:disabled { opacity: .35; cursor: default; transform: none; }
.btn:focus-visible, .seg button:focus-visible, .row:focus-visible, .link:focus-visible {
  outline: 3px solid var(--p0); outline-offset: 2px;
}

/* ---------- game ---------- */
.game {
  flex: 1; min-height: 0;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px; padding-bottom: 14px;
}
.turn {
  justify-self: center; display: inline-flex; align-items: center; gap: 9px;
  padding: 8px 8px 8px 18px; border-radius: 999px; background: var(--card);
  box-shadow: 0 0 0 1px var(--line); font-weight: 650; font-size: 16px; min-height: 40px;
}
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; transition: background .2s; }
.dot.p0 { background: var(--p0); }
.dot.p1 { background: var(--p1); }
.turn.thinking #turn-text::after { content: ''; display: inline-block; width: 1.1em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
.score {
  display: inline-flex; align-items: center; gap: 5px; margin-left: 4px;
  padding: 2px 12px; border-radius: 999px; background: var(--paper); font-variant-numeric: tabular-nums;
}
.score b { font-size: 17px; }
.score .s0 { color: var(--p0); }
.score .s1 { color: var(--p1); }
.score i { font-style: normal; color: var(--muted); }
.toast {
  justify-self: center; min-height: 24px; font-size: 15px; color: var(--muted); text-align: center;
  opacity: 0; transform: translateY(4px); transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: none; }

/* ---------- board ---------- */
.board-wrap { min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; }
.board {
  display: block; width: min(100%, calc(100vh - 210px), 600px); height: auto; aspect-ratio: 1;
  overflow: hidden; user-select: none; -webkit-user-select: none;
}
@supports (container-type: size) {
  .board-wrap { container-type: size; }
  .board { width: min(100cqw, 100cqh, 600px); }
}
.board .hit { fill: transparent; cursor: default; }
.board .edges.live .hit { cursor: pointer; }
.board .ghost { fill: none; stroke-width: 7; stroke-linecap: round; opacity: 0; pointer-events: none; transition: opacity .12s; }
.board .ghost.p0 { stroke: var(--p0); }
.board .ghost.p1 { stroke: var(--p1); }
@media (hover: hover) { .board .edges.live .edge:hover .ghost { opacity: .28; } }

.board .line { fill: none; stroke-width: 7.5; stroke-linecap: round; pointer-events: none; opacity: .9; }
.board .line.p0 { stroke: var(--p0); }
.board .line.p1 { stroke: var(--p1); }
.board .line.last { stroke-width: 9.5; opacity: 1; }
.board .line.fresh { stroke-dasharray: 120; animation: pencil .28s ease-out both; }
@keyframes pencil { from { stroke-dashoffset: 120; } to { stroke-dashoffset: 0; } }
.board .line.pending { stroke-width: 8; stroke-dasharray: 10 12; opacity: .8; animation: pend 1s linear infinite; }
@keyframes pend { to { stroke-dashoffset: -44; } }

.board .box rect { stroke: none; }
.board .box.p0 rect { fill: var(--p0-wash); }
.board .box.p1 rect { fill: var(--p1-wash); }
.board .box .mark { fill: none; stroke-width: 6; stroke-linecap: round; stroke-linejoin: round; opacity: .85; }
.board .box.p0 .mark { stroke: var(--p0); }
.board .box.p1 .mark { stroke: var(--p1); }
.board .box.fresh { animation: boxin .35s ease-out both; transform-box: fill-box; transform-origin: center; }
@keyframes boxin { from { opacity: 0; transform: scale(.85); } to { opacity: 1; transform: none; } }
.board .dots circle { fill: var(--graphite); pointer-events: none; }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(45, 47, 54, .34);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px;
  animation: fade .2s ease-out;
}
.overlay.soft { background: rgba(251, 248, 241, .55); align-items: center; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet, .card {
  background: var(--card); color: var(--ink);
  width: 100%; max-width: 440px; max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  overflow: auto; overscroll-behavior: contain;
  border-radius: 24px; padding: 8px 20px 20px;
  box-shadow: 0 20px 50px rgba(45, 47, 54, .22);
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(16px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 600px) { .overlay { align-items: center; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; min-height: 52px; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.row {
  display: block; width: 100%; text-align: left; min-height: 50px; padding: 0 4px;
  border: 0; border-bottom: 1px solid var(--line); background: transparent; font-weight: 600; cursor: pointer;
}
.group { padding: 12px 0 4px; }
.label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: #f3efe6; }
.seg button {
  flex: 1 1 0; min-height: 44px; border: 0; border-radius: 12px; background: transparent; padding: 0 4px;
  font-weight: 600; cursor: pointer; color: var(--muted); transition: background .15s, color .15s;
}
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(45, 47, 54, .16); }
.note { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.menu-sheet .row:first-of-type { border-top: 1px solid var(--line); }
.menu-sheet #menu-help { margin-top: 10px; border-top: 1px solid var(--line); }
.menu-sheet .btn.wide { margin-top: 16px; }

.over-card { max-width: 340px; text-align: center; padding: 26px 22px 14px; }
.over-title { font-size: 28px; font-weight: 800; }
.over-score { display: flex; justify-content: center; gap: 18px; margin: 10px 0 20px; color: var(--muted); font-size: 15px; }
.over-score .side { display: inline-flex; align-items: center; gap: 6px; }
.over-score b { color: var(--ink); font-size: 20px; }
.over-score i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.over-score .p0 i { background: var(--p0); }
.over-score .p1 i { background: var(--p1); }
.link { display: block; margin: 8px auto 0; min-height: 44px; padding: 0 16px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }

.help-sheet { max-width: 560px; }
.help-body { padding-bottom: 6px; }
.help-body h3 { font-size: 16px; margin: 18px 0 4px; }
.help-body p { font-size: 15px; margin: 4px 0; }
.fine { font-size: 13px; color: var(--muted); margin-top: 14px; }
.t0 { color: var(--p0); }
.t1 { color: var(--p1); }

/* ---------- small and landscape screens ---------- */
@media (max-width: 380px) {
  .app { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .btn { padding: 0 14px; }
  .back { font-size: 13px; }
  .seg button { font-size: 14px; }
}
@media (min-aspect-ratio: 1/1) and (max-height: 480px) {
  .bar { min-height: 50px; }
  .game { gap: 4px; padding-bottom: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* Online play (online.js): no New game while a friend is playing; a presence line under the turn. */
body.is-online #btn-new { display: none; }
.turn-row { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.net { color: var(--muted); }
#over-leave { margin-top: 8px; }
/* The online rows now come first after the settings, so they take over How to play's gap and top line. */
.menu-sheet #menu-online, .menu-sheet #menu-leave { margin-top: 10px; border-top: 1px solid var(--line); }
.menu-sheet #menu-online ~ #menu-help { margin-top: 0; border-top: 0; }
