/* ===== Brilliant-style Design System ===== */

:root {
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --secondary: #059669;
  --secondary-light: #d1fae5;
  --accent: #d97706;
  --accent-light: #fef3c7;
  --error: #dc2626;
  --error-light: #fee2e2;
  --correct: #16a34a;
  --correct-light: #dcfce7;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --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), 0 2px 4px rgba(0,0,0,0.06);
  --radius: 12px;
  --radius-sm: 8px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 18px;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
}

/* ===== Layout ===== */

.lecture-container {
  max-width: 820px;
  margin: 0 auto;
  padding: 1.5rem 1.5rem 4rem;
}

.section-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  margin-bottom: 1.5rem;
}

.section-card h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  color: var(--text);
}

.section-card h3 {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
  color: var(--text);
}

.section-card p {
  margin-bottom: 1rem;
}

.section-card p:last-child {
  margin-bottom: 0;
}

/* ===== Navigation / Progress ===== */

.nav-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  padding: 0.6rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.nav-bar .nav-title {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text);
  white-space: nowrap;
}

.nav-bar .nav-links {
  display: flex;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.nav-bar .nav-links a {
  color: var(--primary);
  text-decoration: none;
  padding: 0.25rem 0.6rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}

.nav-bar .nav-links a:hover {
  background: var(--primary-light);
}

.nav-bar .nav-links a.active {
  background: var(--primary);
  color: white;
}

/* ===== Hero / Page Header ===== */

.page-hero {
  text-align: center;
  padding: 3rem 1rem 2rem;
}

.page-hero h1 {
  font-size: 2.2rem;
  font-weight: 800;
  margin-bottom: 0.5rem;
  color: var(--text);
}

.page-hero .subtitle {
  font-size: 1.1rem;
  color: var(--text-muted);
  max-width: 600px;
  margin: 0 auto;
}

/* ===== Convention Box ===== */

.convention-box {
  background: var(--primary-light);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
  font-size: 0.95rem;
}

.convention-box strong {
  color: var(--primary);
}

/* ===== Callout Boxes ===== */

.callout {
  border-radius: var(--radius-sm);
  padding: 1rem 1.25rem;
  margin: 1.25rem 0;
  font-size: 0.95rem;
}

.callout.info {
  background: var(--primary-light);
  border-left: 4px solid var(--primary);
}

.callout.warning {
  background: var(--accent-light);
  border-left: 4px solid var(--accent);
}

.callout.success {
  background: var(--correct-light);
  border-left: 4px solid var(--correct);
}

.callout.danger {
  background: var(--error-light);
  border-left: 4px solid var(--error);
}

/* ===== Rule Box (matrix calculus rules) ===== */

.rule-box {
  background: var(--card-bg);
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin: 1.25rem 0;
}

.rule-box .rule-title {
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 0.5rem;
  font-size: 1rem;
}

.rule-box .rule-dim {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

/* ===== Interactive Widget ===== */

.interactive-widget {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  margin: 1.5rem 0;
  overflow: hidden;
}

.widget-header {
  padding: 0.75rem 1.5rem;
  font-weight: 700;
  font-size: 0.95rem;
  color: white;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.widget-header.blue { background: var(--primary); }
.widget-header.green { background: var(--secondary); }
.widget-header.amber { background: var(--accent); }

.widget-body {
  padding: 1.5rem;
}

.widget-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1rem;
  align-items: flex-end;
}

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

.widget-control label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.widget-control select,
.widget-control input[type="text"],
.widget-control input[type="number"] {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--bg);
  min-width: 120px;
}

.widget-control select:focus,
.widget-control input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.widget-control input[type="range"] {
  width: 180px;
  accent-color: var(--primary);
}

.widget-control .slider-value {
  font-size: 0.85rem;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  color: var(--primary);
  font-weight: 600;
  min-width: 80px;
}

/* ===== Widget Output / Results ===== */

.widget-output {
  margin-top: 1rem;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.75rem;
  margin: 1rem 0;
}

.result-card {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  text-align: center;
}

.result-card .result-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.result-card .result-value {
  font-size: 1.1rem;
  font-weight: 700;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

/* ===== Comparison Table ===== */

.compare-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  margin: 1rem 0;
}

.compare-table th {
  background: var(--bg);
  padding: 0.6rem 0.75rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
}

.compare-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 0.85rem;
}

.compare-table tr:hover {
  background: var(--bg);
}

/* ===== Error Badge ===== */

.error-badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

.error-badge.good { background: var(--correct-light); color: var(--correct); }
.error-badge.ok { background: var(--accent-light); color: var(--accent); }
.error-badge.bad { background: var(--error-light); color: var(--error); }

/* ===== Step-by-Step Reveal ===== */

.step-container {
  position: relative;
}

.step-item {
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.4s ease, transform 0.4s ease;
  pointer-events: none;
  max-height: 0;
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.step-item.revealed {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
  max-height: 500px;
  margin-bottom: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--primary);
}

.step-item .step-number {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.25rem;
}

.step-controls {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

/* ===== Quiz Block ===== */

.quiz-block {
  background: var(--card-bg);
  border: 2px solid var(--primary-light);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin: 1.5rem 0;
}

.quiz-block .quiz-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--primary);
  margin-bottom: 0.75rem;
}

