/*! hl-grid.css v0.5.0 · © 2026 Heathen Labs. All rights reserved. Licensed to the client for use and modification within the client's own sites; see LICENSE-HL.md. */
/* hl-grid.css: the Heathen Labs adaptive layout grid, v0.5.0.
   No dependencies, no JS. Strict-CSP-safe: load it as ONE same-origin stylesheet (style-src 'self').
   Docs: PRINCIPLES.md (why), TOKENS.md (the knobs), PRIMITIVES.md (the parts and the markup contract).

   HOW A SITE USES IT
   - Link this file first, then hl-foundations.css and the theme (optional skin layer, v0.5), then the site's own
     stylesheet. Site CSS is left UNLAYERED (or put in a later layer), so it
     always beats these rules without specificity fights.
   - Change values, not rules: a site sets the --hl-* custom properties (on :root or on any element) to retune things.
   - Never set them with a style="" attribute: the CSP blocks it. A script may use el.style.setProperty('--hl-…') if a
     site ever has approved JS.

   THE FIVE CONTAINER SIZES (identical in every site; a container condition can't use a custom property):
     sm >= 30rem (480px) · md >= 40rem (640px) · lg >= 60rem (960px) · xl >= 80rem (1280px)   (below sm = xs)
   Media queries are used ONLY for the viewport itself: the overlay panel, the wide-screen frame cap, print and user
   preferences. Everything inside the frame adapts to its CONTAINER, not to the window. */

/* The full layer order of the hl- files (v0.5). hl-foundations.css declares the same order, so either file can load
   first. A theme file uses hl.theme: it comes after hl.tokens, so a theme may override ANY token here, layout ones
   included (frame, gutter, measure, target). Site CSS stays unlayered and beats them all. */
@layer hl.reset, hl.tokens, hl.theme, hl.base, hl.layout, hl.panel, hl.components, hl.enhance;

/* ── reset: only what layout needs ─────────────────────────────────────────────────────────── */
@layer hl.reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { scrollbar-gutter: stable; }               /* the page doesn't shift when a scrollbar appears */
  .hl-frame { overflow-wrap: anywhere; }          /* long ids, shas and URLs wrap instead of widening the page */
  .hl-frame img, .hl-frame svg, .hl-frame video { max-inline-size: 100%; block-size: auto; }
  /* v0.5: a list used AS a row or cluster is layout, not a bulleted list: no bullets, no indent, no margin.
     (Safari drops list semantics with list-style: none; role="list" on the <ul> keeps them if that matters.) */
  :is(ul, ol):is(.hl-row, .hl-cluster, .hl-long) { list-style: none; margin: 0; padding: 0; }
}

