/* Shared styles for maestrood.github.io (main page and course pages) */

:root {
  color-scheme: dark;

  /* Western University brand colours
     https://brand.westernu.ca/visual-identity/colours.html */
  --western-purple: #4f2683;   /* primary */
  --white: #ffffff;            /* primary */
  --orchid: #8f55e0;           /* secondary */
  --deep-focus: #201436;       /* secondary */
  --grey: #818284;             /* secondary */
  --sky: #7deafb;              /* accent: small details only */

  /* How the site uses them */
  --paper: var(--deep-focus);              /* page background */
  --wash: #301a51;                         /* Western Purple at 35% over Deep Focus */
  --ink: var(--white);                     /* headings, primary text */
  --body-text: rgba(255, 255, 255, 0.85);  /* paragraphs */
  --muted: rgba(255, 255, 255, 0.70);      /* secondary text */
  --line: var(--western-purple);           /* dividers, card borders */
  --brand: var(--western-purple);          /* primary button, tags */
  --accent: var(--orchid);                 /* decoration only: underlines, rules (3.7:1 on Deep Focus, too low for small text) */
  --link-hover: var(--sky);                /* underline colour on hover */
  --link: #b794f6;                         /* links: a light purple, 7.1:1 on Deep Focus */
  --header-bg: var(--western-purple);      /* top bar with the name and links */
  --header-ink: var(--white);
  --header-link: rgba(255, 255, 255, 0.85);
  --header-hover-line: var(--sky);
}

/* Light mode: white page, Deep Focus text, pale purple header */
:root[data-theme="light"] {
  color-scheme: light;
  --paper: var(--white);
  --wash: #f1eef5;                         /* Western Purple at 8% over white */
  --ink: var(--deep-focus);                /* 17.3:1 on white */
  --body-text: rgba(32, 20, 54, 0.85);     /* 11.1:1 */
  --muted: rgba(32, 20, 54, 0.72);         /* 6.9:1 */
  --line: #dcd4e6;                         /* Western Purple at 20% over white */
  --link-hover: var(--western-purple);
  --link: var(--orchid);                   /* links: Orchid, 4.6:1 on white */
  --header-bg: #e6e1ee;                    /* Western Purple at 14% over white */
  --header-ink: var(--deep-focus);
  --header-link: rgba(32, 20, 54, 0.85);   /* 10.6:1 on the header */
  --header-hover-line: var(--western-purple);
}

* { box-sizing: border-box; }

/* Text grows with the screen: 16px up to ~1600px wide, about 20px at 2560px */
html { scroll-behavior: smooth; font-size: clamp(100%, calc(0.52vw + 6.7px), 125%); }

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.65;
  overflow-wrap: break-word;
}

/* links are set apart by colour, and underlined only on hover or keyboard focus */
a {
  color: var(--link);
  text-decoration: none;
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
}
a:hover, a:focus-visible { text-decoration: underline; }

.container {
  /* one width for every section: 1008px (portrait + gap + text) on laptops, widening on big screens */
  width: min(max(63rem, calc(76vw - 200px)), calc(100% - 40px));
  margin: 0 auto;
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--header-bg);
  border-bottom: 3px solid var(--accent);
}

header nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 68px;
  gap: 24px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--header-ink);
  text-decoration: none;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.25rem;
  font-weight: 700;
}

.chao { height: 40px; width: auto; flex: none; filter: drop-shadow(0 0 2px rgba(236, 232, 243, 0.45)); }

/* Theme toggle: dark chao in light mode (switches to dark), hero chao in dark mode (switches to light) */
.theme-toggle {
  /* Fixed 40x40 box: both chao images sit centred in the same spot, so the
     button never changes size or position and a second click toggles back */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex: none;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  line-height: 0;
}
.theme-toggle .chao { max-width: 40px; max-height: 40px; }
.theme-toggle:hover .chao { transform: scale(1.06); }
.theme-toggle:focus-visible { outline: 2px solid var(--sky); outline-offset: 3px; border-radius: 6px; }
.theme-toggle .chao-hero { display: none; }
[data-theme="dark"] .theme-toggle .chao-dark { display: none; }
[data-theme="dark"] .theme-toggle .chao-hero { display: block; }
/* Ragnarok look (practice page, picked at random per session): deviling in light mode, angeling in dark mode */
/* one scale for both sprites (the angeling's, fitted to the 40px box), so they keep their sizes relative to each other;
   the wider deviling spills past the button's sides, but the clickable box stays 40x40 */
.theme-toggle .ro { display: none; flex: none; height: auto; max-width: none; filter: drop-shadow(0 0 2px rgba(236, 232, 243, 0.45)); }
/* only the 40x40 button takes clicks, never the part of a sprite that spills past it, so a click that switches modes
   always lands on the button again and switches back */
