/* ---------- Theme ---------- */

:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #eceff6;
  --text: #1d2433;
  --muted: #6b7385;
  --line: #d3d8e3;
  --line-strong: #344057;
  --accent: #3b6cf6;
  --accent-text: #ffffff;
  --accent-soft: #e2e9ff;
  --cell-peer: #eef1f8;
  --cell-same: #c9d7ff;
  --cell-selected: #b3c7ff;
  --digit-given: #1d2433;
  --digit-entered: #2d5be3;
  --wrong: #e5484d;
  --wrong-bg: #fde8e8;
  --success: #2f9e6a;
  --shadow: 0 1px 2px rgb(20 30 60 / 6%), 0 4px 16px rgb(20 30 60 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218;
    --surface: #1a1f2a;
    --surface-2: #232a38;
    --text: #e6e9f0;
    --muted: #8b93a7;
    --line: #2d3445;
    --line-strong: #6b7590;
    --accent: #5b85ff;
    --accent-text: #ffffff;
    --accent-soft: #24304f;
    --cell-peer: #212838;
    --cell-same: #2f3f6e;
    --cell-selected: #3d5296;
    --digit-given: #e6e9f0;
    --digit-entered: #8fb0ff;
    --wrong: #ff6b6f;
    --wrong-bg: #3a2027;
    --success: #4cc38a;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

/* ---------- Base ---------- */

* { box-sizing: border-box; }

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { cursor: default; }

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

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

[hidden] { display: none !important; }

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

.screen {
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
}

/* ---------- Buttons ---------- */

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 10px 18px;
  border-radius: 12px;
  font-weight: 600;
  transition: transform 80ms ease, background-color 120ms ease;
}

.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.secondary { background: var(--surface); box-shadow: var(--shadow); }
.btn.ghost { color: var(--muted); }
.btn.danger { color: var(--wrong); }
.btn.primary.danger { background: var(--wrong); color: #fff; }
.btn.small { min-height: 40px; padding: 8px 14px; font-size: 14px; }

.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
}

.icon-btn:active { background: var(--surface-2); }

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

.home-header {
  text-align: center;
  padding: 24px 0 20px;
}

.home-header h1 {
  margin: 0;
  font-size: 40px;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 4px 0 0;
  color: var(--muted);
}

.section-title {
  margin: 20px 4px 10px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.continue-card {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-text);
  text-align: left;
  box-shadow: var(--shadow);
}

.continue-card b { display: block; font-size: 18px; }
.continue-card small { opacity: 0.85; font-size: 14px; }
.continue-card:active { transform: scale(0.99); }

.difficulty-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.diff {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--surface);
  text-align: left;
  box-shadow: var(--shadow);
}

.diff:active:not(:disabled) { transform: scale(0.99); }
.diff-name { display: block; font-weight: 600; font-size: 17px; }
.diff-meta { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.diff-side { color: var(--muted); display: flex; align-items: center; gap: 4px; }
.diff.locked { opacity: 0.6; box-shadow: none; background: var(--surface-2); }

.pips { display: flex; gap: 4px; margin-top: 6px; }
.pips i { width: 18px; height: 4px; border-radius: 2px; background: var(--line); }
.pips i.on { background: var(--accent); }

.home-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 20px;
}

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

#game {
  /* Board fills the width on phones but leaves room for the controls on short screens. */
  --board: max(240px, min(calc(100vw - 24px), calc(100vh - 300px), 520px));
}

@supports (height: 100dvh) {
  #game { --board: max(240px, min(calc(100vw - 24px), calc(100dvh - 300px), 520px)); }
}

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

.topbar-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 18px;
}

.badge {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

.statusbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 2px 8px;
  color: var(--muted);
  font-size: 14px;
}

.statusbar b { color: var(--text); font-variant-numeric: tabular-nums; }
#mistakes-wrap.danger b { color: var(--wrong); }
.timer { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--text); }

/* Fills the space under the top bar and centres the board + controls vertically. */
.game-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding-bottom: 12px;
}

.board-wrap { width: var(--board); }

/* Board: 3x3 boxes with thick gaps, each a 3x3 grid of cells with thin gaps. */
.board {
  width: var(--board);
  height: var(--board);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
  padding: 2px;
  background: var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
  transition: filter 200ms ease;
}

.board.blurred { filter: blur(10px); }

.box {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 1px;
  background: var(--line);
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}

