:root {
  --bg-0: #0b0d1a;
  --bg-1: #121532;
  --bg-2: #1a1f45;
  --surface: rgba(255, 255, 255, 0.04);
  --surface-2: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.25);
  --text: #e9ecff;
  --text-dim: #9aa3c7;
  --text-mute: #6a739a;
  --accent: #7c5cff;
  --accent-2: #22d3ee;
  --accent-3: #f472b6;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #fb7185;
  --prefill: #cbd0ff;
  --user: #7dd3fc;
  --cell-size: clamp(30px, 9.4vw, 62px);
  --radius: 14px;
  --shadow-lg: 0 30px 60px -20px rgba(0, 0, 0, 0.6);
  --shadow-md: 0 10px 30px -10px rgba(0, 0, 0, 0.5);
  --transition: 180ms cubic-bezier(.2,.7,.2,1);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg-0: #eef1fb;
  --bg-1: #f7f8ff;
  --bg-2: #ffffff;
  --surface: rgba(16, 20, 48, 0.04);
  --surface-2: rgba(16, 20, 48, 0.07);
  --border: rgba(16, 20, 48, 0.12);
  --border-strong: rgba(16, 20, 48, 0.28);
  --text: #171a35;
  --text-dim: #52597a;
  --text-mute: #8990b0;
  --accent: #6b4bff;
  --accent-2: #0ea5b7;
  --accent-3: #db2a87;
  --prefill: #2b3164;
  --user: #0b65d0;
  --shadow-lg: 0 30px 60px -30px rgba(28, 33, 78, 0.28);
  --shadow-md: 0 10px 30px -18px rgba(28, 33, 78, 0.25);
  color-scheme: light;
}

* { box-sizing: border-box; }

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

body {
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(124, 92, 255, 0.25), transparent 60%),
    radial-gradient(1000px 500px at 110% 10%, rgba(34, 211, 238, 0.18), transparent 60%),
    radial-gradient(900px 500px at 50% 110%, rgba(244, 114, 182, 0.15), transparent 60%),
    linear-gradient(180deg, var(--bg-0), var(--bg-1));
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.mono { font-family: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace; }

.app {
  max-width: 620px;
  margin: 0 auto;
  padding: 18px 16px 28px;
  display: grid;
  gap: 14px;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

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

.logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 20px;
  color: white;
  background: conic-gradient(from 220deg, var(--accent), var(--accent-2), var(--accent-3), var(--accent));
  box-shadow: var(--shadow-md), inset 0 0 0 1px rgba(255, 255, 255, 0.2);
  letter-spacing: -0.5px;
}

.brand-text h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.4px;
}

.tagline {
  margin: 0;
  font-size: 12px;
  color: var(--text-dim);
  letter-spacing: 0.02em;
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform var(--transition), background var(--transition), border-color var(--transition);
}
.icon-btn:hover { background: var(--surface-2); transform: translateY(-1px); }
.icon-btn:active { transform: translateY(0); }

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(12px);
}

.stat { display: grid; gap: 2px; text-align: center; }
.stat .label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-mute);
}
.stat .value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.stat .muted { color: var(--text-mute); font-weight: 500; }

/* Board */
.board-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 10px;
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(14px);
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: calc(var(--cell-size) * 9 + 10px);
  margin: 0 auto;
  background: var(--bg-2);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  isolation: isolate;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  font-size: clamp(16px, 5vw, 28px);
  font-weight: 600;
  color: var(--user);
  background: transparent;
  cursor: pointer;
  user-select: none;
  border: 0;
  outline: none;
  transition: background var(--transition), color var(--transition);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

/* thick subgrid lines */
.cell[data-col="2"],
.cell[data-col="5"] { border-right: 2px solid var(--border-strong); }
.cell[data-row="2"],
.cell[data-row="5"] { border-bottom: 2px solid var(--border-strong); }
.cell[data-col="8"] { border-right: 0; }
.cell[data-row="8"] { border-bottom: 0; }

