html {
  --portal-initial-header-height: 120px;
  --portal-header-offset: var(--bvi-panel-offset, 0px);
  --portal-sticky-edge: calc(var(--header-height, var(--portal-initial-header-height)) + var(--portal-header-offset));
  scrollbar-gutter: stable;
}

/* The BVI panel owns its own fixed offset. The header stays below it when active. */
html, html.bvi-active {
  scroll-padding-block-start: calc(var(--portal-sticky-edge) + 24px);
}

.site-header {
  position: sticky;
  inset-block-start: var(--portal-header-offset);
}

.skip-link:focus {
  inset-block-start: calc(var(--portal-header-offset) + 12px);
}

.site-header .mega-panel {
  max-block-size: max(0px, calc(100dvh - var(--portal-sticky-edge) - 8px));
}

:is(.publication-rail, .publication-toc, .materials-audience-rail) {
  inset-block-start: calc(var(--portal-sticky-edge) + 24px);
}

.publication-toc nav {
  max-block-size: min(60vh, 40rem, max(0px, calc(100dvh - var(--portal-sticky-edge) - 96px)));
}

@media (width <= 1050px) {
  html { --portal-initial-header-height: 116px; }

  /* The unenhanced navigation can occupy several rows. Keep it in normal flow. */
  html:not([data-nav-enhancing]):not(:has(.site-header[data-responsive-nav-ready])) {
    --portal-sticky-edge: var(--portal-header-offset);
  }
  html:not([data-nav-enhancing]) .site-header:not([data-responsive-nav-ready]) {
    position: relative;
    inset-block-start: auto;
  }
}

@media (width <= 450px) {
  html { --portal-initial-header-height: 114px; }
}

/* Keep a usable reading viewport at combined high zoom and BVI text enlargement. */
@media (height < 32rem) {
  html.bvi-active { --portal-sticky-edge: var(--portal-header-offset); }
  html.bvi-active .site-header { position: relative; inset-block-start: auto; }
}
