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

:root {
  --bg: #f8f9fc;
  --surface: #ffffff;
  --surface-alt: #f0f2f7;
  --border: #e2e6ef;
  --text: #1a1d26;
  --text-dim: #6b7280;
  --text-light: #9ca3af;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-light: #dbeafe;
  --primary-bg: #eff6ff;
  --success: #10b981;
  --success-light: #d1fae5;
  --error: #ef4444;
  --error-light: #fee2e2;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --accent: #8b5cf6;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-mono: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
  --topbar-h: 56px;
}

html { font-size: 16px; scroll-behavior: smooth; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
  padding-top: var(--topbar-h);
  -webkit-font-smoothing: antialiased;
}

/* ===== Top Bar ===== */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
  backdrop-filter: blur(10px);
}
.topbar-left { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.logo { font-size: 1.5rem; }
.brand { font-size: 1.1rem; font-weight: 700; color: var(--text); }
.topbar-right { display: flex; gap: 8px; }
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--surface-alt); padding: 4px 10px;
  border-radius: 20px; font-size: 0.8rem; font-weight: 600;
  color: var(--text-dim);
}
.eli5-btn {
  cursor: pointer; border: 2px solid transparent;
  transition: all 0.2s; user-select: none;
}
.eli5-btn.active {
  background: #fef3c7; color: #92400e;
  border-color: #f59e0b;
}
.eli5-btn:hover { opacity: 0.8; }

/* ELI5 mode styles */
body.eli5-mode .eli5-box {
  background: linear-gradient(135deg, #fef9c3, #fef3c7);
  border-radius: var(--radius-sm); padding: 16px; margin-bottom: 16px;
  border-left: 4px solid #f59e0b; font-size: 1rem; line-height: 1.8;
}
body.eli5-mode .eli5-box .eli5-label {
  font-weight: 700; color: #92400e; margin-bottom: 6px;
  display: flex; align-items: center; gap: 6px;
}
body:not(.eli5-mode) .eli5-box { display: none; }

/* ===== Main Content ===== */
#app { max-width: 900px; margin: 0 auto; padding: 20px 16px 100px; }

/* ===== Welcome Page ===== */
.welcome-hero {
  text-align: center; padding: 40px 20px;
  background: linear-gradient(135deg, var(--primary-bg), #f0e7ff);
  border-radius: var(--radius); margin-bottom: 24px;
}
.welcome-hero h1 { font-size: 2rem; margin-bottom: 8px; }
.welcome-hero .tagline { color: var(--text-dim); font-size: 1.1rem; margin-bottom: 24px; }
.start-btn {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--primary); color: #fff;
  padding: 14px 32px; border-radius: 50px;
  font-size: 1.1rem; font-weight: 600;
  border: none; cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
}
.start-btn:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: var(--shadow-lg); }

