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

:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-soft: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-soft: #eef2ff;
  --success: #10b981;
  --success-soft: #d1fae5;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --warning: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.07), 0 2px 4px -2px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 10px 15px -3px rgba(15, 23, 42, 0.08), 0 4px 6px -4px rgba(15, 23, 42, 0.05);
  --radius: 0.75rem;
  --radius-sm: 0.5rem;
  --radius-lg: 1rem;
  --header-h: 64px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: linear-gradient(135deg, #f8fafc 0%, #eef2ff 100%);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* === Header === */
.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1.5rem;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  box-shadow: var(--shadow-lg);
}

.menu-btn {
  display: none;
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  width: 40px; height: 40px;
  border-radius: var(--radius-sm);
  font-size: 1.25rem;
  cursor: pointer;
  transition: background 0.15s;
}
.menu-btn:hover { background: rgba(255,255,255,0.25); }

.brand {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  flex: 1;
  min-width: 0;
}

.brand-icon {
  font-size: 2rem;
  font-weight: 900;
  background: rgba(255,255,255,0.2);
  width: 44px; height: 44px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text p {
  margin: 0;
  font-size: 0.8125rem;
  opacity: 0.85;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mode-switcher {
  display: flex;
  gap: 0.25rem;
  background: rgba(255,255,255,0.15);
  border-radius: var(--radius-sm);
  padding: 0.25rem;
}

.mode-btn {
  background: transparent;
  border: none;
  color: white;
  padding: 0.5rem 0.875rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.mode-btn:hover { background: rgba(255,255,255,0.15); }

.mode-btn.is-active {
  background: white;
  color: var(--primary);
  font-weight: 600;
}

/* === Layout === */
.app-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: calc(100vh - var(--header-h));
  max-width: 1400px;
  margin: 0 auto;
}

/* === Sidebar === */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1.25rem;
  overflow-y: auto;
  position: sticky;
  top: var(--header-h);
  height: calc(100vh - var(--header-h));
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 700;
}

.close-btn {
  display: none;
  background: transparent;
  border: none;
  font-size: 1.75rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
}
.close-btn:hover { background: var(--surface-3); color: var(--text); }

.filter-section {
  margin-bottom: 1.5rem;
}

.filter-section h3 {
  margin: 0 0 0.625rem 0;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.field-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
.field-label > span { display: block; margin-bottom: 0.5rem; }

.field-label input,
.select-filter {
  width: 100%;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: all 0.15s;
}

.field-label input:focus,
.select-filter:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* === Chips === */
.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.chip-list-compact { gap: 0.25rem; }

.chip {
  --chip-color: #94a3b8;
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.4rem 0.75rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 999px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
  white-space: nowrap;
}

.chip:hover {
  border-color: var(--chip-color);
  background: var(--surface-2);
}

.chip.is-active {
  background: var(--chip-color);
  border-color: var(--chip-color);
  color: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}

.chip-icon { font-size: 0.95em; }
.chip-text { line-height: 1; }

/* === Filter actions === */
.filter-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.625rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
  background: var(--surface-3);
  color: var(--text);
}
.btn-secondary:hover { background: var(--border); }

.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
}
.btn-danger:hover { background: #fecaca; }

/* === Sidebar footer === */
.sidebar-footer {
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.sidebar-footer p { margin: 0 0 0.375rem 0; }
.sidebar-footer a { color: var(--primary); text-decoration: none; }
.sidebar-footer a:hover { text-decoration: underline; }
.sidebar-footer .hint { font-style: italic; opacity: 0.85; }

/* === Main content === */
.main-content {
  padding: 1.5rem 2rem 4rem;
  max-width: 100%;
  min-width: 0;
}

/* === Stats bar === */
.stats-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.5rem;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

.stat-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-transform: lowercase;
}

.progress-bar {
  grid-column: 1 / -1;
  height: 6px;
  background: var(--surface-3);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 0.5rem;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #8b5cf6);
  transition: width 0.3s ease;
  border-radius: 999px;
}

/* === Random button === */
.btn-random {
  width: 100%;
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
  margin-bottom: 1.5rem;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}
.btn-random:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
}

/* === Question card === */
.questions-container {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.question-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: all 0.2s;
  box-shadow: var(--shadow-sm);
}

.question-card:hover {
  box-shadow: var(--shadow);
  border-color: var(--border-strong);
}

.question-card.is-completed {
  background: linear-gradient(135deg, var(--surface) 0%, var(--success-soft) 100%);
  border-color: var(--success);
}

.question-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
}

.question-meta {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.badge {
  --badge-color: #6366f1;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.625rem;
  border-radius: 0.4rem;
  font-size: 0.75rem;
  font-weight: 600;
  background: color-mix(in srgb, var(--badge-color) 12%, white);
  color: var(--badge-color);
  border: 1px solid color-mix(in srgb, var(--badge-color) 25%, white);
  white-space: nowrap;
}

.badge-num {
  background: var(--text);
  color: white;
  border-color: var(--text);
}

.badge-type {
  background: var(--surface-3);
  color: var(--text-muted);
  border-color: var(--border);
}

.btn-complete {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn-complete:hover {
  border-color: var(--success);
  color: var(--success);
  background: var(--success-soft);
}

.btn-complete.is-on {
  background: var(--success);
  border-color: var(--success);
  color: white;
}

.question-body {
  padding: 1.5rem;
}

.question-text {
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text);
  margin-bottom: 1.25rem;
}

.question-image {
  margin: 1rem 0 1.25rem;
  text-align: center;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 1rem;
}

.question-image img {
  max-width: 100%;
  height: auto;
  border-radius: 0.4rem;
}

/* === ABCD answers === */
.answers-abcd {
  display: grid;
  gap: 0.625rem;
}

.answer-btn {
  display: flex;
  align-items: flex-start;
  gap: 0.875rem;
  text-align: left;
  width: 100%;
  padding: 0.875rem 1rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.9375rem;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text);
}

