:root {
  color-scheme: light dark;

  --bg: #ffffff;
  --ink: #121212;
  --muted: #6b6b6b;
  --rule: #dfdfdf;
  --grid-thin: #b4b4b4;
  --grid-thick: #121212;
  --given: #e6e6e6;
  --select: #ffda00;
  --select-ink: #121212;
  --same: rgb(255 218 0 / 0.5);
  --peer: rgb(255 218 0 / 0.14);
  --error: #e0353b;
  --key: #ececec;
  --key-hover: #dedede;
  --surface: #ffffff;
  --backdrop: rgb(0 0 0 / 0.45);

  --serif: Georgia, "Times New Roman", Times, serif;
  --sans: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;

  /* Stacked layout: the board takes the width, the keypad sits below. */
  --board: min(100vw - 20px, 540px, max(100dvh - 310px, 300px));
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --ink: #f3f3f3;
    --muted: #a3a3a3;
    --rule: #333333;
    --grid-thin: #4d4d4d;
    --grid-thick: #d9d9d9;
    --given: #2b2b2b;
    --same: rgb(255 218 0 / 0.38);
    --peer: rgb(255 218 0 / 0.1);
    --error: #ff5a5f;
    --key: #2a2a2a;
    --key-hover: #3a3a3a;
    --surface: #1c1c1c;
    --backdrop: rgb(0 0 0 / 0.65);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom)
    env(safe-area-inset-left);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.4;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

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

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* ---- Header ---------------------------------------------------------- */

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--rule);
}

.masthead h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.masthead-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.link-button {
  padding: 8px 2px;
  border: 0;
  background: none;
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.link-button.danger {
  color: var(--error);
}

.pill-button {
  padding: 8px 18px;
  border: 1px solid var(--ink);
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 700;
}

.levels {
  display: flex;
  justify-content: center;
  border-bottom: 1px solid var(--rule);
}

.levels button {
  padding: 10px 14px 7px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: none;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}

.levels button[aria-selected="true"] {
  border-bottom-color: var(--ink);
  color: var(--ink);
}

/* ---- Layout ---------------------------------------------------------- */

.game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 8px 10px 28px;
}

.board-column,
.controls {
  width: var(--board);
}

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 36px;
  font-size: 0.9rem;
}

.seed {
  overflow: hidden;
  max-width: 60%;
  padding: 6px 0;
  border: 0;
  background: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.seed-label {
  color: var(--muted);
  font-family: var(--sans);
}

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

#timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

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

.board-frame {
  position: relative;
  width: var(--board);
  height: var(--board);
}

.board {
  display: grid;
  grid-template: repeat(9, 1fr) / repeat(9, 1fr);
  width: 100%;
  height: 100%;
  border: 2px solid var(--grid-thick);
  background: var(--bg);
  font-size: calc(var(--board) / 9 * 0.6);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.board-row {
  display: contents;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--grid-thin);
  border-bottom: 1px solid var(--grid-thin);
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.cell:focus,
.cell:focus-visible {
  outline: none;
}

.cell[data-col="2"],
.cell[data-col="5"] {
  border-right: 2px solid var(--grid-thick);
}

.cell[data-row="2"],
.cell[data-row="5"] {
  border-bottom: 2px solid var(--grid-thick);
}

.cell[data-col="8"] {
  border-right: 0;
}

.cell[data-row="8"] {
  border-bottom: 0;
}

.cell.given {
  background-color: var(--given);
  font-weight: 700;
}

/* Tints sit on top of the cell colour so given cells stay recognisable. */
.cell.peer {
  background-image: linear-gradient(var(--peer), var(--peer));
}

.cell.same {
  background-image: linear-gradient(var(--same), var(--same));
}

.cell.selected {
  background: var(--select);
  color: var(--select-ink);
}

.cell.conflict::after {
  content: "";
  position: absolute;
  right: 9%;
  bottom: 9%;
  width: 17%;
  height: 17%;
  border-radius: 50%;
  background: var(--error);
}

.notes {
  position: absolute;
  inset: 5%;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  color: var(--muted);
  font-size: 0.36em;
  pointer-events: none;
}

.cell.selected .notes {
  color: #4a4a4a;
}

.notes span {
  display: grid;
  place-items: center;
  visibility: hidden;
}

.notes span.on {
  visibility: visible;
}

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

.cell.selected .notes span.match {
  color: var(--select-ink);
}

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

.board.concealed .notes span.on {
  visibility: hidden;
}

.overlay {
  position: absolute;
  inset: 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--bg);
  text-align: center;
}

.overlay p {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.4rem;
  font-weight: 700;
}

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

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mode-row {
  display: flex;
  gap: 8px;
}

.mode {
  display: grid;
  flex: 1;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--ink);
  border-radius: 4px;
  overflow: hidden;
}

