@layer components {
  /* A record's own page, straight on the sheet: its facts, then a section for each thing that hangs off it. */
  .record-page {
    display: flex;
    flex-direction: column;
    gap: var(--block-space-double);
    min-inline-size: 0;

    > * {
      margin-block: 0;
    }

    > .btn {
      align-self: start;
    }

    /* An empty section is one quiet line, starting where the text does. */
    .blank-slate {
      background: none;
      border: 0;
      color: var(--color-text-subtle);
      font-size: var(--text-small);
      font-weight: 400;
      margin-block-start: 0;
      margin-inline: 0;
      padding: 0;

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

  /* What the record is, whose it is and its number, under its name in the page's heading. */
  .record-page__byline {
    --avatar-size: 1.5rem;

    color: var(--color-meta);

    .avatar {
      display: inline-grid;
      font-size: var(--text-xx-small);
      vertical-align: middle;
    }

    strong {
      color: var(--color-ink);
      font-weight: 600;
    }

    a {
      color: inherit;
      text-decoration-color: color-mix(in oklch, currentColor 40%, transparent);
      text-underline-offset: 0.15em;
    }
  }

  .record-page__description {
    font-size: var(--text-row);
    max-inline-size: 70ch;

    /* Lexical saves an empty description as <p><br></p>. */
    action-text-content p:only-child:has(br:only-child) {
      display: none;
    }
  }

  /* A record read as a document: its body in large type beside its author's face, which on a narrow screen
     moves into the byline. */
  .document {
    --avatar-size: 2.95rem;

    align-items: start;
    column-gap: var(--block-space);
    display: grid;
    font-size: calc(var(--text-row) * 10 / 9);
    grid-template-columns: auto minmax(0, 1fr);
    line-height: 1.5;

    > .document__body:only-child {
      grid-column: 1 / -1;
    }

    @media (width < 768px) {
      .document__avatar {
        display: none;
      }

      > .document__body {
        grid-column: 1 / -1;
      }
    }
  }

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

  .document__body {
    max-inline-size: 70ch;
  }

  .record-page__byline .document__byline-avatar {
    @media (width >= 768px) {
      display: none;
    }
  }

  /* A section's heading on its rule, then what it holds. */
  .record-page__section {
    display: flex;
    flex-direction: column;
    gap: var(--block-space-half);
    min-inline-size: 0;

    > * {
      margin-block: 0;
    }

    .record-page & > .rows {
      margin-block: 0;
    }

    > .btn {
      align-self: start;
    }
  }

  .record-page__meta {
    color: var(--color-meta);
    font-size: var(--text-small);
    text-wrap: pretty;
  }

  .record-page__meta-text + .record-page__meta-text::before {
    content: "·";
    margin-inline: 0.5ch;
  }

  /* A finding's page: its name, state and facts, swapped in place for the fields that edit them, then its buttons.
     A to-do, a contract, a person, a vehicle, a machine and a blocklisting are edited the same way. */
  .finding,
  .todo,
  .record-page--editable {
    /* The frame's stream is gone once it has run; until then it takes no room. */
    turbo-stream {
      display: none;
    }

    .page-header {
      margin-block-end: calc(var(--page-header-space) - var(--block-space-double));
    }

    /* The name is typed where it is read, on a line of its own. */
    .page-header__title:has(.autoresize__textarea) {
      flex: 1 1 100%;
      min-inline-size: 0;
    }

    .page-header__title :is(.autoresize__textarea, .input) {
      color: inherit;
      font: inherit;
      letter-spacing: inherit;
      line-height: inherit;
    }
  }

  /* Under the fields of a record being written: its buttons again, over a line on what happens next. */
  .record-page__foot {
    border-block-start: var(--border);
    display: flex;
    flex-direction: column;
    gap: var(--block-space-half);
    padding-block-start: var(--block-space-double);
  }

  /* A field stands on its row's 40px line (32px on a phone), its words where the value's were. One that answers a stage's item stands the same. */
  :is(.finding, .todo, .record-page--editable) .details__value > .input,
  .stage .paper__box > .input {
    --input-padding: 0.15em 0.5em;

    font-size: max(16px, 1em);

    &[type=date] {
      inline-size: auto;
    }
  }

  /* How many of its steps are done, quietly after their label. */
  .finding__tally {
    color: var(--color-meta);
    font-weight: 400;

    &:not(:empty)::before {
      content: "· ";
    }
  }

  /* A value on a to-do's page that opens its field: plain words, underlined while pointed at. */
  .todo .todo__value {
    align-items: center;
    display: inline-flex;
    gap: 0.3em;
    text-decoration: none;

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

  /* Save and Cancel under a field opened on the page, clear of the row after it. */
  .todo .todo__field-actions {
    padding-block: 0.25rem var(--block-space);
  }

  /* How a to-do repeats: a small list to choose from, as wide as the choice in it,
     and how many to skip after it.
     Once it repeats, under them, whether it stops: forever, or on a date that shows once chosen. */
  .todo__repeat {
    display: flex;
    flex-direction: column;
    gap: 0.2em;
  }

  .todo__repeat-how {
    align-items: center;
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.2em 0.4em;
  }

  .todo .todo__repeat :is(.input--select, .todo__interval) {
    --input-border-color: var(--tint-ink-15);

    background-color: var(--color-canvas);
    block-size: var(--control-size);
    flex: none;
    font-size: max(16px, 1em);
    inline-size: auto;
    line-height: 1;
    max-inline-size: 100%;
  }

  .todo .todo__repeats {
    --input-padding: 0 1.8em 0 0.8em;

    field-sizing: content;
  }

  .todo .todo__repeat .todo__interval {
    --input-padding: 2px 6px 2px 12px;
  }

  .todo .todo__interval {
    inline-size: 3.5em;
  }

  .todo__repeat-until {
    align-items: center;
    column-gap: 0.4em;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    justify-items: start;
  }

  .todo__repeat-choice {
    align-items: center;
    cursor: pointer;
    display: flex;
    gap: 0.4em;
    min-block-size: var(--control-size);
  }

  .todo__repeat-choice--forever {
    grid-column: 1 / -1;
  }

  .todo__repeat-until:not(:has(#todo_repeats_until_date:checked)) > .date-field,
  .todo__repeat:has(option[value=never]:checked) :is(.todo__repeat-how > :not(select), .form__hint, .todo__repeat-until) {
    display: none;
  }

  /* A to-do's notes are a line to write on until they are: while empty and left alone, one line high and no toolbar. */
  .todo lexxy-editor.lexxy-editor--empty:not(:focus-within) {
    --lexxy-editor-rows: 1lh;

    lexxy-toolbar {
      display: none;
    }

    .lexxy-editor__content {
      margin-block-start: 0;

      &::before {
        color: var(--color-text-placeholder);
        opacity: 1;
      }

      > p {
        margin-block-end: 0;
      }
    }
  }

  /* Switching a to-do off or on, under the form that changes it. */
  .todo__activation {
    margin-block-start: var(--block-space-double);
  }

  /* Under a to-do's byline, the late date doing it now starts with. */
  .todo__lead {
    color: var(--color-meta);
    font-size: var(--text-small);
    margin-block: 0 calc(var(--page-header-space) - var(--block-space-half));
  }

  .finding__byline:has(+ .todo__lead) {
    margin-block-end: var(--block-space-half);
  }

  /* Over a to-do's dates, which of them follow: late, to do, coming up or done. */
  .todo__dates-heading {
    color: var(--color-meta);
    font-size: var(--text-small);
    font-weight: 600;
    margin-block: var(--block-space-half) 0;
  }

  /* Each person a finding is assigned to: a small avatar and the name. */
  .finding__people {
    --avatar-size: 1.5rem;

    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25em 1em;

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

  .finding__person {
    align-items: center;
    display: inline-flex;
    gap: 0.4em;
  }

  /* What a line of a record offers beside its value, the person to add or the site to pick: a control of the frame's size. */
  .record-page .popup-anchor > .btn--circle {
    --btn-size: var(--control-size);
  }

  /* Save and Cancel while a record is edited, under the fields and, from 800, where the values start. At rest it holds nothing. */
  .record-page__actions {
    &:empty {
      display: none;
    }

    @media (min-width: 100ch) {
      padding-inline-start: calc(var(--record-label-size, 6.3rem) + 0.842rem);   /* the label and its 16px gap */
    }

    /* What ends the record stands under Save and Cancel, on a line of its own. */
    > :is(div, p) {
      flex-basis: 100%;
    }
  }

  /* A contract's labels run up to "Contrato u orden de compra firmada", which takes its line whole while it is
     under half the sheet. */
  .record-page--editable.project__sheet {
    --record-label-size: min(18.1rem, 45%);             /* 344px */
  }

  /* Fields a level down in their row take the row's line too: comments in a box that grows, a list to choose from. */
  .record-page--editable .details__value {
    --input-padding: 0.15em 0.5em;

    .input:is([type=date], .input--select) {
      inline-size: auto;
    }

    .input--select {
      --input-padding: 0.15em 1.8em 0.15em 0.5em;
    }

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

    .form__hint:not(.form__hint--inline) {
      margin-block-start: 0.25em;
    }
  }

  /* A field the save refused stays where it was, in the colour of the error above. */
  .record-page--editable .field_with_errors {
    display: contents;

    > .input {
      --input-border-color: var(--color-error);
    }
  }

  /* A document's type and its number on one line. */
  .record-page__fields {
    display: flex;
    gap: var(--inline-space-half);

    > .input--select {
      flex: none;
    }
  }

  /* The two dates of a term, one under the other, each after its name. */
  .record-page__dates {
    align-items: baseline;
    column-gap: var(--inline-space-half);
    display: grid;
    grid-template-columns: max-content max-content;
    row-gap: 0.4em;

    label {
      display: contents;
    }
  }

  /* A finding's mark, name and byline keep their own distances; its buttons and dates stand off from them.
     Its longest label, "Responsables", needs more than the 120px column. */
  .record-page.finding {
    --record-label-size: 7.4rem;                        /* 140px */

    gap: 0;

    > :is(.record-page__actions, .record-page__meta) {
      margin-block-start: var(--block-space-double);
    }

    .error-explanation {
      margin-block-end: var(--block-space);
    }
  }

  /* The byline follows the name and its buttons, and keeps the space to the facts. A to-do's and a work permit's do too. */
  :is(.record-page:is(.finding, .todo), .permit__head) .page-header {
    margin-block-end: 0;
  }

  /* The mark over an urgent finding's name. */
  .finding__eyebrow {
    line-height: 1;
    margin-block: 0 0.4rem;
  }

  /* Under its name and buttons: which finding it is, its state, who reported it and when. */
  .finding__byline {
    font-size: var(--text-control);
    margin-block: var(--block-space) calc(var(--page-header-space) - var(--block-space-half));

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

      font-size: 0.55em;
    }
  }

  /* Under a finding's name, the large button that closes or reopens it and the quiet one that sets it aside. A work permit's stand the same. */
  .record-page:is(.finding, .todo, .permit) .page-header__actions {
    font-size: var(--text-row);
    gap: var(--inline-space-half) var(--inline-space);
    margin-block-start: var(--inline-space-half);
  }

  /* A record's main action, a large button under its name with a box to tick before its word when pressing it settles the record.
     It stands 40px on a phone and 43px on a computer, taller than a page's other actions. */
  .btn.record-page__toggle {
    --btn-gap: 0.45em;
    --btn-icon-size: 1.1em;
    --btn-padding: 0 0.85em 0 0.65em;

    min-block-size: 2.5em;

    @media (min-width: 100ch) {
      min-block-size: 2.4em;
    }
  }

  .record-page__box {
    --icon-size: 0.8em;

    block-size: 1.1em;
    border: 1.5px solid currentColor;
    border-radius: 20%;
    display: grid;
    flex: none;
    inline-size: 1.1em;
    place-content: center;
  }

  /* Pressed, it is done: pale green with green words, and its box ticked in green as a done row's is. */
  .btn.record-page__toggle--done {
    --btn-background: oklch(var(--lch-green-lightest));
    --btn-color: var(--color-positive);
  }

  /* Closing asks what was done: a tinted card under the finding's buttons, the note a field on it. */
  .finding__closing {
    background-color: var(--color-meta-fill);
    border-radius: var(--radius-lg);
    display: flex;
    flex-direction: column;
    font-size: var(--text-row);
    gap: 0.7em;
    margin-block-start: var(--block-space);
    padding: 1.1em;

    lexxy-editor {
      --lexxy-editor-padding: 0.6em;
      --lexxy-editor-rows: 3lh;

      background-color: var(--color-canvas);
      border: 1px solid var(--color-ink-medium);
      border-radius: var(--radius-lg);
    }
  }

  .finding__closing-title {
    font-size: inherit;
    font-weight: 700;
    line-height: 1.5;
    margin: 0;
  }

  .finding__closing-actions {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: var(--inline-space);
  }

  /* Whether a finding is urgent: a button drawn as a switch that says yes or no on its track, then what it marks. */
  .finding__urgent {
    --btn-gap: 0.5em;

    inline-size: fit-content;
    min-block-size: 1lh;

    .switch {
      flex: none;
      font-size: calc(var(--control-size) / 2);         /* a 28px track */
      inline-size: 3.75em;
    }
  }

  .finding__urgent-word {
    color: var(--color-ink-inverted);
    font-size: 0.8em;
    font-weight: 700;
    inset-block-start: 50%;
    inset-inline-end: 0.6em;
    line-height: 1;
    position: absolute;
    translate: 0 -50%;
  }

  .finding__urgent--on {
    .switch__btn {
      --switch-color: var(--color-positive);

      &::before {
        transform: translateX(2.35em);
      }
    }

    .finding__urgent-word {
      inset-inline: 0.7em auto;
    }
  }

  /* An empty field says what to put in it, quietly, and opens the record to be edited. */
  .record-page .record-page__prompt {
    color: var(--color-text-placeholder);
    text-decoration: none;

    @media (any-hover: hover) {
      &:hover {
        color: var(--color-text-subtle);
      }
    }
  }

  .same-file__summary {
    cursor: pointer;

    .same-file[open] > & {
      margin-block-end: var(--block-space-half);
    }
  }

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

  .same-file__caret {
    font-size: 1ch;
    rotate: -90deg;
    transition: rotate var(--duration-medium) ease-out;

    .same-file[open] & {
      rotate: 0deg;
    }
  }

  /* What an answer has to meet, as lines: a round mark nobody ticks, or the reading's tick or cross over what it found. */
  .conditions {
    display: flex;
    flex-direction: column;
    font-size: var(--text-small);
    gap: var(--block-space-half);
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .conditions__line {
    align-items: start;
    display: flex;
    gap: 0.5em;
  }

  .conditions__body {
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
    overflow-wrap: anywhere;
  }

  /* What the reading made of the files, said before its marks. */
  .conditions__summary {
    font-size: var(--text-small);
    margin: 0;
  }

  .conditions__evidence {
    color: var(--color-meta);
    font-size: 0.875em;
  }

  /* Among a record's facts the lines read at the facts' size. */
  .details__value > .conditions {
    font-size: inherit;
    gap: 0.4em;

    @media (min-width: 100ch) {
      .conditions__evidence {
        font-size: 0.8em;                               /* 16px */
      }
    }
  }

  .conditions__mark {
    --icon-size: 0.7em;

    block-size: 1.2em;
    border: 1.5px solid var(--color-ink);
    border-radius: 50%;
    display: grid;
    flex: none;
    inline-size: 1.2em;
    margin-block-start: calc((1lh - 1.2em) / 2);
    place-content: center;
  }

  .conditions__mark--passed,
  .conditions__mark--failed {
    border: 0;
    color: var(--color-ink-inverted);
  }

  .conditions__mark--passed {
    background-color: var(--color-success-button);
  }

  .conditions__mark--failed {
    background-color: var(--color-error-button);
  }
}
