:root {
  --bg: #f5f0e8;
  --bg-card: #ede7db;
  --bg-input: #e4ddd0;
  --text: #2a2520;
  --text-muted: #6b6159;
  --text-faint: #a89e93;
  --accent: #3c7a7b;
  --accent-dim: rgba(60, 122, 123, 0.1);
  --accent-light: #5fa3a4;
  --rust: #b45a3c;
  --rust-light: #d4886e;
  --success: #2d6a4f;
  --error: #b45a3c;
  --warning: #92640a;
  --border: #d4cdc2;
  --border-light: #e4ddd0;
  --radius: 6px;
  --radius-sm: 4px;
  --font: 'DM Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-serif: 'Instrument Serif', Georgia, serif;
  --mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  --margin-red: #c9827a;
  --rule-blue: #8bafc4;
  --code-bg: #2a2520;
  --code-text: #e2d9cf;
}

/* Dark mode — warm dark, not cold blue */
[data-theme="dark"] {
  --bg: #1a1714;
  --bg-card: #242019;
  --bg-input: #2e2a22;
  --text: #e2d9cf;
  --text-muted: #a89e93;
  --text-faint: #6b6159;
  --accent: #5fa3a4;
  --accent-dim: rgba(95, 163, 164, 0.12);
  --accent-light: #7bbcbd;
  --rust: #d4886e;
  --rust-light: #e5a78f;
  --success: #5fa37a;
  --error: #d4886e;
  --warning: #c9a84c;
  --border: #3a352d;
  --border-light: #2e2a22;
  --margin-red: #8b5a52;
  --rule-blue: #5a7f94;
  --code-bg: #141210;
  --code-text: #e2d9cf;
}

/* Auto-detect system preference */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1714;
    --bg-card: #242019;
    --bg-input: #2e2a22;
    --text: #e2d9cf;
    --text-muted: #a89e93;
    --text-faint: #6b6159;
    --accent: #5fa3a4;
    --accent-dim: rgba(95, 163, 164, 0.12);
    --accent-light: #7bbcbd;
    --rust: #d4886e;
    --rust-light: #e5a78f;
    --success: #5fa37a;
    --error: #d4886e;
    --warning: #c9a84c;
    --border: #3a352d;
    --border-light: #2e2a22;
    --margin-red: #8b5a52;
    --rule-blue: #5a7f94;
    --code-bg: #141210;
    --code-text: #e2d9cf;
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  font-family: var(--font);
  font-weight: 300;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  line-height: 1.6;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 640px;
  margin: 0 auto;
  position: relative;
}

/* Notebook margin line */
#app::before {
  content: '';
  position: fixed;
  top: 0; bottom: 0;
  left: calc(50% - 320px + 40px);
  width: 1px;
  background: var(--margin-red);
  opacity: 0.15;
  pointer-events: none;
  z-index: 0;
}

@media (max-width: 640px) {
  #app::before { left: 40px; }
}

/* Animations */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* Top bar */
#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
  z-index: 5;
}

#topbar-title {
  flex: 1;
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 400;
}

#topbar-status { display: flex; align-items: center; gap: 8px; }

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--error);
  transition: background 0.3s;
}

.dot.online { background: var(--success); }

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.2s;
}

.icon-btn:hover { background: var(--bg-input); }
.icon-btn:active { background: var(--border); }
.hidden { display: none !important; }

/* Main content */
#content {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  -webkit-overflow-scrolling: touch;
  animation: fadeIn 0.3s ease;
}

/* Bottom nav */
#bottomnav {
  display: flex;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom, 0);
  background: var(--bg);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 0;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 10px;
  font-weight: 400;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.2s;
}

.nav-btn:hover { color: var(--text-muted); }
.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 20px; }

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 12px;
  animation: fadeUp 0.35s ease;
}

.card-title {
  font-family: var(--font-serif);
  font-size: 18px;
  font-weight: 400;
  margin-bottom: 6px;
  color: var(--text);
}

.card-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 12px;
  line-height: 1.5;
}

.card-meta {
  display: flex;
  gap: 12px;
  font-size: 11px;
  letter-spacing: 0.5px;
  color: var(--text-faint);
  text-transform: uppercase;
}

.curriculum-card {
  cursor: pointer;
  transition: border-color 0.2s;
}

.curriculum-card:hover {
  border-color: var(--accent);
}

.module-card {
  cursor: pointer;
  transition: border-color 0.2s;
}

.module-card:hover {
  border-color: var(--accent);
}

/* Section labels */
.section-label {
  font-size: 10px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 8px;
  font-weight: 500;
}

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.8px;
  text-transform: uppercase;
}

.badge-ready { background: var(--accent-dim); color: var(--accent); }
.badge-building { background: rgba(146, 100, 10, 0.12); color: var(--warning); }
.badge-draft { background: var(--bg-input); color: var(--text-faint); }
.badge-spec_ready { background: var(--accent-dim); color: var(--accent); }
.badge-error { background: rgba(180, 90, 60, 0.1); color: var(--error); }

