/* ===========================================================================
   Sudoku — "ink & graphite" design system.
   Givens are printed in ink; your entries are blue ink; pencil marks are
   graphite. Light = paper, dark = slate. Themes switch via [data-theme].
   =========================================================================== */

/* Self-hosted fonts (latin) — no external CDN, works fully offline. */
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 400; font-display: swap; src: url('/fonts/inter-400.woff2') format('woff2'); }
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 500; font-display: swap; src: url('/fonts/inter-500.woff2') format('woff2'); }
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 600; font-display: swap; src: url('/fonts/inter-600.woff2') format('woff2'); }
@font-face { font-family: 'Inter'; font-style: normal; font-weight: 700; font-display: swap; src: url('/fonts/inter-700.woff2') format('woff2'); }
@font-face { font-family: 'Sora'; font-style: normal; font-weight: 500; font-display: swap; src: url('/fonts/sora-500.woff2') format('woff2'); }
@font-face { font-family: 'Sora'; font-style: normal; font-weight: 600; font-display: swap; src: url('/fonts/sora-600.woff2') format('woff2'); }
@font-face { font-family: 'Sora'; font-style: normal; font-weight: 700; font-display: swap; src: url('/fonts/sora-700.woff2') format('woff2'); }
@font-face { font-family: 'Sora'; font-style: normal; font-weight: 800; font-display: swap; src: url('/fonts/sora-800.woff2') format('woff2'); }

:root {
  --font-ui: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --font-display: 'Sora', 'Inter', system-ui, sans-serif;
  --radius: 14px;
  --radius-sm: 10px;
  --cell-size: 44px; /* set precisely by JS fitBoard() */
}

[data-theme='light'] {
  --page: #e7e0d2;
  --panel: #fbf8f1;
  --panel-2: #efe9dc;
  --board: #ffffff;
  --cell-alt: #f1eee6;
  --line: #bcc2d0;       /* clearly visible thin grid lines */
  --rule: #2f3a52;       /* dark printed box rules */
  --ink: #14223a;        /* givens: dark, bold */
  --entry: #1f5fe0;      /* your entries: vivid blue */
  --pencil: #6b7488;
  --accent: #2f5bd0;
  --accent-press: #2848a8;
  --on-accent: #ffffff;
  --danger: #d23a2a;
  --danger-bg: #ffd7cf;
  --select: #b3d7f2;     /* selected: soft, clean blue fill */
  --peer: #edf0f7;       /* peer wash: subtle */
  --same: #cfe0ff;       /* same number: medium */
  --text: #1f2a3d;
  --muted: #6b7488;
  --border: #e4ddcd;
  --shadow: 0 18px 50px rgba(60, 50, 30, 0.18);
  --veil: rgba(247, 244, 236, 0.84);
  --hint: #ffe39a;
  --good: #2a9d63;
}

[data-theme='dark'] {
  --page: #0c111c;
  --panel: #19212f;
  --panel-2: #222c3f;
  --board: #222d42;      /* lighter card so it stands out from the page */
  --cell-alt: #283449;
  --line: #4a597a;       /* bright, clearly visible thin grid lines */
  --rule: #7a8cb0;       /* strong box rules */
  --ink: #f7f9fd;        /* givens: bright, bold */
  --entry: #7fb4ff;      /* your entries: blue */
  --pencil: #9aa6bd;
  --accent: #5b8def;
  --accent-press: #4778d8;
  --on-accent: #ffffff;
  --danger: #ff6f60;
  --danger-bg: #532a34;
  --select: #45689c;     /* selected: soft, clean blue fill (a step above 'same') */
  --peer: #31415f;       /* row/column wash: visible */
  --same: #3c5f93;       /* same number: medium */
  --text: #e6ebf5;
  --muted: #9aa3b8;
  --border: #2c3853;
  --shadow: 0 20px 55px rgba(0, 0, 0, 0.5);
  --veil: rgba(12, 17, 28, 0.82);
  --hint: #534a28;
  --good: #3fbf7f;
}

* { box-sizing: border-box; }

/* Disable mobile double-tap-to-zoom on tappable elements (pinch-zoom still works). */
button { touch-action: manipulation; }

html, body { height: 100%; margin: 0; }

body {
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--page);
  display: flex;
  justify-content: center;
  height: 100dvh;
  overflow: hidden;
  padding:
    max(clamp(8px, 1.4vh, 18px), env(safe-area-inset-top))
    max(clamp(12px, 3vw, 26px), env(safe-area-inset-right))
    max(clamp(8px, 1.4vh, 18px), env(safe-area-inset-bottom))
    max(clamp(12px, 3vw, 26px), env(safe-area-inset-left));
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.25s, color 0.25s;
}

