:root {
  color-scheme: dark;
  --bg: #0f1216;
  --panel: #161b22;
  --panel-2: #1b2230;
  --border: #242c38;
  --border-strong: #3b4656;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #4f8cff;
  --accent-text: #ffffff;
  --green: #3a8f4d;
  --green-border: #52ad68;
  --yellow: #c9a227;
  --danger: #f06b6b;
  --key: #2b3644;
  --key-text: #e6edf3;
  --shadow: 0 22px 70px rgba(0, 0, 0, .45);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f8fa;
  --panel: #ffffff;
  --panel-2: #f0f3f6;
  --border: #d8dee6;
  --border-strong: #b9c2cf;
  --text: #11161d;
  --muted: #5c6773;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --green: #4b9f5f;
  --green-border: #3c8a4f;
  --yellow: #d5ad35;
  --danger: #b83a3a;
  --key: #e9edf2;
  --key-text: #11161d;
  --shadow: 0 22px 70px rgba(24, 39, 60, .18);
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  padding: clamp(18px, 4vw, 40px) clamp(12px, 4vw, 32px) 28px;
  background:
    radial-gradient(820px 420px at 50% -160px, color-mix(in srgb, var(--accent) 13%, transparent), transparent 72%),
    var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; }

.page {
  width: min(680px, 100%);
  margin: 0 auto;
}

.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px 18px;
  margin-bottom: clamp(16px, 3.5vw, 26px);
}

.brand h1 {
  margin: 0;
  font-size: clamp(24px, 4vw, 32px);
  line-height: 1.1;
  letter-spacing: .09em;
}

.brand p {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: .86rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
}

.button,
.icon-button {
  min-height: 38px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel);
  color: var(--text);
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease, transform .15s ease, color .15s ease;
}

.button:hover,
.icon-button:hover {
  background: var(--panel-2);
  border-color: var(--border-strong);
}

.button:active { transform: translateY(1px); }

.button--primary {
  border-color: color-mix(in srgb, var(--accent) 70%, var(--border));
  background: var(--accent);
  color: var(--accent-text);
}

.button--primary:hover {
  background: color-mix(in srgb, var(--accent) 84%, #000);
  border-color: var(--accent);
}

.icon-button {
  width: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1rem;
}

button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent);
}

.button:disabled,
.icon-button:disabled,
.action:disabled,
.pad-key:disabled {
  opacity: .45;
  cursor: default;
  transform: none;
}

/* ------------------------------------------------------------ difficulty */

.difficulty {
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
}

.difficulty button {
  min-height: 30px;
  padding: 4px 9px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 650;
  cursor: pointer;
  transition: background-color .15s ease, color .15s ease;
}

.difficulty button:hover { color: var(--text); }

.difficulty button[aria-pressed="true"] {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  color: var(--text);
}

.difficulty button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ----------------------------------------------------------------- stats */

.game {
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
  width: min(520px, 100%);
  margin: 0 auto 13px;
}

.stat {
  min-width: 0;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  text-align: center;
}

.stat > span {
  display: block;
  color: var(--muted);
  font-size: .64rem;
  font-weight: 650;
  letter-spacing: .07em;
  text-transform: uppercase;
}

.stat > strong {
  display: block;
  margin-top: 1px;
  font-size: .92rem;
  font-variant-numeric: tabular-nums;
}

.stat--warn > strong { color: var(--yellow); }
.stat--danger > strong { color: var(--danger); }

/* ----------------------------------------------------------------- board */

.board {
  display: grid;
  grid-template-rows: repeat(9, minmax(0, 1fr));
  width: min(520px, 100%);
  margin: 0 auto;
  aspect-ratio: 1;
  border: 2px solid var(--border-strong);
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  outline: none;
  transition: opacity .15s ease;
}

.board:focus-visible {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent);
}

.board[aria-busy="true"] {
  opacity: .55;
  pointer-events: none;
}

.board-row {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  min-height: 0;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  user-select: none;
  transition: background-color .12s ease;
}

.cell--last-col { border-right: 0; }
.cell--last-row { border-bottom: 0; }

.cell--sep-right {
  border-right-width: 2px;
  border-right-color: var(--border-strong);
}

.cell--sep-bottom {
  border-bottom-width: 2px;
  border-bottom-color: var(--border-strong);
}

.cell:hover { background: color-mix(in srgb, var(--accent) 7%, var(--panel)); }

.cell--peer {
  background: color-mix(in srgb, var(--accent) 5%, var(--panel));
}

.cell--same {
  background: color-mix(in srgb, var(--accent) 15%, var(--panel));
}

.cell--selected {
  z-index: 2;
  background: color-mix(in srgb, var(--accent) 24%, var(--panel));
  box-shadow: inset 0 0 0 2px var(--accent);
}

.cell--error {
  z-index: 3;
  background: color-mix(in srgb, var(--danger) 32%, var(--panel));
  animation: cell-shake .28s ease-out;
}

@keyframes cell-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  50% { transform: translateX(2px); }
  75% { transform: translateX(-1px); }
}

