@layer components {
  /* What was said about a record, under a rule across the sheet: each comment an avatar, who and when, then the words; what was done to the record as quiet lines between them. */
  .comments {
    --avatar-size: 2.5rem;                              /* 40px */
    --comment-gap: 0.75rem;                             /* 12px */
    --comment-padding-block: 1rem;                      /* 16px */

    border-block-start: var(--border);
    display: flex;
    flex-direction: column;
    font-size: 1rem;                                    /* 16px */
    line-height: 1.5;
    margin-block-start: var(--block-space-double);
    margin-inline: calc(-1 * var(--sheet-padding));
    padding-inline: var(--sheet-padding);
    text-align: start;

    @media (min-width: 100ch) {
      --avatar-size: 2.95rem;                           /* 56px */
      --comment-gap: 0.842rem;                          /* 16px */
      --comment-padding-block: 1.263rem;                /* 24px */

      font-size: 1.05rem;                               /* 20px */
      margin-block-start: 3.37rem;                      /* 64px */
    }
  }

  .comment {
    /* Distinguish from the .comment class used for code formatting without extra specificity */
    &:where(.comments &) {
      align-items: start;
      border-block-end: var(--border);
      display: flex;
      gap: var(--comment-gap);
      padding-block: var(--comment-padding-block);
    }

    /* A line of the record's history: no avatar, and when, what and the way to the rest on one line in the text's column. */
    .comment-by-system & {
      color: var(--color-meta);
      font-size: var(--text-small);
      padding-block: calc(var(--comment-padding-block) / 2);
      padding-inline-start: calc(var(--avatar-size) + var(--comment-gap));

      .comment__avatar,
      .comment__author strong {
        display: none;
      }

      .comment__content {
        align-items: baseline;
        column-gap: var(--inline-space);
        flex-direction: row;
        flex-wrap: wrap;
      }

      .comment__author {
        display: contents;

        h3 {
          margin-inline-end: 0;
        }
      }

      .comment__permalink-title {
        margin: 0;
      }

      .comment__body {
        color: var(--color-ink-dark);
        flex: 1 1 20ch;
        order: 1;
      }

      .comment__history {
        order: 2;
      }
    }
  }

  .comment__avatar {
    font-size: 0.8em;
    margin: 0;
  }

  .comment__content {
    --btn-icon-size: 1.2rem;
    --btn-size: var(--control-size);

    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-inline-size: 0;
    word-wrap: break-word;
  }

  .comment__author {
    align-items: center;
    display: flex;
    gap: var(--inline-space-half);

    h3 {
      font-size: inherit;
      margin: 0 auto 0 0;
    }

    .btn {
      font-weight: inherit;
    }
  }

  .comment__permalink-title {
    color: var(--color-meta);
    font-size: var(--text-small);
    margin-inline-start: 0.5ch;
    text-decoration: none;

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

  .comment__body {
    .action-text-content {
      > action-text-attachment:first-child figure {
        margin-block-start: 0.5ch;
      }

      > :last-child {
        margin-block-end: 0;
      }
    }
  }

  .comment__history {
    background-color: transparent;
    display: none;
    font-size: var(--text-control);

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

  /* The box to write in sits in the text's column, under the last comment's rule. */
  .comment--new {
    border-block-end: 0;

    .comment__content {
      padding-block-start: calc((var(--avatar-size) - 2rem) / 2);
    }

    /* Once a comment is posted the box comes back inside itself. */
    > .comment--new {
      flex: 1 1 auto;
      padding-block: 0;
    }
  }

  .comments__subscribers {
    --avatar-size: 2rem;

    gap: var(--block-space-half);
    padding-block-start: var(--block-space-double);

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

  .comment-by-system {
    display: none;
    transition: var(--dialog-duration) allow-discrete;
    transition-property: display;

    .comments--system-expanded & {
      display: contents;
    }
  }

  /* Show the last system comment */
  :nth-last-child(1 of .comment-by-system) {
    display: contents;

    .comment__history {
      display: inline-flex;
    }
  }

  /* Hide the "Show history" button if there's only one system comment */
  :nth-child(1 of .comment-by-system) {
    .comment__history {
      display: none;
    }
  }
}
