@import url('https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,600;0,700;1,400&family=JetBrains+Mono:wght@400;600&display=swap');

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

:root {
  --bg: #f4f0e8;
  --bg2: #fdfaf4;
  --bg3: #ece7da;
  --accent: #2a4e8c;
  --accent2: #b5401a;
  --text: #1c1814;
  --muted: #7a6f62;
  --border: #c9c0b0;
  --rule: #d8d0c4;
  --radius: 3px;
  --font: 'Lora', Georgia, 'Times New Roman', serif;
  --mono: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
  --shadow: 0 2px 8px rgba(80,60,30,0.10), 0 1px 2px rgba(80,60,30,0.08);
  --shadow-lg: 0 6px 24px rgba(80,60,30,0.13), 0 2px 6px rgba(80,60,30,0.09);
}

body {
  background: var(--bg);
  background-image:
    repeating-linear-gradient(
      transparent,
      transparent 31px,
      var(--rule) 31px,
      var(--rule) 32px
    );
  color: var(--text);
  font-family: var(--font);
  line-height: 1.75;
  font-size: 1rem;
}

/* ── Nav ── */
.topnav {
  display: flex; align-items: center; gap: 2rem;
  padding: 0.7rem 2.5rem;
  background: var(--bg2);
  border-bottom: 2px solid var(--text);
  position: sticky; top: 0; z-index: 100;
  flex-wrap: wrap;
  box-shadow: var(--shadow);
}
.topnav .logo {
  font-weight: 700; font-size: 1.05rem; color: var(--text);
  white-space: nowrap; letter-spacing: 0.01em;
  font-style: italic;
}
.topnav .back-link {
  font-size: 0.78rem; color: var(--muted); text-decoration: none;
  padding: 0.25rem 0.55rem; border: 1px solid var(--border);
  border-radius: var(--radius); white-space: nowrap;
  transition: all 0.15s; background: var(--bg3);
}
.topnav .back-link:hover {
  color: var(--text); border-color: var(--accent);
  background: var(--bg2);
}
.topnav ul { display: flex; gap: 0.15rem; list-style: none; flex-wrap: wrap; }
.topnav a {
  text-decoration: none; color: var(--muted); font-size: 0.8rem;
  padding: 0.25rem 0.6rem; border-radius: var(--radius);
  transition: all 0.15s; font-family: var(--font); letter-spacing: 0.01em;
}
.topnav a:hover, .topnav a.active {
  color: var(--text);
  background: var(--bg3);
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ── Hero ── */
.hero {
  display: flex; align-items: center; justify-content: space-between;
  gap: 2rem; padding: 4.5rem 3rem;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: relative;
}
.hero::before {
  content: '';
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px; background: var(--accent2);
}
.hero-text h1 {
  font-size: 2.8rem; line-height: 1.15; font-weight: 700;
  font-style: italic; color: var(--text);
}
.hero-text h1 span { color: var(--accent); font-style: normal; }
.hero-text p {
  margin: 1.25rem 0 2rem; color: var(--muted);
  max-width: 420px; font-size: 1.05rem;
}
.btn-primary {
  display: inline-block; padding: 0.65rem 1.6rem;
  background: var(--accent); color: #fff; border-radius: var(--radius);
  text-decoration: none; font-weight: 600; font-size: 0.95rem;
  font-family: var(--font); letter-spacing: 0.02em;
  transition: background 0.15s;
  box-shadow: var(--shadow);
}
.btn-primary:hover { background: #1e3a6a; }
.hero-canvas-wrap canvas { border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow); }

/* ── Modules grid ── */
.modules { padding: 3rem 3rem 4rem; }
.modules h2 {
  font-size: 1.4rem; margin-bottom: 1.75rem; font-style: italic;
  color: var(--text); border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
}
.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1.25rem;
}
.module-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.5rem;
  text-decoration: none; color: var(--text);
  transition: box-shadow 0.15s, transform 0.15s;
  display: flex; flex-direction: column; gap: 0.5rem;
  box-shadow: var(--shadow);
  position: relative;
}
.module-card::before {
  content: '';
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px; background: var(--accent); border-radius: var(--radius) 0 0 var(--radius);
  opacity: 0; transition: opacity 0.15s;
}
.module-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }
.module-card:hover::before { opacity: 1; }
.mod-num { font-size: 1.8rem; font-weight: 700; color: var(--accent); opacity: 0.35; line-height: 1; font-style: italic; }
.module-card h3 { font-size: 1rem; font-weight: 600; }
.module-card p { font-size: 0.88rem; color: var(--muted); flex: 1; line-height: 1.55; }
.tag {
  display: inline-block; font-size: 0.68rem; padding: 0.18rem 0.45rem;
  background: var(--bg3); border-radius: var(--radius);
  color: var(--accent2); align-self: flex-start;
  font-weight: 600; letter-spacing: 0.05em;
  font-family: var(--mono); border: 1px solid var(--border);
}
.card-meta {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.5rem; margin-top: auto;
}
.card-meta .time {
  font-size: 0.7rem; color: var(--muted);
  font-family: var(--mono); letter-spacing: 0.03em;
}

