/*
 * The filing reader (tasks/growth/milestones/35): a 10-K, 10-Q or 8-K read
 * back from the SEC's HTML into the site's type, at
 * /:ticker/filings/:accession/document. Its markup is Filings::ReadableDocument's
 * and Filings::ReadableTable's, drawn by companies/filing_documents/_document.
 *
 * Prose is set like an article — the serif at the reading measure — and each
 * Item opens on a heavy rule, as a section does. Tables run the page's full
 * width in the data face: figures right-aligned and tabular, a total ruled
 * above and a grand total heavy-ruled below where the filer ruled them, and the
 * row label pinned while a wide table scrolls. A table kept with the filer's
 * own grid says "As filed" over it, its spacer cells closed up; one of words
 * stays in the serif.
 *
 * The cover (everything before the first Item) is set apart, as the SEC sets
 * it: tight lines in the UI face, centred, its own tables unruled. What the
 * filer set in bold — the name, the form, the file number, the answers — is in
 * ink; the plain lines that label them are muted. Images sit on the paper
 * rather than on the white they were saved on. A figure table bands every
 * other row, as the SEC's statements do; it is the reader's own, not the
 * site's data tables'.
 *
 * It is a component rather than a page's stylesheet so /styleguide#filing-document
 * can draw it.
 */
