/* /library/<subject> — one subject's shelf.
 *
 * Page-scoped plain CSS (linked only from library_subject.html), so no Tailwind rebuild
 * — same arrangement as 20-simulate.css / 32-home.css.
 *
 * The category picker is the home page's segmented control generalised: that one has
 * exactly three segments and hardcodes their ids in the stylesheet, which cannot work
 * here because the segment count varies per subject (2 for AP Precalculus, 11 for
 * A-Level Physics). Selection is matched on [data-cat] instead, so the markup carries
 * the mapping and the CSS stays fixed.
 */

/* ─── Action buttons ──────────────────────────────────────────────────────────── */
/* Label only — the descriptions were cut in review, so these are pills, four across on
 * anything but the narrowest phone. */
.libsub-actions {
  display: grid;
  gap: 0.5rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 560px) { .libsub-actions { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.libsub-action {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 0.5rem;
  border-radius: 0.875rem;
  border: 1px solid rgba(var(--accent-rgb), 0.22);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.14) 0%, rgba(255, 255, 255, 0.02) 65%);
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}
.libsub-action:hover {
  border-color: rgba(var(--accent-rgb), 0.55);
  transform: translateY(-2px);
  box-shadow: 0 10px 26px -18px rgba(var(--accent-rgb), 0.9);
}
.libsub-action-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem; height: 1.75rem;
  flex: none;
  border-radius: 0.5rem;
  background: rgba(var(--accent-rgb), 0.18);
  color: rgb(var(--accent-rgb));
  transition: background 0.18s ease, box-shadow 0.18s ease;
}
.libsub-action:hover .libsub-action-icon {
  background: rgba(var(--accent-rgb), 0.3);
  box-shadow: 0 0 18px -6px rgba(var(--accent-rgb), 0.8);
}
.libsub-action-label {
  font-size: 0.875rem;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}

/* ─── Category picker ──────────────────────────────────────────────────────────── */
/* Visually hidden, not display:none — a display:none radio is unreachable by keyboard
 * and drops out of the tab order, which would make the whole picker mouse-only. */
.libsub-radio {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Wraps onto as many rows as it needs. It used to scroll horizontally with a fade, which
 * meant categories were genuinely cut off at the right edge with no reliable way to know
 * what you were missing — worse than two rows of chips. */
.libsub-tabs-wrap { position: relative; }
.libsub-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  padding-bottom: 0.75rem;
}

/* Each tab wears its category's own hue (set per-label from _LIBRARY_TABS), so eight tabs
 * read as eight THINGS rather than one grey strip — and you can re-find "the green one"
 * after scrolling. The icon sits in a tinted chip, matching the file rows and topic chips
 * so the whole page shares one visual grammar. */
.libsub-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.5625rem;
  flex: 0 0 auto;
  cursor: pointer;
  padding: 0.5rem 0.9375rem 0.5rem 0.5rem;
  border-radius: 0.875rem;
  border: 1px solid rgba(var(--accent-rgb), 0.2);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.1) 0%, rgba(255, 255, 255, 0.02) 65%);
  color: rgb(203, 213, 225);
  font-size: 0.875rem;
  font-weight: 600;
  white-space: nowrap;
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease, color 0.18s ease;
}
.libsub-tab:hover {
  border-color: rgba(var(--accent-rgb), 0.5);
  color: #fff;
  transform: translateY(-1px);
}
.libsub-tab-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.875rem; height: 1.875rem;
  flex: none;
  padding: 0.3125rem;
  border-radius: 0.5rem;
  background: rgba(var(--accent-rgb), 0.16);
  color: rgb(var(--accent-rgb));
  transition: background 0.18s ease;
}
.libsub-tab:hover .libsub-tab-icon { background: rgba(var(--accent-rgb), 0.26); }
/* Eleven categories wrap to six rows on a phone at full size, which is a wall of chips
 * before you reach a single file. Tightening below 640px fits three per row. */
@media (max-width: 639px) {
  .libsub-tab { padding: 0.375rem 0.75rem 0.375rem 0.375rem; gap: 0.4375rem; font-size: 0.8125rem; }
  .libsub-tab-icon { width: 1.625rem; height: 1.625rem; padding: 0.25rem; }
  .libsub-tab-count { font-size: 0.625rem; padding: 0 0.25rem; }
}

.libsub-tab-count {
  font-variant-numeric: tabular-nums;
  font-size: 0.6875rem;
  padding: 0.0625rem 0.375rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  color: rgba(148, 163, 184, 0.9);
}

/* Panels are hidden by default and revealed by the matching checked radio. Every pairing
 * has to be written out because CSS cannot interpolate an attribute into a selector; the
 * template generates ids from the same keys, so they cannot drift. */
