:root {
  --bg: #e7dcc8;
  --card: #fbf7f0;
  --ink: #1d1914;
  --muted: #5e564c;
  --line: #241e18;
  --line-soft: rgba(36, 30, 24, 0.28);
  --accent: #0e5f5c;
  --accent-ink: #f4fffd;
  --entry: #0e5f5c;
  --note: #5c5348;
  --select: rgba(14, 95, 92, 0.18);
  --peer: rgba(14, 95, 92, 0.07);
  --same: rgba(14, 95, 92, 0.13);
  --error-bg: #f6d2cc;
  --error-ink: #8d1c1c;
  --btn: #fbf7f0;
  --btn-border: rgba(36, 30, 24, 0.18);
  --shadow: 0 12px 32px rgba(70, 48, 24, 0.08);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #12161c;
  --card: #1c242f;
  --ink: #f3eee4;
  --muted: #c9bdae;
  --line: #f0e7da;
  --line-soft: rgba(240, 231, 218, 0.24);
  --accent: #79d2c7;
  --accent-ink: #08211f;
  --entry: #b7f3ea;
  --note: #ddd2c4;
  --select: rgba(121, 210, 199, 0.22);
  --peer: rgba(121, 210, 199, 0.08);
  --same: rgba(121, 210, 199, 0.16);
  --error-bg: #5c2b2b;
  --error-ink: #ffd2cb;
  --btn: #243140;
  --btn-border: rgba(243, 238, 228, 0.16);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(900px 420px at 0% -10%, rgba(255, 255, 255, 0.28), transparent 55%),
    var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Roboto, "Noto Sans", "Liberation Sans", "DejaVu Sans", Arial, sans-serif;
  touch-action: manipulation;
  overscroll-behavior: none;
  user-select: none;
}

:root[data-theme="dark"] body {
  background:
    radial-gradient(800px 380px at 100% -10%, rgba(121, 210, 199, 0.08), transparent 50%),
    var(--bg);
}

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

button {
  cursor: pointer;
  border: 1px solid var(--btn-border);
  background: var(--btn);
  border-radius: 12px;
  min-height: 44px;
  padding: 8px 12px;
}

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

button:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}

button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
  font-weight: 700;
}

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

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 10px 14px 4px;
}

.brand { flex: 1 1 140px; min-width: 0; }

.brand h1 {
  margin: 0;
  font-family: Palatino, "Palatino Linotype", Georgia, "Liberation Serif", "Nimbus Roman", "DejaVu Serif", serif;
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
}

.top-actions button {
  font-size: 0.92rem;
  padding: 8px 10px;
  white-space: nowrap;
}

.tag {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.timer {
  margin: 0;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.15rem;
  min-width: 3.4rem;
  text-align: right;
}

.top-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.stage {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 6px 12px 14px;
  overflow: auto;
}

.board-wrap {
  width: min(560px, 100%);
  max-width: 100%;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  container-type: inline-size;
  overflow: hidden;
}

.cell {
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line-soft);
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 700;
  font-size: 6.1cqi;
  font-variant-numeric: tabular-nums;
  display: grid;
  place-items: center;
  min-height: 0;
  position: relative;
}

.cell.c2, .cell.c5 { border-right: 2.5px solid var(--line); }
.cell.c8 { border-right: 0; }
.cell.r2, .cell.r5 { border-bottom: 2.5px solid var(--line); }
.cell.r8 { border-bottom: 0; }

.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--select); box-shadow: inset 0 0 0 2px var(--accent); z-index: 1; }
.cell.entry { color: var(--entry); }
.cell.given { color: var(--ink); }
.cell.error { background: var(--error-bg); color: var(--error-ink); }

.notes {
  width: 92%;
  height: 92%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: 2.05cqi;
  font-weight: 600;
  color: var(--note);
  line-height: 1;
}

.notes i {
  font-style: normal;
  display: grid;
  place-items: center;
}