.quiz-block .quiz-question {
  font-weight: 600;
  margin-bottom: 1rem;
  font-size: 1rem;
}

.quiz-options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.quiz-option {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
  font-size: 0.95rem;
}

.quiz-option:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.quiz-option.selected {
  border-color: var(--primary);
  background: var(--primary-light);
}

.quiz-option.correct {
  border-color: var(--correct);
  background: var(--correct-light);
}

.quiz-option.incorrect {
  border-color: var(--error);
  background: var(--error-light);
}

.quiz-option input[type="radio"] {
  accent-color: var(--primary);
}

.quiz-fill-in {
  margin-bottom: 1rem;
}

.quiz-fill-in input {
  padding: 0.5rem 0.75rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  width: 200px;
}

.quiz-fill-in input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.quiz-feedback {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  display: none;
  margin-top: 0.5rem;
}

.quiz-feedback.show { display: block; }
.quiz-feedback.correct { background: var(--correct-light); color: #166534; }
.quiz-feedback.incorrect { background: var(--error-light); color: #991b1b; }

.quiz-hint {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--accent-light);
  color: #92400e;
  font-size: 0.9rem;
  display: none;
  margin-top: 0.5rem;
}

.quiz-hint.show { display: block; }

/* ===== Buttons ===== */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: #1d4ed8; }
.btn-primary:disabled { background: #93c5fd; cursor: not-allowed; }

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

.btn-success {
  background: var(--secondary);
  color: white;
}
.btn-success:hover { background: #047857; }

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

/* ===== Toggle Switch ===== */

.toggle-group {
  display: inline-flex;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.toggle-group button {
  padding: 0.4rem 1rem;
  border: none;
  background: var(--card-bg);
  font-size: 0.85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
}

.toggle-group button.active {
  background: var(--primary);
  color: white;
}

/* ===== Code ===== */

.code-inline {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 0.85em;
  background: var(--bg);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  color: var(--primary);
}

code, pre {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

pre.code-block {
  background: #1e293b;
  color: #e2e8f0;
  padding: 1rem 1.25rem;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.6;
  margin: 1rem 0;
}

/* ===== Tape / Wengert List ===== */

.tape-table {
  width: 100%;
  border-collapse: collapse;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 0.85rem;
  margin: 1rem 0;
}

.tape-table th {
  background: var(--bg);
  padding: 0.5rem 0.75rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
}

.tape-table td {
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.tape-table tr.active-step {
  background: var(--primary-light);
}

.tape-table tr.computed {
  background: var(--correct-light);
}

.tape-table .primal-val {
  color: var(--primary);
  font-weight: 600;
}

.tape-table .tangent-val {
  color: var(--secondary);
  font-weight: 600;
}

.tape-table .adjoint-val {
  color: var(--accent);
  font-weight: 600;
}

/* ===== Computation Graph (SVG) ===== */

.graph-container {
  display: flex;
  justify-content: center;
  margin: 1rem 0;
  overflow-x: auto;
}

.graph-container svg {
  max-width: 100%;
}

.graph-node {
  transition: all 0.3s ease;
}

.graph-node circle {
  fill: white;
  stroke: var(--border);
  stroke-width: 2;
  transition: all 0.3s ease;
}

.graph-node.computed circle {
  fill: var(--primary-light);
  stroke: var(--primary);
}

.graph-node.adjoint-active circle {
  fill: var(--accent-light);
  stroke: var(--accent);
}

.graph-node text {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 12px;
  text-anchor: middle;
  dominant-baseline: central;
}

.graph-edge {
  stroke: var(--border);
  stroke-width: 2;
  fill: none;
  transition: all 0.3s ease;
  marker-end: url(#arrowhead);
}

.graph-edge.active {
  stroke: var(--primary);
  stroke-width: 3;
}

.graph-edge.adjoint-active {
  stroke: var(--accent);
  stroke-width: 3;
}

/* ===== Math Display ===== */

.katex-display {
  margin: 1.25rem 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.math-block {
  text-align: center;
  margin: 1.25rem 0;
  padding: 0.75rem;
  background: var(--bg);
  border-radius: var(--radius-sm);
}

/* ===== Responsive ===== */

@media (max-width: 640px) {
  body { font-size: 16px; }
  .lecture-container { padding: 1rem; }
  .section-card { padding: 1.25rem; }
  .widget-body { padding: 1rem; }
  .widget-controls { flex-direction: column; }
  .widget-control input[type="range"] { width: 100%; }
  .page-hero h1 { font-size: 1.6rem; }
  .result-grid { grid-template-columns: 1fr; }
}

/* ===== Plotly Override ===== */

.js-plotly-plot .plotly .modebar { display: none !important; }

/* ===== Transition helpers ===== */

.fade-in {
  animation: fadeIn 0.4s ease forwards;
}

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

/* ===== Summary / Cheat Sheet Table ===== */

.cheatsheet-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  margin: 1rem 0;
}

.cheatsheet-table th {
  background: var(--primary);
  color: white;
  padding: 0.6rem 0.75rem;
  text-align: left;
  font-weight: 600;
}

.cheatsheet-table td {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.cheatsheet-table tr:nth-child(even) {
  background: var(--bg);
}
