:root {
  --bg: #f7f4ee;
  --surface: #fffdf9;
  --text: #22252b;
  --muted: #7d8089;
  --line: #d3cdc0;
  --line-strong: #3b3e46;
  --given: #1c1f26;
  --typed: #2d52c4;
  --ink: #1e2f6e;
  --wrong: #cf3a36;
  --wrong-bg: #fbe2df;
  --sel: #d9e4ff;
  --peer: #f1ede4;
  --same: #c9d8ff;
  --accent: #2d52c4;
  --lv-easy: #3f9d5a;
  --lv-medium: #2d7fc4;
  --lv-hard: #e08a1e;
  --lv-expert: #c9453d;
  --empty: #e0a526;
  --empty-bg: #fdf3d9;
  --btn: #ffffff;
  --btn-border: #e4dfd4;
  --shadow: 0 1px 2px rgba(40, 30, 10, 0.08), 0 4px 14px rgba(40, 30, 10, 0.07);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18191c;
    --surface: #212226;
    --text: #e9e9ec;
    --muted: #9b9ea6;
    --line: #3a3c43;
    --line-strong: #a2a5ad;
    --given: #f3f3f5;
    --typed: #93b2ff;
    --ink: #bccdff;
    --wrong: #ff7d74;
    --wrong-bg: #4b2424;
    --sel: #2d3b60;
    --peer: #27292e;
    --same: #36487a;
    --accent: #93b2ff;
    --lv-easy: #5cc27a;
    --lv-medium: #5fa6ec;
    --lv-hard: #f0a54a;
    --lv-expert: #f07068;
    --empty: #d9a53a;
    --empty-bg: #3d3320;
    --btn: #26282d;
    --btn-border: #34363d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  overflow: hidden;
}

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

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

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.link {
  color: var(--accent);
  font-weight: 500;
  padding: 10px 12px;
}

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

.home-inner {
  margin: auto;
  width: min(640px, calc(100vw - 32px));
  text-align: center;
  padding: 24px 0;
}

h1 {
  font: 600 46px/1.1 ui-serif, "New York", Georgia, serif;
  margin: 0 0 6px;
  letter-spacing: -0.01em;
}

#home, #new, #saves-screen, #stats { overflow-y: auto; -webkit-overflow-scrolling: touch; }

