/* ==========================================================================
   Sticky table header — the copy fn-sticky-head.js builds
   --------------------------------------------------------------------------
   A table that opts in (data-fn-sticky-head) gets a copy of its header rows
   (.fn-sticky-head, aria-hidden) in front of its scroller. The copy sticks
   to the window, follows the sideways swipe and forwards clicks to the real
   header; the contract is at the top of fn-sticky-head.js.

   Where it sticks is the house rule for readable content, --fn-chrome-line
   in 01-tokens.css (the anchor padding and fn-cat-jump.js use its parts):
   below the floating home/top tools plus one breath — 12 + 40 + 8 = 60px
   on desktop, the pinned bar + 8 + 40 + 8 on a phone — or below a
   page-owned sticky bar (--fn-sticky-stack, set by the component that owns
   it) plus one breath, whichever is lower. One breath under each edge.

   The cap (::before) fills the strip between the chrome and the stuck
   header with the table surface, so rows never show above the header. It
   is as tall as that strip, but never taller than the part of the table
   above the copy (--fn-sticky-head-room, measured by the script), so it
   covers rows only — never the toolbar or title above the table.
   ========================================================================== */

.fn-ui .fn-sticky-head {
  position: -webkit-sticky;
  position: sticky;
  top: max(
    var(--fn-chrome-line),
    calc(var(--fn-chrome-bar) + var(--fn-sticky-stack, 0px) + var(--fn-chrome-breath))
  );
  z-index: var(--fn-z-sticky-head);
  width: 100%;
}

/* Only once the real header is under the stick line (the script adds
   .is-stuck). At rest the real header is seen, hovered and focused. */
.fn-ui .fn-sticky-head:not(.is-stuck) {
  visibility: hidden;
}

/* Strip height = stick line − (bar + page-owned bar): the larger of
   --fn-chrome-line − bar − stack and one breath. */
.fn-ui .fn-sticky-head::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100%;
  height: max(0px, min(
    max(
      calc(var(--fn-chrome-line) - var(--fn-chrome-bar) - var(--fn-sticky-stack, 0px)),
      var(--fn-chrome-breath)
    ),
    var(--fn-sticky-head-room, 0px)
  ));
  background: var(--fn-sticky-head-bg);
}

/* Inside a scrolling pane (the script adds --pane) the header sticks to
   the pane's own top edge, under a sticky bar the pane may pin
   (--fn-sticky-stack). The site chrome is outside the pane, so the cap
   only covers that bar's strip. */
.fn-ui .fn-sticky-head--pane {
  top: var(--fn-sticky-stack, 0px);
}

.fn-ui .fn-sticky-head--pane:not(.is-clamped)::before {
  height: max(0px, min(var(--fn-sticky-stack, 0px), var(--fn-sticky-head-room, 0px)));
}

/* A pane scrolled partly off screen: its top edge is above the window's
   stick line, so the script holds the copy on the window line instead
   (.is-clamped). The pane's own bar is off screen with its top edge, so
   the cap is the window's full strip (line − bar), still limited to
   the rows above the copy. */
.fn-ui .fn-sticky-head--pane.is-clamped::before {
  height: max(0px, min(
    calc(var(--fn-chrome-line) - var(--fn-chrome-bar)),
    var(--fn-sticky-head-room, 0px)
  ));
}

/* The window's stick line as a measurable box. The script reads its
   resolved top to clamp a pane's header (CSS top in a pane is relative to
   the pane, so the window line cannot be read off the copy itself). It is
   never seen and never hit. Not scoped to .fn-ui: it only reads tokens. */
.fn-sticky-head__line {
  position: fixed;
  top: var(--fn-chrome-line);
  left: 0;
  width: 0;
  height: 0;
  visibility: hidden;
  pointer-events: none;
}

/* The sideways scroller. It is its own box (not the sticky one) because a
   scroller would clip the cap above it. Direction comes from the real
   scroller (set by the script) so scrollLeft means the same in both. */
.fn-ui .fn-sticky-head__scroll {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  background: var(--fn-sticky-head-bg);
}

.fn-ui .fn-sticky-head__scroll::-webkit-scrollbar {
  display: none;
}

/* The real header may stick inside its own box; in the copy that would
   fight the copy's own stickiness. The script also sets it inline. */
.fn-ui .fn-sticky-head thead :is(th, td) {
  top: auto;
}

/* High-contrast modes. The surfaces are system colours under forced
   colours (Canvas is opaque, so the cap still hides the rows), and the
   accessibility bridge in 01-tokens.css already points
   --fn-sticky-head-bg at its pure black / white for .darkCont/.brightCont. */
@media (forced-colors: active) {
  .fn-ui .fn-sticky-head::before,
  .fn-ui .fn-sticky-head__scroll {
    background: Canvas;
  }
}

/* The real header prints with its table; the copy would print it twice. */
@media print {
  .fn-ui .fn-sticky-head {
    display: none !important;
  }
}