.dots {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}

.dot {
  position: absolute;
  width: 5.1%;
  height: 5.1%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  box-sizing: border-box;
  border: 0;
}

.dot.white,
.swatch.white {
  background: radial-gradient(circle at center, #fffdf8 0 64%, #1c1915 66% 100%);
  border: 0;
}

.dot.black,
.swatch.black {
  background: radial-gradient(circle at center, #0b0b0b 0 60%, #f7f3ea 62% 100%);
  border: 0;
}

.controls {
  width: min(560px, 100%);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.pad button {
  min-height: 54px;
  font-size: 1.45rem;
  font-weight: 750;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  line-height: 1;
  padding: 6px 4px 4px;
}

.pad button small {
  font-size: 0.68rem;
  font-weight: 650;
  color: var(--muted);
  letter-spacing: 0.02em;
}

body.notes-on #btn-notes {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.actions button { font-weight: 680; min-height: 48px; }

.legend {
  margin: 2px 0 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  color: var(--muted);
  font-size: 0.82rem;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-sizing: border-box;
  display: inline-block;
}

.swatch.white,
.swatch.black { width: 16px; height: 16px; }

.empty-hint, .win-banner {
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
  grid-column: 1 / -1;
}

.win-banner {
  color: var(--accent);
  font-weight: 750;
}

dialog {
  border: none;
  border-radius: 18px;
  background: var(--card);
  color: var(--ink);
  padding: 18px 18px 16px;
  width: min(460px, calc(100vw - 24px));
  max-height: min(88dvh, 760px);
  overflow: auto;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}

dialog::backdrop { background: rgba(18, 14, 10, 0.48); }

.empty-hint[hidden],
.win-banner[hidden],
.warn[hidden] { display: none !important; }

dialog h2 {
  margin: 0 0 8px;
  font-family: Palatino, "Palatino Linotype", Georgia, "Liberation Serif", serif;
  font-size: 1.55rem;
}

dialog h3 { margin: 14px 0 6px; font-size: 1rem; }

dialog p, dialog li { line-height: 1.45; }

.levels {
  display: grid;
  gap: 8px;
  margin: 12px 0;
}

.levels button {
  min-height: 52px;
  font-size: 1.05rem;
  font-weight: 750;
  text-align: left;
  padding-inline: 14px;
}

.warn {
  margin: 0 0 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--error-bg);
  color: var(--error-ink);
}

.callout {
  margin: 10px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--select);
  border: 1px solid var(--btn-border);
}

.callout h3 { margin-top: 0; }

fieldset {
  border: 1px solid var(--btn-border);
  border-radius: 12px;
  margin: 12px 0;
  padding: 8px 12px 10px;
}

fieldset label, .switch {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
}

.stats {
  margin: 0;
  padding-left: 18px;
}

.stats li { margin: 4px 0; }

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

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.loading {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  color: var(--ink);
}

.loading[hidden] { display: none; }

.spinner {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 3px solid var(--line-soft);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
}

@media (min-aspect-ratio: 4/5) {
  .stage {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 18px;
    padding: 8px 16px 16px;
  }

  .board-wrap { width: min(68dvh, calc(100dvw - 330px)); }
  .controls { width: min(300px, 34vw); }
}

body[data-layout="stack"] .stage {
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

body[data-layout="stack"] .board-wrap,
body[data-layout="stack"] .controls {
  width: min(560px, 100%);
}

body[data-layout="split"] .stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(210px, 280px);
  gap: 16px;
  align-items: center;
  justify-content: stretch;
  padding: 8px 16px 16px;
}

body[data-layout="split"] .board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

body[data-layout="split"] .board {
  width: min(100%, calc(100dvh - 120px));
}

body[data-layout="split"] .controls {
  width: 100%;
  justify-content: center;
}

.noscript {
  margin: 12px;
  padding: 12px;
  background: var(--card);
  border-radius: 12px;
}