.theme-toggle img { pointer-events: none; }
.theme-toggle .ro-angel { width: 40px; }                     /* 68px wide at 40/68 */
.theme-toggle .ro-devil { width: calc(86px * 40 / 68); }    /* 86px wide at the same 40/68, about 51px */
.theme-toggle:hover .ro { transform: scale(1.06); }
[data-look="ragnarok"] .theme-toggle .chao { display: none; }
[data-look="ragnarok"] .theme-toggle .ro-devil { display: block; }
[data-look="ragnarok"][data-theme="dark"] .theme-toggle .ro-devil { display: none; }
[data-look="ragnarok"][data-theme="dark"] .theme-toggle .ro-angel { display: block; }

.nav-right { display: flex; align-items: center; gap: 18px; }
.nav-toggle { display: none; }

.nav-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 18px;
  font-size: 0.92rem;
}

/* Header links: white on the purple bar (dark mode), Deep Focus on the pale bar (light mode) */
.nav-links a { color: var(--header-link); text-decoration: none; }
.nav-links a:hover { color: var(--header-ink); text-decoration: underline; text-decoration-color: var(--header-hover-line); }

#about { background: linear-gradient(180deg, var(--wash), var(--paper)); }

h2 { color: var(--ink); }

h2::after {
  content: "";
  display: block;
  width: 48px;
  height: 3px;
  margin-top: 12px;
  background: var(--accent);
}

main { min-height: 70vh; }

section {
  padding: 72px 0;
  border-bottom: 1px solid var(--line);
  scroll-margin-top: 84px;
}

.hero {
  display: grid;
  grid-template-columns: 15rem 1fr;
  gap: 3rem;
  align-items: start;
}

.portrait-figure { margin: 0; width: 15rem; }

.portrait-figure figcaption {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.portrait {
  display: block;
  width: 15rem;
  height: auto;
  object-fit: cover;
  background: var(--wash);
  border: 1px solid var(--line);
}

.portrait-placeholder {
  display: grid;
  place-items: center;
  color: var(--western-purple);
  text-align: center;
  font-size: 0.9rem;
}

h1, h2, h3 {
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1.2;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(2.2rem, 5vw, 4rem);
  letter-spacing: -0.02em;
}

h2 {
  margin: 0 0 28px;
  font-size: 2rem;
}

h3 {
  margin: 0 0 6px;
  font-size: 1.25rem;
}

.subtitle {
  color: var(--ink);
  font-size: 1.12rem;
  font-weight: 700;
}

.bio {
  max-width: none;
  margin: 24px 0;
  color: var(--body-text);
  font-size: 1.05rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 24px;
}

.button {
  display: inline-block;
  padding: 9px 14px;
  border: 1px solid var(--grey);
  border-radius: 7px;
  color: var(--ink);
  text-decoration: none;
  background: transparent;
  font-weight: 700;
  font-size: 0.92rem;
}

.button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--white);
}

.button:hover { text-decoration: none; transform: translateY(-1px); border-color: var(--accent); }
.button.primary:hover { background: var(--accent); border-color: var(--accent); }

.links { font-size: 0.92rem; }
.links a { font-weight: 600; }

.personal { flex: 1; color: var(--muted); margin: 0; }

.irl { display: flex; align-items: flex-start; gap: 2rem; }

.cat {
  display: block;
  flex: none;
  width: 10rem;
  height: auto;
  border: 1px solid var(--line);
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px;
}

.card {
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
}

.card p { margin: 8px 0 0; color: var(--muted); }

.meta {
  color: var(--muted);
  font-size: 0.92rem;
}

.publication {
  display: grid;
  grid-template-columns: 6.875rem 1fr;
  gap: 18px;
  padding: 18px 0;
  border-top: 1px solid var(--line);
}

.publication:last-child { border-bottom: 1px solid var(--line); }

.year {
  align-self: start;
  padding-left: 10px;
  border-left: 3px solid var(--accent);
  color: var(--ink);
  font-weight: 800;
}

.publication p { margin: 4px 0 0; color: var(--muted); }

.news-item {
  display: grid;
  grid-template-columns: 7.5rem 1fr;
  gap: 20px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.news-item:last-child { border-bottom: 1px solid var(--line); }

.news-date { color: var(--muted); }

footer {
  padding: 28px 0;
  color: var(--muted);
  font-size: 0.9rem;
}

footer .container {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}
footer .container.footer-note { display: block; margin-top: 6px; font-size: 0.8rem; }

h2 + h3.group, h3.group {
  margin: 36px 0 4px;
  color: var(--muted);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

h2 + h3.group { margin-top: 0; }
/* group headings you can link to (e.g. #software): land below the sticky header, like sections */
h3.group[id] { scroll-margin-top: 100px; }

.tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--brand);
  color: var(--white);
  font-size: 0.78rem;
  font-weight: 700;
  vertical-align: middle;
}

.interests { margin: 0; padding-left: 20px; color: var(--body-text); }

