/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }

/* ── Tokens ── */
:root {
  --bg: #F1F5F9;
  --surface: #FFFFFF;
  --surface-raised: #F8FAFC;
  --sidebar-bg: #1E1B4B;
  --sidebar-hover: rgba(255,255,255,0.08);
  --sidebar-active: #6366F1;
  --sidebar-text: #C7D2FE;
  --sidebar-text-muted: #818CF8;
  --sidebar-width: 260px;
  --sidebar-collapsed: 56px;
  --text: #0F172A;
  --text-muted: #64748B;
  --text-subtle: #94A3B8;
  --accent: #6366F1;
  --accent-hover: #4F46E5;
  --accent-light: #EEF2FF;
  --success: #22C55E;
  --success-bg: #F0FDF4;
  --error: #EF4444;
  --error-bg: #FEF2F2;
  --warning: #F59E0B;
  --border: #E2E8F0;
  --radius-sm: 6px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.06);
  --shadow: 0 4px 12px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.12);
  --transition: 0.2s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F172A;
    --surface: #1E293B;
    --surface-raised: #263347;
    --sidebar-bg: #0D1117;
    --text: #F1F5F9;
    --text-muted: #94A3B8;
    --text-subtle: #64748B;
    --accent-light: #1E1B4B;
    --success-bg: #052e16;
    --error-bg: #450a0a;
    --border: #334155;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.3);
    --shadow: 0 4px 12px rgba(0,0,0,.4);
    --shadow-lg: 0 8px 24px rgba(0,0,0,.5);
  }
}

body { background: var(--bg); color: var(--text); line-height: 1.6; }

/* ── App Shell ── */
#app { height: 100vh; display: flex; flex-direction: column; }

.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  transition: width var(--transition);
  overflow: hidden;
  flex-shrink: 0;
  z-index: 10;
}

.app-shell.sidebar-collapsed .sidebar {
  width: var(--sidebar-collapsed);
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.07);
  min-height: 60px;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.sidebar-title {
  color: #FFF;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  opacity: 1;
  transition: opacity var(--transition);
}

.app-shell.sidebar-collapsed .sidebar-title { opacity: 0; width: 0; }

.sidebar-collapse-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--sidebar-text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color var(--transition);
}

.sidebar-collapse-btn:hover { color: #FFF; }
.app-shell.sidebar-collapsed .sidebar-collapse-btn { margin-left: 0; }

.sidebar-nav { flex: 1; overflow-y: auto; padding: 8px 0; }
.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 2px; }

.sidebar-home-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  color: var(--sidebar-text-muted);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-size: 13px;
  border-radius: var(--radius-sm);
  margin: 0 6px 8px;
  width: calc(100% - 12px);
  transition: background var(--transition), color var(--transition);
}
.sidebar-home-btn:hover { background: var(--sidebar-hover); color: #FFF; }
.sidebar-home-btn span.icon { font-size: 16px; flex-shrink: 0; }
.sidebar-home-btn span.label { white-space: nowrap; overflow: hidden; }
.app-shell.sidebar-collapsed .sidebar-home-btn span.label { display: none; }

.sidebar-group { margin-bottom: 4px; }

.sidebar-group-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  color: var(--sidebar-text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  margin: 0 6px;
  transition: background var(--transition);
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
}
.sidebar-group-header:hover { background: var(--sidebar-hover); }
.sidebar-group-header .icon { font-size: 15px; flex-shrink: 0; }
.sidebar-group-header .label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.app-shell.sidebar-collapsed .sidebar-group-header .label { display: none; }
.sidebar-group-header .chevron { font-size: 10px; transition: transform var(--transition); flex-shrink: 0; color: var(--sidebar-text-muted); }
.sidebar-group-header.open .chevron { transform: rotate(90deg); }
.app-shell.sidebar-collapsed .sidebar-group-header .chevron { display: none; }

.sidebar-subgroups { overflow: hidden; }
.app-shell.sidebar-collapsed .sidebar-subgroups { display: none; }

.sidebar-subgroup {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 36px;
  color: var(--sidebar-text-muted);
  cursor: pointer;
  font-size: 12px;
  border-radius: var(--radius-sm);
  margin: 1px 6px;
  transition: background var(--transition), color var(--transition);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-subgroup:hover { background: var(--sidebar-hover); color: var(--sidebar-text); }
.sidebar-subgroup.active { background: var(--sidebar-active); color: #FFF; }
.sidebar-subgroup .badge {
  margin-left: auto;
  font-size: 10px;
  background: rgba(255,255,255,0.15);
  padding: 1px 5px;
  border-radius: 10px;
  flex-shrink: 0;
}
.sidebar-subgroup.active .badge { background: rgba(255,255,255,0.25); }

/* ── Main Content ── */
.main-content {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* ── Home Screen ── */
.home-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 40px 20px;
  gap: 32px;
}

.home-hero {
  text-align: center;
}

.home-hero h1 {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent), #8B5CF6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.home-hero p {
  color: var(--text-muted);
  font-size: 16px;
}

.home-stats {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 28px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  min-width: 140px;
}

.stat-card .value {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent);
}

.stat-card .label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.home-modes {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  justify-content: center;
}

.mode-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 32px;
  cursor: pointer;
  transition: border-color var(--transition), box-shadow var(--transition), transform var(--transition);
  text-align: center;
  width: 220px;
  box-shadow: var(--shadow-sm);
}

