@layer cinder.tokens, cinder.foundation, cinder.components, cinder.utilities;
@layer cinder.components {
  /* ========================================
 * FORM SECTION
 * ======================================== */

  .cinder-form-section {
    container-type: inline-size;
    container-name: cinder-form-section;
    display: block;
    border: none;
    padding: 0;
    margin: 0;
  }

  /* ----------------------------------------
 * Heading / Legend
 * ---------------------------------------- */

  .cinder-form-section__heading,
  .cinder-form-section__legend {
    display: block;
    font-size: var(--cinder-text-sm);
    font-weight: var(--cinder-font-semibold);
    line-height: var(--cinder-leading-snug);
    color: var(--cinder-text-default);
    margin: 0 0 var(--cinder-space-1-5);
    padding: 0;
  }

  /* A heading with a note beside it shares one baseline; the row, not the heading, owns the gap
     below. */
  .cinder-form-section__title {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    column-gap: var(--cinder-space-2);
    margin: 0 0 var(--cinder-space-1-5);
  }

  .cinder-form-section__title > .cinder-form-section__heading {
    margin: 0;
  }

  .cinder-form-section__meta {
    color: var(--cinder-text-muted);
    font-size: var(--cinder-text-xs);
    font-weight: var(--cinder-font-normal);
    line-height: var(--cinder-leading-snug);
  }

  /* A count or format note is a distinct summary: align it with the title row's trailing edge
     while letting the row wrap naturally on a narrow section. */
  .cinder-form-section[data-meta-align='end'] .cinder-form-section__meta {
    margin-inline-start: auto;
    text-align: end;
  }

  .cinder-form-section__legend > .cinder-form-section__meta {
    margin-inline-start: var(--cinder-space-2);
  }

  /* ----------------------------------------
 * Description
 * ---------------------------------------- */

  .cinder-form-section__description {
    font-size: var(--cinder-text-xs);
    line-height: var(--cinder-leading-normal);
    color: var(--cinder-text-muted);
    margin: 0 0 var(--cinder-space-3);
  }

  /* A file name or a command inside the description reads as code, at the description's size. */
  .cinder-form-section__description code {
    font-family: var(--cinder-font-mono);
    font-size: inherit;
  }

  /* ----------------------------------------
 * Responsive grid — driven by container queries
 * ---------------------------------------- */

  .cinder-form-section__grid {
    display: grid;
    gap: var(--cinder-form-section-gap, 1rem);
    grid-template-columns: 1fr; /* default: single column */
  }

  .cinder-form-section[data-gap='loose'] .cinder-form-section__grid {
    gap: var(--cinder-space-6);
  }

  @container cinder-form-section (min-width: 40rem) {
    .cinder-form-section[data-columns='2'] .cinder-form-section__grid,
    .cinder-form-section[data-columns='3'] .cinder-form-section__grid,
    .cinder-form-section[data-columns='4'] .cinder-form-section__grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }

  @container cinder-form-section (min-width: 60rem) {
    .cinder-form-section[data-columns='3'] .cinder-form-section__grid,
    .cinder-form-section[data-columns='4'] .cinder-form-section__grid {
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }
  }

  @container cinder-form-section (min-width: 80rem) {
    .cinder-form-section[data-columns='4'] .cinder-form-section__grid {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  /* ----------------------------------------
 * Grouped: one bordered card of rows (settings pages)
 * ---------------------------------------- */

  .cinder-form-section[data-grouped='true'] .cinder-form-section__heading,
  .cinder-form-section[data-grouped='true'] .cinder-form-section__legend {
    font-size: var(--cinder-text-base);
    margin-block-end: var(--cinder-space-0-5);
  }

  /* A grouped section's heading steps with its level as a Section title does, so a settings page
     that mixes the two has one heading size per level: level 2 at the large size, level 3 at the
     base size, level 4 and deeper smaller and muted (2026-11-28 review). A legend keeps the base
     size. */
  .cinder-form-section[data-grouped='true'] .cinder-form-section__heading[data-level='2'] {
    font-size: var(--cinder-text-lg);
    line-height: var(--cinder-leading-tight);
  }

  .cinder-form-section[data-grouped='true']
    .cinder-form-section__heading:is([data-level='4'], [data-level='5'], [data-level='6']) {
    font-size: var(--cinder-text-sm);
    color: var(--cinder-text-muted);
  }

  .cinder-form-section[data-grouped='true'] .cinder-form-section__description {
    font-size: var(--cinder-text-sm);
    margin-block-end: var(--cinder-space-2);
  }

  .cinder-form-section[data-grouped='true'] .cinder-form-section__grid {
    gap: 0;
    margin-block-start: var(--cinder-space-2);
    /* The GroupedList's surface: its raised fill, no border and the surface radius, so a settings
       page's form groups and lists read as one kind of surface (2026-11-28 review). */
    border: 0;
    border-radius: var(--cinder-radius-surface);
    background: var(--cinder-surface-raised);
    overflow: hidden;
    position: relative;
    /* The section is the surface: a GroupedList inside it draws no edge of its own, so its rows
       share the section's edge rather than sitting 16px inside a second box (COR-2697 review). */
    --_cinder-grouped-list-group-edge: transparent;
  }

  /* The page hairline parts the white section 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-form-section[data-grouped='true'] .cinder-form-section__grid::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    border: var(--cinder-border-width) solid var(--_cinder-page-edge);
  }

  /* Dense settings pages opt into a quiet structural edge. The ordinary grouped form remains
     borderless, while this explicit tone supplies the border width that its colour requires. */
  .cinder-form-section[data-grouped='true'][data-cinder-border-tone='muted']
    .cinder-form-section__grid {
    border: var(--cinder-border-width) solid var(--cinder-border-muted);
  }

  /* That border is the section's one edge, in both themes, so the page-edge overlay stands down:
     two stacked lines would read as a heavier frame (COR-2697, design review round 3). */
  .cinder-form-section[data-grouped='true'][data-cinder-border-tone='muted']
    .cinder-form-section__grid::after {
    content: none;
  }

  @media (forced-colors: active) {
    .cinder-form-section[data-grouped='true'] .cinder-form-section__grid,
    .cinder-form-section[data-grouped='true'][data-cinder-border-tone='muted']
      .cinder-form-section__grid {
      border: 1px solid CanvasText;
    }
  }

  .cinder-form-section[data-grouped='true'] .cinder-form-section__grid > * {
    padding: var(--cinder-space-3) var(--cinder-space-4);
  }

  .cinder-form-section[data-grouped='true'] .cinder-form-section__grid > * + * {
    border-block-start: var(--cinder-border-width) solid var(--cinder-border-muted);
  }
}