@media (max-width: 760px) {
  /* Phone header: name + chao on one row, links on a compact row below.
     Not sticky here, so it doesn't eat a slice of a small screen. */
  header { position: static; }
  header nav {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 6px 12px;
    min-height: 0;
    padding: 10px 0 12px;
  }
  .nav-right { display: contents; }
  .theme-toggle { grid-column: 2; grid-row: 1; }
  .nav-links { grid-column: 1 / -1; justify-content: flex-start; gap: 2px 16px; font-size: 0.9rem; }
  /* the menu button (added by theme.js): a fixed 40x40 box beside the chao; the links fold into it */
  .has-nav-toggle header nav { grid-template-columns: 1fr auto auto; }
  .nav-toggle {
    display: inline-flex; grid-column: 3; grid-row: 1; align-items: center; justify-content: center;
    width: 40px; height: 40px; padding: 0; border: 0; border-radius: 6px; background: none; color: var(--header-ink); cursor: pointer;
  }
  .nav-toggle svg path { fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }
  .nav-toggle .nav-x, .nav-toggle[aria-expanded="true"] .nav-bars { display: none; }
  .nav-toggle[aria-expanded="true"] .nav-x { display: inline; }
  .nav-toggle:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }
  .has-nav-toggle .nav-links { display: none; }
  .has-nav-toggle .nav-links.is-open { display: flex; flex-direction: column; align-items: flex-start; gap: 0; font-size: 1rem; padding-bottom: 4px; }
  .has-nav-toggle .nav-links.is-open a { padding: 8px 0; }
  header nav { padding: 8px 0; }
  .nav-links a { padding: 4px 0; }   /* taller tap targets */
  section { padding: 48px 0; scroll-margin-top: 0; }
  h3.group[id] { scroll-margin-top: 16px; }   /* the phone header is not sticky */
  h2 { font-size: 1.75rem; }
  h3 { font-size: 1.1rem; }
  .subtitle { font-size: 1rem; }
  .bio { font-size: 1rem; }
  .card { padding: 16px; }
  .hero { grid-template-columns: 1fr; gap: 28px; }
  .portrait-figure, .portrait { width: 180px; }
  .portrait-figure { margin: 0 auto; text-align: center; }   /* centred above the intro, not stranded on the left */
  .irl { flex-direction: column; gap: 20px; }
  .cat { width: 140px; align-self: center; }
  .grid { grid-template-columns: 1fr; }
  .publication, .news-item { grid-template-columns: 1fr; gap: 4px; }
  footer .container { flex-direction: column; }
}

/* Course pages */
.breadcrumb { margin: 0 0 12px; color: var(--muted); font-size: 0.9rem; }
.page-title { font-size: clamp(1.9rem, 4.5vw, 3rem); }

.placeholder-box {
  padding: 22px;
  border: 1px dashed var(--grey);
  border-radius: 10px;
  color: var(--body-text);
}
.placeholder-box p { margin: 0 0 14px; }
.button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* Course topic menu */
:root { --spring: #b9f876; --tiger: #f0a757; }

.topic-menu {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 32px;
}
.topic-item {
  padding: 16px 0;
  border-top: 1px solid var(--line);
}
/* topics page: one heading per Brightspace category */
.topic-cat { margin: 48px 0 14px; font-size: 1.5rem; scroll-margin-top: 84px; }
.topic-cat::after { margin-top: 8px; }
.topic-cat:first-of-type { margin-top: 28px; }
.topic-item h3 { margin: 0 0 2px; font-size: 1.1rem; }
.topic-item p { margin: 0; color: var(--muted); font-size: 0.95rem; }

/* Topic pages */
.reading-list { margin: 0; padding-left: 20px; color: var(--body-text); max-width: 56rem; }
.reading-list li { margin: 6px 0; }

.problem-links { list-style: none; margin: 0; padding: 0; }
.problem-links li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 6px 0;
}
.status { font-size: 0.8rem; font-weight: 700; white-space: nowrap; color: var(--muted); }
.status.ready::before { content: "● "; color: var(--spring); }
.status.soon::before { content: "○ "; }

.topic-pager {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 0.95rem;
}
.pager-section { padding: 32px 0; }

/* Practice page: an app-style screen. The page itself never scrolls; the text menu on the
   left and the problem on the right each scroll on their own, so the menu is always visible. */
body.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
body.app > header { position: static; flex: none; }
body.app > main { flex: 1; min-height: 0; min-width: 0; display: flex; }
body.app > footer { display: none; }
.practice-app { flex: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; padding: 0; border: 0; }
.practice-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; padding: 6px 0; flex: none; }
.practice-bar .breadcrumb { margin: 0; font-size: 0.85rem; }

/* App pages get a slimmer header (the chao toggle keeps its fixed 40x40 box) */
body.app header nav { min-height: 52px; }
body.app header { border-bottom-width: 2px; }
body.app .brand { font-size: 1.1rem; }
.practice-app .container, body.app header .container { width: min(2400px, calc(100% - 32px)); }
.menu-toggle { display: none; }

