/* @svelte-lean/styles — select. Two markups (ADR 0007, ADR 0011 §1.2):
 * - The owned select (Tier 2): <div data-slean="select"> around a native <select
 *   data-slean-part="native"> and a role="combobox" trigger (a button that shows the label, a
 *   <div> holding the value and placeholder parts of a multiple select, or a search <input>),
 *   optional prefix, suffix and clear parts, and the popup the behavior creates on the first open.
 *   The field, the chevron (the root's ::after) and the dropdown come from control.css (imported
 *   by this file); this file adds the placeholder look, the value and the tags, and hands the field
 *   to the native <select> when scripting is off.
 * - The styled native select (Tier 0): <select data-slean="select">, also the look of the native
 *   part without scripting. Sizes: data-size="small|large". State is native: :disabled, :user-invalid,
 *   :open, option:checked, option:disabled; aria-invalid="true" marks an error the application
 *   rendered (on the native part or the trigger of the owned select), data-status="warning" a
 *   warning. Where appearance: base-select is supported, the dropdown and its picker are drawn
 *   from the tokens (::picker(select), ::checkmark, and ::picker-icon masked with the chevron
 *   icon); elsewhere the closed box is styled, the chevron is two gradient triangles (a <select>
 *   has no pseudo-element to mask) and the picker is the platform's. A select with
 *   multiple or size is a list box and keeps the platform rendering inside the box.
 * Looks of the native select and of the owned shell (control.css): data-variant="outlined" (default), "filled" or "standard"; data-color picks the focus color; data-full-width. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;
@import './control.css';

@layer slean.components {
	:where(select[data-slean='select']),
	:where(select[data-slean-part='native']) {
		appearance: none;
		max-inline-size: 100%;
		border: 1px solid var(--slean-control-border);
		border-radius: var(--slean-field-radius);
		background-color: var(--slean-control-bg);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
		cursor: pointer;
		transition:
			border-color var(--slean-duration-fast) var(--slean-ease),
			box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	/* The dropdown (no multiple, no size). */
	:where(select[data-slean='select']:not([multiple], [size])),
	:where(select[data-slean-part='native']:not([multiple], [size])) {
		min-block-size: var(--slean-control-height-md);
		padding-block: 0;
		padding-inline: var(--slean-space-3) calc(var(--slean-space-3) + 1rem);
	}

	:where(select[data-slean='select'][data-size='small']:not([multiple], [size])),
	:where(select[data-slean-part='native'][data-size='small']:not([multiple], [size])) {
		min-block-size: var(--slean-control-height-sm);
		padding-inline-start: var(--slean-space-2);
		border-radius: var(--slean-radius-sm);
	}

	:where(select[data-slean='select'][data-size='large']:not([multiple], [size])),
	:where(select[data-slean-part='native'][data-size='large']:not([multiple], [size])) {
		min-block-size: var(--slean-control-height-lg);
		padding-inline-start: var(--slean-space-4);
		font-size: var(--slean-text-md);
	}

	/* The list box (multiple or size): the platform draws the options. */
	:where(select[data-slean='select'][multiple]),
	:where(select[data-slean='select'][size]),
	:where(select[data-slean-part='native'][multiple]),
	:where(select[data-slean-part='native'][size]) {
		padding: var(--slean-space-1);
		cursor: default;
	}

	/* Without customizable select the element has no pseudo-element to mask, so the chevron is the
	 * one icon drawn with background gradients: two triangles in the icon color. */
	@supports not (appearance: base-select) {
		:where(select[data-slean='select']:not([multiple], [size])),
		:where(select[data-slean-part='native']:not([multiple], [size])) {
			background-image:
				linear-gradient(45deg, transparent 50%, var(--slean-control-icon) 50%),
				linear-gradient(135deg, var(--slean-control-icon) 50%, transparent 50%);
			background-position:
				calc(100% - var(--slean-space-3) - 0.3125rem) 50%,
				calc(100% - var(--slean-space-3)) 50%;
			background-size: 0.3125rem 0.3125rem;
			background-repeat: no-repeat;
		}

		:where(select[data-slean='select']:not([multiple], [size]):dir(rtl)),
		:where(select[data-slean-part='native']:not([multiple], [size]):dir(rtl)) {
			background-position:
				var(--slean-space-3) 50%,
				calc(var(--slean-space-3) + 0.3125rem) 50%;
		}

		/* The author's customizable-select <button> (with <selectedcontent>) is parsed but not drawn
		 * here; display: none also keeps an empty, unnamed button out of the accessibility tree. */
		:where(select:is([data-slean='select'], [data-slean-part='native']) > button) {
			display: none;
		}
	}

	/* The customizable select. Both the element and its picker opt in. */
	@supports (appearance: base-select) {
		:where(select[data-slean='select']:not([multiple], [size])),
		:where(select[data-slean-part='native']:not([multiple], [size])),
		:where(select[data-slean='select']:not([multiple], [size]))::picker(select),
		:where(select[data-slean-part='native']:not([multiple], [size]))::picker(select) {
			appearance: base-select;
		}

		:where(select[data-slean='select']:not([multiple], [size])),
		:where(select[data-slean-part='native']:not([multiple], [size])) {
			display: inline-flex;
			align-items: center;
			gap: var(--slean-space-2);
			padding-inline-end: var(--slean-space-3);
		}

		:where(select[data-slean='select']:not([multiple], [size]))::picker-icon,
		:where(select[data-slean-part='native']:not([multiple], [size]))::picker-icon {
			content: '';
			inline-size: 1rem;
			block-size: 1rem;
			margin-inline-start: auto;
			background: var(--slean-control-icon);
			mask: var(--slean-icon-select) center / contain no-repeat;
			transition: rotate var(--slean-duration-normal) var(--slean-ease);
		}

		:where(select[data-slean='select']:not([multiple], [size]):open)::picker-icon,
		:where(select[data-slean-part='native']:not([multiple], [size]):open)::picker-icon {
			rotate: var(--slean-icon-select-turn);
		}

		:where(select[data-slean='select'])::picker(select),
		:where(select[data-slean-part='native'])::picker(select) {
			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);
			box-shadow: var(--slean-popup-shadow);
			opacity: 0;
			transition:
				opacity var(--slean-duration-fast) var(--slean-ease),
				display var(--slean-duration-fast) allow-discrete,
				overlay var(--slean-duration-fast) allow-discrete;
		}

		:where(select[data-slean='select']:open)::picker(select),
		:where(select[data-slean-part='native']:open)::picker(select) {
			opacity: 1;
		}

		@starting-style {
			:where(select[data-slean='select']:open)::picker(select),
			:where(select[data-slean-part='native']:open)::picker(select) {
				opacity: 0;
			}
		}

		:where(
			select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size]) option
		) {
			box-sizing: border-box;
			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);
			font-size: var(--slean-text-sm);
			cursor: default;
		}

		:where(
			select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size]) option
		)::checkmark {
			color: var(--slean-color-main);
		}

		:where(
			select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size])
				option:checked
		) {
			background: var(--slean-option-selected);
			color: var(--slean-option-selected-fg);
			font-weight: var(--slean-option-selected-weight);
		}

		@media (hover: hover) {
			:where(
				select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size])
					option:hover:not(:disabled)
			) {
				background: var(--slean-option-active);
				color: var(--slean-option-active-fg);
			}
		}

		/* Keyboard focus in the picker is the row's fill, as a hover (the owned select's active
		 * option); the outline stays, transparent, for forced colors. */
		:where(
			select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size])
				option:focus-visible
		) {
			background: var(--slean-option-active);
			color: var(--slean-option-active-fg);
			outline: var(--slean-focus-ring);
			outline-color: transparent;
			outline-offset: calc(-1 * var(--slean-focus-ring-width));
		}

		:where(
			select:is([data-slean='select'], [data-slean-part='native']):not([multiple], [size])
				option:disabled
		) {
			opacity: 0.5;
			cursor: not-allowed;
		}
	}

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

	:where(select[data-slean='select']:focus-visible),
	:where(select[data-slean-part='native']: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(select[data-slean='select'][data-status='warning']),
	:where(select[data-slean-part='native'][data-status='warning']) {
		border-color: var(--slean-warning-main);
	}

	:where(select[data-slean='select'][data-status='warning']:focus-visible),
	:where(select[data-slean-part='native'][data-status='warning']:focus-visible) {
		box-shadow: var(--slean-control-ring-warning);
	}

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

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

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

	/* data-full-width: the field fills its container. */
	:where(select[data-slean='select'][data-full-width]),
	:where(select[data-slean-part='native'][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 (the chevron keeps its place); the line takes the field's
	 * color (data-color, primary by default) on focus, the status colors on warning and error. */
	:where(select[data-slean='select'][data-variant='filled']),
	:where(select[data-slean='select'][data-variant='standard']),
	:where(select[data-slean-part='native'][data-variant='filled']),
	:where(select[data-slean-part='native'][data-variant='standard']) {
		border-color: transparent;
		border-block-end-color: var(--slean-control-border);
		border-radius: 0;
	}

	:where(select[data-slean='select'][data-variant='filled']),
	:where(select[data-slean-part='native'][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(select[data-slean='select'][data-variant='standard']),
	:where(select[data-slean-part='native'][data-variant='standard']) {
		background-color: transparent;
		padding-inline-start: 0;
	}

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

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

	:where(select[data-slean='select'][data-variant='filled']:focus-visible),
	:where(select[data-slean='select'][data-variant='standard']:focus-visible),
	:where(select[data-slean-part='native'][data-variant='filled']:focus-visible),
	:where(select[data-slean-part='native'][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(select[data-slean='select'][data-variant='filled'][data-status='warning']),
	:where(select[data-slean='select'][data-variant='standard'][data-status='warning']),
	:where(select[data-slean-part='native'][data-variant='filled'][data-status='warning']),
	:where(select[data-slean-part='native'][data-variant='standard'][data-status='warning']) {
		border-color: transparent;
		border-block-end-color: var(--slean-warning-main);
	}

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

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

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

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

	:where(select[data-slean='select'][data-variant='standard']:disabled),
	:where(select[data-slean-part='native'][data-variant='standard']:disabled) {
		background-color: transparent;
	}

	/* Touch: the control heights grow (tokens.css); 16px text keeps iOS Safari from zooming the
	 * page on focus. */
	@media (pointer: coarse) {
		:where(select[data-slean='select']),
		:where(select[data-slean-part='native']) {
			font-size: var(--slean-text-md);
		}
	}

	@media (forced-colors: active) {
		:where(select[data-slean='select']),
		:where(select[data-slean-part='native']) {
			border-color: ButtonText;
		}

		@supports not (appearance: base-select) {
			:where(select[data-slean='select']:not([multiple], [size])),
			:where(select[data-slean-part='native']:not([multiple], [size])) {
				background-image:
					linear-gradient(45deg, transparent 50%, ButtonText 50%),
					linear-gradient(135deg, ButtonText 50%, transparent 50%);
			}
		}

		:where(select[data-slean='select']:disabled),
		:where(select[data-slean-part='native']:disabled) {
			border-color: GrayText;
			color: GrayText;
		}

		:where(select:is([data-slean='select'], [data-slean-part='native']) option:disabled) {
			opacity: 1;
			color: GrayText;
		}

		@supports (appearance: base-select) {
			:where(select[data-slean='select'])::picker-icon,
			:where(select[data-slean-part='native'])::picker-icon {
				forced-color-adjust: none;
				background: ButtonText;
			}
		}
	}

	/* The owned select. With scripting the native <select> gives way to the trigger; without it
	 * the field is the native select and the owned parts are not shown. */
	@media (scripting: enabled) {
		:where(div[data-slean='select'] > [data-slean-part='native']) {
			display: none;
		}
	}

	:where(div[data-slean='select'] [data-slean-part='trigger']) {
		display: flex;
		align-items: center;
		gap: var(--slean-space-1);
		overflow: hidden;
		cursor: inherit;
	}

	:where(div[data-slean='select'] [data-slean-part='value']),
	:where(div[data-slean='select'] [data-slean-part='placeholder']) {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	:where(div[data-slean='select'] [data-slean-part='placeholder']) {
		color: var(--slean-fg-muted);
	}

	/* A trigger that shows the hidden placeholder option's label draws it as a placeholder. */
	:where(
		div[data-slean='select']:has([data-slean-part='native'] option:checked[hidden])
			> [data-slean-part='trigger']
	) {
		color: var(--slean-fg-muted);
	}

	:where(
		div[data-slean='select'] [data-slean-part='value']:not(:empty) ~ [data-slean-part='placeholder']
	),
	:where(div[data-slean='select'] [data-slean-part='value']:empty) {
		display: none;
	}

	/* A multiple select: the chosen options are tags that wrap inside the field. With search, the
	 * tags and the search input share the field's lines. */
	:where(div[data-slean='select']:has(> select[multiple])) {
		block-size: auto;
		padding-block: 3px;
	}

	:where(div[data-slean='select']:has(> select[multiple]):has(> input[data-slean-part='trigger'])) {
		flex-wrap: wrap;
	}

	:where(
		div[data-slean='select']:has(> select[multiple])
			> [data-slean-part='value']:has(~ input[data-slean-part='trigger'])
	) {
		display: contents;
	}

	:where(div[data-slean='select']:has(> select[multiple]) > input[data-slean-part='trigger']) {
		flex: 1 0 4rem;
		min-block-size: calc(var(--slean-control-height-md) - 8px);
	}

	/* The search text's placeholder gives way to the tags. */
	:where(
		div[data-slean='select']:has([data-slean-part='tag']) > input[data-slean-part='trigger']
	)::placeholder {
		color: transparent;
	}

	:where(div[data-slean='select']:has(> select[multiple]) [data-slean-part='value']) {
		display: flex;
		flex-wrap: wrap;
		gap: var(--slean-space-1);
		white-space: normal;
	}

	/* The behavior writes the tags when the value part is first drawn: it hears the start of this
	 * empty, zero-length animation. The application renders them with data-slean-render="app". */
	:where(
		div[data-slean='select']:has(> select[multiple]):not([data-slean-render='app'])
			[data-slean-part='value']
	) {
		animation-name: slean-select-tags;
	}

	@keyframes slean-select-tags {
	}

	:where(div[data-slean='select'] [data-slean-part='tag']) {
		display: inline-flex;
		align-items: center;
		gap: 2px;
		max-inline-size: 100%;
		min-block-size: calc(var(--slean-control-height-md) - 10px);
		padding-inline: var(--slean-space-2) 2px;
		border-radius: var(--slean-chip-radius);
		background: var(--slean-muted);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: 1.25;
		white-space: nowrap;
	}

	:where(div[data-slean='select'] [data-slean-part='remove']) {
		appearance: none;
		display: inline-grid;
		place-items: center;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-fg-muted);
		cursor: pointer;
	}

	:where(div[data-slean='select'] [data-slean-part='remove'])::before {
		content: '';
		inline-size: 0.75rem;
		block-size: 0.75rem;
		background: currentColor;
		mask: var(--slean-icon-close) center / contain no-repeat;
	}

	@media (hover: hover) {
		:where(div[data-slean='select'] [data-slean-part='remove']:hover) {
			background: var(--slean-muted-hover);
			color: var(--slean-fg);
		}
	}

	@media (forced-colors: active) {
		:where(div[data-slean='select'] [data-slean-part='tag']) {
			border: 1px solid CanvasText;
		}

		:where(div[data-slean='select'] [data-slean-part='remove'])::before {
			forced-color-adjust: none;
			background: CanvasText;
		}
	}

	/* Last in the layer: it must win over the trigger's display above (equal specificity). */
	@media (scripting: none) {
		:where(div[data-slean='select']) {
			display: block;
			padding: 0;
			border: 0;
			background: transparent;
			box-shadow: none;
		}

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

		:where(div[data-slean='select'] > [data-slean-part='native']) {
			inline-size: 100%;
		}

		:where(div[data-slean='select'] > [data-slean-part='trigger']),
		:where(div[data-slean='select'] > [data-slean-part='value']),
		:where(div[data-slean='select'] > [data-slean-part='prefix']),
		:where(div[data-slean='select'] > [data-slean-part='suffix']),
		:where(div[data-slean='select'] > [data-slean-part='clear']),
		:where(div[data-slean='select'] > [data-slean-part='popup']) {
			display: none;
		}
	}
}