/* Chat-style question/answer flow */
.chat-bubble {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 14px;
  max-width: 88%;
  line-height: 1.65;
  font-size: 15px;
  animation: msgIn 0.35s ease;
}

.chat-bubble.system {
  border-left: 3px solid var(--accent);
}

.chat-bubble.user {
  margin-left: auto;
  background: rgba(180, 90, 60, 0.06);
  border-right: 3px solid var(--rust-light);
}

.chat-bubble p { margin-bottom: 8px; }
.chat-bubble p:last-child { margin-bottom: 0; }

.chat-bubble code {
  font-family: var(--mono);
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 13px;
  border: 1px solid var(--border-light);
}

.chat-bubble pre {
  background: var(--code-bg);
  color: var(--code-text);
  padding: 14px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  line-height: 1.5;
}

.chat-bubble pre code {
  background: none;
  padding: 0;
  border: none;
  color: inherit;
}

/* MCQ choices */
.choices {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
}

.choice-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.choice-btn:hover { border-color: var(--accent); }
.choice-btn:active { background: var(--accent-dim); }
.choice-btn.selected { border-color: var(--accent); background: var(--accent-dim); }
.choice-btn.correct { border-color: var(--success); background: rgba(45, 106, 79, 0.1); }
.choice-btn.wrong { border-color: var(--error); background: rgba(180, 90, 60, 0.08); }

/* Flashcard */
.flashcard {
  perspective: 1000px;
  cursor: pointer;
  min-height: 180px;
  margin-bottom: 12px;
}

.flashcard-inner {
  position: relative;
  width: 100%;
  min-height: 180px;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}

.flashcard.flipped .flashcard-inner {
  transform: rotateY(180deg);
}

.flashcard-front, .flashcard-back {
  position: absolute;
  width: 100%;
  min-height: 180px;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-serif);
  font-size: 20px;
  text-align: center;
  line-height: 1.4;
}

.flashcard-back {
  transform: rotateY(180deg);
  font-family: var(--font);
  font-size: 16px;
}

.srs-buttons {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.srs-btn {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  cursor: pointer;
  color: white;
  transition: opacity 0.2s;
}

.srs-btn:hover { opacity: 0.85; }
.srs-btn[data-rating="0"] { background: var(--error); }
.srs-btn[data-rating="1"] { background: #92640a; }
.srs-btn[data-rating="2"] { background: var(--success); }
.srs-btn[data-rating="3"] { background: var(--accent); }

/* Text input */
.input-area {
  margin-top: 12px;
}

textarea, input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  font-weight: 300;
  resize: vertical;
  min-height: 80px;
  transition: border-color 0.2s;
  outline: none;
}

textarea:focus, input[type="text"]:focus {
  border-color: var(--accent);
}

textarea::placeholder, input::placeholder {
  color: var(--text-faint);
  font-style: italic;
}

/* Code editor area */
.code-area {
  margin-top: 12px;
}

.code-area textarea {
  font-family: var(--mono);
  font-size: 13px;
  min-height: 150px;
  tab-size: 4;
  background: var(--code-bg);
  color: var(--code-text);
  border-color: var(--border);
}

.code-area textarea:focus {
  border-color: var(--accent);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 400;
  font-family: var(--font);
  letter-spacing: 0.5px;
  cursor: pointer;
  color: var(--bg);
  transition: all 0.2s;
}

.btn:hover { opacity: 0.88; }
.btn:active { opacity: 0.75; }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--bg-input); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border-light); }
.btn-success { background: var(--success); color: white; }
.btn-block { width: 100%; }

/* Feedback / grade display */
.feedback {
  padding: 14px 18px;
  border-radius: var(--radius);
  margin-top: 12px;
  font-size: 14px;
  line-height: 1.6;
  animation: fadeUp 0.3s ease;
}

.feedback.positive { background: rgba(45, 106, 79, 0.08); border-left: 3px solid var(--success); }
.feedback.negative { background: rgba(180, 90, 60, 0.06); border-left: 3px solid var(--error); }
.feedback.neutral { background: var(--bg-card); border-left: 3px solid var(--accent); }
.feedback.pending { background: rgba(146, 100, 10, 0.06); border-left: 3px solid var(--warning); }

.score-display {
  font-family: var(--font-serif);
  font-size: 28px;
  font-weight: 400;
  margin-bottom: 8px;
  color: var(--text);
}

/* Scenario nodes */
.scenario-text {
  font-size: 15px;
  line-height: 1.65;
  margin-bottom: 12px;
}

.scenario-feedback {
  font-family: var(--font-serif);
  font-style: italic;
  color: var(--accent);
  margin-bottom: 12px;
}

