/* ============================================================
   HEADINGS TABLE OF CONTENT MENU
   Fixed right-rail showing section numbers with scroll-spy.
   Part of base page; excluded per-page via tocEnabled: false.
   Hidden on print via .screen-only (print.css).

   Hero section is rendered as the first entry with a star glyph.
   On pages without entries, the desktop rail hides itself; mobile
   shows the scroll-to-top/bottom buttons directly (no active item).
   ============================================================ */

.toc {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 100;

  /* Master scale knob: each level up is (1 + step) × the smaller level.
     Bars and circles both read this; JS injects exact sizes on each element. */
  --toc-scale-step: .25;
}

.toc-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.toc-scroll-btns {
  display: none;
}

/* Desktop: hide the rail entirely when there are no entries
   (mobile dot is still useful for scroll buttons, so it's kept). */
@media (min-width: 769px) {
  .toc.toc--empty {
    display: none;
  }
}

/* Hero entry in the TOC scrolls to top — hide the standalone corner
   ↑ button on every viewport (the in-panel ↑ is removed by JS). */
.has-hero-toc .scroll-btn--top {
  display: none !important;
}

/* ---- Section list ---- */

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.toc-list:empty {
  display: none;
}

.toc-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

/* Section number pill */
.toc-num {
  display: flex;
  align-items: center;
  justify-content: center;
  /* Diameter set inline by JS from --toc-scale-step. */
  border-radius: 50%;
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transform-origin: center top;
  transition:
    color 0.2s ease,
    border-color 0.2s ease,
    background 0.2s ease,
    box-shadow 0.2s ease,
    transform 0.2s ease,
    margin 0.2s ease,
    opacity 0.25s ease;
}

.toc-num:hover {
  color: var(--primary);
  border-color: var(--primary-glow);
  background: var(--bg-card-hover);
}

.toc-num.is-active {
  color: var(--primary);
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-glow);
  pointer-events: none;
  cursor: default;
  transform: scale(1.27);
  /* Top-anchored scale grows 27% downward in addition to widening symmetrically.
     Mirror the downward extension into layout so the next sibling moves down. */
  margin-bottom: calc(var(--rest-h, 0px) * 0.27);
}

.toc-glyph-star {
  width: 14px;
  height: 14px;
  display: block;
}

/* ---- Subheading wireframe bars ---- */

.toc-sub-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.toc-bar {
  display: block;
  /* h6 = 14px hit-friendly base; each higher level grows by 4/3 (= 1/0.75).
     JS injects the exact per-bar width inline; static fallback if not set. */
  width: 20px;
  height: 7px;
  background: var(--border-subtle);
  border-radius: 2px;
  text-decoration: none;
  /* Right edge stays fixed so visual grows leftward toward page content
     (per spec). Top edge stays fixed so margin-bottom claim pushes siblings. */
  transform-origin: right top;
  transition: background 0.2s ease, box-shadow 0.15s ease, transform 0.2s ease, margin 0.2s ease;
}

.toc-bar:hover {
  background: var(--primary);
  box-shadow: 0 0 4px var(--primary-glow);
}

/* Bars grow more horizontally than vertically when active — the wider X
   scale stretches the rest 1.5:1 bar toward a heading-like aspect ratio,
   while the Y scale matches the pill's 27% so the margin-bottom layout
   push (0.27 × rest-h) stays in sync. */
.toc-bar.is-active {
  background: var(--primary);
  box-shadow: 0 0 6px var(--primary-glow);
  pointer-events: none;
  cursor: default;
  transform: scale(2.5, 1.27);
  margin-bottom: calc(var(--rest-h, 0px) * 0.27);
}

/* ============================================================
   MOBILE (≤768px)
   Collapses to a floating dot; expands to a card panel with
   section numbers, subheading bars, and merged scroll buttons.
   The dot reflects current scroll position: section number,
   star glyph for hero, or a wireframe bar for an active sub.
   ============================================================ */

@media (max-width: 768px) {
  .toc {
    right: 14px;
  }

  /* Panel: always-present in layout, no chrome, no overflow handling
     (overflow:auto would clip active-state scale extensions). The panel's
     full height is always taken in layout; collapse is opacity-only so
     the active item never changes position between states. */
  .toc-panel {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
  }

  /* Items fade in/out; staggered by distance from active. Sibling scale gives
     a subtle radiate-out effect without affecting layout (active stays put). */
  .toc-item,
  .toc-sub-item {
    transition:
      opacity   0.25s ease calc(var(--toc-stagger, 0) * 25ms),
      transform 0.25s ease calc(var(--toc-stagger, 0) * 25ms);
    transform-origin: right center;
  }

  /* Collapsed: hide any item whose subtree contains no active element. */
  .toc:not(.is-open) .toc-item:not(:has(.is-active)),
  .toc:not(.is-open) .toc-sub-item:not(:has(.is-active)) {
    opacity: 0;
    transform: scale(0.6);
    pointer-events: none;
  }

  /* When the active element is a sub-bar, also fade the parent section pill
     so only the wireframe shows as the floating display. */
  .toc:not(.is-open) .toc-item:has(.toc-bar.is-active) > .toc-num {
    opacity: 0;
    pointer-events: none;
  }

  /* Collapsed nav passes clicks through entirely — only the explicitly-
     opted-in active item below catches taps. (Setting this on .toc-panel
     alone isn't enough — the nav element itself still catches clicks on
     its hit-area outside the panel children, including the active pill's
     scale(1.27) visual extension and any sub-pixel gaps.) The capture-
     phase nav click handler still fires on bubble through, since event
     propagation runs regardless of an element's own pointer-events. */
  .toc:not(.is-open) {
    pointer-events: none;
  }

  /* Active item is the floating display — restore tap target to open menu. */
  .toc:not(.is-open) .toc-num.is-active,
  .toc:not(.is-open) .toc-bar.is-active {
    pointer-events: auto;
    cursor: pointer;
  }

  /* Scroll buttons fade with menu (skip empty TOC — buttons are the only affordance). */
  .toc-scroll-btns {
    transition: opacity 0.25s ease;
  }
  .toc:not(.is-open):not(.toc--empty) .toc-scroll-btns {
    opacity: 0;
    pointer-events: none;
  }

  /* Empty TOC: scroll buttons are the floating display, always visible. */
  .toc--empty .toc-scroll-btns {
    opacity: 1;
    pointer-events: auto;
  }

  /* Merged scroll buttons — below the TOC list in the panel */
  .toc-scroll-btns {
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 8px;
    padding-top: 8px;
    margin-top: 2px;
    border-top: 1px solid var(--border-subtle);
    width: 100%;
  }

  /* Empty rail → no list above the scroll buttons, drop the divider */
  .toc--empty .toc-scroll-btns {
    border-top: none;
    padding-top: 0;
    margin-top: 0;
  }

  .toc-scroll-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--border-subtle);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease;
  }

  .toc-scroll-btn svg {
    width: 16px;
    height: 16px;
  }

  .toc-scroll-btn:hover {
    color: var(--primary);
    border-color: var(--primary-glow);
    background: var(--bg-card-hover);
  }

  /* Standalone scroll-buttons are replaced by the merged ones in the panel
     whenever the TOC component is loaded (with or without entries). */
  .has-toc .scroll-btn {
    display: none !important;
  }
}

/* Print: belt-and-braces alongside .screen-only from print.css */
@media print {
  .toc {
    display: none !important;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {

  .toc-num,
  .toc-bar {
    transition: none;
  }

  .toc-item,
  .toc-sub-item,
  .toc-scroll-btns {
    transition: none;
  }
}