:root {
  --bg: #f2f4f8;
  --surface: #ffffff;
  --text: #1c2733;
  --muted: #7a8a9a;
  --line: #d5dce4;
  --line-strong: #9aa7b4;
  --accent: #3b82f6;
  --accent-dark: #2563eb;
  --given: #1c2733;
  --user: #3b82f6;
  --error: #ef4444;
  --highlight: #eaf1fe;
  --peer: #f7fafd;
  --same: #e3edfc;
  --ok: #16a34a;
  color-scheme: light;
}

[data-theme='dark'] {
  --bg: #0f141b;
  --surface: #192029;
  --text: #e6ebf2;
  --muted: #8b99ab;
  --line: #2b3644;
  --line-strong: #43516a;
  --accent: #3b82f6;
  --accent-dark: #60a5fa;
  --given: #e6ebf2;
  --user: #7cb0ff;
  --error: #f87171;
  --highlight: #233046;
  --peer: #1f2833;
  --same: #263850;
  --ok: #4ade80;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  min-height: 100vh;
}

.app {
  width: min(460px, 100vw - 24px);
  padding: 24px 0 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.top {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.controls label {
  color: var(--muted);
  font-size: 14px;
}

select,
button {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

select:hover,
button:hover { border-color: var(--line-strong); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.primary:hover { background: var(--accent-dark); }

.board {
  display: grid;
  grid-template-columns: repeat(9, 44px);
  grid-template-rows: repeat(9, 44px);
  gap: 0;
  background: var(--line-strong);
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  overflow: hidden;
  user-select: none;
  touch-action: manipulation;
}

.cell {
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  cursor: pointer;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.cell:nth-child(9n) { border-right: none; }
.cell:nth-last-child(-n + 9) { border-bottom: none; }

.cell.col3 { border-right: 2px solid var(--line-strong); }
.cell.row3 { border-bottom: 2px solid var(--line-strong); }

.cell.given { color: var(--given); font-weight: 600; }
.cell.user { color: var(--user); }

.cell.selected { background: var(--highlight); }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.error { color: var(--error); }
.cell.win { color: var(--ok); }

.numpad {
  display: grid;
  grid-template-columns: repeat(5, 64px);
  gap: 8px;
}

.numpad button {
  padding: 12px 0;
  font-size: 18px;
  font-weight: 600;
  border-radius: 10px;
}

.numpad button.ghost { color: var(--muted); font-size: 16px; }

.numpad button.done { opacity: 0.25; pointer-events: none; }

.stats {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.stats-title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stats-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.status {
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px 12px;
  font-size: 14px;
  color: var(--muted);
  min-height: 20px;
}

#message.ok { color: var(--ok); }
#message.bad { color: var(--error); }

.theme-toggle {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.theme-btn {
  padding: 8px 10px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
}

.theme-btn + .theme-btn { border-left: 1px solid var(--line); }

.theme-btn:hover { background: var(--peer); border-color: var(--line); color: var(--text); }

.theme-btn.active {
  background: var(--accent);
  color: #fff;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 28, 38, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.overlay.hidden { display: none; }

.dialog {
  background: var(--surface);
  border-radius: 14px;
  padding: 28px 32px;
  text-align: center;
  max-width: 320px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.dialog h2 { margin: 0 0 8px; }
.dialog p { margin: 0 0 20px; color: var(--muted); }

@media (max-width: 460px) {
  .board { grid-template-columns: repeat(9, 10.5vw); grid-template-rows: repeat(9, 10.5vw); }
  .cell { font-size: 5vw; }
  .numpad { grid-template-columns: repeat(5, 17vw); }
}
