/* Palette and type follow the nonogram game for a consistent look */
:root {
  --bg: #f5f0e8;
  --bg-warm: #ede7db;
  --surface: #ffffff;
  --text: #2c2826;
  --muted: #8a8279;
  --faint: #b8b0a4;
  --line: #938f8b;
  --line-strong: #2c2826;
  --accent: #c45d3e;
  --accent-hover: #a84a2f;
  --accent-text: #ffffff;
  --given: #2c2826;
  --user: #5b7ab8;
  --wrong: #d94040;
  --wrong-bg: #f6d9d3;
  --related: #f3ebe0;
  --same: #e8cfc4;
  --selected: #dfa894;
  --flash: #f0d9a8;
  --success: #6b8f5a;
  --radius: 6px;
  --cell-font: clamp(18px, 6vw, 30px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1714;
    --bg-warm: #222019;
    --surface: #252220;
    --text: #e8e2d8;
    --muted: #8a8279;
    --faint: #5a544e;
    --line: #6b6560;
    --line-strong: #9e9890;
    --accent: #d4694a;
    --accent-hover: #b8533a;
    --given: #e8e2d8;
    --user: #8aa4dc;
    --wrong: #ff6b5e;
    --wrong-bg: #3e221c;
    --related: #2e2a26;
    --same: #4a3328;
    --selected: #6e3d2c;
    --flash: #5a4a22;
    --success: #8fb87a;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1a1714;
  --bg-warm: #222019;
  --surface: #252220;
  --text: #e8e2d8;
  --muted: #8a8279;
  --faint: #5a544e;
  --line: #6b6560;
  --line-strong: #9e9890;
  --accent: #d4694a;
  --accent-hover: #b8533a;
  --given: #e8e2d8;
  --user: #8aa4dc;
  --wrong: #ff6b5e;
  --wrong-bg: #3e221c;
  --related: #2e2a26;
  --same: #4a3328;
  --selected: #6e3d2c;
  --flash: #5a4a22;
  --success: #8fb87a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'DM Mono', ui-monospace, monospace;
  -webkit-tap-highlight-color: transparent;
}

button, select {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:disabled { cursor: default; opacity: 0.45; }

.app {
  --w: min(100vw - 24px, 520px);
  width: var(--w);
  margin: 0 auto;
  padding: 12px 0 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  user-select: none;
}

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

.topbar h1 {
  margin: 0;
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: 1.6rem;
  letter-spacing: -0.02em;
}

.topbar h1 span { color: var(--accent); }

.topbar-actions { display: flex; gap: 6px; }

.icon-btn {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 1.1rem;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  transition: color 0.15s;
}

.icon-btn:hover { color: var(--text); }

.icon-btn.small { width: 26px; height: 26px; font-size: 0.8rem; }

.setup {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.segmented {
  display: flex;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 3px;
  flex: 1 1 auto;
}

.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

.segmented button[aria-checked="true"] {
  background: var(--text);
  color: var(--bg);
}

select {
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 0.85rem;
}

.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: 1.5px solid var(--accent);
  border-radius: var(--radius);
  padding: 8px 14px;
  font-size: 0.85rem;
  transition: background 0.15s, border-color 0.15s;
}

.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 32px;
}

.pill {
  background: var(--bg-warm);
  border-radius: 3px;
  padding: 2px 8px;
  font-size: 0.75rem;
  color: var(--muted);
}

.lives { font-size: 1.35rem; letter-spacing: 0.15em; color: var(--accent); }
.lives .lost { color: var(--faint); }
.lives .pop { animation: pop 0.5s ease; }

.timer-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  color: var(--muted);
}

/* ---------- Board ---------- */

.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1;
  width: 100%;
  background: var(--surface);
  border: 2px solid var(--line-strong);
  overflow: hidden;
}

.board.paused .cell > * { visibility: hidden; }

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-family: 'Fraunces', Georgia, serif;
  font-size: var(--cell-font);
  color: var(--user);
  cursor: pointer;
  transition: background-color 0.12s;
}

.cell.c8 { border-right: 0; }
.cell.r8 { border-bottom: 0; }
.cell.c2, .cell.c5 { border-right: 2px solid var(--line-strong); }
.cell.r2, .cell.r5 { border-bottom: 2px solid var(--line-strong); }