.libsub-panel { display: none; }

/* Selected category. Every pairing is written out because CSS cannot interpolate an
 * attribute into a selector — but the key set is a fixed registry (_LIBRARY_CATEGORIES in
 * web/routes/site.py plus "papers" and the "misc" catch-all), so this list is complete and
 * a subject simply omits the segments it has no files for. Regenerate if that tuple grows.
 */
#libsub-cat-papers:checked ~ .libsub-panel[data-cat="papers"],
#libsub-cat-handouts:checked ~ .libsub-panel[data-cat="handouts"],
#libsub-cat-exercises:checked ~ .libsub-panel[data-cat="exercises"],
#libsub-cat-past_papers:checked ~ .libsub-panel[data-cat="past_papers"],
#libsub-cat-slides:checked ~ .libsub-panel[data-cat="slides"],
#libsub-cat-vocab:checked ~ .libsub-panel[data-cat="vocab"],
#libsub-cat-revision:checked ~ .libsub-panel[data-cat="revision"],
#libsub-cat-holiday:checked ~ .libsub-panel[data-cat="holiday"],
#libsub-cat-holiday_past_papers:checked ~ .libsub-panel[data-cat="holiday_past_papers"],
#libsub-cat-syllabus:checked ~ .libsub-panel[data-cat="syllabus"],
#libsub-cat-other:checked ~ .libsub-panel[data-cat="other"],
#libsub-cat-misc:checked ~ .libsub-panel[data-cat="misc"] { display: block; }

/* Selected tab state is driven by an `is-selected` CLASS, not by the radio's :checked
 * state, and that is deliberate.
 *
 * ⚠️ Both CSS-only forms were built and VERIFIED BROKEN IN CHROME first:
 *   `#id:checked ~ .wrap .tabs .tab[data-cat=…]`  and
 *   `.libsub-downloads:has(#id:checked) .tab[data-cat=…]`
 * Both are correct selectors — `el.matches(sel)` returns true and the rule is present in
 * the loaded sheet with the right declarations — but after the radio changes, Chrome does
 * not recompute the tab's style: getComputedStyle still reports the unselected colour and
 * the pixels confirm it. (An injected <style> "fixes" it, because appending a stylesheet
 * forces a full recalc — which is exactly why a test rule appeared to work.)
 *
 * The PANELS still switch with pure CSS (`:checked ~ .libsub-panel`, sibling-only, which
 * Chrome does invalidate), so with JS off the picker still works: the first category shows
 * and clicking another reveals it. Only the highlight stops following, which is cosmetic.
 */
.libsub-tab.is-selected {
  border-color: rgba(var(--accent-rgb), 0.65);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.26) 0%, rgba(var(--accent-rgb), 0.1) 70%);
  color: #fff;
  box-shadow: 0 8px 24px -14px rgba(var(--accent-rgb), 1);
}
.libsub-tab.is-selected .libsub-tab-icon { background: rgba(var(--accent-rgb), 0.32); }
.libsub-tab.is-selected .libsub-tab-count {
  background: rgba(var(--accent-rgb), 0.28);
  color: #fff;
}

/* The radio is visually hidden, so its focus ring has to be drawn on the label instead —
 * otherwise keyboard users tabbing through the picker see no indication at all. This one
 * IS reliable: :focus-visible invalidation does not cross a sibling+descendant chain the
 * same way, and it is re-evaluated on every focus change. */
#libsub-cat-papers:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="papers"],
#libsub-cat-handouts:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="handouts"],
#libsub-cat-exercises:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="exercises"],
#libsub-cat-past_papers:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="past_papers"],
#libsub-cat-slides:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="slides"],
#libsub-cat-vocab:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="vocab"],
#libsub-cat-revision:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="revision"],
#libsub-cat-holiday:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="holiday"],
#libsub-cat-holiday_past_papers:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="holiday_past_papers"],
#libsub-cat-syllabus:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="syllabus"],
#libsub-cat-other:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="other"],
#libsub-cat-misc:focus-visible ~ .libsub-tabs-wrap .libsub-tabs .libsub-tab[data-cat="misc"] {
  outline: 2px solid rgb(56, 189, 248);
  outline-offset: 2px;
}

/* ─── Essentials ───────────────────────────────────────────────────────────────── */
/* The single-file categories (syllabus, revision sheet) as direct chips — a tab that
 * reveals one file is a pointless extra click. Reuses the file-chip look, auto width. */
.libsub-essentials {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0 0 0.875rem;
  padding: 0;
  list-style: none;
}
.libsub-essentials li { display: contents; }
.libsub-essentials .libsub-file { flex: 0 0 auto; }