.practice-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 18.75rem minmax(0, 1fr);
  gap: 1.25rem;
  padding-bottom: 14px;
}
.practice-menu {
  min-height: 0;
  overflow-y: auto;
  padding: 6px 6px 6px 0;
  border-right: 1px solid var(--line);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.95rem;
  line-height: 1.45;
}
.practice-menu ol, .practice-menu ul { list-style: none; margin: 0; padding: 0; }
.practice-menu a { display: block; color: var(--body-text); text-decoration: none; padding: 2px 6px; border-radius: 3px; }
.practice-menu a:hover { color: var(--ink); text-decoration: underline; text-decoration-color: var(--link-hover); }
.practice-menu a:focus-visible { outline: 2px solid var(--sky); outline-offset: 1px; }
/* sizes step down: chapter > topic > problem */
.menu-cat + .menu-cat { margin-top: 10px; }
.menu-cat-title { display: block; padding: 2px 6px; color: var(--ink); font-size: 1.12em; font-weight: 700; }
.practice-menu .menu-topic { font-size: 1em; }
.practice-menu .menu-problem { font-size: 0.92em; }
.menu-cat > ol { padding-left: 1.5ch; }
/* the open topic and the current problem are both highlighted */
.menu-topic-item.is-open > .menu-topic { font-weight: 700; }
.menu-topic-item ul { margin: 1px 0 6px; }
.menu-topic-item li { display: flex; }
.menu-topic-item li::before { content: "├─"; flex: none; padding: 2px 0 0 1.6ch; color: var(--muted); }
.menu-topic-item li:last-child::before { content: "└─"; }
.menu-problem { flex: 1; }
.practice-menu .menu-topic-item.is-open > .menu-topic, .practice-menu .menu-problem[aria-current] { background: var(--brand); color: var(--white); }
:root[data-theme="light"] .practice-menu .menu-topic-item.is-open > .menu-topic,
:root[data-theme="light"] .practice-menu .menu-problem[aria-current] { background: #efe9f7; color: var(--western-purple); }

.problem-panel {
  margin-top: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 22px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
}
.problem-panel h2 { margin-bottom: 10px; font-size: 1.45rem; }
.problem-panel h2::after { display: none; }
.problem-panel { font-size: 1.08rem; }
.problem-panel code, .problem-panel .problem-code, .problem-panel .problem-table { font-size: 1.02rem; }
.problem-panel code { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace; color: var(--ink); }
/* code names in running text (variables, registers, labels) get a light chip, so a variable "a" can't be read as the word a.
   Code blocks, fill-in code lines, and drag-to-reorder lines keep plain code. */
.problem-panel :is(p, li:not(.dnd-item), td, th, label) > code,
.problem-panel :is(p, li:not(.dnd-item), td, label) > strong > code {
  padding: 0.05em 0.3em;
  border-radius: 4px;
  background: var(--wash);
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 0.92em;
  white-space: nowrap;
}

/* Work on the left, solution on the right once it is shown (stacks on narrow screens) */
.problem-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0 28px; align-items: start; }
/* (practice.js decides which of these fits the screen) */
.problem-grid.has-solution { grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); }
.problem-grid.has-solution .solution { margin-top: 0; padding-left: 24px; border-left: 1px solid var(--line); }
.problem-grid.split .problem-work { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, max-content); gap: 0 28px; align-items: start; }
.problem-grid.split .problem-prompt { grid-column: 1; grid-row: 1 / span 2; }
.problem-grid.split .answer-form, .problem-grid.split .feedback { grid-column: 2; }
.problem-grid.split .answer-area { flex-direction: column; }
.problem-grid.split .answer-area > * { flex: 0 0 auto; }
.problem-grid.has-solution.split { grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); }

/* A table of answers and the remaining answer boxes sit side by side when there is room */
.answer-area { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 0 36px; }
.answer-area > .table-wrap { flex: 0 1 auto; }
.answer-area > .code-blanks { flex: 1 1 100%; }
.answer-area > .answer-grid { flex: 1 1 auto; min-width: 0; max-width: 100%; }   /* wraps under a wide table, and never runs past the panel */
.answer-area > .table-wrap + .answer-grid { margin-top: 4px; }

/* Answer boxes share one grid so they line up in a single column */
.answer-grid { display: grid; grid-template-columns: fit-content(50%) minmax(0, 1fr); gap: 10px 16px; align-items: center; margin: 12px 0 4px; }
.answer-slot { min-width: 0; }
.answer-slot input, .answer-slot select { max-width: 100%; }
.answer-slot > .answer-cell { max-width: 100%; }
.answer-grid > label { font-weight: 700; color: var(--ink); }
.answer-grid > label.is-wide { grid-column: 1 / -1; font-weight: 600; margin-top: 4px; }
.answer-grid > .answer-slot.is-wide { grid-column: 1 / -1; margin-top: -6px; }
.answer-slot select { max-width: 100%; }

