:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --text: #1b1f2a;
  --muted: #6b7280;
  --line: #c9cdd6;
  --line-strong: #2a2f3a;
  --accent: #3563e9;
  --accent-text: #ffffff;
  --accent-soft: #e3eafc;
  --cell-peer: #e9eef9;
  --cell-same: #c8d6f8;
  --cell-selected: #a8c0f6;
  --user-digit: #2f5bd8;
  --error: #d93a3f;
  --error-bg: #fbe3e4;
  --success: #1f9d55;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 16px rgb(16 24 40 / 6%);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b22;
    --surface-2: #232733;
    --text: #e8eaf0;
    --muted: #8b93a3;
    --line: #343a48;
    --line-strong: #8a93a8;
    --accent: #6d94ff;
    --accent-text: #0b1020;
    --accent-soft: #1f2a44;
    --cell-peer: #1d2230;
    --cell-same: #2b3b62;
    --cell-selected: #3a5394;
    --user-digit: #8eaeff;
    --error: #ff6b70;
    --error-bg: #3c1d22;
    --success: #4ccb84;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #232733;
  --text: #e8eaf0;
  --muted: #8b93a3;
  --line: #343a48;
  --line-strong: #8a93a8;
  --accent: #6d94ff;
  --accent-text: #0b1020;
  --accent-soft: #1f2a44;
  --cell-peer: #1d2230;
  --cell-same: #2b3b62;
  --cell-selected: #3a5394;
  --user-digit: #8eaeff;
  --error: #ff6b70;
  --error-bg: #3c1d22;
  --success: #4ccb84;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body { -webkit-user-select: none; user-select: none; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 0.875rem; }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 20px;
  border-radius: 12px;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 1rem;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.small { min-height: 38px; padding: 0 14px; font-size: 0.9rem; }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.eyebrow {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
.btn-stack { display: flex; flex-direction: column; gap: 10px; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Home ---------- */

#home {
  max-width: 520px;
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top) + 12px)
    calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 24px)
    calc(env(safe-area-inset-left) + 16px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.home-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-right: -10px;
}
.home-header h1 { margin: 8px 0; font-size: 2rem; letter-spacing: -0.02em; }

.install-card { display: flex; flex-direction: column; gap: 12px; padding: 16px; }
.install-card p { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }
.install-actions { display: flex; gap: 8px; }

.daily-card { display: flex; flex-direction: column; gap: 6px; }
.daily-card h2 { margin: 2px 0 0; font-size: 1.5rem; letter-spacing: -0.01em; }
.daily-card p { margin: 0 0 10px; }
.daily-top { display: flex; justify-content: space-between; align-items: center; }
.streak {
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 999px;
}

.continue-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 16px 16px 20px; }
.continue-meta { font-weight: 600; margin-top: 4px; }

.section-title { margin: 8px 0 -4px; font-size: 1rem; color: var(--muted); font-weight: 600; }

.difficulty-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.difficulty {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-height: 84px;
  padding: 16px 18px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform 0.08s ease;
}
.difficulty:active { transform: scale(0.97); }
.difficulty span { font-weight: 700; font-size: 1.1rem; }
.difficulty small { color: var(--muted); font-size: 0.85rem; }
.difficulty::before {
  content: "";
  display: block;
  width: 28px;
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
  margin-bottom: 6px;
}
.difficulty[data-difficulty="easy"]::before { background: #33b06a; }
.difficulty[data-difficulty="medium"]::before { background: #e3a325; }
.difficulty[data-difficulty="hard"]::before { background: #e5673a; }
.difficulty[data-difficulty="expert"]::before { background: #b5459b; }

/* ---------- Game ---------- */

#game {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    calc(env(safe-area-inset-bottom) + 8px)
    env(safe-area-inset-left);
  overflow: hidden;
}

.game-header {
  display: grid;
  grid-template-columns: 88px 1fr 88px;
  align-items: center;
  padding: 6px 8px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}
.game-title { display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
.game-title span { font-weight: 700; }
.game-title small { color: var(--muted); font-size: 0.8rem; min-height: 1em; }
.timer-btn {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 40px;
  padding: 0 10px 0 12px;
  border-radius: 999px;
  background: var(--surface-2);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.timer-btn svg { width: 18px; height: 18px; }

.play-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

.board-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 4px 8px;
  container-type: size;
}

.board {
  --size: min(calc(100vw - 16px), 62vh);
  --size: min(100cqw, 100cqh);
  width: var(--size);
  height: var(--size);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--surface);
  border: 2px solid var(--line-strong);
  border-radius: 10px;
  overflow: hidden;
  font-size: calc(var(--size) / 15);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: background-color 0.12s ease;
}
.cell.c8 { border-right: 0; }
.cell.r8 { border-bottom: 0; }
.cell.box-r { border-right: 2px solid var(--line-strong); }
.cell.box-b { border-bottom: 2px solid var(--line-strong); }

.cell.peer { background: var(--cell-peer); }
.cell.same { background: var(--cell-same); }
.cell.selected { background: var(--cell-selected); }
.cell.given { color: var(--text); font-weight: 600; }
.cell.user { color: var(--user-digit); }
.cell.error { color: var(--error); }
.cell.error:not(.selected) { background: var(--error-bg); }
.cell.conflict-note { color: var(--error); }

.cell .notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: 0.36em;
  font-weight: 500;
  color: var(--muted);
  line-height: 1;
}
.cell .notes span { display: grid; place-items: center; }
.cell .notes span.match { color: var(--accent); font-weight: 800; }

.cell.pop .value { animation: pop 0.18s ease-out; }
@keyframes pop { 0% { transform: scale(0.6); } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }

.board.solved .cell { animation: wave 0.6s ease both; animation-delay: var(--d); }
@keyframes wave {
  0% { background: var(--surface); }
  40% { background: var(--cell-selected); transform: scale(0.92); }
  100% { background: var(--surface); transform: scale(1); }
}

.pause-overlay, .loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  color: var(--muted);
  font-weight: 600;
}

.spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid var(--surface-2);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.controls { padding: 6px 8px 0; display: flex; flex-direction: column; gap: 8px; }

.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tool {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 0 6px;
  border-radius: 12px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}
.tool:active { background: var(--surface-2); }
.tool svg { width: 26px; height: 26px; color: var(--text); }
.tool:disabled { opacity: 0.4; }
.tool .badge {
  position: absolute;
  top: 2px;
  left: calc(50% + 8px);
  font-style: normal;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}
.tool[aria-pressed="true"] { color: var(--accent); }
.tool[aria-pressed="true"] svg { color: var(--accent); }
.tool[aria-pressed="true"] .badge { background: var(--accent); color: var(--accent-text); }

.numpad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
.num {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: clamp(54px, 8.5vh, 72px);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--accent);
  font-size: clamp(1.6rem, 7.5vw, 2.1rem);
  font-weight: 500;
  line-height: 1;
  transition: transform 0.06s ease, background-color 0.1s ease;
}
.num:active { transform: scale(0.92); background: var(--accent-soft); }
.num small { font-size: 0.68rem; color: var(--muted); font-weight: 600; margin-top: 3px; }
.num.done { visibility: hidden; }
.numpad.notes-mode .num { color: var(--muted); font-size: clamp(1.2rem, 5.5vw, 1.6rem); }

