/* =========================================================
   Problem Sets page (problem-sets.html only)

   Scoped under .ps-app so the JetBrains Mono + Outfit widget look
   applies here without changing the rest of the site. Colors reuse the
   shared dark theme tokens from tokens.css, including --color-pass and
   --color-fail for the correct / incorrect states. Correct and incorrect
   are shown with an ICON as well as color, so they do not rely on color
   alone.
   ========================================================= */

/* ---- Layout: 1/4 rail + 3/4 activity ----------------------------------- */
.ps-page {
  max-width: var(--container-max);
  width: 100%;
  margin-inline: auto;
  padding-inline: clamp(1rem, 3vw, 2rem);
}

.ps-app {
  /* Widget fonts, scoped here. UI text is Outfit; code is JetBrains Mono. */
  font-family: "Outfit", var(--font-sans);
  display: grid;
  grid-template-columns: minmax(230px, 1fr) 3fr;
  gap: clamp(1rem, 2.5vw, 1.75rem);
  align-items: start;
}

.ps-app code,
.ps-app kbd,
.ps-app input,
.ps-app select,
.ps-app textarea,
.ps-app .ps-code {
  font-family: "JetBrains Mono", var(--font-mono);
}

.ps-note {
  color: var(--color-ink-muted);
  margin: 0;
  padding: 0.75rem;
  font-size: 0.95rem;
}

/* ---- Left rail: pick a set --------------------------------------------- */
.ps-rail {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.75rem;
  position: sticky;
  top: calc(var(--header-h) + 0.75rem);
  box-shadow: var(--shadow-sm);
}

.ps-rail__title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-ink-muted);
  margin: 0.35rem 0.4rem 0.6rem;
}

.ps-rail__group + .ps-rail__group { margin-top: 0.9rem; }

.ps-rail__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.ps-slot {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 0.55rem 0.7rem;
  color: var(--color-ink);
  cursor: pointer;
  font: inherit;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}

.ps-slot:hover { background: var(--color-tab-bg-hover); }

.ps-slot:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.ps-slot.is-active {
  background: var(--color-accent-soft);
  border-color: var(--color-accent);
}

.ps-slot__name { font-weight: 600; font-size: 0.95rem; }

.ps-slot__meta {
  font-size: 0.8rem;
  color: var(--color-ink-muted);
}

/* Placeholder slots (no content yet) read as muted and are not clickable. */
.ps-slot.is-empty {
  cursor: default;
  opacity: 0.6;
}
.ps-slot.is-empty:hover { background: transparent; }

/* ---- Right side: the activity ------------------------------------------ */
.ps-main {
  min-width: 0; /* let wide code lines scroll instead of stretching the grid */
}

.ps-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.ps-toolbar__meta { display: flex; flex-direction: column; gap: 0.15rem; }
.ps-toolbar__title { margin: 0; font-size: 1.3rem; font-weight: 700; }
.ps-toolbar__points { color: var(--color-ink-muted); font-size: 0.9rem; }

.ps-toolbar__actions { display: flex; gap: 0.5rem; }

/* Big live score: earned / total. The earned count is green so progress
   pops; the total stays muted. */
.ps-score {
  font-family: "JetBrains Mono", var(--font-mono);
  font-weight: 700;
  font-size: clamp(1.8rem, 4.5vw, 2.6rem);
  line-height: 1;
  white-space: nowrap;
  letter-spacing: 0.01em;
}
.ps-score__earned { color: var(--color-pass); }
.ps-score__sep,
.ps-score__total { color: var(--color-ink-muted); }

