:root {
  --paper: #f4efe4;
  --paper-2: #fbf8f1;
  --ink: #1b1a17;
  --ink-2: #3b3830;
  --muted: #736d60;
  --line: #d6cdb9;
  --rule: #1b1a17;
  --entry: #2346b8;
  --sel: #ffd84a;
  --peer: #ebe3cf;
  --same: #f1dc93;
  --bad: #c4362b;
  --bad-bg: #f6d9d3;
  --good: #2f7d4f;
  --hint: #2346b8;
  --hint-bg: #dfe6fb;
  --shadow: #1b1a17;
  --grain-op: .32;
  --grain-blend: multiply;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #151410; --paper-2: #1d1c17; --ink: #efe8d6; --ink-2: #cfc8b4; --muted: #9a937f;
    --line: #3a372d; --rule: #efe8d6; --entry: #93b2ff; --sel: #7d6312; --peer: #27251e; --same: #4b3f19;
    --bad: #ff8070; --bad-bg: #4a2420; --good: #6fcf97; --hint: #93b2ff; --hint-bg: #22304f; --shadow: #000;
    --grain-op: .18; --grain-blend: soft-light; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #151410; --paper-2: #1d1c17; --ink: #efe8d6; --ink-2: #cfc8b4; --muted: #9a937f;
  --line: #3a372d; --rule: #efe8d6; --entry: #93b2ff; --sel: #7d6312; --peer: #27251e; --same: #4b3f19;
  --bad: #ff8070; --bad-bg: #4a2420; --good: #6fcf97; --hint: #93b2ff; --hint-bg: #22304f; --shadow: #000;
  --grain-op: .18; --grain-blend: soft-light; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 "Libre Franklin", "Franklin Gothic Medium", "Helvetica Neue", sans-serif;
  min-height: 100vh;
  padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
}
body::before {
  content: "";
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  opacity: var(--grain-op);
  mix-blend-mode: var(--grain-blend);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5 0 0 0 0 .45 0 0 0 0 .38 0 0 0 .55 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.page { position: relative; z-index: 1; max-width: 1020px; margin: 0 auto; padding: 18px max(16px, env(safe-area-inset-left)) 40px; }

/* ---------- masthead ---------- */
.masthead { animation: rise .55s cubic-bezier(.2,.7,.2,1) both; }
.mast-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.title {
  font-family: "Fraunces", Georgia, serif;
  font-weight: 800;
  font-variation-settings: "opsz" 144;
  font-size: clamp(44px, 8vw, 76px);
  letter-spacing: -.035em;
  line-height: .9;
  margin: 0;
}
.mast-actions { display: flex; gap: 4px; padding-bottom: 6px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: transparent;
  display: grid; place-items: center; transition: background .15s;
}
.icon-btn:hover { background: var(--peer); }
a.icon-btn { color: inherit; }
.mast-sep { width: 1px; height: 22px; background: var(--line); align-self: center; margin: 0 4px; }
.icon-btn.small { width: 34px; height: 34px; }
.dateline {
  margin-top: 12px; padding: 7px 0;
  border-top: 3px double var(--rule); border-bottom: 1px solid var(--rule);
  display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap;
  font-size: 12.5px; font-weight: 600; letter-spacing: .14em; text-transform: uppercase;
}
.today-link { border: 0; background: none; padding: 0; font-weight: 700; letter-spacing: .08em; color: var(--entry); text-transform: uppercase; font-size: 12px; }
.today-link:hover { text-decoration: underline; }

.tabs { display: flex; gap: 26px; margin-top: 14px; }
.tabs button {
  position: relative; border: 0; background: none; padding: 6px 0 9px;
  font-family: "Fraunces", Georgia, serif; font-size: 21px; font-weight: 600; color: var(--muted);
  font-variation-settings: "opsz" 48;
}
.tabs button::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: var(--ink);
  transform: scaleX(0); transform-origin: left; transition: transform .25s cubic-bezier(.2,.7,.2,1);
}
.tabs button.active { color: var(--ink); }
.tabs button.active::after { transform: scaleX(1); }
.tabs button:hover { color: var(--ink); }
.tab-mark { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-left: 6px; vertical-align: 4px; }
.tab-mark.started { border: 1.5px solid var(--muted); }
.tab-mark.solved { background: var(--good); }
.tabs.small { gap: 18px; margin: 4px 0 16px; }
.tabs.small button { font-size: 17px; }

