:root {
  --bg: #f6f3ee; --fg: #1b1f2a; --muted: #6b7280; --card: #ffffff; --line: #1b1f2a;
  --accent: #2563eb; --error: #dc2626; --ok: #16a34a; --cell: #ffffff; --cell-given: #ece9e2;
  --grid-line: rgba(100, 100, 110, 0.35);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161c; --fg: #eceef3; --muted: #9aa3b2; --card: #1e2129; --line: #eceef3;
    --accent: #60a5fa; --error: #f87171; --ok: #22c55e; --cell: #242833; --cell-given: #303542;
    --grid-line: rgba(180, 180, 190, 0.3);
  }
}
* { box-sizing: border-box; }
html, body {
  margin: 0; background: var(--bg); color: var(--fg);
  font-family: -apple-system, system-ui, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; overscroll-behavior: none;
}
html { height: 100%; overflow: hidden; touch-action: none; }
.card.wide, .table-wrap { touch-action: pan-y; } /* the results list is the one thing that scrolls */
body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: none;
}
main { flex: 1; min-height: 0; width: 100%; overflow: hidden; display: flex; flex-direction: column; }
/* Nothing scrolls: each view fills main and the board and tiles shrink to the space left. */
#home, #game { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#home[hidden], #game[hidden] { display: none; }
#home { container-type: size; }
.top { padding: 12px 16px 0; max-width: 520px; margin: 0 auto; }
.topbar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 40px; }
.title { margin: 0; font-size: 20px; font-weight: 800; text-align: center; grid-column: 2; }
.back { grid-column: 1; justify-self: start; background: none; border: 0; color: var(--accent); font: inherit; font-weight: 600; font-size: 17px; padding: 6px 0; cursor: pointer; }
.back[hidden] { display: none; }
#stats-home { grid-column: 3; justify-self: end; }
#stats-home[hidden] { display: none; }
#share-top { grid-column: 3; justify-self: end; }
#share-top[hidden] { display: none; }
.home-sub { color: var(--muted); text-align: center; margin: 14px 0 18px; font-size: 15px; }
.tiles-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.tile-game {
  background: var(--card); border: 0; border-radius: 20px; padding: 18px 14px 16px; text-align: left; color: var(--fg);
  font: inherit; cursor: pointer; display: flex; flex-direction: column; gap: 6px; box-shadow: 0 2px 10px rgba(0,0,0,.06);
  transition: transform .12s;
}
.tile-game:active { transform: scale(.97); }
.tile-game .glyph { width: 100%; height: max(36px, min((50cqw - 35px) / 1.6, 50cqh - 182px)); border-radius: 14px; display: flex; align-items: center; justify-content: center; margin-bottom: 6px; }
.tile-game .glyph svg { width: 56%; height: 70%; }
.tile-game .name { font-size: 20px; font-weight: 800; }
.tile-game .tag { font-size: 13px; color: var(--muted); line-height: 1.3; }
.tile-game .meta { font-size: 12px; color: var(--muted); margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile-game .meta b { color: var(--fg); }
main { padding: 12px 16px calc(24px + env(safe-area-inset-bottom)); max-width: 520px; margin: 0 auto; }
.top { flex: none; width: 100%; }
.bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.timer { font-variant-numeric: tabular-nums; font-size: 22px; font-weight: 700; }
.bar-right { display: flex; gap: 8px; align-items: center; }
.btn { font: inherit; font-weight: 600; border: 0; border-radius: 10px; padding: 9px 16px; background: var(--accent); color: #fff; cursor: pointer; }
.btn.ghost { background: var(--card); color: var(--fg); }
.btn.small { padding: 7px 12px; font-size: 14px; }
.btn:disabled { opacity: .4; cursor: default; }
.link { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 600; padding: 6px; cursor: pointer; }
.link.danger { color: var(--muted); font-weight: 500; font-size: 13px; margin-top: 8px; }
.size {
  font: inherit; font-weight: 600; border-radius: 10px; padding: 8px 30px 8px 12px; color: var(--fg); border: 0; cursor: pointer;
  appearance: none; -webkit-appearance: none;
  background: var(--card) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 4.5l3.5 3.5 3.5-3.5' fill='none' stroke='%236b7280' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 11px center / 12px 12px;
}
.fit { flex: 1; min-height: 0; container-type: size; display: flex; align-items: center; justify-content: center; }
.stage { width: min(100cqw, 100cqh, 480px); position: relative; }
.board { width: 100%; position: relative; transition: filter .3s, opacity .3s; }
.board.covered { filter: blur(10px); opacity: 0; pointer-events: none; transition: none; } /* vanish at once, fade back in on start; a blur alone still shows the layout */
.cover { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 5; background: color-mix(in srgb, var(--bg) 55%, transparent); border-radius: 6px; animation: fade .25s ease-out; }
.cover[hidden] { display: none; }
.cover-card { background: var(--card); border-radius: 18px; padding: 22px 26px; text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,.25); min-width: 60%; }
.cover-card h2 { margin: 0 0 4px; font-size: 24px; }
.cover-card p { margin: 0 0 16px; color: var(--muted); font-size: 14px; }
.cover-card .btn { padding: 12px 28px; font-size: 16px; }
.wrap { position: relative; width: 100%; }
.grid {
  display: grid; grid-template-columns: repeat(var(--n), 1fr); width: 100%; aspect-ratio: 1;
  border: 2px solid var(--line); border-radius: 6px; background: var(--cell); touch-action: none;
}
.cell {
  position: relative; display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--grid-line); aspect-ratio: 1; cursor: pointer;
}
.cell svg { width: 62%; height: 62%; display: block; }
.cell.error { box-shadow: inset 0 0 0 3px var(--error); z-index: 1; }
.overlay { position: absolute; inset: 0; pointer-events: none; }
.actions { display: flex; gap: 6px; justify-content: center; margin: 14px 0 4px; min-height: 36px; flex-wrap: wrap; }
.actions .btn { padding: 9px 11px; font-size: 15px; }
.hintmsg { flex: none; min-height: 1.3em; text-align: center; color: var(--accent); font-weight: 600; font-size: 14px; margin: 10px 0 -6px; animation: fade .2s ease-out; }
.hintmsg[hidden] { display: block; visibility: hidden; } /* keep its line so the board does not jump */
@keyframes hintpulse { 0%, 100% { box-shadow: inset 0 0 0 0 var(--accent); } 50% { box-shadow: inset 0 0 0 4px var(--accent); } }
.cell.hint { animation: hintpulse .9s ease-in-out 3; z-index: 2; }
.patches .patch.hint { animation: hintpulse .9s ease-in-out 3; }
.rules { color: var(--muted); font-size: 14px; line-height: 1.45; text-align: center; margin: 8px 0; }
@media (max-height: 700px) { .rules { font-size: 12.5px; line-height: 1.35; margin: 6px 0; } .actions { margin-top: 10px; } }
.seed { color: var(--muted); font-size: 12px; text-align: center; margin: 0; }
/* Solved celebration */
:root { --gold: #f2b705; --gold-dark: #b8860b; }
.modal[hidden], .confetti[hidden] { display: none; }
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }
.modal {
  position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,.45); animation: fade .25s ease-out;
  padding: 0 16px calc(24px + env(safe-area-inset-bottom));
}
@media (min-width: 520px) { .modal { align-items: center; } }
.card {
  background: var(--card); color: var(--fg); border-radius: 22px; padding: 28px 24px 20px; width: 100%; max-width: 420px;
  text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.35); animation: rise .45s cubic-bezier(.2,.9,.3,1.2);
}
.card h2 { margin: 10px 0 4px; font-size: 26px; }
.card p { margin: 0 0 20px; color: var(--muted); font-size: 17px; font-variant-numeric: tabular-nums; }
.card .row { display: flex; gap: 10px; }
.card .btn.ghost { background: var(--bg); }
.card .btn.full { display: block; width: 100%; padding: 13px 0; font-size: 16px; margin: 0 0 10px; }
.card.wide { max-width: 480px; padding: 20px 18px 16px; text-align: left; max-height: calc(100vh - 48px - env(safe-area-inset-bottom)); overflow-y: auto; }
.results-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.results-head h2 { margin: 0; font-size: 22px; }
.pills { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.pills:empty { display: none; }
.pills button { font: inherit; font-size: 13px; font-weight: 600; border: 1.5px solid var(--grid-line); background: transparent; color: var(--muted); padding: 5px 11px; border-radius: 999px; cursor: pointer; }
.pills button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 12px; }
.tile { background: var(--bg); border-radius: 12px; padding: 10px 8px; text-align: center; }
.tile .v { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.tile .k { font-size: 11px; color: var(--muted); margin-top: 3px; text-transform: uppercase; letter-spacing: .04em; }
.chart-wrap { position: relative; width: 100%; }
.chart { width: 100%; height: 200px; display: block; overflow: visible; touch-action: none; }
.chart .grid-line { stroke: var(--grid-line); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .series { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .avg { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .pt { fill: var(--accent); stroke: var(--card); stroke-width: 2; }
.chart .pt.best { fill: var(--gold); }
.chart .pt.hot { r: 7; }
.chart .cross { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 2 3; }
.chart .empty { fill: var(--muted); font-size: 14px; font-family: inherit; }
.tip { position: absolute; top: 0; left: 0; background: var(--fg); color: var(--bg); font-size: 12px; padding: 6px 9px; border-radius: 8px; pointer-events: none; white-space: nowrap; transform: translate(-50%, -115%); }
.table-wrap { max-height: 170px; overflow-y: auto; margin-top: 12px; border-top: 1px solid var(--grid-line); }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table td { padding: 6px 4px; border-bottom: 1px solid var(--grid-line); font-variant-numeric: tabular-nums; }
.table td:nth-last-child(2) { text-align: right; font-weight: 600; }
.table td.act { width: 1%; text-align: right; padding-right: 0; }
.link.small { font-size: 13px; padding: 2px 4px; }
.table td.muted { color: var(--muted); }
.card .row .btn { flex: 1; padding: 13px 0; font-size: 16px; }
.trophy { width: 72px; height: 72px; margin: 0 auto; border-radius: 50%; background: color-mix(in srgb, var(--gold) 22%, transparent); display: flex; align-items: center; justify-content: center; animation: pop .6s cubic-bezier(.2,.9,.3,1.4) .1s both; }
.trophy svg { width: 44px; height: 44px; fill: var(--gold); filter: drop-shadow(0 2px 2px rgba(0,0,0,.25)); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { transform: translateY(40px) scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes pop { 0% { transform: scale(.3); opacity: 0; } 60% { transform: scale(1.25); opacity: 1; } 100% { transform: scale(1); } }
@keyframes bounce { 0% { transform: scale(1); } 40% { transform: scale(1.1) rotate(-2deg); } 70% { transform: scale(.96) rotate(1deg); } 100% { transform: scale(1); } }
@keyframes glow { from { box-shadow: 0 0 0 3px var(--gold); } to { box-shadow: 0 0 0 3px var(--gold), 0 0 24px var(--gold); } }
/* .solved holds the settled gold look, .celebrate the motion that only plays on a fresh win. */
.solved .grid { box-shadow: 0 0 0 3px var(--gold); }
.celebrate .grid { animation: glow .9s ease-in-out .3s 2 alternate; }
.solved .queens .cell svg:not(.x) { width: 70%; height: 70%; fill: var(--gold); color: var(--gold); filter: drop-shadow(0 1px 2px rgba(0,0,0,.35)); }
.celebrate .queens .cell svg:not(.x) { animation: bounce .6s cubic-bezier(.2,.9,.3,1.2) var(--d, 0s) both; }
.solved .queens .cell svg.x { opacity: 0; transition: opacity .4s; }
.celebrate .tango .cell svg { animation: bounce .6s cubic-bezier(.2,.9,.3,1.2) var(--d, 0s) both; }
.solved .zip .path polyline { stroke: var(--gold); opacity: 1; filter: drop-shadow(0 0 2px rgba(0,0,0,.25)); }
.solved .zip .path circle { fill: var(--gold); }
.solved .zip .num.hit { background: var(--gold); color: #1b1f2a; }
.celebrate .zip .num.hit { animation: bounce .6s cubic-bezier(.2,.9,.3,1.2) var(--d, 0s) both; }
/* Patches tile the whole board, so their win is a gold glimmer rippling out from the centre, */
/* not a pop; afterwards the patches rest in their original colours. */
@keyframes glimmer {
  45% { border-color: var(--gold); box-shadow: inset 0 0 0 3px rgba(255,255,255,.65), inset 0 0 16px rgba(242,183,5,.5); }
}
.celebrate .patches .patch { animation: glimmer .7s ease-in-out var(--sd, 0s) both; }
@media (prefers-reduced-motion: reduce) {
  .celebrate .grid, .celebrate .queens .cell svg:not(.x), .celebrate .tango .cell svg,
  .celebrate .zip .num.hit, .celebrate .patches .patch { animation: none; }
}

/* Queens */
/* Region colours are light in both themes, so the cell lines stay dark instead of following --grid-line. */
.queens .cell { color: #1b1f2a; border-color: rgba(27, 31, 42, 0.28); }
.queens .cell.bt { border-top: 2px solid #1b1f2a; }
.queens .cell.bl { border-left: 2px solid #1b1f2a; }
.queens .cell.bb { border-bottom: 2px solid #1b1f2a; }
.queens .cell.br { border-right: 2px solid #1b1f2a; }
.queens .cell .x { opacity: .55; }
.queens .cell .x.auto { opacity: .3; }

/* Tango */
.tango .cell.given { background: var(--cell-given); }
.tango .overlay { --gb: 2px; } /* the .grid border width */
.tango .con { position: absolute; width: 20px; height: 20px; transform: translate(-50%, -50%); }
.tango .con svg { width: 100%; height: 100%; display: block; overflow: visible; }
.tango .con .ring { fill: var(--card); stroke: var(--line); stroke-width: 1.4; }
.tango .con path { fill: none; stroke: var(--fg); stroke-width: 1.7; stroke-linecap: round; }

/* Zip */
.zip .cell.wr { border-right: 3px solid var(--line); }
.zip .cell.wd { border-bottom: 3px solid var(--line); }
.zip .num {
  width: 68%; height: 68%; border-radius: 50%; background: var(--fg); color: var(--bg);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: clamp(16px, 4.8vmin, 30px);
  position: relative; z-index: 2;
}
.zip .num.hit { background: var(--accent); color: #fff; }
.zip .path { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; pointer-events: none; } /* touches on the line reach the grid */
.zip .path polyline { fill: none; stroke: var(--accent); stroke-width: .42; stroke-linecap: round; stroke-linejoin: round; opacity: .85; }
.zip .path circle { fill: var(--accent); }

/* Patches: a white board with dashed cell lines; clues are soft coloured tiles shaped like their patch. */
.patches { --pt-board: #ffffff; --pt-line: #cfd3d9; --pt-edge: #e3e5e9; background: var(--pt-board); border-radius: 14px; }
@media (prefers-color-scheme: dark) { .patches { --pt-board: var(--cell); --pt-line: #565e6e; --pt-edge: #3a404d; } }
.patches .grid { position: relative; z-index: 1; background: transparent; border: 2.5px solid var(--pt-edge); border-radius: 14px; overflow: hidden; }
.patches .cell { border: 0; z-index: 1; }
.patches .lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.patches .lines line { stroke: var(--pt-line); stroke-width: 1.5; stroke-dasharray: 4.5 4.5; }
.patches .overlay { z-index: 0; }
.patches .ptile {
  position: relative; display: flex; align-items: center; justify-content: center;
  background: var(--pc); border-radius: 10%; box-shadow: 0 3px 6px rgba(27,31,42,.12);
}
.patches .ptile.square { width: 60%; height: 60%; }
.patches .ptile.tall { width: 50%; height: 74%; }
.patches .ptile.wide { width: 74%; height: 50%; }
.patches .ptile.any { width: 74%; height: 74%; background: none; box-shadow: none; }
.patches .ptile .arm, .patches .ptile .core {
  position: absolute; border: 1.5px dashed color-mix(in srgb, var(--pc) 70%, #000); border-radius: 12%;
}
.patches .ptile .arm { background: color-mix(in srgb, var(--pc) 55%, var(--pt-board)); }
.patches .ptile .arm.v { left: 17%; right: 17%; top: 0; bottom: 0; }
.patches .ptile .arm.h { top: 17%; bottom: 17%; left: 0; right: 0; }
.patches .ptile .core { inset: 17%; background: var(--pc); border-radius: 6%; }
.patches .clue {
  position: relative; z-index: 1; color: #fff; font-weight: 600; line-height: 1;
  font-size: clamp(18px, 6vmin, 36px); text-shadow: 0 1px 2px rgba(27,31,42,.18);
}
.patches .patch {
  position: absolute; border-radius: 12px;
  background: color-mix(in srgb, var(--pc) 24%, transparent);
  border: 2px solid color-mix(in srgb, var(--pc) 70%, transparent);
  box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--pt-board) 60%, transparent);
}
.patches .preview { position: absolute; z-index: 2; border: 2.5px dashed var(--accent); background: color-mix(in srgb, var(--accent) 25%, transparent); border-radius: 7px; pointer-events: none; }
.patches .preview.bad { border-color: var(--error); background: color-mix(in srgb, var(--error) 25%, transparent); }
/* Short landscape screens (phones on their side): board left, controls right; home tiles in one row. */
@media (orientation: landscape) and (max-height: 520px) {
  .top, main { max-width: 900px; }
  #game { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto auto auto auto 1fr; column-gap: 20px; }
  #game > .fit { grid-column: 1; grid-row: 1 / -1; }
  #game > :not(.fit) { grid-column: 2; }
  #game .actions { justify-content: flex-start; }
  #game .rules, #game .seed { text-align: left; }
  .tiles-grid { grid-template-columns: repeat(4, 1fr); }
  .tile-game .glyph { height: max(36px, min((25cqw - 39px) / 1.6, 100cqh - 196px)); }
}
