/*
 * Fieldset: a borderless `group` that stacks its Fields with the container gap, headed by a label row like
 * `Field.Header`.
 */

@layer dx-components {
  .dx-fieldset {
    min-inline-size: 0;
  }
  .dx-fieldset:not(.dx-container) {
    display: flex;
    flex-direction: column;
    gap: var(--dx-gap-size);
  }
  /* A grid set (`gutter='inherit'`) spaces its rows as its parent grid does, since a subgrid shares columns only. */
  .dx-fieldset.dx-container {
    row-gap: inherit;
  }

  /*
   * An inset grid set (a nested group) encloses its fields in a border and indents them one step: it spans only the
   * parent's content track, whose edge lines it inherits, so its fields keep the `content` placement (and any interior
   * column lines) while its padding offsets them. It keeps its host's surface; nesting never steps the level.
   */
  .dx-fieldset.dx-container[data-inset] {
    grid-column: content;
    /* Half the size's gap, tighter than the form's own rows, so a group's fields read as one; its Content inherits it. */
    row-gap: calc(var(--dx-gap-size) / 2);
    padding: var(--dx-gap-size);
    border: 1px solid var(--color-separator);
    border-radius: var(--radius-md, 0.375rem);
  }
  /* The set spans no `full` lines, so an inheriting grid inside it (a Collapsible Content) spans the set's tracks. */
  .dx-fieldset.dx-container[data-inset] > .dx-container[data-gutter='inherit'] {
    grid-column: 1 / -1;
  }
  .dx-fieldset > .dx-control {
    margin-block: var(--dx-control-inset);
  }

  .dx-fieldset-legend {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    gap: var(--dx-gap-size);
    width: 100%;
    /* Like `Field.Header`: the set's size, control-tall, in the label step, so a trailing action shares the fields' end column. */
    min-height: var(--dx-control-size);
    padding: 0;
    font-size: var(--dx-label-font-size);
    line-height: var(--dx-label-line-height);
    font-weight: 500;
    /* A group's label, like a field's (see `control.css`). */
    color: var(--color-fg-subtle);
  }
  /* The legend text is a bare text node, so the first trailing part pushes the rest to the row's end. */
  .dx-fieldset-legend
    > :is(.dx-block, .dx-button, .dx-toggle-group):not(:is(.dx-block, .dx-button, .dx-toggle-group) ~ *) {
    margin-inline-start: auto;
  }
  .dx-fieldset-legend > .dx-block {
    width: var(--dx-control-size);
    height: var(--dx-control-size);
    margin: 0 var(--dx-control-inset);
  }
  .dx-fieldset-legend > .dx-button[data-square] {
    margin-block: 0;
  }
  /* A legend's controls take the ink of other buttons (and a select's chevron), not the legend's label tone. */
  .dx-fieldset-legend > .dx-button:not([data-variant='primary']) {
    color: var(--color-fg);
  }
  /* A legend given its own size is a heading row: one block tall, in that size's text. */
  .dx-fieldset-legend[data-size] {
    min-height: var(--dx-block-size);
    font-size: var(--dx-font-size);
    line-height: var(--dx-line-height);
  }
  /* A top-level section's title (the current Form's root legend): content, at text-lg. */
  .dx-fieldset-legend[data-variant='section'] {
    font-size: var(--text-lg);
    line-height: var(--text-lg--line-height);
    color: var(--color-fg);
  }
  /* The section's subtitle reads at the body size in the description colour, as a settings row's description does. */
  .dx-fieldset-legend[data-variant='section'] + .dx-field-helper {
    font-size: var(--text-base);
    line-height: var(--text-base--line-height);
    color: var(--color-fg-muted);
  }
  .dx-fieldset[data-disabled] {
    opacity: 0.5;
  }
}