/* ---------- layout ---------- */
.game {
  display: grid; grid-template-columns: minmax(0, 560px) 300px; gap: 44px;
  justify-content: center; align-items: start; margin-top: 22px;
}
.board-col { animation: rise .6s .08s cubic-bezier(.2,.7,.2,1) both; }
.controls { animation: rise .6s .16s cubic-bezier(.2,.7,.2,1) both; padding-top: 46px; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.board-bar { display: flex; align-items: center; justify-content: space-between; height: 40px; margin-bottom: 6px; }
.timer-wrap { display: flex; align-items: center; gap: 4px; }
.timer { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 17px; min-width: 52px; }
.timer.hidden-timer { visibility: hidden; }
.pause-btn { width: 34px; height: 34px; border-radius: 50%; border: 0; background: transparent; display: grid; place-items: center; }
.pause-btn:hover { background: var(--peer); }
.pause-btn svg { width: 18px; height: 18px; stroke-width: 2.2; }
.pause-btn .i-play { display: none; fill: currentColor; stroke-width: 1.2; }
.pause-btn.paused .i-pause { display: none; }
.pause-btn.paused .i-play { display: block; }
.bar-right { display: flex; align-items: center; gap: 8px; }
.status-chip { font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; padding: 3px 10px; border-radius: 99px; background: var(--ink); color: var(--paper); }
.status-chip.assisted { background: var(--muted); }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 40px; z-index: 20; min-width: 200px;
  background: var(--paper-2); border: 1.5px solid var(--ink); box-shadow: 5px 5px 0 var(--shadow);
  padding: 6px; animation: pop .14s ease-out both;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 9px 12px; font-weight: 500; }
.menu button:hover { background: var(--peer); }
.menu .danger { color: var(--bad); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 6px; }
@keyframes pop { from { opacity: 0; transform: translateY(-4px) scale(.98); } to { opacity: 1; transform: none; } }