/* ─── Paired paper rows ────────────────────────────────────────────────────────── */
/* One row per paper: "Paper 12" + Questions / Answers / Insert buttons, each in its
 * kind's colour. Halves the session list and puts a paper's mark scheme where a student
 * looks for it — next to the paper.
 *
 * Multi-column, NOT a grid — the same reason the topic list uses columns. A grid fills
 * across (11, 12 / 13, 14), so a 23-paper session read in zig-zag; columns fill DOWN, so
 * the paper numbers run in order and you can scan one column for the paper you want. */
/* `columns: <min-width> <max-count>` rather than fixed counts at breakpoints: the browser
 * makes as many columns as fit with each at least 20rem, capped at 3. A fixed count is
 * what breaks here — at a narrow column the Questions/Answers/Insert buttons wrap under
 * the label and every tile doubles in height. A min-width can't produce that. */
.libsub-papers {
  columns: 20rem 3;
  column-gap: 0.5rem;
  margin-bottom: 0.375rem;
}
.libsub-papers > li {
  break-inside: avoid;
  -webkit-column-break-inside: avoid;
  margin-bottom: 0.375rem;
}
/* A paper is the unit people came for, so its tile carries real weight: a full-width
 * card with its own border, a lifted surface and a hover state, rather than the hairline
 * outline it had — which read as a list row rather than as something to act on. */
.libsub-paper {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.625rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.055) 0%, rgba(255, 255, 255, 0.022) 100%);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04) inset;
  transition: border-color 0.18s ease, background-color 0.18s ease;
}
.libsub-paper:hover {
  border-color: rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.075);
}
.libsub-paper-label {
  flex: none;
  font-size: 0.875rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: rgb(226, 232, 240);
}
.libsub-paper-links {
  display: flex;
  flex-wrap: nowrap;
  justify-content: flex-end;
  gap: 0.375rem;
  margin-left: auto;
}
.libsub-paper-link {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.25rem 0.6875rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(var(--accent-rgb), 0.3);
  background: rgba(var(--accent-rgb), 0.08);
  color: rgb(var(--accent-rgb));
  transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}
.libsub-paper-link:hover {
  background: rgba(var(--accent-rgb), 0.22);
  border-color: rgba(var(--accent-rgb), 0.55);
  color: #fff;
}

/* ─── Files ────────────────────────────────────────────────────────────────────── */
.libsub-year { border-top: 1px solid rgba(255, 255, 255, 0.06); }
.libsub-year:first-of-type { border-top: 0; }

.libsub-year-summary {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  padding: 0.75rem 0.25rem;
  list-style: none;
  user-select: none;
}
.libsub-year-summary::-webkit-details-marker { display: none; }
.libsub-year-label { font-weight: 600; color: #fff; font-size: 0.9375rem; }
.libsub-year-count { font-size: 0.75rem; color: rgb(100, 116, 139); }
.libsub-chevron {
  width: 1rem; height: 1rem;
  margin-left: auto;
  color: rgb(100, 116, 139);
  transition: transform 0.18s ease;
}
details[open] > summary .libsub-chevron { transform: rotate(90deg); }

/* Column-major GRID, not a row-major one and not CSS multi-column.
 * `grid-auto-flow: column` + an explicit row count makes items run DOWN each column, and
 * because grid rows are shared across columns every column starts and steps at exactly the
 * same height. Multi-column gives the downward order but cannot guarantee that alignment.
 * `--rows-N` comes from the template (CSS cannot do ceil(items / columns)). */
.libsub-files {
  display: grid;
  gap: 0.375rem;
  grid-auto-flow: column;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(var(--rows-1, 1), auto);
  padding: 0 0 0.75rem;
}
@media (min-width: 480px) {
  .libsub-files {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows-2, 1), auto);
  }
}
@media (min-width: 820px) {
  .libsub-files {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows-3, 1), auto);
  }
}
@media (min-width: 1100px) {
  .libsub-files {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows-4, 1), auto);
  }
}
.libsub-files--flat { padding-top: 0.5rem; }

.libsub-file {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4375rem 0.625rem;
  border-radius: 0.625rem;
  border: 1px solid rgba(255, 255, 255, 0.05);
  background: rgba(255, 255, 255, 0.02);
  color: rgb(203, 213, 225);
  font-size: 0.8125rem;
  transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}
.libsub-file:hover {
  border-color: rgba(var(--accent-rgb), 0.45);
  background: rgba(var(--accent-rgb), 0.07);
  color: #fff;
}
/* The kind colour is the fastest way to tell a question paper from a mark scheme in a
 * 55-item session — the same hue the Library uses for that kind. */
.libsub-file-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem; height: 1.5rem;
  flex: none;
  border-radius: 0.5rem;
  background: rgba(var(--accent-rgb), 0.14);
  color: rgb(var(--accent-rgb));
}
.libsub-file-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.libsub-session {
  margin: 0.75rem 0 0.375rem;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: rgb(100, 116, 139);
}