/* Streak rank pop-up */
.rank-pop {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  transform: translate(-50%, -100%);
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.55)) drop-shadow(0 0 6px rgba(0, 0, 0, 0.25));
  animation: rank-pop 1.4s cubic-bezier(.2, .8, .3, 1) forwards;
  image-rendering: pixelated;
}
.rank-pop.is-max { height: 56px; width: auto; }
@keyframes rank-pop {
  0%   { opacity: 0; transform: translate(-50%, -100%) scale(0.3); }
  14%  { opacity: 1; transform: translate(-50%, -110%) scale(1.15); }
  26%  { transform: translate(-50%, -110%) scale(1); }
  75%  { opacity: 1; transform: translate(-50%, -150%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -190%) scale(0.95); }
}
@media (prefers-reduced-motion: reduce) {
  .rank-pop { animation: rank-fade 1.2s ease forwards; }
  @keyframes rank-fade { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }
}
.problem-panel .breadcrumb { margin-bottom: 6px; }
.problem-prompt { color: var(--body-text); }
.problem-prompt p { margin: 0 0 12px; }

.problem-table { border-collapse: collapse; margin: 4px 0 16px; font-variant-numeric: tabular-nums; }
.problem-table th, .problem-table td { padding: 6px 16px; border: 1px solid var(--line); text-align: center; }
.problem-table th { color: var(--ink); background: var(--wash); }
.problem-table tr.group-head th.blank { background: transparent; border-top-color: transparent; border-left-color: transparent; }

.answer-form { margin-top: 8px; }
.answer-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 10px 0; }
.answer-row label { font-weight: 700; color: var(--ink); }
.answer-row input {
  width: 9em;
  padding: 8px 10px;
  border: 2px solid var(--grey);
  border-radius: 7px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.answer-row input:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }
.answer-row input.is-right { border-color: var(--spring); }
.answer-row input.is-wrong { border-color: var(--tiger); }
.mark { display: inline-block; width: 1.2em; font-weight: 800; }
.mark.is-word { width: auto; margin-left: 6px; font-weight: 600; font-size: 0.92em; }
.mark.is-ok { color: var(--spring); }
.mark.is-bad { color: var(--tiger); }
:root[data-theme="light"] .mark.is-ok { color: #2f7d1e; }
:root[data-theme="light"] .mark.is-bad { color: #a8520a; }

.feedback:empty { display: none; }
.feedback {
  margin-top: 18px;
  padding: 12px 16px;
  border-left: 4px solid var(--grey);
  border-radius: 4px;
  background: var(--wash);
  color: var(--body-text);
}
.feedback.is-right { border-left-color: var(--spring); }
.feedback.is-wrong { border-left-color: var(--tiger); }

.solution { margin-top: 18px; color: var(--body-text); }
.solution h4 { margin: 0 0 6px; color: var(--ink); font-size: 1rem; }
.solution p { margin: 0 0 8px; }
.work { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.table-wrap { max-width: 100%; overflow-x: auto; }
.answer-table { font-size: 1.02rem; }
.answer-table th, .answer-table td { padding: 3px 8px; vertical-align: middle; }
.answer-table td:first-child { white-space: nowrap; }
.answer-form .answer-table input, .answer-form .answer-table select, .answer-form .code-blanks input, .answer-form .code-blanks select { padding: 2px 5px; }
.answer-table .mark, .code-blanks .mark { width: 1.1ch; }
.answer-cell { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.answer-form select, .answer-form input {
  padding: 5px 9px;
  border: 2px solid var(--grey);
  border-radius: 7px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.answer-form select:focus-visible, .answer-form input:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }
.answer-form .is-right { border-color: var(--spring); }
.answer-form .is-wrong { border-color: var(--tiger); }
.problem-code {
  margin: 4px 0 16px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--wash);
  color: var(--ink);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
  line-height: 1.6;
  overflow-x: auto;
  white-space: pre;
}
.code-line { min-height: 1.6em; display: flex; align-items: center; flex-wrap: nowrap; white-space: pre; }
.solution td code { white-space: nowrap; }
.code-blanks .answer-cell { margin: 2px 0; }
.code-blanks select, .code-blanks input { font-family: inherit; font-size: inherit; }
.expr { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; color: var(--ink); }
.fsm { display: block; width: 100%; max-width: 23.75rem; height: auto; margin: 4px 0 12px; color: var(--ink); }
.feedback ul { margin: 6px 0 0; padding-left: 20px; }
.solution ul { margin: 0 0 8px; padding-left: 20px; }
.solution .problem-table { margin-top: 0; }

.answer-row.is-wide { flex-direction: column; align-items: flex-start; gap: 4px; }
.answer-row.is-wide label { font-weight: 600; }
.answer-row.is-wide select { max-width: 100%; }

@media (max-width: 900px) {
  .practice-layout { grid-template-columns: 220px minmax(0, 1fr); gap: 14px; }
  .practice-menu { font-size: 0.88rem; }
}

/* Phones: one column; the menu becomes a drop-down under the ☰ button */
@media (max-width: 640px) {
  .practice-layout { grid-template-columns: minmax(0, 1fr); position: relative; }
  .menu-toggle {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 8px 10px;
    border: 1px solid var(--grey);
    border-radius: 7px;
    background: var(--paper);
    color: var(--ink);
    font: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
  }
  .practice-menu {
    display: none;
    position: absolute;
    inset: 0 0 auto 0;
    z-index: 5;
    max-height: 70vh;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--paper);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  }
  .practice-menu.is-open { display: block; }
  .problem-panel { padding: 14px; }
}

@media (max-width: 760px) {
  .topic-menu { grid-template-columns: 1fr; }
  .topic-item { grid-template-columns: 1fr auto; }
  .problem-panel { padding: 16px; }
  .problem-table th, .problem-table td { padding: 6px 10px; }
}

/* FSM diagrams: a halo in the panel colour keeps labels readable where arrows cross */
.fsm-label { paint-order: stroke; stroke: var(--paper); stroke-width: 4px; stroke-linejoin: round; }

/* Exam cheat sheet: a small [Cheat Sheet] button beside the title opens the matching pages */
.problem-head { position: relative; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 14px; margin-bottom: 10px; }
.problem-head h2 { margin: 0; }
.report-note { margin-left: auto; font-size: 0.8rem; color: var(--muted); }
.report-note a { color: var(--link); }
.cheat-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--link);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
  font-weight: 700;
  text-decoration: none;
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
  cursor: pointer;
}
.cheat-btn:hover, .cheat-btn[aria-expanded="true"] { text-decoration: underline; }
.cheat-btn:focus-visible { outline: 2px solid var(--sky); outline-offset: 3px; border-radius: 3px; }
.cheat-btn[hidden] { display: none; }
.cheat-pop {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 20;
  width: min(700px, 100%);
  max-height: 72vh;
  overflow-y: auto;
  margin-top: 6px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;                 /* the scanned sheet is black on white in both themes */
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
}
.cheat-pop[hidden] { display: none; }
.cheat-pop img { display: block; max-width: 100%; height: auto; margin: 0 auto; }
.cheat-pop img + img { margin-top: 10px; padding-top: 10px; border-top: 1px solid #ddd; }

/* Exam-style cache tables: one row per line, a thicker rule between sets */
.answer-table tr.group-start td { border-top: 2px solid var(--grey); }
.answer-table .mark-cell { border: 0; padding-left: 2px; background: transparent; }
.answer-table tr.group-start td.mark-cell, .answer-table tr.group-head th.mark-cell { border: 0; }
.answer-form .answer-table input[data-part] { text-align: center; padding: 2px 3px; }
.problem-prompt .problem-table { display: block; max-width: 100%; overflow-x: auto; }

.course-links { margin: 14px 0 0; font-size: 1.05rem; color: var(--muted); }

/* Wrong-answer ring burst (drawn in CSS) */
.ring-layer { position: fixed; inset: 0; z-index: 60; pointer-events: none; overflow: hidden; }
.ring { position: absolute; left: 0; top: 0; width: 22px; height: 22px; will-change: transform; }
.ring-spin {
  display: block;
  width: 100%;
  height: 100%;
  border: 4px solid #f6c21a;
  border-radius: 50%;
  box-shadow: 0 0 0 1px #9a6a00, inset 0 0 0 1px #9a6a00, inset 2px 2px 0 1px #fff3b0;
  animation: ring-spin 0.45s linear infinite;
}
@keyframes ring-spin {
  0%, 100% { transform: scaleX(1); }
  50% { transform: scaleX(0.18); }
}
.answer-form .answer-table td select { width: 100%; min-width: 4.5em; }

/* Short screens (e.g. 1366x768 laptops): tighten spacing, keep text readable */
@media (max-height: 760px) and (min-width: 641px) {
  .problem-panel { padding: 10px 18px; font-size: 1rem; }
  .problem-panel h2 { font-size: 1.3rem; }
  .problem-head { margin-bottom: 6px; }
  .problem-prompt p { margin-bottom: 8px; }
  .problem-table th, .problem-table td { padding: 4px 12px; }
  .answer-table th, .answer-table td { padding: 2px 7px; }
  .answer-grid { gap: 7px 14px; margin-top: 8px; }
  .answer-form select, .answer-form input { padding: 3px 8px; }
  .problem-code { padding: 8px 12px; margin-bottom: 10px; }
  .fsm { max-width: 300px; }
  .actions { margin-top: 14px; }
  .feedback { margin-top: 10px; padding: 8px 14px; }
  .practice-menu { font-size: 0.88rem; line-height: 1.35; }
}
@media (max-width: 1100px) and (min-width: 641px) {
  .practice-layout { grid-template-columns: 210px minmax(0, 1fr); gap: 14px; }
  .practice-menu { font-size: 0.86rem; }
}

/* units after an answer box (ns, ps, %, …) match the surrounding text */
.answer-cell .meta { font-size: inherit; color: var(--body-text); }

/* Negation as a bar over the negated part, as on the exams */
/* each bar is its own box, so x̄ȳ (two bars) never looks like one bar over xy */
.ov { display: inline-block; border-top: 1.5px solid currentColor; padding-top: 1px; margin: 0 0.07em; line-height: 1.15; }
.ov .ov { margin-top: 2px; }
/* step-by-step Boolean algebra in solutions */
.derive { margin: 4px 0 10px; padding: 8px 12px; border-left: 3px solid var(--line); font-family: Georgia, "Times New Roman", serif; font-size: 1.05rem; line-height: 1.9; color: var(--ink); }
.derive .meta { font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: 0.85rem; }
/* Light mode: a softer check button on the practice page */
:root[data-theme="light"] .answer-form .button.primary { background: #efe9f7; border-color: var(--western-purple); color: var(--western-purple); }
:root[data-theme="light"] .answer-form .button.primary:hover { background: var(--western-purple); border-color: var(--western-purple); color: var(--white); }

/* Ring counter, top right of the practice page */
.ring-count {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 2px 4px;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 1rem;
  font-weight: 800;
  line-height: 1;
  user-select: none;
  cursor: default;
}
.ring-icon { display: inline-block; width: 20px; height: 20px; }
.ring-icon .ring-spin { border-width: 3px; animation-duration: 0.9s; }
.ring-label {
  color: #f6c21a;
  font-style: italic;
  letter-spacing: 0.06em;
  text-shadow: 1px 1px 0 #6b4a00, -1px -1px 0 #6b4a00, 1px -1px 0 #6b4a00, -1px 1px 0 #6b4a00, 2px 2px 0 #6b4a00;
}
.ring-num { display: inline-block; min-width: 3ch; color: var(--ink); font-variant-numeric: tabular-nums; }
.ring-count.is-gain .ring-num { animation: ring-gain 0.22s ease; }
.ring-count.is-loss { animation: ring-shake 0.45s ease; }
.ring-count.is-loss .ring-num { animation: ring-flash 1.1s steps(1); }
@keyframes ring-gain { 40% { transform: scale(1.35); color: #f6c21a; } }
@keyframes ring-shake { 20% { transform: translateX(-5px); } 40% { transform: translateX(5px); } 60% { transform: translateX(-3px); } 80% { transform: translateX(3px); } }
@keyframes ring-flash { 0%, 30%, 60% { color: #ff5a5a; } 15%, 45%, 75% { color: var(--ink); } }
.ring.is-small { width: 16px; height: 16px; }
.ring.is-small .ring-spin { border-width: 3px; }
@media (prefers-reduced-motion: reduce) {
  .ring-icon .ring-spin { animation: none; }
  .ring-count.is-gain .ring-num, .ring-count.is-loss, .ring-count.is-loss .ring-num { animation: none; }
}

/* Units in problems (GHz, ns, ps, …): hover or tap for their value */
.unit { text-decoration: underline dotted; text-underline-offset: 3px; cursor: help; }
.unit:focus-visible { outline: 2px solid var(--sky); outline-offset: 1px; }
.unit-tip {
  position: fixed;
  z-index: 70;
  max-width: 290px;
  padding: 7px 11px;
  border-radius: 6px;
  background: var(--ink);
  color: var(--paper);
  font-size: 0.88rem;
  line-height: 1.4;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}
.unit-tip span { display: block; margin-top: 3px; font-size: 0.8rem; opacity: 0.8; }

/* Numbered-wire datapath figure (practice problems) */
.datapath-fig { display: block; width: 100%; max-width: 40rem; height: auto; margin: 4px 0 12px; color: var(--ink); }
.datapath-fig .dp-label { font: 600 13px Inter, ui-sans-serif, sans-serif; fill: currentColor; }
.datapath-fig .dp-num { font: 12px Inter, ui-sans-serif, sans-serif; fill: currentColor; }
.datapath-fig .dp-field { font: italic 12px Inter, ui-sans-serif, sans-serif; fill: currentColor; opacity: 0.8; }
.gate-fig { display: block; width: 100%; height: auto; margin: 4px 0 12px; color: var(--ink); }
.gate-fig .dp-label { font: 600 14px Inter, ui-sans-serif, sans-serif; fill: currentColor; }
.gate-fig .g-num { font: 10px Inter, ui-sans-serif, sans-serif; fill: currentColor; opacity: 0.65; }

/* Multiple choice with HTML labels (Boolean expressions with real overbars) */
.answer-cell.is-radio { align-items: flex-start; white-space: normal; gap: 12px; }
.radio-list { display: flex; flex-direction: column; gap: 4px; }
.radio-opt { display: flex; align-items: center; gap: 10px; padding: 3px 10px 3px 6px; border: 1px solid transparent; border-radius: 6px; cursor: pointer; }
.radio-opt:hover { border-color: var(--line); }
.radio-opt input { margin: 0; accent-color: var(--orchid); }
.radio-opt .opt-code { margin: 2px 0; padding: 6px 12px; font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: 0.85em; line-height: 1.45; white-space: pre; background: var(--wash); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; }
.radio-opt input:checked.is-right + .expr { color: var(--spring); }
.radio-opt input:checked.is-wrong + .expr { color: var(--tiger); }
:root[data-theme="light"] .radio-opt input:checked.is-right + .expr { color: #2f7d1e; }
:root[data-theme="light"] .radio-opt input:checked.is-wrong + .expr { color: #a8520a; }
/* an equation in a derivation note never breaks across lines */
.derive .eq { white-space: nowrap; }
/* the cheat sheet's datapath figure: control signals in blue, as on the handout */
.datapath-fig.cheat-dp { max-width: 44rem; }
.datapath-fig .dp-ctl { fill: #1d3fd8; }
:root:not([data-theme="light"]) .datapath-fig .dp-ctl { fill: #8fb0ff; }

/* drag-and-drop code order (practice: reordering to remove stalls) */
.order-box { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
.order-list { list-style: none; margin: 0; padding: 0; min-width: 16rem; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.order-item {
  display: flex; align-items: center; gap: 10px; padding: 4px 12px;
  border-bottom: 1px solid var(--line); background: var(--paper);
  cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none;
}
.order-item:last-child { border-bottom: 0; }
.order-item code { white-space: pre; font-size: 0.95rem; }
.order-item:focus-visible { outline: 2px solid var(--sky); outline-offset: -2px; }
/* drag and drop: the held line is a copy that follows the pointer; the line itself stays as a dashed placeholder */
.order-item.is-placeholder { background: var(--wash); box-shadow: inset 0 0 0 1px var(--accent); }
.order-item.is-placeholder > * { visibility: hidden; }
.dnd-ghost {
  position: fixed; z-index: 60; margin: 0; list-style: none; pointer-events: none; cursor: grabbing;
  border: 1px solid var(--accent); border-radius: 6px; background: var(--paper); color: var(--ink);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28);
}
.dnd-ghost code { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace; font-size: 0.95rem; white-space: pre; }
.dnd-box.is-dragging, .dnd-box.is-dragging * { cursor: grabbing; }
/* instruction bank: the code on the left (fixed number of lines), the bank on the right */
.bank-box { display: flex; flex-wrap: wrap; gap: 6px 28px; align-items: flex-start; }   /* the bank drops under the code when there isn't room beside it */
.bank-box .bank-col { min-width: 0; max-width: 100%; }
.bank-box .order-tools { flex-basis: 100%; }
.bank-head { margin-bottom: 4px; font-size: 0.85rem; font-weight: 700; color: var(--muted); }
.bank-box .order-list { min-width: 15rem; }
.bank-box .bank-slots { min-width: 17.5rem; }   /* wide enough for a labelled line, so the bank does not shift as lines fill in */
.bank-box .order-item, .dnd-empty { min-height: 2.25rem; }
.dnd-empty { border-bottom: 1px dashed var(--line); }
.dnd-empty:last-child { border-bottom: 0; }
.bank-slots[data-gutter] > li { display: flex; align-items: center; padding-left: 10px; }
.bank-slots[data-gutter] > li::before { content: ""; flex: none; width: 4.2ch; font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace; font-size: 0.95rem; color: var(--ink); }
.bank-slots[data-gutter] > .order-item { gap: 6px; }
.bank-pool:empty { min-height: 2.25rem; }

.order-grip { color: var(--muted); font-size: 0.9rem; }
.order-tools { display: flex; align-items: baseline; gap: 12px; }
.order-reset {
  padding: 0; border: 0; background: none; color: var(--link); cursor: pointer;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace; font-size: 0.85rem; font-weight: 700;
  text-decoration: none; text-decoration-thickness: 2px; text-underline-offset: 3px;
}
.order-reset:hover { text-decoration: underline; }
.order-reset:focus-visible { outline: 2px solid var(--sky); outline-offset: 3px; border-radius: 3px; }
.radio-opt .opt-text { font-family: Inter, ui-sans-serif, system-ui, sans-serif; font-size: 1rem; }
.answer-form input.is-bits { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace; letter-spacing: 0.02em; }

/* Phones: each answer label sits above its box, and nothing is wider than the screen */
@media (max-width: 640px) {
  .answer-area > .answer-grid { flex: 1 1 100%; min-width: 0; }
  .answer-grid { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .answer-grid > label { margin-top: 8px; overflow-wrap: anywhere; }
  .answer-grid > label:first-child { margin-top: 0; }
  .answer-grid > .answer-slot.is-wide { margin-top: 0; }
  .answer-slot { min-width: 0; }
  .answer-slot input, .answer-slot select { max-width: 100%; }
  .answer-cell { flex-wrap: wrap; }
  .answer-slot > .answer-cell { display: flex; max-width: 100%; }
  .answer-slot > .answer-cell > input, .answer-slot > .answer-cell > select { flex: 0 1 auto; min-width: 0; }
  .order-box, .order-list { min-width: 0; max-width: 100%; }
  .order-item code, .radio-opt .opt-code { white-space: pre-wrap; overflow-wrap: anywhere; }
  .bank-box .order-list, .bank-box .bank-slots { min-width: 0; }
}
/* long code inside an answer option (e.g. a whole for loop) wraps at spaces instead of running off the edge */
.radio-opt .opt-text code { white-space: pre-wrap; }
.radio-opt .opt-text { min-width: 0; overflow-wrap: anywhere; }
