/* CS223 interactive exams (docs/academics/math/cs223/exams/*).
   Generated pages: scripts/build_cs223_course.py. Behaviour: /javascripts/cs223-exam.js.
   Light tokens on :root, dark overrides on html[data-theme="dark"]; <html> carries
   data-sg-styled so standalone-theme.js skips its forced dark fallback. */

:root {
  --bg: #f6f4fb;
  --surface: #ffffff;
  --surface-2: #f0eafa;
  --line: rgba(22, 17, 31, 0.12);
  --line-strong: rgba(22, 17, 31, 0.22);
  --text: #16111f;
  --muted: #5d5670;
  --faint: #8b84a0;
  --brand: #9b1fd0;
  --brand-soft: rgba(155, 31, 208, 0.08);
  --brand-line: rgba(155, 31, 208, 0.32);
  --ok: #0f8a5f;
  --ok-soft: rgba(15, 138, 95, 0.1);
  --bad: #c8243f;
  --bad-soft: rgba(200, 36, 63, 0.09);
  --warn: #a86a00;
  --warn-soft: rgba(168, 106, 0, 0.1);
  --font-ui: "Inter", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;
  --font-display: "Rajdhani", "Inter", sans-serif;
  --pad: clamp(12px, 3.2vw, 40px);
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #05020a;
  --surface: #0c0617;
  --surface-2: #120a22;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #f4f1fa;
  --muted: #a8a3b6;
  --faint: #6f6a7e;
  --brand: #d978ff;
  --brand-soft: rgba(184, 41, 234, 0.1);
  --brand-line: rgba(184, 41, 234, 0.4);
  --ok: #3ddc97;
  --ok-soft: rgba(61, 220, 151, 0.1);
  --bad: #ff5c77;
  --bad-soft: rgba(255, 92, 119, 0.1);
  --warn: #ffbf47;
  --warn-soft: rgba(255, 191, 71, 0.1);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

/* html-theme-sync.js injects overflow-x:hidden, which turns body into a scroll
   container and breaks the sticky toolbar; clip keeps the no-sideways-scroll
   guarantee without that side effect. */
html:not(#cs223),
html:not(#cs223) body {
  overflow-x: clip;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.ex-shell {
  width: 100%;
  max-width: 1480px;
  margin: 0 auto;
  padding: 0 var(--pad) 64px;
}

/* ---------- header ---------- */
.ex-head {
  padding: clamp(20px, 4vw, 40px) 0 clamp(14px, 2.4vw, 24px);
  border-bottom: 1px solid var(--line);
}
.ex-kicker {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--brand);
}
.ex-head h1 {
  margin: 6px 0 8px;
  font-family: var(--font-display);
  font-size: clamp(1.7rem, 4.2vw, 2.6rem);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.01em;
}
.ex-lede {
  margin: 0;
  color: var(--muted);
}
.ex-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
  padding: 0;
  list-style: none;
}
.ex-meta li {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 4px 10px;
  border: 1px solid var(--line-strong);
  color: var(--muted);
}

/* ---------- sticky toolbar ---------- */
.ex-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  margin: 0 calc(var(--pad) * -1);
  padding: 10px var(--pad);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.ex-progress {
  flex: 1 1 200px;
  min-width: 140px;
}
.ex-progress-track {
  height: 4px;
  background: var(--line);
  overflow: hidden;
}
.ex-progress-fill {
  height: 100%;
  width: 0;
  background: var(--brand);
  transition: width 240ms ease;
}
.ex-progress-label,
.ex-score,
.ex-timer {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
}
.ex-progress-label {
  display: block;
  margin-top: 4px;
}
.ex-score strong,
.ex-timer strong {
  color: var(--text);
  font-weight: 700;
}
.ex-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* ---------- buttons ---------- */
.ex-btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 8px 12px;
  cursor: pointer;
  transition:
    border-color 140ms ease,
    background 140ms ease,
    color 140ms ease;
}
.ex-btn:hover {
  border-color: var(--brand);
  color: var(--brand);
}
.ex-btn:focus-visible,
.ex-input:focus-visible,
.ex-option:focus-within {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
.ex-btn--primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.ex-btn--primary:hover {
  color: #fff;
  filter: brightness(1.08);
}
html[data-theme="dark"] .ex-btn--primary {
  color: #12021c;
}
.ex-btn[disabled] {
  opacity: 0.45;
  cursor: default;
}

/* ---------- sections & questions ---------- */
.ex-section {
  margin-top: clamp(22px, 3.4vw, 36px);
}
.ex-section-head {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--brand-line);
}
.ex-section-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.25rem, 2.6vw, 1.6rem);
  font-weight: 700;
}
.ex-section-pts {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--brand);
  letter-spacing: 0.08em;
}
.ex-section-intro {
  margin: 12px 0 0;
}

.ex-part {
  margin-top: 14px;
  padding: clamp(14px, 2.2vw, 22px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 3px solid var(--line-strong);
  transition: border-color 160ms ease;
}
.ex-part[data-state="correct"] {
  border-left-color: var(--ok);
}
.ex-part[data-state="partial"] {
  border-left-color: var(--warn);
}
.ex-part[data-state="wrong"] {
  border-left-color: var(--bad);
}
.ex-part-head {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.ex-part-label {
  flex: 0 0 auto;
  min-width: 2.2em;
  font-family: var(--font-mono);
  font-weight: 800;
  color: var(--brand);
}
.ex-part-prompt {
  flex: 1 1 auto;
  min-width: 0;
}
.ex-part-prompt p {
  margin: 0 0 8px;
}
.ex-part-prompt p:last-child {
  margin-bottom: 0;
}
.ex-part-pts {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--faint);
  white-space: nowrap;
  padding-top: 3px;
}

/* Wide matrices scroll inside their own box instead of the page. */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
  margin: 0.6em 0;
}

