@layer components {
  :root {
    /* 408px up to 1440, a quarter of a wider window up to 568px, and all of a phone's. */
    --messages-sidebar-width: clamp(408px, 25vw + 8px, 568px);
    --messages-sidebar-duration: var(--duration-slow);

    @media (width < 480px) {
      --messages-sidebar-width: 100vw;
    }
  }

  /* From 768 the open sidebar takes the right edge and the page narrows beside it; under that it lies over the page. */
  @media (width >= 768px) {
    html[data-sidebar-open] body:has(> .messages-sidebar) {
      padding-inline-end: var(--messages-sidebar-width);

      .bar {
        inset-inline-end: var(--messages-sidebar-width);
      }
    }
  }

  /* Under 1200 what is left is narrower than the 800 a sheet stands free in, so the sheet fills it as it does on a phone. */
  @media (800px <= width < 1200px) {
    html[data-sidebar-open] body:has(> .messages-sidebar) {
      --sheet-gutter: 0px;
      --sheet-padding: 0.85rem; /* 16px */
    }
  }

  .messages-sidebar {
    display: contents;
  }

  /* On the page's own ground, set off by a faint line only where the page beside it is narrow. */
  .messages-sidebar__pane {
    background-color: var(--color-page);
    block-size: 100dvh;
    box-shadow: inset 1px 0 var(--tint-ink-5);
    display: flex;
    flex-direction: column;
    inline-size: var(--messages-sidebar-width);
    inset: 0 0 0 auto;
    outline: none;
    padding-block-end: env(safe-area-inset-bottom);
    position: fixed;
    transition: translate var(--messages-sidebar-duration) var(--ease-out-expo), visibility var(--messages-sidebar-duration);
    translate: 100% 0;
    visibility: hidden;
    z-index: calc(var(--z-tray) + 3);

    [data-sidebar-open] & {
      translate: 0 0;
      visibility: visible;
    }

    @media (width >= 1176px) {
      box-shadow: none;
    }

    @media print {
      display: none;
    }
  }

  .messages-sidebar__frame {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-block-size: 0;
    padding-inline-start: 8px;
  }

  /* The top bar's height with no line under it: the way back on a subpage at its start, a plain × at its end, and the pill
     between them, moved from the top bar. */
  .messages-sidebar__bar {
    align-items: center;
    block-size: var(--top-bar-height);
    display: flex;
    flex: none;
    gap: 8px;
    padding-inline: 0 8px;
  }

  .messages-sidebar__back,
  .messages-sidebar__close {
    --btn-background: transparent;
    --btn-color: var(--color-ink-dark);
    --btn-size: var(--control-size);

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

  .messages-sidebar__close {
    margin-inline-start: auto;
  }

  /* The pill's own element, raised over the sidebar with the bar it sits in. Where the sidebar lies over the page, it hides the top bar. */
  html[data-sidebar-open] body:has(> .messages-sidebar) {
    .header:has(> .site-header) {
      @media (width < 768px) {
        visibility: hidden;
      }

      &:not(:has(dialog[open])) {
        z-index: calc(var(--z-tray) + 4);
      }
    }

    .site-header__messages {
      --bar-merge: 0;
      --messages-pill-start: 8px;

      inset-block-start: calc(var(--custom-safe-inset-top) + (var(--top-bar-height) - var(--control-size)) / 2);
      inset-inline-start: calc(100% - var(--messages-sidebar-width) + var(--messages-pill-start));
      max-inline-size: calc(var(--messages-sidebar-width) - var(--messages-pill-start) - var(--control-size) - 16px);
      position: fixed;
      visibility: visible;
    }

    &:has(.messages-sidebar__bar--subpage) .site-header__messages {
      --messages-pill-start: calc(var(--control-size) + 16px);
    }
  }

  /* A subpage's name, under the bar. */
  .messages-sidebar__heading-row {
    align-items: center;
    display: flex;
    flex: none;
    gap: 8px;
    padding: 0 8px 8px;
  }

  /* Its menu is quiet, as the strip's actions are. */
  .messages-sidebar__heading-row > .popup-anchor > .btn {
    --btn-background: transparent;
    --btn-border-color: transparent;
    --btn-hover-amount: 8%;
    --btn-size: var(--control-size);
  }

  .messages-sidebar__title {
    flex: 1 1 auto;
    font-size: 1.125rem; /* 18px */
    line-height: 1.15;
    margin: 0;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    @media (min-width: 100ch) {
      font-size: 0.95rem;
    }
  }

  .messages-sidebar__scroll {
    flex: 1 1 auto;
    min-block-size: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 8px var(--block-space) 0;

    /* Starting a Ping or renaming one, in from the edge with the subpage's name. */
    > form {
      padding: 4px 0 12px 8px;
    }
  }

  .messages-sidebar__skeleton {
    background: repeating-linear-gradient(to bottom, var(--color-ink-lightest) 0 2.5rem, transparent 2.5rem 3rem);
    border-radius: 0.5em;
    flex: 1 1 auto;
    margin: var(--block-space) var(--inline-space);
    mask-image: linear-gradient(to bottom, oklch(0% 0 0) 0, oklch(0% 0 0 / 0) 60%);
  }

  .messages-sidebar__none {
    margin: var(--block-space-half) var(--inline-space);
  }

  /* Ping someone: eight 40px faces to a row, each named under it in 12px, wrapping onto more rows past eight. */
  .directs {
    --avatar-size: 40px;

    display: grid;
    gap: 2px;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    padding: 2px;
  }

  .direct {
    border-radius: var(--radius-sm);
    color: var(--color-ink-dark);
    display: grid;
    gap: 2px;
    justify-items: center;
    min-inline-size: 0;
    padding: 6px 4px 2px;
    text-decoration: none;

    .avatar {
      font-size: 14px;
    }

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--tint-ink-10);
        border-radius: var(--radius-md);
      }
    }
  }

  .direct__name {
    font-size: 12px;
    max-inline-size: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Conversations, and the firms to pick from */
  .messages-sidebar__scroll .row {
    > .row__body {
      padding-block: calc(var(--block-space) / 3);
    }

    .row__title {
      font-size: var(--text-small);
    }

    .row__meta {
      font-size: var(--text-x-small);
    }
  }

  /* Unread Pings under the faces read as the notifications under them do: on the sidebar's ground with no rules, a 48px face,
     the name in 14/600 over two lines of the last message, shaded under the pointer. */
  .messages-sidebar__conversations {
    --border: 0 none;
    --rows-bg: transparent;
    --rows-border-size: 0;
    --rows-margin: 0;

    .messages-sidebar__conversation.unread {
      --avatar-size: 48px;
      --row-padding-block: 8px;
      --row-padding-inline: 8px;

      border-radius: var(--radius-md);

      > .row__body {
        gap: 12px;
      }

      .row__title {
        font-size: 14px;
        font-weight: 600;
      }

      .row__meta {
        color: var(--color-ink);
        font-size: 14px;
      }

      .messages-sidebar__when {
        font-size: 12px;
      }

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

  /* A section's name in 14/600, in from the edge as its rows are. */
  .messages-sidebar__heading {
    color: var(--color-warning-text);
    font-size: var(--text-control);
    font-weight: 600;
    line-height: 1.5;
    margin: 0;
    padding: 8px 0 4px 8px;
  }

  .messages-sidebar__conversation {
    --avatar-size: 1.5rem;

    > .row__body {
      align-items: center;
      flex-direction: row;
      gap: var(--inline-space);
    }

    .avatar {
      font-size: var(--text-x-small);
    }

    .row__title {
      font-weight: 500;
    }

    /* The conversation open on the page, read as it is shown. */
    &:has([aria-current]) {
      background-color: var(--color-ink-lightest);

      .messages-sidebar__count {
        display: none;
      }
    }

    &.unread {
      --avatar-size: 2rem;

      > .row__body {
        align-items: start;
      }

      .row__title {
        font-weight: 800;
      }

      .row__meta {
        overflow-wrap: anywhere;
      }
    }
  }

  .messages-sidebar__reading {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-inline-size: 0;
  }

  .messages-sidebar__end {
    align-items: end;
    align-self: start;
    display: flex;
    flex: none;
    flex-direction: column;
    gap: 0.3em;
  }

  .messages-sidebar__when {
    color: var(--color-ink-dark);
    font-size: var(--text-x-small);
    padding-block-start: 0.1rem;
  }

  .messages-sidebar__count {
    --count-color: var(--color-warning-filled);
  }

  /* Notifications under the conversations: new ones, then those already seen, each part gone while empty. */
  .messages-sidebar__notifications {
    .notifications-list {
      &:not(:has(.card--notification)) {
        display: none;
      }

      .tray__item {
        border-block-end: 0;
      }

      .card--notification {
        box-shadow: none;
        margin-block-start: 0;
      }
    }
  }

  /* "New for you" and "Mark all read" on one line, the action small and grey at its end. */
  .messages-sidebar__notifications .notifications-list__header {
    align-items: baseline;
    margin: 0;
    padding-inline-end: 8px;
  }

  .messages-sidebar__mark-all {
    --btn-padding: 0;

    color: var(--color-text-subtle);
    font-size: 12px;
  }

  .messages-sidebar__heading--read {
    color: var(--color-meta);
  }

  .messages-sidebar__more {
    margin: var(--block-space-half) var(--inline-space) 0;
  }
}
