/* ---------- Theme ---------- */
:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #1d2430;
  --muted: #6b7380;
  --accent: #2f62d0;
  --accent-text: #ffffff;
  --line: #c9ced6;
  --line-thick: #2b3340;
  --given: #1d2430;
  --user: #2f62d0;
  --wrong: #d23c3c;
  --wrong-bg: #fbe3e3;
  --sel: #bcd3ff;
  --peer: #e6ecf7;
  --same: #c9d9f7;
  --note: #5b6472;
  --note-hl-bg: #2f62d0;
  --note-hl: #ffffff;
  --cage: #56606f;
  --shadow: 0 10px 40px rgba(20, 30, 50, .18);

  --gutter: 12px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1216;
  --surface: #191e25;
  --surface-2: #232a33;
  --text: #e7ebf0;
  --muted: #8f99a6;
  --accent: #5b8cff;
  --accent-text: #0b1020;
  --line: #2f3642;
  --line-thick: #8794a8;
  --given: #e7ebf0;
  --user: #7fa8ff;
  --wrong: #ff6b6b;
  --wrong-bg: #45232a;
  --sel: #2d4c86;
  --peer: #1f2733;
  --same: #2a3c5d;
  --note: #9aa4b2;
  --note-hl-bg: #5b8cff;
  --note-hl: #0b1020;
  --cage: #93a0b4;
  --shadow: 0 10px 40px rgba(0, 0, 0, .55);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1216;
    --surface: #191e25;
    --surface-2: #232a33;
    --text: #e7ebf0;
    --muted: #8f99a6;
    --accent: #5b8cff;
    --accent-text: #0b1020;
    --line: #2f3642;
    --line-thick: #8794a8;
    --given: #e7ebf0;
    --user: #7fa8ff;
    --wrong: #ff6b6b;
    --wrong-bg: #45232a;
    --sel: #2d4c86;
    --peer: #1f2733;
    --same: #2a3c5d;
    --note: #9aa4b2;
    --note-hl-bg: #5b8cff;
    --note-hl: #0b1020;
    --cage: #93a0b4;
    --shadow: 0 10px 40px rgba(0, 0, 0, .55);
  }
}

/* ---------- Native feel ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
body { overflow: hidden; position: fixed; inset: 0; }
button {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0;
  cursor: pointer; touch-action: manipulation;
}
button:focus { outline: none; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { display: block; }
[hidden] { display: none !important; }

/* ---------- Layout ---------- */
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-t) + 4px) calc(var(--safe-r) + var(--gutter)) calc(var(--safe-b) + 8px) calc(var(--safe-l) + var(--gutter));
}

.top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 52px;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}
.title { flex: 1; min-width: 0; }
#mode-label { font-weight: 650; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#sub-label { font-size: 12.5px; color: var(--muted); }
#timer { font-variant-numeric: tabular-nums; font-size: 17px; color: var(--muted); font-weight: 550; }
body.no-timer #timer { visibility: hidden; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center; flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
svg .fill { fill: currentColor; stroke: none; }
#btn-pause .i-play { display: none; }
body.paused #btn-pause .i-play { display: block; }
body.paused #btn-pause .i-pause { display: none; }

.play {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-block: 4px;
  gap: clamp(14px, 4dvh, 40px);
  /* Board size: fit the width, but leave room for header + controls on short screens. */
  --board: min(
    100vw - 2 * var(--gutter) - var(--safe-l) - var(--safe-r),
    100dvh - 290px - var(--safe-t) - var(--safe-b),
    600px
  );
}