.app {
  width: min(96vw, 1060px);
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ---------- screens ---------- */
.screen { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: clamp(8px, 1.6vh, 16px); }
.screen[hidden] { display: none; }

/* ---------- home ---------- */
#screen-home { position: relative; }
.home-theme { position: absolute; top: 4px; right: 2px; }
.home {
  flex: 1; min-height: 0;
  width: 100%; max-width: 380px; margin: 0 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(22px, 5vh, 42px);
  padding: 16px 4px;
}
.home-hero { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.home-mark { width: 60px; height: 60px; color: var(--accent); stroke-width: 1.6; }
.home-title { margin: 8px 0 0; font-family: var(--font-display); font-weight: 800; font-size: clamp(2.2rem, 8vw, 3rem); letter-spacing: 0.02em; }
.home-tagline { margin: 0; color: var(--muted); font-size: 0.95rem; }
.home-actions { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.home-foot { display: flex; gap: 8px; width: 100%; }
.home-link {
  flex: 1; position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 14px 4px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); color: var(--text); font: inherit; font-size: 0.78rem; font-weight: 600; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.home-link:hover { background: var(--panel-2); border-color: var(--accent); }
.home-link svg { width: 20px; height: 20px; }

.soon-badge { font-size: 0.6rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; background: var(--panel-2); color: var(--muted); padding: 2px 7px; border-radius: 999px; }
.btn.soon { display: inline-flex; align-items: center; justify-content: center; gap: 8px; opacity: 0.8; }
.home-link.soon .soon-badge { position: absolute; top: 5px; right: 5px; }

.btn.lg { padding: 16px; font-size: 1.1rem; border-radius: 14px; }

/* ---------- game top bar ---------- */
.game-topbar { display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }
.game-topbar .timer-block { margin-right: auto; }
.game-topbar .timer { font-size: clamp(1.3rem, 2.6vh, 1.7rem); }

/* ---------- difficulty chooser ---------- */
.difficulty-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.difficulty-choice {
  display: flex; flex-direction: column; gap: 4px; align-items: flex-start;
  padding: 16px; border: 1px solid var(--border); border-radius: 14px;
  background: var(--panel-2); color: var(--text); font: inherit; cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.difficulty-choice:hover { border-color: var(--accent); background: var(--panel); }
.difficulty-choice:active { transform: translateY(1px); }
.difficulty-choice b { font-family: var(--font-display); font-size: 1.15rem; }
.dc-best { color: var(--muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
.difficulty-choice.master { grid-column: 1 / -1; border-color: var(--accent); }
.difficulty-choice.master b { color: var(--accent); }

/* ---------- top bar (legacy/unused, kept harmless) ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; flex: 0 0 auto; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 28px; height: 28px; color: var(--accent); }
.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.25rem, 2.6vh, 1.7rem);
  letter-spacing: 0.04em;
}
.topbar-actions { display: flex; gap: 8px; }

.icon-btn {
  width: 42px; height: 42px;
  display: inline-grid; place-items: center;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.icon-btn.sm { width: 34px; height: 34px; }
.icon-btn:hover { background: var(--panel-2); border-color: var(--accent); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.sm svg { width: 16px; height: 16px; }

/* default icon stroke style; filled icons set fill/stroke inline */
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.brand-mark { stroke-width: 1.8; }

[data-theme='light'] .i-sun { display: none; }
[data-theme='dark'] .i-moon { display: none; }

/* ---------- play area ---------- */
.play {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center; /* center the board + controls as one group */
  gap: clamp(10px, 2vh, 18px);
}

.board-wrap {
  position: relative;
  flex: 0 0 auto; /* shrink to the board so extra height stays as margins, not a gap */
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rail {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.4vh, 14px);
}

@media (min-width: 880px) {
  .play { flex-direction: row; align-items: stretch; justify-content: flex-start; }
  .play::before { content: ''; flex: 0 0 260px; } /* balances the rail so the board sits centered */
  .board-wrap { flex: 1 1 auto; }
  .rail { flex: 0 0 260px; width: 260px; overflow-y: auto; justify-content: center; }
}

/* ---------- board ---------- */
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr); /* equal rows — without this, empty rows squish */
  aspect-ratio: 1 / 1;
  width: min(100%, 70vh); /* fallback; exact size set by JS */
  max-width: 100%;
  max-height: 100%;
  background: var(--board);
  border: 2.5px solid var(--rule);
  border-radius: 3px;
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
}

.cell {
  position: relative;
  border: 1px solid var(--line);
  background: var(--board);
  font-family: var(--font-display);
  color: var(--entry);
  font-size: calc(var(--cell-size) * 0.5);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: background 0.12s;
}
.cell.box-left { border-left: 2.5px solid var(--rule); }
.cell.box-top { border-top: 2.5px solid var(--rule); }
.cell.given { color: var(--ink); font-weight: 800; }
.cell .value { line-height: 1; pointer-events: none; }

.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--select); }
.cell.conflict { color: var(--danger); }
.cell.wrong { color: var(--danger); background: var(--danger-bg); }
.cell.hint { animation: hintpulse 1.2s ease; }
.cell.unit-done { animation: unitpulse 0.7s ease; }
.cell.mistake-flash { animation: mistakeShake 0.4s ease, mistakePulse 0.55s ease; }
.cell:focus-visible { outline: none; box-shadow: inset 0 0 0 2px var(--accent); }

@keyframes hintpulse { 0%, 100% { background: var(--board); } 35% { background: var(--hint); } }
@keyframes unitpulse {
  0% { background: var(--board); }
  40% { background: color-mix(in srgb, var(--good) 28%, var(--board)); }
  100% { background: var(--board); }
}
@keyframes mistakeShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-1.5px); }
  50% { transform: translateX(1.5px); }
  75% { transform: translateX(-1px); }
}
@keyframes mistakePulse {
  0%, 100% { background: var(--board); }
  35% { background: var(--danger-bg); }
}

