:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263248;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #14b8a6;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: radial-gradient(circle at top right, #172554 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.55;
}

code {
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 0.9em;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(11, 17, 32, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}
.brand-icon { font-size: 1.6rem; }
.brand-title { font-weight: 700; font-size: 1.1rem; letter-spacing: -0.02em; }
.brand-sub { font-size: 0.75rem; color: var(--muted); }

.nav-tabs {
  display: flex;
  gap: 6px;
}

.nav-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: 0.92rem;
}
.nav-btn:hover { color: var(--text); background: var(--surface-2); }
.nav-btn.active {
  color: var(--text);
  background: rgba(59, 130, 246, 0.18);
  border-color: rgba(59, 130, 246, 0.45);
}

.user-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-pill {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
}
.stat-pill.streak { color: #fbbf24; }
.user-label { font-size: 0.85rem; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Container & Cards */
.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: 28px 20px 60px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
}

/* Auth */
.auth-wrapper {
  display: flex;
  justify-content: center;
  padding-top: 32px;
}
.auth-card { width: 100%; max-width: 440px; }
.auth-header h1 { margin: 0 0 6px; font-size: 1.5rem; }
.domain-note {
  background: rgba(20, 184, 166, 0.12);
  border: 1px solid rgba(20, 184, 166, 0.3);
  color: #5eead4;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 18px 0;
}
.tab-btn {
  padding: 9px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  cursor: pointer;
  font-weight: 600;
}
.tab-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* Forms & Buttons */
.form-stack { display: flex; flex-direction: column; gap: 14px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 0.88rem; color: var(--muted); }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], textarea, select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.95rem;
  font-family: inherit;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
}
.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  color: var(--text);
  cursor: pointer;
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--accent); color: #042f2e; }
.btn-secondary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-danger { background: rgba(239, 68, 68, 0.18); color: #fca5a5; border: 1px solid rgba(239, 68, 68, 0.4); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.3); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-link { background: transparent; color: #93c5fd; padding: 4px; }
.btn-sm { padding: 6px 12px; font-size: 0.84rem; }
.btn-full { width: 100%; }

.select-sm { padding: 6px 10px; font-size: 0.86rem; }
.row-gap { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row-gap.center { justify-content: center; }
.row-gap.end { justify-content: flex-end; }

/* Banners & Toast */
.banner {
  padding: 10px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  font-size: 0.9rem;
}
.banner.success { background: rgba(34, 197, 94, 0.14); border: 1px solid rgba(34, 197, 94, 0.4); color: #86efac; }
.banner.error { background: rgba(239, 68, 68, 0.14); border: 1px solid rgba(239, 68, 68, 0.4); color: #fca5a5; }
.banner a { color: #fff; text-decoration: underline; }

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  background: var(--surface-2);
  border: 1px solid var(--primary);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 10px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
  max-width: 360px;
}
.toast.error { border-color: var(--danger); }

/* Chapters Grid */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.section-header.compact { margin-bottom: 12px; }
.section-header h2, .section-header h3 { margin: 0 0 4px; }

.chapters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 18px;
}

.chapter-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chapter-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.chapter-num {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
}
.chapter-title { margin: 2px 0 0; font-size: 1.15rem; }

.sections-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.sec-pill {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  cursor: pointer;
}
.sec-pill:hover { border-color: var(--primary); color: #93c5fd; }

/* Progress Bar */
.progress-wrap { margin-bottom: 18px; }
.progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}
.progress-bar {
  height: 9px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  transition: width 0.3s ease;
}

/* Quiz Arena */
.quiz-topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  gap: 12px;
  flex-wrap: wrap;
}
.quiz-controls { display: flex; gap: 8px; flex-wrap: wrap; }

.quiz-card { max-width: 780px; margin: 0 auto; }
.quiz-meta-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: rgba(59, 130, 246, 0.16);
  color: #93c5fd;
}
.badge.easy { background: rgba(34, 197, 94, 0.16); color: #86efac; }
.badge.medium { background: rgba(245, 158, 11, 0.16); color: #fcd34d; }
.badge.hard { background: rgba(239, 68, 68, 0.16); color: #fca5a5; }
.badge.draft { background: rgba(148, 163, 184, 0.2); color: #cbd5e1; }

.q-prompt { font-size: 1.08rem; margin-bottom: 18px; }
.chem-content p:first-child { margin-top: 0; }
.chem-content p:last-child { margin-bottom: 0; }

.q-media {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin: 16px 0 20px;
  padding: 14px;
  background: #ffffff;
  border-radius: 10px;
}
.q-diagram-img { max-width: 100%; max-height: 280px; object-fit: contain; }
.q-smiles-canvas { max-width: 100%; }

.options-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.option-btn {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  font-size: 0.98rem;
  transition: border-color 0.15s, background 0.15s;
}
.option-btn:hover:not(:disabled) {
  border-color: var(--primary);
  background: rgba(59, 130, 246, 0.12);
}
.option-btn:disabled { cursor: default; }
.option-letter {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
}
.option-btn.correct {
  border-color: var(--success);
  background: rgba(34, 197, 94, 0.16);
}
.option-btn.correct .option-letter { background: var(--success); color: #052e16; border-color: var(--success); }
.option-btn.incorrect {
  border-color: var(--danger);
  background: rgba(239, 68, 68, 0.16);
}
.option-btn.incorrect .option-letter { background: var(--danger); color: #fff; border-color: var(--danger); }

.feedback-box {
  margin-top: 20px;
  padding: 16px 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
}
.feedback-box.correct { border-color: rgba(34, 197, 94, 0.5); }
.feedback-box.incorrect { border-color: rgba(239, 68, 68, 0.5); }
.feedback-title { font-weight: 700; margin-bottom: 8px; font-size: 1.02rem; }
.feedback-box.correct .feedback-title { color: #86efac; }
.feedback-box.incorrect .feedback-title { color: #fca5a5; }
.feedback-actions { margin-top: 16px; display: flex; justify-content: flex-end; }

/* Profile & Leaderboard */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.three-col { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.options-edit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.upload-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 14px; }
.full-span { grid-column: 1 / -1; }

.stat-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 12px;
}
.stat-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
}
.stat-val { font-size: 1.45rem; font-weight: 700; color: var(--text); }
.stat-lbl { font-size: 0.8rem; color: var(--muted); }

.divider { border: none; border-top: 1px solid var(--border); margin: 22px 0; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
th { color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; }
tr.me-row { background: rgba(59, 130, 246, 0.14); font-weight: 600; }

/* Admin Questions */
.admin-section-block { margin-bottom: 22px; }
.admin-section-title {
  font-weight: 700;
  color: var(--accent);
  margin: 0 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.admin-q-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 10px;
}
.admin-q-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.72); }
.modal-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 760px;
  max-height: 90vh;
  overflow-y: auto;
}
.preview-box {
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 12px;
}

.empty-state { text-align: center; padding: 40px 24px; }
.empty-icon { font-size: 2.4rem; margin-bottom: 10px; }

@media (max-width: 780px) {
  .two-col, .three-col, .options-edit-grid, .upload-grid { grid-template-columns: 1fr; }
}
