/*
 * The picture over a statement (companies/financials/_*_picture,
 * Companies::StatementPicture): its sentence, charts and figures, laid out
 * by the card's own width (the `statement-picture` container).
 */
@layer components {
  /* The picture over a statement (companies/financials/_*_picture,
     Companies::StatementPicture): the heading, a sentence about this company
     with the way to what the statement is, then the bridge, the balance
     sheet's columns or the trend. On a wide page the income statement's
     bridge and its margins sit side by side. */
  /* The sentence close under the heading, and the charts set well apart
     from it (owner, 2026-11-19). */
  .statement-picture {
    container: statement-picture / inline-size;
    display: grid;
    gap: var(--space-xs);
    margin-block-end: var(--space-lg);
  }

  /* On the card's children, to outweigh the cash flow's figures' own
     `margin: 0` further down. */
  .statement-picture > .statement-picture__lede + *,
  .statement-picture > .statement-picture__stats + .statement-picture__body {
    margin-block-start: var(--space-xl);
  }

  .statement-picture__body + .statement-picture__foot {
    margin-block-start: var(--space-xs);
  }

  .statement-picture__lede {
    max-inline-size: var(--measure);
    color: var(--color-ink-soft);
    font-family: var(--font-prose);
    font-size: var(--text-prose);
    line-height: var(--leading-prose);
    text-wrap: pretty;
  }

  .statement-picture__about {
    font-family: var(--font-ui);
    font-size: var(--text-ui-sm);
    white-space: nowrap;
  }

  /* On a phone the waterfall fits the card rather than scrolling (owner,
     2026-11-19): its columns share the width, and its figures, shares and
     names are turned to read upwards, set at the smallest size, each hung
     from the top of its row. Turned with `writing-mode` and a half turn, the
     inline end is the top and the inline start the foot. Below 41rem, where
     nine columns of 4.5rem stop fitting. */
  @container statement-picture (width < 41rem) {
    .statement-picture .waterfall-scroll {
      padding-block-end: 0;
      overflow: visible;
    }

    .statement-picture .waterfall {
      grid-template-columns: repeat(var(--steps), minmax(0, 1fr));
    }

    .statement-picture :is(.waterfall__value, .waterfall__share, .waterfall__label) {
      justify-self: center;
      padding: 0;
      font-size: var(--text-meta-xs);
      line-height: var(--leading-meta-xs);
      text-align: end;
      white-space: nowrap;
      writing-mode: vertical-rl;
      rotate: 180deg;
    }

    .statement-picture .waterfall__value {
      padding-inline-start: var(--space-2xs);
    }

    .statement-picture .waterfall__share,
    .statement-picture .waterfall:not(.waterfall--with-share) .waterfall__value {
      padding-inline-start: var(--space-xs);
    }

    .statement-picture .waterfall__label {
      padding-inline-end: var(--space-xs);
    }
  }

  /* One track the card's width: an implicit one grew to the waterfall's
     widest columns and pushed a phone's page wider than its screen, where
     the waterfall should scroll in its own box. */
  .statement-picture__body {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-xl);
  }

  /* The charts, and the cash flow's figures over them, stop stretching at
     the chart measure. */
  .statement-picture__body,
  .statement-picture__stats {
    max-inline-size: var(--chart-measure);
  }

  /* A statement's charts take at most half the screen (owner, 2026-11-19):
     stacked on a narrow card, the two together; side by side, each. The
     trend's drawing is capped here, through its width so its period labels
     stay under their points, and the chart beside it stretches to it. */
  .statement-picture__body .trend-chart__view {
    max-inline-size: calc(max(8rem, 25svh - 4rem) * var(--aspect));
  }

  @container statement-picture (width >= 48rem) {
    .statement-picture__body .trend-chart__view {
      max-inline-size: calc(max(12rem, 50svh - 7rem) * var(--aspect));
    }

    .statement-picture__body {
      grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
      gap: var(--space-2xl);
      align-items: start;
    }
  }

  @container statement-picture (width >= 48rem) {
    /* Every statement's two charts, side by side: each stretches to the
       other's height. */
    .statement-picture__body--paired {
      align-items: stretch;
    }

    .statement-picture__chart {
      display: grid;
    }

    /* A waterfall in half the card: narrower columns, so nine steps still
       fit without scrolling. */
    .statement-picture__chart .waterfall {
      grid-template-columns: repeat(var(--steps), minmax(3rem, 1fr));
    }

    .balance-columns {
      grid-template-rows: auto minmax(10rem, 1fr) auto;
    }

    /* The income statement's and the cash flow's: the waterfall and the
       trend on five shared rows (owner, 2026-11-19) — the waterfall's
       figures beside the trend's title; its shares; the two drawings; its
       names beside the trend's period labels, so its foot and the trend's x
       axis are one line; the trend's key. Each part is a subgrid down to
       its cells, with no gaps of its own, so the rows stay shared. */
    .statement-picture__body--aligned {
      grid-template-rows: auto auto minmax(0, 1fr) auto auto;
      row-gap: 0;
    }

    .statement-picture__body--aligned > *,
    .statement-picture__body--aligned .waterfall-scroll,
    .statement-picture__body--aligned .waterfall,
    .statement-picture__body--aligned .trend-chart {
      display: grid;
      grid-row: 1 / -1;
      grid-template-rows: subgrid;
      row-gap: 0;
    }

    /* Wide enough here not to scroll, and a scroller's padding would
       take room from the shared rows. */
    .statement-picture__body--aligned .waterfall-scroll {
      padding: 0;
      overflow: visible;
    }

    .statement-picture__body--aligned .waterfall__value,
    .statement-picture__body--aligned .trend-chart__caption {
      grid-row: 1;
    }

    .statement-picture__body--aligned .waterfall__share {
      grid-row: 2;
    }

    .statement-picture__body--aligned .waterfall__plot {
      grid-row: 3;
      min-block-size: 10rem;
    }

    .statement-picture__body--aligned .waterfall__label {
      grid-row: 4;
    }

    .statement-picture__body--aligned .trend-chart__view {
      grid-row: 3 / span 2;
      grid-template-rows: subgrid;
    }

    /* At the foot of its row, whichever chart sets the row's height, and
       drawn down by the strip under its x axis (a margin's percentage is of
       the width), so the axis itself is the row's foot and the strip, with
       the lowest axis label, hangs into the period labels' padding. */
    .statement-picture__body--aligned .trend-chart__plot {
      align-self: end;
      margin-block-end: calc(-100% * var(--foot));
    }

    .statement-picture__body--aligned .trend-chart__ticks {
      padding-block-start: var(--space-xs);
    }

    .statement-picture__body--aligned .trend-chart__keys {
      grid-row: 5;
      padding-block-start: var(--space-sm);
    }

    /* The balance sheet's on the same rows: the columns' date beside the
       trend's sentence, the two columns standing on the trend's x axis, and
       their note, its rule the same line, beside the period labels. The
       trend and its sentence share their wrapper's rows. */
    .statement-picture__body--aligned .statement-picture__trend .trend-chart {
      grid-column: 1;
    }

    .statement-picture__body--aligned :is(.balance-columns__caption, .statement-picture__trend-sentence) {
      grid-row: 1;
      grid-column: 1;
      padding-block-end: var(--space-sm);
    }

    .statement-picture__body--aligned .balance-columns__pair {
      grid-row: 3;
    }

    .statement-picture__body--aligned .balance-columns__note {
      grid-row: 4;
    }

    /* A container cannot be a subgrid, so here the trend is not one: which
       drawing it shows is read off the card's width instead, at the column
       width its own query uses (30rem, the card being two columns and a 2rem
       gap). Capped at the chart measure, a column never reaches the wide
       drawing's 48rem. */
    .statement-picture__body--aligned .trend-chart {
      container-type: normal;
    }

    .statement-picture__body--aligned .trend-chart__view.trend-chart__view--wide,
    .statement-picture__body--aligned .trend-chart__view.trend-chart__view--narrow {
      display: none;
    }

    .statement-picture__body--aligned .trend-chart__view.trend-chart__view--small {
      display: grid;
    }
  }

  @container statement-picture (width >= 62rem) {
    .statement-picture__body--aligned .trend-chart__view.trend-chart__view--narrow {
      display: grid;
    }

    .statement-picture__body--aligned .trend-chart__view.trend-chart__view--small {
      display: none;
    }
  }

  /* The trend beside the columns, its sentence over it. */
  .statement-picture__trend {
    display: grid;
    gap: var(--space-sm);
  }

  .statement-picture__trend-sentence {
    color: var(--color-ink-soft);
    font-size: var(--text-ui-sm);
    line-height: var(--leading-ui-sm);
  }

  /* Metrics & Ratios: four panels, two by two once there is room. */
  .statement-picture__panels {
    display: grid;
    gap: var(--space-xl) var(--space-2xl);
  }

  /* Four panels in a row on a wide page, two on a narrower one, where a
     panel's chart stops growing at a little past its drawn size. */
  @container statement-picture (width >= 36rem) {
    .statement-picture__panels {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .statement-picture__panel .trend-chart__view {
      max-inline-size: 24rem;
    }

    /* Panels side by side share their rows — heading, sentence, drawing,
       period labels, key — so a sentence a line longer than its
       neighbour's no longer lifts or drops its chart, and every chart's x
       axis is one line (owner, 2026-11-19). The drawing stands at the foot
       of its row, pulled down by the strip under its axis. */
    .statement-picture__panels {
      row-gap: var(--space-xs);
    }

    .statement-picture__panel {
      grid-row: span 5;
      grid-template-rows: subgrid;
      padding-block-end: var(--space-lg);
    }

    .statement-picture__panel .trend-chart {
      grid-row: span 3;
      grid-template-rows: subgrid;
    }

    .statement-picture__panel .trend-chart__view {
      grid-row: span 2;
      grid-template-rows: subgrid;
    }

    .statement-picture__panel .trend-chart__plot {
      align-self: end;
      margin-block-end: calc(-100% * var(--foot));
    }
  }

  @container statement-picture (width >= 68rem) {
    .statement-picture__panels {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  .statement-picture__panel {
    display: grid;
    gap: var(--space-xs);
    align-content: start;
    padding-block-start: var(--space-xs);
    border-block-start: var(--rule-hairline);
  }

  .statement-picture__fact {
    color: var(--color-ink-soft);
    font-size: var(--text-ui);
    line-height: var(--leading-ui);
  }

  .statement-picture__stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-md) var(--space-lg);
    margin: 0;
    padding-block: var(--space-sm);
    border-block: var(--rule-hairline);

    & dd {
      display: grid;
      gap: var(--space-3xs);
      margin: 0;
    }
  }

  @container statement-picture (width >= 40rem) {
    .statement-picture__stats {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  .statement-picture__stat {
    display: grid;
    gap: var(--space-3xs);
    align-content: start;
  }

  .statement-picture__figure {
    font-family: var(--font-prose);
    font-size: var(--text-stat);
    font-weight: var(--weight-semibold);
    line-height: var(--leading-stat);
  }

  .statement-picture__note,
  .statement-picture__foot {
    color: var(--color-ink-subtle);
    font-size: var(--text-meta);
    line-height: var(--leading-meta);
  }
}