.mode-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow);
  transform: translateY(-2px);
}

.mode-card .mode-icon { font-size: 36px; margin-bottom: 12px; }
.mode-card h2 { font-size: 18px; font-weight: 700; margin-bottom: 6px; }
.mode-card p { font-size: 13px; color: var(--text-muted); }

.home-recent {
  width: 100%;
  max-width: 500px;
}

.home-recent h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.history-list { display: flex; flex-direction: column; gap: 8px; }

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}

.history-item .topic { color: var(--text-muted); }
.history-item .score { font-weight: 700; color: var(--accent); }
.history-item .date { font-size: 11px; color: var(--text-subtle); }

/* ── Setup Panel ── */
.setup-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 40px 20px;
  gap: 24px;
}

.setup-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow);
}

.setup-card h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 6px;
}

.setup-card .subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 24px;
}

.setup-section { margin-bottom: 20px; }

.setup-section label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.scope-options { display: flex; gap: 8px; flex-wrap: wrap; }

.scope-btn {
  padding: 8px 16px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  background: none;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: all var(--transition);
}
.scope-btn:hover { border-color: var(--accent); color: var(--accent); }
.scope-btn.active { border-color: var(--accent); background: var(--accent-light); color: var(--accent); font-weight: 600; }

.count-select {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.count-btn {
  width: 48px;
  height: 40px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all var(--transition);
}
.count-btn:hover { border-color: var(--accent); color: var(--accent); }
.count-btn.active { border-color: var(--accent); background: var(--accent); color: #FFF; }

.setup-info {
  background: var(--surface-raised);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: #FFF;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { background: var(--border); color: var(--text-muted); cursor: not-allowed; }

/* ── Study Mode ── */
.study-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 24px;
}

.session-header {
  width: 100%;
  max-width: 600px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-muted);
}

.session-title { font-weight: 600; color: var(--text); }

.progress-bar {
  width: 100%;
  max-width: 600px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s ease;
}

/* ── Flip Card ── */
.flip-card-wrap {
  perspective: 1000px;
  width: 100%;
  max-width: 600px;
  cursor: pointer;
}

.flip-card {
  display: grid;
  width: 100%;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.4,0,0.2,1);
}

.flip-card.flipped { transform: rotateY(180deg); }

.flip-card-front, .flip-card-back {
  grid-area: 1 / 1;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.flip-card-back { transform: rotateY(180deg); }

.flip-card-front {
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 16px;
  min-height: 280px;
}

.flip-card-front .card-tag {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  background: var(--accent-light);
  padding: 4px 10px;
  border-radius: 10px;
}

.flip-card-front .card-question {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.4;
}

.flip-card-front .flip-hint {
  font-size: 12px;
  color: var(--text-subtle);
  margin-top: 8px;
}

.flip-card-back .card-answer {
  font-size: 14px;
  line-height: 1.7;
  color: var(--text);
  white-space: pre-wrap;
  flex: 1;
}

.flip-card-back pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  font-size: 12px;
  line-height: 1.6;
  overflow-x: auto;
  margin-top: 14px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;
  color: var(--text);
}

.flip-card-back code { font-family: inherit; }

