/* ============================================================
   Sudoku — clean, rounded, responsive, offline
   ============================================================ */

/* ---------- Theme variables ---------- */
:root,
[data-theme="light"] {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f1f5fb;
  --text: #101a2e;
  --muted: #5b6b84;
  --accent: #2563eb;
  --accent-soft: #dbe7ff;
  --accent-contrast: #ffffff;
  --given: #1e293b;
  --entered: #2563eb;
  --line: #d9e1ec;
  --line-strong: #9fadc6;
  --peer: #eef4ff;
  --selected-bg: #dbe7ff;
  --same-bg: #e2ecff;
  --wrong: #dc2626;
  --wrong-bg: #fdecec;
  --hint-bg: #dcfce7;
  --hint-color: #15803d;
  --shadow: 0 12px 32px rgba(16, 26, 46, 0.10);
  --overlay: rgba(16, 26, 46, 0.45);
}

[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #16213a;
  --surface-2: #1d2b4a;
  --text: #e8edf8;
  --muted: #93a4c4;
  --accent: #60a5fa;
  --accent-soft: #1e3a5f;
  --accent-contrast: #0b1220;
  --given: #d7e0f0;
  --entered: #6cb2ff;
  --line: #243354;
  --line-strong: #41557f;
  --peer: #1a2745;
  --selected-bg: #24406f;
  --same-bg: #21365c;
  --wrong: #ff7a7a;
  --wrong-bg: #4a2027;
  --hint-bg: #17452a;
  --hint-color: #4ade80;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.40);
  --overlay: rgba(3, 7, 16, 0.62);
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
  transition: background 0.2s ease, color 0.2s ease;
}

button {
  font-family: inherit;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 14px 14px calc(26px + env(safe-area-inset-bottom, 0px));
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 2px 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.brand h1 {
  font-size: 1.3rem;
  font-weight: 800;
  letter-spacing: 0.01em;
  margin: 0;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 14px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(16, 26, 46, 0.05);
  transition: transform 0.08s ease, background 0.12s ease, color 0.12s ease;
}

.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { pointer-events: none; }

/* ---------- Status bar ---------- */
.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 2px 4px 12px;
}

.badge {
  display: inline-flex;
  align-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 5px 14px;
  border-radius: 999px;
}

.timer {
  font-size: 1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  background: var(--surface);
  border: 1px solid var(--line);
  padding: 5px 14px;
  border-radius: 999px;
  box-shadow: 0 2px 6px rgba(16, 26, 46, 0.05);
}

/* ---------- Board ---------- */
.board-wrap {
  padding: 0 2px;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  background: var(--surface);
  border: 2px solid var(--line-strong);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: transparent;
  color: var(--entered);
  font-size: clamp(14px, 5vmin, 26px);
  font-weight: 600;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.12s ease;
}

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

/*
  3×3 box separators: each thick line is drawn exactly once, by the cell
  to its left (box-r) or above (box-b). Inner lines stay 1px.
*/
.cell.box-r { border-right: 2px solid var(--line-strong); }
.cell.box-b { border-bottom: 2px solid var(--line-strong); }

.cell.given {
  color: var(--given);
  font-weight: 800;
  cursor: default;
}

.cell.empty {
  cursor: pointer;
}

.cell.peer { background: var(--peer); }

.cell.same {
  background: var(--same-bg);
  font-weight: 800;
}

.cell.selected {
  background: var(--selected-bg);
}
.cell.selected::after {
  content: "";
  position: absolute;
  inset: 2px;
  border: 2px solid var(--accent);
  border-radius: 8px;
  pointer-events: none;
}

.cell.wrong {
  color: var(--wrong);
  background: var(--wrong-bg);
}

.cell.hint-flash {
  background: var(--hint-bg);
  color: var(--hint-color);
  animation: hintPulse 0.85s ease-in-out 3;
}

@keyframes hintPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.10); }
}

.cell.pop {
  animation: popIn 0.16s ease-out;
}

@keyframes popIn {
  from { transform: scale(0.55); }
  to { transform: scale(1); }
}

/* ---------- Number pad ---------- */
.pad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  width: 100%;
  max-width: 520px;
  margin: 16px auto 0;
}

.pad.hidden { display: none; }

