:root {
  --bg: #12151c;
  --panel: #1b202b;
  --line: #2f3746;
  --line-strong: #6b7789;
  --text: #e7ebf3;
  --muted: #9aa5b8;
  --accent: #4f8cff;
  --given: #cdd6e6;
  --user: #7fb0ff;
  --bad: #ff5f6d;
  --ok: #3ddc97;
  --sel: #2a3a58;
  --peer: #212a39;
  --same: #2c3d33;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa;
    --panel: #ffffff;
    --line: #cfd6e2;
    --line-strong: #465060;
    --text: #171b23;
    --muted: #5a6478;
    --given: #171b23;
    --user: #1f5fd0;
    --sel: #d5e3ff;
    --peer: #eaeff8;
    --same: #dcf0e4;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 12px 12px 32px;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

header {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

h1 { font-size: 1.4rem; margin: 0; letter-spacing: .02em; }

.status { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .95rem; }
.status .dot { opacity: .5; margin: 0 6px; }

.toolbar {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: end;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: .72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  flex: 1 1 100px;
}

select, button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  cursor: pointer;
}

select { width: 100%; }

button:hover { border-color: var(--line-strong); }
button:active { transform: translateY(1px); }

button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .45; cursor: default; }

main { width: 100%; display: flex; justify-content: center; }

#board-wrap { position: relative; }

#board {
  display: grid;
  grid-template-columns: repeat(var(--n, 9), 1fr);
  width: min(94vw, 540px);
  aspect-ratio: 1 / 1;
  background: var(--panel);
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  font-size: calc(min(94vw, 540px) / var(--n, 9) * 0.56);
  line-height: 1;
  user-select: none;
  cursor: pointer;
  color: var(--user);
}

.cell.r0 { border-top: none; }
.cell.c0 { border-left: none; }
.cell.bt { border-top: 2px solid var(--line-strong); }
.cell.bl { border-left: 2px solid var(--line-strong); }

.cell.given { color: var(--given); font-weight: 700; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.bad { color: var(--bad); }
.cell.bad.given { color: var(--bad); }

.cell .notes {
  position: absolute;
  inset: 6%;
  display: grid;
  grid-template-columns: repeat(var(--bw, 3), 1fr);
  font-size: calc(min(94vw, 540px) / var(--n, 9) * 0.20);
  color: var(--muted);
  line-height: 1;
  pointer-events: none;
}
.cell .notes span { display: flex; align-items: center; justify-content: center; }

#overlay[hidden] { display: none; }

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .55);
  color: #fff;
  border-radius: 6px;
  font-size: 1.2rem;
  font-weight: 600;
  text-align: center;
  padding: 16px;
}

#pad {
  width: min(94vw, 540px);
  display: grid;
  grid-template-columns: repeat(var(--pad, 9), 1fr);
  gap: 6px;
}

#pad button {
  padding: 10px 0;
  font-size: 1.05rem;
  font-weight: 600;
}

#pad button.full { opacity: .4; }

#msg {
  max-width: 560px;
  min-height: 1.2em;
  margin: 0;
  text-align: center;
  color: var(--ok);
  font-weight: 600;
}
#msg.warn { color: var(--bad); }

footer {
  max-width: 560px;
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
  line-height: 1.5;
}

kbd {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: .9em;
}