/* Buttons */
.ps-btn {
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  padding: 0.5rem 1rem;
  cursor: pointer;
  background: var(--color-surface);
  color: var(--color-ink);
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), opacity 0.15s var(--ease);
}
.ps-btn:hover { border-color: var(--color-accent); }
.ps-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.ps-btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.ps-btn--primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}
[data-theme="dark"] .ps-btn--primary { color: #08243a; }
.ps-btn--primary:hover { filter: brightness(1.05); }

/* ---- Capture area (this is what becomes the submitted PNG) ------------- */
.ps-capture {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: clamp(1rem, 2.5vw, 1.6rem);
  box-shadow: var(--shadow-sm);
  position: relative; /* anchors the .ps-watermark overlay on Submit */
}

/* ---- Submission watermark (.ps-watermark) -------------------------------- */
/* Injected by buildWatermark() in problem-set.js just before the Submit PNG
   renders, and removed right after, so it only ever exists in the image:
   a red diagonal stamp with the student's name and ceiling-rounded percent
   score. Translucent on purpose so the work underneath stays gradable. */
.ps-watermark {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;      /* clip the rotated text at the card edges */
  pointer-events: none;
}

.ps-watermark__stamp {
  transform: rotate(-45deg);
  text-align: center;
  color: #dc2626;
  opacity: 0.38;
  font-family: "Outfit", var(--font-sans, sans-serif);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  line-height: 1.15;
  white-space: nowrap;
}

.ps-capture__head {
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 0.75rem;
  margin-bottom: 1rem;
}
.ps-capture__title { margin: 0; font-size: 1.15rem; font-weight: 700; }
.ps-capture__stamp {
  margin: 0.35rem 0 0;
  color: var(--color-ink-muted);
  font-size: 0.9rem;
}
.ps-capture__stamp:empty { display: none; }

/* The server's process record and verify code, under the name stamp. The
   code is read back off the image by hand, so it gets the code font. */
.ps-capture__record { font-family: "JetBrains Mono", var(--font-mono, monospace); font-size: 0.8rem; }
.ps-capture__code { white-space: nowrap; font-weight: 700; color: var(--color-ink); }

/* ---- Automated-browser banner (.ps-automation) --------------------------- */
/* Injected by buildAutomationBanner() in problem-set.js only while the Submit
   PNG renders, and only when the browser reported navigator.webdriver. Large,
   solid red text across the top of the image, so it cannot be missed while
   grading. Red on the card in both themes: #FF6B6B is 5.9:1 on the dark
   card, #B91C1C is 6.5:1 on white. */
.ps-automation {
  margin: 0 0 1rem;
  padding: 0.6rem 0.5rem;
  border-block: 4px solid currentColor;
  color: #FF6B6B;
  text-align: center;
  font-family: "Outfit", var(--font-sans, sans-serif);
}
:root[data-theme="light"] .ps-automation { color: #B91C1C; }
.ps-automation__title {
  font-size: clamp(1.8rem, 5vw, 3rem);
  font-weight: 800;
  line-height: 1.1;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.ps-automation__note { margin-top: 0.3rem; font-size: 1rem; font-weight: 600; }

/* ---- A part (one section of the set) ----------------------------------- */
.ps-part { margin-top: 1.5rem; }
.ps-part:first-child { margin-top: 0; }

.ps-part__title {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--color-accent);
  line-height: 1.7;
}

/* The setup line a part's rows all depend on, shown on the title row so it
   cannot be skimmed past. Resets the title's accent color and bold weight so
   it reads as code rather than as more heading. It is inline-block, so on a
   narrow screen it wraps to its own line instead of overflowing. */
.ps-given {
  display: inline-block;
  margin-left: 0.6rem;
  vertical-align: middle;
  font-family: "JetBrains Mono", var(--font-mono);
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--color-ink);
  background: var(--color-accent-soft);
  border: 1px solid var(--color-accent);
  border-radius: var(--radius-sm);
  padding: 0.15rem 0.5rem;
  max-width: 100%;
  word-break: break-word;
}

.ps-part__instructions {
  margin: 0 0 0.85rem;
  color: var(--color-ink-muted);
  font-size: 0.9rem;
}
.ps-part__instructions p { margin: 0.15rem 0; }

/* ---- Rows table -------------------------------------------------------- */
.ps-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}
.ps-table th {
  text-align: left;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-ink-muted);
  font-weight: 600;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--color-border);
}
/* A variable-trace header is a Python variable name, and Python is case
   sensitive: show it exactly as written (a, not A), in the code font. */
