/* @svelte-lean/styles — field (Tier 0: a layout for a label, a control, a description and an error).
 * Markup: <div data-slean="field"> <label for="…" data-slean-part="label"> <control
 * aria-describedby="…"> <p id="…" data-slean-part="description"> <p id="…" data-slean-part="error">.
 * The control keeps its own stylesheet (input, select, …), which colors :user-invalid and
 * aria-invalid="true". The error part is shown whenever it is rendered: the application renders it
 * when there is an error. Full-width controls stretch to the field; group and compact controls
 * (a fieldset, an input of a number, date, time or color type, a one-time code, a number shell, a
 * calendar, a toggle group) keep their own width. The description and the error share one look
 * (the error in the error color); the label takes the error color while the control is invalid.
 * Option: data-slean-orientation="horizontal" puts the label beside the control, in a 10rem column.
 * With data-slean-validate (@svelte-lean/primitives/field/validation/register) the error part is
 * the browser's validation message, written by the module. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='field']) {
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-2);
		min-inline-size: 0;
	}

	/* A group control (a fieldset: radio group, rating, checkbox group) and a compact control (an
	 * input of a number, date, time or color type, a one-time code, a number shell, a calendar, a
	 * toggle group) keep their own width; full-width controls (a text input, a textarea, a field
	 * shell, select, autocomplete, date picker) stretch to the field. */
	:where([data-slean='field'] > fieldset),
	:where([data-slean='field'] > input[data-slean='input'][type='number']),
	:where([data-slean='field'] > input[data-slean='input'][type='date']),
	:where([data-slean='field'] > input[data-slean='input'][type='time']),
	:where([data-slean='field'] > input[data-slean='input'][type='datetime-local']),
	:where([data-slean='field'] > input[data-slean='input'][type='month']),
	:where([data-slean='field'] > input[data-slean='input'][type='week']),
	:where([data-slean='field'] > input[data-slean='input'][type='color']),
	:where([data-slean='field'] > input[data-slean='input'][autocomplete~='one-time-code']),
	:where([data-slean='field'] > div[data-slean='input']:has(> input[type='number'])),
	:where([data-slean='field'] > [data-slean='calendar']),
	:where([data-slean='field'] > [data-slean='toggle-group']) {
		align-self: flex-start;
		justify-self: start;
	}

	:where([data-slean='field'] [data-slean-part='label']) {
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		font-weight: var(--slean-font-weight-medium);
		line-height: 1.25;
	}

	/* The helper texts under the control (MUI's FormHelperText): the description and the error
	 * share one look, a little closer to the control than the label is, and differ in color only. */
	:where([data-slean='field'] [data-slean-part='description']),
	:where([data-slean='field'] [data-slean-part='error']) {
		margin: 0;
		color: var(--slean-fg-muted);
		font-size: var(--slean-text-sm);
		line-height: 1.4;
	}

	:where(
		[data-slean='field']:not([data-slean-orientation='horizontal'])
			> :not([data-slean-part='label'])
			+ [data-slean-part='description']
	),
	:where(
		[data-slean='field']:not([data-slean-orientation='horizontal'])
			> :not([data-slean-part='label'])
			+ [data-slean-part='error']
	) {
		margin-block-start: calc(-1 * var(--slean-space-1));
	}

	:where([data-slean='field'] [data-slean-part='error']) {
		color: var(--slean-error-main);
	}

	/* An error part the validation module emptied, or one rendered empty, takes no space. */
	:where([data-slean='field'] [data-slean-part='error']:empty) {
		display: none;
	}

	/* The label takes the error color while the control is invalid (MUI's FormLabel error). */
	:where(
		[data-slean='field']:has(:is(input, select, textarea):is([aria-invalid='true'], :user-invalid))
			[data-slean-part='label']
	) {
		color: var(--slean-error-main);
	}

	/* A disabled control (a direct child, or the field of an input shell) mutes the label and the
	 * description; a disabled option inside a select does not count. */
	:where(
		[data-slean='field']:has(> :disabled, > [data-slean] > :is(input, select, textarea):disabled)
			[data-slean-part='label']
	),
	:where(
		[data-slean='field']:has(> :disabled, > [data-slean] > :is(input, select, textarea):disabled)
			[data-slean-part='description']
	) {
		color: var(--slean-fg-muted);
	}

	/* Horizontal: the label in the first column, beside the control's first line; everything
	 * else in the second column, in document order. */
	:where([data-slean='field'][data-slean-orientation='horizontal']) {
		display: grid;
		grid-template-columns: minmax(0, 10rem) minmax(0, 1fr);
		gap: var(--slean-space-2) var(--slean-space-4);
		align-items: baseline;
	}

	:where([data-slean='field'][data-slean-orientation='horizontal'] > *) {
		grid-column: 2;
	}

	:where([data-slean='field'][data-slean-orientation='horizontal'] > [data-slean-part='label']) {
		grid-row: 1;
		grid-column: 1;
	}

	@media (forced-colors: active) {
		:where(
			[data-slean='field']:has(> :disabled, > [data-slean] > :is(input, select, textarea):disabled)
				[data-slean-part='label']
		),
		:where(
			[data-slean='field']:has(> :disabled, > [data-slean] > :is(input, select, textarea):disabled)
				[data-slean-part='description']
		) {
			color: GrayText;
		}
	}
}
