/* @svelte-lean/styles — control shell (ADR 0007). The one field every owned control shares:
 * select (<div data-slean="select">), autocomplete (<div data-slean="autocomplete">) and date
 * picker. The root is the field: it draws the border, the hover and focus states, and holds the
 * text part, the affixes, the clear button and the chevron on one line; the popup under it is the
 * shared dropdown surface with its options.
 * Parts: input (a borderless text input; the autocomplete's only <input> needs no part), trigger
 * (a borderless button or search input that shows the value), prefix and suffix (start and end
 * adornments), clear (<button tabindex="-1">, shown on hover or focus while the field has a
 * value), separator (between the two inputs of a range),
 * popup (a [popover] anchored under the root, created by the behavior on the first open), option
 * rows (role="option"; data-slean-new marks the row that creates a value), group-label and empty.
 * The chevron of select and autocomplete is the root's ::after (ADR 0011 §2.1), so a press on it
 * is a press on the field.
 * Sizes: data-size="small|large" on the root. State: :focus-within, a disabled native part, text
 * part or trigger, aria-invalid or :user-invalid on the native part, a text part or the trigger,
 * data-status="warning" on the root. The clear button needs a placeholder on a text input
 * (placeholder=" " when there is none) to know the field is empty.
 * `--slean-control-anchor` is an anchor name, not a token. select.css, autocomplete.css and
 * date-picker.css import this file. Its rules sit in the `slean.components.shell` sublayer, so a
 * rule of any component file (input.css, select.css, …) wins over the shell whatever the order
 * the files load in, and importing this file twice changes nothing.
 * Looks: data-variant="outlined" (default), "filled" or "standard"; data-color picks the focus color (primary by default); data-size="small|large". The shell already fills its container. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components.shell {
	/* The root is not positioned: a positioned root would be the containing block of a
	 * hidden-toggled popup, and anchor positioning excludes the containing block as an anchor. */
	:where(div[data-slean='autocomplete']),
	:where(div[data-slean='select']),
	:where([data-slean='date-picker']) {
		display: inline-flex;
		align-items: center;
		gap: var(--slean-space-1);
		inline-size: 100%;
		min-inline-size: 0;
		min-block-size: var(--slean-control-height-md);
		padding-block: 0;
		padding-inline: var(--slean-space-3) var(--slean-space-2);
		border: 1px solid var(--slean-control-border);
		border-radius: var(--slean-field-radius);
		background: var(--slean-control-bg);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
		vertical-align: middle;
		cursor: text;
		anchor-name: --slean-control-anchor;
		anchor-scope: --slean-control-anchor;
		transition:
			border-color var(--slean-duration-fast) var(--slean-ease),
			box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	:where(div[data-slean='select']) {
		cursor: pointer;
	}

	@media (hover: hover) {
		:where(div[data-slean='autocomplete']:hover),
		:where(div[data-slean='select']:hover),
		:where([data-slean='date-picker']:hover) {
			border-color: var(--slean-control-border-hover, var(--slean-color-main));
		}
	}

	:where(div[data-slean='autocomplete']:focus-within),
	:where(div[data-slean='select']:focus-within),
	:where([data-slean='date-picker']:focus-within) {
		border-color: var(--slean-color-main);
		box-shadow: 0 0 0 3px color-mix(in oklch, var(--slean-color-main) 22%, transparent);
	}

	/* The ring is the shell's: the text parts inside it keep the shared focus outline transparent.
	 * Forced colors paint it again with a system color, where the shell's halo (a shadow) is gone. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='input']:focus-visible
	),
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='trigger']:focus-visible
	),
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			input:focus-visible
	) {
		outline-color: transparent;
	}

	/* Status. */
	:where(
		div[data-slean='autocomplete']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		div[data-slean='select']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		[data-slean='date-picker']:has(
			:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
				[aria-invalid='true'],
				:user-invalid
			)
		)
	) {
		border-color: var(--slean-error-main);
	}

	:where(
		div[data-slean='autocomplete']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		div[data-slean='select']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		[data-slean='date-picker']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	) {
		box-shadow: var(--slean-control-ring-error);
	}

	:where(div[data-slean='autocomplete'][data-status='warning']),
	:where(div[data-slean='select'][data-status='warning']),
	:where([data-slean='date-picker'][data-status='warning']) {
		border-color: var(--slean-warning-main);
	}

	:where(div[data-slean='autocomplete'][data-status='warning']:focus-within),
	:where(div[data-slean='select'][data-status='warning']:focus-within),
	:where([data-slean='date-picker'][data-status='warning']:focus-within) {
		box-shadow: var(--slean-control-ring-warning);
	}

	/* Disabled: a disabled text part or trigger disables the whole field. */
	:where(
		div[data-slean='autocomplete']:has(:is(input, select, [data-slean-part='trigger']):disabled)
	),
	:where(div[data-slean='select']:has(:is(input, select, [data-slean-part='trigger']):disabled)),
	:where([data-slean='date-picker']:has(:is(input, select, [data-slean-part='trigger']):disabled)) {
		border-color: var(--slean-control-border-disabled);
		background: var(--slean-control-bg-disabled);
		color: var(--slean-fg-muted);
		box-shadow: none;
		cursor: not-allowed;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker']):has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
			[data-slean-part]
	) {
		cursor: not-allowed;
	}

	/* Sizes. */
	:where(div[data-slean='autocomplete'][data-size='small']),
	:where(div[data-slean='select'][data-size='small']),
	:where([data-slean='date-picker'][data-size='small']) {
		min-block-size: var(--slean-control-height-sm);
		padding-inline: var(--slean-space-2) var(--slean-space-1);
		border-radius: var(--slean-radius-sm);
	}

	:where(div[data-slean='autocomplete'][data-size='large']),
	:where(div[data-slean='select'][data-size='large']),
	:where([data-slean='date-picker'][data-size='large']) {
		min-block-size: var(--slean-control-height-lg);
		padding-inline: var(--slean-space-4) var(--slean-space-3);
		font-size: var(--slean-text-md);
	}

	/* Looks (ADR 0011 §3.3): data-variant="filled" is a tinted surface with a bottom line and
	 * rounded top corners, "standard" the bottom line alone (the suffix icons keep their place); the line takes the field's
	 * color (data-color, primary by default) on focus, the status colors on warning and error. */
	:where(div[data-slean='autocomplete'][data-variant='filled']),
	:where(div[data-slean='autocomplete'][data-variant='standard']),
	:where(div[data-slean='select'][data-variant='filled']),
	:where(div[data-slean='select'][data-variant='standard']),
	:where([data-slean='date-picker'][data-variant='filled']),
	:where([data-slean='date-picker'][data-variant='standard']) {
		border-color: transparent;
		border-block-end-color: var(--slean-control-border);
		border-radius: 0;
	}

	:where(div[data-slean='autocomplete'][data-variant='filled']),
	:where(div[data-slean='select'][data-variant='filled']),
	:where([data-slean='date-picker'][data-variant='filled']) {
		border-start-start-radius: var(--slean-field-radius);
		border-start-end-radius: var(--slean-field-radius);
		background-color: var(--slean-control-filled-bg);
	}

	:where(div[data-slean='autocomplete'][data-variant='standard']),
	:where(div[data-slean='select'][data-variant='standard']),
	:where([data-slean='date-picker'][data-variant='standard']) {
		background-color: transparent;
		padding-inline-start: 0;
	}

	@media (hover: hover) {
		:where(div[data-slean='autocomplete'][data-variant='filled']:hover),
		:where(div[data-slean='autocomplete'][data-variant='standard']:hover),
		:where(div[data-slean='select'][data-variant='filled']:hover),
		:where(div[data-slean='select'][data-variant='standard']:hover),
		:where([data-slean='date-picker'][data-variant='filled']:hover),
		:where([data-slean='date-picker'][data-variant='standard']:hover) {
			border-color: transparent;
			border-block-end-color: var(--slean-fg);
		}

		:where(div[data-slean='autocomplete'][data-variant='filled']:hover),
		:where(div[data-slean='select'][data-variant='filled']:hover),
		:where([data-slean='date-picker'][data-variant='filled']:hover) {
			background-color: var(--slean-control-filled-bg-hover);
		}
	}

	:where(div[data-slean='autocomplete'][data-variant='filled']:focus-within),
	:where(div[data-slean='autocomplete'][data-variant='standard']:focus-within),
	:where(div[data-slean='select'][data-variant='filled']:focus-within),
	:where(div[data-slean='select'][data-variant='standard']:focus-within),
	:where([data-slean='date-picker'][data-variant='filled']:focus-within),
	:where([data-slean='date-picker'][data-variant='standard']:focus-within) {
		border-color: transparent;
		border-block-end-color: var(--slean-color-main);
		box-shadow: inset 0 -1px 0 var(--slean-color-main);
	}

	:where(div[data-slean='autocomplete'][data-variant='filled'][data-status='warning']),
	:where(div[data-slean='autocomplete'][data-variant='standard'][data-status='warning']),
	:where(div[data-slean='select'][data-variant='filled'][data-status='warning']),
	:where(div[data-slean='select'][data-variant='standard'][data-status='warning']),
	:where([data-slean='date-picker'][data-variant='filled'][data-status='warning']),
	:where([data-slean='date-picker'][data-variant='standard'][data-status='warning']) {
		border-color: transparent;
		border-block-end-color: var(--slean-warning-main);
	}

	:where(div[data-slean='autocomplete'][data-variant='filled'][data-status='warning']:focus-within),
	:where(
		div[data-slean='autocomplete'][data-variant='standard'][data-status='warning']:focus-within
	),
	:where(div[data-slean='select'][data-variant='filled'][data-status='warning']:focus-within),
	:where(div[data-slean='select'][data-variant='standard'][data-status='warning']:focus-within),
	:where([data-slean='date-picker'][data-variant='filled'][data-status='warning']:focus-within),
	:where([data-slean='date-picker'][data-variant='standard'][data-status='warning']:focus-within) {
		box-shadow: inset 0 -1px 0 var(--slean-warning-main);
	}

	:where(
		div[data-slean='autocomplete'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		div[data-slean='autocomplete'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		div[data-slean='select'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		div[data-slean='select'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		[data-slean='date-picker'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	),
	:where(
		[data-slean='date-picker'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			)
	) {
		border-color: transparent;
		border-block-end-color: var(--slean-error-main);
	}

	:where(
		div[data-slean='autocomplete'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		div[data-slean='autocomplete'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		div[data-slean='select'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		div[data-slean='select'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		[data-slean='date-picker'][data-variant='filled']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	),
	:where(
		[data-slean='date-picker'][data-variant='standard']:has(
				:is([data-slean-part='native'], [data-slean-part='trigger'], input):is(
					[aria-invalid='true'],
					:user-invalid
				)
			):focus-within
	) {
		box-shadow: inset 0 -1px 0 var(--slean-error-main);
	}

	:where(
		div[data-slean='autocomplete'][data-variant='filled']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		div[data-slean='autocomplete'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		div[data-slean='select'][data-variant='filled']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		div[data-slean='select'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		[data-slean='date-picker'][data-variant='filled']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		[data-slean='date-picker'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	) {
		border-color: transparent;
		border-block-end-color: var(--slean-control-border-disabled);
		border-block-end-style: dotted;
	}

	:where(
		div[data-slean='autocomplete'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		div[data-slean='select'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	),
	:where(
		[data-slean='date-picker'][data-variant='standard']:has(
				:is(input, select, [data-slean-part='trigger']):disabled
			)
	) {
		background-color: transparent;
	}

	/* The text parts: borderless, transparent, filling the line. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='input']
	),
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='trigger']
	),
	:where(div[data-slean='autocomplete'] > input) {
		appearance: none;
		flex: 1 1 0;
		align-self: stretch;
		min-inline-size: 0;
		margin: 0;
		padding: 0;
		border: 0;
		border-radius: 0;
		background: transparent;
		color: inherit;
		font: inherit;
		text-align: start;
		text-overflow: ellipsis;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker']) input
	)::placeholder {
		color: var(--slean-fg-muted);
		opacity: 1;
	}

	/* A text box with an owned list has no browser list indicator (Chromium draws one for
	 * <input list> until the behavior takes the list over). */
	:where(div[data-slean='autocomplete'] input)::-webkit-calendar-picker-indicator {
		display: none;
	}

	/* Adornments: text or an icon before and after the value, in the icon color. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='prefix']
	),
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='suffix']
	) {
		flex: none;
		display: inline-flex;
		align-items: center;
		gap: var(--slean-space-1);
		color: var(--slean-control-icon);
		white-space: nowrap;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			:is([data-slean-part='prefix'], [data-slean-part='suffix'])
			> svg
	) {
		flex: none;
		inline-size: 1rem;
		block-size: 1rem;
	}

	/* The clear button: a 1.5rem square around a 1rem mask icon. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='clear']
	) {
		appearance: none;
		flex: none;
		display: inline-grid;
		place-items: center;
		inline-size: 1.5rem;
		block-size: 1.5rem;
		margin: 0;
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-control-icon);
		cursor: inherit;
		-webkit-tap-highlight-color: transparent;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='clear']
	)::before,
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='separator']
	)::before {
		content: '';
		inline-size: 1rem;
		block-size: 1rem;
		background: currentColor;
		mask: var(--slean-icon-chevron-down) center / contain no-repeat;
	}

	/* The chevron of select and autocomplete (ADR 0011 §2.1): the root's last box, so a press on
	 * it is a press on the field. A select draws the select icon, a chevron by default; a theme may
	 * draw the up-and-down pair instead (--slean-icon-select) and keep it still
	 * (--slean-icon-select-turn: 0deg). */
	:where(div[data-slean='autocomplete'])::after,
	:where(div[data-slean='select'])::after {
		content: '';
		flex: none;
		inline-size: 1rem;
		block-size: 1rem;
		margin-inline: var(--slean-space-1);
		background: var(--slean-control-icon);
		mask: var(--slean-icon-chevron-down) center / contain no-repeat;
		transition: rotate var(--slean-duration-normal) var(--slean-ease);
	}

	:where(div[data-slean='select'])::after {
		mask-image: var(--slean-icon-select);
	}

	:where(div[data-slean='autocomplete']:has([aria-expanded='true']))::after {
		rotate: 180deg;
	}

	:where(div[data-slean='select']:has([aria-expanded='true']))::after {
		rotate: var(--slean-icon-select-turn);
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='clear']
	) {
		display: none;
		cursor: pointer;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='clear']
	)::before {
		mask-image: var(--slean-icon-clear);
	}

	/* The clear button takes the place of the chevron or the calendar icon while the pointer is over
	 * a field that has a value, or while the field has focus. A select has a value when its native
	 * select holds an option that is neither the hidden placeholder nor empty. */
	:where(
		div[data-slean='autocomplete']:is(:hover, :focus-within):has(
				input:not(:placeholder-shown, :disabled)
			)
			[data-slean-part='clear']
	),
	:where(
		[data-slean='date-picker']:is(:hover, :focus-within):has(
				[data-slean-part='input']:not(:placeholder-shown, :disabled)
			)
			[data-slean-part='clear']
	),
	:where(
		div[data-slean='select']:is(:hover, :focus-within):has(
				[data-slean-part='native'] option:checked:not([hidden], [value=''])
			):not(:has(:is(select, [data-slean-part='trigger']):disabled))
			[data-slean-part='clear']
	) {
		display: inline-grid;
	}

	:where(
		div[data-slean='autocomplete']:hover:has(input:not(:placeholder-shown, :disabled)):has(
				[data-slean-part='clear']
			)
	)::after,
	:where(
		div[data-slean='autocomplete']:focus-within:has(input:not(:placeholder-shown, :disabled)):has(
				[data-slean-part='clear']
			)
	)::after,
	:where(
		div[data-slean='select']:hover:has(
				[data-slean-part='native'] option:checked:not([hidden], [value=''])
			):has([data-slean-part='clear']):not(:has(:is(select, [data-slean-part='trigger']):disabled))
	)::after,
	:where(
		div[data-slean='select']:focus-within:has(
				[data-slean-part='native'] option:checked:not([hidden], [value=''])
			):has([data-slean-part='clear']):not(:has(:is(select, [data-slean-part='trigger']):disabled))
	)::after {
		display: none;
	}

	@media (hover: hover) {
		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='clear']:hover
		) {
			color: var(--slean-fg);
		}
	}

	/* The dropdown surface. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='popup']
	) {
		position: absolute;
		position-anchor: --slean-control-anchor;
		position-area: block-end span-inline-end;
		position-try-fallbacks:
			flip-block,
			flip-inline,
			flip-block flip-inline;
		inset: auto;
		box-sizing: border-box;
		/* As wide as its options, never wider than the screen: an auto width would shrink into a
		 * narrow position area (a field by the right edge) and cut the options instead of
		 * overflowing it, so the inline flip — the popup taking the field's end edge — would never
		 * be tried. */
		inline-size: max-content;
		max-inline-size: calc(100vw - 2 * var(--slean-space-2));
		/* WebKit can stretch an auto-sized popup to its whole position area. Keep every field popup
		 * content-sized, bounded by that area, and scroll its contents when the bound is reached. */
		block-size: max-content;
		max-block-size: calc(100% - var(--slean-space-1));
		overflow: auto;
		margin: 0;
		margin-block: var(--slean-space-1);
		padding: var(--slean-space-1);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-popup-radius);
		background: var(--slean-surface);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		font-weight: normal;
		line-height: var(--slean-leading);
		box-shadow: var(--slean-popup-shadow);
		cursor: default;
	}

	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [data-slean-part='popup']) {
		min-inline-size: anchor-size(width);
	}

	/* A popover popup lives in the top layer and slides in from its field. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='popup'][popover]
	) {
		position: fixed;
		opacity: 0;
		translate: 0 -0.25rem;
		transition:
			opacity var(--slean-duration-normal) var(--slean-ease),
			translate var(--slean-duration-normal) var(--slean-ease),
			display var(--slean-duration-normal) allow-discrete,
			overlay var(--slean-duration-normal) allow-discrete;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='popup'][popover]:popover-open
	) {
		opacity: 1;
		/* none, not 0 0: an open popup is never the containing block of its fixed descendants. */
		translate: none;
	}

	@starting-style {
		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='popup'][popover]:popover-open
		) {
			opacity: 0;
			translate: 0 -0.25rem;
		}
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
			[data-slean-part='popup'][hidden]
	) {
		display: none;
	}

	/* Options. */
	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='listbox']) {
		display: flex;
		flex-direction: column;
		gap: 1px;
		max-block-size: 16rem;
		margin: 0;
		padding: 0;
		list-style: none;
		overflow: auto;
		overscroll-behavior: contain;
		scroll-padding-block: var(--slean-space-1);
	}

	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option']) {
		display: flex;
		align-items: center;
		gap: var(--slean-space-2);
		min-block-size: var(--slean-option-min-height);
		padding-block: var(--slean-option-padding-block);
		padding-inline: var(--slean-option-padding-inline);
		border-radius: var(--slean-radius-sm);
		color: var(--slean-fg);
		text-align: start;
		cursor: pointer;
		user-select: none;
		-webkit-tap-highlight-color: transparent;
	}

	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option'][hidden]) {
		display: none;
	}

	/* The check mark of the chosen option sits at the end of its row. */
	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option'])::after {
		content: '';
		flex: none;
		inline-size: var(--slean-option-check-size);
		block-size: var(--slean-option-check-size);
		margin-inline-start: auto;
		background: currentColor;
		mask: var(--slean-icon-check) center / contain no-repeat;
		visibility: hidden;
	}

	@media (hover: hover) {
		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'])
				[role='option']:hover:not([aria-disabled='true'])
		) {
			background: var(--slean-option-active);
			color: var(--slean-option-active-fg);
		}
	}

	/* The active option is the keyboard position while DOM focus stays on the field. */
	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option'][data-slean-active]
	) {
		background: var(--slean-option-active);
		color: var(--slean-option-active-fg);
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'])
			[role='option'][data-slean-selected]
	) {
		background: var(--slean-option-selected);
		color: var(--slean-option-selected-fg);
		font-weight: var(--slean-option-selected-weight);
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'])
			[role='option'][data-slean-selected]
	)::after {
		visibility: visible;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'])
			[role='option'][data-slean-selected][data-slean-active]
	) {
		box-shadow: var(--slean-option-selected-ring);
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select'])
			[role='option'][aria-disabled='true']
	) {
		color: var(--slean-fg-muted);
		opacity: 0.55;
		cursor: not-allowed;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select']) [data-slean-part='group-label']
	) {
		padding-block: var(--slean-space-2) var(--slean-space-1);
		padding-inline: var(--slean-space-2);
		color: var(--slean-fg-muted);
		font-size: var(--slean-text-xs);
		font-weight: var(--slean-font-weight-medium);
	}

	/* The row that offers typed text as a new value (data-slean-create): a plus before the text. */
	:where(div[data-slean='select'] [role='option'][data-slean-new])::before {
		content: '';
		flex: none;
		inline-size: var(--slean-option-check-size);
		block-size: var(--slean-option-check-size);
		background: currentColor;
		mask: var(--slean-icon-plus) center / contain no-repeat;
	}

	:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [data-slean-part='empty']) {
		display: grid;
		place-items: center;
		padding-block: var(--slean-space-4);
		padding-inline: var(--slean-space-3);
		color: var(--slean-fg-muted);
		text-align: center;
	}

	:where(
		:is(div[data-slean='autocomplete'], div[data-slean='select']) [data-slean-part='empty'][hidden]
	) {
		display: none;
	}

	/* No anchor positioning: a hidden-toggled popup sits under the root; a popover popup is placed
	 * under its field by the behavior (inline fixed coordinates, @svelte-lean/primitives'
	 * anchor fallback) and keeps the centered top-layer placement only until then. */
	@supports not (position-area: block-end) {
		:where(div[data-slean='autocomplete']),
		:where(div[data-slean='select']),
		:where([data-slean='date-picker']) {
			position: relative;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='popup']
		) {
			inset-block-start: 100%;
			inset-inline-start: 0;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='popup'][popover]
		) {
			inset: 0;
			inline-size: max-content;
			margin: auto;
		}
	}

	@media (pointer: coarse) {
		:where(div[data-slean='autocomplete']),
		:where(div[data-slean='select']),
		:where([data-slean='date-picker']) {
			font-size: var(--slean-text-md);
		}

		:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option']) {
			min-block-size: var(--slean-control-height-md);
		}
	}

	@media (forced-colors: active) {
		:where(div[data-slean='autocomplete']),
		:where(div[data-slean='select']),
		:where([data-slean='date-picker']) {
			border-color: FieldText;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='clear']
		)::before,
		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'], [data-slean='date-picker'])
				[data-slean-part='separator']
		)::before,
		:where(div[data-slean='autocomplete'])::after,
		:where(div[data-slean='select'])::after,
		:where(:is(div[data-slean='autocomplete'], div[data-slean='select']) [role='option'])::after {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'])
				[role='option'][data-slean-active]
		) {
			forced-color-adjust: none;
			background: Highlight;
			color: HighlightText;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'])
				[role='option'][data-slean-active]
		)::after {
			background: HighlightText;
		}

		:where(
			:is(div[data-slean='autocomplete'], div[data-slean='select'])
				[role='option'][aria-disabled='true']
		) {
			color: GrayText;
			opacity: 1;
		}
	}
}