.menu { display: flex; flex-direction: column; gap: 14px; margin-top: 36px; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 24px 28px;
  background: var(--surface);
  border: 1px solid var(--btn-border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform 0.12s;
}
.menu-item:active { transform: scale(0.98); }
.menu-item svg { width: 30px; height: 30px; color: var(--accent); }
.menu-text { display: flex; flex-direction: column; gap: 2px; }
.menu-title { font: 600 24px/1.2 ui-serif, "New York", Georgia, serif; }
.menu-sub { color: var(--muted); font-size: 15px; min-height: 1.4em; }

.page-inner {
  width: min(640px, calc(100vw - 32px));
  margin: 16px auto 0;
  padding-bottom: 32px;
}

/* Difficulty indicator: 1-4 filled dots in the level's colour */
.dots { display: inline-flex; gap: 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.dot.on { background: var(--lv); }
[data-level=easy], .lv-easy { --lv: var(--lv-easy); }
[data-level=medium], .lv-medium { --lv: var(--lv-medium); }
[data-level=hard], .lv-hard { --lv: var(--lv-hard); }
[data-level=expert], .lv-expert { --lv: var(--lv-expert); }

.empty-state { text-align: center; color: var(--muted); padding: 40px 0; }
.primary-btn {
  margin-top: 8px;
  padding: 14px 24px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--surface);
  font-weight: 600;
}

.saves { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.save {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--btn-border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.save-open {
  flex: 1;
  display: grid;
  grid-template-columns: 120px 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 16px 18px;
  text-align: left;
}
.save-level { display: flex; flex-direction: column; gap: 6px; font: 600 20px/1.1 ui-serif, "New York", Georgia, serif; }
.save-progress { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.save-bar { height: 6px; border-radius: 3px; background: var(--peer); overflow: hidden; }
.save-bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.save-when { color: var(--muted); font-size: 14px; text-align: right; font-variant-numeric: tabular-nums; }
.save-delete {
  width: 52px;
  color: var(--muted);
  border-left: 1px solid var(--btn-border);
  display: flex;
  align-items: center;
  justify-content: center;
}

.levels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.level {
  background: var(--surface);
  border: 1px solid var(--btn-border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 34px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  transition: transform 0.12s;
}
.level:active { transform: scale(0.97); }
.level .name { font: 600 28px/1.1 ui-serif, "New York", Georgia, serif; }
.level .dots { margin-bottom: 4px; }
.level .dot { width: 10px; height: 10px; }
.level .meta { color: var(--muted); font-size: 15px; min-height: 1.4em; }
.level .meta.continue { color: var(--accent); font-weight: 500; }


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

.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 20px;
  gap: 12px;
}
.bar > :last-child { justify-self: end; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 10px 8px 4px;
  color: var(--accent);
  font-weight: 500;
  justify-self: start;
}

.level-name {
  font: 600 22px/1 ui-serif, "New York", Georgia, serif;
}
.title-block { text-align: center; }
.saved-note { color: var(--muted); font-size: 12px; margin-top: 4px; }

.seg {
  display: inline-flex;
  background: var(--peer);
  border-radius: 12px;
  padding: 3px;
  gap: 2px;
}
.seg button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 9px;
  color: var(--muted);
  font-weight: 500;
  font-size: 15px;
}
.seg button.on {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.play {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 36px;
  padding: 4px 24px 24px;
}

.board {
  --size: min(calc(100dvh - 104px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc(100vw - 380px));
  --cell: calc(var(--size) / 9);
  position: relative;
  width: var(--size);
  height: var(--size);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--surface);
  box-shadow: 0 0 0 2.5px var(--line-strong), var(--shadow);
  border-radius: 2px;
  touch-action: none;
}

.cell {
  position: relative;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.12s;
}
.cell.br { border-right: 2px solid var(--line-strong); }
.cell.bb { border-bottom: 2px solid var(--line-strong); }
.cell.er { border-right: 0; }
.cell.eb { border-bottom: 0; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--sel); }
.cell.wrong { background: var(--wrong-bg); }
.cell.empty-mark { box-shadow: inset 0 0 0 3px var(--empty); background: var(--empty-bg); }

.val {
  font-size: calc(var(--cell) * 0.6);
  line-height: 1;
  color: var(--typed);
  font-weight: 400;
}
.cell.given .val {
  color: var(--given);
  font-family: ui-serif, "New York", Georgia, serif;
  font-weight: 500;
}
.cell.wrong .val { color: var(--wrong); }

.notes {
  position: absolute;
  inset: 5%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--cell) * 0.22);
  line-height: 1;
  color: var(--muted);
  text-align: center;
  align-items: center;
}

.hint {
  position: absolute;
  right: 7%;
  bottom: 5%;
  font-size: calc(var(--cell) * 0.19);
  line-height: 1;
  color: var(--muted);
  opacity: 0.8;
}

#ink {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.board.solved .cell { animation: ripple 0.6s ease-out both; }
@keyframes ripple {
  40% { background: var(--same); }
  100% { background: transparent; }
}

/* Correction picker */
.picker {
  position: absolute;
  z-index: 5;
  width: calc(var(--cell) * 3 + 20px);
  background: var(--surface);
  border: 1px solid var(--btn-border);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  padding: 8px;
}
.picker-title { font-size: 13px; color: var(--muted); padding: 0 4px 6px; }
.picker-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
}
.picker-grid button {
  height: calc(var(--cell) * 0.85);
  border-radius: 9px;
  font-size: calc(var(--cell) * 0.42);
  background: var(--peer);
}
.picker-grid button.current { background: var(--accent); color: var(--surface); }
.picker-grid button.guess { box-shadow: inset 0 0 0 2px var(--accent); }
.picker-clear {
  width: 100%;
  margin-top: 6px;
  padding: 8px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 15px;
}

