:root {
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-2: #23272f;
  --border: #30343d;
  --text: #eef0f3;
  --text-dim: #9aa1ac;
  --accent: #4f8cff;
  --accent-text: #ffffff;
  --good: #2fb872;
  --good-bg: #10321f;
  --bad: #e5584f;
  --bad-bg: #3a1614;
  --warn: var(--warn);
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

h1, h2, h3 { margin: 0 0 12px; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
p { line-height: 1.5; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.topbar .back {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 1rem;
  padding: 8px 0;
  cursor: pointer;
}

button {
  font: inherit;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: 1rem;
  font-weight: 600;
  min-height: 46px;
  touch-action: manipulation;
}

.btn:active { opacity: 0.85; }

.btn.secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.danger {
  background: var(--bad-bg);
  color: var(--bad);
  border: 1px solid var(--bad);
}

.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.card.danger-zone { border-color: var(--bad); margin-top: 24px; }

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

.deck-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  gap: 12px;
}

.deck-row .info { flex: 1; min-width: 0; }
.deck-row .name { font-weight: 600; font-size: 1.05rem; }
.deck-row .meta { color: var(--text-dim); font-size: 0.85rem; margin-top: 2px; }
.deck-row .actions { display: flex; gap: 8px; flex-shrink: 0; }

.deck-row.pending-delete { opacity: 0.55; }
.deck-row.pending-delete .meta { color: var(--bad); }

.deck-type-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  margin-bottom: 4px;
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.deck-type-badge.flashcard { color: var(--accent); border-color: var(--accent); }
.deck-type-badge.quiz { color: var(--good); border-color: var(--good); }

.type-toggle-btn { opacity: 0.6; }
.type-toggle-btn.active { opacity: 1; border-color: var(--accent); color: var(--accent); }

.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}

.icon-btn.danger { color: var(--bad); border-color: var(--bad); }

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

input[type=text], input[type=email], input[type=password], textarea, select {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 1rem;
  font-family: inherit;
}

textarea { resize: vertical; min-height: 60px; }

.bulk-textarea {
  min-height: 220px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  white-space: pre;
}

.form-error {
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--bad-bg);
  border: 1px solid var(--bad);
  color: var(--bad);
  border-radius: 8px;
  font-size: 0.85rem;
  line-height: 1.4;
}

label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 6px;
  margin-top: 12px;
}
label:first-child { margin-top: 0; }

.field { margin-bottom: 4px; }

.empty {
  color: var(--text-dim);
  text-align: center;
  padding: 40px 16px;
}

.question-list { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }

.question-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.question-item .qtext { font-weight: 500; margin-bottom: 6px; }
.question-item .opts { color: var(--text-dim); font-size: 0.85rem; }
.question-item .opts .correct { color: var(--good); }
.question-item .actions { margin-top: 10px; display: flex; gap: 8px; }
.question-item .actions button { flex: 1; padding: 8px; font-size: 0.85rem; min-height: 38px; }

.question-item.snippet { cursor: pointer; }
.question-item.snippet .snippet-text {
  margin-bottom: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.image-preview-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.image-preview {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-dim);
  cursor: pointer;
}

.checkbox-row input[type=checkbox] { width: auto; }

.explanation-inline {
  margin-top: 8px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  font-size: 0.85rem;
  color: var(--text-dim);
  line-height: 1.45;
}

.deck-menu { display: flex; flex-direction: column; gap: 10px; }

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
  cursor: pointer;
  font-size: 0.95rem;
}

.switch-row input[type=checkbox] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-row .switch {
  position: relative;
  flex-shrink: 0;
  width: 44px;
  height: 26px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: background 0.15s ease;
}

.switch-row .switch::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: transform 0.15s ease, background 0.15s ease;
}

.switch-row input:checked ~ .switch {
  background: var(--accent);
  border-color: var(--accent);
}

.switch-row input:checked ~ .switch::before {
  transform: translateX(18px);
  background: var(--accent-text);
}

