@layer components {
  /* An inspection answered in place, on screen: its name, what it is for and how far along it is, then each check on
     a line with its answer beside it, and at the foot the observation and the button. On paper it stays the sheet
     paper.css draws, so every rule here is the screen's. */
  @media screen {
    .paper--run {
      .paper__page {
        gap: var(--block-space-double);
        margin-inline: 0;
        max-inline-size: none;
      }

      /* Its index is the printed sheet's; on screen the checks follow one another down the page. */
      .paper__index {
        display: none;
      }

      .paper__header {
        display: flex;
        flex-direction: column;
      }

      .paper__title {
        order: 1;
      }

      .paper__legend {
        margin-block-start: var(--block-space-half);
        order: 2;
      }

      .paper__progress {
        order: 3;
      }

      .paper__fields {
        order: 4;
      }

      .paper__sections {
        row-gap: var(--block-space-double);
      }

      .paper__questions {
        gap: var(--block-space);
      }

      .paper__prompt {
        font-weight: 400;
      }

      .paper__question > .paper__box {
        align-items: center;
        column-gap: var(--inline-space-double);
        grid-template-columns: minmax(0, 1fr) auto;
        row-gap: var(--block-space-half);

        > * {
          grid-column: 1 / -1;
        }
      }

      /* A conformity, a number, a date or a time is answered at the end of the prompt's line. */
      .paper__question--beside > .paper__box {
        > .paper__prompt {
          grid-column: 1;
          grid-row: 1;
        }

        > :is(.paper__options, .input) {
          grid-column: 2;
          grid-row: 1;
        }

        > .input {
          inline-size: 7.5rem;
        }
      }

      /* Conforme, No conforme, No aplica: one control in three parts, the part chosen raised in its colour. */
      .paper__options--conformity {
        background-color: var(--color-ink-lightest);
        border: 1px solid var(--color-ink-lighter);
        border-radius: var(--radius-md);
        display: inline-flex;
        justify-self: end;

        > .btn {
          --btn-background: transparent;
          --btn-border-color: transparent;
          --btn-color: var(--color-ink);

          border-color: var(--btn-border-color);
          flex: 1 1 0;
          font-size: var(--text-pill);
          gap: 0.3em;
          justify-content: center;
          margin: -1px;
          min-block-size: var(--control-size);
          padding: 0 0.75em;
          white-space: nowrap;

          > .checkbox {
            inset: 0;
            opacity: 0;
            position: absolute;
          }

          .icon {
            --icon-size: 0.9em;

            display: none;
            margin-inline-end: 0.3em;
            vertical-align: -0.1em;
          }

          &:has(input:checked) {
            --btn-background: var(--color-canvas);
            --btn-border-color: var(--tint-ink-25);
            --btn-color: var(--color-ink);

            font-weight: 700;

            .icon {
              display: inline-block;
            }
          }

          &:has(input[value="ok"]:checked) {
            --btn-background: oklch(var(--lch-green-lightest));
            --btn-border-color: oklch(var(--lch-green-light));
            --btn-color: var(--color-success-text);
          }

          &:has(input[value="not_ok"]:checked) {
            --btn-background: oklch(var(--lch-red-lightest));
            --btn-border-color: oklch(var(--lch-red-light));
            --btn-color: var(--color-error-text);
          }
        }
      }

      /* A check's observation is a line to write on, opened by asking for it, by marking the check not conforming, or
         by having been written. */
      .paper__note {
        display: none;

        > .input {
          --input-padding: 0.2em 0;

          background: transparent;
          border: 0;
          border-block-end: 1px dashed var(--color-ink-light);
          border-radius: 0;
          font-size: var(--text-row);
        }
      }

      .paper__question:is(.paper__question--noting, :has(input[value="not_ok"]:checked), :has(.paper__note .input:not(:placeholder-shown))) .paper__note {
        display: flex;
      }

      .paper__add-note {
        display: none;
        font-size: var(--text-small);
        justify-self: start;
      }

      /* Asked for once the check is answered, or at once where the answer is written rather than marked. */
      .paper__question:is(:has(.paper__options input:checked), :not(:has(.paper__options))):not(.paper__question--noting, :has(input[value="not_ok"]:checked), :has(.paper__note .input:not(:placeholder-shown))) .paper__add-note {
        display: inline;
      }

      /* How many checks are answered, then the day and who is doing it. */
      .paper__progress {
        align-items: baseline;
        column-gap: var(--inline-space-double);
        display: flex;
        flex-wrap: wrap;
        font-size: var(--text-row);
        margin-block: var(--block-space-half) 0;

        strong {
          font-weight: 700;
        }
      }

      .paper__when {
        color: var(--color-meta);
      }

      .paper__heading .paper__count {
        flex: none;
      }

      /* The foot: whoever is doing it beside a line for the observation on the whole inspection, the buttons, and what is
         still missing. */
      .paper__foot {
        --avatar-size: 3.5rem;

        align-items: start;
        border-block-start: var(--border);
        column-gap: var(--inline-space-double);
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        padding-block-start: var(--block-space-double);

        &:not(:has(> .paper__foot-avatar)) {
          grid-template-columns: minmax(0, 1fr);
        }
      }

      .paper__foot-avatar {
        font-size: var(--text-row);
      }

      .paper__foot-body {
        display: flex;
        flex-direction: column;
        gap: var(--block-space-half);
        min-inline-size: 0;
      }

      .paper__observations {
        .paper__comments {
          --input-padding: 0.5em 0;

          background: transparent;
          border: 0;
          border-block-end: 1px dashed var(--color-ink-light);
          border-radius: 0;
          font-size: var(--text-row);
          min-block-size: 0;
        }
      }

      /* A line's placeholder says what it is for on screen, so its label is for a screen reader; on paper it prints. */
      :is(.paper__note > .paper__label, .paper__observations > label) {
        block-size: 1px;
        clip-path: inset(50%);
        inline-size: 1px;
        overflow: hidden;
        position: absolute;
        white-space: nowrap;
      }

      .paper__actions {
        --btn-icon-size: 1em;

        display: flex;
        flex-wrap: wrap;
        gap: var(--inline-space);
        margin-block-start: var(--block-space-half);

        > .btn {
          font-size: var(--text-row);
          min-block-size: 2.5em;
        }
      }

      .paper__hint {
        color: var(--color-meta);
        font-size: var(--text-small);
        margin: 0;
      }

      .paper__go {
        display: inline;
      }

      /* On a phone each answer goes under its prompt, the three parts across the column. */
      @media (width < 640px) {
        .paper__question--beside > .paper__box {
          > :is(.paper__prompt, .paper__options, .input) {
            grid-column: 1 / -1;
            grid-row: auto;
          }

          > .paper__options--conformity {
            display: flex;
            justify-self: stretch;
          }
        }

        .paper__foot {
          --avatar-size: 2.5rem;
        }
      }
    }

    /* Its index takes no column of its own, even where the window has room for one. */
    @container layout (width >= 64rem) {
      .paper--run .paper__page:has(> .paper__body > .paper__index) {
        grid-template-columns: minmax(0, 1fr);

        > *,
        > .paper__body > .paper__sections {
          grid-column: 1;
        }

        > .paper__body {
          grid-template-columns: minmax(0, 1fr);
        }
      }
    }
  }

  /* An inspection done, read as a record: how it came out beside who did it and when, then each check on a line of its
     own, its mark first and any finding at the line's end. */
  .run {
    .page-header {
      margin-block-end: var(--block-space-half);
    }
  }

  .run__lead {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: var(--block-space-half) var(--inline-space-double);

    > .finding__byline {
      margin: 0;
    }
  }

  /* A section's name, how many checks it holds, and a rule running on to the edge. */
  .run__heading {
    align-items: center;
    display: flex;
    font-size: var(--text-section);
    font-weight: 800;
    gap: var(--inline-space);
    margin: 0;

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

  .run__count {
    color: var(--color-meta);
    font-size: var(--text-pill);
    font-weight: 400;
  }

  .run__answers {
    display: grid;
    gap: var(--block-space-half);
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .run__answer {
    align-items: start;
    column-gap: var(--inline-space);
    display: grid;
    font-size: var(--text-row);
    grid-template-columns: 1.25rem minmax(0, 1fr) auto;
    line-height: var(--leading-snug);

    > .run__body {
      grid-column: 2;
    }

    > .run__end {
      align-self: center;
      grid-column: 3;
      justify-self: end;
    }
  }

  .run__body {
    display: flex;
    flex-direction: column;
    gap: 0.15em;
    min-inline-size: 0;

    > .lexxy-content,
    > .paper__grid {
      margin-block-start: var(--block-space-half);
    }
  }

  /* Conforming is a ticked box, not conforming a filled one with its cross, not applicable a dash. */
  .run__mark {
    block-size: 1.25rem;
    border: 1.5px solid var(--color-ink-medium);
    border-radius: var(--radius-xs);
    color: var(--color-ink-medium);
    display: inline-grid;
    inline-size: 1.25rem;
    margin-block-start: 0.1em;
    place-items: center;

    .icon {
      --icon-size: 0.8rem;
    }
  }

  .run__mark--ok {
    border-color: var(--color-success);
    color: var(--color-success);
  }

  .run__mark--not_ok {
    background-color: var(--color-error-button);
    border-color: var(--color-error-button);
    color: var(--color-ink-inverted);
  }

  a.run__end {
    color: var(--color-link);
    text-underline-offset: 0.15em;
  }

  /* By its public link, under the inspection just saved: another, or the sheet printed. */
  .run__again {
    --btn-icon-size: 1em;

    display: flex;
    flex-wrap: wrap;
    gap: var(--inline-space);
    margin-block-start: var(--block-space-double);
  }

  .run__value {
    font-weight: 700;
  }

  .run__written {
    margin: 0;
    white-space: pre-line;
  }

  .run__note,
  .run__blank {
    color: var(--color-meta);
    font-size: var(--text-small);
  }

  /* On a phone a finding goes under what was found. */
  @media (width < 640px) {
    .run__answer > .run__end {
      grid-column: 2;
      justify-self: start;
      margin-block-start: var(--block-space-half);
    }
  }

  /* By its public link an inspection stands under the account's name alone, centred across the bar. */
  .public:has(#main :is(.paper--run, .run)) .site-header__brand {
    grid-column: 1 / -1;
    justify-self: center;
  }

  /* On paper a field's hint is not written in. */
  @media print {
    .paper--run :is(.paper__comments, .paper__note .input)::placeholder {
      color: transparent;
    }

    /* What the foot holds is spaced as the rest of the sheet. */
    .paper--run .paper__foot-body {
      display: grid;
      gap: var(--block-space);
    }
  }
}
