@layer components {
  /* A conversation holds the screen under the strip: its lines scroll inside it and the composer stays at the foot. Under 20rem a landscape phone would leave the lines no room, so the page scrolls instead. */
  body.chat-page {
    block-size: 100dvh;
    grid-template-rows: minmax(0, 1fr) auto;

    #global-container {
      min-block-size: 0;
    }

    #main {
      display: flex;
      flex: 1 1 auto;
      flex-direction: column;
      min-block-size: 20rem;
      padding-block-end: calc(var(--footer-height) + env(safe-area-inset-bottom));
    }
  }

  .chat,
  .thread {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-block-size: 0;
    text-align: start;
  }

  /* Bubbles in 16px text, author and time over the words at 12.8px, the 40px avatar outside the bubble, all a little in from the composer's edges. */
  .messages {
    --message-avatar-size: 2.5rem;  /* 40px */
    --message-column-gap: 0.25rem;
    --message-inset: 1rem;
    --message-space: 1.25rem;
    --message-text: 1rem;           /* 16px */
    --message-meta-text: 0.8rem;    /* 12.8px */
    --reaction-size: 2rem;          /* 32px */
    --reaction-text: 16px;
    --reaction-emoji: 20px;

    @media (min-width: 100ch) {
      --message-avatar-size: 2.1rem;
      --message-column-gap: 0.21rem;
      --message-inset: 0.85rem;
      --message-space: 1.05rem;
      --message-text: 0.842rem;
      --message-meta-text: 0.674rem;
      --reaction-size: 1.6875rem;
    }

    display: flex;
    flex: 1 1 auto;
    flex-direction: column-reverse;
    gap: var(--message-space);
    min-block-size: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-block: var(--block-space);
  }

  .message {
    --avatar-size: var(--message-avatar-size);

    column-gap: var(--message-column-gap);
    display: grid;
    flex: none;
    grid-template-areas:
      "sep sep"
      "avatar body";
    grid-template-columns: var(--message-avatar-size) minmax(0, 1fr);
    padding-inline: var(--message-inset);
  }

  /* A new day opens with the timeline's dark date pill, centred on a hairline. */
  .message__day-separator {
    align-items: center;
    column-gap: 1.714em;            /* 24px */
    display: none;
    font-size: var(--text-control);
    font-weight: 800;
    grid-area: sep;
    grid-template-columns: 1fr auto 1fr;
    line-height: 1;
    margin: 0 0 var(--message-space);
    text-align: center;
    text-transform: uppercase;

    &::before,
    &::after {
      border-block-start: 1px solid var(--color-ink-light);
      content: "";
    }

    .message--first-of-day & {
      display: grid;
    }
  }

  .message__avatar {
    align-self: start;
    grid-area: avatar;
  }

  .message__body {
    display: flex;
    grid-area: body;
    justify-self: start;
    max-inline-size: min(80ch, 85%);
    min-inline-size: 0;
    position: relative;

    @media (width < 640px) {
      max-inline-size: 100%;
    }
  }

  .message__body-content {
    background-color: var(--color-meta-fill);
    border-radius: var(--radius-lg);
    font-size: var(--message-text);
    inline-size: fit-content;
    line-height: 1.5;
    max-inline-size: 100%;
    min-inline-size: 0;
    padding: 0.55em 1em;
  }

  .message__meta {
    align-items: center;
    column-gap: calc(var(--message-column-gap) / 2);
    display: flex;
    line-height: 1.5;
    min-inline-size: 0;
  }

  .message__heading {
    align-items: baseline;
    column-gap: 0.5ch;
    display: inline-flex;
    font-size: var(--message-meta-text);
    font-weight: 400;
    line-height: inherit;
    margin: 0;
    min-inline-size: 0;
  }

  .message__author {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message__permalink {
    color: var(--color-ink-dark);
    text-decoration: none;
    white-space: nowrap;

    @media (any-hover: hover) {
      &:hover {
        text-decoration: underline;
      }
    }
  }

  .message__presentation {
    overflow-wrap: anywhere;
    white-space: pre-wrap;
  }

  /* What a reply quotes stands apart behind a thick rule. */
  .message__quote {
    border-inline-start: 3px solid var(--color-ink);
    margin: 0.5em 0;
    padding-inline-start: 1.25em;
  }

  /* The ⋯ sits beside the time; its menu hangs from the bubble's row, not the button, so it stays inside the scrolling thread at any width. */
  .message .message__actions {
    flex: none;
    position: static;
  }

  /* Two classes, to outrank the circle buttons.css makes of any labelled icon button. */
  .message .message__options-btn {
    --btn-background: transparent;
    --btn-size: 1.5em;
    --icon-size: 0.9em;

    margin-block: -0.25em;

    /* Unseen until the line is under the pointer, then a little faint until the ⋯ itself is. */
    @media (any-hover: hover) {
      opacity: 0;
      transition: opacity var(--duration-medium) ease-in-out;

      .message:hover & {
        opacity: 0.75;
      }

      .message &:is(:hover, :focus-visible),
      .message:has(dialog[open]) & {
        opacity: 1;
      }
    }

    @media (any-hover: none) {
      color: var(--color-ink-dark);
    }
  }

  .message .message__actions-menu {
    inset: auto auto 100% 0;
    margin: 0 0 0.25rem;
    translate: none;

    .popup__list + .popup__list {
      border-block-start: 1px solid var(--color-ink-lighter);
      margin-block-start: 0.25rem;
      padding-block-start: 0.25rem;
    }
  }

  .message__edited {
    color: var(--color-ink-dark);
    white-space: nowrap;
  }

  /* A firm's message is changed or taken back by its writer alone, and only for a while. */
  .thread:not(.ping) .message:not(.message--editable) :is(.message__edit, .message__retract) {
    display: none;
  }

  .message__retract-form {
    display: flex;
    flex: 1 1 auto;
    min-inline-size: 0;
  }

  /* Two classes, to outrank a menu row's own flex. */
  .message .message__reacting {
    display: grid;
    gap: 0.25rem;
    grid-template-columns: repeat(4, auto);
    justify-content: start;
    padding: 0.25rem calc(var(--popup-item-padding-inline) / 2);

    @media (any-hover: hover) {
      &:hover {
        background-color: transparent;
      }
    }
  }

  .message__react-btn {
    --btn-border-radius: 50%;

    block-size: 2.25em;
    inline-size: 2.25em;
    padding: 0;
  }

  /* While a line is edited its bubble takes the line's full width, so the words have room. */
  .message__body:has(.message__body-content--editing) {
    inline-size: 100%;

    .message__body-content {
      flex-grow: 1;
    }
  }

  .message__edit-form {
    display: flex;
    flex-direction: column;
    gap: var(--inline-space-half);
    margin-block: var(--inline-space-half);

    textarea {
      inline-size: 100%;
      resize: none;
    }
  }

  .message__edit-actions {
    display: flex;
    gap: var(--inline-space-half);
  }

  /* Boosts under a line's words, 8px below them, then a round button to add one. */
  .message__reactions {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-block: 8px 0;

    &:not(:has(.reaction)) {
      display: none;
    }
  }

  /* A boost is a pale sand pill as tall as a control, ringed: the booster's face 2px in from its edge, then what they sent
     in 16px, an emoji at 20px. */
  .reaction {
    --avatar-size: calc(var(--reaction-size) - 4px);

    align-items: center;
    background-color: var(--color-chip-fill);
    block-size: var(--reaction-size);
    border-radius: 99rem;
    box-shadow: 0 0 0 1px var(--tint-ink-5);
    display: inline-flex;
    font-size: var(--reaction-text);
    gap: 4px;
    line-height: 1;
    padding: 2px 10px 2px 2px;

    .avatar {
      font-size: 11px;
    }
  }

  .reaction__content {
    font-size: var(--reaction-emoji);
  }

  /* Only your own boost has a ×. */
  .reaction__form {
    display: none;

    .reaction--mine & {
      display: inline-flex;
    }
  }

  /* The × takes no room until the pointer is on the boost or Tab reaches it, and stays at rest on a touch screen. */
  .reaction__remove {
    --btn-background: transparent;
    --icon-size: 0.6em;

    block-size: 1.2em;
    inline-size: 1.2em;
    margin-inline-end: -4px;

    @media (any-hover: hover) {
      .reaction:not(:hover) &:not(:focus-visible) {
        inline-size: 0;
        margin-inline: -4px 0;
        opacity: 0;
      }
    }
  }

  .message__boost-btn {
    --btn-background: var(--tint-ink-5);
    --btn-border-size: 0;
    --btn-padding: 0;
    --icon-size: 60%;

    block-size: var(--reaction-size);
    border-radius: 50%;
    box-shadow: 0 0 0 1px var(--tint-ink-5);
    inline-size: var(--reaction-size);
  }

  /* After the boosts, even one just added; its eight boosts open over it in a row. */
  .message__boost {
    display: flex;
    order: 1;
  }

  .message .message__boost-menu {
    inset-block: auto calc(100% + 0.25rem);

    .message__reacting {
      grid-template-columns: repeat(8, auto);
    }
  }

  .message--highlighted .message__body-content {
    animation: tint-fade var(--duration-slow) ease-out 1s backwards;
  }

  .composer {
    flex: none;
    padding-block: var(--block-space-half) var(--block-space);
  }

  .composer__note {
    margin-block: 0 var(--block-space-half);
  }

  /* A 48px box, 2px blue around it while written in. Where it shows its round send button, at its end, the field centres on
     the button's line and grows above it; a Ping's shows one only where nothing can hover, as Enter sends. */
  .composer__input {
    --input-background: var(--color-canvas);
    --input-border-color: var(--color-ink-light);
    --input-border-radius: var(--radius-lg);
    --input-padding: 0.4rem 0.4rem 0.4rem var(--block-space);

    align-items: end;
    cursor: text;
    font-size: 1rem;                /* 16px */
    min-block-size: 48px;

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

  .composer__field {
    align-self: center;
  }

  .composer__send {
    --btn-border-radius: 50%;
    --btn-size: 2rem;               /* 32px */

    flex: none;

    @media (min-width: 100ch) {
      --btn-size: 1.6875rem;
    }
  }

  /* Inline, the field would leave a descender's room under its one line. */
  .composer__input .autoresize__textarea {
    display: block;
  }
}
