:root {
  --nav-height: 57px; /* Unified top nav bar height */
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #0f172a;
  --subtext: #475569;
  --primary: #1d4ed8;
  --primary-light: #eff6ff;
  --border: #e2e8f0;
  --correct: #16a34a;
  --correct-bg: #dcfce7;
  --wrong: #dc2626;
  --wrong-bg: #fee2e2;
  --font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --card-bg: #1e293b;
  --text: #f8fafc;
  --subtext: #94a3b8;
  --primary: #3b82f6;
  --primary-light: #1e3a8a;
  --border: #334155;
  --correct-bg: #064e3b;
  --wrong-bg: #7f1d1d;
}

* { 
  box-sizing: border-box; 
  margin: 0; 
  padding: 0; 
  -webkit-tap-highlight-color: transparent; 
}

body { 
  font-family: var(--font-family); 
  background-color: var(--bg); 
  color: var(--text); 
  transition: background 0.2s, color 0.2s; 
  line-height: 1.5; 
}

/* Header Navigation */
header { 
  height: var(--nav-height);
  background: var(--card-bg); 
  border-bottom: 1px solid var(--border); 
  position: sticky; 
  top: 0; 
  z-index: 100; 
}

.nav-container { 
  max-width: 1000px; 
  height: 100%;
  margin: 0 auto; 
  display: flex; 
  justify-content: space-between; 
  align-items: center; 
  padding: 0 1rem; 
}

.brand-group { display: flex; align-items: center; gap: 0.5rem; }
.brand { font-size: 1.25rem; font-weight: 800; color: var(--primary); text-decoration: none; }
.tag { font-size: 0.7rem; background: var(--primary-light); color: var(--primary); padding: 0.15rem 0.4rem; border-radius: 4px; font-weight: 600; }

#authContainer { display: flex; align-items: center; gap: 0.5rem; }
.user-name { font-size: 0.8rem; font-weight: 600; color: var(--text); }

main { max-width: 1000px; margin: 1rem auto; padding: 0 0.75rem; }

.section-title { margin-bottom: 1rem; }
.section-title-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}
.section-title-heading .btn { flex-shrink: 0; text-decoration: none; }
.section-subtitle { font-size: 0.85rem; color: var(--subtext); margin-top: 0.2rem; }
.subject-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0.75rem; margin-bottom: 1.5rem; }
.subject-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; }
.subject-card h3 { color: var(--primary); font-size: 0.95rem; margin-bottom: 0.4rem; }
.subject-card p { font-size: 0.8rem; color: var(--subtext); margin-bottom: 0.75rem; }
.badge { display: inline-block; font-size: 0.7rem; background: var(--primary-light); color: var(--primary); padding: 0.2rem 0.5rem; border-radius: 10px; font-weight: 600; }

/* Quiz Container */
.quiz-box { 
  background: var(--card-bg); 
  border: 1px solid var(--border); 
  border-radius: 10px; 
  padding: 1rem; 
  margin-top: 0.75rem; 
  box-shadow: 0 1px 3px rgba(0,0,0,0.04); 
}