.welcome-section { background: var(--surface); border-radius: var(--radius); padding: 24px; margin-bottom: 16px; box-shadow: var(--shadow); }
.welcome-section h2 { font-size: 1.3rem; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.welcome-section p, .welcome-section li { color: var(--text-dim); line-height: 1.7; }
.welcome-section ul { list-style: none; padding: 0; }
.welcome-section li { padding: 6px 0; padding-left: 24px; position: relative; }
.welcome-section li::before { content: ""; position: absolute; left: 0; top: 14px; width: 8px; height: 8px; background: var(--primary); border-radius: 50%; }

.example-box {
  background: #1e1e2e; border-radius: var(--radius-sm); padding: 16px; margin: 12px 0;
  font-family: var(--font-mono); font-size: 0.9rem; color: #cdd6f4;
  overflow-x: auto;
}
.example-box .comment { color: #6c7086; }
.example-box .output { color: var(--success); border-top: 1px solid #313244; margin-top: 8px; padding-top: 8px; }

/* ===== Dashboard ===== */
.dashboard-header { margin-bottom: 24px; }
.dashboard-header h1 { font-size: 1.5rem; margin-bottom: 4px; }
.dashboard-header p { color: var(--text-dim); }

.topic-grid { display: grid; gap: 12px; }
.topic-card {
  background: var(--surface); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow);
  cursor: pointer; transition: all 0.2s;
  border: 2px solid transparent;
  display: flex; gap: 16px; align-items: center;
}
.topic-card:hover { border-color: var(--primary); transform: translateY(-1px); box-shadow: var(--shadow-lg); }
.topic-card .icon { font-size: 2rem; min-width: 48px; text-align: center; }
.topic-card .info { flex: 1; min-width: 0; }
.topic-card .info h3 { font-size: 1rem; margin-bottom: 2px; }
.topic-card .info p { font-size: 0.85rem; color: var(--text-dim); margin-bottom: 8px; }
.progress-bar { height: 6px; background: var(--surface-alt); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--success); border-radius: 3px; transition: width 0.5s ease; }
.progress-text { font-size: 0.75rem; color: var(--text-light); margin-top: 4px; }

/* ===== Topic / Lesson Page ===== */
.back-btn {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--primary); font-weight: 600; font-size: 0.9rem;
  cursor: pointer; padding: 8px 0; margin-bottom: 12px;
  background: none; border: none;
}
.back-btn:hover { text-decoration: underline; }

.topic-header {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 20px;
}
.topic-header .icon { font-size: 2.5rem; }
.topic-header h1 { font-size: 1.5rem; }
.topic-header p { color: var(--text-dim); font-size: 0.9rem; }

.tab-bar {
  display: flex; gap: 0; margin-bottom: 20px;
  border-bottom: 2px solid var(--border);
}
.tab {
  padding: 10px 20px; font-weight: 600; font-size: 0.9rem;
  color: var(--text-dim); cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px; transition: all 0.2s;
  background: none; border-top: none; border-left: none; border-right: none;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.lesson-content { background: var(--surface); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); }
.lesson-section { margin-bottom: 32px; }
.lesson-section:last-child { margin-bottom: 0; }
.lesson-section h3 { font-size: 1.15rem; margin-bottom: 12px; color: var(--text); }
.lesson-section p { margin-bottom: 10px; color: var(--text); line-height: 1.7; }
.lesson-section code { background: var(--surface-alt); padding: 2px 6px; border-radius: 4px; font-family: var(--font-mono); font-size: 0.9em; }
.lesson-section ul, .lesson-section ol { margin: 8px 0; padding-left: 24px; }
.lesson-section li { margin-bottom: 6px; line-height: 1.6; }
.lesson-section strong { color: var(--text); }

.lesson-example {
  background: #1e1e2e; border-radius: var(--radius-sm); margin: 16px 0;
  overflow: hidden;
}
.lesson-example-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 16px; background: #181825; font-size: 0.8rem; color: #a6adc8;
}
.lesson-example pre {
  padding: 16px; font-family: var(--font-mono); font-size: 0.88rem;
  color: #cdd6f4; overflow-x: auto; margin: 0;
}
.lesson-example .output-line {
  color: var(--success); display: block;
  border-top: 1px solid #313244; padding-top: 8px; margin-top: 8px;
}
.lesson-example-explain {
  padding: 12px 16px; background: var(--primary-bg);
  font-size: 0.85rem; color: var(--text-dim); line-height: 1.6;
  border-top: 1px solid var(--border);
}
.try-btn {
  background: var(--primary); color: #fff; border: none;
  padding: 4px 12px; border-radius: 4px; cursor: pointer;
  font-size: 0.8rem; font-weight: 600;
}
.try-btn:hover { background: var(--primary-hover); }