.answer-btn:hover:not(.locked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.answer-btn.locked {
  cursor: default;
  opacity: 0.85;
}

.answer-btn.is-correct {
  border-color: var(--success);
  background: var(--success-soft);
  opacity: 1;
}

.answer-btn.is-wrong {
  border-color: var(--danger);
  background: var(--danger-soft);
  opacity: 1;
}

.answer-letter {
  flex-shrink: 0;
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--surface-3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.875rem;
  color: var(--text-muted);
  transition: all 0.15s;
}

.answer-btn:hover:not(.locked) .answer-letter {
  background: var(--primary);
  color: white;
}

.answer-btn.is-correct .answer-letter {
  background: var(--success);
  color: white;
}

.answer-btn.is-wrong .answer-letter {
  background: var(--danger);
  color: white;
}

.answer-text {
  flex: 1;
  min-width: 0;
  padding-top: 0.25rem;
}

.answer-text img { max-width: 100%; height: auto; }

/* === True/False answers === */
.answers-tf {
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
}

.tf-row {
  display: flex;
  gap: 1rem;
  align-items: center;
  padding: 0.875rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  flex-wrap: wrap;
}

.tf-statement {
  flex: 1;
  min-width: 200px;
  font-size: 0.9375rem;
  line-height: 1.5;
}

.tf-num {
  font-weight: 700;
  color: var(--primary);
  margin-right: 0.4rem;
}

.tf-choices {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.tf-btn {
  padding: 0.5rem 1.25rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text);
  min-width: 80px;
}

.tf-btn:hover:not(.locked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.tf-btn.locked { cursor: default; opacity: 0.85; }
.tf-btn.is-correct {
  border-color: var(--success);
  background: var(--success);
  color: white;
  opacity: 1;
}
.tf-btn.is-wrong {
  border-color: var(--danger);
  background: var(--danger);
  color: white;
  opacity: 1;
}

/* === Explanation === */
.explanation {
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

.explanation summary {
  padding: 0.875rem 1.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--primary);
  user-select: none;
  list-style: none;
  transition: background 0.15s;
}

.explanation summary::-webkit-details-marker { display: none; }
.explanation summary:hover { background: var(--primary-soft); }

.explanation[open] summary {
  background: var(--primary-soft);
  border-bottom: 1px solid var(--border);
}

.explanation-content {
  padding: 1.25rem 1.5rem;
  font-size: 0.9375rem;
  line-height: 1.65;
  color: var(--text);
}

.explanation-content img { max-width: 100%; height: auto; }

/* === Empty state === */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.empty-icon { font-size: 3rem; margin-bottom: 1rem; }
.empty-state h3 { margin: 0 0 0.5rem; font-size: 1.25rem; }
.empty-state p { margin: 0; color: var(--text-muted); }

/* === Pagination === */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin-top: 2rem;
  padding: 1rem;
}

.pag-btn {
  padding: 0.625rem 1.25rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s;
}

.pag-btn:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}

.pag-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.pag-info {
  font-size: 0.875rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* === Responsive === */
@media (max-width: 1024px) {
  .app-layout {
    grid-template-columns: 280px 1fr;
  }
  .main-content { padding: 1.25rem 1.5rem 4rem; }
}

@media (max-width: 768px) {
  .app-header {
    padding: 0 1rem;
  }
  .menu-btn { display: flex; align-items: center; justify-content: center; }
  .brand-text p { display: none; }
  .brand-text h1 { font-size: 1rem; }
  .brand-icon { width: 36px; height: 36px; font-size: 1.5rem; }

  .mode-btn { padding: 0.4rem 0.625rem; font-size: 0.8125rem; }

  .app-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    width: 320px;
    max-width: 90vw;
    height: calc(100vh - var(--header-h));
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .close-btn { display: flex; align-items: center; justify-content: center; }

  .main-content { padding: 1rem; }

  .stats-bar {
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    padding: 1rem;
  }

  .stat-value { font-size: 1.5rem; }

  .question-header {
    padding: 0.875rem 1rem;
  }

  .question-body { padding: 1.25rem 1rem; }

  .btn-complete {
    width: 100%;
    margin-top: 0.5rem;
  }

  .answer-btn { padding: 0.75rem 0.875rem; font-size: 0.875rem; }
  .answer-letter { width: 28px; height: 28px; font-size: 0.8125rem; }

  .tf-row { flex-direction: column; align-items: stretch; }
  .tf-choices { justify-content: stretch; }
  .tf-btn { flex: 1; }
}

@media (max-width: 480px) {
  .stats-bar { grid-template-columns: 1fr; }
  .badge { font-size: 0.7rem; padding: 0.25rem 0.5rem; }
  .question-text { font-size: 0.9375rem; }
}

/* === KaTeX overrides === */
.katex { font-size: 1.05em; }
.katex-display { margin: 0.5em 0; overflow-x: auto; overflow-y: hidden; }
