/* @svelte-lean/styles — carousel (Tier 0, scroll snapping and CSS carousels).
 * Markup: <section data-slean="carousel" aria-roledescription="carousel" aria-label="…"> <div
 * data-slean-part="viewport" tabindex="0"> <div data-slean-part="slide" role="group"
 * aria-roledescription="slide" aria-label="1 of 4">…</div> … </div> </section>.
 * Everywhere: a horizontal row that scrolls and snaps one slide at a time. Where the browser
 * implements CSS carousels (::scroll-button(), ::scroll-marker), the viewport also generates a
 * previous and a next button at the ends of a row below it and one marker per slide in between,
 * a group of tabs (scroll-marker-group: after tabs).
 * Their states come from scroll-state container queries rather than from :disabled and
 * :target-current on the pseudo-elements, which a zero-specificity selector cannot reach: a
 * button is faded when the viewport cannot scroll its way, and the marker of the snapped slide
 * is filled and wider. The button names are the browser's unless the viewport carries
 * data-slean-previous and data-slean-next. Color: data-color on the root or an ancestor colors
 * the marker of the snapped slide (primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='carousel']) {
		position: relative;
		display: flex;
		flex-direction: column;
		min-inline-size: 0;
		color: var(--slean-fg);
	}

	:where([data-slean='carousel'] [data-slean-part='viewport']) {
		display: flex;
		gap: var(--slean-space-4);
		overflow-x: auto;
		overflow-y: hidden;
		overscroll-behavior-inline: contain;
		scroll-snap-type: inline mandatory;
		/* The markers are tabs: a tablist of one tab per slide, the snapped one selected, one tab
		 * stop and arrow keys between them. The mode keyword is explicit because the default is
		 * links (a navigation of links, each its own tab stop), and Chrome 154 exposes the default
		 * that way. An engine without the keyword drops the second declaration and keeps the
		 * first. */
		scroll-marker-group: after;
		scroll-marker-group: after tabs;
		container-type: scroll-state;
		border-radius: var(--slean-radius-lg);
	}

	@media (prefers-reduced-motion: no-preference) {
		:where([data-slean='carousel'] [data-slean-part='viewport']) {
			scroll-behavior: smooth;
		}
	}

	:where([data-slean='carousel'] [data-slean-part='slide']) {
		flex: 0 0 100%;
		min-inline-size: 0;
		scroll-snap-align: start;
		container-type: scroll-state;
	}

	/* CSS carousels: the scrollbar gives way to the buttons and markers. */
	@supports selector(::scroll-marker) {
		:where([data-slean='carousel'] [data-slean-part='viewport']) {
			scrollbar-width: none;
		}
	}

	:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-marker-group {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
		min-block-size: 2.25rem;
		margin-block-start: var(--slean-space-3);
		margin-inline: 2.75rem;
	}

	/* A marker is a 1.5rem target around a small dot (the fill is clipped to the content box). */
	:where([data-slean='carousel'] [data-slean-part='slide'])::scroll-marker {
		content: '';
		box-sizing: border-box;
		inline-size: 1.5rem;
		block-size: 1.5rem;
		padding: 0.5rem;
		border-radius: var(--slean-radius-full);
		background: var(--slean-border-strong) content-box;
		cursor: pointer;
		transition:
			inline-size var(--slean-duration-fast) var(--slean-ease),
			background-color var(--slean-duration-fast) var(--slean-ease);
	}

	@container scroll-state(snapped: inline) {
		:where([data-slean='carousel'] [data-slean-part='slide'])::scroll-marker {
			inline-size: 2rem;
			background-color: var(--slean-color-main);
		}
	}

	/* The two buttons sit at the ends of the marker row, above it, so a click reaches them and not
	 * the marker group. Only the inline-start and inline-end rules set content, so no button is
	 * generated for the other directions ::scroll-button(*) matches. A button is a pseudo-element
	 * that cannot carry its own ::before, so the whole button is painted in its text color through
	 * two mask layers: the chevron icon and a thin ring (an outline button in one color). */
	:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(*) {
		position: absolute;
		inset-block-end: 0;
		z-index: 1;
		box-sizing: border-box;
		inline-size: 2.25rem;
		block-size: 2.25rem;
		border: 0;
		border-radius: var(--slean-radius-full);
		background: currentColor;
		color: var(--slean-fg);
		mask:
			var(--slean-icon-chevron-left) center / 1rem no-repeat,
			radial-gradient(
				closest-side,
				transparent calc(100% - 1.5px),
				color-mix(in oklch, currentColor 30%, transparent) calc(100% - 1px),
				transparent
			);
		opacity: 0.4;
		cursor: pointer;
		transition: opacity var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(inline-start) {
		content: '';
		inset-inline-start: 0;
	}

	:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(inline-end) {
		content: '';
		inset-inline-end: 0;
		mask-image:
			var(--slean-icon-chevron-right),
			radial-gradient(
				closest-side,
				transparent calc(100% - 1.5px),
				color-mix(in oklch, currentColor 30%, transparent) calc(100% - 1px),
				transparent
			);
	}

	/* Under dir="rtl" both chevrons are mirrored: the previous button is the right one. */
	:where([data-slean='carousel'] [data-slean-part='viewport']:dir(rtl))::scroll-button(*) {
		scale: -1 1;
	}

	/* Author names, in the page language, for the two buttons (attributes of the viewport). */
	:where([data-slean='carousel'] [data-slean-previous])::scroll-button(inline-start) {
		content: '' / attr(data-slean-previous);
	}

	:where([data-slean='carousel'] [data-slean-next])::scroll-button(inline-end) {
		content: '' / attr(data-slean-next);
	}

	@container scroll-state(scrollable: inline-start) {
		:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(inline-start) {
			opacity: 1;
		}
	}

	@container scroll-state(scrollable: inline-end) {
		:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(inline-end) {
			opacity: 1;
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='carousel'] [data-slean-part='viewport'])::scroll-button(*) {
			forced-color-adjust: none;
			background: ButtonText;
		}

		:where([data-slean='carousel'] [data-slean-part='slide'])::scroll-marker {
			forced-color-adjust: none;
			background-color: CanvasText;
		}

		@container scroll-state(snapped: inline) {
			:where([data-slean='carousel'] [data-slean-part='slide'])::scroll-marker {
				background-color: Highlight;
			}
		}
	}
}
