@layer components {

  /* The house's technical documentation (houses/documents/show): one sheet, numbered sections. On
     screen it sits on the app's surface; printed, it's black on white A4. */
  /* Wide enough for the manifold tables' ten columns on a laptop; printed, the page decides. */
  .document {
    --document-width: 1120px;

    padding: calc(16px + env(safe-area-inset-top)) 16px 40px;
  }

  .document__toolbar {
    max-width: var(--document-width);
    margin: 0 auto 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
  }

  .document__actions {
    display: flex;
    gap: 8px;
  }

  .document__back {
    color: var(--color-ink-muted);
    text-decoration: none;

    &:hover {
      color: var(--color-ink);
    }
  }

  .document__sheet {
    max-width: var(--document-width);
    margin: 0 auto;
    padding: 36px 40px;
    color: var(--color-ink);
    background: var(--color-surface);
    border: 1px solid var(--color-edge);
    border-radius: 8px;
    counter-reset: section;
  }

  .document__heading {
    padding-bottom: 18px;
    margin-bottom: 8px;
    border-bottom: 2px solid var(--color-ink);
  }

  .document__kind {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--color-ink-muted);
  }

  .document__title {
    margin: 4px 0;
    font-size: 26px;
    font-weight: 700;
  }

  .document__scope {
    color: var(--color-ink-muted);
  }

  /* Customer, address, designer, revision and date under the title. */
  .document__title-block {
    margin-top: 14px;
  }

  /* A table still wider than the sheet (a narrow window, a phone) scrolls within its section. */
  .document__section {
    margin-top: 24px;
    overflow-x: auto;
  }

  /* "1. Загальні дані": numbered in order, so sections can come and go. */
  .document__section-title {
    margin-bottom: 8px;
    font-size: 17px;
    font-weight: 700;
    counter-increment: section;
    break-after: avoid;

    &::before {
      content: counter(section) ". ";
    }
  }

  .document__floor {
    margin: 14px 0 6px;
    font-size: 13px;
    font-weight: 600;
    break-after: avoid;
  }

  .document__empty {
    color: var(--color-ink-muted);
  }

  /* A floor's plan (houses/documents/_drawings): kept on one page, in a light frame. */
  .document__drawing {
    margin: 0 0 18px;
    break-inside: avoid;

    & .drawing {
      padding: 6px;
      border: 1px solid var(--color-line);
      border-radius: 4px;
    }
  }

  /* What the layout couldn't do (a missing manifold, an unreachable device): warnings. */
  .document__issues {
    margin: 10px 0 0;
    padding-left: 18px;
    color: var(--color-warning);

    &:empty {
      display: none;
    }
  }

  .document-table {
    width: 100%;
    font-size: 12px;
    border-collapse: collapse;

    & th,
    & td {
      padding: 5px 8px;
      text-align: left;
      vertical-align: top;
      border: 1px solid var(--color-line);
    }

    & thead th {
      font-weight: 600;
      color: var(--color-ink-muted);
      background: var(--color-surface-sunken);
    }

    & tfoot :is(th, td) {
      font-weight: 600;
    }

    & tfoot .document__note {
      font-weight: 400;
    }

    & tr {
      break-inside: avoid;
    }

    & small {
      display: block;
      color: var(--color-ink-muted);
    }

    & .table__num {
      text-align: right;
      white-space: nowrap;
      font-variant-numeric: tabular-nums;
    }

    & .table__qty {
      white-space: nowrap;
      font-variant-numeric: tabular-nums;
    }
  }

  /* A heading row inside a long table (the bill of materials' sections). */
  .document-table__section th {
    padding-top: 10px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    font-size: 11px;
    color: var(--color-ink-muted);
    break-after: avoid;
  }

  /* A unit price typed into its row on the estimate (house_estimate_controller): as narrow as a figure,
     aligned with the costs; printed, just the figure. */
  .materials__price {
    width: 6em;
    padding: 2px 4px;
    font: inherit;
    text-align: right;
  }

  .document__note {
    margin-bottom: 8px;
    font-size: 12px;
    color: var(--color-ink-muted);
  }

  /* A room whose heating falls short of its heat loss. */
  .document-table tr.is-short [data-field=installed] {
    font-weight: 700;
    color: var(--color-warning);
  }

  .document-table__code {
    font-weight: 600;
    white-space: nowrap;
  }

  /* The house in 3D under the general information (houses/documents/_overview), captured on the app's
     dark background; printed, it's inverted to dark lines on white (see @media print). */
  .document__figure {
    margin: 16px 0 0;
    break-inside: avoid;
  }

  .document__image {
    display: block;
    width: 100%;
    height: auto;
    border-radius: 6px;
  }

  .document__figure figcaption {
    margin: 6px 0 0;
  }

  /* The revision this page shows, in the list of revisions. */
  .document-table tr.is-current td {
    font-weight: 600;
  }

  /* General information: label column, value column. */
  .document-table--facts th {
    width: 40%;
    font-weight: 400;
    color: var(--color-ink-muted);
  }

  /* Printed: the sheet alone, black on white, edge to the page margins (hardcoded colours are fine
     in print). */
  @media print {
    @page {
      size: A4;
      margin: 15mm 15mm 18mm;
    }

    :root {
      color-scheme: light;
    }

    .document {
      padding: 0;
    }

    .document__toolbar {
      display: none;
    }

    .materials__price {
      padding: 0;
      color: #000;
      background: none;
      border: 0;
      appearance: textfield;
    }

    /* The overview's dark background would print as a block of ink: inverted, lines go dark on white.
       The half turn of hue undoes inversion's colour swap, so supply stays red and return blue; the
       brightness lifts the inverted graphite (not quite black on screen) to paper white. */
    .document__image {
      filter: invert(1) hue-rotate(180deg) brightness(1.2);
    }

    .document__sheet {
      max-width: none;
      padding: 0;
      color: #000;
      background: none;
      border: 0;
    }

    .document__heading {
      border-bottom-color: #000;
    }

    .document__issues,
    .document-table tr.is-short [data-field=installed] {
      color: #000;
    }

    .document__note,
    .document-table__section th,
    .document__kind,
    .document__scope,
    .document__empty,
    .document-table small,
    .document-table thead th,
    .document-table--facts th {
      color: #444;
    }

    .document__section {
      overflow: visible;
    }

    .document-table {

      & th,
      & td {
        border-color: #999;
      }

      & thead th {
        background: #eee;
      }
    }

    /* Many columns (a manifold's circuits): tighter, to fit A4. */
    .document-table--wide {
      font-size: 10px;

      & th,
      & td {
        padding: 3px 4px;
      }
    }
  }
}