/* ── Lesson page layout ── */
.lesson-wrap { display: flex; min-height: calc(100vh - 52px); }
.sidebar {
  width: 220px; flex-shrink: 0;
  background: var(--bg2); border-right: 2px solid var(--border);
  padding: 1.5rem 1rem; position: sticky; top: 52px;
  height: calc(100vh - 52px); overflow-y: auto;
}
.sidebar h4 {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--muted); margin-bottom: 0.75rem;
  font-family: var(--mono); font-weight: 600;
}
.sidebar a {
  display: block; text-decoration: none; color: var(--muted);
  font-size: 0.82rem; padding: 0.35rem 0.5rem; border-radius: var(--radius);
  transition: all 0.12s;
}
.sidebar a:hover, .sidebar a.active, .sidebar a.scroll-active {
  color: var(--text); background: var(--bg3);
  text-decoration: underline; text-underline-offset: 2px;
}
.sidebar a.scroll-active {
  border-left: 2px solid var(--accent);
  padding-left: calc(0.5rem - 2px);
  font-weight: 600;
}

/* Mobile sidebar toggle */
.sidebar-toggle {
  display: none;
  width: 100%;
  padding: 0.6rem 1rem;
  background: var(--bg2);
  border: none;
  border-bottom: 1px solid var(--border);
  font-family: var(--font);
  font-size: 0.85rem;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-style: italic;
}
.sidebar-toggle:hover { background: var(--bg3); }

@media (max-width: 800px) {
  .lesson-wrap { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; max-height: 60vh;
    position: relative; top: 0;
    border-right: none; border-bottom: 2px solid var(--border);
    padding: 1rem;
  }
  .sidebar.collapsed { display: none; }
  .sidebar-toggle { display: block; }
  .lesson-content { padding: 1.5rem 1.25rem; }
  .lesson-content h1 { font-size: 1.6rem; }
  .topnav { padding: 0.7rem 1rem; gap: 0.75rem; }
  .topnav ul { gap: 0; }
  .hero { padding: 2.5rem 1.25rem; }
  .hero-text h1 { font-size: 2rem; }
  .modules { padding: 2rem 1.25rem 3rem; }
  .demo-row { flex-direction: column; }
  .demo-row canvas { max-width: 100%; height: auto; }
}

/* Highlighted "continue" module card */
.module-card.module-last {
  border-color: var(--accent);
  box-shadow: var(--shadow-lg);
}
.module-card.module-last::before { opacity: 1; }
.module-card.module-last .tag::before {
  content: 'last visited · ';
  color: var(--accent);
}

.lesson-content { flex: 1; padding: 2.5rem 3rem; max-width: 860px; }
.lesson-content h1 {
  font-size: 2rem; margin-bottom: 0.5rem; font-style: italic; font-weight: 700;
  border-bottom: 2px solid var(--text); padding-bottom: 0.4rem;
}
.lesson-content .subtitle { color: var(--muted); margin-bottom: 2rem; font-style: italic; }
.lesson-content h2 {
  font-size: 1.2rem; margin: 2.2rem 0 0.75rem; color: var(--accent);
  font-style: italic; font-weight: 600;
  border-left: 3px solid var(--accent); padding-left: 0.65rem;
}
.lesson-content p { margin-bottom: 1rem; color: #2e2820; }
.lesson-content ul { padding-left: 1.5rem; margin-bottom: 1rem; color: #2e2820; }
.lesson-content ul li { margin-bottom: 0.4rem; }

/* ── Interactive demo boxes ── */
.demo-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.5rem; margin: 1.5rem 0;
  box-shadow: var(--shadow);
  position: relative;
}
.demo-box::after {
  content: '';
  position: absolute; bottom: -4px; right: 6px; left: 6px; height: 4px;
  background: rgba(80,60,30,0.07); border-radius: 0 0 3px 3px;
  filter: blur(2px);
}
.demo-box h3 {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--accent2); margin-bottom: 1rem;
  font-family: var(--mono); font-weight: 600;
}
.demo-row { display: flex; gap: 1.25rem; align-items: flex-start; flex-wrap: wrap; }
.demo-row canvas { border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow); }
.controls { display: flex; flex-direction: column; gap: 0.75rem; min-width: 160px; }
.control-group label { display: block; font-size: 0.78rem; color: var(--muted); margin-bottom: 0.25rem; font-family: var(--mono); }
.control-group input[type=range] { width: 100%; accent-color: var(--accent); }
.control-group select, .control-group button {
  width: 100%; padding: 0.4rem 0.6rem; background: var(--bg3);
  border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius); font-size: 0.82rem; cursor: pointer;
  font-family: var(--font);
}
.control-group button:hover { border-color: var(--accent); background: var(--bg2); }
.val-display { font-size: 0.75rem; color: var(--accent); font-weight: 600; font-family: var(--mono); }