.cell.peer { background: var(--cell-peer); }
.cell.same { background: var(--cell-same); }
.cell.selected { background: var(--cell-selected); }
.cell.wrong { background: var(--wrong-bg); }
.cell.wrong.selected { background: var(--cell-selected); }

.value {
  font-size: calc(var(--board) * 0.062);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.cell.given .value { color: var(--digit-given); font-weight: 600; }
.cell.entered .value { color: var(--digit-entered); font-weight: 500; }
.cell.wrong .value { color: var(--wrong); font-weight: 500; }

.notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.notes span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--board) * 0.026);
  line-height: 1;
  color: var(--muted);
  font-weight: 500;
}

.notes span.hl {
  color: var(--accent-text);
  background: var(--accent);
  border-radius: 3px;
}

/* ---------- Controls ---------- */

.controls {
  width: var(--board);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tools {
  display: flex;
  justify-content: space-around;
}

.tool {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 64px;
  padding: 6px 10px;
  border-radius: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.tool:active { background: var(--surface-2); }
.tool svg { width: 26px; height: 26px; }

.pill {
  position: absolute;
  top: 0;
  right: 2px;
  font-style: normal;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--line);
  color: var(--muted);
}

.tool.active { color: var(--accent); }
.tool.active .pill { background: var(--accent); color: var(--accent-text); }

.pad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 4px;
}

.pad-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px 0 4px;
  min-height: 56px;
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--digit-entered);
  transition: transform 60ms ease;
}

.pad-btn:active:not(:disabled) { transform: scale(0.92); background: var(--accent-soft); }
.pad-digit { font-size: clamp(22px, calc(var(--board) * 0.075), 34px); line-height: 1.1; font-weight: 500; }
.pad-count { font-size: 11px; color: var(--muted); }
.pad-btn:disabled { opacity: 0.25; box-shadow: none; }
.pad.notes-mode .pad-btn { color: var(--muted); }
.pad.notes-mode .pad-digit { font-size: clamp(16px, calc(var(--board) * 0.05), 24px); }

/* Phones held sideways: board on the left, controls on the right. */
@media (orientation: landscape) and (max-height: 560px) {
  #game.screen { max-width: 900px; }
  #game { --board: max(200px, min(calc(100vh - 110px), 58vw)); }
  @supports (height: 100dvh) {
    #game { --board: max(200px, min(calc(100dvh - 110px), 58vw)); }
  }
  .game-body { flex-direction: row; justify-content: center; align-items: center; gap: 24px; }
  .controls { width: min(300px, 34vw); }
  .pad { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .pad-btn { min-height: 0; aspect-ratio: 1.6; }
  .game-body { padding-bottom: 0; }
  .statusbar { margin-bottom: 4px; }
}

/* ---------- Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgb(10 14 24 / 45%);
  animation: fade-in 150ms ease;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  padding: 24px 20px 16px;
  border-radius: 20px;
  background: var(--surface);
  box-shadow: 0 12px 40px rgb(0 0 0 / 25%);
  animation: pop-in 180ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

.modal-card h2 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 24px;
}

.modal-body { color: var(--muted); text-align: center; }
.modal-body p { margin: 8px 0; }

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 18px;
}

.modal-actions .btn.secondary { box-shadow: none; background: var(--surface-2); }

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0;
}

.result-grid div {
  padding: 10px 4px;
  border-radius: 12px;
  background: var(--surface-2);
}

.result-grid small { display: block; font-size: 12px; }
.result-grid b { display: block; color: var(--text); font-size: 20px; font-variant-numeric: tabular-nums; }

.highlight { color: var(--success); font-weight: 600; }

.code-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.code-field {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: 600 16px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
  text-align: center;
  -webkit-user-select: text;
  user-select: text;
}

.code-field.editable { width: 100%; text-transform: uppercase; margin-top: 8px; }
.code-field::placeholder { color: var(--muted); opacity: 0.6; text-transform: none; letter-spacing: 0; }

.error { color: var(--wrong); min-height: 1.4em; font-size: 14px; }

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  text-align: left;
  cursor: pointer;
}

.setting b { display: block; color: var(--text); font-weight: 500; }
.setting small { display: block; font-size: 13px; margin-top: 2px; }

.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  position: relative;
  width: 46px;
  height: 28px;
  margin: 0;
  border-radius: 999px;
  background: var(--line);
  cursor: pointer;
  transition: background-color 150ms ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
  transition: transform 150ms ease;
}

.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }

.spinner {
  width: 36px;
  height: 36px;
  margin: 16px auto 8px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: scale(0.94); } }

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