@layer components {
  /* Base: a source list on the window colour beside the content card. It
     collapses to a rail of icons. Everything that changes between the two
     moves on the panel's timing (--duration-panel, --ease-panel): the width,
     the group headers folding away, the separators opening, the tree indent.
     Labels fade out first when it closes and in last when it opens, so text
     is never seen clipped mid-word.
  /* -------------------------------------------------------------------------- */

  .sidebar {
    --sidebar-row-size: 28px;
    --sidebar-row-padding: 6px;
    --sidebar-hover: oklch(var(--lch-ink-darkest) / 6%);
    --sidebar-motion: var(--duration-panel) var(--ease-panel);
    --sidebar-fade-out: 80ms;
    --sidebar-fade-in: 100ms;

    align-self: flex-start;
    block-size: 100dvh;
    color: var(--color-ink);
    cursor: default;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    font-size: var(--text-small);
    gap: var(--space-8);
    inline-size: var(--sidebar-width);
    inset-block-start: 0;
    /* 13/16 exactly: 1.23 lands on 15.99px and nudges rows off whole pixels */
    line-height: 16px;
    padding:
      calc(var(--window-inset) + var(--custom-safe-inset-top))
      var(--window-inset)
      calc(var(--window-inset) + var(--custom-safe-inset-bottom))
      calc(var(--window-inset) + var(--custom-safe-inset-left));
    position: sticky;
    transition: inline-size var(--sidebar-motion);
    user-select: none;
    z-index: var(--z-nav);

    @media (prefers-reduced-motion: reduce) {
      transition: none;
    }
  }

  /* Elements
  /* -------------------------------------------------------------------------- */

  .sidebar__header {
    align-items: center;
    display: flex;
    gap: 2px;
  }

  /* The catalogue's sections, then the global pages; scrolls if they overflow */
  .sidebar__body {
    flex: 1;
    min-block-size: 0;
    overflow-x: hidden;
    overflow-y: auto;
  }

  /* A hairline between the catalogue's sections and the global pages, fading
     out to the right */
  .sidebar__sections + .sidebar__global::before {
    background-image: linear-gradient(to right, var(--color-border), transparent);
    block-size: 1px;
    content: "";
    display: block;
    margin: 6px var(--sidebar-row-padding);
    transition: margin var(--sidebar-motion);
  }

  .sidebar__footer {
    display: flex;
    flex-direction: column;
    gap: 1px;
    margin-block-start: auto;
  }

  .sidebar__list {
    display: flex;
    flex-direction: column;
    gap: 1px;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  /* One row: icon, label, optional trailing glyph */
  /* Grey at rest; the current page and the hovered row in full ink */
  .sidebar__row {
    align-items: center;
    background: none;
    border: 0;
    border-radius: var(--border-radius);
    color: var(--color-ink-muted);
    cursor: default;
    display: flex;
    font: inherit;
    gap: var(--space-8);
    inline-size: 100%;
    min-block-size: var(--sidebar-row-size);
    overflow: hidden;
    padding: var(--sidebar-row-padding);
    text-align: start;
    white-space: nowrap;

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--sidebar-hover);
        color: var(--color-ink);
      }
    }

    /* The highlight pair: accent tint inside an accent hairline */
    &[aria-current="page"] {
      background-color: var(--color-accent-fill);
      color: var(--color-ink);
      box-shadow: inset 0 0 0 1px var(--color-accent);
    }
  }

  .sidebar__icon {
    --icon-size: 16px;
  }

  .sidebar__label {
    flex: 1;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    /* Opening: fade in over the end of the slide */
    transition: opacity var(--sidebar-fade-in) ease calc(var(--duration-panel) - var(--sidebar-fade-in));
  }

  .sidebar__trailing {
    --icon-size: 12px;

    color: var(--color-ink-muted);
    transition:
      opacity var(--sidebar-fade-in) ease calc(var(--duration-panel) - var(--sidebar-fade-in)),
      rotate var(--duration-fast) ease-in-out;
  }

  /* Keyboard hint, a step dimmer than the label */
  .sidebar__shortcut {
    font-family: inherit;
    font-size: 90%;
    letter-spacing: -0.01em;
  }

  /* The theme toggle names the theme it switches to */
  .sidebar__theme--to-light { display: none; }

  html[data-theme="dark"] .sidebar__theme-toggle {
    .sidebar__theme--to-dark { display: none; }
    .sidebar__theme--to-light { display: inline-block; }
  }

  @media (prefers-color-scheme: dark) {
    html:not([data-theme]) .sidebar__theme-toggle {
      .sidebar__theme--to-dark { display: none; }
      .sidebar__theme--to-light { display: inline-block; }
    }
  }

  /* Groups: a header that opens and closes its rows, joined by a tree line
  /* -------------------------------------------------------------------------- */

  /* A fixed height, so the rail can fold it away smoothly. Restates the hover
     transitions from base.css, which this one would otherwise replace. */
  .sidebar__group-header {
    block-size: var(--sidebar-row-size);
    transition:
      block-size var(--sidebar-motion),
      min-block-size var(--sidebar-motion),
      padding-block var(--sidebar-motion),
      opacity var(--sidebar-fade-in) ease calc(var(--duration-panel) - var(--sidebar-fade-in)),
      visibility var(--duration-panel),
      background-color var(--duration-fast) ease,
      color var(--duration-fast) ease;

    /* A closed group stands in for the current page it hides */
    &[aria-expanded="false"][data-contains-current] {
      background-color: var(--color-accent-fill);
      color: var(--color-ink);
      box-shadow: inset 0 0 0 1px var(--color-accent);
    }
  }

  /* The rail's line above each group, closed while the sidebar is open. 1px
     tall with the hairline drawn inside, so the rows below stay on whole
     pixels. */
  .sidebar__group::before {
    background: linear-gradient(var(--color-border) 0 0) top / 100% var(--hairline) no-repeat;
    block-size: 0;
    content: "";
    display: block;
    opacity: 0;
    transition:
      block-size var(--sidebar-motion),
      margin-block var(--sidebar-motion),
      opacity var(--sidebar-motion);
  }

  .sidebar__group-caret {
    [aria-expanded="false"] > & {
      rotate: -90deg;
    }
  }

  .sidebar__group-body {
    display: grid;
    grid-template-rows: 1fr;
    transition:
      grid-template-rows var(--duration) ease-out,
      visibility var(--duration);

    > * {
      min-block-size: 0;
      overflow: hidden;
    }

    [aria-expanded="false"] + & {
      grid-template-rows: 0fr;
      visibility: hidden;
    }

    @media (prefers-reduced-motion: reduce) {
      transition: none;
    }
  }

  /* The line drops from under the header's icon and turns into the last row.
     Whole pixels, so the nested icons stay crisp on 1x displays. */
  .sidebar__tree {
    margin-inline-start: calc(var(--sidebar-row-padding) + 7px);
    padding-block: 1px 2px;
    padding-inline-start: 12px;
    position: relative;
    transition:
      margin-inline-start var(--sidebar-motion),
      padding var(--sidebar-motion);

    &::before {
      border-block-end: 1px solid var(--color-border);
      border-end-start-radius: 5px;
      border-inline-start: 1px solid var(--color-border);
      content: "";
      inline-size: 8px;
      inset-block: 0 calc(var(--sidebar-row-size) / 2 + 2px);
      inset-inline-start: 0;
      pointer-events: none;
      position: absolute;
      transition: opacity var(--sidebar-motion);
    }
  }

  /* Rail: icons only. Phones always get the rail, without the toggle.
  /* -------------------------------------------------------------------------- */

  .app[data-sidebar-state="collapsed"] {
    .sidebar { inline-size: var(--sidebar-rail-width); }
    /* Closing: fade out at once, before the slide clips the text */
    .sidebar__label,
    .sidebar__trailing { opacity: 0; transition: opacity var(--sidebar-fade-out) ease; }
    .sidebar__group-header { block-size: 0; min-block-size: 0; opacity: 0; padding-block: 0; visibility: hidden; }
    .sidebar__group-header { transition: block-size var(--sidebar-motion), min-block-size var(--sidebar-motion), padding-block var(--sidebar-motion), opacity var(--sidebar-fade-out) ease, visibility var(--duration-panel); }
    .sidebar__group::before { block-size: 1px; margin-block: 4px; opacity: 1; }
    .sidebar__group-body { grid-template-rows: 1fr; visibility: visible; }
    .sidebar__tree { margin-inline-start: 0; padding: 0; }
    .sidebar__tree::before { opacity: 0; }
    .sidebar__sections + .sidebar__global::before { margin: 4px 0; }
  }

  @media (max-width: 639px) {
    .app {
      .sidebar { inline-size: var(--sidebar-rail-width); }
      /* Closing: fade out at once, before the slide clips the text */
      .sidebar__label,
      .sidebar__trailing { opacity: 0; transition: opacity var(--sidebar-fade-out) ease; }
      .sidebar__group-header { block-size: 0; min-block-size: 0; opacity: 0; padding-block: 0; visibility: hidden; }
      .sidebar__group-header { transition: block-size var(--sidebar-motion), min-block-size var(--sidebar-motion), padding-block var(--sidebar-motion), opacity var(--sidebar-fade-out) ease, visibility var(--duration-panel); }
      .sidebar__group::before { block-size: 1px; margin-block: 4px; opacity: 1; }
      .sidebar__group-body { grid-template-rows: 1fr; visibility: visible; }
      .sidebar__tree { margin-inline-start: 0; padding: 0; }
      .sidebar__tree::before { opacity: 0; }
      .sidebar__sections + .sidebar__global::before { margin: 4px 0; }
      .sidebar__toggle { display: none; }
    }
  }

  /* Reduced motion: every change between open and rail happens at once */
  @media (prefers-reduced-motion: reduce) {
    .sidebar,
    .sidebar *,
    .sidebar *::before {
      transition: none !important;
    }
  }

  @media print {
    .sidebar { display: none; }
  }
}
