:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #1d2233;
  --muted: #6b7280;
  --line: #c7ccd9;
  --line-strong: #1d2233;
  --given: #1d2233;
  --entered: #2f5bd3;
  --peer: #e8ecf6;
  --same: #c9d6f7;
  --selected: #a9c0f5;
  --conflict: #d23f3f;
  --conflict-bg: #fbe3e3;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --button: #e9ecf4;
  --hint-area: #fdf3d4;
  --hint-key: #f9dc86;

  color-scheme: light dark;

  --board: min(94vw, 480px, 62vh);
  --cell: calc(var(--board) / 9);
}

/*
 * Dark palette. It applies when the system prefers dark and the player
 * hasn't chosen light, or when the player has chosen dark. The chosen
 * theme is set as data-theme on <html> (see the script in index.html).
 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12141c;
    --surface: #1b1e29;
    --text: #e6e8ef;
    --muted: #9aa1b2;
    --line: #3a3f52;
    --line-strong: #c9cedd;
    --given: #e6e8ef;
    --entered: #8fb0ff;
    --peer: #232839;
    --same: #2f3a5c;
    --selected: #3c4f86;
    --conflict: #ff7b7b;
    --conflict-bg: #4a2327;
    --accent: #5b82ef;
    --button: #262a38;
    --hint-area: #2f2b1d;
    --hint-key: #5c4d1c;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #12141c;
  --surface: #1b1e29;
  --text: #e6e8ef;
  --muted: #9aa1b2;
  --line: #3a3f52;
  --line-strong: #c9cedd;
  --given: #e6e8ef;
  --entered: #8fb0ff;
  --peer: #232839;
  --same: #2f3a5c;
  --selected: #3c4f86;
  --conflict: #ff7b7b;
  --conflict-bg: #4a2327;
  --accent: #5b82ef;
  --button: #262a38;
  --hint-area: #2f2b1d;
  --hint-key: #5c4d1c;
  color-scheme: dark;
}

:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  border: none;
  border-radius: 8px;
  background: var(--button);
  padding: 0.5em 0.9em;
  cursor: pointer;
  touch-action: manipulation;
}

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

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

.app {
  width: var(--board);
  margin: 0 auto;
  padding: 12px 0 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

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

h1 {
  display: flex;
  align-items: center;
  gap: 0.35em;
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
}

.logo { width: 1.3em; height: 1.3em; flex: none; }

/* Keep the title and header buttons on one line on the narrowest phones. */
@media (max-width: 359px) {
  h1 { font-size: 1.25rem; }
  .topbar-buttons { gap: 6px; }
  .topbar-buttons button { padding: 0.45em 0.7em; }
}

.topbar-buttons { display: flex; align-items: center; gap: 8px; }

#theme { font-size: 1.1rem; padding: 0.35em 0.6em; line-height: 1; }

#new-game { background: var(--accent); color: var(--accent-text); }

.status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
}

.badge { font-weight: 600; }

.mistakes { font-variant-numeric: tabular-nums; }
.mistakes[hidden] { display: none; }
.mistakes.last-chance { color: var(--conflict); font-weight: 600; }

.clock { display: flex; align-items: center; gap: 8px; }

#timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.15rem;
  color: var(--text);
}

.icon-button { padding: 0.3em 0.6em; font-size: 0.8rem; }

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

.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: var(--board);
  height: var(--board);
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface);
  user-select: none;
}

.board.paused .cell { color: transparent; }
.board.paused .notes { visibility: hidden; }

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--cell) * 0.58);
  color: var(--entered);
  cursor: pointer;
}

.cell.col-end { border-right: 2px solid var(--line-strong); }
.cell.row-end { border-bottom: 2px solid var(--line-strong); }
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n + 73) { border-bottom: none; }