/* Progress bar */
.progress-bar {
  height: 3px;
  background: var(--border-light);
  border-radius: 2px;
  overflow: hidden;
  margin: 8px 0;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.4s ease;
}

/* Settings */
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--border-light);
}

.setting-label { font-size: 14px; }
.setting-value { font-size: 13px; color: var(--text-muted); }

/* New curriculum form */
.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  font-size: 10px;
  letter-spacing: 1.8px;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 6px;
  font-weight: 500;
}

.form-group input, .form-group select {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 300;
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus, .form-group select:focus {
  border-color: var(--accent);
}

.form-group input::placeholder {
  color: var(--text-faint);
  font-style: italic;
}

/* Loading spinner */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.loading-msg {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  color: var(--text-muted);
  font-size: 14px;
}

/* Loading dots (for chat) */
.loading-dots {
  display: flex;
  gap: 5px;
  padding: 8px 0;
}

.loading-dots span {
  width: 5px; height: 5px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 1.2s ease infinite;
}
.loading-dots span:nth-child(2) { animation-delay: 0.15s; }
.loading-dots span:nth-child(3) { animation-delay: 0.3s; }

/* Empty states */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
  animation: fadeUp 0.5s ease;
}

.empty-state-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state-text {
  font-family: var(--font-serif);
  font-size: 18px;
  margin-bottom: 24px;
  color: var(--text);
}

/* Hints */
.hint-toggle {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
  margin-top: 8px;
  border-bottom: 1px dashed var(--accent-light);
}

.hint-toggle:hover { color: var(--rust); border-color: var(--rust); }

.hint-text {
  font-size: 13px;
  color: var(--text-muted);
  padding: 10px 14px;
  background: var(--bg);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  margin-top: 6px;
}

/* Lesson card */
.lesson-card {
  cursor: pointer;
  transition: border-color 0.2s;
}

.lesson-card:hover {
  border-color: var(--accent);
}

/* Sources section */
.sources-section {
  margin: 12px 0 16px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
}

.sources-header {
  font-size: 10px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 10px 14px 6px;
}

.source-link {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  text-decoration: none;
  border-top: 1px solid var(--border-light);
  transition: background 0.15s;
}

.source-link:hover {
  background: var(--bg);
}

.source-title {
  font-size: 13px;
  color: var(--accent);
  flex: 1;
  margin-right: 12px;
}

.source-url {
  font-size: 11px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}

/* Lesson content typography */
.chat-bubble.system h2 {
  font-family: var(--font-serif);
  font-size: 18px;
  margin: 16px 0 8px;
  color: var(--text);
}

.chat-bubble.system h3 {
  font-family: var(--font-serif);
  font-size: 16px;
  margin: 14px 0 6px;
  color: var(--text);
}

.chat-bubble.system h4 {
  font-size: 14px;
  font-weight: 600;
  margin: 12px 0 6px;
  color: var(--text);
}

.chat-bubble.system ul,
.chat-bubble.system ol {
  padding-left: 20px;
  margin: 8px 0;
}

.chat-bubble.system li {
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 4px;
}

.chat-bubble.system a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.chat-bubble.system a:hover {
  color: var(--rust);
}

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Toast notifications */
#toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 600px;
  width: calc(100% - 32px);
  pointer-events: none;
}

.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.5;
  pointer-events: auto;
  animation: fadeUp 0.3s ease;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.toast-error {
  background: var(--bg-card);
  border-left: 3px solid var(--error);
  color: var(--text);
}

.toast-success {
  background: var(--bg-card);
  border-left: 3px solid var(--success);
  color: var(--text);
}

.toast-info {
  background: var(--bg-card);
  border-left: 3px solid var(--accent);
  color: var(--text);
}

.toast-exit {
  animation: fadeOut 0.3s ease forwards;
}

@keyframes fadeOut {
  to { opacity: 0; transform: translateY(-8px); }
}

/* Selection */
::selection {
  background: rgba(60, 122, 123, 0.2);
}

/* Module/lesson expansion panels */
.module-edit-panel,
.lesson-edit-panel {
  border-top: 1px solid var(--border);
  margin-top: 10px;
  padding-top: 10px;
}

/* Source citations */
.source-list summary {
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.source-item {
  display: flex;
  flex-direction: column;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}

.source-item:last-child {
  border-bottom: none;
}

.source-item a {
  color: var(--accent);
  text-decoration: none;
  font-size: 12px;
}

.source-item a:hover {
  text-decoration: underline;
}

/* Feed dashboard */
.feed-stats-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.feed-source-row {
  padding: 12px 16px;
}

.feed-article-row {
  padding: 10px 16px;
}

.feed-article-row .card-title {
  color: var(--accent);
}

/* Toggle switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border);
  border-radius: 20px;
  transition: background 0.2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background: var(--bg);
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(16px);
}

/* Tags */
.tag {
  display: inline-block;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--border);
  color: var(--text-muted);
}