/* ---------- board ---------- */
.board-frame { position: relative; }
.board {
  container-type: inline-size;
  position: relative;
  width: 100%; aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  gap: 2px; padding: 2.5px; background: var(--ink);
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.box { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 1px; background: var(--line); }
.cell {
  position: relative; background: var(--paper-2);
  display: grid; place-items: center; cursor: pointer; overflow: hidden;
  transition: background-color .12s;
}
.cell .val {
  font-size: 6.6cqw; line-height: 1; font-weight: 400; color: var(--entry);
  font-variant-numeric: tabular-nums;
}
.cell.given .val { color: var(--ink); font-weight: 500; }
.cell.revealed .val { color: var(--ink-2); }
.cell.revealed::after {
  content: ""; position: absolute; top: 0; right: 0;
  border-style: solid; border-width: 0 1.6cqw 1.6cqw 0; border-color: transparent var(--muted) transparent transparent;
}
.cell .notes {
  position: absolute; inset: 6%; display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(0, 1fr));
}
.cell .notes span {
  display: grid; place-items: center; min-width: 0; min-height: 0; font-size: 2.55cqw; line-height: 1; color: var(--muted); font-weight: 500;
  border-radius: 50%;
}
.cell .notes span.peek { color: var(--entry); opacity: .45; }
.cell .notes span.peek-on { color: var(--entry); box-shadow: inset 0 0 0 1.5px var(--entry); }
.cell.peekable { cursor: crosshair; }
.cell .notes span.match { background: var(--ink); color: var(--paper-2); font-weight: 700; }
.cell .notes span.elim { background: var(--bad); color: #fff; text-decoration: line-through; }
.cell .notes span.keep { background: var(--hint); color: var(--paper-2); }

.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.conflict .val { color: var(--bad); }
.cell.conflict-src { background-image: radial-gradient(circle at center, transparent 58%, color-mix(in srgb, var(--bad) 22%, transparent) 59%); }
.cell.wrong { background: var(--bad-bg); }
.cell.wrong .val { color: var(--bad); }
.cell.wrong.sel { background: color-mix(in srgb, var(--sel) 55%, var(--bad-bg)); }
.cell.hint-unit { background: var(--hint-bg); }
.cell.hint-focus { box-shadow: inset 0 0 0 3px var(--hint); }
.cell.hint-focus.sel { background: var(--sel); }
.cell.hint-target { box-shadow: inset 0 0 0 2px var(--bad); }

.cell.flash { animation: flash .7s ease-out both; animation-delay: calc(var(--k, 0) * 40ms); }
@keyframes flash {
  0% { background: var(--paper-2); }
  35% { background: var(--sel); }
  100% { }
}
.board.solved .cell { animation: wave .9s cubic-bezier(.3,.6,.3,1) both; animation-delay: calc(var(--d) * 45ms); }
@keyframes wave {
  0% { transform: scale(1); }
  40% { transform: scale(.82); background: var(--sel); }
  100% { transform: scale(1); }
}
.board.paused .cell .val, .board.paused .cell .notes { visibility: hidden; }

.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: color-mix(in srgb, var(--paper) 72%, transparent); backdrop-filter: blur(3px); z-index: 5; }
.overlay-card { text-align: center; }
.overlay-kicker { font-family: "Fraunces", Georgia, serif; font-size: 34px; font-weight: 600; margin: 0 0 14px; }
.overlay.loading p { margin: 14px 0 0; font-weight: 600; color: var(--muted); }
.spinner { width: 34px; height: 34px; margin: 0 auto; border: 3px solid var(--line); border-top-color: var(--ink); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- controls ---------- */
.mode-toggle { display: grid; grid-template-columns: 1fr 1fr; border: 1.5px solid var(--ink); border-radius: 99px; padding: 3px; }
.mode-toggle button { border: 0; background: none; border-radius: 99px; padding: 9px 0; font-weight: 600; transition: background .18s, color .18s; }
.mode-toggle button.active { background: var(--ink); color: var(--paper); }

.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 16px; }
.pad button {
  position: relative; aspect-ratio: 1; border: 1.5px solid var(--line); background: var(--paper-2);
  border-radius: 10px; font-family: "Fraunces", Georgia, serif; font-size: 36px; font-weight: 400;
  font-variation-settings: "opsz" 72;
  display: grid; padding: 0; transition: transform .08s, background .15s, border-color .15s;
}
.pad button .d { grid-area: 1/1; place-self: center; line-height: 1; }
.pad button .left { grid-area: 1/1; place-self: end center; font-family: "Libre Franklin", sans-serif; font-size: 11px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.pad button:hover { border-color: var(--ink); }
.pad button:active { transform: scale(.94); background: var(--peer); }
.pad button.done { opacity: .28; }
.pad.cand button .d { font-size: 15px; font-family: "Libre Franklin", sans-serif; font-weight: 600; }
.pad.cand button[data-n="1"] .d { place-self: start start; margin: 10px; }
.pad.cand button[data-n="2"] .d { place-self: start center; margin-top: 10px; }
.pad.cand button[data-n="3"] .d { place-self: start end; margin: 10px; }
.pad.cand button[data-n="4"] .d { place-self: center start; margin-left: 10px; }
.pad.cand button[data-n="6"] .d { place-self: center end; margin-right: 10px; }
.pad.cand button[data-n="7"] .d { place-self: end start; margin: 10px; }
.pad.cand button[data-n="8"] .d { place-self: end center; margin-bottom: 10px; }
.pad.cand button[data-n="9"] .d { place-self: end end; margin: 10px; }
.pad.cand button .left { display: none; }

.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 14px; }
.act { border: 0; background: none; border-radius: 10px; padding: 8px 0 6px; display: grid; justify-items: center; gap: 3px; font-size: 12px; font-weight: 600; }
.act:hover { background: var(--peer); }
.act:disabled { opacity: .35; cursor: default; background: none; }

