/*
 * Layout primitives: the composition layer between tokens and components.
 * Algorithms adapted from Every Layout (every-layout.dev), in pure CSS.
 *
 * - Spacing belongs to the parent: components never set their own outer
 *   margins; the primitive that contains them does.
 * - Responsive by intrinsic size: primitives react to the space their
 *   container gives them, not to the viewport.
 * - Configure one instance with inline custom properties that use tokens:
 *     <div class="cluster" style="--cluster-gap: var(--space-4)">
 *   Each primitive declares its own defaults, so a tuned value never leaks
 *   into nested primitives through inheritance. The Stack is the exception:
 *   see its notes below.
 *
 * Each primitive's comment shows its usage and lists its options, with
 * defaults. Options marked "on a child" go on a direct child, not on the
 * primitive itself.
 */

@layer layout {
  /*
   * Stack: vertical flow, with space between children and none around them.
   *
   *   <div class="stack" data-space="2">
   *     <h2>…</h2>
   *     <p>…</p>
   *     <figure style="--stack-space: var(--space-8)">…</figure>
   *   </div>
   *
   * Options:
   *   data-space     Space between children, as a step of the space scale
   *                  (1, 2, 3, 4, 6, 8, 12). Default: --space-gap.
   *   --stack-space  On a child: replace the space above that child. Set it
   *                  on the next sibling too to change the space below.
   *   .stack__split  On a child: the stack splits after it, pushing the
   *                  children that follow to the bottom. Needs spare height,
   *                  such as a stretched grid cell.
   *
   * The space is a margin on each child, as in Every Layout, so each child
   * reads --stack-space itself. That's why the stack sets it with an
   * attribute rather than an inline custom property: a nested Stack's own
   * inline value would replace its parent's value for the space above the
   * nested Stack itself. The rules below give every child of a Stack its
   * parent's value, so an exception on one child never reaches a Stack
   * nested inside it.
   */
  .stack {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;

    & > * { --stack-space: var(--space-gap); }
    &[data-space="1"] > *  { --stack-space: var(--space-1); }
    &[data-space="2"] > *  { --stack-space: var(--space-2); }
    &[data-space="3"] > *  { --stack-space: var(--space-3); }
    &[data-space="4"] > *  { --stack-space: var(--space-4); }
    &[data-space="6"] > *  { --stack-space: var(--space-6); }
    &[data-space="8"] > *  { --stack-space: var(--space-8); }
    &[data-space="12"] > * { --stack-space: var(--space-12); }

    /* Dialogs and popovers sit on top of the page, not in its flow, so
       they keep their own centering margins. */
    & > * + :not(dialog, [popover]) { margin-block-start: var(--stack-space); }

    & > .stack__split { margin-block-end: auto; }

    &:only-child:has(> .stack__split) { block-size: 100%; }
  }

  /*
   * Cluster: inline items that wrap, such as tags, toolbars, button groups
   * and nav links.
   *
   *   <ul class="cluster" role="list" style="--cluster-justify: space-between">…</ul>
   *
   * Options:
   *   --cluster-gap      Space between items. Default: --space-2.
   *   --cluster-justify  A justify-content value. Default: flex-start.
   *   --cluster-align    An align-items value. Default: flex-start.
   */
  .cluster {
    --cluster-gap: var(--space-2);
    --cluster-justify: flex-start;
    --cluster-align: flex-start;

    display: flex;
    flex-wrap: wrap;
    justify-content: var(--cluster-justify);
    align-items: var(--cluster-align);
    gap: var(--cluster-gap);
  }

  /*
   * Center: a horizontally centered column, capped at a readable measure.
   *
   *   <div class="center" data-intrinsic>…</div>
   *
   * Options:
   *   --center-max     The column's maximum width. Default: --measure.
   *   --center-gutter  The minimum space on either side. Default: 0, since
   *                    the page's padded <main> already provides it.
   *   data-intrinsic   Also center children narrower than the column, such
   *                    as a lone button.
   *
   * Don't make a Center a direct child of a flex-based primitive (Stack,
   * Cluster, Sidebar, ...): as a flex item, its auto margins shrink it to its
   * content instead of filling the column. Put the other primitive inside
   * the Center, on the same element, or wrap the Center in a plain element.
   */
  .center {
    --center-max: var(--measure);
    --center-gutter: 0;

    box-sizing: content-box;
    max-inline-size: var(--center-max);
    margin-inline: auto;
    padding-inline: var(--center-gutter);

    &[data-intrinsic] {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }

  /*
   * Box: a padded, visibly bounded area.
   *
   *   <div class="box" style="--box-bg: var(--color-surface); --box-fg: var(--color-text)">…</div>
   *
   * Options:
   *   --box-padding       Padding on every side. Default: --space-4.
   *   --box-border-width  Default: 1px. Use 0 for a borderless box.
   *   --box-border-color  Default: --color-border.
   *   --box-radius        Default: 0.
   *   --box-bg            Background. Default: transparent.
   *   --box-fg            Text color, inherited inside. Set it with
   *                       --box-bg so text stays legible. Default: inherited.
   *
   * The transparent outline is invisible until forced-colors mode draws
   * it, so a borderless box keeps its shape there.
   */
  .box {
    --box-padding: var(--space-4);
    --box-border-width: 1px;
    --box-border-color: var(--color-border);
    --box-radius: 0;
    --box-bg: transparent;
    --box-fg: currentColor;

    padding: var(--box-padding);
    color: var(--box-fg);
    background: var(--box-bg);
    border: var(--box-border-width) solid var(--box-border-color);
    border-radius: var(--box-radius);
    outline: 1px solid transparent;
    outline-offset: -1px;
  }

  /*
   * Sidebar: a sidebar beside the content, stacking when the content would
   * get too narrow. The sidebar is the first child.
   *
   *   <div class="with-sidebar" style="--sidebar-width: 20rem">
   *     <nav>…</nav>
   *     <div>…</div>
   *   </div>
   *
   * Options:
   *   --sidebar-width        The sidebar's width beside the content.
   *                          Default: its content's width.
   *   --sidebar-content-min  The content's narrowest width before both
   *                          stack, as a percentage. Default: 50%.
   *   --sidebar-gap          Default: --space-gap.
   *   --sidebar-align        An align-items value; flex-start stops the two
   *                          stretching to equal height. Default: stretch.
   *   data-side="end"        Make the last child the sidebar.
   */
  .with-sidebar {
    --sidebar-width: auto;
    --sidebar-content-min: 50%;
    --sidebar-gap: var(--space-gap);
    --sidebar-align: stretch;

    display: flex;
    flex-wrap: wrap;
    align-items: var(--sidebar-align);
    gap: var(--sidebar-gap);

    & > :first-child {
      flex-basis: var(--sidebar-width);
      flex-grow: 1;
    }

    & > :last-child {
      flex-basis: 0;
      flex-grow: 999;
      min-inline-size: var(--sidebar-content-min);
    }

    &[data-side="end"] > :first-child {
      flex-basis: 0;
      flex-grow: 999;
      min-inline-size: var(--sidebar-content-min);
    }

    &[data-side="end"] > :last-child {
      flex-basis: var(--sidebar-width);
      flex-grow: 1;
      min-inline-size: auto;
    }
  }

  /*
   * Switcher: equal-width children in a row, switching to a column all at
   * once when the container is narrower than the threshold. No media queries.
   *
   *   <div class="switcher" style="--switcher-threshold: 40rem">…</div>
   *
   * Options:
   *   --switcher-threshold  The container width at which it switches.
   *                         Default: --measure.
   *   --switcher-gap        Default: --space-gap.
   *
   * Five or more children always form a column, so a row never gets
   * squashed. CSS can't read a custom property in a selector, so this limit
   * of four is fixed.
   */
  .switcher {
    --switcher-threshold: var(--measure);
    --switcher-gap: var(--space-gap);

    display: flex;
    flex-wrap: wrap;
    gap: var(--switcher-gap);

    & > * {
      flex-grow: 1;
      flex-basis: calc((var(--switcher-threshold) - 100%) * 999);
    }

    & > :nth-last-child(n + 5),
    & > :nth-last-child(n + 5) ~ * {
      flex-basis: 100%;
    }
  }

  /*
   * Grid: columns of equal width, as many as fit, each at least --grid-min
   * wide. A row with fewer items than columns keeps the empty columns, so a
   * lone card stays card-sized instead of stretching across the row.
   *
   *   <ul class="grid" role="list" style="--grid-min: 20rem">…</ul>
   *
   * Options:
   *   --grid-min   The narrowest a column gets before the grid drops one.
   *                Never overflows a narrower container. Default: 16rem.
   *   --grid-gap   Default: --space-gap.
   *   --grid-fill  auto-fill keeps empty columns; auto-fit collapses them,
   *                so a short row stretches to fill the width.
   *                Default: auto-fill.
   */
  .grid {
    --grid-min: 16rem;
    --grid-gap: var(--space-gap);
    --grid-fill: auto-fill;

    display: grid;
    grid-template-columns: repeat(var(--grid-fill), minmax(min(var(--grid-min), 100%), 1fr));
    gap: var(--grid-gap);
  }

  /*
   * Cover: a tall block with one principal child centered vertically between
   * an optional header and footer, as on splash and empty states.
   *
   *   <section class="cover">
   *     <header>…</header>
   *     <h1 class="cover__principal">…</h1>
   *     <footer>…</footer>
   *   </section>
   *
   * Options:
   *   --cover-min-height  Default: 100svh.
   *   --cover-padding     Default: --space-4.
   *   --cover-gap         The minimum space between children. Default:
   *                       --space-4.
   *   .cover__principal   On a child: the one to center.
   */
  .cover {
    --cover-min-height: 100svh;
    --cover-padding: var(--space-4);
    --cover-gap: var(--space-4);

    display: flex;
    flex-direction: column;
    gap: var(--cover-gap);
    min-block-size: var(--cover-min-height);
    padding: var(--cover-padding);

    & > .cover__principal { margin-block: auto; }
  }

  /*
   * Reel: a horizontally scrolling row.
   *
   *   <div class="reel" style="--reel-item-width: 20rem"
   *        tabindex="0" role="region" aria-label="…">…</div>
   *
   * Options:
   *   --reel-item-width  Default: each item's content width.
   *   --reel-gap         Default: --space-4.
   *
   * Give it tabindex="0", role="region" and an aria-label so keyboard and
   * screen reader users can reach and scroll it. Image children share the
   * reel's height and keep their aspect ratio.
   */
  .reel {
    --reel-item-width: auto;
    --reel-gap: var(--space-4);

    display: flex;
    gap: var(--reel-gap);
    overflow-x: auto;
    overflow-y: hidden;
    padding-block-end: var(--space-2);
    scrollbar-color: var(--color-border) transparent;

    & > * { flex: 0 0 var(--reel-item-width); }

    & > img { block-size: 100%; flex-basis: auto; inline-size: auto; }
  }

  /*
   * Icon: an icon sized to the cap height of the surrounding text, sitting
   * on its baseline. On its own, .icon is spaced from the text by a normal
   * word space; wrap both in .with-icon to set the space instead.
   *
   *   <span class="with-icon"><svg class="icon" aria-hidden="true">…</svg> Saved</span>
   *
   * Options:
   *   --icon-size  The icon's width and height, set on the icon or any
   *                ancestor. Default: the text's cap height. Icon sets
   *                drawn with padding inside their viewBox, like
   *                Heroicons, look small at cap height; so do icons that
   *                are a control's only content.
   *   --icon-gap   On .with-icon. Default: 0.5em.
   *
   * Mark decorative icons aria-hidden="true"; an icon-only control needs
   * its own accessible name (aria-label or .visually-hidden text).
   */
  /* --icon-size is read with a fallback rather than declared here, so a
     component can size every icon inside it; icons don't nest, so the
     value can't leak anywhere unwanted. */
  .icon {
    inline-size: var(--icon-size, 0.75em);
    block-size: var(--icon-size, 0.75em);

    @supports (inline-size: 1cap) {
      inline-size: var(--icon-size, 1cap);
      block-size: var(--icon-size, 1cap);
    }
  }

  .with-icon {
    --icon-gap: 0.5em;

    display: inline-flex;
    align-items: baseline;
    gap: var(--icon-gap);
  }
}
