@layer components {
  .trail {
    display: grid;
    grid-template-columns: max-content max-content max-content minmax(0, max-content) minmax(0, 1fr);
    column-gap: var(--inline-space);
    row-gap: var(--block-space-half);
    margin-block: var(--block-space);
    font-size: var(--text-small);
    color: var(--color-ink);
  }

  .trail__entry {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    align-items: baseline;
    min-width: 0;
    row-gap: 0;
  }

  .trail__time {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }

  .trail__time--short {
    color: var(--color-ink-dark);
    font-family: var(--font-sans);
    font-size: var(--text-x-small);
  }

  .trail__more {
    --prose-gap: var(--block-space);

    grid-column: 1 / -1;
    margin-block: var(--block-space-half);
    padding: var(--block-space);
    background-color: var(--color-canvas);
    border-radius: var(--radius-md);
    overflow-wrap: break-word;

    @media screen {
      border: var(--trail-more-border-size, 1px) solid var(--color-ink-lighter);
    }

    > :first-child {
      margin-block-start: 0;
    }

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

  .trail__entry {
    outline: none;

    &[hidden] {
      display: none;
    }

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

  .trail__sentence {
    grid-column: 2 / -1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .trail__sentence--link {
    color: inherit;
    text-decoration: none;

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

  .trail .pagination-link {
    grid-column: 1 / -1;
  }

  @media (width < 640px) {
    .trail {
      grid-template-columns: max-content max-content max-content minmax(0, 1fr);
      row-gap: var(--block-space);
    }

    .trail__sentence {
      white-space: normal;
      overflow-wrap: break-word;
    }
  }

  .trail--events {
    grid-template-columns: max-content minmax(0, 1fr);

    .trail__entry {
      position: relative;
    }

    .trail__sentence {
      grid-column: 2;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .trail__sentence--link::after {
      content: "";
      inset: 0;
      position: absolute;
    }

    /* Above the sentence link's overlay, so its attachments open */
    .trail__more {
      position: relative;
    }
  }

  .trail--events {
    row-gap: var(--block-space-half);
  }

  .trail-day__title {
    margin-block: var(--block-space) 0;
    color: var(--color-ink-dark);
    font-size: var(--text-small);
    font-weight: 600;
  }

  /* A day with nothing done is one quiet line, starting where the day's title does. */
  .trail-day .blank-slate {
    background: none;
    border: 0;
    color: var(--color-text-subtle);
    font-size: var(--text-small);
    font-weight: 400;
    margin-inline: 0;
    padding: 0;

    > strong {
      color: inherit;
      display: inline;
      font-weight: inherit;
    }
  }
}

/* What happened across the account or what a person did, newest first, under a divider per day. */
@layer components {
  .history {
    --history-avatar-size: 2.5rem;  /* 40px */
    --history-indent: 0.25em;       /* 4px */
    --history-kind-size: 1.2em;     /* 19px */

    display: grid;
    grid-template-columns: max-content max-content max-content minmax(0, 1fr);
    row-gap: 1.5em;
    font-size: 1rem;                /* 16px */
    line-height: 1.4;
    color: var(--color-ink);

    @media (width >= 640px) {
      padding-inline-end: 0.75em;    /* 12px */
    }

    @media (min-width: 100ch) {
      --history-avatar-size: 2.1rem; /* 40px */
      --history-indent: 0.8125em;    /* 13px */

      padding-inline-end: 4.5em;     /* 72px */
      font-size: 0.842rem;           /* 16px */
    }

    .pagination-link {
      grid-column: 1 / -1;
    }

    /* Under the log's menus the first day stands two lines off; the bar's own margin folds into this one. */
    .filter-bar + & {
      margin-block-start: 2em;
    }
  }

  /* A day and its entries, so the day goes with them when the filter box leaves none. */
  .history__group {
    display: contents;

    &:not(:has(.history__entry:not([hidden]))) {
      display: none;
    }
  }

  /* A dark pill on a hairline that runs to the end. */
  .history__day {
    display: flex;
    grid-column: 1 / -1;
    align-items: center;
    gap: 1.714em;                   /* 24px */
    margin: 0;
    font-size: var(--text-control);
    font-weight: 800;
    line-height: 1;
    text-transform: uppercase;

    /* In a timeline it sits a little nearer its entries than they are to each other. */
    .history & {
      margin-block-end: -0.286em;
    }

    .history__group:not(:first-child) > &,
    turbo-frame ~ turbo-frame > .history__group > & {
      margin-block-start: 1.714em;
    }

    &::after {
      flex: 1;
      block-size: 1px;
      background-color: var(--color-ink-light);
      content: "";
    }
  }

  .history__date {
    padding: 0.571em 0.857em;       /* 30px high */
    border-radius: var(--radius-sm);
    background-color: var(--color-ink);
    color: var(--color-ink-inverted);
    white-space: nowrap;
  }

  .history__entry {
    display: grid;
    grid-column: 1 / -1;
    grid-template-columns: subgrid;
    align-items: start;
    position: relative;
    padding-inline-start: var(--history-indent);
    outline: none;

    &[hidden] {
      display: none;
    }

    /* The day's line, down through the marks from its first entry's and a little past its last. */
    &::before {
      position: absolute;
      inset-block: 0 -1.5em;
      inset-inline-start: calc(var(--history-indent) + var(--history-kind-size) / 2 - 1px);
      inline-size: 2px;
      background-color: var(--color-meta-fill);
      content: "";
    }

    .history__day + &::before {
      inset-block-start: calc(var(--history-avatar-size) / 2);
    }

    :is(.history, .history__group:last-child) > &:last-child::before {
      inset-block-end: -0.75em;
    }
  }

  /* The log picks its first entry as the page loads; the pick shows once the keys move it, as in a list. */
  :is(.history__entry, .changes__entry)[aria-selected="true"]:is(:hover, :focus-visible:not([data-shift-alone] *), :has(:focus-visible)) {
    border-radius: var(--radius-md);
    box-shadow: 0 0 0 0.35em var(--color-ink-lightest);
    background-color: var(--color-ink-lightest);
  }

  /* The kind of record, a white glyph on a round of its tint, level with the avatar's middle and over the day's line. */
  .history__kind {
    --icon-size: 0.7em;

    display: grid;
    grid-column: 1;
    place-items: center;
    position: relative;
    block-size: var(--history-kind-size);
    inline-size: var(--history-kind-size);
    margin-block-start: calc((var(--history-avatar-size) - var(--history-kind-size)) / 2);
    margin-inline-end: 0.4375em;    /* 7px */
    border-radius: 50%;
    background-color: oklch(var(--kind-tint, var(--lch-ink-medium)));
    color: var(--color-white);
  }

  .history__kind--finding { --kind-tint: var(--lch-yellow-medium); }
  .history__kind--contract { --kind-tint: var(--lch-uncolor-medium); }
  .history__kind--site { --kind-tint: var(--lch-aqua-medium); }
  .history__kind--employee,
  .history__kind--independent,
  .history__kind--vehicle,
  .history__kind--machinery { --kind-tint: var(--lch-violet-medium); }
  .history__kind--submission,
  .history__kind--todo { --kind-tint: var(--lch-green-medium); }
  .history__kind--requirement { --kind-tint: var(--lch-lime-medium); }
  .history__kind--work-permit { --kind-tint: var(--lch-red-medium); }
  .history__kind--checklist,
  .history__kind--run { --kind-tint: var(--lch-blue-medium); }
  .history__kind--training { --kind-tint: var(--lch-purple-medium); }
  .history__kind--gate { --kind-tint: var(--lch-pink-medium); }

  .history__time {
    grid-column: 2;
    min-inline-size: 5em;           /* 62px and its 8px gap */
    padding-block-start: calc((var(--history-avatar-size) - 1lh) / 2);
    padding-inline-end: 0.571em;    /* 8px */
    color: var(--color-meta);
    font-size: 0.875em;             /* 14px */
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }

  .history__avatar {
    --avatar-size: var(--history-avatar-size);

    grid-column: 3;
    margin-inline-end: 0.5625em;    /* 9px */
  }

  /* Done by the account itself: its word in the avatar's place, over its middle and no wider a column. */
  .history__system {
    display: flex;
    grid-column: 3;
    justify-content: center;
    block-size: var(--history-avatar-size);
    inline-size: var(--history-avatar-size);
    margin-inline-end: 0.75em;      /* 9px */
    padding-block-start: calc((var(--history-avatar-size) - 1lh) / 2);
    color: var(--color-meta);
    font-size: 0.75em;              /* 12px, so the word fits the avatar's width */
    white-space: nowrap;
  }

  /* One line sits level with the avatar's middle; more start at its top. */
  .history__body {
    display: flex;
    grid-column: 4;
    flex-direction: column;
    justify-content: center;
    min-block-size: var(--history-avatar-size);
    font-weight: 700;
  }

  .history__body,
  .changes__body {
    min-inline-size: 0;
    overflow-wrap: anywhere;

    .trail__sentence {
      color: inherit;
    }

    /* The whole entry opens the record; its name reads as the link, underlined at rest. */
    .trail__sentence--link {
      text-decoration: none;

      .trail__record {
        color: var(--color-link);
        text-decoration: underline;
      }

      &::after {
        position: absolute;
        inset: 0;
        content: "";
      }
    }

    /* Above the sentence link's overlay, so its attachments open. */
    .trail__more {
      position: relative;
      margin-block: 0.5em 0;
      font-weight: 400;
    }
  }

  /* The trail keeps each sentence to one line, cut short; on a phone it runs to two. */
  .history__body .trail__sentence {
    display: block;

    @media (width < 640px) {
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      display: -webkit-box;
    }
  }

  /* A record's history says each change in full. */
  .changes__body .trail__sentence {
    overflow: visible;
    white-space: normal;
  }

  /* The way to a signature, a quiet link under the sentence. */
  .changes__signature {
    margin-block-start: 0.25em;
    font-size: 0.875em;             /* 14px */

    > summary {
      inline-size: fit-content;
      color: var(--color-meta);
      cursor: pointer;
      list-style: none;
      text-decoration: underline;

      &::-webkit-details-marker {
        display: none;
      }
    }
  }

  .history__blank {
    grid-column: 1 / -1;
  }

  /* A person's activity loads a day at a time, each day a list of its own. */
  .trail-days .history {
    margin-block-end: 2.5em;
  }

  /* On a phone the mark and the time sit over the avatar and the sentence, and the day's pill is centred between two rules. */
  @media (width < 640px) {
    .history {
      grid-template-columns: max-content max-content minmax(0, 1fr);
    }

    .history__day {
      gap: 0.286em;                 /* 4px */
    }

    .history__day::before {
      flex: 1;
      block-size: 1px;
      background-color: var(--color-ink-light);
      content: "";
    }

    .history__entry {
      row-gap: 0.375em;             /* 6px */

      .history__day + &::before {
        inset-block-start: calc(var(--history-kind-size) / 2);
      }
    }

    .history__kind {
      grid-area: 1 / 1;
      margin-block-start: 0;
      margin-inline-end: 0.375em;   /* 6px */
    }

    .history__time {
      grid-area: 1 / 2 / auto / -1;
      align-self: center;
      min-inline-size: 0;
      padding: 0 0 0 0.143em;       /* 2px */
    }

    .history__avatar,
    .history__system {
      grid-area: 2 / 2;
      margin-inline-end: 0.5rem;    /* 8px */
    }

    .history__body {
      grid-area: 2 / 3;
    }
  }
}

/* A record's own history, newest first: when and what, each at a dot on a line. The block at the foot of a record and its full history page are the same list at two lengths. */
@layer components {
  .changes {
    --changes-dot-size: 0.5625em;   /* 9px */

    display: grid;
    row-gap: 1.25em;                /* 20px */
    font-size: 1rem;                /* 16px */
    line-height: 1.5;
    color: var(--color-ink);

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

  .changes__entry {
    position: relative;
    padding-inline-start: 1.5em;    /* 24px */
    outline: none;

    /* The dot, level with the date. */
    &::before {
      position: absolute;
      inset-block-start: calc((1em - var(--changes-dot-size)) / 2);
      inset-inline-start: 0.125em;  /* 2px */
      block-size: var(--changes-dot-size);
      inline-size: var(--changes-dot-size);
      border-radius: 50%;
      background-color: var(--color-meta);
      content: "";
    }

    /* The line, from this dot's middle to the next one's. */
    &:not(:last-child)::after {
      position: absolute;
      inset-block-start: 0.5em;
      inset-inline-start: calc(0.125em + var(--changes-dot-size) / 2 - 0.5px);
      block-size: calc(100% + 1.25em);
      inline-size: 1px;
      background-color: color-mix(in oklch, var(--color-meta) 50%, transparent);
      content: "";
    }
  }

  .changes__time {
    display: block;
    color: var(--color-meta);
    font-size: 0.8em;               /* 12.8px */
    line-height: 1.25;              /* 16px */
  }

  .changes__all {
    margin-block-start: 1.5em;
  }
}
