:root {
  --primary: #1a56db;
  --primary-dark: #1341a8;
  --success: #057a55;
  --error: #c81e1e;
  --bg: #f3f4f6;
  --card: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── Header ─────────────────────────────────────────── */
header {
  background: var(--primary);
  color: #fff;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

header .logo { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.5px; }
header .logo span { opacity: .7; font-weight: 400; }
header nav { margin-left: auto; display: flex; gap: .5rem; }
header nav button {
  background: rgba(255,255,255,.15);
  border: none; color: #fff;
  padding: .45rem .9rem; border-radius: 8px;
  cursor: pointer; font-size: .85rem; font-weight: 600;
  transition: background .2s;
}
header nav button:hover { background: rgba(255,255,255,.3); }
header nav button.active { background: rgba(255,255,255,.9); color: var(--primary); }

/* ── Layout ─────────────────────────────────────────── */
main { max-width: 820px; margin: 2rem auto; padding: 0 1rem; }

/* ── Cards ──────────────────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 2rem;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
  margin-bottom: 1.5rem;
}

/* ── Home ───────────────────────────────────────────── */
#home h1 { font-size: 1.8rem; font-weight: 700; margin-bottom: .4rem; }
#home p.subtitle { color: var(--muted); margin-bottom: 2rem; font-size: 1rem; }

.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.topic-card {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem 1rem;
  cursor: pointer;
  transition: border-color .2s, transform .15s, box-shadow .2s;
  position: relative;
  user-select: none;
}
.topic-card:hover { border-color: var(--primary); box-shadow: 0 4px 12px rgba(26,86,219,.15); transform: translateY(-2px); }
.topic-card.selected { border-color: var(--primary); background: #eff6ff; }
.topic-card .check {
  position: absolute; top: .6rem; right: .6rem;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--primary); display: none;
  align-items: center; justify-content: center;
  color: #fff; font-size: .7rem;
}
.topic-card.selected .check { display: flex; }
.topic-card h3 { font-size: .95rem; font-weight: 600; margin-bottom: .2rem; }
.topic-card small { color: var(--muted); font-size: .8rem; }
.topic-card .icon { font-size: 1.6rem; margin-bottom: .5rem; }

.start-options {
  display: flex; flex-wrap: wrap; gap: 1rem; align-items: center;
}
.start-options label { font-size: .9rem; color: var(--muted); }
.start-options select {
  padding: .45rem .8rem; border-radius: 8px;
  border: 1px solid var(--border); background: #fff;
  font-size: .9rem; cursor: pointer;
}

/* ── Buttons ─────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .5rem;
  padding: .65rem 1.4rem;
  border-radius: 9px; font-size: .95rem; font-weight: 600;
  cursor: pointer; border: none; transition: all .2s;
  text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(26,86,219,.3); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; transform: none; box-shadow: none; }
.btn-ghost { background: transparent; border: 1.5px solid var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--primary); color: var(--primary); }

/* ── Quiz ────────────────────────────────────────────── */
#quiz-view .quiz-header {
  display: flex; justify-content: space-between;
  align-items: center; margin-bottom: 1.5rem; flex-wrap: wrap; gap: .5rem;
}
.progress-bar {
  height: 6px; background: var(--border);
  border-radius: 99px; margin-bottom: 1.5rem; overflow: hidden;
}
.progress-fill {
  height: 100%; background: var(--primary);
  border-radius: 99px; transition: width .4s ease;
}
.quiz-meta { color: var(--muted); font-size: .85rem; }
.quiz-meta strong { color: var(--text); }
.quiz-timer {
  font-size: 1.1rem; font-weight: 700;
  background: #eff6ff; color: var(--primary);
  padding: .3rem .8rem; border-radius: 8px;
}
.quiz-timer.warn { background: #fef3c7; color: #92400e; }
.quiz-timer.danger { background: #fef2f2; color: #991b1b; animation: pulse .7s infinite; }

@keyframes pulse { 0%,100% { opacity:1 } 50% { opacity:.5 } }

.question-text {
  font-size: 1.15rem; font-weight: 600;
  line-height: 1.5; margin-bottom: 1.5rem;
}
.question-topic {
  display: inline-block; font-size: .75rem; font-weight: 600;
  background: #eff6ff; color: var(--primary);
  padding: .2rem .6rem; border-radius: 6px; margin-bottom: .8rem;
}

.options { display: flex; flex-direction: column; gap: .75rem; }
.option-btn {
  text-align: left; background: #fff;
  border: 2px solid var(--border);
  border-radius: 10px; padding: .9rem 1.1rem;
  font-size: .95rem; cursor: pointer;
  transition: all .2s; display: flex; gap: .75rem; align-items: flex-start;
}
.option-btn:hover:not(:disabled) { border-color: var(--primary); background: #eff6ff; }
.option-btn .opt-letter {
  min-width: 26px; height: 26px;
  border-radius: 50%; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: .8rem; font-weight: 700; color: var(--muted);
  transition: all .2s; flex-shrink: 0;
}
.option-btn:hover:not(:disabled) .opt-letter { background: var(--primary); color: #fff; }
.option-btn.correct { border-color: var(--success); background: #f0fdf4; }
.option-btn.correct .opt-letter { background: var(--success); color: #fff; }
.option-btn.wrong { border-color: var(--error); background: #fef2f2; }
.option-btn.wrong .opt-letter { background: var(--error); color: #fff; }
.option-btn:disabled { cursor: default; }

.explanation {
  margin-top: 1.2rem; padding: 1rem 1.2rem;
  background: #f0fdf4; border-left: 4px solid var(--success);
  border-radius: 8px; font-size: .9rem; line-height: 1.5; color: #065f46;
  display: none;
}
.explanation.show { display: block; }
.explanation.wrong-exp { background: #fef2f2; border-color: var(--error); color: #7f1d1d; }

.quiz-nav {
  display: flex; justify-content: space-between;
  align-items: center; margin-top: 1.5rem;
}

/* ── Results ─────────────────────────────────────────── */
#results-view .score-circle {
  width: 140px; height: 140px;
  border-radius: 50%; margin: 0 auto 1.5rem;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  font-size: 2.5rem; font-weight: 800;
  border: 6px solid var(--primary);
  color: var(--primary); background: #eff6ff;
}
.score-circle span { font-size: .9rem; font-weight: 500; color: var(--muted); }
#results-view h2 { text-align: center; font-size: 1.4rem; margin-bottom: .3rem; }
#results-view .result-subtitle { text-align: center; color: var(--muted); margin-bottom: 2rem; }

.stats-row {
  display: flex; gap: 1rem; justify-content: center;
  flex-wrap: wrap; margin-bottom: 2rem;
}
.stat-box {
  flex: 1; min-width: 100px; max-width: 150px;
  background: var(--bg); border-radius: 10px;
  padding: 1rem; text-align: center;
}
.stat-box .val { font-size: 1.6rem; font-weight: 800; }
.stat-box .lbl { font-size: .78rem; color: var(--muted); margin-top: .2rem; }
.stat-box.ok .val { color: var(--success); }
.stat-box.ko .val { color: var(--error); }
.stat-box.skip .val { color: #d97706; }

.review-list { display: flex; flex-direction: column; gap: 1rem; }
.review-item {
  border: 1.5px solid var(--border); border-radius: 10px; padding: 1rem 1.1rem;
}
.review-item.correct { border-color: #a7f3d0; background: #f0fdf4; }
.review-item.wrong { border-color: #fca5a5; background: #fef2f2; }
.review-item .ri-q { font-size: .9rem; font-weight: 600; margin-bottom: .5rem; }
.review-item .ri-topic { font-size: .72rem; color: var(--muted); margin-bottom: .3rem; }
.review-item .ri-ans { font-size: .85rem; }
.review-item .ri-ans .tag {
  display: inline-block; padding: .15rem .5rem;
  border-radius: 5px; font-size: .75rem; font-weight: 600; margin-right: .3rem;
}
.tag-correct { background: #d1fae5; color: #065f46; }
.tag-wrong { background: #fee2e2; color: #991b1b; }
.review-item .ri-exp {
  font-size: .82rem; color: var(--muted); margin-top: .5rem;
  padding-top: .5rem; border-top: 1px solid var(--border);
}
.result-actions { display: flex; gap: .75rem; justify-content: center; flex-wrap: wrap; margin-top: 1.5rem; }

/* ── Ranking ─────────────────────────────────────────── */
#ranking-view h2 { font-size: 1.4rem; font-weight: 700; margin-bottom: 1.2rem; }
.ranking-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.ranking-table th { text-align: left; color: var(--muted); font-size: .78rem; font-weight: 600; padding: .5rem .8rem; border-bottom: 1.5px solid var(--border); text-transform: uppercase; }
.ranking-table td { padding: .75rem .8rem; border-bottom: 1px solid var(--border); }
.ranking-table tr:last-child td { border: none; }
.rank-pos { font-weight: 800; width: 36px; text-align: center; }
.rank-medal { font-size: 1.1rem; }
.rank-name { font-weight: 600; }
.rank-score { color: var(--primary); font-weight: 700; }
.rank-date { color: var(--muted); font-size: .8rem; }
.empty-ranking { text-align: center; color: var(--muted); padding: 2rem; }

/* ── Responsive ─────────────────────────────────────── */
@media (max-width: 600px) {
  .card { padding: 1.2rem; }
  .question-text { font-size: 1rem; }
  header .logo { font-size: 1.1rem; }
  #home h1 { font-size: 1.4rem; }
}
