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

:root {
  --blue:   #2563eb;
  --blue-d: #1d4ed8;
  --blue-l: #dbeafe;
  --dark:   #1e293b;
  --mid:    #334155;
  --muted:  #64748b;
  --border: #e2e8f0;
  --bg:     #f8fafc;
  --white:  #ffffff;
  --green:  #22c55e;
  --red:    #ef4444;
  --amber:  #f59e0b;
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: var(--bg); }

/* ── App shell ────────────────────────────────────────────── */
#app {
  position: relative;
  width: 100%;
  min-height: 100vh;
  overflow: hidden;
}

/* ── View transitions ─────────────────────────────────────── */
.view {
  position: absolute;
  inset: 0;
  min-height: 100vh;
  background: var(--bg);
  transform: translateX(100%);
  transition: transform .32s cubic-bezier(.4,0,.2,1);
  overflow-y: auto;
  will-change: transform;
}
.view.active      { transform: translateX(0);     z-index: 1; }
.view.slide-left  { transform: translateX(-30%);  z-index: 0; }

/* ── Utility ──────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Header ───────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  background: var(--white);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.logo { font-size: 18px; font-weight: 800; color: var(--blue); flex: 1; }
.header-right { display: flex; align-items: center; gap: 8px; }
.username-chip {
  font-size: 12px; color: var(--muted); background: #f1f5f9;
  padding: 4px 10px; border-radius: 999px;
}
.icon-btn {
  background: none; border: none; cursor: pointer;
  font-size: 18px; color: var(--muted); padding: 4px 6px;
  border-radius: 6px; transition: background .15s;
}
.icon-btn:hover { background: #f1f5f9; }
.back-btn {
  background: none; border: none; cursor: pointer;
  font-size: 22px; color: var(--blue); padding: 2px 8px 2px 0;
  font-weight: 700; line-height: 1;
}

/* ── View body ────────────────────────────────────────────── */
.view-body {
  padding: 20px;
  max-width: 540px;
  margin: 0 auto;
}