.ps-table th.ps-th--var,
.ps-table th .ps-th-var {
  text-transform: none;
  letter-spacing: normal;
  font-family: "JetBrains Mono", var(--font-mono);
  font-size: 0.9rem;
}
.ps-table td {
  padding: 0.45rem 0.6rem;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}
.ps-table tr:last-child td { border-bottom: none; }

.ps-rownum { color: var(--color-ink-muted); width: 1%; white-space: nowrap; }

/* Code shown to the student. Wraps long lines rather than overflowing. */
.ps-code {
  display: inline-block;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.25rem 0.5rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* A multi-line code block (an if statement, for example), built by codeEl()
   as one .ps-code__line shield per line. The COLUMN direction is what makes
   the lines stack: each line is itself a flex container (.ps-shield), and a
   newline inside one would not break the parent's flex line. Keep this rule
   after .ps-code so the flex display wins over inline-block. */
.ps-code--block {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin: 0;
  font-family: "JetBrains Mono", var(--font-mono);
  line-height: 1.5;
}

/* Each line keeps the shield's own inline-flex layout (set by .ps-shield);
   this only stops a short line from being stretched by the column. */
.ps-code__line { flex: 0 0 auto; }

/* ---- Line Tracing ------------------------------------------------------ */
/* One snippet, a line per row, so the code column reads as a single listing:
   no box per line and no rule between lines, just tight rows on one shared
   background. Leading spaces carry the indentation (the shield keeps them at
   full width), so the font must stay monospace. */
.ps-table--lines td {
  border-bottom: none;
  padding-block: 0.15rem;
}
.ps-table--lines td.ps-line {
  background: var(--color-bg);
  font-family: "JetBrains Mono", var(--font-mono);
  height: 1.6rem;
  width: 1%;               /* the code column hugs its longest line... */
  white-space: nowrap;
  padding-right: 1.5rem;
}
.ps-table--lines .ps-line .ps-code {
  border: none;
  background: transparent;
  padding: 0;
}
/* ...and never wraps one: a wrapped line reads as an extra line to number. */
.ps-table--lines .ps-line .ps-shield { flex-wrap: nowrap; }
.ps-table--lines td.ps-line--blank {
  height: 0.6rem;
  padding-block: 0;
}
.ps-scroll { overflow-x: auto; }

/* Variable Tracing and Name That Error keep each code line whole too (see
   NOWRAP_TYPES in problem-set.js): a = 7 split over three lines reads as three
   lines of code. The code column takes its full width first. */
.ps-table--nowrap .ps-code { white-space: pre; }
.ps-table--nowrap .ps-shield { flex-wrap: nowrap; }
/* With the code at full width, three trace columns only fit if their boxes
   give way, so they share what is left instead of keeping a fixed width. */
.ps-table--nowrap .ps-input { width: 100%; min-width: 5rem; }

/* The index table under a given string or list (PS9 onward). */
.ps-index-wrap { margin: 0 0 0.75rem; }
.ps-index {
  border-collapse: collapse;
  font-family: "JetBrains Mono", var(--font-mono);
  font-size: 0.9rem;
}
.ps-index th,
.ps-index td {
  border: 1px solid var(--color-border);
  padding: 0.2rem 0.45rem;
  text-align: center;
  white-space: nowrap;
}
.ps-index th {
  font-family: var(--font-sans, sans-serif);
  font-size: 0.78rem;
  font-weight: 600;
  text-align: left;
  color: var(--color-ink-muted);
}
.ps-index td { color: var(--color-ink); }
.ps-index tr:first-child td { background: var(--color-accent-soft); }
.ps-index tr:last-child td { font-weight: 600; }
.ps-table--lines .ps-input { min-width: 6rem; width: 7rem; }

/* The worked first line: its steps are given, so they show as a filled,
   locked box rather than an input. */
.ps-given-steps {
  display: inline-block;
  min-width: 6rem;
  font-family: "JetBrains Mono", var(--font-mono);
  font-weight: 600;
  background: var(--color-accent-soft);
  border: 1px solid var(--color-accent);
  border-radius: var(--radius-sm);
  padding: 0.35rem 0.5rem;
}

/* "Output would be:" sits under the listing, set apart by a rule. */
.ps-table--lines tr:last-child td {
  border-top: 1px solid var(--color-border);
  padding-top: 0.6rem;
}
.ps-line__label { font-weight: 600; }

/* The character shown for a caesar row. */
.ps-char {
  font-family: "JetBrains Mono", var(--font-mono);
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.05em;
}

/* ---- Copy-deterrent text (.ps-shield) ----------------------------------- */
/* Built by shieldedText() in problem-set.js: characters sit in shuffled DOM
   order and the inline `order` on each span restores the visual order, so
   these flex rules are what makes the shuffle invisible. Keep this AFTER the
   .ps-code rules so the flex display wins. Do not "simplify" these to plain
   inline text: that would put clean, copyable text back in the DOM. */
.ps-shield {
  display: inline-flex;
  flex-wrap: wrap;          /* wraps between word chunks, in visual order */
  user-select: none;        /* no highlight-copy (screen readers unaffected) */
  -webkit-user-select: none;
}

.ps-shield__chunk {
  display: inline-flex;
}

.ps-shield__chunk > span {
  white-space: pre;         /* keep spaces and runs of spaces at full width */
}

/* ---- Inputs + verdict cell --------------------------------------------- */
/* A cell holds the control(s) plus a verdict marker. The marker shows an
   icon and color once the server has checked the answer. */
.ps-cell { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.ps-input,
.ps-select {
  font-size: 0.95rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-ink);
  min-width: 7rem;
}
.ps-input:focus-visible,
.ps-select:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 1px;
}
.ps-input--block { width: 100%; min-width: 0; }
textarea.ps-input { resize: vertical; min-height: 2.4rem; line-height: 1.4; }

