:root {
  --paper: #f7f7f4;
  --surface: #ffffff;
  --ink: #191b1d;
  --muted: #686e73;
  --line: #d9dcde;
  --red: #b73a32;
  --teal: #14766f;
  --blue: #315b9b;
  --yellow: #f3d478;
  --shadow: 0 8px 24px rgba(25, 27, 29, .08);
}

body.dark {
  --paper: #151719;
  --surface: #202326;
  --ink: #f2f1ec;
  --muted: #adb2b5;
  --line: #3a3f42;
  --red: #ee786e;
  --teal: #5bc1b6;
  --blue: #82a8e2;
  --yellow: #6d5927;
  --shadow: 0 8px 24px rgba(0, 0, 0, .25);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 16px/1.65 Inter, sans-serif; }
button, input { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; height: 58px; padding: 0 max(20px, calc((100vw - 1180px) / 2)); background: color-mix(in srgb, var(--paper) 92%, transparent); border-bottom: 1px solid var(--line); backdrop-filter: blur(12px); }
.brand { color: var(--ink); font-weight: 700; text-decoration: none; }
.brand span { color: var(--red); font-weight: 500; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.progress-label { color: var(--muted); font-size: .82rem; }
.icon-button { width: 34px; height: 34px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }

.home-view { max-width: 1180px; margin: 0 auto; padding: 58px 24px 80px; }
.course-heading { display: grid; grid-template-columns: minmax(0, 1fr) 390px; align-items: center; gap: 64px; min-height: 300px; border-bottom: 1px solid var(--line); }
.kicker { margin: 0 0 10px; color: var(--red); font: 500 .78rem IBM Plex Mono, monospace; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(2.6rem, 6vw, 5rem); line-height: 1; letter-spacing: 0; }
.course-dek { max-width: 650px; margin: 22px 0 0; color: var(--muted); font-size: 1.06rem; }
.course-facts { display: flex; flex-wrap: wrap; gap: 18px; margin-top: 24px; color: var(--muted); font-size: .78rem; }
.course-facts strong { margin-right: 4px; color: var(--ink); font: 600 1rem IBM Plex Mono, monospace; }

.overview-diagram { position: relative; height: 210px; }
.object { position: absolute; display: grid; place-items: center; width: 62px; aspect-ratio: 1; border: 2px solid var(--ink); border-radius: 50%; background: var(--surface); font: 600 1.3rem IBM Plex Mono, monospace; }
.object-a { left: 0; top: 70px; }.object-b { left: 160px; top: 70px; }.object-c { right: 0; top: 70px; }
.arrow { position: absolute; top: 69px; width: 94px; color: var(--red); font: 500 .8rem IBM Plex Mono, monospace; text-align: center; border-bottom: 2px solid var(--red); }
.arrow::after { content: ''; position: absolute; right: -1px; bottom: -5px; width: 8px; height: 8px; border-top: 2px solid var(--red); border-right: 2px solid var(--red); transform: rotate(45deg); }
.arrow-ab { left: 64px; }.arrow-bc { left: 224px; }
.arc-label { position: absolute; left: 80px; top: 16px; width: 240px; padding-top: 9px; border-top: 2px dashed var(--teal); color: var(--teal); font: 500 .8rem IBM Plex Mono, monospace; text-align: center; }

.controls-band { display: flex; justify-content: space-between; align-items: end; gap: 24px; padding: 28px 0; }
.search-field { display: grid; gap: 6px; width: min(420px, 100%); color: var(--muted); font-size: .75rem; text-transform: uppercase; }
.search-field input { height: 42px; padding: 0 12px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); outline: none; }
.search-field input:focus { border-color: var(--teal); }
.command-row { display: flex; gap: 8px; }
.command, .back-button, .quiz-topline button { min-height: 40px; padding: 0 15px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.command.primary { color: #fff; border-color: var(--teal); background: var(--teal); }
.course-brief { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; margin-bottom: 28px; padding: 1px; background: var(--line); }
.course-brief > div { padding: 18px 20px; background: var(--surface); }
.course-brief p { margin: 6px 0 0; color: var(--muted); font-size: .86rem; }
.brief-label { color: var(--red); font: 500 .7rem IBM Plex Mono, monospace; text-transform: uppercase; }

.module-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.module-card { min-height: 220px; padding: 22px; border: 0; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface); text-align: left; cursor: pointer; transition: background .15s; }
.module-card:hover { background: color-mix(in srgb, var(--surface) 90%, var(--yellow)); }
.module-number { color: var(--red); font: 500 .72rem IBM Plex Mono, monospace; }
.module-card h2 { margin: 38px 0 10px; font-size: 1.3rem; line-height: 1.25; }
.module-card p { margin: 0; color: var(--muted); font-size: .88rem; }
.module-card-footer { display: flex; justify-content: space-between; margin-top: 22px; color: var(--muted); font-size: .72rem; }

.lesson-view { max-width: 1180px; margin: 0 auto; padding: 42px 24px 90px; }
.lesson-shell { max-width: 780px; margin: 0 auto; }
.back-button { margin-bottom: 34px; }
.lesson-header { padding-bottom: 32px; border-bottom: 1px solid var(--line); }
.lesson-header h1 { font-size: clamp(2.2rem, 5vw, 4.2rem); }
.lesson-header p { color: var(--muted); font-size: 1.05rem; }
.lesson-body h2 { margin: 58px 0 15px; font-size: 1.65rem; line-height: 1.2; }
.lesson-body h3 { margin: 36px 0 12px; }
.lesson-body p { margin: 0 0 18px; }
.lesson-body code, .notation { font-family: IBM Plex Mono, monospace; }
.callout { margin: 26px 0; padding: 18px 20px; border-left: 4px solid var(--teal); background: color-mix(in srgb, var(--surface) 88%, var(--teal)); }
.callout strong { display: block; margin-bottom: 4px; color: var(--teal); }
.diagram { display: grid; place-items: center; min-height: 220px; margin: 28px 0; padding: 16px; overflow-x: auto; border: 1px solid var(--line); background: var(--surface); }
.diagram svg { display: block; width: min(100%, 680px); height: auto; min-width: 500px; color: var(--ink); }
.diagram .node { fill: var(--surface); stroke: var(--ink); stroke-width: 2; }
.diagram .node-accent { fill: color-mix(in srgb, var(--surface) 82%, var(--teal)); stroke: var(--teal); stroke-width: 2; }
.diagram .arrow-line { fill: none; stroke: var(--blue); stroke-width: 2.2; marker-end: url(#arrowhead); }
.diagram .arrow-line.accent { stroke: var(--red); }
.diagram .guide-line { fill: none; stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 6 5; marker-end: url(#arrowhead-muted); }
.diagram text { fill: var(--ink); font: 500 15px IBM Plex Mono, monospace; text-anchor: middle; }
.diagram .arrow-label { fill: var(--blue); font-size: 13px; paint-order: stroke; stroke: var(--surface); stroke-width: 5px; stroke-linejoin: round; }
.diagram .caption { fill: var(--muted); font-size: 12px; }
.objectives { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; margin: 28px 0 0; padding: 1px; background: var(--line); }
.objective { padding: 12px; background: var(--surface); color: var(--muted); font-size: .78rem; }
.objective::before { content: '✓'; margin-right: 7px; color: var(--teal); }
.example { margin: 24px 0; padding: 20px; border: 1px solid var(--line); background: var(--surface); }
.example-label { color: var(--red); font: 500 .72rem IBM Plex Mono, monospace; text-transform: uppercase; }
.exercise-set { margin: 48px 0 34px; padding: 22px 24px; border: 1px solid var(--teal); background: color-mix(in srgb, var(--surface) 92%, var(--teal)); }
.exercise-set h2 { margin: 0 0 14px; font-size: 1.2rem; }
.exercise-intro { margin: -4px 0 18px; color: var(--muted); font-size: .84rem; }
.written-exercise { border: 1px solid var(--line); background: var(--surface); }
.written-exercise + .written-exercise { margin-top: 10px; }
.written-exercise summary { display: grid; grid-template-columns: 90px minmax(0, 1fr); gap: 12px; align-items: start; padding: 15px 16px; cursor: pointer; list-style: none; }
.written-exercise summary::-webkit-details-marker { display: none; }
.written-exercise summary::after { content: '+'; position: absolute; right: 16px; color: var(--teal); font: 500 1rem IBM Plex Mono, monospace; }
.written-exercise[open] summary::after { content: '−'; }
.written-exercise summary { position: relative; padding-right: 42px; }
.written-number { color: var(--red); font: 500 .7rem IBM Plex Mono, monospace; text-transform: uppercase; }
.lyx-workspace { border-top: 1px solid var(--line); }
.lyx-toolbar { display: flex; flex-wrap: wrap; gap: 1px; padding: 7px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--surface) 84%, var(--line)); }
.lyx-toolbar button { min-width: 32px; min-height: 30px; padding: 3px 8px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font: 500 .76rem IBM Plex Mono, monospace; }
.lyx-toolbar button:hover { border-color: var(--teal); }
.lyx-editor { display: block; width: 100%; min-height: 210px; padding: 16px; resize: vertical; border: 0; border-bottom: 1px solid var(--line); outline: 0; color: var(--ink); background: var(--surface); font: 400 .9rem/1.65 IBM Plex Mono, monospace; }
.lyx-editor:focus { box-shadow: inset 3px 0 var(--teal); }
.lyx-pane-label { padding: 7px 14px; border-bottom: 1px solid var(--line); color: var(--muted); background: color-mix(in srgb, var(--surface) 84%, var(--line)); font: 500 .68rem IBM Plex Mono, monospace; text-transform: uppercase; }
.lyx-preview { min-height: 110px; padding: 16px; overflow-x: auto; border-bottom: 1px solid var(--line); background: var(--surface); }
.preview-empty { color: var(--muted); font-size: .84rem; }
.written-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; }
.written-saved { color: var(--muted); font-size: .72rem; }
.ai-written-check { min-height: 38px; padding: 0 14px; border: 0; color: #fff; background: var(--teal); cursor: pointer; }
.ai-written-check:disabled { opacity: .6; cursor: wait; }
.written-feedback { margin: 0 12px 12px; padding: 14px; border-left: 3px solid var(--blue); background: color-mix(in srgb, var(--surface) 90%, var(--blue)); color: var(--muted); }
.written-feedback strong { color: var(--ink); }
.written-feedback-content { white-space: normal; }
.written-feedback-status { margin-bottom: 8px; color: var(--blue); font: 500 .68rem IBM Plex Mono, monospace; text-transform: uppercase; }
.written-feedback-content > :first-child { margin-top: 0; }
.written-feedback-content > :last-child { margin-bottom: 0; }
.lesson-ai-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 38px 0; padding-top: 24px; border-top: 1px solid var(--line); }
.lesson-ai-actions button { min-height: 42px; }
.katex { font-size: 1.04em; }

.review-card { margin: 38px 0; padding: 24px; border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow); }
.review-card h3 { margin: 0 0 18px; font-size: 1.05rem; }
.option-list { display: grid; gap: 8px; }
.option { display: flex; gap: 12px; align-items: flex-start; min-height: 44px; padding: 10px 12px; border: 1px solid var(--line); background: transparent; text-align: left; cursor: pointer; }
.option:hover, .option.selected { border-color: var(--blue); background: color-mix(in srgb, var(--surface) 90%, var(--blue)); }
.option.correct { border-color: var(--teal); background: color-mix(in srgb, var(--surface) 88%, var(--teal)); }
.option.wrong { border-color: var(--red); background: color-mix(in srgb, var(--surface) 90%, var(--red)); }
.marker { flex: 0 0 24px; font: 500 .8rem IBM Plex Mono, monospace; }
.check-button { margin-top: 14px; min-height: 38px; padding: 0 16px; color: #fff; border: 0; background: var(--blue); cursor: pointer; }
.feedback { margin-top: 14px; color: var(--muted); }
.feedback strong { color: var(--ink); }
.lesson-nav { display: flex; justify-content: space-between; gap: 16px; margin-top: 60px; padding-top: 24px; border-top: 1px solid var(--line); }
.lesson-nav button { max-width: 48%; border: 0; color: var(--blue); background: none; cursor: pointer; }
.lesson-toc { position: fixed; top: 110px; width: 170px; display: grid; gap: 8px; }
.lesson-toc a { color: var(--muted); font-size: .76rem; text-decoration: none; }

.quiz-view { min-height: calc(100vh - 58px); padding: 60px 24px; }
.quiz-shell { max-width: 720px; margin: 0 auto; }
.quiz-topline { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; color: var(--muted); }
.quiz-question { font-size: 1.5rem; line-height: 1.3; }
.quiz-next { margin-top: 20px; min-height: 42px; padding: 0 18px; border: 0; color: white; background: var(--teal); cursor: pointer; }
.score { padding: 40px 0; text-align: center; }
.score strong { display: block; color: var(--teal); font-size: 5rem; line-height: 1; }

@media (max-width: 940px) {
  .course-heading { grid-template-columns: 1fr; gap: 20px; }
  .overview-diagram { width: 390px; max-width: 100%; }
  .module-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .lesson-toc { display: none; }
}
@media (max-width: 640px) {
  .home-view { padding: 38px 16px 60px; }
  .course-heading { min-height: auto; padding-bottom: 30px; }
  .overview-diagram { transform: scale(.82); transform-origin: left center; }
  .controls-band { align-items: stretch; flex-direction: column; }
  .command-row { display: grid; grid-template-columns: 1fr; }
  .course-brief { grid-template-columns: 1fr; }
  .module-grid { grid-template-columns: 1fr; }
  .module-card { min-height: 180px; }
  .topbar { padding: 0 14px; }
  .progress-label { display: none; }
  .lesson-view { padding: 28px 16px 70px; }
  .objectives { grid-template-columns: 1fr; }
  .diagram { justify-content: start; }
  .exercise-set { padding: 16px 12px; }
  .written-exercise summary { grid-template-columns: 1fr; gap: 5px; }
  .written-actions { align-items: stretch; flex-direction: column; }
}

@media print {
  .topbar, .controls-band, .back-button, .lesson-toc, .lesson-nav, .check-button, .lyx-toolbar, .lyx-editor, .written-actions, .ai-written-check { display: none !important; }
  body { background: #fff; color: #000; }
  .home-view { display: none !important; }
  .lesson-view.hidden { display: block !important; }
  .lesson-view { padding: 0; }
  .lesson-shell { max-width: none; }
  .review-card { box-shadow: none; break-inside: avoid; }
  .written-exercise { break-inside: avoid; }
  .written-exercise > summary { display: block; }
  .written-exercise:not([open]) > .lyx-workspace { display: block; }
}