.mode button {
  min-height: 42px;
  border: 0;
  background: none;
  font-size: 0.95rem;
  font-weight: 600;
}

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

.tool {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  background: none;
  font-size: 0.95rem;
  font-weight: 600;
}

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

.key {
  min-height: 50px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: var(--key);
  font-size: 1.6rem;
  font-weight: 500;
  line-height: 1;
}

.key.used-up {
  opacity: 0.3;
}

.keypad.candidate .key[data-digit] {
  color: var(--muted);
  font-size: 1.05rem;
}

.options {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 20px;
  font-size: 0.9rem;
}

.options label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  cursor: pointer;
}

.options input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--ink);
}

.hint {
  display: none;
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
}

@media (hover: hover) {
  .key:hover:not(:disabled) {
    background: var(--key-hover);
  }

  .hint {
    display: block;
  }
}

.key:active:not(:disabled) {
  background: var(--key-hover);
}

/* Side-by-side layout for landscape screens. */
@media (min-width: 720px) and (min-aspect-ratio: 11/10) {
  :root {
    --board: min(560px, 100vw - 360px, max(100dvh - 170px, 260px));
  }

  .game {
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
    gap: 36px;
    padding-top: 16px;
  }

  .controls {
    width: 280px;
    padding-top: 36px;
  }

  .keypad {
    grid-template-columns: repeat(3, 1fr);
  }

  .key {
    min-height: min(68px, max(40px, (var(--board) - 170px) / 4));
  }

  .key-erase {
    grid-column: 1 / -1;
    min-height: 46px;
  }

  .options {
    flex-direction: column;
  }
}

@media (max-height: 520px) {
  .masthead {
    padding-block: 2px;
  }

  .masthead h1 {
    font-size: 1.25rem;
  }

  .levels button {
    padding-block: 5px 3px;
  }

  .game {
    padding-top: 4px;
  }
}

/* ---- Dialogs --------------------------------------------------------- */

dialog {
  width: min(460px, 100vw - 24px);
  max-height: min(640px, 100dvh - 32px);
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.3);
}

dialog::backdrop {
  background: var(--backdrop);
}

.dialog-body {
  padding: 22px 22px 18px;
}

dialog h2 {
  margin: 0 0 12px;
  font-family: var(--serif);
  font-size: 1.5rem;
  line-height: 1.2;
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 18px;
  margin-top: 18px;
}

.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  margin: 0 0 16px;
  padding: 0;
  border: 0;
}

.choices legend,
.field {
  display: block;
  margin-bottom: 6px;
  padding: 0;
  font-size: 0.85rem;
  font-weight: 700;
}

.choices label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  cursor: pointer;
}

.choices input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--ink);
}

.optional {
  color: var(--muted);
  font-weight: 400;
}

.field-row {
  display: flex;
  gap: 8px;
}

.field-row input {
  flex: 1;
  min-width: 0;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--grid-thin);
  border-radius: 4px;
  background: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.help {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.summary-card {
  padding: 8px 10px;
  border: 1px solid var(--rule);
  border-radius: 6px;
}

.summary-card strong {
  display: block;
  font-size: 0.85rem;
}

.summary-card span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.history-list {
  border-top: 1px solid var(--rule);
}

.history-empty {
  margin: 16px 0 0;
  color: var(--muted);
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--rule);
}

.history-main {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.history-meta {
  color: var(--muted);
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

.history-meta code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.done-time {
  margin: 0;
  font-size: 2.4rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

#done-detail {
  margin: 6px 0 0;
  color: var(--muted);
}

/* ---- Toast ----------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 10;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 6px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.noscript {
  padding: 16px;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .toast {
    transition: none;
  }
}