/* Valid / Invalid toggle (two buttons) */
.ps-toggle { display: inline-flex; gap: 0.3rem; }
.ps-toggle__btn {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-ink);
  cursor: pointer;
}
.ps-toggle__btn[aria-pressed="true"] {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
}
[data-theme="dark"] .ps-toggle__btn[aria-pressed="true"] { color: #08243a; }

/* The verdict marker: hidden until checked, then icon + color. */
.ps-verdict {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.85rem;
  font-weight: 600;
  min-width: 1.25rem;
}
.ps-verdict__icon { font-size: 1rem; line-height: 1; }

.ps-cell.is-correct .ps-input,
.ps-cell.is-correct .ps-select { border-color: var(--color-pass); }
.ps-cell.is-correct .ps-verdict { color: var(--color-pass); }

.ps-cell.is-incorrect .ps-input,
.ps-cell.is-incorrect .ps-select { border-color: var(--color-fail); }
.ps-cell.is-incorrect .ps-verdict { color: var(--color-fail); }

.ps-cell.is-checking .ps-verdict { color: var(--color-ink-muted); }

/* Per-row feedback (hint shown only when wrong). */
.ps-feedback {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: var(--color-fail);
}
.ps-feedback:empty { display: none; }

/* Shown for ~3s when a paste into an answer box is refused (blockPaste() in
   problem-set.js). Accent, not --color-fail, so red keeps meaning "your
   answer is wrong" and never gets confused with "that input was rejected". */
.ps-paste-note {
  margin: 0.3rem 0 0;
  font-size: 0.8rem;
  color: var(--color-accent);
  opacity: 0;
  transition: opacity 140ms var(--ease);
}
.ps-paste-note:empty { display: none; }
.ps-paste-note.is-visible { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  .ps-paste-note { transition: none; }
}

/* A small banner for load or network problems. */
.ps-banner {
  background: var(--color-fail-soft);
  border: 1px solid var(--color-fail);
  color: var(--color-ink);
  border-radius: var(--radius-md);
  padding: 0.6rem 0.85rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

/* =========================================================
   Small-screen overrides

   These live at the END of the file on purpose: media queries do NOT add
   specificity, so an override only wins if it comes AFTER the base rule it
   overrides in source order. The base .ps-rail (sticky), .ps-rail__list,
   .ps-table, and .ps-input rules are all defined above, so these must follow
   them — otherwise the rail stays sticky and pins over the activity.
   ========================================================= */

@media (max-width: 760px) {
  /* One column: the set picker sits on top and the activity below it, so a
     student picks a set and then scrolls down to the problems. (The picker is
     first in source order, so stacking already puts it on top.) */
  .ps-app { grid-template-columns: 1fr; }

  /* On a phone the rail must scroll away with the page instead of pinning,
     so it never covers the activity as the student scrolls down to it. */
  .ps-rail {
    position: static;
    top: auto;
  }

  /* Compact the numbered slots into a wrapping grid so the picker stays a
     short block at the top instead of a tall list to scroll past. */
  .ps-rail__list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.4rem;
  }

  /* Let wide tables fit a narrow screen: tighten the cell padding and lower
     the input floor so columns shrink rather than pushing the page sideways. */
  .ps-table th,
  .ps-table td { padding: 0.4rem 0.4rem; }
  .ps-input,
  .ps-select { min-width: 4.5rem; }
}

@media (max-width: 480px) {
  /* Reclaim edge space and shrink the controls another step for the smallest
     phones, keeping multi-column tables on-screen. */
  .ps-page { padding-inline: 0.75rem; }
  .ps-rail__list { grid-template-columns: 1fr 1fr; }
  .ps-table { font-size: 0.9rem; }
  .ps-input,
  .ps-select { min-width: 3.5rem; }

  /* Line Tracing never wraps code, so on a phone the listing shrinks to fit
     the code and the step boxes side by side (it scrolls only if a line is
     still too long). */
  .ps-table--lines td.ps-line { font-size: 0.75rem; padding-right: 0.5rem; }
  .ps-table--lines .ps-input { min-width: 3.75rem; width: 4rem; }

  /* Stack the toolbar so the title, score, and buttons each get their own
     row instead of crowding. */
  .ps-toolbar { flex-direction: column; align-items: flex-start; }
  .ps-toolbar__actions { width: 100%; }
  .ps-toolbar__actions .ps-btn { flex: 1; }
}

/* ---- Verify a Submission (verify.html) ----------------------------------- */
/* Staff page for checking a PNG's verify code. Reuses the Problem Sets
   buttons and tokens; only the form and result card are new. */
.pv-lede { margin: 0.6rem 0 0; color: var(--color-ink-muted); max-width: 60ch; }
.pv-form {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  margin-bottom: 1.25rem;
}
.pv-field { display: flex; flex-direction: column; gap: 0.3rem; }
.pv-field label { font-weight: 600; }
.pv-field input {
  font: inherit;
  padding: 0.5rem 0.65rem;
  min-width: 16rem;
  border: 1px solid var(--color-ink-muted);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  color: var(--color-ink);
}
#pv-code { font-family: "JetBrains Mono", var(--font-mono, monospace); min-width: 22rem; text-transform: uppercase; }
.pv-card {
  background: var(--color-surface);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0.9rem 1.1rem;
  margin-bottom: 1.25rem;
}
.pv-card.is-match { border-color: var(--color-pass); }
.pv-card.is-nomatch { border-color: var(--color-fail); }
.pv-card__title { margin: 0 0 0.6rem; font-weight: 700; }
.pv-card__grid { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1.25rem; margin: 0; }
.pv-card__grid dt { color: var(--color-ink-muted); }
.pv-card__grid dd { margin: 0; }
.pv-help { color: var(--color-ink); max-width: 70ch; }
.pv-help h2 { font-size: 1.05rem; }
.pv-help li { margin-bottom: 0.4rem; }
