/* ---------- Theme variables ---------- */
:root, [data-theme="light"] {
  --bg: #f3f6fa;
  --surface: #ffffff;
  --text: #344861;
  --text-dim: #94a3b7;
  --accent: #325aaf;
  --accent-soft: #dce3f1;
  --grid-line: #bfc6d4;
  --grid-line-strong: #344861;
  --cell-selected: #bbdefb;
  --cell-related: #e2ebf3;
  --cell-same: #c3d7ea;
  --cell-conflict-bg: #f7cfd6;
  --user-value: #325aaf;
  --wrong-value: #e55c6c;
  --note-color: #6e7c8c;
  --btn-bg: #eaeef4;
  --btn-hover: #dce3f1;
  --shadow: 0 4px 16px rgba(52, 72, 97, .12);
}
[data-theme="dark"] {
  --bg: #10151c;
  --surface: #1a2028;
  --text: #d7e0ea;
  --text-dim: #6b7787;
  --accent: #6ea8fe;
  --accent-soft: #263650;
  --grid-line: #3a4553;
  --grid-line-strong: #8fa2b8;
  --cell-selected: #2e4a6b;
  --cell-related: #212a35;
  --cell-same: #2a3f58;
  --cell-conflict-bg: #4a2630;
  --user-value: #6ea8fe;
  --wrong-value: #ff7a8a;
  --note-color: #93a3b5;
  --btn-bg: #232c37;
  --btn-hover: #2c3846;
  --shadow: 0 4px 16px rgba(0, 0, 0, .4);
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* ---------- Header ---------- */
.header {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 900px; margin: 0 auto; padding: 14px 20px 0;
}
.logo { font-size: 26px; font-weight: 700; margin: 0; color: var(--accent); letter-spacing: .5px; }
.header-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--btn-bg); color: var(--text);
  transition: background .15s;
}
.icon-btn:hover { background: var(--btn-hover); }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: none; }

/* ---------- Layout ---------- */
.layout {
  display: flex; gap: 28px; justify-content: center; align-items: flex-start;
  max-width: 900px; margin: 0 auto; padding: 16px 20px 40px;
}
.board-panel { flex: 0 1 500px; min-width: 0; }
.controls-panel { flex: 0 0 260px; display: flex; flex-direction: column; gap: 16px; padding-top: 42px; }

/* ---------- Status bar ---------- */
.statusbar { display: flex; justify-content: space-between; padding: 0 2px 8px; }
.status-item { display: flex; flex-direction: column; gap: 1px; }
.status-label { font-size: 12px; color: var(--text-dim); }
.status-value { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.pause-btn {
  width: 22px; height: 22px; border-radius: 50%;
  display: inline-grid; place-items: center;
  background: var(--btn-bg); color: var(--text-dim);
}
.pause-btn:hover { background: var(--btn-hover); }
.pause-btn .icon-play { display: none; }
.paused .pause-btn .icon-play { display: block; }
.paused .pause-btn .icon-pause { display: none; }

/* ---------- Board ---------- */
.board-wrap { position: relative; }
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1;
  width: 100%;
  background: var(--surface);
  border: 2px solid var(--grid-line-strong);
  border-radius: 6px;
  overflow: hidden;
  user-select: none;
}
.cell {
  position: relative;
  display: grid; place-items: center;
  font-size: clamp(18px, 4.2vmin, 30px);
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
  color: var(--text);
  cursor: pointer;
}
.cell:nth-child(9n) { border-right: none; }
.cell:nth-child(n+73) { border-bottom: none; }
.cell.box-right { border-right: 2px solid var(--grid-line-strong); }
.cell.box-bottom { border-bottom: 2px solid var(--grid-line-strong); }

.cell.related { background: var(--cell-related); }
.cell.same { background: var(--cell-same); }
.cell.conflict { background: var(--cell-conflict-bg); }
.cell.selected { background: var(--cell-selected); }
.cell.user { color: var(--user-value); }
.cell.wrong { color: var(--wrong-value); }

.cell .notes {
  position: absolute; inset: 0;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: clamp(8px, 1.5vmin, 12px);
  color: var(--note-color); line-height: 1;
}
.cell .notes span { display: grid; place-items: center; }

@keyframes pop { 0% { transform: scale(.6); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }
.score-value { color: var(--accent); }
@keyframes score-bump { 0% { transform: scale(1); } 40% { transform: scale(1.35); } 100% { transform: scale(1); } }
.score-value.bump { animation: score-bump .3s ease-out; display: inline-block; }
.cell.pop > .val { animation: pop .18s ease-out; display: block; }

/* ---------- Pause overlay ---------- */
.pause-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: var(--surface);
  border-radius: 6px; border: 2px solid var(--grid-line-strong);
}
.resume-btn {
  width: 76px; height: 76px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  box-shadow: var(--shadow);
}