.cell .notes {
  position: absolute;
  inset: 7%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}
.cell .note {
  font-family: var(--font-ui);
  font-size: calc(var(--cell-size) * 0.27);
  font-weight: 600;
  color: var(--pencil);
  display: flex;
  align-items: center;
  justify-content: center;
  visibility: hidden;
}

/* ---------- board veils (loading / paused) ---------- */
.board-veil {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  justify-content: center;
  background: var(--veil);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  border-radius: 3px;
  color: var(--muted);
  font-weight: 600;
  z-index: 2;
}
.board-veil.show { display: flex; }
#paused-veil span { font-family: var(--font-display); font-size: 1.1rem; letter-spacing: 0.08em; text-transform: uppercase; }
.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- rail: status ---------- */
.rail-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.timer-block { display: flex; align-items: center; gap: 8px; }
.timer {
  font-family: var(--font-display);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: clamp(1.5rem, 3vh, 2rem);
}
.meta-block { display: flex; flex-direction: column; align-items: flex-end; gap: 1px; }
.meta-score { font-family: var(--font-display); font-weight: 700; font-variant-numeric: tabular-nums; font-size: clamp(1.05rem, 2.2vh, 1.45rem); color: var(--accent); line-height: 1.1; }
.meta-difficulty { color: var(--muted); font-size: 0.82rem; font-weight: 600; }
.meta-best { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

/* ---------- difficulty ---------- */
.difficulty-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 4px;
  border-radius: 999px;
}
.difficulty-tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 8px 4px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.difficulty-tab:hover { color: var(--text); }
.difficulty-tab.active { background: var(--accent); color: var(--on-accent); }

/* ---------- numpad ---------- */
.numpad { display: grid; grid-template-columns: repeat(9, 1fr); gap: clamp(4px, 0.8vw, 7px); }
@media (min-width: 880px) { .numpad { grid-template-columns: repeat(3, 1fr); gap: 8px; } }
.num {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-family: var(--font-display);
  font-size: clamp(1.8rem, 8vw, 2.25rem);
  font-weight: 700;
  min-height: clamp(54px, 8.5vh, 68px);
  cursor: pointer;
  border-radius: 12px;
  transition: background 0.12s, transform 0.06s, opacity 0.2s;
}
.num:hover { background: var(--panel); }
.num:active { transform: scale(0.9); background: var(--panel-2); }
.num.done { opacity: 0.22; pointer-events: none; }

/* ---------- tools ---------- */
.tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px; }
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 2px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.tool:hover { background: var(--panel); }
.tool:active { background: var(--panel-2); }
.tool svg { width: 22px; height: 22px; }
.tool.active { color: var(--accent); }
.tool:disabled { opacity: 0.35; cursor: default; }
.tool .tool-state { font-size: 0.6rem; color: var(--muted); }
.tool.active .tool-state { color: var(--accent); }