.cell.prefill { color: var(--prefill); font-weight: 700; }
.cell.peer { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.cell.same-number { background: color-mix(in srgb, var(--accent-2) 16%, transparent); }
.cell.selected {
  background: color-mix(in srgb, var(--accent) 28%, transparent);
  box-shadow: inset 0 0 0 2px var(--accent);
  z-index: 2;
}
.cell.conflict {
  color: var(--bad) !important;
  animation: shake 260ms cubic-bezier(.36,.07,.19,.97) both;
}
.cell.conflict::after {
  content: "";
  position: absolute; inset: 0;
  background: color-mix(in srgb, var(--bad) 18%, transparent);
  pointer-events: none;
}
.cell.correct-pop { animation: pop 260ms cubic-bezier(.3,1.4,.4,1) both; }
.cell.hint-flash { animation: hint-flash 900ms ease-out both; }

.notes {
  position: absolute;
  inset: 6%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 1px;
  font-size: clamp(8px, 2.1vw, 11px);
  font-weight: 600;
  color: var(--text-dim);
  pointer-events: none;
}
.notes span { display: grid; place-items: center; opacity: 0; }
.notes span.on { opacity: 1; }

@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-3px); }
  40%, 60% { transform: translateX(3px); }
}
@keyframes pop {
  0% { transform: scale(.6); }
  60% { transform: scale(1.18); }
  100% { transform: scale(1); }
}
@keyframes hint-flash {
  0% { background: color-mix(in srgb, var(--warn) 60%, transparent); }
  100% { background: transparent; }
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg-0) 70%, transparent);
  border-radius: calc(var(--radius) + 4px);
  backdrop-filter: blur(10px);
  animation: fade 240ms ease both;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.overlay-card {
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 22px;
  width: min(380px, 86%);
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.overlay-card h2 {
  margin: 0 0 6px;
  font-size: 22px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.overlay-card p { margin: 0 0 16px; color: var(--text-dim); font-size: 14px; }
.overlay-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 18px; }
.overlay-stats > div {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 10px;
  border-radius: 10px;
  display: grid;
  gap: 2px;
}
.overlay-stats .label { font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text-mute); }
.overlay-stats .value { font-size: 18px; font-weight: 700; }

@keyframes fade { from { opacity: 0 } to { opacity: 1 } }

/* Pad */
.pad {
  display: grid;
  gap: 10px;
}

.numbers {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}
.num {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: clamp(18px, 4.8vw, 22px);
  font-weight: 700;
  padding: 12px 0;
  border-radius: 12px;
  cursor: pointer;
  transition: transform var(--transition), background var(--transition), border-color var(--transition), color var(--transition);
  font-family: 'Inter', sans-serif;
}
.num:hover:not([disabled]) { background: var(--surface-2); transform: translateY(-1px); }
.num:active:not([disabled]) { transform: translateY(0); }
.num[disabled] { opacity: 0.35; cursor: not-allowed; }
.num .count {
  position: absolute;
  top: 4px; right: 6px;
  font-size: 9px;
  font-weight: 600;
  color: var(--text-mute);
}

.actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.action {
  display: grid;
  grid-template-rows: auto auto;
  justify-items: center;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 8px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  transition: transform var(--transition), background var(--transition), border-color var(--transition), color var(--transition);
  position: relative;
}
.action:hover { background: var(--surface-2); transform: translateY(-1px); }
.action:active { transform: translateY(0); }
.action.on {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
  color: var(--text);
}
.action .pill {
  position: absolute;
  top: 6px; right: 6px;
  font-size: 10px;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 999px;
  color: var(--text-dim);
  border: 1px solid var(--border);
}

/* Controls */
.controls {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}

.difficulty {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: color var(--transition), background var(--transition), border-color var(--transition);
}
.chip:hover { color: var(--text); }
.chip.active {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
  color: var(--text);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  border-radius: 12px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid transparent;
  font-family: inherit;
  transition: transform var(--transition), box-shadow var(--transition), filter var(--transition);
}
.btn.primary {
  color: white;
  background: linear-gradient(135deg, var(--accent), var(--accent-3));
  box-shadow: 0 10px 24px -10px color-mix(in srgb, var(--accent) 60%, transparent);
}
.btn.primary:hover { transform: translateY(-1px); filter: brightness(1.05); }
.btn.primary:active { transform: translateY(0); }

.foot {
  text-align: center;
  color: var(--text-mute);
  font-size: 11px;
  padding-top: 4px;
}

/* Responsive tweaks */
@media (max-width: 520px) {
  .app { padding: 14px 10px 22px; gap: 12px; }
  .stats { grid-template-columns: repeat(4, 1fr); padding: 8px; gap: 4px; }
  .stat .value { font-size: 14px; }
  .brand-text h1 { font-size: 18px; }
  .tagline { display: none; }
  .action .pill { display: none; }
  .numbers { gap: 4px; }
  .num { padding: 10px 0; }
  .foot { display: none; }
}

@media (max-width: 380px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}

@media (hover: none) {
  .cell:hover, .num:hover, .action:hover, .btn:hover { transform: none; filter: none; }
}

/* Focus ring (keyboard only) */
.cell:focus-visible,
.num:focus-visible,
.action:focus-visible,
.btn:focus-visible,
.chip:focus-visible,
.icon-btn:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 2px;
  z-index: 3;
}
