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

:root {
  --bg:          #070e1b;
  --surface:     #0c1525;
  --surface2:    #111e30;
  --border:      rgba(255,255,255,0.06);
  --border2:     rgba(255,255,255,0.13);
  --box-line:    rgba(255,255,255,0.32);
  --cell-line:   rgba(255,255,255,0.06);
  --accent:      #6c5fff;
  --accent-dim:  rgba(108,95,255,0.26);
  --accent-peer: rgba(108,95,255,0.04);
  --accent-same: rgba(108,95,255,0.22);
  --text:        #e8edf8;
  --given:       #c8d2e8;
  --player-num:  #a78bfa;
  --muted:       #8892aa;
  --green:       #34d399;
  --red:         #f87171;
  --red-dim:     rgba(248,113,113,0.14);
  --gold:        #fbbf24;
  --header-h:    52px;
  --radius:      12px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
}

button { font: inherit; cursor: pointer; border: none; background: none; touch-action: manipulation; }

/* ── Screens ────────────────────────────────────────────────────── */
.screen { display: none; }
.screen.active { display: flex; flex-direction: column; min-height: 100dvh; }

/* ── Header ─────────────────────────────────────────────────────── */
header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

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

.logo-back {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.1rem;
  padding: 6px 4px;
  line-height: 1;
  transition: color 0.15s;
}
.logo-back:hover { color: var(--text); }

.logo-title { font-weight: 800; font-size: 1rem; }
.logo-title span { color: var(--accent); }

.player-label {
  font-size: 12px;
  color: var(--muted);
  max-width: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  flex: 1;
  min-width: 0;
}

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

.diff-badge {
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  background: rgba(108,95,255,0.12);
  border: 1px solid rgba(108,95,255,0.25);
  border-radius: 20px;
  padding: 3px 10px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
}

/* ── Shared Buttons ─────────────────────────────────────────────── */
.btn-ghost {
  background: transparent;
  border: 1px solid var(--border2);
  border-radius: 8px;
  color: var(--muted);
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn-ghost:hover { color: var(--text); border-color: rgba(255,255,255,0.25); }

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #6c5fff, #a855f7);
  border-radius: var(--radius);
  color: #fff;
  font-weight: 700;
  padding: 14px 32px;
  font-size: 1rem;
  transition: opacity 0.15s, transform 0.1s;
}
.btn-primary:hover  { opacity: 0.9; }
.btn-primary:active { transform: scale(0.97); }

/* ── SCREEN: MENU ───────────────────────────────────────────────── */
#screen-menu { align-items: center; }

.menu-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 36px 20px 40px;
  gap: 28px;
  width: 100%;
  max-width: 400px;
}

.menu-hero { text-align: center; }
.menu-icon {
  width: 72px;
  height: 72px;
  color: var(--accent);
  margin: 0 auto 16px;
  opacity: 0.9;
}
.menu-icon svg { width: 100%; height: 100%; }
.menu-hero h1 { font-size: 1.9rem; font-weight: 800; margin-bottom: 6px; }
.menu-hero h1 span { color: var(--accent); }
.menu-hero p { color: var(--muted); font-size: 0.9rem; min-height: 1.2em; }

.menu-section-label {
  width: 100%;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.diff-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
}

