/* ── Design Tokens ─────────────────────────────────────── */
:root {
  --bg:            #080b14;
  --surface-0:     #0d1120;
  --surface-1:     #121828;
  --surface-2:     #1a2238;
  --surface-3:     #232e4a;

  --border-thin:   #1e2840;
  --border-mid:    #2a375a;
  --border-thick:  #3a4e7a;

  --text:          #e8edf8;
  --text-muted:    #5a6b8a;
  --text-dim:      #8899bb;

  --accent:        #6c63ff;
  --accent-soft:   rgba(108, 99, 255, 0.15);
  --accent-glow:   rgba(108, 99, 255, 0.4);
  --pink:          #e83f7a;
  --pink-soft:     rgba(232, 63, 122, 0.15);
  --success:       #22d3a0;
  --success-soft:  rgba(34, 211, 160, 0.15);
  --error:         #e83f7a;
  --error-soft:    rgba(232, 63, 122, 0.12);

  --given-color:   #c0cce8;
  --user-color:    #7ab8f5;
  --note-color:    #4a6a9a;

  --cell-bg:       #0e1526;
  --cell-hover:    #16203a;
  --cell-selected: rgba(108, 99, 255, 0.35);
  --cell-peer:     rgba(108, 99, 255, 0.07);
  --cell-same:     rgba(108, 99, 255, 0.18);
  --cell-correct:  rgba(34, 211, 160, 0.1);
  --cell-error:    rgba(232, 63, 122, 0.15);

  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;

  --font: 'Inter', system-ui, -apple-system, sans-serif;

  --shadow-board: 0 2px 4px rgba(0,0,0,0.3), 0 8px 32px rgba(0,0,0,0.5), 0 0 0 1px var(--border-mid);
  --shadow-card:  0 4px 16px rgba(0,0,0,0.4), 0 24px 64px rgba(0,0,0,0.6);
  --shadow-btn:   0 2px 8px rgba(0,0,0,0.3);
  --glow-accent:  0 0 20px rgba(108, 99, 255, 0.3);
}

/* ── Reset ──────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Base ───────────────────────────────────────────────── */
html { height: 100%; }

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 28px 16px 48px;
  position: relative;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Background orbs ──────────────────────────────────── */
.bg-orbs {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(100px);
}

.orb-1 {
  width: 500px;
  height: 500px;
  background: var(--accent);
  opacity: 0.07;
  top: -200px;
  left: -150px;
  animation: drift 20s ease-in-out infinite alternate;
}

.orb-2 {
  width: 400px;
  height: 400px;
  background: var(--pink);
  opacity: 0.06;
  bottom: -150px;
  right: -100px;
  animation: drift 25s ease-in-out infinite alternate-reverse;
}

.orb-3 {
  width: 300px;
  height: 300px;
  background: var(--success);
  opacity: 0.04;
  top: 50%;
  left: 60%;
  animation: drift 18s ease-in-out infinite alternate;
}

@keyframes drift {
  from { transform: translate(0, 0) scale(1); }
  to   { transform: translate(30px, 20px) scale(1.1); }
}

/* ── App Layout ─────────────────────────────────────────── */
.app {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ── Header ─────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2px;
}

.title {
  font-size: clamp(1.8rem, 7vw, 2.6rem);
  font-weight: 900;
  letter-spacing: 0.3em;
  background: linear-gradient(135deg, var(--accent) 0%, #a78bfa 45%, var(--pink) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1;
}

.stats {
  display: flex;
  gap: 18px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.stat-label {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-muted);
  font-weight: 700;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
}

#mistakes.has-error { color: var(--error); }

/* ── Toolbar ─────────────────────────────────────────────── */
.toolbar {
  display: flex;
  gap: 10px;
  align-items: stretch;
}

.select-wrap {
  flex: 1;
  position: relative;
}

.select-arrow {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  font-size: 0.8rem;
  line-height: 1;
}

.difficulty-select {
  width: 100%;
  height: 100%;
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface-1);
  border: 1px solid var(--border-mid);
  color: var(--text);
  padding: 10px 36px 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-family: var(--font);
  font-weight: 600;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

.difficulty-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent) 0%, var(--pink) 100%);
  border: none;
  color: #fff;
  padding: 10px 24px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-family: var(--font);
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.03em;
  box-shadow: var(--shadow-btn), 0 0 24px rgba(108,99,255,0.25);
  transition: opacity 0.2s, transform 0.15s, box-shadow 0.2s;
  white-space: nowrap;
  touch-action: manipulation;
}

