:root {
  --bg: #f6f5f2;
  --text: #3b3a45;
  --text-strong: #1d1c26;
  --muted: #7d7b8a;
  --panel: #ffffff;
  --border: #e3e1dc;
  --line: #c9c6cf;
  --line-thick: #3b3a45;
  --cell: #ffffff;
  --given: #1d1c26;
  --user: #d62839;
  --note: #6f6d7c;
  --accent: #e63946;
  --accent-soft: rgba(230, 57, 70, 0.12);
  --accent-text: #ffffff;
  --peer: #f1eff4;
  --same: #fbd7d9;
  --sel: #f7b9bd;
  --spot: rgba(230, 57, 70, 0.55);
  --wrong-bg: #fde2e4;
  --secondary: #ecebef;
  --secondary-text: #2c2b36;
  --danger: #c2410c;
  --shadow: 0 10px 30px rgba(30, 20, 40, 0.14);
  --overlay: rgba(246, 245, 242, 0.86);
  --board: 360px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f0f14;
  --text: #d9d9e3;
  --text-strong: #f4f4f8;
  --muted: #9898ab;
  --panel: #191922;
  --border: #2c2c3a;
  --line: #34344a;
  --line-thick: #8a8aa3;
  --cell: #191922;
  --given: #f4f4f8;
  --user: #ff7a83;
  --note: #a3a3b8;
  --accent: #e63946;
  --accent-soft: rgba(230, 57, 70, 0.2);
  --peer: #212130;
  --same: #4a2430;
  --sel: #6b2a36;
  --spot: rgba(255, 122, 131, 0.7);
  --wrong-bg: #45202a;
  --secondary: #262634;
  --secondary-text: #ececf1;
  --danger: #fb923c;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  --overlay: rgba(15, 15, 20, 0.86);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0f14; --text: #d9d9e3; --text-strong: #f4f4f8; --muted: #9898ab; --panel: #191922; --border: #2c2c3a;
    --line: #34344a; --line-thick: #8a8aa3; --cell: #191922; --given: #f4f4f8; --user: #ff7a83; --note: #a3a3b8;
    --accent: #e63946; --accent-soft: rgba(230, 57, 70, 0.2); --peer: #212130; --same: #4a2430; --sel: #6b2a36;
    --spot: rgba(255, 122, 131, 0.7); --wrong-bg: #45202a; --secondary: #262634; --secondary-text: #ececf1;
    --danger: #fb923c; --shadow: 0 10px 30px rgba(0, 0, 0, 0.5); --overlay: rgba(15, 15, 20, 0.86);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  line-height: 1.5;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.app {
  max-width: 540px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 24px max(12px, env(safe-area-inset-left));
}

/* ---------- header ---------- */
.top { display: flex; align-items: center; gap: 10px; }
.brand { display: flex; }
.brand img { width: 34px; height: 34px; }
.title { font-size: 1.5rem; font-weight: 800; letter-spacing: -0.02em; margin: 0; color: var(--text-strong); flex: 1; }
.title small { font-size: 0.6em; font-weight: 600; color: var(--muted); margin-left: 6px; }
.top-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent; color: var(--text);
  display: inline-grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: var(--secondary); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.small { width: 32px; height: 32px; border-radius: 10px; }
.icon-btn.small svg { width: 18px; height: 18px; }

/* ---------- mode + level tabs ---------- */
.modes { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 8px; }
.segmented { display: inline-flex; background: var(--secondary); border-radius: 12px; padding: 3px; gap: 2px; }
.segmented button {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 9px; cursor: pointer; font-weight: 600;
  font-size: 0.92rem; color: var(--muted); position: relative;
}
.segmented button[aria-selected="true"] { background: var(--panel); color: var(--text-strong); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.segmented.levels { flex: 1; justify-content: space-between; }
.segmented.levels button { flex: 1; padding-inline: 6px; }
.dot { position: absolute; top: 5px; right: 4px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }

.status { display: flex; align-items: center; gap: 12px; font-size: 0.9rem; color: var(--muted); margin: 4px 2px 8px; min-height: 32px; }
.status b { color: var(--text-strong); }
.pill { background: var(--accent-soft); color: var(--accent); font-weight: 700; padding: 2px 10px; border-radius: 99px; font-size: 0.82rem; }
.timer-wrap { margin-left: auto; display: inline-flex; align-items: center; gap: 2px; }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text-strong); font-size: 1rem; }

/* ---------- board ---------- */
.play { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.board-wrap { position: relative; width: var(--board); height: var(--board); flex: none; }
.board {
  width: 100%; height: 100%;
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(0, 1fr)); gap: 2px;
  background: var(--line-thick); border: 2px solid var(--line-thick); border-radius: 10px; overflow: hidden;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.box { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(0, 1fr)); gap: 1px; background: var(--line); min-height: 0; }