.tips-box, .mistakes-box {
  background: var(--surface); border-radius: var(--radius);
  padding: 20px; margin-top: 16px; box-shadow: var(--shadow);
}
.tips-box h3, .mistakes-box h3 { margin-bottom: 12px; }
.tip-item { padding: 8px 0 8px 20px; position: relative; color: var(--text-dim); }
.tip-item::before { content: "💡"; position: absolute; left: 0; }
.mistake-item { padding: 10px; background: var(--error-light); border-radius: var(--radius-sm); margin-bottom: 8px; }
.mistake-item .wrong { color: var(--error); font-family: var(--font-mono); font-size: 0.85rem; }
.mistake-item .right { color: var(--success); font-family: var(--font-mono); font-size: 0.85rem; }
.mistake-fix { color: var(--text-dim); font-size: 0.85rem; margin-top: 4px; }

/* ===== Exercise List ===== */
.exercise-list { display: grid; gap: 8px; }
.exercise-item {
  background: var(--surface); border-radius: var(--radius-sm);
  padding: 14px 16px; display: flex; align-items: center; gap: 12px;
  cursor: pointer; transition: all 0.15s; border: 1px solid var(--border);
}
.exercise-item:hover { border-color: var(--primary); background: var(--primary-bg); }
.exercise-item.completed { border-left: 3px solid var(--success); }
.exercise-num {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem; font-weight: 700;
  background: var(--surface-alt); color: var(--text-dim);
  flex-shrink: 0;
}
.exercise-item.completed .exercise-num { background: var(--success-light); color: var(--success); }
.exercise-item .info { flex: 1; min-width: 0; }
.exercise-item .info h4 { font-size: 0.9rem; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.exercise-item .diff-dots { display: flex; gap: 3px; }
.diff-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border); }
.diff-dot.active { background: var(--warning); }
.exercise-item .check-icon { font-size: 1.2rem; color: var(--success); }