.auto-row { display: flex; align-items: center; gap: 10px; margin-top: 14px; font-weight: 600; cursor: pointer; justify-content: center; }
.switch { position: relative; width: 40px; height: 23px; flex: none; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch i { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: background .2s; pointer-events: none; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 17px; height: 17px; border-radius: 50%; background: var(--paper-2); transition: transform .2s cubic-bezier(.3,.7,.3,1); box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.switch input:checked + i { background: var(--ink); }
.switch input:checked + i::after { transform: translateX(17px); }
.switch input:focus-visible + i { outline: 2px solid var(--entry); outline-offset: 2px; }

.hint-panel {
  margin-top: 16px; padding: 14px 16px 12px; background: var(--paper-2);
  border: 1.5px solid var(--ink); box-shadow: 5px 5px 0 var(--shadow);
  animation: pop .18s ease-out both;
}
.hint-kicker { margin: 0 0 4px; font-size: 11.5px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; color: var(--hint); }
.hint-text { margin: 0; font-family: "Fraunces", Georgia, serif; font-size: 16.5px; line-height: 1.4; font-variation-settings: "opsz" 24; }
.hint-actions { display: flex; gap: 8px; margin-top: 12px; }

.kbd-help { margin-top: 22px; font-size: 12px; color: var(--muted); line-height: 1.9; }
kbd { font: 600 11px/1 "Libre Franklin", sans-serif; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 2px 5px; background: var(--paper-2); }

.primary, .ghost {
  border-radius: 99px; padding: 11px 22px; font-weight: 700; border: 1.5px solid var(--ink);
  transition: transform .08s;
}
.primary { background: var(--ink); color: var(--paper); }
.ghost { background: transparent; }
.primary:active, .ghost:active { transform: scale(.96); }
.primary.small, .ghost.small { padding: 7px 15px; font-size: 13px; }
.primary.danger { background: var(--bad); border-color: var(--bad); color: #fff; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(26px + env(safe-area-inset-bottom, 0)); transform: translateX(-50%);
  background: var(--ink); color: var(--paper); padding: 11px 20px; border-radius: 99px; font-weight: 600;
  z-index: 50; max-width: calc(100vw - 32px); text-align: center; animation: toast .25s ease-out both;
}
@keyframes toast { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- modals ---------- */
.modal {
  border: 1.5px solid var(--ink); background: var(--paper-2); color: var(--ink);
  box-shadow: 8px 8px 0 var(--shadow); padding: 26px 28px 24px; width: min(440px, calc(100vw - 32px));
  max-height: calc(100vh - 40px); overflow: auto;
}
.modal[open] { animation: pop .2s ease-out both; }
.modal::backdrop { background: color-mix(in srgb, var(--paper) 55%, transparent); backdrop-filter: blur(2px); }
.modal-kicker { margin: 0 0 6px; font-size: 12px; font-weight: 700; letter-spacing: .16em; text-transform: uppercase; color: var(--muted); }
.modal-x { position: absolute; top: 10px; right: 12px; border: 0; background: none; font-size: 28px; line-height: 1; width: 36px; height: 36px; border-radius: 50%; }
.modal-x:hover { background: var(--peer); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 22px; }
.fine { font-size: 12px; color: var(--muted); margin: 16px 0 0; }

.win-title { font-family: "Fraunces", Georgia, serif; font-size: 46px; line-height: 1; margin: 2px 0 8px; font-weight: 800; letter-spacing: -.03em; }
.win-sub { margin: 0; color: var(--ink-2); }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 20px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 14px 0; }
.win-stats div { text-align: center; }
.win-stats b, .stats-grid b { display: block; font-family: "Fraunces", Georgia, serif; font-size: 30px; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; }
.win-stats span, .stats-grid span { font-size: 11.5px; font-weight: 600; color: var(--muted); letter-spacing: .04em; }
.win-burst { position: absolute; top: -40px; right: -40px; width: 160px; height: 160px; border-radius: 50%; background: var(--sel); opacity: .55; pointer-events: none; animation: burst .7s cubic-bezier(.2,.8,.2,1) both; }
.modal#dlg-win { overflow: hidden; }
.modal#dlg-win > *:not(.win-burst):not(.modal-x) { position: relative; }
@keyframes burst { from { transform: scale(0); } to { transform: scale(1); } }

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px 8px; text-align: center; }

.cal-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 10px; }
.cal-head h3 { margin: 0; font-family: "Fraunces", Georgia, serif; font-size: 22px; font-weight: 600; text-transform: capitalize; }
.cal-head button:disabled { opacity: .25; cursor: default; background: none; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal .dow { text-align: center; font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .06em; padding-bottom: 4px; }
.cal .day {
  border: 1px solid var(--line); background: var(--paper); border-radius: 8px; aspect-ratio: 1;
  display: grid; grid-template-rows: 1fr auto; justify-items: center; padding: 4px 0 6px; font-weight: 600; font-size: 14px;
}
.cal .day:hover:not(:disabled) { border-color: var(--ink); }
.cal .day:disabled { opacity: .3; cursor: default; }
.cal .day.today { border: 2px solid var(--ink); }
.cal .day.current { background: var(--sel); border-color: var(--ink); }
.cal .dots { display: flex; gap: 3px; }
.dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; border: 1px solid var(--line); vertical-align: 0; }
.dot.started { border-color: var(--muted); background: transparent; border-width: 1.5px; }
.dot.solved { background: var(--good); border-color: var(--good); }
.cal-legend .dot { margin: 0 2px 0 6px; }