/* ── tokens: every knob, with its default ─────────────────────────────────────────────────── */
@layer hl.tokens {
  :root {
    /* spacing scale, 4 px base; nothing off the scale */
    --hl-s-2: 2px;  --hl-s-4: 4px;  --hl-s-8: 8px;  --hl-s-12: 12px; --hl-s-16: 16px;
    --hl-s-24: 24px; --hl-s-32: 32px; --hl-s-48: 48px; --hl-s-64: 64px;

    /* frame */
    --hl-frame-max: 1280px;                 /* content never gets wider; the rest is even margin */
    --hl-gutter: clamp(16px, 3vw, 32px);    /* the ONLY fluid space */
    --hl-gap: var(--hl-s-16);               /* between columns and cards */
    --hl-gap-section: var(--hl-s-24);       /* between stacked sections */
    --hl-measure: 68ch;                     /* running text stops here, even in a wide column */

    /* auto row (cards) */
    /* v0.4: auto rows no longer use --hl-min / --hl-max-cols (stepped columns + data-max + the orphan guard instead).
       Kept defined so older site CSS that sets them doesn't break; nothing in hl-grid reads them now. */
    --hl-min: 300px;
    --hl-max-cols: 2;
    --hl-sub-rows: 3;                       /* parts per card when cards align on a subgrid (head, body, foot) */

    /* split row (main + side) */
    --hl-side-min: 320px;
    --hl-side-max: 400px;

    /* switch row: below this width ALL children stack, above it ALL sit side by side */
    --hl-switch: 40rem;

    /* list-detail row: the list's width once both show */
    --hl-list: 360px;

    /* panes row (multi-pane live views): narrowest pane, and the most per row */
    --hl-pane-min: 28rem;
    --hl-pane-max-cols: 3;

    /* chrome */
    --hl-head: 48px;                        /* sticky header height; also scroll padding */
    --hl-target: 48px;                      /* minimum touch target (v0.5: 48, the phone rule); density never lowers it */
    --hl-panel: 440px;                      /* side panel width from tablet up */
    --hl-panel-bg: Canvas;
    --hl-scrim: rgb(0 0 0 / .55);

    /* long lists */
    --hl-row-est: 72px;                     /* placeholder height for off-screen rows (content-visibility) */

    scroll-padding-block-start: calc(var(--hl-head) + var(--hl-s-8));
  }

  @media (min-width: 100rem) {               /* >= 1600 px window: the frame and the side column grow a little */
    :root { --hl-frame-max: 1400px; --hl-side-max: 440px; }
  }

  /* density: one attribute on any ancestor swaps the spacing values (comfortable is the default) */
  [data-density="compact"] {
    --hl-gap: var(--hl-s-8);
    --hl-gap-section: var(--hl-s-16);
    --hl-row-est: 52px;
  }
}

