/* @svelte-lean/styles — card (Tier 0, <article>).
 * Markup: <article data-slean="card" data-variant="outlined"> with optional parts media (bleeds to
 * the edges when first or last), header, title, description, body (takes the free height) and
 * footer. Structural parts are matched by element first (ADR 0011 §2.4): an <img>, <video> or
 * <picture> that is the first or last child is the media, a child <header> the header, its first
 * heading the title and a <p> right after it the description, a child <footer> the footer;
 * data-slean-part names an element the structure does not tell. Parts are children of the card;
 * title and description may be children of the header instead. A card whose whole surface opens
 * one page puts data-slean-part="link" on the real <a> that is a child of its title: the link's
 * ::after covers the card, other controls stay their own targets, and the focus ring moves to
 * the card.
 * Variant (data-variant, MUI's Paper): elevation (default: a shadow, --slean-card-elevation, over
 * a hairline that keeps the edge in a dark theme) or outlined (a border, no shadow). Radius:
 * --slean-card-radius. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='card']) {
		position: relative;
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-4);
		min-inline-size: 0;
		padding: var(--slean-space-5);
		border: 1px solid color-mix(in oklch, var(--slean-border) 55%, transparent);
		border-radius: var(--slean-card-radius);
		background: var(--slean-surface);
		box-shadow: var(--slean-card-elevation);
		color: var(--slean-fg);
	}

	:where([data-slean='card'][data-variant='outlined']) {
		border-color: var(--slean-border);
		box-shadow: none;
	}

	/* Every selector below is keyed on an attribute or an element name and follows the card's
	 * structure with child combinators: no card selector is tried against every element of a large
	 * subtree (a table of a thousand rows). No selector list inside :where() and no :is() in the
	 * key (Chromium matches either against every element of the page), no ancestor-walking :not(),
	 * and :has() only over fixed-depth child chains and only on the element the rule styles: a
	 * :has() on an ancestor (card:has(> … > link) button) makes Chromium restyle every element the
	 * key could match under that ancestor whenever an element is inserted anywhere in it, so the
	 * rows of a table arriving in slices restyled all the rows before them at every slice
	 * (apps/playground/tests/primitives/selector-cost.spec.ts).
	 * A part is the card's where the card's structure puts it, so the parts of a primitive nested
	 * in the card (a dialog's title, an alert's body) are never matched: a child of the card; the
	 * title and the description also a child of the header, or of a plain wrapper in it (beside an
	 * avatar); the link a child of the title. */

	/* Media bleeds over the padding at the edge it touches and takes the card's corners there. */
	:where([data-slean='card'] > [data-slean-part='media']),
	:where([data-slean='card'] > img:first-child),
	:where([data-slean='card'] > img:last-child),
	:where([data-slean='card'] > video:first-child),
	:where([data-slean='card'] > video:last-child),
	:where([data-slean='card'] > picture:first-child),
	:where([data-slean='card'] > picture:last-child) {
		display: block;
		inline-size: calc(100% + 2 * var(--slean-space-5));
		max-inline-size: none;
		margin-block: 0;
		margin-inline: calc(var(--slean-space-5) * -1);
		overflow: clip;
		object-fit: cover;
	}

	:where([data-slean='card'] > [data-slean-part='media'] > img),
	:where([data-slean='card'] > [data-slean-part='media'] > video),
	:where([data-slean='card'] > [data-slean-part='media'] > picture),
	:where([data-slean='card'] > [data-slean-part='media'] > picture > img) {
		display: block;
		inline-size: 100%;
		block-size: 100%;
		object-fit: cover;
	}

	:where([data-slean='card'] > [data-slean-part='media']:first-child),
	:where([data-slean='card'] > img:first-child),
	:where([data-slean='card'] > video:first-child),
	:where([data-slean='card'] > picture:first-child) {
		margin-block-start: calc(var(--slean-space-5) * -1);
		border-start-start-radius: calc(var(--slean-card-radius) - 1px);
		border-start-end-radius: calc(var(--slean-card-radius) - 1px);
	}

	:where([data-slean='card'] > [data-slean-part='media']:last-child),
	:where([data-slean='card'] > img:last-child),
	:where([data-slean='card'] > video:last-child),
	:where([data-slean='card'] > picture:last-child) {
		margin-block-end: calc(var(--slean-space-5) * -1);
		border-end-start-radius: calc(var(--slean-card-radius) - 1px);
		border-end-end-radius: calc(var(--slean-card-radius) - 1px);
	}

	:where([data-slean='card'] > header),
	:where([data-slean='card'] > [data-slean-part='header']) {
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-1);
	}

	:where([data-slean='card'] > header > h1:first-child),
	:where([data-slean='card'] > header > h2:first-child),
	:where([data-slean='card'] > header > h3:first-child),
	:where([data-slean='card'] > header > h4:first-child),
	:where([data-slean='card'] > header > h5:first-child),
	:where([data-slean='card'] > header > h6:first-child),
	:where([data-slean='card'] > [data-slean-part='title']),
	:where([data-slean='card'] > :is(header, [data-slean-part='header']) > [data-slean-part='title']),
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :not([data-slean])
			> [data-slean-part='title']
	) {
		margin: 0;
		font-size: var(--slean-text-lg);
		font-weight: var(--slean-font-weight-semibold);
		line-height: 1.3;
	}

	:where([data-slean='card'] > header > h1:first-child + p),
	:where([data-slean='card'] > header > h2:first-child + p),
	:where([data-slean='card'] > header > h3:first-child + p),
	:where([data-slean='card'] > header > h4:first-child + p),
	:where([data-slean='card'] > header > h5:first-child + p),
	:where([data-slean='card'] > header > h6:first-child + p),
	:where([data-slean='card'] > [data-slean-part='description']),
	:where(
		[data-slean='card'] > :is(header, [data-slean-part='header']) > [data-slean-part='description']
	),
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :not([data-slean])
			> [data-slean-part='description']
	) {
		margin: 0;
		color: var(--slean-fg-muted);
		font-size: var(--slean-text-sm);
	}

	:where([data-slean='card'] > [data-slean-part='body']) {
		flex: 1 1 auto;
		min-block-size: 0;
	}

	:where([data-slean='card'] > footer),
	:where([data-slean='card'] > [data-slean-part='footer']) {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: var(--slean-space-2);
		margin-block-start: auto;
	}

	/* Stretched link: one real <a> in the title, its hit area enlarged to the card by ::after.
	 * The link's accessible name stays the title text. The ::after lies under the card's content
	 * (below, the linked card is its own stacking context) and takes the pointer where the content
	 * does not; without :has() it is under the card too, and the link is its own text only. */
	:where(
		[data-slean='card']
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	),
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	),
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :not([data-slean])
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	) {
		color: inherit;
		text-decoration: none;
	}

	:where(
		[data-slean='card']
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	)::after,
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	)::after,
	:where(
		[data-slean='card']
			> :is(header, [data-slean-part='header'])
			> :not([data-slean])
			> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
			> [data-slean-part='link']
	)::after {
		content: '';
		position: absolute;
		z-index: -1;
		inset: 0;
		border-radius: var(--slean-card-radius);
		pointer-events: auto;
	}

	/* A card with a stretched link takes no pointer events and its content inherits that, so the
	 * pointer reaches the link's area through the text, the images and the padding. Only the card
	 * asks whether it holds the link; its content learns it by inheritance, never by a :has() on
	 * its ancestor. The controls (and a popover or dialog) take the pointer back, with everything
	 * inside them, in a sublayer: a component rule that sets pointer-events on a control (the
	 * inputs of a two-thumb slider) still wins. */
	@supports selector(:has(*)) {
		:where(
			[data-slean='card']:has(
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title']) > [data-slean-part='link'],
				> :is(header, [data-slean-part='header'])
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
					> [data-slean-part='link'],
				> :is(header, [data-slean-part='header'])
					> :not([data-slean])
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
					> [data-slean-part='link']
			)
		) {
			isolation: isolate;
			pointer-events: none;
		}

		@layer card {
			:where([data-slean='card'] a[href]),
			:where([data-slean='card'] button),
			:where([data-slean='card'] input),
			:where([data-slean='card'] select),
			:where([data-slean='card'] textarea),
			:where([data-slean='card'] summary),
			:where([data-slean='card'] [tabindex]),
			:where([data-slean='card'] [popover]),
			:where([data-slean='card'] dialog) {
				pointer-events: auto;
			}
		}

		/* The focus ring moves from the link to the card only where :has() can draw it there. */
		:where(
			[data-slean='card']:has(
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
					> [data-slean-part='link']:focus-visible,
				> :is(header, [data-slean-part='header'])
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
					> [data-slean-part='link']:focus-visible,
				> :is(header, [data-slean-part='header'])
					> :not([data-slean])
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
					> [data-slean-part='link']:focus-visible
			)
		) {
			outline: var(--slean-focus-ring);
			outline-offset: var(--slean-focus-ring-offset);
		}

		/* The link's own ring is replaced by the card's, not removed: the card draws it above. */
		:where(
			[data-slean='card']
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']:focus-visible
		),
		:where(
			[data-slean='card']
				> :is(header, [data-slean-part='header'])
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']:focus-visible
		),
		:where(
			[data-slean='card']
				> :is(header, [data-slean-part='header'])
				> :not([data-slean])
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']:focus-visible
		) {
			outline-style: none;
		}
	}

	@media (hover: hover) {
		:where(
			[data-slean='card']:hover:has(
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title']) > [data-slean-part='link'],
					> :is(header, [data-slean-part='header'])
						> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
						> [data-slean-part='link'],
					> :is(header, [data-slean-part='header'])
						> :not([data-slean])
						> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
						> [data-slean-part='link']
				)
		) {
			box-shadow: var(--slean-elevation-2);
		}

		:where(
			[data-slean='card'][data-variant='outlined']:hover:has(
					> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title']) > [data-slean-part='link'],
					> :is(header, [data-slean-part='header'])
						> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
						> [data-slean-part='link'],
					> :is(header, [data-slean-part='header'])
						> :not([data-slean])
						> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
						> [data-slean-part='link']
				)
		) {
			border-color: var(--slean-border-strong);
			box-shadow: none;
		}

		:where(
			[data-slean='card']:hover
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']
		),
		:where(
			[data-slean='card']:hover
				> :is(header, [data-slean-part='header'])
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']
		),
		:where(
			[data-slean='card']:hover
				> :is(header, [data-slean-part='header'])
				> :not([data-slean])
				> :is(h1, h2, h3, h4, h5, h6, [data-slean-part='title'])
				> [data-slean-part='link']
		) {
			text-decoration: underline;
			text-underline-offset: 0.2em;
		}
	}

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