:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #1d2433;
  --muted: #6b7280;
  --line: #c9ced8;
  --line-strong: #1d2433;
  --given: #1d2433;
  --entry: #2563eb;
  --note: #4b5563;
  --select: #bcd6fb;
  --peer: #e6edf8;
  --conflict-bg: #fde0e0;
  --error: #d92626;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --success: #15803d;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 4px 16px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0e131a;
  --surface: #171e28;
  --surface-2: #1f2834;
  --text: #e4e8ef;
  --muted: #98a2b1;
  --line: #2f3a49;
  --line-strong: #8d98a8;
  --given: #e4e8ef;
  --entry: #7cb0ff;
  --note: #b3bdca;
  --select: #29466e;
  --peer: #1d2938;
  --conflict-bg: #4b1e25;
  --error: #ff7474;
  --accent: #4f8ff7;
  --accent-text: #ffffff;
  --success: #4ade80;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; touch-action: manipulation; }

[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px max(16px, env(safe-area-inset-left));
  max-width: 1040px;
  margin: 0 auto;
}

.brand { font-size: 1.4rem; margin: 0; letter-spacing: 0.02em; }

.topnav { display: flex; gap: 8px; align-items: center; }

/* ---------- Buttons ---------- */

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--error); border-color: var(--error); background: transparent; }
.btn-danger:hover { background: var(--conflict-bg); }
.btn-small { padding: 4px 10px; font-size: 0.85rem; }
.btn-icon { width: 38px; height: 38px; padding: 0; font-size: 1.15rem; display: inline-grid; place-items: center; }

:root[data-theme="dark"] .icon-moon,
:root:not([data-theme="dark"]) .icon-sun { display: none; }

/* ---------- Layout ---------- */

.view { max-width: 1040px; margin: 0 auto; padding: 0 16px 32px; }

.game-layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.board-column, .controls-column { width: min(94vw, 520px); }

@media (min-width: 900px) {
  .game-layout { flex-direction: row; align-items: flex-start; justify-content: center; gap: 32px; padding-top: 8px; }
  .board-column { width: 520px; }
  .controls-column { width: 340px; padding-top: 44px; }
}

/* ---------- Status bar ---------- */

.statusbar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 10px;
  min-height: 32px;
}
.chip {
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px 12px;
  font-weight: 600;
  font-size: 0.9rem;
}
.stat { font-size: 0.95rem; }
.stat-label { color: var(--muted); }
.timer { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 1.05rem; }

/* ---------- Board ---------- */

.board-wrap {
  --board: min(94vw, 520px);
  position: relative;
  width: var(--board);
  height: var(--board);
}
@media (min-width: 900px) { .board-wrap { --board: 520px; } }

.board {
  --cell: calc(var(--board) / 9);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: 100%;
  height: 100%;
  background: var(--surface);
  border: 2.5px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  outline: none;
  transition: background 0.1s;
}
.cell.c-edge { border-right: 2.5px solid var(--line-strong); }
.cell.r-edge { border-bottom: 2.5px solid var(--line-strong); }
.cell.c-last { border-right: none; }
.cell.r-last { border-bottom: none; }

.cell.peer { background: var(--peer); }
.cell.selected { background: var(--select); }
.cell.conflict { background: var(--conflict-bg); }
.cell.selected.conflict { background: var(--select); box-shadow: inset 0 0 0 2px var(--error); }

.value {
  font-size: calc(var(--cell) * 0.58);
  line-height: 1;
  font-weight: 500;
  color: var(--entry);
}
.cell.given .value { color: var(--given); font-weight: 700; }
.cell.conflict .value,
.cell.wrong .value { color: var(--error); }
.cell.wrong:not(.given) .value { text-decoration: underline wavy var(--error) 1.5px; text-underline-offset: 3px; }

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}
.notes span {
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.28);
  line-height: 1;
  color: var(--note);
  font-weight: 600;
}
.notes span.not {
  color: var(--error);
  font-weight: 700;
  text-decoration: line-through;
  text-decoration-thickness: 2px;
}

.board.readonly .cell { cursor: default; }
.board.blurred { filter: blur(10px); }

.board-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface) 55%, transparent);
  font-size: 1.2rem;
  font-weight: 600;
}
.board-overlay p { margin: 0; }

