@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * BREADCRUMBS
 * Navigation landmark with an ordered list of links.
 * ======================================== */

  .cinder-breadcrumbs {
    /* How far a crumb link's hit area reaches past its 20px line on each side: 2px, so a press
       target is the 24px minimum (WCAG 2.5.8). */
    --_cinder-breadcrumbs-link-overhang: var(--cinder-space-0-5);

    container-type: inline-size;
    container-name: cinder-breadcrumbs;
  }

  .cinder-breadcrumbs__list {
    list-style: none;
    /* Room inside the list's clip for crumb links and the overflow button, given back by a
       matching negative margin so the trail's layout does not move. */
    padding: var(--_cinder-breadcrumbs-link-overhang) 0;
    margin: calc(var(--_cinder-breadcrumbs-link-overhang) * -1) 0;
    display: flex;
    align-items: center;
    gap: var(--cinder-space-2);
    font-size: var(--cinder-text-sm);
    /* The type scale's 20px line for 13px text, as the navigation rows use: the page's 1.5 would
       make every crumb a 19.5px box whose centre sits on a half pixel beside the selector. */
    line-height: 1.25rem;
    color: var(--cinder-text-muted);
    /* A long trail never wraps to a second row: earlier ancestors fold into the "…" menu
     (`maxVisible`), and what is left clips with an ellipsis. */
    overflow: hidden;
    white-space: nowrap;
  }

  .cinder-breadcrumbs__item {
    /* The foundation gives every list item a bottom margin for running text (`:where(li)`); a
       crumb kept it, so the trail was 4px taller than its words and the crumbs sat 2px above the
       centre of any bar that centres the trail (COR-2438). */
    margin: 0;
    display: inline-flex;
    align-items: center;
    gap: var(--cinder-space-2);
    /* Ancestor crumbs keep their words: they shrink slowly (factor 1), so a narrow trail never
     reduces one to nothing or to a stray fragment. */
    min-width: 0;
    flex-shrink: 1;
  }

  /* The last item holds the current crumb, and it gives way first (design rows 03l and 04f). It
   shrinks so much faster than the ancestors that a long title ellipsizes while the ancestors keep
   every pixel of their words, and it stops at a floor of four characters, enough for a glyph or two
   and an ellipsis; only then do the ancestors shrink. Plain flex cannot express strict
   source-order priority, and a proportional ratio such as 100 to 1 still takes a fraction of a pixel
   from an ancestor, which is enough for its ellipsis to engage on a label that fits. So the ratio
   is lopsided enough (100000 to 1, here and on the phone trail's list) that the fraction is below
   what layout resolves, while the leaf still never overflows the container without its own
   ellipsis. */
  .cinder-breadcrumbs__item:last-child {
    flex-shrink: 100000;
    min-inline-size: min(4ch, 100%);
  }

  .cinder-breadcrumbs__link {
    position: relative;
    min-width: 0;
    flex: 1 1 auto;
    color: var(--cinder-text-muted);
    text-decoration: none;
    border-radius: var(--cinder-radius-sm);
    transition: color var(--cinder-duration-fast) var(--cinder-ease-standard);
    /* Clipped for the ellipsis, but the clip reaches as far as the hit area below, so it answers a
       press there; the text never paints past the box, which its ellipsis truncates. */
    overflow: clip;
    overflow-clip-margin: var(--_cinder-breadcrumbs-link-overhang);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The link's 20px line is under the 24px minimum target: a pseudo-element that takes no layout
     space and paints nothing extends its hit area to 24px, so the crumb is drawn exactly as before. */
  .cinder-breadcrumbs__link::before {
    content: '';
    position: absolute;
    inset-block: calc(var(--_cinder-breadcrumbs-link-overhang) * -1);
    inset-inline: 0;
  }

  .cinder-breadcrumbs__overflow::before {
    content: '';
    position: absolute;
    inset-block: calc(var(--_cinder-breadcrumbs-link-overhang) * -1);
    inset-inline: 0;
    clip-path: inset(0);
  }

  .cinder-breadcrumbs__link:hover {
    color: var(--cinder-text-default);
    text-decoration: underline;
  }

  .cinder-breadcrumbs__link:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  @media (forced-colors: active) {
    .cinder-breadcrumbs__link:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: 3px;
    }
  }

  .cinder-breadcrumbs__current {
    color: var(--cinder-text-default);
    font-weight: var(--cinder-font-medium);
    /* The current crumb is the priority: its low-shrink item (see `:last-child`)
     makes ancestors yield first. But it must never hard-clip against the
     container edge — `max-width: 100%` plus `min-width: 0` bound it to the
     available width so its own ellipsis engages as a last resort when it alone
     exceeds the container. */
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .cinder-breadcrumbs__separator {
    color: var(--cinder-text-muted);
    user-select: none;
    /* Separators never collapse. */
    flex-shrink: 0;
  }

  /* ----------------------------------------
 * Current page as a popover trigger
 * A long title keeps one line: the button truncates its label with an ellipsis at
 * 300px (18.75rem) and the popover holds the full text.
 * ---------------------------------------- */

  .cinder-breadcrumbs__trigger {
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    gap: var(--cinder-space-2);
    /* A fixed cap, not a percentage: a percentage against the item is cyclic, so the item would size
       to the whole untruncated title and squeeze the ancestors out of the trail. */
    max-inline-size: 18.75rem;
    min-inline-size: 0;
    /* The painted height of a medium Button, so the switcher is as tall as a search field or an
       icon button beside it in a bar and shares their centre line (it was 30px beside 32px). A
       coarse pointer keeps this face and reaches 44px through the hit area below. */
    min-block-size: var(--_cinder-painted-height-md, 2rem);
    padding-block: 0;
    padding-inline: var(--cinder-space-3) var(--cinder-space-2);
    border: 1px solid var(--cinder-border-control);
    border-radius: var(--cinder-radius-md);
    background: var(--cinder-surface-raised);
    color: var(--cinder-text-default);
    font: inherit;
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-tight);
    cursor: pointer;
  }

  .cinder-breadcrumbs__trigger-label {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The label is centred as a line box, and a line box is not centred on its capitals: Chromium
   * rounds a font's ascent and descent to whole pixels and floors the half-leading of a line whose
   * height is not a whole number of pixels (here 1.15 of 14px, 16.1px), so the capitals stood
   * about 1.5px above the middle of the 30px box in the 320 and 375 captures (the same
   * derivation error `callout.css` measures). Where `text-box-trim` ships (Chromium, Safari) the
   * label is trimmed to its capitals and baseline, so flex centring centres the capitals exactly,
   * for any font. The trim leaves no room for ascenders and descenders, and the label clips, so the
   * block padding gives them room and the negative margin hands it back, as Button's label does. */
  @supports (text-box-trim: trim-both) {
    .cinder-breadcrumbs__trigger-label {
      text-box: trim-both cap alphabetic;
      padding-block: 0.25em;
      margin-block: -0.25em;
    }
  }

  .cinder-breadcrumbs__trigger-icon {
    flex: none;
    inline-size: var(--cinder-space-4);
    block-size: var(--cinder-space-4);
    color: var(--cinder-text-muted);
  }

  .cinder-breadcrumbs__trigger:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  @media (forced-colors: active) {
    .cinder-breadcrumbs__trigger:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: 3px;
    }
  }

  /* On a coarse pointer the current page's popover trigger paints a medium Button's 32px and its
     hit area reaches 44px through a positioned pseudo-element that takes no layout space. The
     trail's list clips what overflows it, and the trigger clipped its own overflow too, so a hit
     area drawn past the trigger's edge would be cut back to 32px: the trigger lets its overflow show (its label
     truncates itself), and the list gains room inside its clip with padding that a matching
     negative margin gives back, so the trail's layout does not move. */
  @media (any-pointer: coarse) {
    .cinder-breadcrumbs__trigger {
      position: relative;
      overflow: visible;
    }

    .cinder-breadcrumbs__trigger::before {
      content: '';
      position: absolute;
      inset-block: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
      inset-inline: 0;
    }

    .cinder-breadcrumbs__list:has(> .cinder-breadcrumbs__item > .cinder-breadcrumbs__trigger) {
      --_cinder-breadcrumbs-hit-overhang: max(
        0px,
        calc((var(--cinder-touch-target-min) - var(--_cinder-painted-height-md, 2rem)) / 2)
      );

      padding-block: var(--_cinder-breadcrumbs-hit-overhang);
      margin-block: calc(var(--_cinder-breadcrumbs-hit-overhang) * -1);
    }

    .cinder-breadcrumbs__overflow::before {
      inset-block: min(0px, calc((100% - var(--cinder-touch-target-min)) / 2));
      clip-path: none;
    }

    .cinder-breadcrumbs__link {
      --_cinder-breadcrumbs-link-overhang: var(--_cinder-breadcrumbs-hit-overhang);
      overflow: visible;
    }

    .cinder-breadcrumbs__link-label {
      display: block;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .cinder-breadcrumbs__link::before {
      clip-path: inset(0);
    }

    /* The 20px links and overflow button need more room than the trigger. */
    .cinder-breadcrumbs__list:has(> .cinder-breadcrumbs__item > .cinder-breadcrumbs__link),
    .cinder-breadcrumbs__list:has(> .cinder-breadcrumbs__item > .cinder-breadcrumbs__overflow) {
      --_cinder-breadcrumbs-hit-overhang: max(
        0px,
        calc((var(--cinder-touch-target-min) - 1.25rem) / 2)
      );

      padding-block: var(--_cinder-breadcrumbs-hit-overhang);
      margin-block: calc(var(--_cinder-breadcrumbs-hit-overhang) * -1);
    }
  }

  .cinder-breadcrumbs__popover-text {
    margin: 0;
    max-inline-size: 24rem;
    color: var(--cinder-text-default);
    font-size: var(--cinder-text-sm);
    line-height: var(--cinder-leading-normal);
    overflow-wrap: anywhere;
  }

  /* ----------------------------------------
 * Phone form
 * Below `collapseBelow` the trail is a back link to the parent, then the current page, which
 * truncates first. The back link is a touch target and keeps its full label. Both forms are in the
 * markup, server HTML included, and the viewport query at the end of this file shows one of them,
 * so the first paint is already the right form (COR-2770). Each hidden part is `display: none`, so
 * the accessibility tree holds one form at a time. The query here is the default `collapseBelow`
 * (640px); any other length brings the same rules in a `<style>` of its own
 * (`breadcrumbs-phone-form.ts`).
 * ---------------------------------------- */

  /* The back link keeps its whole label while there is room: the current page, with its far higher
   shrink factor, truncates first (design row 04f), and the link shrinks only once that is at its
   floor. It shows only in the phone form. */
  .cinder-breadcrumbs__back {
    box-sizing: border-box;
    display: none;
    flex: 0 1 auto;
    min-inline-size: 0;
    align-items: center;
    position: relative;
    /* The link starts on the trail's edge, so its box and its hit area stay inside a trail that
       clips sideways; only its chevron hangs (below). The separator after it sets the gap. */
    padding-inline: 0;
    border-radius: var(--cinder-radius-sm);
    color: var(--cinder-accent-text);
    font-size: var(--cinder-text-base);
    text-decoration: none;
  }

  /* A 44px hit area that takes no layout space. */
  /* A short label ("Chat") draws a link narrower than 44px: the hit area then reaches past its
     inline end, toward the separator, and never past its start, which sits on an edge a trail may
     clip. */
  .cinder-breadcrumbs__back::after {
    content: '';
    position: absolute;
    inset-block: 50% auto;
    block-size: var(--cinder-touch-target-min);
    transform: translateY(-50%);
    margin-inline-end: min(0px, calc(100% - var(--cinder-touch-target-min)));
    inset-inline: 0;
  }

  .cinder-breadcrumbs__back:hover {
    color: var(--cinder-accent-text-hover);
  }

  .cinder-breadcrumbs__back:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  @media (forced-colors: active) {
    .cinder-breadcrumbs__back {
      color: LinkText;
    }

    .cinder-breadcrumbs__back:focus-visible {
      outline: var(--cinder-ring-width) solid ButtonText;
      outline-offset: 3px;
    }
  }

  /* The chevron hangs: lucide's chevron draws its ink a third of the way into its 18px box, so the
     box starts 6px before the link's edge and the ink starts on it, the edge a page title below
     shares (2026-11-28 review). What hangs out is the empty side bearing, so a trail that clips
     sideways cuts no ink, and the link's own hit area is unmoved. */
  .cinder-breadcrumbs__back-icon {
    flex: none;
    inline-size: 1.125rem;
    block-size: 1.125rem;
    margin-inline-start: calc(var(--cinder-space-1-5) * -1);
  }

  /* The separator between the back link and the current page shows only in the phone form. */
  .cinder-breadcrumbs__back-separator {
    display: none;
  }

  .cinder-breadcrumbs__back-label {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  @container cinder-breadcrumbs (max-width: 24rem) {
    .cinder-breadcrumbs__item:not(:first-child):not(:last-child) {
      flex: 1 1 min(8rem, 25%);
      max-inline-size: min(8rem, 25%);
    }

    .cinder-breadcrumbs__item:not(:first-child):not(:last-child) .cinder-breadcrumbs__link {
      max-inline-size: 100%;
    }
  }

  /* The "…" button that stands for the ancestors folded out of a narrow trail. It is the width
   * of its glyph, so it never shrinks to a fragment. */
  .cinder-breadcrumbs__item--overflow {
    flex-shrink: 0;
  }

  .cinder-breadcrumbs__overflow {
    appearance: none;
    position: relative;
    min-inline-size: 1.5rem;
    padding-block: 0;
    padding-inline: var(--cinder-space-1);
    color: var(--cinder-text-muted);
    font: inherit;
    line-height: inherit;
    background: transparent;
    border: none;
    border-radius: var(--cinder-radius-sm);
    cursor: pointer;
  }

  .cinder-breadcrumbs__overflow:hover {
    color: var(--cinder-text-default);
    background-color: var(--cinder-surface-hover);
  }

  .cinder-breadcrumbs__overflow:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  .cinder-breadcrumbs__overflow-list {
    display: flex;
    flex-direction: column;
    gap: var(--cinder-space-1);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .cinder-breadcrumbs__overflow-link {
    display: block;
    padding: var(--cinder-space-1) var(--cinder-space-2);
    color: var(--cinder-text-default);
    text-decoration: none;
    border-radius: var(--cinder-radius-sm);
  }

  .cinder-breadcrumbs__overflow-link:hover {
    background-color: var(--cinder-surface-hover);
  }

  .cinder-breadcrumbs__overflow-link:focus-visible {
    outline: var(--cinder-ring-width) solid transparent;
    box-shadow: var(--_cinder-focus-ring-shadow);
  }

  .cinder-breadcrumbs__monospace {
    font-family: var(--cinder-font-mono);
  }

  /* The phone form at the default `collapseBelow`: a viewport query, as `collapseBelow` is a viewport
     width. Only a trail marked with that length takes it, so `collapseBelow={false}` and other
     lengths keep their own form. The ancestors and the "…" item hide, the back link shows, and the
     current page is the trail's last item. */
  @media (width < 640px) {
    .cinder-breadcrumbs[data-cinder-phone-below='640px'] {
      display: flex;
      align-items: center;
      gap: var(--cinder-space-2);
      min-inline-size: 0;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'] .cinder-breadcrumbs__back {
      display: inline-flex;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'] .cinder-breadcrumbs__back-separator {
      display: inline;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px']
      .cinder-breadcrumbs__item:not(:last-child) {
      display: none;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'] .cinder-breadcrumbs__list {
      flex: 1 100000 auto;
      min-inline-size: 3rem;
      font-size: var(--cinder-text-base);
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'] .cinder-breadcrumbs__current {
      font-weight: var(--cinder-font-semibold);
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'][data-cinder-phone-layout='stacked'] {
      flex-direction: column;
      align-items: flex-start;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'][data-cinder-phone-layout='stacked']
      .cinder-breadcrumbs__back {
      flex: none;
      max-inline-size: 100%;
      min-block-size: var(--cinder-touch-target-min);
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'][data-cinder-phone-layout='stacked']
      .cinder-breadcrumbs__back-separator {
      display: none;
    }

    .cinder-breadcrumbs[data-cinder-phone-below='640px'][data-cinder-phone-layout='stacked']
      .cinder-breadcrumbs__list {
      flex: none;
      align-self: stretch;
      min-inline-size: 0;
    }
  }
}