.cell {
  position: relative; background: var(--cell); display: grid; place-items: center; cursor: pointer; min-width: 0; min-height: 0; overflow: hidden;
  font-size: calc(var(--board) / 15); font-weight: 500; color: var(--user); transition: background-color 0.12s;
}
.large .cell { font-size: calc(var(--board) / 12.5); }
.cell.given { color: var(--given); font-weight: 700; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.focus-area { background: var(--accent-soft); }
.cell.wrong { color: var(--accent); background: var(--wrong-bg); }
.cell.conflict .val { text-decoration: underline wavy var(--accent) 2px; text-underline-offset: 4px; }
.cell.spot::after {
  content: ''; position: absolute; left: 50%; top: 50%; width: 18%; height: 18%; transform: translate(-50%, -50%);
  border-radius: 50%; background: var(--spot); opacity: 0.55; pointer-events: none;
}
.cell.spot:has(.notes:not(.hidden))::after { display: none; }
.cell.drop { background: var(--sel); box-shadow: inset 0 0 0 3px var(--accent); }
.cell.hint-focus { box-shadow: inset 0 0 0 3px var(--accent); }
.cell .val { line-height: 1; }
.notes {
  position: absolute; inset: 6%; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--board) / 34); color: var(--note); font-weight: 500; line-height: 1;
}
.large .notes { font-size: calc(var(--board) / 30); }
.notes span { display: grid; place-items: center; }
.notes span.hit { color: var(--accent-text); background: var(--accent); border-radius: 4px; font-weight: 700; }
.cell.pop .val { animation: pop 0.22s ease-out; }
@keyframes pop { 0% { transform: scale(0.5); opacity: 0.2; } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }
.cell.shake { animation: shake 0.32s ease-in-out; }
@keyframes shake { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
.cell.sparkle { animation: sparkle 0.7s ease-out; }
@keyframes sparkle { 0% { background: var(--cell); } 30% { background: #ffd166; } 100% { background: var(--cell); } }
.board.solved .cell { animation: sparkle 0.9s ease-out both; }
@media (prefers-reduced-motion: reduce) {
  .cell.pop .val, .cell.shake, .cell.sparkle, .board.solved .cell { animation: none; }
}
.board.blur { filter: blur(10px); }

.cover, .intro {
  position: absolute; inset: 0; display: grid; place-items: center; background: var(--overlay); border-radius: 10px; z-index: 5; padding: 16px;
}
.intro { background: transparent; pointer-events: none; align-items: end; }
.cover-card, .intro-card {
  background: var(--panel); border-radius: 16px; padding: 18px 20px; box-shadow: var(--shadow); text-align: center; max-width: 320px;
}
.intro-card { pointer-events: auto; text-align: left; font-size: 0.95rem; }
.intro-card p { margin: 0 0 10px; }
.cover-card h2 { margin: 0 0 6px; color: var(--text-strong); }
.cover-card p { margin: 0 0 14px; color: var(--muted); }

/* ---------- tools + number pad ---------- */
.side { display: flex; flex-direction: column; gap: 10px; width: var(--board); }
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tool {
  border: 0; background: transparent; border-radius: 12px; padding: 6px 2px; cursor: pointer; display: flex; flex-direction: column;
  align-items: center; gap: 2px; font-size: 0.78rem; font-weight: 600; color: var(--muted);
}
.tool svg { width: 24px; height: 24px; color: var(--text); }
.tool:hover { background: var(--secondary); }
.tool:disabled { opacity: 0.35; cursor: default; background: transparent; }
.tool em { font-style: normal; }
.tool[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.tool[aria-pressed="true"] svg { color: var(--accent); }

.pad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; touch-action: none; user-select: none; -webkit-user-select: none; }
.pad-side { display: none; }
.num {
  border: 0; border-radius: 12px; background: var(--secondary); color: var(--text-strong); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 6px 0 4px; min-height: 58px;
  font-size: 1.7rem; font-weight: 600; line-height: 1; position: relative; transition: transform 0.08s, background-color 0.12s;
}
.num small { font-size: 0.68rem; font-weight: 600; color: var(--muted); margin-top: 4px; }
.num:active { transform: scale(0.95); }
.num.armed { background: var(--accent); color: var(--accent-text); }
.num.armed small { color: rgba(255, 255, 255, 0.85); }
.num.armed.note-mode { background: var(--panel); color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.num.done { opacity: 0.35; }
.num.done small::before { content: '✓ '; }
.num.erase svg { width: 26px; height: 26px; }
.num.erase { color: var(--text); }
.notes-on .num:not(.erase) { font-size: 1.25rem; color: var(--note); }
.notes-on .num.armed { color: var(--accent); }

.ghost {
  position: fixed; left: 0; top: 0; width: 64px; height: 64px; margin: -32px 0 0 -32px; border-radius: 16px; z-index: 50;
  background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 2rem; font-weight: 700;
  box-shadow: var(--shadow); pointer-events: none; transition: transform 0.12s, width 0.12s, height 0.12s;
}
.ghost.as-note { transform: scale(0.65); background: var(--panel); color: var(--accent); box-shadow: inset 0 0 0 3px var(--accent), var(--shadow); }
.ghost.cancel { opacity: 0.55; }

/* ---------- hint bar ---------- */
.hint-bar {
  position: fixed; z-index: 30; top: max(8px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); width: min(540px, calc(100vw - 16px)); background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 0 12px 12px 0; padding: 12px 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
.hint-text b { color: var(--accent); }
.hint-actions { display: flex; gap: 8px; justify-content: flex-end; }
.hint-bar { animation: slide-down 0.2s ease-out; }
@keyframes slide-down { from { opacity: 0; transform: translate(-50%, -12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- buttons, dialogs, toast ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 11px 18px; font-weight: 700; cursor: pointer; font-size: 0.98rem; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn.small { padding: 7px 14px; font-size: 0.88rem; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--secondary); color: var(--secondary-text); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--border); }
.btn.wide { width: 100%; }
.btn svg { width: 20px; height: 20px; }

.sheet {
  border: 0; border-radius: 20px; padding: 18px 18px 20px; background: var(--panel); color: var(--text); width: min(440px, calc(100vw - 24px));
  box-shadow: var(--shadow); max-height: calc(100dvh - 24px);
}
.sheet::backdrop { background: rgba(10, 10, 15, 0.45); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 1.25rem; color: var(--text-strong); }
.close { border: 0; background: var(--secondary); width: 34px; height: 34px; border-radius: 50%; font-size: 1.3rem; cursor: pointer; line-height: 1; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.sheet-actions .btn { flex: 1 1 140px; }

.settings { display: flex; flex-direction: column; gap: 2px; }
.settings .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.settings .row > span { display: flex; flex-direction: column; font-weight: 600; color: var(--text-strong); font-size: 0.95rem; }
.settings small { font-weight: 400; color: var(--muted); font-size: 0.8rem; }
.settings select, .settings input[type="text"] {
  background: var(--secondary); color: var(--text-strong); border: 1px solid var(--border); border-radius: 10px; padding: 7px 8px; font: inherit;
  font-size: 0.9rem; max-width: 52%;
}
.settings .btn { margin-top: 8px; }
.settings .row.locked { opacity: 0.5; }
.locked-note { background: var(--accent-soft); color: var(--accent); border-radius: 10px; padding: 8px 10px; margin: 0 0 6px; font-size: 0.88rem; }
.switch { appearance: none; -webkit-appearance: none; width: 46px; height: 28px; border-radius: 99px; background: var(--line); position: relative; cursor: pointer; flex: none; transition: background 0.2s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform 0.2s; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }
.switch:disabled { cursor: default; }

.result-big { font-size: 3rem; font-weight: 900; color: var(--text-strong); margin: 0; line-height: 1.1; font-variant-numeric: tabular-nums; }
.result-sub { color: var(--muted); margin: 2px 0 12px; }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0; }
.fact { background: var(--secondary); border-radius: 12px; padding: 8px 10px; }
.fact span { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.fact b { font-size: 1.15rem; color: var(--text-strong); font-variant-numeric: tabular-nums; }
.versus { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.versus .side-card { background: var(--secondary); border-radius: 14px; padding: 12px; text-align: center; }
.versus .side-card.win { box-shadow: inset 0 0 0 2px var(--accent); }
.versus .who { font-weight: 700; color: var(--text-strong); }
.versus .score { font-size: 1.6rem; font-weight: 900; color: var(--text-strong); font-variant-numeric: tabular-nums; }
.versus small { color: var(--muted); display: block; }
.verdict { text-align: center; font-weight: 800; font-size: 1.15rem; color: var(--accent); margin: 6px 0; }
.tally { text-align: center; color: var(--muted); font-size: 0.9rem; }
.card-preview { width: 100%; border-radius: 14px; display: block; margin: 8px 0; box-shadow: var(--shadow); }
.earned { background: var(--accent-soft); color: var(--accent); border-radius: 10px; padding: 8px 10px; font-weight: 600; font-size: 0.9rem; margin: 8px 0 0; }
.challenge-intro .big { font-size: 1.2rem; color: var(--text-strong); margin: 0 0 6px; }
.rules { font-size: 0.85rem; color: var(--muted); margin: 8px 0 0; }

.stats-grid { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.stats-grid th, .stats-grid td { text-align: right; padding: 7px 4px; border-bottom: 1px solid var(--border); }
.stats-grid th:first-child, .stats-grid td:first-child { text-align: left; }
.stats-grid th { color: var(--muted); font-weight: 600; font-size: 0.78rem; }
.streak { display: flex; gap: 8px; margin: 12px 0; }
.ach { list-style: none; padding: 0; margin: 8px 0 0; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.ach li { background: var(--secondary); border-radius: 10px; padding: 7px 9px; font-size: 0.82rem; opacity: 0.45; }
.ach li.got { opacity: 1; }
.ach li b { display: block; color: var(--text-strong); }
.sheet h3 { font-size: 1rem; margin: 16px 0 4px; color: var(--text-strong); }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text-strong); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 0.92rem;
  transition: opacity 0.2s, transform 0.2s; z-index: 60; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.help { text-align: center; color: var(--muted); font-size: 0.82rem; margin: 14px 0 0; }
kbd { background: var(--secondary); border-radius: 5px; padding: 1px 5px; font-size: 0.8em; font-family: inherit; border: 1px solid var(--border); }

/* ---------- info sections + footer ---------- */
.info { margin-top: 28px; }
.info > p { color: var(--text); }
.info details { border-top: 1px solid var(--border); }
.info summary { cursor: pointer; list-style: none; padding: 12px 0; }
.info summary::-webkit-details-marker { display: none; }
.info summary h2 { display: inline; font-size: 1.05rem; color: var(--text-strong); }
.info summary::after { content: '+'; float: right; color: var(--muted); font-size: 1.2rem; }
.info details[open] summary::after { content: '–'; }
.details-body { padding-bottom: 12px; }
.details-body h3 { font-size: 0.98rem; color: var(--text-strong); margin: 14px 0 4px; }
.info a, .site-footer a, .article a { color: var(--accent); }
.site-footer { margin-top: 28px; padding-top: 14px; border-top: 1px solid var(--border); font-size: 0.85rem; color: var(--muted); }
.site-footer nav { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 8px; }
.site-footer [aria-current] { color: var(--text-strong); font-weight: 700; }
.article { line-height: 1.7; }
.article h1 { color: var(--text-strong); line-height: 1.2; }
.article h2 { color: var(--text-strong); margin-top: 28px; }
.article-page .top { justify-content: space-between; }
.not-found { text-align: center; padding-top: 80px; }

/* ---------- layouts (set by js/ui/layout.js) ---------- */
/* Phone, landscape: numbers 1-5 on the left edge, 6-9 + erase on the right. */
body[data-layout="landscape"] .app { max-width: none; padding-top: max(6px, env(safe-area-inset-top)); }
body[data-layout="landscape"] .modes, body[data-layout="landscape"] .top { display: none; }
body[data-layout="landscape"] .status { justify-content: center; margin: 0 0 6px; }
body[data-layout="landscape"] .play { flex-direction: row; justify-content: center; align-items: center; gap: 14px; }
body[data-layout="landscape"] .pad-side { display: grid; grid-template-columns: 1fr; width: 76px; gap: 6px; }
body[data-layout="landscape"] #pad-main { display: none; }
body[data-layout="landscape"] .side { width: auto; flex-direction: row-reverse; align-items: center; gap: 12px; }
body[data-layout="landscape"] .tools { grid-template-columns: 1fr; width: 64px; }
body[data-layout="landscape"] .num { min-height: 0; height: calc((var(--board) - 24px) / 5); font-size: 1.5rem; }

/* Tablet / desktop: 3x3 keypad beside the board (left-handed option swaps sides). */
body[data-layout="desktop"] .app { max-width: 980px; }
body[data-layout="desktop"] .play { flex-direction: row; justify-content: center; align-items: flex-start; gap: 28px; }
body[data-layout="desktop"].left-hand .play { flex-direction: row-reverse; }
body[data-layout="desktop"] .side { width: 300px; }
body[data-layout="desktop"] #pad-main { grid-template-columns: repeat(3, 1fr); gap: 8px; }
body[data-layout="desktop"] .num { min-height: 84px; font-size: 2.1rem; }
body[data-layout="desktop"] .num.erase { grid-column: span 3; min-height: 54px; flex-direction: row; gap: 8px; font-size: 1rem; }
body[data-layout="desktop"] .modes { justify-content: space-between; }
body[data-layout="desktop"] .segmented.levels { flex: 0 1 420px; }
body[data-layout="desktop"] .status { max-width: calc(var(--board) + 328px); margin-inline: auto; }
body[data-layout="desktop"] .info, body[data-layout="desktop"] .site-footer, body[data-layout="desktop"] .help { max-width: calc(var(--board) + 328px); margin-left: auto; margin-right: auto; }