/* ── layout: frame → row → column ────────────────────────────────────────────────────────── */
@layer hl.layout {

  /* FRAME: centers the content, owns the gutter, and is the first container. */
  .hl-frame {
    container: hl hl-frame / inline-size;
    inline-size: 100%;
    max-inline-size: var(--hl-frame-max);
    margin-inline: auto;
    padding-inline: max(var(--hl-gutter), env(safe-area-inset-left)) max(var(--hl-gutter), env(safe-area-inset-right));
  }

  /* REGION: opt any element in as a container ("hl"). Rows inside it then adapt to IT, not to the frame.
     Only on elements whose width comes from outside: block children, or grid items in fr/minmax tracks.
     Never on flex items or items in auto/max-content tracks (they collapse to zero width). */
  .hl-cq { container: hl / inline-size; }

  /* MEASURE: caps running text. */
  .hl-measure { max-inline-size: var(--hl-measure); }

  /* ROW: the default is a stack (one column). data-row picks a mode. */
  .hl-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--hl-gap);
    align-content: start;
  }
  .hl-row > * { min-inline-size: 0; }
  .hl-row[data-gap="section"] { gap: var(--hl-gap-section); }

  /* auto (v0.4): cards in STEPPED columns, with a built-in ORPHAN GUARD.
     REQUIREMENT: an auto row never leaves a lone item on its last row by accident, at any width.
     - Columns step by the row's container: 1 → 2 at sm (30rem) → data-max (3 or 4) at lg (60rem). The default
       data-max is 2.
     - Guard, using the item count (:has() + nth-child, no JS): if N items would leave 1 on the last row (N ≡ 1 mod c),
       use the largest smaller count that doesn't (4 → 3 → 2).
     - At 2 columns with an odd count, the last item spans the full row ON PURPOSE (a "featured" last card), because no
       smaller grid avoids the lone item except 1 column.
     Examples: 4 items: 1 → 2×2 → 2×2 (max 3) or 4 (max 4). 5 items: 1 → 2+2+full → 3+2. 7 items, max 3: 2+2+2+full. */
  .hl-row[data-row="auto"] { --hl-c: 1; grid-template-columns: repeat(var(--hl-c), minmax(0, 1fr)); }

  @container hl (min-width: 30rem) {
    .hl-row[data-row="auto"] { --hl-c: 2; }
  }
  @container hl (min-width: 60rem) {
    .hl-row[data-row="auto"][data-max="3"] { --hl-c: 3; }
    .hl-row[data-row="auto"][data-max="4"] { --hl-c: 4; }
    /* guard at 3 columns: N ≡ 1 (mod 3), N > 1 → 2 columns */
    .hl-row[data-row="auto"][data-max="3"]:has(> :last-child:nth-child(3n+1):not(:first-child)) { --hl-c: 2; }
    /* guard at 4: N ≡ 1 (mod 4) → 3 columns; if also N ≡ 1 (mod 3), i.e. N ≡ 1 (mod 12) → 2 columns */
    .hl-row[data-row="auto"][data-max="4"]:has(> :last-child:nth-child(4n+1):not(:first-child)) { --hl-c: 3; }
    .hl-row[data-row="auto"][data-max="4"]:has(> :last-child:nth-child(12n+1):not(:first-child)) { --hl-c: 2; }
  }

  /* guard at 2 columns (odd N > 1): the last item spans the row. These are the exact cases where the row is at 2. */
  @container hl (30rem <= width < 60rem) {
    .hl-row[data-row="auto"]:has(> :last-child:nth-child(2n+1):not(:first-child)) > :last-child { grid-column: 1 / -1; }
  }
  @container hl (min-width: 60rem) {
    .hl-row[data-row="auto"]:not([data-max="3"], [data-max="4"]):has(> :last-child:nth-child(2n+1):not(:first-child)) > :last-child,
    .hl-row[data-row="auto"][data-max="3"]:has(> :last-child:nth-child(6n+1):not(:first-child)) > :last-child,
    .hl-row[data-row="auto"][data-max="4"]:has(> :last-child:nth-child(12n+1):not(:first-child)) > :last-child { grid-column: 1 / -1; }
  }
  /* ...and with data-sub, each card's parts (head, body, foot) line up across the row on a subgrid. */
  .hl-row[data-row="auto"][data-sub] > * {
    grid-row: span var(--hl-sub-rows);
    display: grid;
    grid-template-rows: subgrid;
    row-gap: var(--hl-s-8);
  }

  /* switch: equal columns that flip all at once (no half-wrapped rows), no query needed. */
  .hl-row[data-row="switch"] { display: flex; flex-wrap: wrap; }
  .hl-row[data-row="switch"] > * { flex-grow: 1; flex-basis: calc((var(--hl-switch) - 100%) * 999); }

  /* split: main + side. Stacked until its container (the nearest .hl-frame or .hl-cq) is lg (>= 60rem).
     Source order is main first, then side; it's never reordered (keyboard order = screen order). */
  .hl-main, .hl-side { container: hl / inline-size; }  /* definite tracks (minmax), so safe as containers */

  @container hl (min-width: 60rem) {
    .hl-row[data-row="split"] {
      grid-template-columns: minmax(0, 1fr) minmax(var(--hl-side-min), var(--hl-side-max));
      align-items: start;
    }
    .hl-row[data-row="split"] > .hl-side[data-sticky] {
      position: sticky;
      inset-block-start: calc(var(--hl-head) + var(--hl-gap));
      max-block-size: calc(100dvh - var(--hl-head) - 2 * var(--hl-gap));
      overflow: auto;
      overscroll-behavior: contain;
    }
  }

  /* list-detail (drill-down): list first in the source, detail second.
     Narrow container: ONE shows. The list shows until a detail is open ([data-open] or :target); then only the detail
     shows (it must carry a "Back to list" link). lg and up: a fixed-width list beside a flexible detail. */
  .hl-list, .hl-detail { container: hl / inline-size; }

  @container hl (width < 60rem) {
    .hl-row[data-row="list-detail"] > .hl-detail:not([data-open], :target) { display: none; }
    .hl-row[data-row="list-detail"]:has(> .hl-detail:is([data-open], :target)) > .hl-list { display: none; }
  }
  @container hl (min-width: 60rem) {
    .hl-row[data-row="list-detail"] {
      grid-template-columns: minmax(0, var(--hl-list)) minmax(0, 1fr);
      align-items: start;
    }
    .hl-row[data-row="list-detail"] .hl-back { display: none; }   /* the Back link only matters when one shows */
  }

  /* panes (multi-pane live views). Narrow: a swipe strip, one pane per screen, snapping (no JS needed).
     md and up: a capped grid of panes, never narrower than --hl-pane-min, at most --hl-pane-max-cols per row.
     Each pane is a container, so its contents adapt to the pane. */
  .hl-row[data-row="panes"] {
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
  }
  .hl-row[data-row="panes"] > * { scroll-snap-align: start; container: hl / inline-size; }

  @container hl (min-width: 40rem) {
    .hl-row[data-row="panes"] {
      grid-auto-flow: row;
      grid-auto-columns: auto;
      overflow-x: visible;
      scroll-snap-type: none;
      grid-template-columns: repeat(auto-fill, minmax(
        max(min(100%, var(--hl-pane-min)),
            calc((100% - (var(--hl-pane-max-cols) - 1) * var(--hl-gap)) / var(--hl-pane-max-cols))),
        1fr));
    }
  }

  /* CLUSTER: inline things (tags, buttons) that wrap as a group. */
  .hl-cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--hl-s-8); }

  /* LONG LIST: skip rendering off-screen rows; "auto" remembers each row's real height once seen. */
  .hl-long > * {
    content-visibility: auto;
    contain-intrinsic-block-size: auto var(--hl-row-est);
  }
}

