/* ---------------------------------------------------------------- reset -- */

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

/* `hidden` is only a UA stylesheet rule, so any author `display` beats it.
   Pin it here once, with !important, or every panel with a layout class
   stays painted while el.hidden reads true. */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button { font: inherit; color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --------------------------------------------------------------- tokens -- */

:root {
  --bg: #f4f2ee;
  --panel: #fbfaf8;
  --panel-2: #eceae4;
  --ink: #1f2229;
  --muted: #6d6a63;
  --faint: #a3a09a;
  --line: #d9d5cd;
  --line-strong: #8d887f;
  --cell-bg: #fdfcfa;
  --cell-fill: #2f3542;
  --cell-hover: #e6efe9;
  --accent: #1f7a5a;
  --accent-soft: #d7ece2;
  --warn: #b4462f;
  --warn-soft: #f7ddd6;
  --hint: #b8860b;
  --shadow: 0 1px 2px rgba(31, 34, 41, .06), 0 8px 24px rgba(31, 34, 41, .07);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161b;
    --panel: #1c1f26;
    --panel-2: #23262f;
    --ink: #e8e5df;
    --muted: #9a968e;
    --faint: #6a675f;
    --line: #333742;
    --line-strong: #6d7280;
    --cell-bg: #1a1d24;
    --cell-fill: #e6e2d9;
    --cell-hover: #23303a;
    --accent: #4fbf95;
    --accent-soft: #17332a;
    --warn: #e07a5f;
    --warn-soft: #3a231d;
    --hint: #d8ad3f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 28px rgba(0, 0, 0, .35);
  }
}

:root[data-theme="dark"] {
  --bg: #14161b;
  --panel: #1c1f26;
  --panel-2: #23262f;
  --ink: #e8e5df;
  --muted: #9a968e;
  --faint: #6a675f;
  --line: #333742;
  --line-strong: #6d7280;
  --cell-bg: #1a1d24;
  --cell-fill: #e6e2d9;
  --cell-hover: #23303a;
  --accent: #4fbf95;
  --accent-soft: #17332a;
  --warn: #e07a5f;
  --warn-soft: #3a231d;
  --hint: #d8ad3f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 28px rgba(0, 0, 0, .35);
}

/* ---------------------------------------------------------------- shell -- */

