/*
 * The keystats report (companies/keystats/show): the company Overview's
 * sidebar and main column, and the panels in them that have no component of
 * their own — the capital ratios, the valuation percentiles, the aside.
 */
@layer components {
  /*
   * The keystats report. From 1280px: the sidebar in a fixed column to the
   * left, the main column (.keystats__main, a grid) beside it, in source
   * order. Below 1280px: one column in reading order — the annual grid, the
   * chart and target (.keystats__overview), the revenue flow (M23), the
   * sidebar, the peers, the
   * business profile, the commentary, the rating changes, the holders, pay
   * and ownership from the proxy, the news, the metric links. `.keystats__main` is `display: contents` there, so its children are
   * items of this flex column and take their places by `order`. The desktop
   * DOM order and layout are unchanged.
   *
   * The catch: below 1280px `.keystats__main` has no box, so a gap, margin,
   * padding or border set on it silently stops applying there. The spacing
   * between its children is this column's gap at both widths.
   */
  .keystats {
    display: flex;
    flex-direction: column;
    gap: var(--space-lg);

    @media (width >= 1280px) {
      flex-direction: row;
      align-items: flex-start;
    }

    /* With the alert drawer open (components/alert-drawer.css) the page is
       a drawer's width narrower than the window says, so the sidebar goes
       under the main column and the price chart keeps the width its alert
       lines are set against. */
    .app:has(.alert-drawer[open]) & {
      @media (width >= 1280px) {
        flex-direction: column;
        align-items: stretch;

        & .keystats__sidebar {
          order: 3;
          inline-size: auto;
        }
      }
    }
  }

  /* The overview's three forecast columns as one Powerpack lock
     (M04-D4, G3): a cell spanning every row, still in the
     forecast tint, its lock in the middle. Upright: the italic is the
     forecast figures', and there are none. */
  .keystats__forecast-lock {
    vertical-align: middle;
    font-style: normal;
    white-space: normal;
  }

  /* Capital & Financial Ratios (companies/keystats/_capital_ratios): four
     groups, each a caps label over its rows, a little air between them —
     one column in the wide screen's sidebar, two where the card runs wider
     than two lists of label and figure need. */
  .keystats__ratios {
    container: ratios / inline-size;
  }

  /* A row the stat bar already shows: on paper only (print.css). */
  .keystats__ratio--stat-bar {
    display: none;
  }

  .keystats__ratio-groups {
    display: grid;
    gap: 0 var(--space-xl);

    @container ratios (width >= 40rem) {
      grid-template-columns: repeat(2, minmax(0, 1fr));
      align-items: start;
    }
  }

  .keystats__ratio-group {
    display: grid;
    gap: var(--space-2xs);
    padding-block-start: var(--space-sm);
  }

  /* Valuation's second half (companies/keystats/_valuation, M13-D7): the EV/Sales
     line, linked, then each valuation percentile's row over its range track. */
  .keystats__score-link {
    text-decoration: none;
  }

  /* EV/Sales over the company's own ten years (M17-D8). */
  .keystats__history {
    padding-block: var(--space-sm);
    border-block-end: var(--rule-hairline);

    & .keystats__score-link {
      display: block;
      color: inherit;
    }
  }

  .keystats__percentile {
    padding-block-end: var(--space-xs);
    border-block-end: var(--rule-hairline);

    & .stat-list__row {
      border-block-end: 0;
    }

  }

  /* From 1280px, one column that never grows past its box: the charts and
     tables inside scroll within themselves instead of widening the page.

     Below it the sidebar gives up its box, as .keystats__main does, so its
     cards are items of the one column: the latest quarter straight after
     the chart and target (order 2, and earlier in the source than the
     annual grid), the rest where the sidebar always went. */
  .keystats__sidebar {
    order: 3;
    display: contents;

    & > * {
      order: 3;
      min-inline-size: 0;
    }

    & > .keystats__quarter {
      order: 2;
    }

    /* A new listing's card (companies/keystats/_listing), where the latest
       quarter goes for a company that has one: straight after the chart. */
    & > .keystats__listing {
      order: 2;
    }

    @media (width >= 1280px) {
      order: 1;
      display: grid;
      flex: none;
      grid-template-columns: minmax(0, 1fr);
      gap: var(--space-md);
      inline-size: 23.75rem;
    }
  }

  /* The Earnings Trends card (companies/keystats/_earnings_trends) in the
     sidebar's fixed column: the phone's plot and dots, not the tablet's. */
  .keystats__sidebar .earnings-trends {
    @media (width >= 1280px) {
      --earnings-trends-plot-height: 12rem;
      --earnings-trends-dot: 0.875rem;
    }
  }

  .keystats__main {
    display: contents;

    @media (width >= 1280px) {
      order: 2;
      display: grid;
      grid-template-columns: minmax(0, 1fr);
      gap: var(--space-lg);
      flex: 1;
      min-inline-size: 0;
    }
  }

  /* The main column's children, placed in the one-column reading order.
     From 1280px they are grid items of .keystats__main and keep its source
     order. */
  .keystats__financials,
  .keystats__overview,
  .keystats__flow,
  .keystats__peers,
  .keystats__profile,
  .keystats__commentary,
  .keystats__ratings,
  .keystats__holders,
  .keystats__proxy,
  .keystats__news,
  .keystats__metrics {
    min-inline-size: 0;
  }

  @media (width < 1280px) {
    .keystats__overview { order: 1; }
    .keystats__financials { order: 2; }
    .keystats__profile { order: 2; }
    .keystats__flow { order: 2; }
    .keystats__peers { order: 4; }
    .keystats__commentary { order: 4; }
    .keystats__ratings { order: 4; }
    .keystats__holders { order: 4; }
    .keystats__proxy { order: 4; }
    .keystats__news { order: 5; }
    .keystats__metrics { order: 6; }
  }

  .keystats__overview {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);

    @media (width >= 768px) {
      flex-direction: row;
    }
  }

  /* The price chart's height, read by the chart (.company-chart) and by the
     placeholder it replaces, so the page does not jump when it lands. 570px
     from 768px is `Companies::PriceChart::HEIGHT`; change both together. */
  .keystats__chart {
    --company-chart-height: 20rem;

    flex: 1;
    min-inline-size: 0;

    @media (width >= 768px) {
      --company-chart-height: 570px;
    }
  }

  .keystats__aside {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
    gap: var(--space-md);

    @media (width >= 768px) {
      flex: none;
      inline-size: 14.375rem;
    }
  }

  /* Under the annual grid on a phone, where it shows four years of ten: the
     way to the rest. */
  .keystats__all-years {
    margin: 0;
    padding-inline: var(--space-md);
    font-size: var(--text-ui-sm);
    font-weight: var(--weight-semibold);

    & a {
      display: inline-flex;
      align-items: center;
      min-block-size: var(--target-min);
    }

    @media (width >= 640px) {
      display: none;
    }
  }
}
