:root {
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  color: #edf3f2;
  background: #101519;
  --panel: #1c252b;
  --line: #41515b;
  --accent: #d4f977;
  --muted: #bac8cf;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; }
button, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .58; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
a { color: var(--accent); text-underline-offset: 3px; }
.shell {
  --gutter: clamp(12px, 2.5vw, 28px);
  --play-gap: clamp(12px, 2vw, 24px);
  --board-size: clamp(220px, calc(100dvh - 166px), 650px);
  --controls-width: clamp(218px, 25vw, 300px);
  --layout-width: min(100%, calc(var(--board-size) + var(--controls-width) + var(--play-gap)));
  display: flex;
  flex-direction: column;
  gap: var(--play-gap);
  width: 100%;
  min-height: 100dvh;
  padding: max(10px, env(safe-area-inset-top)) max(var(--gutter), env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
}
.play-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  width: var(--layout-width);
  margin-inline: auto;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}
.play-header .back-link { display: inline-flex; align-items: center; min-height: 44px; }
.settings-button { justify-self: end; min-height: 44px; padding: 0 8px; border: 1px solid var(--line); border-radius: 7px; color: inherit; background: var(--panel); }
.puzzle-picker { grid-column: 1 / -1; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.difficulty-tabs { display: flex; gap: 6px; }
.difficulty-tabs a, #history-button, .history-links a { display: inline-flex; align-items: center; min-height: 44px; gap: 5px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 7px; color: inherit; background: var(--panel); text-decoration: none; font-size: .875rem; }
.difficulty-tabs a[aria-current] { border-color: var(--accent); color: var(--accent); }
#update-game { min-height: 44px; padding: 4px 8px; border: 1px solid var(--accent); border-radius: 7px; color: #152006; background: var(--accent); font-size: .875rem; font-weight: 700; white-space: nowrap; }
#update-status { grid-column: 1 / -1; margin: 0; color: var(--muted); font-size: .875rem; text-align: center; }
#update-status:empty { display: none; }
.puzzle-link-state { color: var(--accent); }
.history-days { display: grid; gap: 14px; }
.history-days h3 { margin: 0 0 6px; font-size: .95rem; }
.history-links { display: flex; flex-wrap: wrap; gap: 6px; }
.history-links .puzzle-link-state { font-size: .75rem; color: var(--muted); }
.puzzle-title { text-align: center; }
h1 { font-size: clamp(1.5rem, 3vw, 2.2rem); margin: 0; line-height: 1.05; letter-spacing: -.04em; }
.puzzle-title p { color: var(--muted); font-size: .875rem; margin: 4px 0 0; }
h2 { margin: 0 0 12px; font-size: 1.125rem; }
.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--controls-width);
  align-items: center;
  gap: var(--play-gap);
  width: var(--layout-width);
  margin: auto;
}
.board-frame { width: 100%; aspect-ratio: 1; container-type: inline-size; }
#board, .ranked-grid { width: 100%; height: 100%; table-layout: fixed; border-collapse: collapse; border: 2px solid #93a3ab; }
#board tr, .ranked-grid tr { height: calc(100% / 9); }
#board td, .ranked-grid td { padding: 0; border: 1px solid var(--line); }
#board td:nth-child(3n), .ranked-grid td:nth-child(3n) { border-right: 2px solid #93a3ab; }
#board tr:nth-child(3n) td, .ranked-grid tr:nth-child(3n) td { border-bottom: 2px solid #93a3ab; }
.ranked-grid .cell { padding: 0; }
.ranked-grid .ranked-marks { font-size: min(.72rem, 2.5cqi); line-height: 1.2; }
#ranked-play .board-frame { width: 100%; }
#ranked-play .keypad { margin-top: 8px; }
.cell { position: relative; display: grid; place-items: center; width: 100%; height: 100%; padding: 0; border: 0; color: #b9e6f4; background: #172026; font-size: clamp(.9rem, 5.2cqi, 2rem); line-height: 1; font-weight: 650; }
.cell.given { color: #f5f8f8; font-weight: 800; }
.cell.related { background: #283738; }
.cell.same-digit:not(.conflict) { background: #3e4d31; box-shadow: inset 0 0 0 1px #a9bb79; }
.cell[aria-pressed="true"] { background: #344717; box-shadow: inset 0 0 0 2px var(--accent); }
.cell:focus-visible { outline-offset: -4px; }
.cell.conflict { color: #ffb2a7; background: #492828; }
.cell.conflict::after { content: '!'; position: absolute; font-size: .65em; right: 8%; top: 0; }
.pencil { position: absolute; inset: 4%; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); color: var(--muted); font-size: clamp(.4rem, 2.1cqi, .8rem); font-weight: 550; line-height: 1; align-items: center; }
.controls, .result, dialog { padding: 16px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); }
.keypad { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.keypad button, .actions button { min-height: 44px; padding: 4px; color: inherit; background: #28343c; border: 1px solid #53636c; border-radius: 7px; }
.keypad button { font-weight: 700; }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.actions button { padding-inline: 12px; }
.actions button[aria-pressed="true"] { color: #152006; background: var(--accent); border-color: var(--accent); }
#status, #storage, details p { color: var(--muted); font-size: .875rem; }
#account-status:empty { display: none; }
#account-status { color: var(--muted); font-size: .875rem; }
#account-resume { max-height: 80dvh; max-width: min(32rem, 92vw); overflow: auto; }
#status { min-height: 1lh; margin: 8px 0; }
.board-status { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 0 8px; }
#clock { color: var(--muted); font-size: .875rem; font-variant-numeric: tabular-nums; }
.support-actions { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: start; gap: 4px 12px; }
.support-actions details[open] { flex-basis: 100%; }
summary { padding-block: 9px; cursor: pointer; }
.text-button { min-height: 36px; padding: 0; border: 0; color: var(--muted); background: none; text-decoration: underline; text-underline-offset: 3px; }
.result { width: var(--layout-width); margin-inline: auto; }
.result h2 { color: var(--accent); }
textarea { display: block; width: min(100%, 340px); margin-top: 8px; }
dialog { width: min(90vw, 420px); color: inherit; }
dialog::backdrop { background: #07090ad9; }
dialog p:empty { display: none; }

@media (max-width: 640px), (orientation: portrait) and (max-width: 760px) {
  .shell {
    --gutter: clamp(8px, 3vw, 14px);
    --play-gap: 10px;
    --board-size: clamp(198px, calc(100dvh - 344px), 520px);
    --layout-width: min(100%, max(var(--board-size), 290px));
  }
  .play-layout { grid-template-columns: minmax(0, 1fr); margin: 0 auto auto; }
  .board-frame { width: min(100%, var(--board-size)); justify-self: center; }
}

@media (max-width: 430px), (orientation: landscape) and (max-height: 365px) and (min-width: 550px) {
  .play-header .back-link { min-width: 44px; font-size: 1.2rem; text-decoration: none; }
  .play-header .back-link span { display: none; }
  .settings-button { padding-inline: 4px; font-size: .8rem; }
  .puzzle-picker:has(#update-game:not([hidden])), .puzzle-picker:has(#update-game:not([hidden])) .difficulty-tabs { gap: 4px; }
  .puzzle-picker:has(#update-game:not([hidden])) :is(a, button) { padding-inline: 6px; font-size: .8rem; }
  #update-game span { display: none; }
}

@media (orientation: landscape) and (max-height: 520px) and (min-width: 550px) {
  .shell {
    --play-gap: 10px;
    --board-size: max(155px, calc(100dvh - 140px));
    --controls-width: clamp(260px, 35vw, 300px);
    --layout-width: min(100%, calc(var(--board-size) + var(--controls-width) + var(--play-gap)));
  }
  .play-header { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); }
  .puzzle-title { text-align: center; }
  .play-layout { grid-template-columns: minmax(0, 1fr) var(--controls-width); align-items: start; margin: auto; }
  .board-frame { width: 100%; }
}

@media (max-width: 640px), (orientation: portrait) and (max-width: 760px), (orientation: landscape) and (max-height: 520px) {
  .controls { padding: 10px; }
  .controls h2 { margin-bottom: 6px; font-size: 1rem; }
  .controls .actions { margin-top: 6px; gap: 6px; }
  .keypad { grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 4px; }
  .keypad button { grid-column: span 2; }
  .keypad button:nth-child(6) { grid-column: 2 / span 2; }
}

@media (orientation: landscape) and (max-height: 365px) and (min-width: 550px) {
  .shell { --board-size: max(155px, calc(100dvh - 90px)); }
  .play-header {
    width: 100%;
    grid-template-columns: 44px minmax(0, 1fr) auto auto;
    gap: 6px;
  }
  .puzzle-picker { grid-column: auto; }
  .puzzle-title p { font-size: .75rem; }
  .controls h2 {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }
  .support-actions { font-size: .875rem; }
}

.fit-portrait-keypad {
  --play-gap: 6px;
  --board-size: clamp(180px, var(--keypad-fit-board-size, 520px), 520px);
  --layout-width: 100%;
}
.fit-portrait-keypad .play-header {
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px;
  padding-bottom: 4px;
}
.fit-portrait-keypad .play-header > nav,
.fit-portrait-keypad .puzzle-title h1 { display: none; }
.fit-portrait-keypad .puzzle-title { grid-column: 1 / -1; text-align: start; }
.fit-portrait-keypad .puzzle-title p { margin: 0; }
.fit-portrait-keypad .puzzle-picker {
  grid-column: 1;
  grid-row: 2;
  justify-content: start;
  gap: 4px;
}
.fit-portrait-keypad .difficulty-tabs { gap: 4px; }
.fit-portrait-keypad :is(.difficulty-tabs a, #history-button, .settings-button) {
  min-width: 44px;
  justify-content: center;
  padding-inline: 4px;
  font-size: .8125rem;
}
.fit-portrait-keypad .settings-button { grid-column: 2; grid-row: 2; align-self: start; }
.fit-portrait-keypad.hosted-date .puzzle-title { display: none; }
.fit-portrait-keypad.hosted-date :is(.puzzle-picker, .settings-button) { grid-row: 1; }
.fit-portrait-keypad .controls { padding: 6px; }
.fit-portrait-keypad .controls h2 {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

.controls { container: keypad / inline-size; }
.keypad button { min-width: 44px; }
.digit-count {
  display: block;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 500;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
@container keypad (min-width: 428px) {
  .keypad.single-row { grid-template-columns: repeat(9, minmax(44px, 1fr)); gap: 4px; }
  .keypad.single-row button { grid-column: auto; }
}
#game-settings > label { display: block; margin-block: 8px; }
.practice-box {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  gap: 1px;
  width: fit-content;
  margin-block: 12px;
  background: var(--line);
  border: 1px solid var(--line);
}
.practice-box > * {
  display: grid;
  place-items: center;
  height: 48px;
  background: var(--panel);
  font-size: 1.25rem;
}
.practice-box .practice-note { place-items: start; padding: 4px; font-size: .8rem; }