/* Controls */
.controls {
  width: 300px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

body.mode-pencil .digital-only,
body.mode-digital .pencil-only { display: none !important; }

.tools { align-self: stretch; }
.tools button { flex: 1; padding: 12px 14px; font-size: 17px; }

.tip {
  margin: 0;
  color: var(--muted);
  font-size: 15px;
  text-align: center;
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.pad button {
  aspect-ratio: 1;
  background: var(--btn);
  border: 1px solid var(--btn-border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  font-size: 34px;
  color: var(--typed);
  transition: transform 0.08s;
}
.pad button:active { transform: scale(0.94); }
.pad button.done { opacity: 0.3; }
body.notes-on .pad button { font-size: 22px; color: var(--muted); }

.entry-toggle { align-self: stretch; }
.entry-toggle button { flex: 1; padding: 10px; font-size: 16px; }

.actions {
  display: flex;
  gap: 10px;
}
.actions > * { flex: 1; min-width: 0; }
.action {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 10px;
  background: var(--btn);
  border: 1px solid var(--btn-border);
  border-radius: 14px;
  font-weight: 500;
  font-size: 16px;
}
.action:disabled { opacity: 0.4; }
.done-btn {
  padding: 16px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--surface);
  font-weight: 600;
  font-size: 18px;
}
.done-btn:disabled { opacity: 0.4; }

#new-game { align-self: center; }

@media (orientation: portrait) {
  .play { flex-direction: column; gap: 22px; padding-top: 8px; }
  .board { --size: min(calc(100vw - 48px), calc(100dvh - 420px - env(safe-area-inset-top) - env(safe-area-inset-bottom))); }
  .controls { width: var(--size); gap: 14px; }
  .pad { grid-template-columns: repeat(9, 1fr); gap: 8px; }
  .pad button { font-size: 30px; border-radius: 12px; }
  .tools, .entry-toggle { align-self: center; width: min(360px, 100%); }
  .done-btn { padding: 14px; }
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  top: calc(10px + env(safe-area-inset-top));
  transform: translate(-50%, -20px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: 999px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 20;
  width: max-content;
  max-width: min(560px, calc(100vw - 32px));
  text-align: center;
}
.toast.long { border-radius: 18px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Solved overlay */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 18, 14, 0.25);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade 0.3s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.card {
  background: var(--surface);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  padding: 36px 44px 30px;
  text-align: center;
  min-width: 320px;
  animation: pop 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes pop { from { transform: scale(0.9); opacity: 0; } }
.card-kicker { font: 600 34px/1.1 ui-serif, "New York", Georgia, serif; }
.card-time { font-size: 44px; font-weight: 300; margin: 10px 0 2px; font-variant-numeric: tabular-nums; }
.card-sub { color: var(--muted); min-height: 1.4em; }
.card-actions { display: flex; gap: 10px; margin-top: 26px; justify-content: center; }
.card-actions button {
  padding: 12px 22px;
  border-radius: 12px;
  background: var(--peer);
  font-weight: 500;
}
.card-actions .primary { background: var(--accent); color: var(--surface); }

/* ---------- Stats ---------- */

.stats-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}
.stats-table th, .stats-table td { padding: 14px 16px; text-align: right; }
.stats-table th { color: var(--muted); font-weight: 500; font-size: 14px; }
.stats-table td:first-child, .stats-table th:first-child { text-align: left; font-weight: 600; }
.stats-table tbody tr { border-top: 1px solid var(--line); }
h2 { font: 600 22px/1.2 ui-serif, "New York", Georgia, serif; margin: 30px 0 10px; }
.history { list-style: none; margin: 0; padding: 0; }
.history li {
  display: flex;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.history .when { color: var(--muted); }
.history .empty { color: var(--muted); justify-content: center; border: 0; }

/* Landscape: messages sit in the free space under the controls, off the board. */
@media (orientation: landscape) {
  .toast {
    left: auto;
    right: calc(24px + env(safe-area-inset-right));
    top: auto;
    bottom: calc(24px + env(safe-area-inset-bottom));
    width: 300px;
    max-width: 300px;
    border-radius: 18px;
    transform: translateY(20px);
  }
  .toast.show { transform: none; }
}
