@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@import '/assets/f47cf25678047dc1d0c5372c25b330b4ffdf35604610caa62c68ff9ad26b877e/components/statistic/statistic.css';
@layer cinder.components {
  /* The root is the query container; the grid is its child. An element can
     never query itself -- a container query resolves against the nearest
     ANCESTOR container -- so with `container-type` on the grid the collapse
     rules below never fired for a standalone group (CIN-499). The container
     sits on the public root, not on an extra wrapper around it, so the root
     stays the element that receives a consumer's `class`, `style`, and
     `...rest`: constraining the group's inline size constrains exactly what
     the queries measure, and a group nested inside another group queries its
     own width rather than the outer's (RESPONSIVE-POLICY.md).

     A size query measures the container's CONTENT box. The `default` variant
     pads its root, so its 30rem and 18rem thresholds are the width the cells
     actually share, not the group's outer width. */
  .cinder-statistic-group {
    container-type: inline-size;
    container-name: cinder-statistic-group;
  }

  .cinder-statistic-group__grid {
    display: grid;
    gap: var(--cinder-statistic-group-gap, var(--cinder-space-4, 1rem));
  }

  /* The stats in one row share their rows: each stat's label, value, caption and change sit in
     the group's own row tracks (a subgrid), so a label that wraps makes its whole row of labels
     taller and every value stays on one line with its neighbours' (2026-11-28 review). The
     subgrid takes no gap of its own: the space between a stat's parts is a margin on each part
     after the label, so a stat with no caption or change adds no empty gap under its value. Nor
     does the grid have a row gap, because it would fall between a stat's own four tracks: the
     space between rows of stats is a margin under each stat, and the grid hands the last row's
     back (shared-borders' 1px divider shows through that margin as it did through the gap). */
  .cinder-statistic-group__grid {
    row-gap: 0;
    margin-block-end: calc(var(--cinder-statistic-group-gap, var(--cinder-space-4)) * -1);
  }

  .cinder-statistic-group__grid > .cinder-statistic {
    grid-row: span 4;
    grid-template-rows: subgrid;
    row-gap: 0;
    /* A cell that draws a divider under itself (the default variant, where cells stack) sets
       `--_cinder-statistic-divider-space` to half the gap, which splits the gap across the rule:
       that half as padding above it and the other half as margin below it, so the rule sits
       midway between two statistics. Every other cell keeps the whole gap as margin. */
    padding-block-end: var(--_cinder-statistic-divider-space, 0);
    margin-block-end: var(
      --_cinder-statistic-divider-space,
      var(--cinder-statistic-group-gap, var(--cinder-space-4))
    );
  }

  .cinder-statistic-group__grid
    > .cinder-statistic
    > :is(.cinder-statistic__value, .cinder-statistic__caption, .cinder-statistic__change) {
    margin-block-start: var(--cinder-statistic-row-gap, var(--cinder-space-1));
  }

  .cinder-statistic-group[data-cinder-columns='auto'] > .cinder-statistic-group__grid {
    grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
  }

  .cinder-statistic-group[data-cinder-columns='1'] > .cinder-statistic-group__grid {
    grid-template-columns: 1fr;
  }

  .cinder-statistic-group[data-cinder-columns='2'] > .cinder-statistic-group__grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .cinder-statistic-group[data-cinder-columns='3'] > .cinder-statistic-group__grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .cinder-statistic-group[data-cinder-columns='4'] > .cinder-statistic-group__grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  /* `auto` is `auto-fit` for every count but three and four, whose column counts leave a lone
     last cell (COR-3000). Both are quantity queries on the grid's own children (`:has()` on a
     last child at that position), so they count what actually lays out, with no script.

     Exactly three statistics are three columns, and one column at or below 30rem of content
     width (the ≤30rem block below), the step `columns={3}` takes, so they never lay out as 2 + 1.

     Exactly four step 1, 2, 4 columns and never 3 + 1: one column below 33rem (the width at
     which two 16rem tracks and their 1rem gap fit), two from there, and four from 67rem (four
     16rem tracks and three gaps). The steps are written out rather than left to `auto-fit`,
     which would reach three tracks from 50rem, and from sooner at a smaller gap. */
  .cinder-statistic-group[data-cinder-columns='auto']
    > .cinder-statistic-group__grid:has(> :nth-child(3):last-child) {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .cinder-statistic-group[data-cinder-columns='auto']
    > .cinder-statistic-group__grid:has(> :nth-child(4):last-child) {
    grid-template-columns: 1fr;
  }

  @container cinder-statistic-group (min-width: 33rem) {
    .cinder-statistic-group[data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child) {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }

  @container cinder-statistic-group (min-width: 67rem) {
    .cinder-statistic-group[data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child) {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  /* Fixed multi-column layouts would squeeze each stat below readability on a
     narrow group. Collapse to two columns under 30rem, then a single column
     under 18rem, so values never crush. The `auto` mode already self-collapses
     via auto-fit, so it is intentionally excluded. */
  @container cinder-statistic-group (max-width: 30rem) {
    .cinder-statistic-group[data-cinder-columns='3'] > .cinder-statistic-group__grid,
    .cinder-statistic-group[data-cinder-columns='4'] > .cinder-statistic-group__grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    /* Exactly three statistics never lay out as 2 + 1 (COR-3000): they go straight to one
       column, fixed or automatic. It is a quantity query (a third child and no fourth), so
       other counts keep the two-column step. */
    .cinder-statistic-group:is([data-cinder-columns='3'], [data-cinder-columns='auto'])
      > .cinder-statistic-group__grid:has(> :nth-child(3):last-child) {
      grid-template-columns: 1fr;
    }
  }

  @container cinder-statistic-group (max-width: 18rem) {
    .cinder-statistic-group[data-cinder-columns='2'] > .cinder-statistic-group__grid,
    .cinder-statistic-group[data-cinder-columns='3'] > .cinder-statistic-group__grid,
    .cinder-statistic-group[data-cinder-columns='4'] > .cinder-statistic-group__grid {
      grid-template-columns: 1fr;
    }
  }

  /* variant: default — the GroupedList's surface (the raised fill, no border, the surface radius,
     a 16px inset), with per-cell dividers, so a stat set and the lists beside it read as one kind
     of surface. It was a grey inset fill with a resting border and a smaller radius (2026-11-28
     review). */
  .cinder-statistic-group[data-cinder-variant='default'] {
    padding: var(--cinder-statistic-group-card-padding, var(--cinder-space-4, 1rem));
    border: 0;
    border-radius: var(--cinder-radius-surface);
    background: var(--cinder-surface-raised);
    position: relative;
  }

  /* The page hairline parts the white group from the white light page; transparent in dark. It is
     drawn on an overlay, so it takes no layout in either theme (COR-2697). */
  .cinder-statistic-group[data-cinder-variant='default']::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    border: var(--cinder-border-width) solid var(--_cinder-page-edge);
  }

  @media (forced-colors: active) {
    .cinder-statistic-group[data-cinder-variant='default'] {
      border: 1px solid CanvasText;
    }
  }

  /* ----------------------------------------
   * default-variant dividers
   *
   * A divider belongs BETWEEN two adjacent cells, so a divider rule is only correct
   * where the effective column count is knowable. For the fixed counts that is the
   * declared count at wide widths, then the collapsed count under the same 30rem and
   * 18rem thresholds the layout rules use above -- the dividers must describe the
   * grid that actually renders, so they mirror those thresholds exactly.
   *
   * For `columns='auto'` the count is not knowable: `repeat(auto-fit, ...)` renders
   * however many 16rem tracks fit, without an upper bound, and CSS cannot select "the
   * last cell in a row" without that number. So `auto` carries no per-cell dividers,
   * keeping the variant's resting border, inset surface, and gap. That is what makes
   * `default` read as deliberate rather than unstyled (CIN-113); the dividers are an
   * enhancement applied where they can be drawn correctly. The one exception is an `auto`
   * group of exactly three statistics (COR-3000): its count is known, three columns or one,
   * so it draws the same dividers as `columns={3}`. So does an `auto` group of exactly four,
   * whose steps are 1, 2 and 4 tracks: it draws what `columns={4}` draws at that layout.
   *
   * A divider drawn under a cell (`border-block-end`, where cells stack) sits in the middle of
   * the space between two statistics: the gap is split, half as `padding-block-end` inside
   * the cell, above the rule, and half as `margin-block-end`, below it. A cell with no
   * divider keeps the whole gap as margin.
   *
   * SPECIFICITY IS LOAD-BEARING. Every enabler, row-end suppressor, and band rule
   * in this section is written as
   * `.cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='N']
   *   > .cinder-statistic-group__grid > .cinder-statistic:<one pseudo-class>` --
   * exactly (0,6,0) -- so that among them source order alone decides the cascade
   * and a later band can override an earlier one. The one exception is the
   * final-cell reset at the end, which drops `[data-cinder-columns]` and is (0,5,0):
   * it only ever matches `:last-child`, which every enabler excludes with
   * `:not(:last-child)`, and the row-end suppressors that can also match the last
   * cell set the same `none`, so it never competes with a higher-specificity rule.
   * An earlier revision enabled dividers with ONE generic rule carrying two
   * `:not([data-cinder-columns=...])` clauses, which out-ranked every row-end
   * suppressor below it, so a multi-row group drew a divider at the end of every
   * row but the last. Do not "simplify" the enablers back into one rule.
   * ---------------------------------------- */

  /* --- Declared layout (wide): vertical dividers, row ends suppressed --- */

  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='2']
    > .cinder-statistic-group__grid
    > .cinder-statistic:not(:last-child),
  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='3']
    > .cinder-statistic-group__grid
    > .cinder-statistic:not(:last-child),
  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='4']
    > .cinder-statistic-group__grid
    > .cinder-statistic:not(:last-child) {
    border-inline-end: 1px solid var(--cinder-border-muted);
  }

  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='2']
    > .cinder-statistic-group__grid
    > .cinder-statistic:nth-child(2n),
  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='3']
    > .cinder-statistic-group__grid
    > .cinder-statistic:nth-child(3n),
  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='4']
    > .cinder-statistic-group__grid
    > .cinder-statistic:nth-child(4n) {
    border-inline-end: none;
  }

  /* --- Automatic, exactly three: three tracks, so vertical dividers (COR-3000) --- */

  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='auto']
    > .cinder-statistic-group__grid:has(> :nth-child(3):last-child)
    > .cinder-statistic:not(:last-child) {
    border-inline-end: 1px solid var(--cinder-border-muted);
  }

  /* --- Automatic, exactly four: the steps 1, 2, 4 are known, so they draw what `columns={4}`
     draws at the equivalent layout (the stacked rule, the two-track rule, the four-track
     rule). The thresholds are the ones the layout rules use (33rem and 67rem). Every rule
     carries the same `:has()` quantity query, so source order decides among them. --- */

  @container cinder-statistic-group (width < 33rem) {
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child)
      > .cinder-statistic:not(:last-child) {
      border-block-end: 1px solid var(--cinder-border-muted);
      /* @runtime-state */
      --_cinder-statistic-divider-space: calc(
        var(--cinder-statistic-group-gap, var(--cinder-space-4)) / 2
      );
    }
  }

  @container cinder-statistic-group (min-width: 33rem) {
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child)
      > .cinder-statistic:not(:last-child) {
      border-inline-end: 1px solid var(--cinder-border-muted);
    }

    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child)
      > .cinder-statistic:nth-child(2n) {
      border-inline-end: none;
    }
  }

  @container cinder-statistic-group (min-width: 67rem) {
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='auto']
      > .cinder-statistic-group__grid:has(> :nth-child(4):last-child)
      > .cinder-statistic:not(:last-child) {
      border-inline-end: 1px solid var(--cinder-border-muted);
    }
  }

  /* --- Single column (declared): horizontal dividers --- */

  .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='1']
    > .cinder-statistic-group__grid
    > .cinder-statistic:not(:last-child) {
    border-block-end: 1px solid var(--cinder-border-muted);
    /* @runtime-state */
    --_cinder-statistic-divider-space: calc(
      var(--cinder-statistic-group-gap, var(--cinder-space-4)) / 2
    );
  }

  /* --- Collapsed to two tracks (<= 30rem): 3 and 4 behave as 2 --- */

  @container cinder-statistic-group (max-width: 30rem) {
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='3']
      > .cinder-statistic-group__grid
      > .cinder-statistic:not(:last-child),
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='4']
      > .cinder-statistic-group__grid
      > .cinder-statistic:not(:last-child) {
      border-inline-end: 1px solid var(--cinder-border-muted);
    }

    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='3']
      > .cinder-statistic-group__grid
      > .cinder-statistic:nth-child(2n),
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='4']
      > .cinder-statistic-group__grid
      > .cinder-statistic:nth-child(2n) {
      border-inline-end: none;
    }

    /* Exactly three statistics, fixed or automatic, are one track here (COR-3000), so the
       divider is horizontal. It carries the `:has()` quantity query, which outranks the (0,6,0)
       rules above by specificity, and ties the automatic group's vertical divider above, which
       this block follows, so it does not depend on its position among the fixed rules. */
    .cinder-statistic-group[data-cinder-variant='default']:is(
        [data-cinder-columns='3'],
        [data-cinder-columns='auto']
      )
      > .cinder-statistic-group__grid:has(> :nth-child(3):last-child)
      > .cinder-statistic:not(:last-child) {
      border-inline-end: none;
      border-block-end: 1px solid var(--cinder-border-muted);
      /* @runtime-state */
      --_cinder-statistic-divider-space: calc(
        var(--cinder-statistic-group-gap, var(--cinder-space-4)) / 2
      );
    }
  }

  /* --- Collapsed to one track (<= 18rem): 2, 3 and 4 behave as 1 --- */

  @container cinder-statistic-group (max-width: 18rem) {
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='2']
      > .cinder-statistic-group__grid
      > .cinder-statistic:not(:last-child),
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='3']
      > .cinder-statistic-group__grid
      > .cinder-statistic:not(:last-child),
    .cinder-statistic-group[data-cinder-variant='default'][data-cinder-columns='4']
      > .cinder-statistic-group__grid
      > .cinder-statistic:not(:last-child) {
      border-inline-end: none;
      border-block-end: 1px solid var(--cinder-border-muted);
      /* @runtime-state */
      --_cinder-statistic-divider-space: calc(
        var(--cinder-statistic-group-gap, var(--cinder-space-4)) / 2
      );
    }
  }

  /* The final cell never has a neighbour in either axis, in any layout. */
  .cinder-statistic-group[data-cinder-variant='default']
    > .cinder-statistic-group__grid
    > .cinder-statistic:last-child {
    border-inline-end: none;
    border-block-end: none;
  }

  /* variant: cards — each stat gets a card of its own with one edge (COR-1821), a hairline border
   * by default. */
  .cinder-statistic-group[data-cinder-variant='cards']
    > .cinder-statistic-group__grid
    > .cinder-statistic {
    padding: var(--cinder-statistic-group-card-padding, var(--cinder-space-4, 1rem));
    background: var(--cinder-surface-raised);
    border: 1px solid var(--cinder-border);
    border-radius: var(--cinder-radius-md);
  }

  /* The shadow edge, opted into: one soft shadow and no visible border. The 1px border stays in
   * the box, transparent, so the card does not change size (as Card's shadow edge does). */
  .cinder-statistic-group[data-cinder-variant='cards'][data-cinder-edge='shadow']
    > .cinder-statistic-group__grid
    > .cinder-statistic {
    border-color: transparent;
    box-shadow: var(--cinder-shadow-sm);
  }

  /* variant: plain — the stats sit in the page flow with no surface, border, padding or divider,
   * and their values a step smaller than a statistic on its own, so a stat set under a Spotlight
   * reads after it rather than beside it as a second focal value. */
  .cinder-statistic-group[data-cinder-variant='plain'] > .cinder-statistic-group__grid {
    /* @runtime-state */
    --cinder-statistic-value-font-size: var(--cinder-text-2xl);
  }

  /*
 * variant: shared-borders — outer border on the root; the 1px grid gap is filled by
 * the root's background and acts as the divider between cells (the grid element is
 * transparent and inherits the gap token). Children paint over the gap with the
 * surface color, avoiding nth-child math for any column count.
 */
  .cinder-statistic-group[data-cinder-variant='shared-borders'] {
    --cinder-statistic-group-gap: 1px;
    border: 1px solid var(--cinder-border);
    border-radius: var(--cinder-radius-md);
    overflow: hidden;
    background: var(--cinder-border-muted);
  }

  .cinder-statistic-group[data-cinder-variant='shared-borders']
    > .cinder-statistic-group__grid
    > .cinder-statistic {
    padding: var(--cinder-statistic-group-shared-cell-padding, var(--cinder-space-4, 1rem));
    background: var(--cinder-surface-raised);
  }
}