.shell {
  max-width: 780px;
  margin: 0 auto;
  padding: 18px 16px calc(28px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark { width: 26px; height: 26px; flex: none; }
.brand-mark rect:first-child { fill: var(--cell-fill); }
.brand-pips rect { fill: var(--bg); }
.brand-text { font-size: 17px; font-weight: 640; letter-spacing: -.015em; }

.icon-btn {
  width: 36px; height: 36px; flex: none;
  display: grid; place-items: center;
  font-size: 16px; line-height: 1;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.icon-btn:hover { background: var(--panel-2); }

/* -------------------------------------------------------------- toolbar -- */

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }

.seg {
  display: flex; gap: 2px; padding: 3px;
  background: var(--panel-2); border-radius: 11px;
}
.seg-btn {
  padding: 7px 14px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 13.5px; font-weight: 560; cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: background .15s, color .15s;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn[aria-checked="true"] {
  background: var(--panel); color: var(--ink); box-shadow: var(--shadow);
}

.btn {
  padding: 8px 15px;
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
  font-size: 13.5px; font-weight: 560; cursor: pointer;
  transition: background .15s, border-color .15s, transform .06s;
}
.btn:hover { background: var(--panel-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .42; cursor: default; transform: none; }
.btn-primary { background: var(--cell-fill); color: var(--bg); border-color: var(--cell-fill); }
.btn-primary:hover { background: var(--cell-fill); filter: brightness(1.12); }

/* ---------------------------------------------------------------- stats -- */

.stats {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  padding: 11px 6px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius);
}
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.stat-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .085em; color: var(--faint); font-weight: 620; }
.stat-value { font-size: 17px; font-weight: 620; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat-value.is-best { color: var(--accent); }

/* ---------------------------------------------------------------- board -- */

.stage { position: relative; }

.board-scroll { overflow-x: auto; overflow-y: hidden; padding: 2px; margin: -2px; }

.board {
  --cell: 30px;
  display: grid;
  grid-template-columns: calc(var(--rc) * var(--cell)) repeat(var(--n), var(--cell));
  grid-template-rows: calc(var(--cc) * var(--cell)) repeat(var(--n), var(--cell));
  width: max-content;
  margin: 0 auto;
  outline: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
/* The focused cell is the focus indicator; a ring around the whole grid,
   clue lanes included, says nothing about where the keyboard is. */
.board:focus-visible { outline: none; }

/* Rows carry the ARIA semantics; their children are the grid items. */
.brow { display: contents; }

.corner { grid-area: 1 / 1; }

.clue {
  display: flex; gap: 0;
  color: var(--muted);
  font-size: calc(var(--cell) * .46);
  font-weight: 620;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  transition: color .18s, opacity .18s;
}
.clue-col { flex-direction: column; align-items: center; justify-content: flex-end; padding-bottom: calc(var(--cell) * .18); }
.clue-row { flex-direction: row; align-items: center; justify-content: flex-end; padding-right: calc(var(--cell) * .22); }
.clue span { display: block; padding: calc(var(--cell) * .07) calc(var(--cell) * .11); }
.clue-empty { color: var(--faint); }

.clue.is-done { color: var(--faint); opacity: .5; }
.clue.is-lit { color: var(--ink); background: var(--accent-soft); border-radius: 5px; }
.clue.is-lit.is-done { color: var(--faint); }

.cell {
  position: relative;
  background: var(--cell-bg);
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  cursor: pointer;
}
.cell.edge-r { border-right: 1px solid var(--line); }
.cell.edge-b { border-bottom: 1px solid var(--line); }
.cell.g5-l { border-left-color: var(--line-strong); }
.cell.g5-t { border-top-color: var(--line-strong); }
.cell.edge-r { border-right-color: var(--line-strong); }
.cell.edge-b { border-bottom-color: var(--line-strong); }
.cell.edge-l { border-left-color: var(--line-strong); }
.cell.edge-t { border-top-color: var(--line-strong); }

.cell.is-lit { background: var(--cell-hover); }

.cell.is-filled { background: var(--cell-fill); }

.cell.is-marked::before,
.cell.is-marked::after {
  content: ""; position: absolute;
  left: 50%; top: 50%;
  width: calc(var(--cell) * .42); height: 1.5px;
  background: var(--faint); border-radius: 1px;
}
.cell.is-marked::before { transform: translate(-50%, -50%) rotate(45deg); }
.cell.is-marked::after  { transform: translate(-50%, -50%) rotate(-45deg); }

.cell.is-hinted { box-shadow: inset 0 0 0 2px var(--hint); }
.cell.is-wrong { background: var(--warn); }
.board:focus-visible .cell.is-cursor { box-shadow: inset 0 0 0 2px var(--accent); z-index: 2; }

.board.is-won .cell { cursor: default; border-color: transparent; transition: border-color .5s ease; }
.board.is-won .cell.is-lit { background: var(--cell-bg); }
.board.is-won .cell.is-marked::before,
.board.is-won .cell.is-marked::after { opacity: 0; }

/* ------------------------------------------------------------- controls -- */

.controls { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }

.mode { display: flex; gap: 2px; padding: 3px; background: var(--panel-2); border-radius: 11px; }
.mode-btn {
  display: flex; align-items: center; gap: 7px;
  padding: 7px 13px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 13.5px; font-weight: 560; cursor: pointer;
  transition: background .15s, color .15s;
}
.mode-btn.is-active { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }
.mode-glyph { width: 14px; height: 14px; flex: none; border-radius: 3px; position: relative; }
.mode-glyph-fill { background: var(--cell-fill); }
.mode-glyph-mark { border: 1px solid var(--line-strong); }
.mode-glyph-mark::before, .mode-glyph-mark::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 9px; height: 1.5px; background: var(--muted);
}
.mode-glyph-mark::before { transform: translate(-50%, -50%) rotate(45deg); }
.mode-glyph-mark::after  { transform: translate(-50%, -50%) rotate(-45deg); }

.tools { display: flex; gap: 6px; flex-wrap: wrap; }

.hintline { margin: 0; color: var(--faint); font-size: 12.5px; text-align: center; }

/* ---------------------------------------------------------------- howto -- */

.howto {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 0;
}
.howto summary {
  padding: 12px 15px; cursor: pointer;
  font-size: 13.5px; font-weight: 600; color: var(--ink);
  list-style: none;
}
.howto summary::-webkit-details-marker { display: none; }
.howto summary::after { content: " ↓"; color: var(--faint); }
.howto[open] summary::after { content: " ↑"; }
.howto-body { padding: 0 15px 14px; color: var(--muted); font-size: 13.5px; }
.howto-body p { margin: 0 0 10px; }
.howto-body code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }

.keys { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 12px 0 0; padding: 0; list-style: none; }
.keys li { display: flex; align-items: center; gap: 5px; font-size: 12.5px; }
kbd {
  display: inline-block; min-width: 20px; padding: 2px 5px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-bottom-width: 2px; border-radius: 5px;
  font: 600 11px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center; color: var(--ink);
}

/* --------------------------------------------------------------- footer -- */

.footer {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  color: var(--faint); font-size: 12px;
}
.footer code { font: 600 11.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); }
.linkbtn {
  background: none; border: 0; padding: 0;
  color: var(--accent); font-size: 12px; font-weight: 600;
  cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}

/* ------------------------------------------------------------------ win -- */

.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

/* The finished picture is the reward, so the result is a panel under the
   board rather than a scrim over it. */
.win {
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
  animation: rise .3s cubic-bezier(.2, .8, .3, 1);
}
@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
}

.win-kicker { margin: 0 0 3px; font-size: 10.5px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); }
.win-title { margin: 0 0 14px; font-size: 20px; font-weight: 660; letter-spacing: -.02em; }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 0 12px; max-width: 340px; margin-inline: auto; }
.win-stat { display: flex; flex-direction: column; gap: 2px; }
.win-stats dt { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--faint); font-weight: 620; }
.win-stats dd { margin: 0; font-size: 15px; font-weight: 620; font-variant-numeric: tabular-nums; }
.win-note { margin: 0 0 12px; font-size: 12.5px; color: var(--accent); font-weight: 600; }
.win-note:empty { display: none; }
.win-actions { display: flex; gap: 8px; justify-content: center; }

/* ----------------------------------------------------------- responsive -- */

@media (max-width: 520px) {
  .shell { padding: 14px 10px calc(24px + env(safe-area-inset-bottom)); gap: 12px; }
  .stat-value { font-size: 15.5px; }
  .toolbar, .controls { justify-content: center; }
  .tools { justify-content: center; }
  .btn { padding: 8px 13px; }
}

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