/* ─── Topic list ──────────────────────────────────────────────────────────────── */
/* Column-major grid: items run DOWN each column (topic 1 above topic 2, as the syllabus
 * reads), and grid rows are shared so all three columns start and step at the same height.
 * Row counts are passed in as --rows-N by the template. */
.libsub-topics {
  display: grid;
  gap: 0.5rem;
  grid-auto-flow: column;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(var(--rows-1, 1), auto);
}
@media (min-width: 640px) {
  .libsub-topics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows-2, 1), auto);
  }
}
@media (min-width: 1024px) {
  .libsub-topics {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows-3, 1), auto);
  }
}
.libsub-topics > li { min-width: 0; }

/* ─── Topic chips ──────────────────────────────────────────────────────────────── */
/* Each carries the same icon and hue as its /know page, so a topic looks like itself
 * everywhere. Without them this was 25 identical grey rows. */
.libsub-topic {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.6875rem;
  height: 100%;
  padding: 0.75rem 0.875rem;
  border-radius: 1rem;
  border: 1px solid rgba(var(--accent-rgb), 0.16);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.09) 0%, rgba(255, 255, 255, 0.02) 62%);
  overflow: hidden;
  transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}
/* A hairline of the topic's own hue down the leading edge — enough colour to tell topics
 * apart at a glance without 25 saturated blocks fighting each other. */
.libsub-topic::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: rgb(var(--accent-rgb));
  opacity: 0.55;
  transition: opacity 0.18s ease;
}
.libsub-topic:hover {
  border-color: rgba(var(--accent-rgb), 0.5);
  transform: translateY(-2px);
  box-shadow: 0 10px 26px -18px rgba(var(--accent-rgb), 0.9);
}
.libsub-topic:hover::before { opacity: 1; }
.libsub-topic-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem; height: 2rem;
  flex: none;
  border-radius: 0.5rem;
  background: rgba(var(--accent-rgb), 0.15);
  color: rgb(var(--accent-rgb));
}
.libsub-topic-num { font-size: 0.6875rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.libsub-topic-no {
  margin-right: 0.4375rem;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  color: rgb(100, 116, 139);
}
.libsub-topic-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
  font-weight: 500;
  color: rgb(226, 232, 240);
}
.libsub-topic:hover .libsub-topic-title { color: #fff; }

/* ─── Tip cards ─────────────────────────────────────────────────────────────────── */
/* Was a <details> around ~200 words of unbroken prose. Same sentences, one card each. */
.libsub-tips {
  display: grid;
  gap: 0.625rem;
  grid-template-columns: 1fr;
}
@media (min-width: 900px) { .libsub-tips { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.libsub-tip {
  display: flex;
  gap: 0.75rem;
  padding: 0.9375rem 1rem;
  border-radius: 1rem;
  border: 1px solid rgba(var(--accent-rgb), 0.16);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.07) 0%, rgba(255, 255, 255, 0.015) 60%);
}
.libsub-tip-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.875rem; height: 1.875rem;
  flex: none;
  border-radius: 0.625rem;
  background: rgba(var(--accent-rgb), 0.16);
  color: rgb(var(--accent-rgb));
}
.libsub-tip p {
  font-size: 0.8125rem;
  line-height: 1.65;
  color: rgb(203, 213, 225);
}

/* ─── How-to fold ──────────────────────────────────────────────────────────────── */
/* Collapsed and last. In the DOM (so it is indexed and reachable), out of the way. */
.libsub-guide {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.02);
}
.libsub-guide-summary {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  cursor: pointer;
  padding: 0.8125rem 1.125rem;
  list-style: none;
  user-select: none;
  font-size: 0.875rem;
  font-weight: 600;
  color: rgb(203, 213, 225);
}
.libsub-guide-summary::-webkit-details-marker { display: none; }
.libsub-guide-summary:hover { color: #fff; }
.libsub-guide-icon { width: 1.125rem; height: 1.125rem; flex: none; color: rgb(56, 189, 248); }
.libsub-guide-body { padding: 0 1.125rem 1.125rem; }
.libsub-guide-body p {
  max-width: 68ch;
  font-size: 0.8125rem;
  line-height: 1.7;
  color: rgb(148, 163, 184);
}
.libsub-guide-body p + p { margin-top: 0.6875rem; }

@media (prefers-reduced-motion: reduce) {
  .libsub-action, .libsub-action-icon, .libsub-tab, .libsub-file, .libsub-topic,
  .libsub-paper-link, .libsub-chevron { transition: none; }
  .libsub-action:hover, .libsub-topic:hover { transform: none; }
}