/* ---------- Controls ---------- */
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool-btn {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 4px 8px; border-radius: 10px;
  background: var(--btn-bg); color: var(--text-dim);
  font-size: 12px;
  transition: background .15s, color .15s;
}
.tool-btn:hover { background: var(--btn-hover); }
.tool-btn.active { color: var(--accent); background: var(--accent-soft); }
.badge {
  position: absolute; top: 2px; right: 2px;
  font-size: 9px; font-weight: 700;
  background: var(--text-dim); color: var(--surface);
  border-radius: 8px; padding: 2px 5px;
}
.tool-btn.active .badge { background: var(--accent); }
.badge-count { background: var(--accent); }

.numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.num-btn {
  aspect-ratio: 1.35;
  font-size: 26px; font-weight: 500;
  border-radius: 10px;
  background: var(--btn-bg); color: var(--accent);
  transition: background .15s;
  position: relative;
}
.num-btn:hover { background: var(--btn-hover); }
.num-btn.done { opacity: 0; pointer-events: none; }
.num-btn .remaining {
  position: absolute; bottom: 3px; right: 6px;
  font-size: 10px; color: var(--text-dim); font-weight: 600;
}

.primary-btn {
  padding: 14px; border-radius: 10px;
  background: var(--accent); color: #fff;
  font-size: 16px; font-weight: 600;
  transition: filter .15s;
}
.primary-btn:hover { filter: brightness(1.1); }

/* ---------- Popover ---------- */
.popover {
  position: absolute; z-index: 30;
  background: var(--surface); border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px; min-width: 200px;
  border: 1px solid var(--grid-line);
}
.popover-title { font-size: 12px; color: var(--text-dim); padding: 6px 10px 4px; }
.popover-item {
  display: block; width: 100%; text-align: left;
  padding: 9px 10px; border-radius: 6px; font-size: 15px;
  color: var(--text);
}
.popover-item:hover { background: var(--btn-hover); }
.popover-restart { border-top: 1px solid var(--grid-line); border-radius: 0 0 6px 6px; margin-top: 4px; color: var(--text-dim); font-size: 13px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .45);
  display: grid; place-items: center;
}
.modal {
  background: var(--surface); color: var(--text);
  border-radius: 14px; padding: 26px 28px;
  width: min(92vw, 380px);
  box-shadow: var(--shadow);
  text-align: center;
}
.modal h2 { margin: 0 0 10px; }
.modal-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 18px; }
.modal-actions .primary-btn { width: 100%; }
.modal-actions .secondary-btn {
  padding: 12px; border-radius: 10px;
  background: var(--btn-bg); color: var(--text); font-size: 15px;
}
.modal-actions .secondary-btn:hover { background: var(--btn-hover); }
.modal-actions .danger-btn {
  padding: 12px; border-radius: 10px;
  background: transparent; color: var(--wrong-value);
  border: 1px solid var(--wrong-value); font-size: 15px;
}
.modal-actions .danger-btn:hover { background: var(--cell-conflict-bg); }

.privacy-note { font-size: 12px; line-height: 1.5; color: var(--text-dim); margin-top: 14px; text-align: left; }
[data-theme="dark"] .privacy-note { color: #ffffff; }

/* ---------- Footer ---------- */
.footer {
  max-width: 900px; margin: 0 auto; padding: 0 20px 32px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  text-align: center;
}
.footer .privacy-note { margin-top: 0; text-align: center; max-width: 480px; }
.delete-data-btn {
  padding: 9px 16px; border-radius: 8px;
  background: transparent; color: var(--wrong-value);
  border: 1px solid var(--wrong-value); font-size: 13px;
  transition: background .15s;
}
.delete-data-btn:hover { background: var(--cell-conflict-bg); }

.stats-table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.stats-table th, .stats-table td { padding: 7px 4px; font-size: 14px; }
.stats-table th { color: var(--text-dim); font-weight: 500; text-align: left; font-size: 12px; }
.stats-table td:first-child { text-align: left; font-weight: 600; }
.stats-table td { text-align: center; }
.stats-table tr + tr { border-top: 1px solid var(--grid-line); }

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .layout { flex-direction: column; align-items: stretch; gap: 14px; padding-bottom: 24px; }
  .controls-panel { flex: none; padding-top: 0; }
  .board-panel { flex: none; }
  .tools { grid-template-columns: repeat(4, 1fr); }
  .numpad { grid-template-columns: repeat(9, 1fr); gap: 4px; }
  .num-btn { aspect-ratio: auto; padding: 10px 0; font-size: 20px; }
  .num-btn .remaining { display: none; }
}