.diff-btn {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  color: var(--text);
  padding: 18px 16px 16px;
  text-align: left;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.diff-btn:hover  { border-color: rgba(255,255,255,0.22); background: var(--surface2); }
.diff-btn:active { transform: scale(0.97); }

.diff-name { font-size: 1rem; font-weight: 800; }
.diff-desc { font-size: 12px; color: var(--muted); }
.diff-solved {
  font-size: 11px;
  font-weight: 600;
  margin-top: 6px;
  opacity: 0.8;
}

.diff-btn[data-difficulty="easy"]   .diff-name,
.diff-btn[data-difficulty="easy"]   .diff-solved { color: var(--green); }
.diff-btn[data-difficulty="medium"] .diff-name,
.diff-btn[data-difficulty="medium"] .diff-solved { color: var(--gold); }
.diff-btn[data-difficulty="hard"]   .diff-name,
.diff-btn[data-difficulty="hard"]   .diff-solved { color: #f97316; }
.diff-btn[data-difficulty="expert"] .diff-name,
.diff-btn[data-difficulty="expert"] .diff-solved { color: var(--red); }

/* ── SCREEN: LOADING ────────────────────────────────────────────── */
#screen-loading {
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.spinner {
  width: 44px;
  height: 44px;
  border: 3px solid var(--border2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text { color: var(--text); font-size: 15px; font-weight: 700; }
.loading-sub  { color: var(--muted); font-size: 13px; }

/* ── SCREEN: GAME ───────────────────────────────────────────────── */
#screen-game { align-items: stretch; }

.game-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 14px 12px 16px;
  gap: 16px;
  width: 100%;
}

/* ── Board ──────────────────────────────────────────────────────── */
#board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3px;
  background: var(--box-line);
  border: 3px solid var(--box-line);
  border-radius: 10px;
  overflow: hidden;
  width: min(94vw, 414px);
  height: min(94vw, 414px);
  flex-shrink: 0;
}

.box {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 1px;
  background: var(--cell-line);
}

.cell {
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background 0.07s;
}

/* ── Cell states ────────────────────────────────────────────────── */
.cell.peer     { background: var(--surface); }
.cell.same-num { background: var(--accent-same); }
.cell.selected { background: var(--accent-dim) !important; }
.cell.error    { background: var(--red-dim); }

/* ── Cell numbers ───────────────────────────────────────────────── */
.cell-num {
  font-size: clamp(14px, 4.2vw, 20px);
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
}
.cell.given      .cell-num { color: var(--given); }
.cell:not(.given) .cell-num { color: var(--player-num); }
.cell.error      .cell-num { color: var(--red); font-weight: 800; }
.cell.selected.given .cell-num { color: var(--text); }

/* ── Pencil notes ───────────────────────────────────────────────── */
.cell-notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 1px;
  pointer-events: none;
}

.note {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(7px, 1.8vw, 10px);
  font-weight: 700;
  color: transparent;
  line-height: 1;
}
.note.active { color: rgba(200,210,232,0.75); }
.cell.selected .note.active { color: var(--accent); }

/* ── Action buttons ─────────────────────────────────────────────── */
.action-btns {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.action-btn {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 10px;
  color: var(--muted);
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  transition: color 0.12s, border-color 0.12s, background 0.12s;
  white-space: nowrap;
}
.action-btn:hover   { color: var(--text); border-color: rgba(255,255,255,0.2); }
.action-btn:disabled { opacity: 0.3; cursor: default; }
.action-btn.active  {
  color: var(--accent);
  border-color: rgba(108,95,255,0.45);
  background: rgba(108,95,255,0.10);
}

/* ── Numpad ─────────────────────────────────────────────────────── */
#numpad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  width: min(94vw, 414px);
  flex-shrink: 0;
}

.num-btn {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 10px;
  color: var(--text);
  font-size: clamp(1rem, 4.5vw, 1.25rem);
  font-weight: 700;
  aspect-ratio: 1.1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, transform 0.08s;
}
.num-btn:hover  { background: var(--surface2); }
.num-btn:active { transform: scale(0.92); background: rgba(108,95,255,0.14); }
.num-btn.erase  { font-size: clamp(0.9rem, 4vw, 1.1rem); color: var(--muted); }

/* ── SCREEN: WIN ────────────────────────────────────────────────── */
#screen-win {
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}

.win-card {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 20px;
  padding: 40px 28px 36px;
  text-align: center;
  max-width: 360px;
  width: 100%;
  animation: winIn 0.4s cubic-bezier(0.34,1.56,0.64,1);
}
@keyframes winIn {
  from { opacity: 0; transform: scale(0.88) translateY(24px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.win-emoji { font-size: 3.2rem; margin-bottom: 14px; }
.win-card h2 { font-size: 1.9rem; font-weight: 800; margin-bottom: 8px; }

.win-diff-label {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  background: rgba(108,95,255,0.12);
  border: 1px solid rgba(108,95,255,0.25);
  border-radius: 20px;
  padding: 3px 12px;
  margin-bottom: 28px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.win-section-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 14px;
}

.stats-row {
  display: flex;
  justify-content: center;
  gap: 0;
  margin-bottom: 32px;
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  overflow: hidden;
}

.stat-item {
  flex: 1;
  padding: 14px 8px;
  border-right: 1px solid var(--border2);
}
.stat-item:last-child { border-right: none; }

.stat-val { font-size: 1.5rem; font-weight: 800; line-height: 1; margin-bottom: 4px; }
.stat-lbl { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; }

.stat-item[data-stat="easy"]   .stat-val { color: var(--green); }
.stat-item[data-stat="medium"] .stat-val { color: var(--gold); }
.stat-item[data-stat="hard"]   .stat-val { color: #f97316; }
.stat-item[data-stat="expert"] .stat-val { color: var(--red); }

/* ── Responsive ─────────────────────────────────────────────────── */
@media (max-height: 680px) {
  .game-body { justify-content: flex-start; padding-top: 6px; gap: 8px; }
  .action-btn { padding: 7px 11px; font-size: 12px; }
}

@media (max-width: 360px) {
  .action-btn { padding: 8px 10px; font-size: 12px; }
  .diff-grid  { grid-template-columns: 1fr; }
}
