/* @svelte-lean/styles — scroll-area (Tier 0, a focusable scrolling region).
 * Markup: <div role="region" aria-label="…" tabindex="0" data-slean="scroll-area">…</div>, with a
 * block size (or a max) set by the page. Scrolling, the keyboard and the scrollbars are native;
 * this file makes the scrollbars thin and token-colored, keeps a stable gutter and contains
 * overscroll along the scrolling axis (only that axis, so a wheel across the other axis still
 * scrolls the page). Where scroll-driven animations exist, inset shadows mark the edges that have
 * more content; they are drawn under the content. Option: data-slean-orientation="horizontal".
 * Variant: data-variant="outlined" (border, radius, surface).
 *
 * Edge fades: data-slean-fade (block) or data-slean-fade="inline", on a scroll area or on any
 * element that scrolls (a sidebar's nav, a list in a card), fades the content itself out at the
 * edges it can still scroll towards, in place of the shadows. A mask works over any background,
 * a selected row's included. Depth: --slean-fade-size. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='scroll-area']) {
		display: block;
		min-inline-size: 0;
		overflow: auto;
		overscroll-behavior-block: contain;
		scrollbar-width: thin;
		scrollbar-color: var(--slean-border-strong) transparent;
		scrollbar-gutter: stable;
		border-radius: var(--slean-radius-md);
	}

	:where([data-slean='scroll-area'][data-slean-orientation='horizontal']) {
		overscroll-behavior-block: auto;
		overscroll-behavior-inline: contain;
	}

	:where([data-slean='scroll-area'][data-variant='outlined']) {
		border: 1px solid var(--slean-border);
		background: var(--slean-surface);
		color: var(--slean-fg);
	}

	/* Edge shadows: a scroll timeline on the region itself drives its inset shadows, so the start
	 * shadow appears once the content has moved and the end shadow leaves at the end. Chromium
	 * 115 and Safari 26; elsewhere the region scrolls without them. */
	@supports (animation-timeline: scroll()) {
		:where([data-slean='scroll-area']) {
			animation: slean-scroll-edges-block linear both;
			animation-timeline: scroll(self block);
		}

		:where([data-slean='scroll-area'][data-slean-orientation='horizontal']) {
			animation-name: slean-scroll-edges-inline;
			animation-timeline: scroll(self inline);
		}

		:where([data-slean='scroll-area'][data-slean-orientation='horizontal']:dir(rtl)) {
			animation-name: slean-scroll-edges-inline-rtl;
		}
	}

	@keyframes slean-scroll-edges-block {
		0% {
			box-shadow:
				inset 0 0.75rem 0.75rem -0.75rem transparent,
				inset 0 -0.75rem 0.75rem -0.75rem var(--slean-overlay);
		}

		8%,
		92% {
			box-shadow:
				inset 0 0.75rem 0.75rem -0.75rem var(--slean-overlay),
				inset 0 -0.75rem 0.75rem -0.75rem var(--slean-overlay);
		}

		100% {
			box-shadow:
				inset 0 0.75rem 0.75rem -0.75rem var(--slean-overlay),
				inset 0 -0.75rem 0.75rem -0.75rem transparent;
		}
	}

	@keyframes slean-scroll-edges-inline {
		0% {
			box-shadow:
				inset 0.75rem 0 0.75rem -0.75rem transparent,
				inset -0.75rem 0 0.75rem -0.75rem var(--slean-overlay);
		}

		8%,
		92% {
			box-shadow:
				inset 0.75rem 0 0.75rem -0.75rem var(--slean-overlay),
				inset -0.75rem 0 0.75rem -0.75rem var(--slean-overlay);
		}

		100% {
			box-shadow:
				inset 0.75rem 0 0.75rem -0.75rem var(--slean-overlay),
				inset -0.75rem 0 0.75rem -0.75rem transparent;
		}
	}

	/* Right-to-left: the inline start is on the right, so the two shadows trade sides. */
	@keyframes slean-scroll-edges-inline-rtl {
		0% {
			box-shadow:
				inset -0.75rem 0 0.75rem -0.75rem transparent,
				inset 0.75rem 0 0.75rem -0.75rem var(--slean-overlay);
		}

		8%,
		92% {
			box-shadow:
				inset -0.75rem 0 0.75rem -0.75rem var(--slean-overlay),
				inset 0.75rem 0 0.75rem -0.75rem var(--slean-overlay);
		}

		100% {
			box-shadow:
				inset -0.75rem 0 0.75rem -0.75rem var(--slean-overlay),
				inset 0.75rem 0 0.75rem -0.75rem transparent;
		}
	}

	/* Edge fades. Two mask layers, intersected, each a ramp (ghost to solid over
	 * --slean-fade-size) on an image one fade deeper than the element: the start layer is held a
	 * fade past the start edge and the region's scroll timeline slides it in over the first
	 * stretch scrolled; the end layer reaches a fade past the end edge and the timeline shrinks it
	 * onto the element, then grows it back over the last stretch. With nothing to scroll the
	 * timeline is inactive and both ramps stay out of sight. Declared after the shadows: on a
	 * scroll area the fades replace them. Chromium 115 and Safari 26; elsewhere no fade. */
	@supports (animation-timeline: scroll()) {
		:where([data-slean-fade]) {
			mask-image:
				linear-gradient(to bottom, var(--slean-fade-ramp)),
				linear-gradient(to top, var(--slean-fade-ramp));
			mask-repeat: no-repeat;
			mask-composite: intersect;
			mask-position:
				0 calc(var(--slean-fade-size) * -1),
				0 0;
			mask-size:
				100% calc(100% + var(--slean-fade-size)),
				100% calc(100% + var(--slean-fade-size));
			animation:
				slean-fade-start linear both,
				slean-fade-block-end linear both;
			animation-timeline: scroll(self block), scroll(self block);
			animation-range:
				0 var(--slean-fade-size),
				calc(100% - var(--slean-fade-size)) 100%;
		}

		:where([data-slean-fade='inline']) {
			mask-image:
				linear-gradient(to right, var(--slean-fade-ramp)),
				linear-gradient(to left, var(--slean-fade-ramp));
			mask-position:
				calc(var(--slean-fade-size) * -1) 0,
				0 0;
			mask-size:
				calc(100% + var(--slean-fade-size)) 100%,
				calc(100% + var(--slean-fade-size)) 100%;
			animation-name: slean-fade-start, slean-fade-inline-end;
			animation-timeline: scroll(self inline), scroll(self inline);
		}

		/* Right-to-left: the inline start is on the right; both layers anchor there. */
		:where([data-slean-fade='inline']:dir(rtl)) {
			mask-image:
				linear-gradient(to left, var(--slean-fade-ramp)),
				linear-gradient(to right, var(--slean-fade-ramp));
			mask-position:
				right calc(var(--slean-fade-size) * -1) top 0,
				right 0 top 0;
			animation-name: slean-fade-inline-start-rtl, slean-fade-inline-end;
		}
	}

	@keyframes slean-fade-start {
		to {
			mask-position:
				0 0,
				0 0;
		}
	}

	@keyframes slean-fade-block-end {
		from {
			mask-size:
				100% calc(100% + var(--slean-fade-size)),
				100% 100%;
		}
	}

	@keyframes slean-fade-inline-start-rtl {
		to {
			mask-position:
				right 0 top 0,
				right 0 top 0;
		}
	}

	@keyframes slean-fade-inline-end {
		from {
			mask-size:
				calc(100% + var(--slean-fade-size)) 100%,
				100% 100%;
		}
	}

	/* Forced colors replace scrollbar-color with the system scrollbar and remove box shadows; the
	 * outlined variant keeps a visible edge. */
	@media (forced-colors: active) {
		:where([data-slean='scroll-area'][data-variant='outlined']) {
			border-color: CanvasText;
		}
	}
}