.cell-value {
  display: block;
  font-size: clamp(18px, 4.7vw, 29px);
  font-weight: 650;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.cell--given .cell-value {
  color: var(--text);
  font-weight: 750;
}

.cell--user .cell-value {
  color: var(--green-border);
  font-weight: 750;
}

.cell--reveal .cell-value {
  color: var(--danger);
  font-weight: 750;
}

.cell-notes {
  display: none;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  width: 100%;
  height: 100%;
  padding: 2px;
  color: var(--muted);
  font-size: clamp(7px, 1.7vw, 10px);
  font-weight: 650;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.cell-notes > span {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.cell--notes .cell-notes { display: grid; }

/* ---------------------------------------------------------------- status */

.status {
  min-height: 1.45em;
  margin: 16px 0 0;
  text-align: center;
  font-size: .88rem;
  font-weight: 650;
  color: var(--text);
}

.status--error { color: var(--danger); }
.status--warn { color: var(--yellow); }
.status--win { color: var(--green-border); }
.status--lose { color: var(--danger); }
.status--hint { color: var(--accent); }

/* ------------------------------------------------------------ number pad */

.pad {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: clamp(3px, .9vw, 6px);
  width: min(520px, 100%);
  margin: 15px auto 0;
}

.pad-key {
  position: relative;
  display: grid;
  place-items: center;
  height: clamp(42px, 9.5vw, 50px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--key);
  color: var(--key-text);
  cursor: pointer;
  user-select: none;
  transition: background-color .14s ease, border-color .14s ease, color .14s ease, transform .1s ease, opacity .14s ease;
}

.pad-key:hover { border-color: var(--border-strong); }
.pad-key:active { transform: translateY(1px); }

.pad-key .pad-digit {
  font-size: clamp(.92rem, 2.7vw, 1.08rem);
  font-weight: 750;
  line-height: 1;
}

.pad-key::after {
  content: attr(data-count);
  position: absolute;
  right: 4px;
  bottom: 1px;
  color: var(--muted);
  font-size: .55rem;
  font-weight: 650;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.pad-key--done {
  opacity: .42;
}

.pad--notes .pad-key {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  background: color-mix(in srgb, var(--accent) 12%, var(--key));
}

.pad--notes .pad-key .pad-digit { color: var(--accent); }

/* --------------------------------------------------------------- actions */

.actions {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  width: min(520px, 100%);
  margin: 7px auto 0;
}

.action {
  min-height: 40px;
  padding: 6px 7px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--panel);
  color: var(--text);
  font-size: .74rem;
  font-weight: 650;
  cursor: pointer;
  transition: background-color .14s ease, border-color .14s ease, color .14s ease, transform .1s ease, opacity .14s ease;
}

.action:hover {
  background: var(--panel-2);
  border-color: var(--border-strong);
}

.action:active { transform: translateY(1px); }

.action[aria-pressed="true"] {
  border-color: color-mix(in srgb, var(--accent) 62%, var(--border));
  background: color-mix(in srgb, var(--accent) 18%, var(--panel));
  color: var(--accent);
}

.key-hint {
  display: inline-grid;
  place-items: center;
  min-width: 15px;
  height: 16px;
  margin-left: 3px;
  padding: 0 3px;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--muted);
  font-size: .58rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: 1px;
}

.help {
  margin: 12px 0 0;
  text-align: center;
  color: var(--muted);
  font-size: .72rem;
  line-height: 1.55;
}

/* ---------------------------------------------------------------- result */

.result {
  margin: 20px 0 0;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  animation: result-in .16s ease-out;
}

@keyframes result-in {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: translateY(0); }
}

.result-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px 14px;
}

.result-head > span {
  color: var(--muted);
  font-size: .8rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .06em;
}

.result-time {
  font-size: 1.35rem;
  letter-spacing: .04em;
  font-variant-numeric: tabular-nums;
}

.result-meta {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: .84rem;
}

.result-record {
  margin: 7px 0 0;
  color: var(--accent);
  font-size: .84rem;
  font-weight: 650;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

/* ---------------------------------------------------------------- footer */

.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 20px;
  margin-top: 30px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: .76rem;
}

.footer a {
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover { text-decoration: underline; }

.noscript {
  width: min(680px, 100%);
  margin: 18px auto 0;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--danger);
  text-align: center;
  font-size: .85rem;
}

@media (max-width: 600px) {
  .header {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar { justify-content: flex-start; }

  .toolbar .button { flex: 0 1 auto; }

  .footer { flex-direction: column; }
}

@media (max-width: 520px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .action { font-size: .7rem; }

  .key-hint { display: none; }
}

@media (max-width: 430px) {
  .cell { border-radius: 0; }

  .cell-notes { padding: 1px; }

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

  .pad-key::after { right: 3px; }
}

@media (max-width: 370px) {
  body { padding-left: 8px; padding-right: 8px; }

  .board { border-radius: 8px; }

  .pad { gap: 3px; }

  .pad-key { height: 40px; border-radius: 6px; }

  .pad-key::after { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