/* Sticky Header Wrapper: Pins seamlessly under nav height */
.sticky-quiz-header {
  position: sticky;
  top: var(--nav-height); 
  z-index: 95;
  background: var(--card-bg);
  padding: 0.5rem 0 0.75rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.quiz-controls { 
  display: flex; 
  justify-content: space-between; 
  align-items: center; 
  gap: 0.5rem; 
}

.set-tabs { 
  display: flex; 
  gap: 0.35rem; 
  overflow-x: auto; 
  white-space: nowrap; 
  padding: 2px 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.set-tabs::-webkit-scrollbar { display: none; }

.tab-btn { 
  background: var(--bg); 
  border: 1px solid var(--border); 
  color: var(--subtext); 
  padding: 0.4rem 0.75rem; 
  border-radius: 20px; 
  cursor: pointer; 
  font-size: 0.8rem; 
  font-weight: 600; 
  flex-shrink: 0;
  touch-action: manipulation;
  transition: all 0.15s ease; 
}

.tab-btn.active { 
  background: var(--primary); 
  color: #ffffff; 
  border-color: var(--primary); 
  box-shadow: 0 2px 4px rgba(29, 78, 216, 0.25);
}

.tab-btn.locked {
  opacity: 0.6;
  background: transparent;
  border-style: dashed;
}

.tab-btn.completed {
  border-color: var(--correct);
  color: var(--correct);
}

.score-pill { 
  font-weight: 700; 
  color: var(--primary); 
  background: var(--primary-light); 
  padding: 0.35rem 0.65rem; 
  border-radius: 16px; 
  font-size: 0.775rem; 
  border: 1px solid var(--border);
  flex-shrink: 0;
  white-space: nowrap;
}

.timer-box {
  display: inline-block;
  font-family: monospace;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-light);
  padding: 0.5rem 1.25rem;
  border-radius: 8px;
  border: 1px solid var(--border);
}

/* Progress Bar locked inside sticky header */
.progress-container { 
  width: 100%; 
  height: 6px; 
  background: var(--border); 
  border-radius: 3px; 
  overflow: hidden; 
}

.progress-bar { 
  height: 100%; 
  background: var(--primary); 
  transition: width 0.25s ease; 
}

/* Questions & Buttons */
.question-card { margin-bottom: 1.25rem; padding-bottom: 1rem; border-bottom: 1px solid var(--border); }
.question-card:last-child { border-bottom: none; }
.q-text { font-size: 0.975rem; font-weight: 600; margin: 0.5rem 0 0.85rem 0; line-height: 1.45; }

.options-grid { display: grid; grid-template-columns: 1fr; gap: 0.5rem; }

.opt-btn {
  text-align: left; 
  padding: 0.85rem 1rem; 
  border: 1px solid var(--border);
  background: var(--card-bg); 
  color: var(--text); 
  border-radius: 8px;
  cursor: pointer; 
  font-size: 0.9rem; 
  line-height: 1.35;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s;
}

.opt-btn.correct { background: var(--correct-bg); border-color: var(--correct); color: var(--text); font-weight: 600; }
.opt-btn.wrong { background: var(--wrong-bg); border-color: var(--wrong); color: var(--text); }

.explanation-box { 
  margin-top: 0.75rem; 
  padding: 0.75rem; 
  background: var(--primary-light); 
  border-left: 3px solid var(--primary); 
  border-radius: 4px; 
  font-size: 0.825rem; 
  line-height: 1.45; 
  color: var(--text); 
}

/* Skeleton Loading Animation */
.skeleton {
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s infinite;
  border-radius: 4px;
}

@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-badge { width: 80px; height: 18px; border-radius: 10px; margin-bottom: 0.75rem; }
.skeleton-text { width: 100%; height: 16px; margin-bottom: 0.5rem; }
.skeleton-text.short { width: 60%; }
.skeleton-opt { width: 100%; height: 44px; border-radius: 8px; }

/* Global Buttons */
.btn { 
  background: var(--primary); 
  color: #fff; 
  border: none; 
  padding: 0.55rem 1rem; 
  border-radius: 6px; 
  cursor: pointer; 
  font-weight: 600; 
  font-size: 0.85rem; 
  touch-action: manipulation;
  transition: opacity 0.15s; 
}
.btn-primary { background: var(--primary); }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.725rem; }

@media (min-width: 640px) {
  :root { --nav-height: 60px; }
  main { margin: 1.5rem auto; padding: 0 1rem; }
  .quiz-box { padding: 1.5rem; }
  .tab-btn { padding: 0.4rem 0.85rem; font-size: 0.85rem; }
  .score-pill { padding: 0.4rem 0.85rem; font-size: 0.85rem; }
  .q-text { font-size: 1.05rem; }
}

@media (max-width: 639px) {
  .quiz-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .set-tabs {
    flex-wrap: wrap;
    overflow: visible;
    white-space: normal;
  }

  .score-pill {
    align-self: flex-end;
  }
}