/* /library — category sidebar + grouped sections.
 *
 * Page-scoped plain CSS (linked only from library.html), so no Tailwind rebuild — the same
 * arrangement as 33-library-subject.css. Everything new on the page wears a lib-cat-* class.
 *
 * The problem this solves: one subject renders 30–38 category blocks (~1,000 rows for an
 * A-Level) as a single vertical stack with nothing to jump by. The blocks are now grouped into
 * SECTIONS (library_sections() in web/routes/site.py — the same keys, order, labels and hues as
 * the tabs on /library/<subject>) and a sticky sidebar lists them; on phones the same element
 * is a sticky horizontal strip under the site header. Category hues appear ONLY on the small
 * icon chip, the count pill and the active state — never as a wash — so a page whose picker
 * was just brought onto one cool ramp does not grow a rainbow somewhere else.
 */

/* ── Width ─────────────────────────────────────────────────────────────────────────────
   The grid view borrows base.html's main_class hook (as the video watch page does) so the
   cards keep their 3–4 columns beside a 15rem sidebar. Gated on the GRID view being shown:
   the PDF detail view keeps the 64rem column its fit-to-width sizing was tuned for. */
#site-main.lib-wide:has(#lib-grid-view:not(.hidden)) { max-width: 86rem; }

/* ── Layout ─────────────────────────────────────────────────────────────────────────── */
.lib-layout { display: grid; grid-template-columns: minmax(0, 1fr); }
.lib-grids { min-width: 0; }
@media (min-width: 1024px) {
  /* :has(): with the nav hidden (no JS, or a subject with no sections) the column must go
     too, or the grid sits beside 15rem of nothing. */
  .lib-layout:not(:has(> .lib-cat-nav[hidden])) {
    grid-template-columns: 15rem minmax(0, 1fr);
    gap: 1.5rem;
    align-items: start;
  }
}

/* ── Sections in the grid ──────────────────────────────────────────────────────────── */
/* display sits on :not(.hidden) so the search can still collapse an emptied section with
   Tailwind's .hidden (the documented .hidden-vs-component-rule trap, 02-auth-gate.css). */
.lib-cat-section:not(.hidden) { display: flex; flex-direction: column; gap: 1.5rem; }
.lib-cat-section + .lib-cat-section { margin-top: 1rem; }          /* 2.5rem with the grid's gap-6 */
/* Clearance for the sticky site header when a nav entry (or a #cat- link) lands here. */
.lib-cat-section, .lib-year-group[data-block] {
  scroll-margin-top: calc(var(--preview-sticky-top) + 0.75rem);
}

.lib-cat-heading { display: flex; align-items: center; gap: 0.625rem; min-width: 0; }
.lib-cat-heading-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.875rem; height: 1.875rem;
  flex: none;
  border-radius: 0.5625rem;
  background: rgba(var(--accent-rgb), 0.16);
  color: rgb(var(--accent-rgb));
  box-shadow: 0 0 18px -8px rgba(var(--accent-rgb), 0.7);
}
.lib-cat-heading-icon svg { width: 1rem; height: 1rem; }
.lib-cat-heading-label {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
  color: #fff;
  white-space: nowrap;
}
.lib-cat-heading-count {
  flex: none;
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  padding: 0.0625rem 0.4375rem;
  border-radius: 999px;
  background: rgba(var(--accent-rgb), 0.14);
  color: rgb(var(--accent-rgb));
}
.lib-cat-heading-rule {
  flex: 1;
  height: 1px;
  background: linear-gradient(to right, rgba(var(--accent-rgb), 0.35), transparent);
}

/* ── The nav: shared item grammar ──────────────────────────────────────────────────── */
.lib-cat-nav[hidden] { display: none; }
.lib-cat-nav { font-size: 0.8125rem; }
.lib-cat-nav-list, .lib-cat-nav-sub { list-style: none; margin: 0; padding: 0; }
.lib-cat-nav-list { display: flex; flex-direction: column; gap: 0.125rem; }
.lib-cat-nav-item { min-width: 0; }

/* A section entry: hue-tinted icon chip, neutral label, count pill — the .libsub-tab look
   from /library/<subject>, so the two pages share one visual grammar. */
.lib-cat-nav-section {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.5rem;
  border-radius: 0.625rem;
  border: 1px solid transparent;
  color: rgb(203, 213, 225);
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}
.lib-cat-nav-section:hover { background: rgba(255, 255, 255, 0.05); color: #fff; }
.lib-cat-nav-section:focus-visible { outline: 2px solid rgba(56, 189, 248, 0.7); outline-offset: -2px; }
.lib-cat-nav-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem; height: 1.5rem;
  flex: none;
  border-radius: 0.4375rem;
  background: rgba(var(--accent-rgb), 0.16);
  color: rgb(var(--accent-rgb));
  transition: background-color 0.15s ease, box-shadow 0.15s ease;
}
.lib-cat-nav-icon svg { width: 0.875rem; height: 0.875rem; }
.lib-cat-nav-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.lib-cat-nav-count {
  flex: none;
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  padding: 0 0.375rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  color: rgba(148, 163, 184, 0.9);
}
/* The current section: the hue arrives as a soft, low-opacity bloom (the site's glow
   language), never a solid fill. */
