@layer components {
  /* The page's crumbs and actions, as wide as the page under it, staying at the top of the window. */
  .strip {
    --header-gap: 0.5ch;
    --header-btn-size: 2rem;
    --strip-gutter: max(var(--main-padding), (100% - var(--strip-size, 65rem)) / 2);

    align-items: center;
    border-image: conic-gradient(var(--color-page) 0 0) fill 0 / 0 / 0 100vmax;
    display: flex;
    gap: var(--inline-space);
    inset-block-start: var(--custom-safe-inset-top);
    margin-inline:
      calc(var(--strip-gutter) + var(--custom-safe-inset-left))
      calc(var(--strip-gutter) + var(--custom-safe-inset-right));
    min-block-size: var(--strip-height);
    /* The hairline takes the last pixel, so the crumbs sit one pixel higher to keep the same air below as above. */
    padding-block: var(--block-space-half) calc(var(--block-space-half) + 1px);
    position: sticky;
    z-index: var(--z-nav);

    &::after {
      border-block-end: 1px solid var(--color-ink-lighter);
      content: "";
      inset: auto 0 0;
      position: absolute;
    }

    &:has(dialog[open]) {
      z-index: var(--z-nav-open);
    }

    .header__title {
      flex: 1 1 auto;
      font-size: var(--text-normal);
      font-weight: 700;
      margin: 0;
      text-align: start;
    }

    .breadcrumbs {
      font-size: inherit;
      margin-inline: 0;
    }

    .header__actions {
      align-items: center;
      flex: none;
      font-size: var(--text-small);
      inline-size: auto;
    }

    .btn--circle,
    .btn--circle-mobile {
      --btn-size: var(--header-btn-size);
    }

    @media (width >= 640px) {
      --header-btn-size: 1.75rem;

      .header__actions .btn--circle-mobile[aria-label] {
        --btn-gap: var(--inline-space-half);
        --btn-padding: 0 0.9em;
        --icon-size: 1em;

        block-size: var(--header-btn-size);
        min-block-size: 0;
      }
    }

    @media print {
      display: none;
    }
  }

  /* At the top of the sheet the strip is as wide as the sheet, 48px tall with its hairline and fill kept inside it, in 14px words and 32px buttons. It sticks from 800 up; on a phone it scrolls away with the page, under the top bar. */
  .sheet > .strip {
    --header-btn-size: var(--control-size);

    border-image-outset: 0;
    font-size: var(--text-control);
    margin-inline: 0;
    padding-block: 0;
    padding-inline:
      calc(var(--strip-padding) + var(--custom-safe-inset-left))
      calc(var(--strip-padding-end) + var(--custom-safe-inset-right));

    @media (width < 800px) {
      inset-block-start: auto;
      position: relative;
    }

    :is(.header__title, .header__actions) {
      font-size: inherit;
    }

    /* The actions stand 4px apart, a button with words padded half their size at each end. */
    .header__actions {
      gap: calc(var(--control-gap) / 2);

      .btn:not(dialog *, .popup *, .btn--circle, .btn--circle-mobile, [aria-label]:has(.icon), :has(.for-screen-reader):has(.icon)) {
        --btn-padding: 0 0.5em;
      }
    }

    /* As the top bar merges, the crumbs and the actions leave the middle to its field, and a button whose words
       are too long for what is left gives up their end. */
    @media (width >= 800px) {
      :is(.header__title, .header__actions) {
        max-inline-size: calc(100% - var(--bar-merge) * (50% + var(--jump-field-size) / 2 + var(--inline-space)));
      }

      .header__actions > .btn {
        min-inline-size: 0;

        > span:not(.icon) {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }

    @media (width >= 640px) {
      .header__actions .btn--circle-mobile[aria-label] {
        --btn-padding: 0 0.5em;
      }
    }

    /* On a phone too an action says what it is beside its icon. */
    @media (width < 640px) {
      .header__actions .btn--circle-mobile {
        --btn-gap: var(--inline-space-half);
        --btn-padding: 0 0.5em;
        --icon-size: 1em;

        aspect-ratio: auto;
        block-size: var(--control-size);
        display: inline-flex;
        inline-size: auto;
        white-space: nowrap;

        span:last-of-type:not(.icon) {
          display: inline;
        }
      }
    }

    /* The crumbs read as plain links in the ink, each after a small chevron pointing back, and the page's own name, where it is still a crumb, in bold. */
    .breadcrumbs {
      font-weight: 400;

      > strong {
        color: var(--color-ink);
        font-weight: 700;
      }

      /* Underlined only under the pointer or the keys. */
      > a {
        text-decoration: none;

        &:is(:hover, :focus-visible) {
          text-decoration: underline;
        }
      }

      /* With the name in a heading on the page, the crumbs only lead back: the first one in bold, and below 800 only the parent, after its chevron. */
      &:not(:has(> strong)) {
        > :nth-child(1 of a:not(.breadcrumbs__home)) {
          font-weight: 700;
        }

        > .breadcrumbs__separator:last-child {
          display: none;
        }

        @media (width < 800px) {
          > :not(:nth-last-child(-n + 2)) {
            display: none;
          }

          > .breadcrumbs__separator:last-child {
            display: block;
            order: -1;
          }
        }
      }

      /* Inside a site: its name in bold, then the page's own in plain words; below 800 only the way back. */
      &:has(> .breadcrumbs__page) {
        > .breadcrumbs__page {
          color: var(--color-ink);
        }

        @media (width < 800px) {
          > a {
            display: block;
          }

          > .breadcrumbs__separator {
            order: -1;
          }

          > .breadcrumbs__page {
            display: none;
          }
        }
      }

      /* Where the name is still a crumb, below 800 the trail is cut to the parent and the name. */
      @media (width < 800px) {
        &:has(> strong) > :not(:nth-last-child(-n + 4)) {
          display: none;
        }
      }

      > .breadcrumbs__separator {
        --svg: url("/assets/caret-down-b59252bf.svg");

        background-color: var(--color-ink-medium);
        block-size: 0.6em;
        color: transparent;
        inline-size: 0.6em;
        mask: var(--svg) center / contain no-repeat;
        rotate: 90deg;
      }
    }

    /* The page's actions are quiet: no outline, a tint under the pointer. */
    .header__actions :where(.btn:not(.btn--link, .btn--negative, .btn--positive, .btn--reversed, [aria-current], :has(input))) {
      --btn-background: transparent;
      --btn-border-color: transparent;
      --btn-hover-amount: 8%;
    }
  }

  .strip .header__actions:not(:has(*)) {
    display: none;
  }

  /* A sheet with no strip takes the strip's row once it reaches the top of the window, for the top bar's field to sit in. A public page has no such bar. */
  @supports (animation-timeline: scroll()) {
    @media screen and (width >= 800px) {
      body:not(.public) .sheet:not(:has(> .strip))::before {
        animation: bar-merge linear both;
        animation-range: calc(var(--top-bar-height) - 0.5rem) var(--top-bar-height);
        animation-timeline: scroll(root);
        background-color: var(--color-canvas);
        block-size: var(--strip-height);
        box-shadow: inset 0 -1px var(--color-ink-lighter);
        content: "";
        inset-block-start: var(--custom-safe-inset-top);
        margin-block-end: calc(-1 * var(--strip-height));
        opacity: var(--bar-merge);
        pointer-events: none;
        position: sticky;
        z-index: var(--z-nav);
      }
    }
  }

  /* What stays at the top is as tall as the strip at every width: the strip from 800 up, the top bar on a phone. Not while the focus is in it, or the browser would scroll the page to bring it out from under itself. */
  html:has(.strip, body:not(.public) .sheet):not(:has(:is(#header, .strip):focus-within)) {
    scroll-padding-block-start: calc(var(--strip-height) + var(--custom-safe-inset-top) + var(--block-space));
  }

  html:has(.bar) {
    scroll-padding-block-end: calc(var(--footer-height) + var(--custom-safe-inset-bottom) + var(--block-space-half));
  }
}