/* ── Auth screen ──────────────────────────────────────────── */
.auth-shell {
  max-width: 400px;
  margin: 0 auto;
  padding: 40px 24px 32px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.auth-hero { text-align: center; margin-bottom: 36px; }
.hero-icon {
  font-size: 60px; display: block; margin-bottom: 10px; line-height: 1;
  color: var(--blue);
}
.auth-hero h1 { font-size: 26px; font-weight: 800; color: var(--dark); margin-bottom: 6px; }
.auth-hero p  { font-size: 14px; color: var(--muted); }

.error-banner {
  background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c;
  padding: 10px 14px; border-radius: 8px; font-size: 14px;
  margin-bottom: 16px;
}

.field { margin-bottom: 16px; }
.field label {
  display: block; font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em; margin-bottom: 6px;
}
.field input {
  width: 100%; padding: 12px 14px; border: 1.5px solid var(--border);
  border-radius: 10px; font-size: 15px; outline: none; color: var(--dark);
  background: var(--bg); transition: border-color .15s, background .15s;
}
.field input:focus { border-color: var(--blue); background: var(--white); }
#login-pin, #reg-pin { letter-spacing: 8px; text-align: center; font-size: 20px; font-weight: 700; }

.btn-primary {
  display: block; width: 100%; padding: 14px;
  background: var(--blue); color: var(--white); border: none;
  border-radius: 12px; font-size: 16px; font-weight: 600; cursor: pointer;
  margin-top: 8px; transition: background .15s;
}
.btn-primary:hover  { background: var(--blue-d); }
.btn-primary:active { background: #1e40af; }

.btn-ghost {
  display: block; width: 100%; padding: 12px; background: transparent;
  color: var(--muted); border: none; font-size: 13px; cursor: pointer;
  margin-top: 4px; text-align: center; border-radius: 8px;
  transition: background .15s;
}
.btn-ghost:hover { background: #f1f5f9; }

/* ── Home screen ──────────────────────────────────────────── */
.greeting      { font-size: 22px; font-weight: 700; color: var(--dark); margin-bottom: 4px; }
.greeting-sub  { font-size: 14px; color: var(--muted); margin-bottom: 24px; }

.start-btn {
  display: block; width: 100%; padding: 20px;
  background: linear-gradient(135deg, var(--blue), #7c3aed);
  color: var(--white); border: none; border-radius: 16px;
  text-align: center; cursor: pointer; margin-bottom: 20px;
  box-shadow: 0 4px 20px rgba(37,99,235,.35);
  transition: transform .15s, box-shadow .15s;
}
.start-btn:hover  { transform: translateY(-1px); box-shadow: 0 6px 24px rgba(37,99,235,.45); }
.start-btn:active { transform: translateY(0); }
.start-label { display: block; font-size: 20px; font-weight: 800; }
.start-sub   { display: block; font-size: 12px; opacity: .8; margin-top: 3px; }

.quick-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px;
}
.stat-card {
  background: var(--white); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px; text-align: center;
}
.stat-val { font-size: 24px; font-weight: 800; color: var(--blue); }
.stat-lbl { font-size: 11px; color: var(--muted); margin-top: 3px; }
.nemesis-card { background: #fff8f8; border-color: #fecaca; }
.nemesis-val  { color: var(--red) !important; }
.nemesis-lbl  { color: var(--red) !important; }

.home-nav { display: flex; gap: 10px; }
.nav-btn {
  flex: 1; padding: 12px; border: 1.5px solid var(--border); border-radius: 12px;
  background: var(--white); font-size: 14px; font-weight: 500; color: var(--mid);
  cursor: pointer; text-align: center; transition: border-color .15s, box-shadow .15s;
}
.nav-btn:hover { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-l); }

/* ── Practice screen ──────────────────────────────────────── */
.practice-header {
  background: var(--blue); color: var(--white);
  padding: 16px 20px 12px; position: sticky; top: 0; z-index: 10;
}
.timer-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.timer { font-size: 32px; font-weight: 800; font-variant-numeric: tabular-nums; }
.score-badges { display: flex; gap: 8px; }
.badge-correct { background: rgba(255,255,255,.25); padding: 4px 14px; border-radius: 999px; font-size: 14px; font-weight: 600; }
.badge-wrong   { background: rgba(255,80,80,.4);   padding: 4px 14px; border-radius: 999px; font-size: 14px; font-weight: 600; }
.progress-bar  { height: 4px; background: rgba(255,255,255,.3); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--white); border-radius: 2px; width: 0%; transition: width 1s linear; }

.question-area { padding: 32px 20px 16px; text-align: center; }
.question-text {
  font-size: 48px; font-weight: 900; color: var(--dark);
  letter-spacing: -1px; margin-bottom: 16px;
}
.answer-row { display: flex; justify-content: center; }
.answer-display {
  font-size: 38px; font-weight: 700; color: var(--blue);
  min-width: 90px; height: 52px; line-height: 52px; text-align: center;
  border-bottom: 3px solid var(--blue);
}
.correct-flash {
  margin-top: 12px; font-size: 18px; color: var(--red); font-weight: 600;
}

.calc-pad {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 8px; padding: 12px 20px 28px; max-width: 340px; margin: 0 auto;
}
.calc-key {
  padding: 17px; background: var(--white); border: 1.5px solid var(--border);
  border-radius: 12px; font-size: 20px; font-weight: 600; color: var(--dark);
  cursor: pointer; text-align: center; transition: background .1s, border-color .1s;
  user-select: none;
}
.calc-key:hover  { background: var(--blue-l); border-color: var(--blue); }
.calc-key:active { background: var(--blue-l); }
.key-backspace { font-size: 18px; color: var(--red); }
.key-wide { grid-column: span 2; }

/* ── Results screen ───────────────────────────────────────── */
.results-body { text-align: center; padding-top: 32px; }
.results-score-lbl { font-size: 13px; color: var(--muted); }
.results-score { font-size: 80px; font-weight: 900; color: var(--blue); line-height: 1; margin: 8px 0 4px; }
.results-sublbl { font-size: 13px; color: var(--muted); margin-bottom: 28px; }
.results-row {
  display: flex; justify-content: center; align-items: flex-start;
  gap: 20px; margin-bottom: 24px;
}
.results-stat { text-align: center; }
.rval { font-size: 28px; font-weight: 800; color: var(--dark); }
.rval.green { color: var(--green); }
.rval.red   { color: var(--red); }
.rlbl { font-size: 12px; color: var(--muted); margin-top: 2px; }
.results-divider { font-size: 28px; color: var(--border); }

.results-comparison {
  background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 12px;
  padding: 14px; font-size: 14px; color: #166534; margin-bottom: 24px;
}
.results-comparison.down { background: #fff8f8; border-color: #fecaca; color: #991b1b; }

/* ── Stats screen ─────────────────────────────────────────── */
.stats-summary {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px;
}
.summary-card {
  background: var(--white); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px;
}
.s-val { font-size: 20px; font-weight: 800; color: var(--blue); }
.s-lbl { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-top: 2px; }

.section-title { font-size: 13px; font-weight: 600; color: var(--mid); margin-bottom: 10px; }

.combo-grid-wrap { overflow-x: auto; margin-bottom: 12px; }
.combo-grid {
  display: grid;
  grid-template-columns: 18px repeat(12, 1fr);
  gap: 2px;
  min-width: 280px;
}
.combo-cell {
  aspect-ratio: 1; border-radius: 3px; cursor: pointer;
  transition: transform .1s, filter .1s;
}
.combo-cell:hover { transform: scale(1.25); filter: brightness(.9); z-index: 2; position: relative; }
.col-lbl, .row-lbl {
  font-size: 8px; font-weight: 600; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
}
.c-none  { background: #cbd5e1; }
.c-bad   { background: #fca5a5; }
.c-mid   { background: #fcd34d; }
.c-good  { background: #86efac; }
.c-great { background: #4ade80; }

.legend {
  display: flex; flex-wrap: wrap; gap: 10px;
  font-size: 11px; color: var(--muted); margin-bottom: 24px;
}
.legend-item { display: flex; align-items: center; gap: 4px; }
.legend-dot { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* Run history */
.run-row {
  background: var(--white); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; margin-bottom: 8px;
  display: flex; justify-content: space-between; align-items: center;
}
.run-left { font-size: 13px; color: var(--muted); }
.run-score { font-size: 20px; font-weight: 800; color: var(--blue); }
.run-detail { font-size: 11px; color: var(--muted); text-align: right; }

.pagination { display: flex; gap: 8px; justify-content: center; margin: 16px 0; }
.page-btn {
  padding: 6px 14px; border: 1.5px solid var(--border); border-radius: 8px;
  background: var(--white); font-size: 13px; cursor: pointer; font-weight: 500;
}
.page-btn.active, .page-btn:hover { border-color: var(--blue); color: var(--blue); }

/* ── Leaderboard ──────────────────────────────────────────── */
.lb-header-row {
  display: flex; justify-content: space-between;
  font-size: 10px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em;
  padding: 0 12px; margin-bottom: 10px;
}
.lb-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--white); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px; margin-bottom: 6px;
}
.lb-row.me { background: #eff6ff; border-color: #bfdbfe; }
.lb-rank { font-size: 13px; font-weight: 700; color: var(--muted); width: 22px; text-align: center; }
.lb-rank.gold   { color: var(--amber); }
.lb-rank.silver { color: #94a3b8; }
.lb-rank.bronze { color: #b45309; }
.lb-name { font-size: 14px; font-weight: 600; flex: 1; color: var(--dark); }
.lb-me-badge { font-size: 10px; color: var(--blue); margin-left: 4px; }
.lb-right { text-align: right; }
.lb-score { font-size: 16px; font-weight: 800; color: var(--blue); }
.lb-acc   { font-size: 11px; color: var(--muted); }

/* ── Modal ────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.modal {
  background: var(--white); border-radius: 16px;
  width: 100%; max-width: 420px; max-height: 80vh;
  overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  font-size: 16px; font-weight: 700; color: var(--dark);
  position: sticky; top: 0; background: var(--white);
}
.modal-close {
  background: none; border: none; cursor: pointer; font-size: 16px;
  color: var(--muted); padding: 4px;
}
#modal-body { padding: 16px 20px; }
.history-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.history-row:last-child { border-bottom: none; }
.hist-date { color: var(--muted); }
.hist-answer { font-weight: 600; }
.hist-answer.correct { color: var(--green); }
.hist-answer.wrong   { color: var(--red); }

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 400px) {
  .question-text { font-size: 38px; }
  .results-score { font-size: 64px; }
  .calc-pad { padding: 8px 12px 20px; gap: 6px; }
  .calc-key  { padding: 14px; font-size: 18px; }
}