@layer components {
  .filing-document {
    color: var(--color-ink);
    font-family: var(--font-prose);
    font-size: var(--text-prose);
    line-height: var(--leading-prose);
    text-wrap: pretty;

    & :is(p, ul, ol, h3, h4) {
      max-inline-size: var(--measure);
    }

    & p {
      margin-block: 0 1em;
    }

    & a {
      color: var(--color-link);
    }

    & sup,
    & sub {
      line-height: 0;
    }
  }

  .filing-document__item {
    margin-block: var(--space-2xl) var(--space-sm);
    padding-block-start: var(--space-xs);
    border-block-start: var(--rule-heavy);
    font-size: var(--text-h2);
    line-height: var(--leading-h2);
    font-weight: var(--weight-semibold);
    scroll-margin-block-start: var(--space-md);
  }

  .filing-document__heading {
    margin-block: 1.5em 0.4em;
    font-size: var(--text-h4);
    line-height: var(--leading-h4);
    font-weight: var(--weight-semibold);

    &:is(h4) {
      font-size: var(--text-prose);
      font-style: italic;
      font-weight: var(--weight-regular);
    }
  }

  .filing-document__centred {
    margin-inline: auto;
    text-align: center;
  }

  .filing-document__figure img {
    max-inline-size: 100%;
    block-size: auto;
    mix-blend-mode: multiply;
  }

  .filing-cover {
    margin-block-end: var(--space-xl);
    padding-block: var(--space-md);
    border-block-end: var(--rule-hairline);
    color: var(--color-ink-muted);
    font-family: var(--font-ui);
    font-size: var(--text-ui-sm);
    line-height: var(--leading-ui-sm);

    & p {
      margin-block: 0 var(--space-xs);
    }

    & :is(p, .filing-document__centred) {
      max-inline-size: none;
    }

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

    & .filing-document__figure img {
      margin: var(--space-sm) auto;
    }

    & .filing-table-scroll {
      margin-block: var(--space-sm);
    }

    & .filing-table {
      margin-inline: auto;
      font-family: inherit;
      font-size: inherit;
      line-height: inherit;
    }

    & .filing-table :is(td, th) {
      padding: var(--space-3xs) var(--space-xs);
      border: 0;
      text-align: center;
    }

    & .filing-table__label {
      position: static;
      min-inline-size: 0;
      max-inline-size: none;
      background-color: transparent;
    }
  }

  /* The filer's lists, and its tables of bullets read as one: the reset
     strips list markers, and a filing's lists are read by them. */
  .filing-document :is(ul, ol),
  .filing-document__list {
    padding-inline-start: var(--space-lg);
    list-style: disc;
  }

  .filing-document ol {
    list-style: decimal;
  }

  /* A statement's units and its pointer to the notes: a caption, not a line
     of the text. */
  .filing-document .filing-document__caption {
    margin-block: 0 var(--space-xs);
    color: var(--color-ink-muted);
    font-family: var(--font-ui);
    font-size: var(--text-meta);
    line-height: var(--leading-meta);
  }
  /* A caption after its table (the pointer to the notes) closes the
     statement: the next one starts well clear of it. */
  .filing-document .filing-table-scroll + .filing-document__caption {
    margin-block-end: var(--space-4xl);
  }


  .filing-document__note {
    color: var(--color-ink-muted);
    font-size: var(--text-meta-xs);
  }

  .filing-document__plain-text {
    overflow-x: auto;
    font-family: var(--font-data);
    font-size: var(--text-data);
    line-height: var(--leading-data);
    white-space: pre;
  }

  .filing-document__source {
    margin-block-start: var(--space-xl);
    padding-block-start: var(--space-xs);
    border-block-start: var(--rule-hairline);
  }

  .filing-table-scroll {
    margin-block: var(--space-sm) var(--space-lg);
  }

  /* A figure table is as wide as its columns, never narrower than the text
     beside it, and up to the window's width (page-card--full) before it
     scrolls. The track is sized here, in the text's face, so the measure is
     the paragraphs' and not the narrower data face's. */
  .filing-table-scroll:has(> .filing-table--figures) {
    display: grid;
    grid-template-columns: minmax(min(100%, var(--measure)), max-content);
  }

  .filing-table {
    border-collapse: separate;
    border-spacing: 0;
    font-family: var(--font-prose);
    font-size: var(--text-prose-sm);
    line-height: var(--leading-prose-sm);

    & :is(th, td) {
      padding: 0.3125rem var(--space-xs);
      border-block-end: var(--rule-hairline);
      text-align: start;
      vertical-align: top;
    }

    & thead th {
      color: var(--color-ink-muted);
      font-family: var(--font-data);
      font-size: var(--text-table-head);
      line-height: var(--leading-table-head);
      font-weight: var(--weight-medium);
      text-align: end;
      vertical-align: bottom;
      min-inline-size: 5rem;
    }

    & thead th[colspan] {
      text-align: center;
    }

    & thead th:first-child {
      text-align: start;
    }

    & thead tr:last-child th {
      border-block-end: var(--rule-width) solid var(--color-rule-strong);
    }
  }

  .filing-table--figures {
    inline-size: 100%;
    font-family: var(--font-data);
    font-size: var(--text-data);
    line-height: var(--leading-data);
    font-variant-numeric: tabular-nums lining-nums;

    /* Rows set close, ruled only where the filer ruled them: a hairline
       under every row hid the subtotal rules that give a statement its
       shape. A group's label opens a space above it. */
    & td {
      padding-block: var(--space-2xs);
      border-block-end: 0;
      vertical-align: bottom;
    }

    & tbody tr:last-child td {
      border-block-end: var(--rule-hairline);
    }

    /* Every other row on the sunken paper, as the SEC bands its statements,
       so a figure is easy to follow to its label across a wide table. The
       pinned label takes the band too. */
    & tbody tr:nth-child(even) td {
      background-color: var(--color-surface-sunken);
    }

    & tr.is-group td {
      padding-block-start: var(--space-sm);
    }

    & td:not(.filing-table__label) {
      text-align: end;
      white-space: nowrap;
    }

    & tr.is-subtotal td {
      font-weight: var(--weight-semibold);
    }

    & tr.has-rule td:not(:first-child) {
      border-block-start: var(--rule-width) solid var(--color-rule-strong);
    }

    /* The filer's double rule under a grand total is the heavy rule here:
       the double rule is the masthead's alone (agents/DESIGN.md §5). */
    & tr.has-double-rule td:not(:first-child) {
      border-block-end: var(--rule-heavy);
    }
  }

  /* The filer's own grid: its empty spacer cells take no room, so a figure
     sits beside its `$` and its `)` as it was filed. */
  .filing-table--as-filed :is(td, th):empty {
    padding-inline: 0;
  }

  .filing-table__label {
    position: sticky;
    inset-inline-start: 0;
    z-index: 1;
    min-inline-size: 9rem;
    max-inline-size: 11rem;
    background-color: var(--color-surface);
    white-space: normal;

    @media (width >= 768px) {
      min-inline-size: 16rem;
      max-inline-size: 28rem;
    }
  }

  /* Above the cell padding's own rule, so the filer's indent shows. */
  .filing-table .filing-table__label {
    padding-inline-start: calc(var(--space-xs) + var(--indent, 0) * var(--space-xs));

    @media (width >= 768px) {
      padding-inline-start: calc(var(--space-xs) + var(--indent, 0) * var(--space-md));
    }
  }

  .filing-table__figure {
    white-space: nowrap;
  }

  .filing-table__caption {
    caption-side: top;
    padding-block-end: var(--space-3xs);
    text-align: start;
  }
}
