@layer components {
  /* A tray is a tab on the bottom bar and the sheet it opens over the page's.
  /* ------------------------------------------------------------------------ */

  /* The tabs stand side by side in the middle of the bar; the row lets through what is not a tab. */
  .trays {
    --tray-gutter: 1.5rem;
    --tray-inset-end: 0px;

    block-size: var(--footer-height);
    display: flex;
    inset-block: auto env(safe-area-inset-bottom);
    inset-inline: 0 var(--tray-inset-end);
    justify-content: center;
    pointer-events: none;
    position: fixed;
    z-index: var(--z-tray);

    > * {
      pointer-events: auto;
    }

    /* Beside the open messages, the tabs and the sheet centre on what is left of the window. */
    @media (min-width: 640px) {
      html[data-sidebar-open] body:has(> .messages-sidebar) & {
        --tray-inset-end: var(--messages-sidebar-width);
      }
    }

    /* Under 1200 what the messages leave is narrower than a sheet stands free in, and the tray fills it as on a phone. */
    @media (640px <= width < 1200px) {
      html[data-sidebar-open] body:has(> .messages-sidebar) & {
        --tray-gutter: 0px;
      }
    }
  }

  .tray {
    align-items: center;
    display: flex;
  }

  .tray--pins {
    view-transition-name: tray-pins;
  }

  .tray--assignments {
    view-transition-name: tray-assignments;
  }

  .tray--events {
    view-transition-name: tray-events;
  }

  ::view-transition-group(tray-pins),
  ::view-transition-group(tray-assignments),
  ::view-transition-group(tray-events) {
    z-index: 100;
  }

  /* The sheet
  /* ------------------------------------------------------------------------ */

  /* Over the page's own sheet, from the top bar to the bottom bar: the window's width on a phone, the standard
     sheet's from 800. Nothing behind it dims. */
  .tray__dialog {
    --tray-corner: 0.5rem;
    --tray-padding: 1rem;

    background-color: var(--color-canvas);
    box-shadow: 0 0 30px oklch(var(--lch-black) / 8%);
    color: var(--color-ink);
    flex-direction: column;
    block-size: auto;
    inline-size: auto;
    inset:
      calc(var(--top-bar-height) + var(--custom-safe-inset-top))
      var(--tray-inset-end)
      calc(var(--footer-height) + env(safe-area-inset-bottom))
      0;
    margin: 0;
    max-block-size: none;
    max-inline-size: none;
    opacity: 0;
    overflow: auto;
    overscroll-behavior: contain;
    padding: 0.5rem var(--tray-padding) var(--tray-padding);
    position: fixed;
    transition: 250ms var(--ease-out-overshoot-subtle) allow-discrete;
    transition-property: display, opacity, overlay, translate;
    translate: 0 1rem;

    &[open] {
      display: flex;
      opacity: 1;
      translate: none;
    }

    @starting-style {
      &[open] {
        opacity: 0;
        translate: 0 1rem;
      }
    }

    &:focus-visible {
      outline: 0;
    }

    /* Off a phone it keeps 24px of the page on either side. */
    @media (min-width: 640px) {
      inline-size: min(var(--sheet-size-standard), 100% - 2 * var(--tray-gutter) - var(--tray-inset-end));
      margin-inline: auto;
    }

    /* Until the page is a sheet on a tinted ground, a hairline tells the tray from the page of its own colour. */
    @media (640px <= width < 800px) {
      border-inline: var(--border);
    }

    @media (min-width: 800px) {
      --tray-corner: 1rem;
      --tray-padding: 2rem;
    }

    @media (min-width: 100ch) {
      --tray-padding: 1.7rem; /* 32px */

      inline-size: min(var(--sheet-size-standard), 100% - var(--sheet-gutter) - var(--tray-inset-end));
    }
  }

  /* Its buttons in the top corner, its name under them at the start, as a page's. */
  .tray__header {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    font-size: var(--text-control);
    gap: 0 var(--inline-space-half);
    justify-content: end;
    margin-block-end: calc(var(--page-header-space) / 2);
    margin-inline-end: calc(var(--tray-corner) - var(--tray-padding));
    order: -1;

    .btn--circle {
      --btn-size: var(--control-size);
    }

    [open] & [aria-selected] .btn {
      outline: var(--focus-ring-size) solid var(--focus-ring-color);
      outline-offset: var(--focus-ring-offset);
    }
  }

  .tray__title {
    flex: 0 0 100%;
    font-size: var(--text-page-title);
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1.15;
    margin: 0.5rem 0 0;
    order: 1;
  }

  /* A filter row under the name sits as close as under a page's, and stays out of the page's own transition. */
  .tray__dialog:has(.filter-bar) .tray__header {
    margin-block-end: var(--inline-space);
  }

  .tray__dialog .filter-bar {
    view-transition-name: none;
  }

  /* What the reader did has a filter wide enough to type a name in, and each day's rule runs to the tray's edge. */
  .tray--events {
    .filter-bar {
      --filter-input-width: calc(var(--filter-field-size) * 1.6); /* 240px */
    }

    .history {
      padding-inline-end: 0;
    }
  }

  /* What a tray lists
  /* ------------------------------------------------------------------------ */

  .tray__item {
    position: relative;

    .bubble {
      display: none;
    }
  }

  /* Under the pointer what a tray lists is picked out in pale blue, ringed in blue. */
  @media (any-hover: hover) {
    .tray__dialog :is(.row:has(> :is(.inbox__link, a.row__body):hover), .tray__card:hover) {
      background-color: var(--color-selected-light);
      outline: 2px solid var(--color-link);
    }
  }

  /* A notification is still the card it was: its own height, one under another. */
  .tray__item .card {
    --card-padding-block: 1.5ch;
    --card-padding-inline: 1.5ch;
    --text-xx-large: 2em;

    border: 0;
    border-radius: var(--radius-md);
    box-shadow: 0 0 0 1px var(--color-ink-lighter);
    font-size: var(--text-normal);
    margin-block-start: var(--block-space-half);
    view-transition-name: unset !important;

    .card__background {
      display: none;
    }

    .card__body {
      margin-block-start: 0.2em;
      padding-block-end: 0;
    }

    .card__board {
      padding-block: 0.25ch;
    }

    .card__title {
      --lines: 1;

      font-size: var(--text-small);
      font-weight: bold;
      min-block-size: 0;
    }
  }

  /* The tray shows ten pins. */
  .tray__item--pin:nth-child(1n + 11) {
    display: none;
  }
}