.lib-cat-nav-item.is-active > .lib-cat-nav-section {
  border-color: rgba(var(--accent-rgb), 0.35);
  background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.18) 0%, rgba(var(--accent-rgb), 0.05) 70%);
  color: #fff;
  box-shadow: 0 8px 24px -14px rgba(var(--accent-rgb), 0.9);
}
.lib-cat-nav-item.is-active .lib-cat-nav-icon {
  background: rgba(var(--accent-rgb), 0.3);
  box-shadow: 0 0 14px -4px rgba(var(--accent-rgb), 0.7);
}
.lib-cat-nav-item.is-active .lib-cat-nav-count { background: rgba(var(--accent-rgb), 0.22); color: #fff; }

/* Block entries, nested under their section behind a hairline. */
.lib-cat-nav-sub {
  margin: 0.125rem 0 0.375rem 1.25rem;
  padding-left: 0.625rem;
  border-left: 1px solid rgba(255, 255, 255, 0.07);
}
.lib-cat-nav-block {
  display: block;
  padding: 0.2rem 0.5rem;
  border-radius: 0.4rem;
  color: rgb(148, 163, 184);
  font-size: 0.75rem;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-decoration: none;
  box-shadow: inset 2px 0 0 transparent;
  transition: background-color 0.12s ease, color 0.12s ease, box-shadow 0.12s ease;
}
.lib-cat-nav-block:hover { color: rgb(226, 232, 240); background: rgba(255, 255, 255, 0.04); }
.lib-cat-nav-block:focus-visible { outline: 2px solid rgba(56, 189, 248, 0.7); outline-offset: -2px; }
.lib-cat-nav-block.is-active {
  color: #fff;
  background: rgba(var(--accent-rgb), 0.1);
  box-shadow: inset 2px 0 0 rgba(var(--accent-rgb), 0.75);
}

/* ── Desktop: sticky sidebar in its own scroll box ─────────────────────────────────── */
@media (min-width: 1024px) {
  .lib-cat-nav:not([hidden]) {
    position: sticky;
    top: calc(var(--preview-sticky-top) + 0.5rem);
    margin-top: 2rem;                                   /* aligns with the grid's mt-8 */
    max-height: calc(100vh - var(--preview-sticky-top) - 1.5rem);
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0.375rem;
    border-radius: 1rem;
    border: 1px solid rgba(255, 255, 255, 0.07);
    background: rgba(8, 14, 30, 0.55);
    scrollbar-gutter: stable;
    scrollbar-width: thin;
    scrollbar-color: rgba(56, 189, 248, 0.22) transparent;
  }
  .lib-cat-nav::-webkit-scrollbar { width: 8px; }
  .lib-cat-nav::-webkit-scrollbar-track { background: transparent; }
  .lib-cat-nav::-webkit-scrollbar-thumb {
    background: rgba(56, 189, 248, 0.22);
    border-radius: 4px;
    border: 2px solid transparent;
    background-clip: padding-box;
  }
}

/* ── Phones / tablets: a sticky chip strip under the site header ───────────────────── */
@media (max-width: 1023px) {
  .lib-cat-nav:not([hidden]) {
    position: sticky;
    top: var(--preview-sticky-top);
    z-index: 15;                                        /* under .site-header (z-20), over the cards */
    margin-top: 1.5rem;
    padding: 0.5rem 0;
    background: rgba(1, 4, 14, 0.82);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
  }
  .lib-cat-nav::-webkit-scrollbar { display: none; }
  .lib-cat-nav-list { flex-direction: row; gap: 0.375rem; width: max-content; padding: 0 0.125rem; }
  .lib-cat-nav-sub { display: none; }
  .lib-cat-nav-section {
    padding: 0.3rem 0.625rem 0.3rem 0.3rem;
    border-radius: 999px;
    border-color: rgba(var(--accent-rgb), 0.2);
    background: linear-gradient(135deg, rgba(var(--accent-rgb), 0.1) 0%, rgba(255, 255, 255, 0.02) 65%);
    white-space: nowrap;
  }
  .lib-cat-nav-label { overflow: visible; }
  /* The strip sits under the header's fixed corner widgets, so a jump has to clear both. */
  .lib-cat-section, .lib-year-group[data-block] {
    scroll-margin-top: calc(var(--preview-sticky-top) + 3.75rem);
  }
}
/* Phones: nothing is pinned at the top. .site-header is position: relative and scrolls
   away, and below 768px the brand wordmark and the account chip are `absolute`, i.e. they
   scroll away with it (07-header-preview-chrome.css, 13-account.css). A strip parked at
   --preview-sticky-top would leave a 72px band of page content scrolling ABOVE it, so here
   it sticks to the very top and a jump only has to clear the strip itself. */
@media (max-width: 767px) {
  .lib-cat-nav:not([hidden]) { top: 0; }
  .lib-cat-section, .lib-year-group[data-block] { scroll-margin-top: 3.75rem; }
}

@media (prefers-reduced-motion: reduce) {
  .lib-cat-nav-section, .lib-cat-nav-icon, .lib-cat-nav-block { transition: none; }
}