.cell.hint-area { background: var(--hint-area); }
.cell.hint-key { background: var(--hint-key); }
.cell.hint-target { box-shadow: inset 0 0 0 3px var(--accent); }
.cell.given { color: var(--given); font-weight: 600; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--selected); }
.cell.conflict { color: var(--conflict); }
.cell.conflict:not(.selected) { background: var(--conflict-bg); }
.cell.wrong { color: var(--conflict); }
.cell.wrong.shake { animation: shake 0.3s; }

@keyframes shake {
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

@media (prefers-reduced-motion: reduce) {
  .cell.wrong.shake { animation: none; }
}

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--cell) * 0.24);
  color: var(--muted);
  line-height: 1;
}

.notes span { display: flex; align-items: center; justify-content: center; border-radius: 3px; }
.notes span.match { color: var(--accent-text); background: var(--accent); }

/* ---- Hint panel ---- */

.hint-panel {
  padding: 10px 14px 12px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
}

.hint-panel[hidden] { display: none; }
.hint-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hint-head h2 { margin: 0; font-size: 1rem; }
.hint-panel ol { margin: 6px 0 10px; padding-left: 1.3em; line-height: 1.5; }
.hint-panel ol.single-step { padding-left: 0; list-style: none; }
.hint-panel li + li { margin-top: 4px; }
.hint-more-item { list-style: none; }
.hint-more { padding: 0.3em 0.7em; font-size: 0.85rem; }
.hint-actions { display: flex; justify-content: flex-end; }
.hint-actions .primary { background: var(--accent); color: var(--accent-text); }

/* ---- Overlay (pause / solved) ---- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border-radius: 6px;
}

.overlay[hidden] { display: none; }
.overlay p { margin: 0 0 8px; }
.overlay-title { font-size: 1.6rem; font-weight: 700; }
.overlay-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 90%;
}

.overlay-buttons button.primary { background: var(--accent); color: var(--accent-text); }

/* Difficulty picker: a 2×2 grid of large buttons. */
.overlay-buttons.levels {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: min(90%, 320px);
}

.levels .level {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 3.6em;
  padding: 0.5em;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
}

.levels .level small { font-weight: 400; font-size: 0.75rem; opacity: 0.85; }
.levels .cancel { grid-column: 1 / -1; }

/* ---- Actions and number pad ---- */

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

.action {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.85rem;
}

.action-icon { font-size: 1.2rem; }

#notes[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

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

.pad button {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.45em 0;
  font-size: calc(var(--cell) * 0.5);
  line-height: 1.1;
}

.pad .left { font-size: 0.65rem; color: var(--muted); }

/* ---- Install button and dialog ---- */

.links { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; }

.link-button {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.5em 0.9em;
  border-radius: 8px;
  background: var(--button);
  color: var(--text);
  font-size: 0.9rem;
  text-decoration: none;
}

.link-button[hidden] { display: none; }
a.link-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.link-icon {
  width: 1.1em;
  height: 1.1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.install-dialog {
  width: min(92vw, 400px);
  padding: 0;
  border: none;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.3);
}

.install-dialog::backdrop { background: rgb(0 0 0 / 0.45); }

.dialog-body { padding: 20px; }
.dialog-body h2 { margin: 0 0 8px; font-size: 1.25rem; }
.dialog-body p { margin: 0 0 12px; color: var(--muted); }
.dialog-body ol { margin: 0 0 16px; padding-left: 1.3em; line-height: 1.6; }
.dialog-body li + li { margin-top: 6px; }
.dialog-body form { display: flex; justify-content: flex-end; }
.dialog-body .primary { background: var(--accent); color: var(--accent-text); }

.step-icon {
  width: 1.3em;
  height: 1.3em;
  vertical-align: -0.3em;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.help { margin: 0; font-size: 0.8rem; color: var(--muted); line-height: 1.8; }

kbd {
  font: inherit;
  font-size: 0.75rem;
  padding: 0.05em 0.4em;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
}

/* The keyboard help is noise on touch-only devices. */
@media (hover: none) and (pointer: coarse) { .help { display: none; } }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