/* ── Code blocks ── */
pre {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem; overflow-x: auto;
  font-size: 0.8rem; line-height: 1.75; margin: 1rem 0;
  color: #3a2e20;
  box-shadow: inset 0 1px 3px rgba(80,60,30,0.07);
  border-left: 3px solid var(--muted);
}
code { font-family: var(--mono); color: #5a3e28; background: var(--bg3); padding: 0.1em 0.3em; border-radius: 2px; font-size: 0.88em; }
pre code { background: none; padding: 0; color: inherit; }

/* ── Quiz ── */
.quiz-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.5rem; margin: 2rem 0;
  box-shadow: var(--shadow);
}
.quiz-box h3 {
  color: var(--accent2); margin-bottom: 1rem;
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.1em;
  font-family: var(--mono); font-weight: 600;
}
.quiz-q { margin-bottom: 1.5rem; }
.quiz-q p { font-weight: 600; margin-bottom: 0.75rem; }
.quiz-q label {
  display: block; padding: 0.5rem 0.75rem; border-radius: var(--radius);
  cursor: pointer; transition: background 0.12s; margin-bottom: 0.35rem;
  border: 1px solid var(--border); font-size: 0.88rem;
}
.quiz-q label:hover { background: var(--bg3); }
.quiz-q label.correct { background: #e8f5ec; border-color: #2e8b57; color: #1a5c35; }
.quiz-q label.wrong { background: #faecea; border-color: #c0392b; color: #7a1e10; }
.quiz-q input[type=radio] { margin-right: 0.5rem; accent-color: var(--accent); }

/* ── Navigation buttons ── */
.lesson-nav {
  display: flex; justify-content: space-between; margin-top: 3rem;
  padding-top: 1.5rem; border-top: 1px solid var(--border);
}
.lesson-nav a {
  text-decoration: none; color: var(--accent); font-size: 0.9rem; font-weight: 600;
  padding: 0.5rem 1rem; border-radius: var(--radius);
  border: 1px solid var(--border); transition: all 0.12s;
  font-family: var(--font);
}
.lesson-nav a:hover { border-color: var(--accent); background: var(--bg3); }

/* ── Info callouts ── */
.callout {
  border-left: 3px solid var(--accent); background: var(--bg3);
  padding: 0.9rem 1.1rem; border-radius: 0 var(--radius) var(--radius) 0;
  margin: 1rem 0; font-size: 0.88rem; color: #2e2820;
  box-shadow: var(--shadow);
}
.callout.tip { border-color: #2e8b57; background: #eef7f1; }
.callout.warning { border-color: #b8860b; background: #faf5e4; }

/* ── Formula ── */
.formula {
  text-align: center; font-family: var(--mono);
  background: var(--bg3); padding: 0.85rem 1rem;
  border-radius: var(--radius) var(--radius) 0 0;
  margin: 1.25rem 0 0; font-size: 1rem; color: var(--accent);
  border: 1px solid var(--border); border-bottom: none;
  box-shadow: var(--shadow);
}
.formula + .formula-breakdown, .formula-breakdown {
  background: var(--bg2); border: 1px solid var(--border);
  border-top: 1px dashed var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  padding: 0.75rem 1rem; margin: 0 0 1.5rem;
  box-shadow: var(--shadow);
}
.formula-breakdown .fb-title {
  font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--muted); margin-bottom: 0.5rem; font-family: var(--mono); font-weight: 600;
}
.formula-breakdown table { border-collapse: collapse; width: 100%; }
.formula-breakdown td {
  padding: 0.25rem 0.4rem; vertical-align: top; border: none;
  font-size: 0.8rem; line-height: 1.55;
}
.formula-breakdown td:first-child {
  font-family: var(--mono); color: var(--accent);
  white-space: nowrap; padding-right: 1rem; width: 1%;
}
.formula-breakdown td:last-child { color: var(--muted); }
.formula-standalone {
  background: var(--bg3); border-radius: var(--radius) var(--radius) 0 0;
  padding: 0.75rem 1rem; margin: 1rem 0 0;
  font-family: var(--mono); font-size: 1rem;
  color: var(--accent); text-align: center;
  border: 1px solid var(--border);
}

/* ── Footer ── */
footer {
  text-align: center; padding: 2rem; color: var(--muted);
  font-size: 0.82rem; border-top: 1px solid var(--border);
  font-style: italic;
}