.settings { display: grid; gap: 2px; margin-top: 8px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 11px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.setting:last-child { border-bottom: 0; }
.setting b { display: block; font-weight: 600; }
.setting small { display: block; color: var(--muted); font-size: 12.5px; }
.setting select { font: inherit; padding: 5px 8px; border: 1.5px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); }
.confirm-text { font-family: "Fraunces", Georgia, serif; font-size: 19px; margin: 4px 0 0; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .game { grid-template-columns: minmax(0, 560px); gap: 14px; margin-top: 14px; }
  .controls { padding-top: 0; width: 100%; max-width: 560px; justify-self: center; }
  .pad { grid-template-columns: repeat(9, 1fr); gap: 5px; margin-top: 12px; }
  .pad button { aspect-ratio: auto; height: 58px; font-size: 28px; border-radius: 8px; }
  .pad button .left { margin-bottom: 3px; font-size: 10px; }
  .pad.cand button .d { margin: 0 !important; place-self: center !important; font-size: 17px; }
  .actions { margin-top: 8px; }
  .kbd-help { display: none; }
  .tabs { gap: 22px; margin-top: 10px; }
  .tabs button { font-size: 19px; }
  .controls { display: flex; flex-direction: column; }
  .mode-toggle { order: 0; }
  .hint-panel { order: -1; margin: 0 0 12px; }
  .board-bar { height: 34px; }
}
@media (max-width: 420px) {
  .page { padding-top: 10px; }
  .pad button { height: 52px; font-size: 25px; }
  .mast-actions .icon-btn { width: 38px; height: 38px; }
  .dateline { font-size: 11px; letter-spacing: .1em; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-delay: 0ms !important; transition: none !important; }
}