/* ---------- buttons ---------- */
.btn { border: 0; border-radius: 12px; font: inherit; font-weight: 600; cursor: pointer; transition: background 0.15s, transform 0.05s; }
.btn:active { transform: translateY(1px); }
.btn.block { width: 100%; padding: 12px; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-press); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--panel-2); }
.btn-soft { background: var(--panel); border: 1px solid var(--border); color: var(--text); }
.btn-soft:hover { background: var(--panel-2); border-color: var(--accent); }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--page);
  padding: 11px 18px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9rem;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 40;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- modals ---------- */
.modal {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: rgba(8, 12, 22, 0.55);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 50;
  animation: fade 0.18s ease;
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative;
  width: min(94vw, 440px);
  max-height: 88vh;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 26px;
  border-radius: 20px;
  box-shadow: var(--shadow);
  animation: pop 0.22s ease;
}
.modal-card h2 { margin: 0 0 18px; font-family: var(--font-display); font-weight: 700; }
.modal-close { position: absolute; top: 14px; right: 14px; width: 32px; height: 32px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: 1rem; cursor: pointer; }
.modal-close:hover { background: var(--panel-2); color: var(--text); }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(0.94); opacity: 0; } }

/* settings */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 0; border-top: 1px solid var(--border); }
.setting:first-of-type { border-top: 0; }
.setting-label { display: flex; flex-direction: column; gap: 2px; font-weight: 600; }
.setting-label small { font-weight: 400; color: var(--muted); font-size: 0.78rem; }
.segmented { display: inline-flex; background: var(--panel-2); border-radius: 10px; padding: 3px; }
.segmented button { border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; font-size: 0.85rem; padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.segmented button.active { background: var(--accent); color: var(--on-accent); }

.toggle-row { cursor: pointer; }
.switch-input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track { position: relative; flex: 0 0 auto; width: 46px; height: 26px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--border); transition: background 0.18s, border-color 0.18s; }
.switch-track::after { content: ''; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: transform 0.18s, background 0.18s; }
.switch-input:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch-input:checked + .switch-track::after { transform: translateX(20px); background: #fff; }
.switch-input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* stats */
.stats-table { width: 100%; table-layout: fixed; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stats-table th, .stats-table td { padding: 8px 4px; text-align: right; border-top: 1px solid var(--border); font-size: 0.9rem; }
.stats-table th:first-child, .stats-table td:first-child { text-align: left; }
.stats-table thead th { color: var(--muted); font-weight: 600; font-size: 0.78rem; border-top: 0; }
.stats-table tbody td:first-child { font-weight: 600; }
.stats-overall { display: flex; gap: 10px; margin-bottom: 16px; }
.stats-overall .stat-card { flex: 1; background: var(--panel-2); border-radius: 12px; padding: 12px; text-align: center; }
.stats-overall .stat-card b { display: block; font-family: var(--font-display); font-size: 1.4rem; }
.stats-overall .stat-card span { color: var(--muted); font-size: 0.74rem; }
#reset-stats { margin-top: 16px; }

/* win */
.win-card { text-align: center; }
.win-card .trophy { font-size: 3rem; }
.win-card h2 { margin: 4px 0 6px; }
.win-score { display: flex; flex-direction: column; gap: 2px; margin: 2px 0 18px; }
.win-score-value { font-family: var(--font-display); font-weight: 800; font-size: 3rem; line-height: 1; color: var(--accent); font-variant-numeric: tabular-nums; }
.win-score-label { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.08em; }
.win-stats { display: flex; gap: 10px; margin-bottom: 20px; }
.win-stats > div { flex: 1; background: var(--panel-2); border-radius: 12px; padding: 12px 6px; display: flex; flex-direction: column; gap: 2px; }
.win-stat-value { font-family: var(--font-display); font-weight: 700; font-size: 1.3rem; font-variant-numeric: tabular-nums; }
.win-stat-label { color: var(--muted); font-size: 0.75rem; }
.win-card .btn { margin-top: 8px; }
.gameover-sub { color: var(--muted); margin: 2px 0 18px; }

/* ---------- mobile: maximize board width ---------- */
@media (max-width: 879px) {
  body {
    padding-left: max(7px, env(safe-area-inset-left));
    padding-right: max(7px, env(safe-area-inset-right));
  }
  .app { width: 100%; }
}

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