/* # Host */

nldd-form-section {
	display: block;
	box-sizing: border-box;
}

nldd-form-section[hidden] {
	display: none;
}

/* A margin and not padding, so it collapses with the gap the form puts under
   the previous child: 16 and 24 next to each other are 24, not 40. Padding
   collapses with nothing and would have made it 32. */

nldd-form > form > nldd-form-section:not(:first-child) {
	margin-block-start: var(--semantics-forms-sections-gap);
}

/* The :has() guard also settles the cascade: a physical margin-bottom and a
   logical margin-block-end for the same side are decided on declaration order,
   not specificity, so without it the outcome would depend on the load order of
   the files. */

nldd-form > form > nldd-form-section:has(~ *:not([hidden]):not([type='hidden'])) {
	margin-block-end: var(--semantics-forms-sections-gap);
}


/* # Block (fieldset) — reset native styles */

nldd-form-section > fieldset.form-section {
	border: none;
	margin: 0;
	padding: 0;
	min-width: 0;
	display: block;
	width: 100%;
}


/* # Header (rendered as <legend>, must be direct child of <fieldset>).
   Title and supporting text are spans inside the legend so screen readers read
   them together as the group label. */

nldd-form-section .form-section__header {
	padding: 0;
	margin: 0;
	width: 100%;
	text-align: left;
}

nldd-form-section .form-section__header[hidden] {
	display: none;
}


/* # Title */

nldd-form-section .form-section__title {
	display: block;
	--context-content-color: var(--semantics-content-color);
	color: var(--semantics-content-color);
	font: var(--semantics-forms-section-title-font);
	text-wrap: balance;
}


/* # Supporting text */

nldd-form-section .form-section__supporting-text {
	display: block;
	--context-content-color: var(--semantics-content-secondary-color);
	color: var(--semantics-content-secondary-color);
	font: var(--semantics-forms-section-supporting-text-font);
	text-wrap: pretty;
}


/* # Main — gets margin-top to separate from header. When no header is
   rendered (no text + no supporting-text), main is the first child of
   the fieldset and the margin collapses via :first-child.

   Closer to the heading than a section stands to what came before it: the
   heading belongs to what is under it, and the same distance on both sides
   would leave it floating between the two. */

nldd-form-section > fieldset.form-section > .form-section__main {
	display: block;
	margin-top: var(--primitives-space-8);
}

nldd-form-section > fieldset.form-section > .form-section__main:first-child {
	margin-top: 0;
}

/* # Vertical rhythm within a section

   Same rule and same reason as in form.css: the gap hangs on the bottom, and
   the :has() guard keeps a trailing gap off the last visible child. */

nldd-form-section .form-section__main > *:has(~ *:not([hidden]):not([type='hidden'])) {
	margin-bottom: var(--semantics-forms-fields-gap);
}
