/* @svelte-lean/styles — date picker (Tier 2; behavior comes from @svelte-lean/primitives/date-picker).
 * Markup: <div data-slean="date-picker"> around <input type="date" data-slean-part="native">
 * (two for a range) and a text input per native one (<input role="combobox"
 * data-slean-part="input" placeholder="…">), a separator between the two of a range. The behavior
 * adds the clear button (data-slean-clearable) and, on the first open, a <div popover="manual"
 * data-slean-part="popup"> holding the calendar (with its month and year selects) and a footer
 * with a Today button (data-slean-today) and, over <input type="datetime-local"> natives, one
 * <input type="time" data-slean-part="time"> per native input; an authored popup with a footer of presets receives the calendar after the
 * footer, and the footer is drawn last. A presets part in the popup is a list of presets beside
 * the calendar; the preset that is the value has aria-pressed="true". The calendar icon is the
 * root's ::after (ADR 0011 §2.1), so a press on it is a press on the field; the clear button takes
 * its place while a field with a value is hovered or focused. With data-slean-empty, an empty
 * field without focus shows the option's text on the root's ::before in place of its
 * placeholders. The field and the popup surface come from control.css (load it
 * first) and the grid from calendar.css; the native inputs are styled here for the path without
 * scripting. With scripting the native inputs give way to the text inputs; without it they are
 * the field. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;
@import './control.css';
@import './calendar.css';

@layer slean.compositions {
	:where([data-slean='date-picker']) {
		inline-size: auto;
		min-inline-size: 12rem;
	}

	:where([data-slean='date-picker']:has([data-slean-part='separator'])) {
		min-inline-size: 18rem;
	}

	:where([data-slean='date-picker'] [data-slean-part='input']) {
		inline-size: 0;
		font-variant-numeric: tabular-nums;
	}

	/* The calendar icon: the root's last box. */
	:where([data-slean='date-picker'])::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-calendar) center / contain no-repeat;
	}

	:where(
		[data-slean='date-picker']:hover:has(
				[data-slean-part='input']:not(:placeholder-shown, :disabled)
			):has([data-slean-part='clear'])
	)::after,
	:where(
		[data-slean='date-picker']:focus-within:has(
				[data-slean-part='input']:not(:placeholder-shown, :disabled)
			):has([data-slean-part='clear'])
	)::after {
		display: none;
	}

	@media (hover: hover) {
		:where([data-slean='date-picker']:hover)::after {
			background: var(--slean-fg);
		}
	}

	:where([data-slean='date-picker'] [data-slean-part='separator']) {
		display: inline-grid;
		place-items: center;
		flex: none;
		inline-size: 1.25rem;
		color: var(--slean-control-icon);
	}

	:where([data-slean='date-picker'] [data-slean-part='separator'])::before {
		mask-image: var(--slean-icon-arrow-right);
	}

	:where([data-slean='date-picker']:dir(rtl) [data-slean-part='separator'])::before {
		scale: -1 1;
	}

	/* A range: the input that has focus is the end the next day fills; a bar marks it. */
	:where(
		[data-slean='date-picker']:has([data-slean-part='separator']) [data-slean-part='input']:focus
	) {
		box-shadow: inset 0 -2px 0 var(--slean-color-main);
	}

	/* An empty field without focus says what empty means (data-slean-empty="All time"): the
	 * option's text in place of the placeholders and the separator. The text inputs stay in the
	 * field, at no width, so Tab and a press still reach them; focus brings the placeholders back. */
	:where(
		[data-slean='date-picker'][data-slean-empty]:not(
				:focus-within,
				:has([data-slean-part='input']:not(:placeholder-shown)),
				:has([aria-expanded='true'])
			)
	)::before {
		content: attr(data-slean-empty);
		flex: 1 1 auto;
		min-inline-size: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	:where(
		[data-slean='date-picker'][data-slean-empty]:not(
				:focus-within,
				:has([data-slean-part='input']:not(:placeholder-shown)),
				:has([aria-expanded='true'])
			)
			[data-slean-part='input']
	) {
		flex: none;
		opacity: 0;
	}

	:where(
		[data-slean='date-picker'][data-slean-empty]:not(
				:focus-within,
				:has([data-slean-part='input']:not(:placeholder-shown)),
				:has([aria-expanded='true'])
			)
			[data-slean-part='separator']
	) {
		display: none;
	}

	/* The popup: the calendar, then the footer, whatever their order in the markup. */
	:where([data-slean='date-picker'] [data-slean-part='popup']:popover-open) {
		display: flex;
		flex-direction: column;
	}

	:where([data-slean='date-picker'] [data-slean-part='popup']) {
		padding: 0;
	}

	:where([data-slean='date-picker'] [data-slean-part='popup'] [data-slean='calendar']) {
		display: grid;
	}

	:where([data-slean='date-picker'] [data-slean-part='footer']) {
		order: 1;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		gap: var(--slean-space-2);
		padding: var(--slean-space-2) var(--slean-space-3);
		border-block-start: 1px solid var(--slean-border);
	}

	:where([data-slean='date-picker'] [data-slean-part='footer']:has([data-slean-part='preset'])) {
		justify-content: flex-start;
	}

	/* Presets beside the calendar: a presets part in the popup is a column at its inline start,
	 * the calendar and the footer beside it. */
	:where(
		[data-slean='date-picker']
			[data-slean-part='popup']:popover-open:has(> [data-slean-part='presets'])
	) {
		display: grid;
		grid-template-columns: auto auto;
		grid-template-areas:
			'presets calendar'
			'presets footer';
		align-items: start;
	}

	:where([data-slean='date-picker'] [data-slean-part='popup'] > [data-slean-part='presets']) {
		grid-area: presets;
		align-self: stretch;
		display: flex;
		flex-direction: column;
		gap: 2px;
		min-inline-size: 8rem;
		padding: var(--slean-space-2);
		border-inline-end: 1px solid var(--slean-border);
	}

	:where(
		[data-slean='date-picker']
			[data-slean-part='popup']:has(> [data-slean-part='presets'])
			> [data-slean='calendar']
	) {
		grid-area: calendar;
	}

	:where(
		[data-slean='date-picker']
			[data-slean-part='popup']:has(> [data-slean-part='presets'])
			> [data-slean-part='footer']
	) {
		grid-area: footer;
	}

	/* The time of a datetime-local picker: a compact native time input, the platform's segments. */
	:where([data-slean='date-picker'] input[data-slean-part='time']) {
		box-sizing: border-box;
		min-block-size: var(--slean-control-height-sm);
		padding-block: 0;
		padding-inline: var(--slean-space-2);
		border: 1px solid var(--slean-control-border);
		border-radius: var(--slean-radius-sm);
		background: var(--slean-control-bg);
		color: var(--slean-fg);
		color-scheme: var(--slean-color-scheme);
		font: inherit;
		font-size: var(--slean-text-sm);
		font-variant-numeric: tabular-nums;
	}

	/* A time beside Today: the time leads, Today follows at the end. */
	:where([data-slean='date-picker'] [data-slean-part='footer']:has([data-slean-part='time'])) {
		justify-content: space-between;
	}

	:where([data-slean='date-picker'] [data-slean-part='today']) {
		appearance: none;
		padding: var(--slean-space-1) var(--slean-space-2);
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-color-main);
		font: inherit;
		font-size: var(--slean-text-sm);
		cursor: pointer;
	}

	:where([data-slean='date-picker'] [data-slean-part='preset']) {
		appearance: none;
		padding: 1px var(--slean-space-2);
		border: 1px solid var(--slean-color-soft);
		border-radius: var(--slean-radius-sm);
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
		font: inherit;
		font-size: var(--slean-text-xs);
		line-height: 1.5rem;
		cursor: pointer;
	}

	/* The preset that is the value (the behavior writes aria-pressed while the popup is open). */
	:where([data-slean='date-picker'] [data-slean-part='preset'][aria-pressed='true']) {
		border-color: var(--slean-color-main);
		background: var(--slean-color-main);
		color: var(--slean-color-contrast-text);
	}

	/* In the side list a preset is a row: its text at the start, the soft tint when it is the value. */
	:where([data-slean='date-picker'] [data-slean-part='presets'] [data-slean-part='preset']) {
		padding: var(--slean-space-1) var(--slean-space-2);
		border-color: transparent;
		background: transparent;
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		text-align: start;
		white-space: nowrap;
	}

	:where(
		[data-slean='date-picker']
			[data-slean-part='presets']
			[data-slean-part='preset'][aria-pressed='true']
	) {
		border-color: transparent;
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
		font-weight: var(--slean-font-weight-medium);
	}

	@media (hover: hover) {
		:where([data-slean='date-picker'] [data-slean-part='today']:hover) {
			color: var(--slean-color-dark);
		}

		:where([data-slean='date-picker'] [data-slean-part='preset']:hover) {
			border-color: var(--slean-color-main);
		}

		:where(
			[data-slean='date-picker'] [data-slean-part='presets'] [data-slean-part='preset']:hover
		) {
			border-color: transparent;
			background: var(--slean-muted);
		}

		:where(
			[data-slean='date-picker']
				[data-slean-part='presets']
				[data-slean-part='preset'][aria-pressed='true']:hover
		) {
			background: var(--slean-color-soft);
		}

		:where([data-slean='date-picker'] input[data-slean-part='time']:hover) {
			border-color: var(--slean-control-border-hover, var(--slean-color-main));
		}
	}

	@media (scripting: enabled) {
		:where([data-slean='date-picker'] > [data-slean-part='native']) {
			display: none;
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='date-picker'])::after {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where([data-slean='date-picker'] [data-slean-part='preset'][aria-pressed='true']) {
			forced-color-adjust: none;
			border-color: Highlight;
			background: Highlight;
			color: HighlightText;
		}
	}

	/* Without scripting the native date inputs are the field: the owned parts would do nothing. */
	@media (scripting: none) {
		:where([data-slean='date-picker']) {
			display: flex;
			gap: var(--slean-space-2);
			padding: 0;
			border: 0;
			background: transparent;
			box-shadow: none;
		}

		:where([data-slean='date-picker'])::after,
		:where([data-slean='date-picker'])::before {
			display: none;
		}

		:where([data-slean='date-picker'] > [data-slean-part='native']) {
			box-sizing: border-box;
			min-block-size: var(--slean-control-height-md);
			padding-inline: var(--slean-space-3);
			border: 1px solid var(--slean-control-border);
			border-radius: var(--slean-radius-md);
			background: var(--slean-control-bg);
			color: var(--slean-fg);
			color-scheme: var(--slean-color-scheme);
			font: inherit;
			font-size: var(--slean-text-sm);
		}

		:where([data-slean='date-picker'] > [data-slean-part='input']),
		:where([data-slean='date-picker'] > [data-slean-part='separator']),
		:where([data-slean='date-picker'] > [data-slean-part='clear']),
		:where([data-slean='date-picker'] > [data-slean-part='popup']) {
			display: none;
		}
	}
}