/* ===== Exercise Page ===== */
.exercise-header {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 16px; flex-wrap: wrap; gap: 8px;
}
.exercise-header h2 { font-size: 1.2rem; }
.exercise-meta { display: flex; gap: 8px; align-items: center; }
.diff-badge {
  padding: 3px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 600;
}
.diff-1 { background: var(--success-light); color: #047857; }
.diff-2 { background: #dbeafe; color: #1d4ed8; }
.diff-3 { background: var(--warning-light); color: #92400e; }
.diff-4 { background: #fce7f3; color: #9d174d; }
.diff-5 { background: var(--error-light); color: #991b1b; }

.exercise-description {
  background: var(--surface); border-radius: var(--radius);
  padding: 20px; margin-bottom: 16px; box-shadow: var(--shadow);
  line-height: 1.7;
}
.exercise-description code { background: var(--surface-alt); padding: 2px 6px; border-radius: 4px; font-family: var(--font-mono); font-size: 0.9em; }
.exercise-concepts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.concept-tag { background: var(--primary-light); color: var(--primary); padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; }

.editor-wrapper {
  background: #1e1e2e; border-radius: var(--radius);
  overflow: hidden; margin-bottom: 12px; box-shadow: var(--shadow);
}
.editor-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px; background: #181825;
}
.editor-toolbar span { font-size: 0.8rem; color: #a6adc8; font-family: var(--font-mono); }
.run-btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--success); color: #fff;
  padding: 8px 20px; border-radius: 8px;
  font-size: 0.9rem; font-weight: 600;
  border: none; cursor: pointer; transition: all 0.2s;
}
.run-btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
.run-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.output-panel {
  background: var(--surface); border-radius: var(--radius);
  overflow: hidden; margin-bottom: 12px; box-shadow: var(--shadow);
  min-height: 60px;
}
.output-header {
  padding: 8px 16px; background: var(--surface-alt);
  font-size: 0.8rem; font-weight: 600; color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}
.output-body { padding: 12px 16px; font-family: var(--font-mono); font-size: 0.85rem; white-space: pre-wrap; }
.output-body.pass { background: var(--success-light); color: #047857; }
.output-body.fail { background: var(--error-light); color: #991b1b; }
.output-body.idle { color: var(--text-light); font-style: italic; font-family: var(--font); }

.action-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.action-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 8px;
  font-size: 0.85rem; font-weight: 500;
  border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; color: var(--text-dim); transition: all 0.15s;
}
.action-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-bg); }

.hint-box {
  background: var(--warning-light); border-radius: var(--radius-sm);
  padding: 14px 16px; margin-bottom: 12px;
  font-size: 0.9rem; color: #92400e; line-height: 1.6;
}
.solution-box {
  background: var(--surface); border-radius: var(--radius);
  overflow: hidden; margin-bottom: 12px; box-shadow: var(--shadow);
}
.solution-box .editor-toolbar { background: #047857; }
.solution-box .editor-toolbar span { color: #d1fae5; }
.solution-box pre {
  padding: 16px; background: #1e1e2e;
  font-family: var(--font-mono); font-size: 0.85rem; color: #cdd6f4;
  overflow-x: auto; margin: 0;
}

.exercise-nav {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 0; border-top: 1px solid var(--border); margin-top: 16px;
}
.nav-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 20px; border-radius: 8px;
  font-weight: 600; font-size: 0.9rem;
  border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; color: var(--text); transition: all 0.15s;
}
.nav-btn:hover { border-color: var(--primary); color: var(--primary); }
.nav-btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.nav-btn.primary:hover { background: var(--primary-hover); }
.nav-btn:disabled { opacity: 0.3; cursor: not-allowed; }

/* ===== Loading Overlay ===== */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.overlay.hidden { display: none; }
.loader { text-align: center; background: var(--surface); padding: 40px; border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.loader p { margin-top: 12px; font-weight: 600; }
.loader .subtle { font-weight: 400; color: var(--text-dim); font-size: 0.85rem; }
.spinner {
  width: 40px; height: 40px; margin: 0 auto;
  border: 4px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== CodeMirror Overrides ===== */
.CodeMirror {
  height: auto; min-height: 120px; max-height: 400px;
  font-family: var(--font-mono) !important; font-size: 0.9rem !important;
  line-height: 1.5 !important; padding: 8px 0;
}
.CodeMirror-scroll { min-height: 120px; }

/* ===== Responsive ===== */
@media (max-width: 600px) {
  :root { --topbar-h: 48px; }
  #app { padding: 12px 10px 80px; }
  .welcome-hero h1 { font-size: 1.5rem; }
  .welcome-hero { padding: 24px 16px; }
  .topic-card { padding: 14px; }
  .tab { padding: 8px 12px; font-size: 0.8rem; }
  .exercise-header { flex-direction: column; }
  .exercise-nav { flex-direction: column; gap: 8px; }
  .nav-btn { width: 100%; justify-content: center; }
  .lesson-content { padding: 16px; }
  .CodeMirror { font-size: 0.82rem !important; }
}

/* ===== Celebration Toast ===== */
.celebration-toast {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.4);
  animation: fadeIn 0.3s ease;
  backdrop-filter: blur(4px);
}
.celebration-content {
  background: var(--surface); border-radius: 20px;
  padding: 32px 40px; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  max-width: 420px; width: 90%;
  animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.celebration-emoji { font-size: 3rem; margin-bottom: 8px; }
.celebration-msg { font-size: 1.3rem; font-weight: 700; margin-bottom: 8px; color: var(--text); }
.celebration-milestone {
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  padding: 8px 16px; border-radius: 8px; margin-bottom: 12px;
  font-weight: 600; color: #92400e; font-size: 0.9rem;
}
.celebration-stats { font-size: 0.85rem; color: var(--text-dim); margin-bottom: 20px; }
.celebration-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.celebration-actions .start-btn { font-size: 0.95rem; padding: 10px 24px; }
.celebration-actions .action-btn { font-size: 0.85rem; }

/* ===== Shake animation (wrong answer) ===== */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}
.shake { animation: shake 0.4s ease; }

/* ===== Animations ===== */
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes popIn { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }
.fade-in { animation: fadeIn 0.3s ease; }
