@layer components {
  /* Off the sheet a list is a card of its own. Inside a surface that already is one, or on the sheet, it has no card. */
  :is(.sheet, .tray__dialog, .panel, .record, .project__sheet, .tool__panel, .paper__section, .suggestions__list, .messages-sidebar__pane),
  .rows .rows {
    --rows-border-radius: 0;
    --rows-shadow: none;
    --rows-side-size: 0;
  }

  .rows {
    --row-padding-inline: calc(var(--inline-space) * 1.5);
    --rows-corner: max(0px, calc(var(--rows-border-radius, 1em) - var(--rows-border-size, 1px)));

    list-style: none;
    margin-block: var(--rows-margin, var(--block-space));
    padding: 0;
    background-color: var(--rows-bg, var(--color-canvas));
    border: solid var(--color-ink-lighter);
    border-width: var(--rows-border-size, 1px) var(--rows-side-size, var(--rows-border-size, 1px));
    border-radius: var(--rows-border-radius, 1em);
    box-shadow: var(--rows-shadow, var(--shadow));

    /* First on a page, the list starts where any page's first block does, under the title's own space. */
    .page > &:first-child {
      margin-block-start: 0;
    }

    &:not(:has(.row:not([hidden]))) {
      --rows-bg: transparent;
      --rows-border-size: 0;
      --rows-shadow: none;
    }

    /* A list within has no corner to take, so the card cuts what it holds, except a floating suggestions list. */
    &:has(.rows):not(:has(.suggestions__list)) {
      overflow: hidden;
    }

    @media print {
      --rows-border-size: 0;
      --rows-shadow: none;
    }
  }

  /* A section's title over the rows it groups, with a rule running on after it. The list under it drops its own top line. */
  .rows__heading {
    align-items: center;
    display: flex;
    font-size: var(--text-section);
    font-weight: 800;
    gap: var(--inline-space);
    line-height: 1.15;
    margin: 0;
    padding-inline: calc(var(--inline-space) * 1.5);

    &::after {
      border-block-start: var(--border);
      content: "";
      flex: 1;
    }

    + .rows {
      border-block-start-width: 0;
      margin-block-start: var(--block-space-half);
    }
  }

  /* What the section holds, said quietly after its title and before the rule. */
  .rows__heading-meta {
    color: var(--color-meta);
    font-size: var(--text-control);
    font-weight: 400;
  }

  .rows__heading:has(> .rows__heading-meta) {
    flex-wrap: wrap;
    row-gap: 0.25em;
  }

  /* A heading among one list's rows, over those of a kind. A filter runs through every kind, so the headings go while one is typed. */
  .rows__divider {
    margin-block: var(--block-space) var(--block-space-half);

    [data-filtering] & {
      display: none;
    }
  }

  /* Rows that lead to another list are not what a filter searches, so they go while one is typed. */
  [data-filtering] .rows--links {
    display: none;
  }

  /* :has cannot sit inside :has, so the heading goes with its section when a filter leaves no row in it. */
  .rows__section:not(:has(.row:not([hidden]))) {
    display: none;
  }

  .row {
    --row-padding-block: calc(var(--block-space-half) * 0.75);

    display: flex;
    align-items: center;
    gap: var(--inline-space);
    margin: 0;
    padding-inline: var(--row-padding-inline);
    line-height: var(--leading-snug);
    outline: none;

    &[aria-selected="true"] {
      background-color: var(--color-ink-lightest);
    }

    /* A title alone keeps the half block, or its row would be little more than the line. */
    &:where(:not(:has(.row__meta:not(:empty)))) {
      --row-padding-block: var(--block-space-half);
    }

    &[hidden] {
      display: none;
    }
  }

  /* A row has no corner of its own: its ground runs edge to edge, and the rows at the ends take the card's. */
  .rows > :nth-child(1 of :not([hidden])) {
    border-start-start-radius: var(--rows-corner);
    border-start-end-radius: var(--rows-corner);
  }

  .rows > :nth-last-child(1 of :not([hidden])) {
    border-end-start-radius: var(--rows-corner);
    border-end-end-radius: var(--rows-corner);
  }

  /* A rule between two rows or groups, and none over the first one a filter leaves in view. */
  :is(.row, .rows__group):not([hidden]) ~ :is(.row, .rows__group) {
    border-block-start: var(--border);
  }

  /* A list has no end, so its rows are not all named for the view transition: only the one under the pointer
     or holding the focus is, which is the one being opened. It says its own name in --row-transition-name. */
  .row:is(:hover, :focus-within) {
    view-transition-name: var(--row-transition-name, none);
  }

  /* A group of rows inside a list, under a header of its own. Its rows are ruled as the list's own, with one
     rule under the header and no box around them. */
  .rows__group {
    --rows-border-size: 0;
    --rows-margin: 0;

    > .rows {
      border-block-start: var(--border);
    }
  }

  /* The title wraps and the link keeps the end, lined up with the rows' ends. A phone has no room for both, so
     there the link always takes a line of its own. */
  .rows__group-header {
    display: flex;
    align-items: center;
    gap: var(--inline-space-half) var(--inline-space);
    padding: var(--block-space-half) var(--row-padding-inline);

    > .btn {
      --btn-font-weight: normal;

      flex: none;
      margin-inline-start: auto;
      white-space: nowrap;
    }

    @media (max-width: 639px) {
      flex-wrap: wrap;
    }
  }

  .rows__group-title {
    flex: 1 1 0;
    min-inline-size: 0;

    @media (max-width: 639px) {
      flex-basis: 100%;
    }
  }

  /* What the group offers, beside its name as a control of the toolbar's size. */
  .rows__group-header .btn {
    --btn-size: var(--control-size);

    font-size: var(--text-control);
    min-block-size: var(--control-size);
    padding-block: 0;
  }

  /* Within a section, whom its next rows are about: smaller than the section's title, with no rule of its own. */
  .rows__subheading {
    color: var(--color-ink-dark);
    font-size: var(--text-pill);
    font-weight: 700;
    margin: var(--block-space) 0 0;

    + .rows {
      margin-block-start: var(--inline-space-half);
    }
  }

  .rows__subsection:not(:last-child) > .rows {
    margin-block-end: 0;
  }

  /* A row under the one above it, as a subcontractor under the firm that hired it. */
  .row--nested {
    padding-inline-start: calc(var(--row-padding-inline) + var(--depth, 0) * var(--inline-space-double));
  }

  /* What a nested row hangs from. Typing in the filter can leave the row without the one above it, so the indent
     goes and the row says it. */
  .row__meta--nested {
    display: none;
  }

  [data-filtering] {
    .row--nested[data-quick-find-target~="item"] {
      padding-inline-start: var(--row-padding-inline);
    }

    .row__meta--nested {
      display: block;
    }
  }

  /* A form opened in place under a row, indented as the rows it adds. */
  .rows__form {
    padding-inline: calc(var(--row-padding-inline) + var(--depth, 0) * var(--inline-space-double)) var(--row-padding-inline);
  }

  .row--inactive {
    color: var(--color-ink-dark);
  }

  .row__body {
    display: flex;
    flex-direction: column;
    flex: 0 1 auto;
    min-width: 0;
    padding-block: var(--row-padding-block);
    color: inherit;
    text-decoration: none;

    &:last-child {
      flex-grow: 1;
    }
  }

  /* At a class's weight, as the rows' other parts, so the one-line row after them wins by order. */
  :where(button).row__body {
    align-items: stretch;
    background: none;
    border: 0;
    border-radius: 0;
    font-weight: inherit;
    padding-inline: 0;
    text-align: start;

    &:enabled:hover {
      background: none;
    }
  }

  .row__title,
  .row__meta {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .row__title {
    font-weight: normal;
  }

  /* The whole row is one link, and under the pointer only its name is underlined. */
  @media (any-hover: hover) {
    :is(a.row__body, button.row__body, .inbox__link):hover .row__title {
      text-decoration: underline;
    }
  }

  .row__meta {
    color: var(--color-meta);
    font-size: var(--text-small);
    line-height: 1.2;
  }

  .row__title--wrap,
  .row__meta--wrap {
    overflow: visible;
    overflow-wrap: anywhere;
    white-space: normal;
  }

  /* A state said in the meta line is the meta's size, whatever the list sets it to. */
  .row__meta .status {
    font-size: inherit;
  }

  /* A row's details as small items in one pale pill: who, when, where, how many. It hugs what it holds, and where
     its line ends its items wrap onto the next, so none is ever cut. */
  .row__pill {
    --icon-size: 1.05em;

    align-items: center;
    align-self: flex-start;
    background-color: var(--color-meta-fill);
    border-radius: 0.51em;
    column-gap: 0.8em;
    display: flex;
    flex-wrap: wrap;
    font-size: var(--text-pill);
    line-height: 1.5;
    max-inline-size: 100%;
    overflow: visible;
    overflow-wrap: anywhere;
    padding-inline: 0.34em 0.51em;
    white-space: normal;
  }

  /* A tool's preview is a glimpse of its list, and a menu is narrow, so there the details keep to one line, a dot
     between them, and end in an ellipsis. What is cut stays inside, words for screen readers included, so it never
     widens the page. */
  :is(.tool__panel, .popup) .row__pill {
    display: block;
    overflow: hidden;
    position: relative;
    text-overflow: ellipsis;
    white-space: nowrap;

    > :not(.for-screen-reader) ~ :not(.for-screen-reader)::before {
      color: var(--color-meta);
      content: "·" / "";
      margin-inline: 0.35em;
    }
  }

  /* A glyph or a face sits on the line of the words it comes before, in a detail as in a state said after its glyph. */
  :is(.row__detail, .status--glyph) {
    :is(.icon, .avatar) {
      display: inline-grid;
      margin-inline-end: 0.3em;
      vertical-align: middle;
    }

    .icon {
      inset-block-start: -0.06em;
    }
  }

  .row__detail .avatar {
    --avatar-size: 2.4em;

    font-size: 0.5em;
    margin-inline-end: 0.6em;
  }

  /* A state among the details is told by its colour alone, as at the row's end. */
  .row__detail .status {
    font-weight: normal;
  }

  /* A box at a row's start that ticks its record off: on its name's first line however many the row takes, green
     once ticked, and a fingertip's size on a touch screen. */
  .row__check {
    align-items: center;
    align-self: flex-start;
    display: flex;
    flex: none;
    min-block-size: calc(1lh + var(--row-padding-block) * 2);

    .checkbox {
      transform: none;

      &:is(:checked, .checkbox-mark--checked) {
        background-color: oklch(var(--lch-green-lightest));
        color: var(--color-positive);
        opacity: 0.75;
      }

      /* A box with no input: one that opens a form to fill, or a date done by filling it. */
      &.checkbox-mark {
        --mark: var(--color-positive);

        block-size: 1.1em;
        border-width: 1.5px;
        inline-size: 1.1em;
      }

      &.checkbox-mark--checked {
        border-color: var(--color-positive);
      }

      @media (any-pointer: coarse) {
        margin-block: calc((2.75rem - 1.1em) / 2);
      }
    }
  }

  /* A box that does something takes the pointer; one that only says how a date stands does not. */
  .row__check:is(form, a) .checkbox {
    cursor: pointer;
  }

  /* A row ticked off is done, and its name goes grey. */
  .row:has(> .row__check :is(:checked, .checkbox-mark--checked)) .row__title {
    color: var(--color-meta);
  }

  .row__end {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    gap: var(--inline-space);
    color: var(--color-meta);
    font-size: var(--text-small);
    white-space: nowrap;
  }

  /* What can be done to a row from its list, at the row's end: quiet icons hung on the row's middle, so they take
     no height from it. Under a pointer they show with it; where there is none they are always in sight. */
  .row__actions {
    --btn-background: transparent;
    --btn-border-color: transparent;
    --btn-hover-amount: 8%;
    --btn-size: var(--control-size);

    align-items: center;
    display: flex;
    flex: none;
    font-size: var(--text-control);
    margin-block: calc(var(--control-size) / -2);
  }

  /* Under a pointer a row's actions, and a step's, come into sight with the pointer or the focus on their row. */
  @media (any-hover: hover) {
    :is(.row__actions, :is(.step__card, .step__row) > .step__actions) {
      opacity: 0;
      transition: opacity var(--duration-medium) ease-out;
    }

    :is(.row, .step__card, .step__row):is(:hover, :focus-within) > :is(.row__actions, .step__actions) {
      opacity: 1;
    }
  }

  /* A field at a row's end goes under the row's words where a phone's width leaves no room for both. */
  @container layout (inline-size < 640px) {
    .row:has(> .row__end > .input) {
      flex-wrap: wrap;

      > .row__body {
        flex-basis: 100%;
      }

      > .row__end {
        padding-block-end: var(--row-padding-block);
      }
    }
  }

  /* A row's end that stacks its lines, flush with the row's edge. */
  .row__end--stacked {
    align-items: flex-end;
    flex-direction: column;
    font-variant-numeric: tabular-nums;
    gap: 0.2em;
  }

  /* A list straight on the sheet, or on a tray, which lies over it, is plain rows, with no rule above, between or below them, in 18px text on a 27px line (16 on 24 on a phone), its text flush with the page's edge and its fill a little past it: 31px a row on one line, 28 on a phone, and as tall as a fingertip on a touch screen. In a card, a tool's preview, a dropdown or a dialog it keeps its lines. */
  :is(.sheet #main, .tray__dialog) .rows:not(:is(.panel, .record, .credential, .project__sheet, .tool__panel, .paper__section, .suggestions__list, :where(dialog:not(.tray__dialog))) *) {
    --row-padding-inline: 0;

    border-block-width: 0;
    font-size: var(--text-row);

    .row {
      --row-padding-block: 0.3em;
      --row-padding-inline: var(--inline-space);

      border-block-start: 0;
      border-radius: var(--radius-md);
      line-height: 1.5;
      margin-inline: calc(var(--row-padding-inline) * -1);

      &:where(:not(:has(.row__meta:not(:empty)))) {
        --row-padding-block: 2px;
      }

      /* The list picks its first row as the page loads, which is no one's doing: the pick shows once the keys move it.
         Shift held for the key caps moves nothing, though the browser takes it for the keys coming into use.
         At the gate it shows from the start, as the row a guard opens with a key, and in a tray once the arrows or
         typing move it while the keys are there, since Enter in its field opens the picked row. A tray's row under
         the pointer is left to the tray, which colours it. */
      &[aria-selected="true"]:not(:focus-visible, :has(:focus-visible)):not(#gate-results *, .gate-rows *, .tray__dialog:is([data-navigated], :has([data-filtering])):has(:focus-visible) *, .tray__dialog :has(> :is(.inbox__link, a.row__body):hover)),
      [data-shift-alone] &[aria-selected="true"]:focus:not(#gate-results *, .gate-rows *) {
        background-color: transparent;
      }

      /* A destructive button in a row is quiet until pointed at or focused. */
      .btn--negative:not(:hover, :focus-visible) {
        --btn-background: var(--color-canvas);
        --btn-border-color: var(--tint-ink-25);
        --btn-color: var(--color-ink);
      }

      @media (any-pointer: coarse) {
        > .row__body {
          justify-content: center;
          min-block-size: 2.75rem;
        }

        > .inbox__link {
          min-block-size: 2.75rem;
        }
      }
    }

    /* A list whose rows carry buttons or switches: each row as tall as a button and a gap, a hairline between rows,
       so the buttons of two rows never touch. */
    &:has(.row :is(.btn, .switch):not(.row__actions *)) {
      .row:not(.suggestions__list *) {
        align-items: center;
        min-block-size: calc(var(--control-size) + 0.84rem);
      }

      .row:not([hidden]) ~ .row:not(.suggestions__list *) {
        border-block-start: var(--border);
        border-radius: 0;
      }
    }

    .row .inbox__avatar {
      --avatar-size: 2rem;
    }

    /* The row's end says its date and its state in the size of the details, and never on two lines of its own. */
    .row__end {
      font-size: var(--text-pill);
    }

    .row__end .status {
      font-size: inherit;
    }

    /* Where there is room a row is one line: its name, its details after it in quiet text, and its date and state at the end.
       A row whose words are meant to wrap keeps them under its name, as does a file or folder beside its large icon. */
    @container layout (inline-size >= 640px) {
      .row:not(.library__row) {
        --row-padding-block: 2px;
      }

      .row .inbox__avatar {
        --avatar-size: calc(var(--text-row) * 1.5);

        font-size: var(--text-xx-small);
      }

      /* The parent's weight is set aside, so this weighs a class and a list that lays its rows out its own way
         has only to say so. It comes after the rows' own parts, above, and wins over them by order. */
      :where(& .row:not(.library__row)) .row__body:where(:not(:has(.row__title--wrap, .row__meta--wrap:not(.row__meta--nested)))) {
        align-items: baseline;
        flex-direction: row;
        gap: 0.75em;

        /* The name takes what it needs first, short of leaving a pill no room; quiet details take what is left and
           end in an ellipsis, and a pill's items wrap within it. */
        > :where(.row__title) {
          flex: 0 0 auto;
          max-inline-size: 100%;
          min-inline-size: 0;
        }

        &:where(:has(> .row__pill)) > :where(.row__title) {
          max-inline-size: 70%;
        }

        > :where(.row__meta) {
          flex: 0 1 auto;
          font-size: var(--text-pill);
          min-inline-size: 0;
        }

        > :where(.row__pill) {
          align-self: center;
          flex: 0 1 auto;
          min-inline-size: 0;
        }
      }

      .row__end--stacked {
        align-items: baseline;
        flex-direction: row;
        gap: 0.75em;
      }

      .row:not(:has(.inbox__avatar)) .inbox__end {
        padding-block: var(--row-padding-block);
      }
    }

    /* On a phone a row with details wraps its name rather than cutting it, and the details take the line under it
       with the row's date and state after them, so the name has the row's whole width. A row of words alone, or
       one laid out its own way, keeps its own. */
    @container layout (inline-size < 640px) {
      :where(& .row:not(.library__row):has(> .row__body > .row__pill, > .inbox__link > .row__body > .row__pill)) .row__title {
        overflow: visible;
        overflow-wrap: anywhere;
        white-space: normal;
      }

      :where(& .row:not(.library__row)) .inbox__link:where(:has(> .row__end):has(> .row__body > .row__pill)) {
        align-items: center;
        display: grid;
        gap: 0;
        grid-template-areas: "lead title title" "lead meta end";
        grid-template-columns: auto minmax(0, 1fr) auto;
        padding-block: var(--row-padding-block);

        > :not(.row__body, .row__end) {
          grid-area: lead;
          margin-inline-end: var(--inline-space);
        }

        > .row__body {
          display: contents;
        }

        .row__title {
          grid-area: title;
        }

        .row__pill {
          grid-area: meta;
          justify-self: start;
        }

        > .row__end {
          align-items: baseline;
          flex-direction: row;
          gap: 0.75em;
          grid-area: end;
          margin-inline-start: var(--inline-space);
          padding-block: 0;
        }
      }
    }
  }

  /* Its section's title starts where its rows do. */
  .sheet #main .rows__heading:not(:is(.panel, .record, .credential, .project__sheet, .tool__panel, .paper__section, .suggestions__list, dialog) *) {
    padding-inline: 0;
  }
}