.pad button {
  min-height: 54px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(16, 26, 46, 0.06);
  transition: transform 0.08s ease, background 0.12s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.pad button:hover { background: var(--surface-2); }
.pad button:active { transform: scale(0.93); }

.pad-erase {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 1rem;
}
.pad-erase svg { pointer-events: none; }

/* ---------- Footer note ---------- */
.footer-note {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  margin: 18px 8px 0;
}

.nojs {
  text-align: center;
  padding: 20px;
  font-size: 1rem;
}

/* ---------- Overlays / modals ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--overlay);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadeIn 0.16s ease-out;
}

.overlay[hidden] { display: none; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal {
  background: var(--surface);
  color: var(--text);
  border-radius: 22px;
  padding: 22px;
  width: 100%;
  max-width: 430px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  animation: modalIn 0.18s ease-out;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.modal h2 {
  font-size: 1.2rem;
  font-weight: 800;
  margin: 0 0 8px;
}

.modal p {
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.55;
  margin: 0 0 14px;
}

.modal h3 { font-size: 1rem; margin: 0 0 8px; }
.modal h4 {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 14px 0 6px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 18px;
}

.modal-actions.center { justify-content: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 10px 20px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.97); }

.btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-contrast);
}
.btn.primary:hover { filter: brightness(1.06); background: var(--accent); }

.btn.danger {
  color: var(--wrong);
  background: transparent;
}
.btn.danger:hover { background: var(--wrong-bg); }

/* ---------- Settings rows ---------- */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 0;
}

.setting-row + .setting-row {
  border-top: 1px solid var(--line);
}

.setting-row label {
  font-weight: 700;
  font-size: 0.95rem;
  flex: none;
}

.setting-note {
  margin: -4px 0 10px;
  font-size: 0.78rem;
  color: var(--muted);
}

.seg {
  display: flex;
  gap: 3px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 4px;
  flex: 1;
  max-width: 270px;
}

.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px 6px;
  border-radius: 9px;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease;
}

.seg button.active {
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 1px 4px rgba(16, 26, 46, 0.18);
}

.switch {
  position: relative;
  width: 50px;
  height: 30px;
  flex: none;
  border: 0;
  border-radius: 999px;
  background: var(--line-strong);
  cursor: pointer;
  transition: background 0.15s ease;
  padding: 0;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.28);
  transition: left 0.15s ease;
}

.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { left: 23px; }

/* ---------- Settings: history section ---------- */
.settings-section {
  border-top: 1px solid var(--line);
  margin-top: 8px;
  padding-top: 14px;
}

.best-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 18px;
  margin-top: 4px;
}

.best-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.9rem;
}

.best-row span:last-child {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.hist-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  max-height: 250px;
  overflow-y: auto;
}

.hist-list li {
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}

.hist-list li:last-child { border-bottom: 0; }

.hist-empty {
  color: var(--muted);
  font-size: 0.9rem;
}

.hist-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hist-info {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.hist-info b { font-size: 0.92rem; }

.hist-info small {
  color: var(--muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}

.hist-play {
  flex: none;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--accent);
  cursor: pointer;
  transition: transform 0.08s ease;
}

.hist-play:active { transform: scale(0.92); }
.hist-play svg { pointer-events: none; }

.mini {
  display: grid;
  grid-template-columns: repeat(9, 4px);
  gap: 1px;
  flex: none;
  align-self: center;
}

.mini i {
  width: 4px;
  height: 4px;
  border-radius: 1.5px;
  background: var(--line-strong);
}

.mini i.off {
  background: var(--line);
  opacity: 0.45;
}

/* ---------- Solved modal ---------- */
.solved { text-align: center; }

.solved h2 { margin-top: 4px; }

.solved-emoji {
  font-size: 2.6rem;
  line-height: 1;
  margin-bottom: 6px;
}

.badge-newbest {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.88rem;
  padding: 6px 16px;
  border-radius: 999px;
  margin: 10px 0 4px;
}

.solved-stats {
  display: flex;
  justify-content: center;
  gap: 26px;
  margin: 16px 0 4px;
}

.solved-stats > div {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.solved-stats small {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.solved-stats span {
  font-size: 1.2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.mono { font-variant-numeric: tabular-nums; }

/* ---------- Responsive tweaks ---------- */
@media (max-width: 380px) {
  .brand h1 { font-size: 1.1rem; }
  .icon-btn { width: 40px; height: 40px; }
  .modal { padding: 18px; }
  .solved-stats { gap: 18px; }
}

/* short / landscape screens: keep everything visible */
@media (max-height: 640px) and (orientation: landscape) {
  .app { padding-top: 10px; }
  .board { max-width: min(520px, calc(100vh - 250px)); }
  .pad { grid-template-columns: repeat(10, 1fr); max-width: 560px; gap: 8px; }
  .pad button { min-height: 48px; font-size: 1.1rem; }
  .footer-note { margin-top: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