/* ── panel: the side panel ─────────────────────────────────────────────────────────────────
   Opened by [data-open] (set by the page's script) or :target (JS-free mockups).
   phone: full screen, modal · tablet (>= 40rem window): docked right over a scrim, modal ·
   desktop (>= 64rem window): NON-MODAL, docked right under the header, no scrim, the page stays usable.
   The script owns aria-modal, focus, Escape and inert; see PRIMITIVES.md. */
@layer hl.panel {
  .hl-panel {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: none;
    flex-direction: column;
    background: var(--hl-panel-bg);
    overflow: auto;
    overscroll-behavior: contain;
  }
  .hl-panel:is([data-open], :target) { display: flex; animation: hl-slide .2s ease-out; }

  @media (min-width: 40rem) {
    .hl-panel {
      inset: 0 0 0 auto;
      inline-size: min(var(--hl-panel), 90vw);
      box-shadow: 0 0 0 100vmax var(--hl-scrim);   /* the scrim, without an extra element */
    }
  }
  @media (min-width: 64rem) {
    .hl-panel {
      inset-block-start: var(--hl-head);
      inline-size: var(--hl-panel);
      box-shadow: -1px 0 0 0 color-mix(in srgb, currentColor 20%, transparent);
    }
  }

  @keyframes hl-slide { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
  @keyframes hl-fade  { from { opacity: 0; } to { opacity: 1; } }
  @media (prefers-reduced-motion: reduce) {
    .hl-panel:is([data-open], :target) { animation-name: hl-fade; }
  }
}

/* ── enhance: preferences, print, and the place for future progressive features ───────────── */
@layer hl.enhance {
  @media (forced-colors: active) {
    .hl-panel { border-inline-start: 1px solid CanvasText; box-shadow: none; }
  }
  @media print {
    .hl-panel { display: none !important; }
    .hl-row[data-row="split"] > .hl-side { position: static; max-block-size: none; overflow: visible; }
    .hl-long > * { content-visibility: visible; }
  }
  /* Deliberately NOT used (see PRINCIPLES.md): grid lanes/masonry (Safari only), reading-flow and gap
     decorations (Chromium only), container style queries and anchor positioning (newly Baseline: add only behind
     @supports, with the plain layout above as the fallback). */
}
