:root {
  --bg: #f4f0e6;
  --paper: #fffdf7;
  --ink: #1d1b16;
  --muted: #736c5e;
  --line: #d8d0bf;
  --accent: #2c5a86;
  --peer: #eee7d7;
  --selected: #cfdff0;
  --same: #dfe8f1;
  --bad: #b0271f;
  --bad-bg: #f6dcd6;
  --good: #3b7a4a;
  --serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16150f;
    --paper: #1f1d16;
    --ink: #ece6d6;
    --muted: #9a9281;
    --line: #3a372c;
    --accent: #8db8e3;
    --peer: #29271f;
    --selected: #2e4257;
    --same: #263341;
    --bad: #f08b80;
    --bad-bg: #4a2420;
    --good: #86c796;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  padding-inline: 16px;
  -webkit-tap-highlight-color: transparent;
}

main {
  width: min(100%, 480px);
  margin: 0 auto;
  padding-block: 28px 40px;
}

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

button:disabled {
  cursor: default;
}

header {
  text-align: center;
  margin-bottom: 20px;
}

h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 2.1rem;
  letter-spacing: -0.01em;
  margin: 0;
}

.date {
  margin: 4px 0 0;
  color: var(--muted);
  font-family: var(--serif);
  font-style: italic;
}

/* Difficulty tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1.5px solid var(--ink);
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 14px;
}

.tab {
  background: var(--paper);
  border: 0;
  border-left: 1px solid var(--line);
  padding: 8px 4px 7px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.tab:first-child {
  border-left: 0;
}

.tab[aria-selected='true'] {
  background: var(--ink);
  color: var(--bg);
}

.tab-meta {
  font-size: 0.72rem;
  font-weight: 400;
  opacity: 0.7;
  font-variant-numeric: tabular-nums;
}

.tab.solved .tab-meta {
  color: var(--good);
  opacity: 1;
  font-weight: 600;
}

.tab.solved[aria-selected='true'] .tab-meta {
  color: inherit;
}

.bar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
  min-height: 1.4em;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.status {
  color: var(--muted);
  font-size: 0.9rem;
}

/* Board */
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1;
  width: 100%;
  max-width: 100%;
  border: 2.5px solid var(--ink);
  border-radius: 4px;
  background: var(--paper);
  user-select: none;
  touch-action: manipulation;
}

.cell {
  position: relative;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  padding: 0;
  font-size: clamp(1.1rem, 5.6vw, 1.75rem);
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.cell:nth-child(9n) {
  border-right: 0;
}

.cell:nth-child(n + 73) {
  border-bottom: 0;
}

.cell.bx {
  border-right: 2px solid var(--ink);
}

.cell.by {
  border-bottom: 2px solid var(--ink);
}

.cell:focus {
  outline: none;
}

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

.cell.given {
  font-weight: 700;
}

.cell.entry {
  color: var(--accent);
}

.cell.peer {
  background: var(--peer);
}

.cell.same {
  background: var(--same);
}

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

.cell.conflict {
  color: var(--bad);
}

.cell.conflict.entry {
  background: var(--bad-bg);
}

.cell.done {
  color: var(--ink);
  cursor: default;
}

.cell.done.entry {
  color: var(--good);
}

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: clamp(0.5rem, 2.2vw, 0.68rem);
  color: var(--muted);
  font-weight: 500;
}

.notes span {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Controls */
.controls {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.tool {
  flex: 1;
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: 9px;
  font-weight: 500;
}

.tool[aria-pressed='true'] {
  border-color: var(--accent);
  background: var(--selected);
}

.tool:disabled,
.pad button:disabled {
  opacity: 0.4;
}

kbd {
  font-family: var(--sans);
  font-size: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  margin-left: 4px;
  color: var(--muted);
}

@media (pointer: coarse) {
  kbd {
    display: none;
  }
}

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

.pad button {
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: 10px 0;
  font-size: 1.3rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.pad button:active:not(:disabled),
.tool:active:not(:disabled) {
  background: var(--selected);
}

.pad button.complete {
  color: var(--muted);
  opacity: 0.45;
}

/* Stats */
.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 16px;
  margin-top: 28px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.stats div {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.stat-num {
  font-family: var(--serif);
  font-size: 1.7rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  color: var(--muted);
  font-size: 0.88rem;
}

.stat-wide {
  grid-column: 1 / -1;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}

footer {
  margin-top: 22px;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}

footer p {
  margin: 4px 0;
}

/* Win dialog */
.win {
  border: 1.5px solid var(--ink);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  padding: 22px 24px;
  width: min(calc(100% - 32px), 340px);
  text-align: center;
}

.win::backdrop {
  background: rgb(0 0 0 / 0.35);
}

.win-title {
  font-family: var(--serif);
  font-size: 1.8rem;
  font-weight: 600;
  margin: 0 0 6px;
}

.win p {
  margin: 4px 0;
}

.muted {
  color: var(--muted);
}

.win-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

.win-actions button {
  border: 1.5px solid var(--ink);
  background: transparent;
  border-radius: 8px;
  padding: 8px 16px;
  font-weight: 600;
}

.win-actions .primary {
  background: var(--ink);
  color: var(--bg);
}