.btn-primary:hover {
  opacity: 0.92;
  box-shadow: var(--shadow-btn), 0 0 32px rgba(108,99,255,0.4);
}

.btn-primary:active { transform: scale(0.96); }

.btn-wide { width: 100%; padding: 14px; font-size: 1rem; }

/* ── Board wrapper ─────────────────────────────────────── */
.board-wrap {
  position: relative;
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.board-loader {
  position: absolute;
  inset: 0;
  background: rgba(8, 11, 20, 0.85);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-lg);
  z-index: 10;
  transition: opacity 0.3s;
}

.board-loader.hidden {
  opacity: 0;
  pointer-events: none;
}

.loader-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-mid);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Board ───────────────────────────────────────────────── */
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  background: var(--border-thick);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-board);
  gap: 0;
}

/* ── Cells ───────────────────────────────────────────────── */
.cell {
  aspect-ratio: 1;
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(13px, 3.6vw, 22px);
  font-weight: 700;
  cursor: pointer;
  position: relative;
  transition: background 0.12s, color 0.12s;
  user-select: none;
  touch-action: manipulation;
  outline: none;

  /* Thin right and bottom borders */
  border-right: 1px solid var(--border-thin);
  border-bottom: 1px solid var(--border-thin);
}

/* Remove right border on last column */
.cell[data-col="8"] { border-right: none; }
/* Remove bottom border on last row */
.cell[data-row="8"] { border-bottom: none; }

/* Thick box borders — right side of columns 2 and 5 */
.cell[data-col="2"] { border-right: 2px solid var(--border-thick); }
.cell[data-col="5"] { border-right: 2px solid var(--border-thick); }
/* Thick box borders — bottom side of rows 2 and 5 */
.cell[data-row="2"] { border-bottom: 2px solid var(--border-thick); }
.cell[data-row="5"] { border-bottom: 2px solid var(--border-thick); }

/* Cell states */
.cell.given      { color: var(--given-color); cursor: default; }
.cell.user-input { color: var(--user-color); }
.cell.peer       { background: var(--cell-peer); }
.cell.same-num   { background: var(--cell-same); }
.cell.selected   { background: var(--cell-selected) !important; }
.cell.has-error  { color: var(--error) !important; background: var(--cell-error) !important; }
.cell.just-placed { animation: cellPop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1); }
.cell.hint-flash  { animation: hintFlash 0.5s ease; }

.cell:not(.given):not(.selected):hover { background: var(--cell-hover); }

@keyframes cellPop {
  0%   { transform: scale(0.55); opacity: 0.4; }
  100% { transform: scale(1);    opacity: 1; }
}

@keyframes hintFlash {
  0%   { background: rgba(34, 211, 160, 0.5); }
  100% { background: var(--cell-selected); }
}

/* ── Notes inside cell ──────────────────────────────────── */
.notes-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 1px;
  font-size: clamp(5px, 1.1vw, 8px);
  font-weight: 600;
  color: var(--note-color);
}