.cell.given { color: var(--given); font-weight: 700; }
.cell.related { background: var(--related); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--selected); }
.cell.wrong { color: var(--wrong); }
.cell.wrong:not(.selected) { background: var(--wrong-bg); }

.cell .value { line-height: 1; }

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-family: 'DM Mono', ui-monospace, monospace;
  font-size: calc(var(--cell-font) * 0.38);
  color: var(--muted);
  pointer-events: none;
}

.notes span {
  display: grid;
  place-items: center;
  line-height: 1;
  border-radius: 3px;
}

.notes span.match {
  color: var(--accent-text);
  background: var(--accent);
  font-weight: 700;
}

.cell.flash { animation: flash 0.7s ease both; animation-delay: var(--delay, 0s); }
.cell.shake { animation: shake 0.4s ease; }
.cell.popin .value { animation: popin 0.2s ease; }

@keyframes flash {
  0% { transform: scale(1); }
  35% { background: var(--flash); transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-4px); }
  40%, 80% { transform: translateX(4px); }
}

@keyframes popin {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.6); }
  100% { transform: scale(1); }
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  backdrop-filter: blur(6px);
}

.overlay[hidden] { display: none; }

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

.actions {
  display: flex;
  gap: 8px;
}

.app:not(.mode-normal) .normal-only { display: none; }

.action {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 4px;
  font-size: 0.8rem;
  color: var(--muted);
}

.action.wide { flex-direction: row; justify-content: center; gap: 8px; font-size: 0.9rem; }
.action .ico { font-size: 1.25rem; color: var(--text); }
.action.wide .ico { font-size: 1rem; }
.action[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.action:hover { border-color: var(--accent); }
.action .badge {
  font-size: 0.7rem;
  background: var(--bg-warm);
  color: var(--text);
  border-radius: 3px;
  padding: 0 5px;
}
.action[aria-pressed="true"] .badge { background: var(--accent); color: var(--accent-text); }

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

.num {
  position: relative;
  aspect-ratio: 0.8;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-family: 'Fraunces', Georgia, serif;
  font-size: clamp(20px, 6.5vw, 30px);
  color: var(--user);
  padding: 0;
}

.num:active:not(:disabled) { transform: scale(0.94); }
.num .left { font-family: 'DM Mono', ui-monospace, monospace; font-size: 0.4em; color: var(--muted); }
.num.done { color: var(--success); }
.num.done:disabled { opacity: 0.8; }
.app.notes-on .num { color: var(--muted); border-style: dashed; }

/* ---------- Scan ---------- */

.app[hidden] { display: none; }

.secondary {
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 0.85rem;
  transition: border-color 0.15s;
}

.secondary:hover { border-color: var(--accent); }

.file-btn { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.scan-view .file-btn { flex: 1; justify-content: center; }

.scan-photo { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.scan-photo[hidden] { display: none; }
.scan-photo canvas { touch-action: none; border-radius: var(--radius); max-width: 100%; }

.scan-hint { margin: 0; font-size: 0.8rem; color: var(--muted); text-align: center; }
.scan-hint[hidden] { display: none; }

.scan-status { margin: 0; min-height: 1.2em; font-size: 0.85rem; text-align: center; }
.scan-status.good { color: var(--success); }
.scan-status.bad { color: var(--wrong); }

.scan-view .cell.unsure { outline: 2px dotted var(--accent); outline-offset: -4px; }
.scan-view .num { aspect-ratio: 1; }

.start-btn { padding: 12px; font-size: 1rem; }

/* ---------- Toast & dialog ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 10;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.good { background: var(--success); color: #fff; }
.toast.bad { background: var(--wrong); color: #fff; }

dialog {
  border: 1.5px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 20px;
  width: min(90vw, 360px);
  background: var(--bg);
  color: var(--text);
}

dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h2 { margin: 0 0 8px; font-family: 'Fraunces', Georgia, serif; color: var(--accent); }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; padding: 0; margin: 16px 0 0; }
dialog menu button { border-radius: var(--radius); padding: 8px 14px; border: 1.5px solid var(--line); background: var(--surface); font-size: 0.85rem; }
dialog menu button.primary { border-color: var(--accent); background: var(--accent); }

.stats { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stats th, .stats td { padding: 4px 6px; text-align: left; border-bottom: 1px solid var(--line); }

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