.study-controls {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 600px;
}

.btn-secondary {
  padding: 10px 20px;
  background: var(--surface);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  flex: 1;
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-success {
  padding: 10px 20px;
  background: var(--success);
  color: #FFF;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--transition);
  flex: 1;
}
.btn-success:hover { opacity: 0.9; }

.all-done-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 48px;
  text-align: center;
  max-width: 400px;
  box-shadow: var(--shadow);
}
.all-done-card .done-icon { font-size: 48px; margin-bottom: 16px; }
.all-done-card h2 { font-size: 22px; font-weight: 700; margin-bottom: 8px; }
.all-done-card p { color: var(--text-muted); font-size: 14px; margin-bottom: 24px; }

/* ── Quiz Mode ── */
.quiz-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 24px;
  gap: 20px;
}

.quiz-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 100%;
  max-width: 680px;
  box-shadow: var(--shadow);
}

.question-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.question-type-badge {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 3px 8px;
  border-radius: 8px;
  background: var(--accent-light);
  color: var(--accent);
}

.question-num { font-size: 12px; color: var(--text-muted); margin-left: auto; }

/* Breadcrumb showing which topic a question came from */
.question-topic {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.question-topic .topic-icon { font-size: 13px; }
.question-topic .topic-group { font-weight: 600; color: var(--text); }
.question-topic .topic-sep { color: var(--text-subtle); }
.question-topic .topic-sub { color: var(--text-muted); }

.summary-item .question-topic { margin-bottom: 6px; font-size: 11px; }

.question-text {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.5;
  margin-bottom: 16px;
  color: var(--text);
}

.question-code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  font-size: 12px;
  line-height: 1.6;
  overflow-x: auto;
  margin-bottom: 20px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;
  color: var(--text);
  white-space: pre;
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.option-btn {
  padding: 12px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-size: 14px;
  line-height: 1.5;
  transition: all var(--transition);
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.option-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--accent-light);
}

.option-btn:disabled { cursor: default; }

.option-btn.correct {
  border-color: var(--success);
  background: var(--success-bg);
  color: var(--text);
}

.option-btn.wrong {
  border-color: var(--error);
  background: var(--error-bg);
  color: var(--text);
}

.option-btn.selected-wrong {
  border-color: var(--error);
  background: var(--error-bg);
}

.option-letter {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 1px;
}

.option-btn.correct .option-letter { background: var(--success); color: #FFF; }
.option-btn.wrong .option-letter, .option-btn.selected-wrong .option-letter { background: var(--error); color: #FFF; }

.feedback-panel {
  background: var(--surface-raised);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
  font-size: 14px;
  line-height: 1.6;
  border-left: 3px solid var(--border);
}

.feedback-panel.correct { border-left-color: var(--success); }
.feedback-panel.wrong { border-left-color: var(--error); }

.feedback-label {
  font-weight: 700;
  margin-bottom: 4px;
  font-size: 13px;
}
.feedback-panel.correct .feedback-label { color: var(--success); }
.feedback-panel.wrong .feedback-label { color: var(--error); }

.quiz-footer {
  width: 100%;
  max-width: 680px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* ── Summary ── */
.summary-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 24px;
  gap: 24px;
}

.summary-hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  text-align: center;
  width: 100%;
  max-width: 680px;
  box-shadow: var(--shadow);
}

.summary-score {
  font-size: 64px;
  font-weight: 800;
  line-height: 1;
  background: linear-gradient(135deg, var(--accent), #8B5CF6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.summary-score-label { color: var(--text-muted); font-size: 14px; margin: 8px 0 16px; }

.summary-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

.summary-list { width: 100%; max-width: 680px; display: flex; flex-direction: column; gap: 12px; }

.summary-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
}

.summary-item.correct { border-left: 3px solid var(--success); }
.summary-item.wrong { border-left: 3px solid var(--error); }

.summary-item .summary-q {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.summary-item pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-size: 11px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
}

.summary-item .summary-answer {
  font-size: 13px;
  color: var(--text-muted);
}

.summary-item .summary-answer span {
  font-weight: 600;
}

.summary-item .summary-explanation {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.6;
}

.correct-answer { color: var(--success) !important; }
.wrong-answer { color: var(--error) !important; }

/* ── Utility ── */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state p { margin-top: 8px; font-size: 14px; }