.note-digit {
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.note-digit.active { color: #7ab8f5; }

/* ── Actions row ─────────────────────────────────────────── */
.actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.action-btn {
  background: var(--surface-1);
  border: 1px solid var(--border-mid);
  color: var(--text-dim);
  padding: 10px 6px 8px;
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font-family: var(--font);
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.12s;
  touch-action: manipulation;
}

.action-btn:hover {
  background: var(--surface-2);
  border-color: var(--border-thick);
  color: var(--text);
}

.action-btn:active { transform: scale(0.93); }

.action-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #a78bfa;
}

.action-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.action-label {
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  line-height: 1;
}

/* ── Number pad ──────────────────────────────────────────── */
.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}

.num-btn {
  aspect-ratio: 1;
  background: var(--surface-1);
  border: 1px solid var(--border-mid);
  color: var(--text);
  border-radius: var(--radius-xs);
  font-size: clamp(14px, 3.4vw, 22px);
  font-weight: 800;
  font-family: var(--font);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, border-color 0.12s, color 0.12s, transform 0.1s, opacity 0.15s;
  touch-action: manipulation;
  position: relative;
}

.num-btn::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(135deg, var(--accent), var(--pink));
  opacity: 0;
  transition: opacity 0.15s;
}

.num-btn:hover::after  { opacity: 0.12; }
.num-btn:hover { border-color: var(--accent); color: #a78bfa; }
.num-btn:active { transform: scale(0.88); }

.num-btn.completed {
  opacity: 0.18;
  pointer-events: none;
}

/* Remaining count badge */
.num-remaining {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: clamp(5px, 1vw, 7px);
  font-weight: 700;
  color: var(--text-muted);
  line-height: 1;
}

/* ── Modals ──────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 14, 0.8);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  align-items: center;
  justify-content: center;
  z-index: 300;
  padding: 20px;
}

.modal-overlay.show {
  display: flex;
  animation: modalFadeIn 0.3s ease;
}

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

.modal-card {
  background: var(--surface-1);
  border: 1px solid var(--border-thick);
  border-radius: var(--radius-xl);
  padding: 44px 40px 40px;
  text-align: center;
  box-shadow: var(--shadow-card), 0 0 60px rgba(108, 99, 255, 0.15);
  max-width: 340px;
  width: 100%;
  position: relative;
  overflow: hidden;
  animation: cardSlideUp 0.4s cubic-bezier(0.34, 1.45, 0.64, 1);
}

@keyframes cardSlideUp {
  from { transform: translateY(40px) scale(0.9); opacity: 0; }
  to   { transform: translateY(0)    scale(1);   opacity: 1; }
}

.modal-trophy {
  font-size: 3.5rem;
  margin-bottom: 12px;
  animation: trophyBounce 0.6s 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes trophyBounce {
  from { transform: scale(0) rotate(-20deg); opacity: 0; }
  to   { transform: scale(1) rotate(0deg);  opacity: 1; }
}

.modal-title {
  font-size: 1.6rem;
  font-weight: 900;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--accent), var(--pink));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.modal-subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 4px;
}

.modal-time {
  font-size: 2.5rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  color: var(--text);
  margin-bottom: 16px;
}

.modal-difficulty-badge {
  display: inline-block;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  color: #a78bfa;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding: 4px 12px;
  border-radius: 99px;
  margin-bottom: 28px;
}

/* ── Confetti ─────────────────────────────────────────────── */
.confetti-container {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  width: 8px;
  height: 8px;
  top: -10px;
  border-radius: 2px;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  0%   { transform: translateY(0) rotate(0deg);   opacity: 1; }
  100% { transform: translateY(400px) rotate(720deg); opacity: 0; }
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 360px) {
  body  { padding: 16px 8px 32px; }
  .app  { gap: 10px; }
  .actions { gap: 5px; }
  .numpad  { gap: 4px; }
  .modal-card { padding: 32px 20px 28px; }
}

@media (min-width: 520px) {
  body { padding: 40px 24px 60px; }
  .app { gap: 16px; }
}

@media (prefers-color-scheme: light) {
  /* Keep dark theme regardless */
}

/* ── Focus visible ───────────────────────────────────────── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