/* ---------- Board ---------- */
#board-wrap { position: relative; width: var(--board); height: var(--board); flex: none; }
#board { position: absolute; inset: 0; container-type: inline-size; }
.grid {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--surface);
  border: 2.5px solid var(--line-thick);
  border-radius: 8px;
  overflow: hidden;
}
.cell {
  position: relative;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: grid;
  place-items: center;
  background-color: var(--surface);
  transition: background-color .08s;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.box-r { border-right: 2.5px solid var(--line-thick); }
.cell.box-b { border-bottom: 2.5px solid var(--line-thick); }
.cell.peer { background-color: var(--peer); }
.cell.same { background-color: var(--same); }
.cell.selected { background-color: var(--sel); }
.cell.wrong:not(.selected) { background-color: var(--wrong-bg); }

.val {
  font-size: calc(100cqw / 9 * .6);
  line-height: 1;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.cell.given .val { color: var(--given); font-weight: 650; }
.cell.user .val { color: var(--user); }
.cell.wrong .val, .cell.user.conflict .val { color: var(--wrong); }

.notes {
  position: absolute; inset: 6%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}
.killer .notes { inset: 27% 10% 6% 12%; }
.notes span {
  display: grid; place-items: center;
  font-size: calc(100cqw / 9 * .25);
  line-height: 1;
  color: var(--note);
  visibility: hidden;
  border-radius: 3px;
}
.killer .notes span { font-size: calc(100cqw / 9 * .21); }
.notes span.on { visibility: visible; }
.notes span.hl { background: var(--note-hl-bg); color: var(--note-hl); font-weight: 650; }
.cell.user .notes, .cell.given .notes { display: none; }

.sum {
  position: absolute; left: 6%; top: 3%;
  font-size: calc(100cqw / 9 * .22);
  line-height: 1;
  font-weight: 650;
  color: var(--cage);
  pointer-events: none;
  z-index: 3;
}
.sum:empty { display: none; }

.cages {
  position: absolute; inset: 2.5px; /* inside the outer border */
  width: calc(100% - 5px); height: calc(100% - 5px);
  pointer-events: none;
  z-index: 2;
}
.cages path {
  fill: none;
  stroke: var(--cage);
  stroke-width: 2.6;
  stroke-dasharray: 7 5;
  stroke-linecap: butt;
}

.overlay {
  position: absolute; inset: 0; z-index: 5;
  border-radius: 8px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: var(--muted);
  font-weight: 550;
}
#paused svg { width: 56px; height: 56px; color: var(--accent); }
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 3.5px solid var(--surface-2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Controls ---------- */
#controls { width: var(--board); flex: none; display: flex; flex-direction: column; gap: 12px; }
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 6px 0; border-radius: 12px;
  font-size: 12px; color: var(--muted);
}
.tool svg { color: var(--text); }
.tool:active { background: var(--surface-2); }
.tool:disabled { opacity: .35; }
.badge {
  font-size: 9.5px; font-weight: 700; padding: 1px 4px; border-radius: 5px;
  background: var(--surface-2); color: var(--muted); vertical-align: 1px;
}
#btn-notes[aria-pressed="true"] svg { color: var(--accent); }
#btn-notes[aria-pressed="true"] .badge { background: var(--accent); color: var(--accent-text); }

.pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
.pad button {
  position: relative;
  height: clamp(52px, 8.5dvh, 72px);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 1px 0 var(--line);
  font-size: clamp(24px, 7.2vw, 32px);
  font-weight: 500;
  color: var(--user);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1;
  transition: transform .05s, background-color .1s;
}
.pad button small { font-size: 11px; color: var(--muted); font-weight: 500; margin-top: 3px; min-height: 11px; }
.pad button:active { transform: scale(.94); background: var(--surface-2); }
.pad button.done { opacity: .3; }
body.notes-mode .pad button { color: var(--note); font-size: clamp(18px, 5.4vw, 24px); }
body.solved .pad button, body.solved .tool { opacity: .3; pointer-events: none; }

/* Landscape tablets / desktop: board left, controls right */
@media (min-aspect-ratio: 5/4) and (min-width: 700px) {
  .play {
    flex-direction: row;
    justify-content: center;
    gap: 32px;
    --board: min(100dvh - 90px - var(--safe-t) - var(--safe-b), 100vw - 400px, 760px);
  }
  #controls { width: 300px; gap: 20px; }
  .pad { grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .pad button { height: 82px; font-size: 34px; }
  .top { max-width: none; }
}

/* ---------- Dialogs ---------- */
dialog {
  border: 0; padding: 0; color: var(--text); background: var(--surface);
  box-shadow: var(--shadow);
  -webkit-user-select: none; user-select: none;
}
dialog::backdrop { background: rgba(10, 14, 20, .45); }

.sheet {
  width: min(520px, 100vw);
  max-width: 100vw;
  max-height: 100dvh;
  margin: auto auto 0;
  border-radius: 20px 20px 0 0;
  padding: 6px calc(18px + var(--safe-r)) calc(18px + var(--safe-b)) calc(18px + var(--safe-l));
  overflow-y: auto;
  overscroll-behavior: contain;
}
@media (min-width: 700px) {
  .sheet { margin: auto; border-radius: 20px; max-height: 90dvh; padding-bottom: 18px; }
}
.sheet[open] { animation: up .22s cubic-bezier(.2, .8, .3, 1); }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: -6px; background: var(--surface); padding-top: 6px; z-index: 1; }
.sheet h2 { margin: 0; font-size: 20px; }
.sheet section { padding: 12px 0; border-bottom: 1px solid var(--surface-2); }
.sheet section:last-of-type { border-bottom: 0; }
.sheet h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

.seg {
  display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; margin-bottom: 8px;
}
.seg button { flex: 1; padding: 9px 4px; border-radius: 9px; font-weight: 550; color: var(--muted); font-size: 15px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.seg.small { margin: 0; min-width: 210px; }
.seg.small button { padding: 6px 4px; font-size: 14px; }

.row-btns { display: flex; gap: 10px; margin-top: 6px; }
.row-btns > * { flex: 1; }
.primary, .secondary {
  padding: 13px 10px; border-radius: 12px; font-weight: 650; font-size: 15.5px;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--surface-2); }
.primary:active, .secondary:active { filter: brightness(.92); }
.primary:disabled, .secondary:disabled { opacity: .5; }
.hint { margin: 8px 2px 0; color: var(--muted); font-size: 13px; min-height: 1em; }

.toggle, .setting-line {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 9px 0; font-size: 15.5px;
}
.toggle input {
  appearance: none; -webkit-appearance: none;
  width: 46px; height: 28px; border-radius: 14px; background: var(--surface-2);
  position: relative; flex: none; margin: 0; cursor: pointer; transition: background .15s;
  box-shadow: inset 0 0 0 1px var(--line);
}
.toggle input::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s;
}
.toggle input:checked { background: var(--accent); box-shadow: none; }
.toggle input:checked::after { transform: translateX(18px); }

#stats table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
#stats th { text-align: left; font-weight: 550; color: var(--muted); padding: 4px 0; }
#stats th:not(:first-child), #stats td:not(:first-child) { text-align: right; }
#stats td { padding: 5px 0; border-top: 1px solid var(--surface-2); }
.footer { color: var(--muted); font-size: 12px; text-align: center; margin: 8px 0 0; }

.card {
  width: min(360px, calc(100vw - 32px));
  border-radius: 22px;
  padding: 26px 22px 20px;
  text-align: center;
}
.card[open] { animation: pop .25s cubic-bezier(.2, .9, .3, 1.2); }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.card h2 { margin: 6px 0 2px; font-size: 24px; }
.muted { color: var(--muted); margin: 0; }
.win-badge {
  width: 58px; height: 58px; margin: 0 auto; border-radius: 50%;
  display: grid; place-items: center; font-size: 30px; font-weight: 700;
  background: var(--accent); color: var(--accent-text);
}
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); margin: 18px 0 8px; }
.win-stats b { display: block; font-size: 21px; font-variant-numeric: tabular-nums; }
.win-stats span { font-size: 12.5px; color: var(--muted); }
.record { color: var(--accent); font-weight: 650; margin: 4px 0 6px; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 20px);
  background: var(--text); color: var(--bg);
  padding: 11px 16px; border-radius: 12px; font-size: 14.5px; font-weight: 550;
  box-shadow: var(--shadow); z-index: 100; display: flex; gap: 14px; align-items: center;
  max-width: calc(100vw - 32px);
}
#toast button { color: var(--accent); font-weight: 700; filter: brightness(1.4); }
