/* @svelte-lean/styles — autocomplete. Two markups (ADR 0011 §1.2):
 * - Tier 0: <input list="cities" data-slean="autocomplete"> and <datalist id="cities"> with
 *   <option> elements. The suggestion list is rendered by the browser outside the page and cannot
 *   be styled; this file draws the text box from the tokens and hides the browser's list
 *   indicator. State: :focus-visible, :user-invalid, aria-invalid="true", :disabled;
 *   data-status="warning" (an error is aria-invalid="true" or :user-invalid).
 * - Tier 2: <div data-slean="autocomplete"> around the input (and its datalist), with optional
 *   prefix, suffix and clear parts; the behavior creates the popup on the first open. The field,
 *   the chevron (the root's ::after), the popup and the option rows come from control.css
 *   (imported by this file); this file hands the field back to the plain input when scripting is
 *   off, where the browser's datalist works.
 * Looks: data-variant="outlined" (default), "filled" or "standard"; data-color picks the focus color (primary by default); data-size="small|large"; data-full-width. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;
@import './control.css';

@layer slean.components {
	:where(input[data-slean='autocomplete']) {
		box-sizing: border-box;
		inline-size: 100%;
		max-inline-size: 20rem;
		min-block-size: var(--slean-control-height-md);
		padding-block: 0;
		padding-inline: var(--slean-space-3);
		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);
		transition:
			border-color var(--slean-duration-fast) var(--slean-ease),
			box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	:where(input[data-slean='autocomplete'])::placeholder {
		color: var(--slean-fg-muted);
		opacity: 1;
	}

	/* A text box with suggestions has no dropdown arrow (the suggestions open as the user types);
	 * Chromium draws one for <input list>. */
	:where(input[data-slean='autocomplete'])::-webkit-calendar-picker-indicator {
		display: none;
	}

	/* The field states shared with input.css and control.css. */
	@media (hover: hover) {
		:where(input[data-slean='autocomplete']:hover:not(:disabled)) {
			border-color: var(--slean-control-border-hover, var(--slean-color-main));
		}
	}

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

	:where(input[data-slean='autocomplete'][data-status='warning']) {
		border-color: var(--slean-warning-main);
	}

	:where(input[data-slean='autocomplete'][data-status='warning']:focus-visible) {
		box-shadow: var(--slean-control-ring-warning);
	}

	:where(input[data-slean='autocomplete']:user-invalid),
	:where(input[data-slean='autocomplete'][aria-invalid='true']) {
		border-color: var(--slean-error-main);
	}

	:where(input[data-slean='autocomplete']:user-invalid:focus-visible),
	:where(input[data-slean='autocomplete'][aria-invalid='true']:focus-visible) {
		box-shadow: var(--slean-control-ring-error);
	}

	:where(input[data-slean='autocomplete']:disabled) {
		border-color: var(--slean-control-border-disabled);
		background-color: var(--slean-control-bg-disabled);
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	/* Sizes: data-size="small" and "large" ("medium" is the default). */
	:where(input[data-slean='autocomplete'][data-size='small']) {
		min-block-size: var(--slean-control-height-sm);
		border-radius: var(--slean-radius-sm);
	}

	:where(input[data-slean='autocomplete'][data-size='large']) {
		min-block-size: var(--slean-control-height-lg);
		font-size: var(--slean-text-md);
	}

	/* data-full-width: the field fills its container. */
	:where(input[data-slean='autocomplete'][data-full-width]) {
		inline-size: 100%;
		max-inline-size: none;
	}

	/* 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 (no horizontal padding); the line takes the field's
	 * color (data-color, primary by default) on focus, the status colors on warning and error. */
	:where(input[data-slean='autocomplete'][data-variant='filled']),
	:where(input[data-slean='autocomplete'][data-variant='standard']) {
		border-color: transparent;
		border-block-end-color: var(--slean-control-border);
		border-radius: 0;
	}

	:where(input[data-slean='autocomplete'][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(input[data-slean='autocomplete'][data-variant='standard']) {
		background-color: transparent;
		padding-inline: 0;
	}

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

		:where(input[data-slean='autocomplete'][data-variant='filled']:not(:disabled):hover) {
			background-color: var(--slean-control-filled-bg-hover);
		}
	}

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

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

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

	:where(input[data-slean='autocomplete'][data-variant='filled']:user-invalid),
	:where(input[data-slean='autocomplete'][data-variant='filled'][aria-invalid='true']),
	:where(input[data-slean='autocomplete'][data-variant='standard']:user-invalid),
	:where(input[data-slean='autocomplete'][data-variant='standard'][aria-invalid='true']) {
		border-color: transparent;
		border-block-end-color: var(--slean-error-main);
	}

	:where(input[data-slean='autocomplete'][data-variant='filled']:user-invalid:focus-visible),
	:where(
		input[data-slean='autocomplete'][data-variant='filled'][aria-invalid='true']:focus-visible
	),
	:where(input[data-slean='autocomplete'][data-variant='standard']:user-invalid:focus-visible),
	:where(
		input[data-slean='autocomplete'][data-variant='standard'][aria-invalid='true']:focus-visible
	) {
		box-shadow: inset 0 -1px 0 var(--slean-error-main);
	}

	:where(input[data-slean='autocomplete'][data-variant='filled']:disabled),
	:where(input[data-slean='autocomplete'][data-variant='standard']:disabled) {
		border-color: transparent;
		border-block-end-color: var(--slean-control-border-disabled);
		border-block-end-style: dotted;
	}

	:where(input[data-slean='autocomplete'][data-variant='standard']:disabled) {
		background-color: transparent;
	}

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

	@media (forced-colors: active) {
		:where(input[data-slean='autocomplete']) {
			border-color: FieldText;
		}

		:where(input[data-slean='autocomplete']:disabled) {
			border-color: GrayText;
			color: GrayText;
		}
	}

	/* The owned field without scripting: the input keeps its datalist (the browser's list); the
	 * clear button, the affixes' buttons and the chevron would do nothing. */
	@media (scripting: none) {
		:where(div[data-slean='autocomplete'] > [data-slean-part='clear']),
		:where(div[data-slean='autocomplete'] > [data-slean-part='popup']) {
			display: none;
		}

		:where(div[data-slean='autocomplete'])::after {
			content: none;
		}
	}
}