.section-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  margin-top: 20px;
}

.progress-bar {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 20px;
}
.progress-bar .fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.2s ease;
}

.quiz-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--text-dim);
  font-size: 0.9rem;
  margin-bottom: 10px;
}

.quiz-meta .icon-btn {
  width: 32px;
  height: 32px;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.question-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

.question-card .qtext {
  font-size: 1.15rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.question-image {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: var(--radius);
  margin-bottom: 12px;
  background: var(--surface-2);
}

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

.option-btn {
  text-align: left;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 1rem;
  min-height: 50px;
}

.option-btn:active { opacity: 0.85; }
.option-btn.correct { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.option-btn.incorrect { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }
.option-btn.disabled-not-picked { opacity: 0.5; }
.option-btn:disabled { cursor: default; }

.explanation {
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  font-size: 0.95rem;
  color: var(--text-dim);
  line-height: 1.5;
}

.explanation strong { color: var(--text); }

.quiz-nav {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
.quiz-nav-prev { flex: 0 0 auto; }
.quiz-nav-next { flex: 1; }

.score-hero {
  text-align: center;
  padding: 24px 16px;
}
.score-hero .big {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent);
}
.score-hero .sub { color: var(--text-dim); margin-top: 4px; }

.missed-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
}
.missed-item .qtext { font-weight: 600; margin-bottom: 8px; }
.missed-item .answer-row { font-size: 0.9rem; margin-bottom: 4px; }
.missed-item .your-answer { color: var(--bad); }
.missed-item .correct-answer { color: var(--good); }

.flip-card { cursor: pointer; min-height: 140px; display: flex; align-items: center; justify-content: center; text-align: center; }
.flip-card:active { opacity: 0.9; }
.flip-hint { padding: 8px 0 0; }

.best-score-card { text-align: center; padding: 16px; }
.best-score-card .best-label { color: var(--text-dim); font-size: 0.85rem; }
.best-score-card .best-value { font-size: 1.8rem; font-weight: 700; color: var(--accent); margin-top: 2px; }

.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 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.history-item .history-date { color: var(--text-dim); font-size: 0.85rem; }
.history-item .history-score { font-weight: 600; }
.history-item .history-pct { color: var(--text-dim); font-weight: 400; font-size: 0.85rem; }

.category-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.category-badge.objective { color: var(--accent); border-color: var(--accent); }

/* Weak-area stats page */
.stat-list { display: flex; flex-direction: column; gap: 10px; }
.stat-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.stat-row-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.stat-name { font-weight: 600; }
.stat-pct { font-weight: 700; font-size: 1.05rem; }
.stat-bar-bg { background: var(--surface-2); border-radius: 999px; height: 6px; overflow: hidden; }
.stat-bar-fill { height: 100%; border-radius: 999px; }
.stat-row-bottom { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-top: 8px; }
.stat-meta { color: var(--text-dim); font-size: 0.8rem; }

.trend-sparkline { display: flex; align-items: flex-end; gap: 3px; height: 22px; }
.trend-bar { width: 6px; border-radius: 2px 2px 0 0; min-height: 2px; }

.stat-good { color: var(--good); }
.stat-mid { color: var(--warn); }
.stat-bad { color: var(--bad); }
.stat-bar-fill.stat-good, .trend-bar.stat-good { background: var(--good); }
.stat-bar-fill.stat-mid, .trend-bar.stat-mid { background: var(--warn); }
.stat-bar-fill.stat-bad, .trend-bar.stat-bad { background: var(--bad); }

.stat-row-wrap { display: flex; flex-direction: column; }
.stat-toggle {
  align-self: flex-start;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 8px 2px 0;
}
.stat-list.nested { margin: 8px 0 0 16px; padding-left: 12px; border-left: 2px solid var(--border); }
.stat-subnote { padding: 6px 0 0 16px; font-size: 0.78rem; text-align: left; }

/* Quiz setup category/objective filters */
.filter-category-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 4px; }
.filter-category-group + .filter-category-group { border-top: 1px solid var(--border); padding-top: 10px; }
.filter-category-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.filter-category-row .stat-toggle { padding-top: 0; }
.filter-objective-list { margin: 8px 0 0 16px; padding-left: 12px; border-left: 2px solid var(--border); }
.filter-weak-areas-meta { padding-left: 24px; margin-top: 2px; }

.toast {
  position: fixed;
  bottom: calc(16px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 50;
}

.hidden { display: none !important; }

.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: var(--surface-2);
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  text-align: center;
  font-size: 0.8rem;
  padding: 6px 12px calc(6px);
  padding-top: calc(6px + env(safe-area-inset-top));
  transform: translateY(-100%);
  transition: transform 0.2s ease;
}

.offline-banner.visible { transform: translateY(0); }

@media (min-width: 640px) {
  #app { padding-top: 32px; }
}

/* Lectures */
.lecture-search { margin: 12px 0 8px; }
.lecture-count { margin: 0 0 8px; }
.lecture-list { display: flex; flex-direction: column; gap: 10px; }
.lecture-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
}
.lecture-row:active { opacity: 0.85; }
.lecture-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.lecture-row-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.lecture-row-title { font-weight: 600; }
.lecture-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.lecture-header { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }

/* Tags naming one of the deck's question categories are the broad ones;
   all others are narrower sub-topics. */
.tag-chip {
  display: inline-block;
  font: inherit;
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
}
.tag-chip.category { background: var(--surface-2); color: var(--text); font-weight: 600; }
button.tag-chip { cursor: pointer; }

.lecture-body { margin-top: 16px; line-height: 1.6; overflow-wrap: anywhere; }
.lecture-body h2, .lecture-body h3, .lecture-body h4, .lecture-body h5 { margin: 20px 0 8px; }
.lecture-body p, .lecture-body ul, .lecture-body ol, .lecture-body pre, .lecture-body blockquote, .lecture-body .table-scroll { margin: 0 0 12px; }
.lecture-body ul, .lecture-body ol { padding-left: 22px; }
.lecture-body li { margin-bottom: 4px; }
.lecture-body code { background: var(--surface-2); border-radius: 4px; padding: 1px 5px; font-size: 0.9em; }
.lecture-body pre { background: var(--surface-2); border-radius: 8px; padding: 12px; overflow-x: auto; }
.lecture-body pre code { background: none; padding: 0; }
.lecture-body blockquote {
  padding: 10px 14px;
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
}
.lecture-body blockquote > :last-child { margin-bottom: 0; }
.lecture-body hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.table-scroll { overflow-x: auto; }
.lecture-body table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
.lecture-body th, .lecture-body td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.lecture-body th { background: var(--surface-2); }

.lecture-link { margin-top: 10px; width: 100%; text-align: left; }
.lecture-delete summary { cursor: pointer; color: var(--bad); font-weight: 600; }
.lecture-delete p { margin: 10px 0; }

.audio-badge { margin-left: auto; font-size: 1rem; }
.audio-card { margin-top: 16px; }
.audio-title { font-weight: 600; margin-bottom: 10px; overflow-wrap: anywhere; }
.audio-card audio { width: 100%; }
.audio-rate { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.audio-rate select { width: auto; }
.audio-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.audio-transfer progress { width: 100%; height: 10px; }
.audio-remove { margin-top: 16px; }
.audio-remove summary { cursor: pointer; color: var(--bad); font-weight: 600; }
.audio-remove p { margin: 10px 0; }

.tts-card { margin-top: 16px; }
.tts-title { font-weight: 600; margin-bottom: 10px; }
.tts-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.tts-rate { display: flex; align-items: center; gap: 8px; }
.tts-rate select { width: auto; }
.tts-card progress { width: 100%; height: 10px; margin-top: 8px; }