/* ---------- answer area ---------- */
.ex-answer {
  margin-top: 14px;
  padding-left: calc(2.2em + 12px);
}
.ex-options {
  display: grid;
  gap: 8px;
}
.ex-option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  cursor: pointer;
  transition:
    border-color 140ms ease,
    background 140ms ease;
}
.ex-option:hover {
  border-color: var(--brand-line);
}
.ex-option input {
  margin: 4px 0 0;
  accent-color: var(--brand);
}
.ex-option.is-picked {
  border-color: var(--brand);
  background: var(--brand-soft);
}
.ex-option.is-right {
  border-color: var(--ok);
  background: var(--ok-soft);
}
.ex-option.is-wrong {
  border-color: var(--bad);
  background: var(--bad-soft);
}
.ex-fields {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 230px), 1fr));
  gap: 10px 14px;
}
.ex-field {
  display: grid;
  gap: 4px;
}
.ex-field label {
  font-size: 0.86rem;
  color: var(--muted);
}
.ex-input {
  width: 100%;
  font-family: var(--font-mono);
  font-size: 0.92rem;
  padding: 8px 10px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: 0;
}
.ex-input.is-right {
  border-color: var(--ok);
  background: var(--ok-soft);
}
.ex-input.is-wrong {
  border-color: var(--bad);
  background: var(--bad-soft);
}
.ex-hint {
  margin: 8px 0 0;
  font-size: 0.8rem;
  color: var(--faint);
}
.ex-part-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.ex-feedback {
  margin-top: 10px;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.03em;
}
.ex-feedback:empty {
  display: none;
}
.ex-feedback.is-right {
  color: var(--ok);
}
.ex-feedback.is-wrong {
  color: var(--bad);
}
.ex-feedback.is-partial {
  color: var(--warn);
}

/* ---------- solutions ---------- */
.ex-solution {
  margin-top: 14px;
  padding: 14px clamp(12px, 2vw, 18px);
  border: 1px dashed var(--brand-line);
  background: var(--brand-soft);
}
.ex-solution[hidden] {
  display: none;
}
.ex-solution-title {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--brand);
  margin-bottom: 6px;
}
.ex-steps {
  margin: 0;
  padding-left: 1.3em;
}
.ex-steps li {
  margin: 6px 0;
}
.ex-final {
  margin: 10px 0 0;
  padding: 8px 12px;
  border-left: 3px solid var(--ok);
  background: var(--ok-soft);
}
.ex-note {
  margin: 10px 0 0;
  padding: 8px 12px;
  border-left: 3px solid var(--warn);
  background: var(--warn-soft);
  font-size: 0.9rem;
}
.ex-selfmark {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  font-size: 0.86rem;
  color: var(--muted);
}
.ex-selfmark .ex-btn.is-on {
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}
html[data-theme="dark"] .ex-selfmark .ex-btn.is-on {
  color: #12021c;
}

/* ---------- result panel ---------- */
.ex-result {
  margin-top: 32px;
  padding: clamp(16px, 3vw, 26px);
  border: 1px solid var(--brand-line);
  background: var(--surface);
}
.ex-result h2 {
  margin: 0 0 6px;
  font-family: var(--font-display);
  font-size: 1.5rem;
}
.ex-result p {
  margin: 0;
  color: var(--muted);
}

/* ---------- header row with the theme toggle ---------- */
.ex-head-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
/* html-theme-sync.js injects a fixed round button style after our stylesheet;
   the doubled class wins without !important. Inside the course wrapper the
   site header already has a theme button, so this one only shows standalone. */
.sg-theme-toggle.sg-theme-toggle {
  position: static;
  flex: 0 0 auto;
  width: auto;
  height: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 0;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  box-shadow: none;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.sg-theme-toggle.sg-theme-toggle:hover {
  transform: none;
  border-color: var(--brand);
}
.sg-theme-toggle svg {
  width: 15px;
  height: 15px;
}
html[data-theme="dark"] .sg-theme-toggle .sg-icon-moon,
html:not([data-theme="dark"]) .sg-theme-toggle .sg-icon-sun {
  display: none;
}
html.is-embedded .sg-theme-toggle.sg-theme-toggle {
  display: none;
}
.ex-search:empty {
  display: none;
}
.ex-search {
  margin-top: 12px;
}

@media (max-width: 640px) {
  body {
    font-size: 14.5px;
  }
  .ex-answer {
    padding-left: 0;
  }
  /* Page gutter (12px) + card padding stays under ~24px per side on a phone. */
  .ex-part {
    padding: 12px 10px;
  }
  .ex-solution {
    padding: 12px 10px;
  }
  .ex-part-head {
    flex-wrap: wrap;
    gap: 4px 10px;
  }
  .ex-part-prompt {
    flex-basis: 100%;
    order: 3;
  }
  /* Two short rows on a phone: progress + score + timer, then one swipeable
     row of buttons. */
  .ex-bar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 6px 10px;
    padding-top: 8px;
    padding-bottom: 8px;
  }
  .ex-progress {
    min-width: 0;
  }
  .ex-progress-label {
    display: none;
  }
  .ex-score,
  .ex-timer {
    font-size: 0.68rem;
  }
  .ex-actions {
    grid-column: 1 / -1;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .ex-actions::-webkit-scrollbar {
    display: none;
  }
  .ex-btn {
    flex: 0 0 auto;
    padding: 7px 10px;
  }
}

@media print {
  .ex-bar,
  .ex-part-actions,
  .ex-selfmark,
  .sg-theme-toggle {
    display: none;
  }
  .ex-solution[hidden] {
    display: block;
  }
}