/* Landscape phones: board on the left, controls on the right. */
@media (orientation: landscape) and (max-height: 600px) {
  .play-area { flex-direction: row; max-width: none; padding: 0 8px; gap: 12px; }
  .board-wrap { flex: 1.2; }
  .controls { flex: 1; justify-content: center; max-width: 360px; padding: 0; }
  .numpad { grid-template-columns: repeat(3, 1fr); }
  .num { height: clamp(44px, 14vh, 64px); font-size: 1.6rem; }
  .game-header { max-width: none; padding: 2px 8px; }
}

/* ---------- Dialogs ---------- */

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  max-width: 100vw;
  max-height: 100dvh;
  width: 100%;
  margin: auto auto 0;
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog[open] .sheet { animation: rise 0.22s cubic-bezier(0.2, 0.9, 0.3, 1); }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }

.sheet {
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  overflow-y: auto;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 22px 20px calc(env(safe-area-inset-bottom) + 20px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-user-select: text;
  user-select: text;
}
.sheet h2 { margin: 0; font-size: 1.4rem; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; margin: -6px -10px -6px 0; }

@media (min-width: 640px) {
  dialog { margin: auto; }
  .sheet { border-radius: 22px; padding-bottom: 22px; }
}

#win-dialog h2 { text-align: center; font-size: 1.8rem; }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.win-stats > div {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 4px;
  border-radius: 12px;
  background: var(--surface-2);
}
.stat-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 0.75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
#win-note:empty { display: none; }
#win-note { margin: -4px 0 0; }

.submit-form { display: flex; flex-direction: column; gap: 8px; }
.submit-form label { font-weight: 600; font-size: 0.95rem; }
.input-row { display: flex; gap: 8px; }
.name-setting { display: flex; flex-direction: column; gap: 8px; font-weight: 500; }
#win-post:empty { display: none; }
#win-post { margin: -4px 0 0; }

.input-row input, .name-setting input {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
}

.date-nav { display: flex; align-items: center; justify-content: space-between; font-weight: 600; }
.date-nav .icon-btn:disabled { opacity: 0.3; }
#lb-mine:empty, #lb-status:empty { display: none; }
#lb-mine, #lb-status { margin: 0; }

.leaderboard { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; min-height: 120px; }
.leaderboard li {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
}
.leaderboard li.me { background: var(--accent-soft); outline: 2px solid var(--accent); outline-offset: -2px; }
.leaderboard .rank { font-weight: 700; color: var(--muted); text-align: center; }
.leaderboard li:nth-child(1) .rank { color: #d4a017; }
.leaderboard li:nth-child(2) .rank { color: #9aa3b2; }
.leaderboard li:nth-child(3) .rank { color: #c27c46; }
.leaderboard .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard .time { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.leaderboard .time small { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 500; }

.toggle, .setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  font-weight: 500;
  cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i {
  position: relative;
  flex: none;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--line);
  transition: background-color 0.2s ease;
}
.toggle i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
  transition: transform 0.2s ease;
}
.toggle input:checked + i { background: var(--accent); }
.toggle input:checked + i::after { transform: translateX(20px); }
.toggle input:focus-visible + i { outline: 3px solid var(--accent); outline-offset: 2px; }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.segmented button { padding: 7px 12px; border-radius: 8px; font-size: 0.9rem; font-weight: 600; color: var(--muted); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

#confirm-dialog p { margin: 4px 0 6px; font-size: 1.05rem; line-height: 1.4; }

/* ---------- Toast ---------- */

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

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