.banner {
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.banner.success { color: var(--success); }

/* ---------- Controls ---------- */

.modes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.mode {
  border: 2px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 8px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.mode-title { font-weight: 700; }
.mode-hint { font-size: 0.72rem; color: var(--muted); }
.mode.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.mode-not.active { border-color: var(--error); background: color-mix(in srgb, var(--error) 12%, var(--surface)); }

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
@media (min-width: 900px) { .numpad { grid-template-columns: repeat(3, 1fr); gap: 8px; } }

.num {
  position: relative;
  aspect-ratio: 1 / 1.15;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  cursor: pointer;
  font-size: clamp(1.2rem, 5vw, 1.7rem);
  font-weight: 600;
  color: var(--entry);
  display: grid;
  place-items: center;
  box-shadow: var(--shadow);
}
@media (min-width: 900px) { .num { aspect-ratio: 1.4 / 1; } }
.num:hover { background: var(--surface-2); }
.num .remaining {
  position: absolute;
  bottom: 3px;
  right: 6px;
  font-size: 0.65rem;
  color: var(--muted);
  font-weight: 500;
}
.num.done { opacity: 0.35; }
.numpad.mode-maybe .num { color: var(--note); font-size: clamp(1rem, 4vw, 1.3rem); }
.numpad.mode-not .num { color: var(--error); font-size: clamp(1rem, 4vw, 1.3rem); text-decoration: line-through; }

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

.kbd-help { font-size: 0.78rem; color: var(--muted); margin-top: 14px; line-height: 1.5; }
@media (hover: none) { .kbd-help { display: none; } }

/* ---------- History ---------- */

.history-header { display: flex; align-items: center; gap: 16px; margin: 8px 0 16px; flex-wrap: wrap; }
.history-header h2 { margin: 0; font-size: 1.3rem; }

.stats-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 16px;
}
.stats-card h3 { margin: 0 0 8px; font-size: 1rem; }
.stats-table-wrap { overflow-x: auto; }
.stats-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.stats-table th, .stats-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.stats-table th { color: var(--muted); font-weight: 600; }
.stats-table tr:last-child td { border-bottom: none; }

.history-filters { display: flex; gap: 6px; margin-bottom: 12px; }
.filter {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
}
.filter.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.history-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  cursor: pointer;
  border: 2px solid transparent;
}
.history-item:hover { border-color: var(--line); }
.history-item .level-badge {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: grid; place-items: center;
  font-weight: 700;
  background: var(--surface-2);
}
.history-item .title { font-weight: 600; }
.history-item .meta { font-size: 0.85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.status-pill { font-size: 0.78rem; font-weight: 600; padding: 3px 10px; border-radius: 999px; white-space: nowrap; }
.status-pill.completed { background: color-mix(in srgb, var(--success) 16%, transparent); color: var(--success); }
.status-pill.unfinished { background: var(--surface-2); color: var(--muted); }
.status-pill.current { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }

.detail-layout { display: flex; flex-wrap: wrap; gap: 24px; align-items: flex-start; }
.detail-board-wrap { --board: min(94vw, 460px); }
.detail-info { flex: 1; min-width: 240px; }
.detail-stats { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0 0 16px; }
.detail-stats dt { color: var(--muted); }
.detail-stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.detail-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 0.85rem; color: var(--muted); margin-bottom: 16px; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.swatch-given { background: var(--given); }
.swatch-entry { background: var(--entry); }
.swatch-wrong { background: var(--error); }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Dialogs ---------- */

.dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  width: min(92vw, 460px);
  max-height: 90vh;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.dialog-small { width: min(92vw, 380px); }
.dialog::backdrop { background: rgba(10, 15, 25, 0.5); }
.dialog form { padding: 20px 22px; }
.dialog h2 { margin: 0 0 6px; font-size: 1.25rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.levels { display: grid; gap: 8px; margin: 14px 0; }
.level-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  text-align: left;
}
.level-btn:hover { border-color: var(--accent); background: var(--surface-2); }
.level-num {
  width: 32px; height: 32px;
  border-radius: 8px;
  display: grid; place-items: center;
  background: var(--accent);
  color: var(--accent-text);
}

fieldset { border: none; padding: 0; margin: 16px 0 0; }
legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 6px 0; cursor: pointer; line-height: 1.35; }
.check input { margin-top: 3px; width: 17px; height: 17px; accent-color: var(--accent); flex-shrink: 0; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 7px 16px; }
.segmented input:checked + span { background: var(--accent); color: var(--accent-text); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- Generating overlay & toast ---------- */

.generating {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  z-index: 50;
  font-weight: 600;
}
.spinner {
  width: 36px; height: 36px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 480px) {
  .topbar { padding: 10px 12px; }
  .brand { font-size: 1.2rem; }
  .topnav { gap: 6px; }
  .topnav .btn:not(.btn-icon) { padding: 7px 10px; font-size: 0.9rem; }
  .btn-icon { width: 34px; height: 34px; }
  .view { padding: 0 10px 24px; }
  .statusbar { gap: 10px; }
}
