/* @svelte-lean/styles — accordion (Tier 0, native <details> items, no wrapper).
 * Markup: <details data-slean="accordion" name="faq" open> <summary>…</summary> content or <div
 * data-slean-part="panel">…</div> </details>, one per item. Adjacent items join into one bordered
 * list: the second and later items drop their top border and corners, :has() takes the bottom
 * corners off every item followed by another. One item open at a time comes from the shared name
 * attribute. Open state is the native [open] attribute. The item pads its content: inline on the
 * item (the summary takes it back with negative margins), the block end on ::details-content,
 * whose height animates where interpolate-size exists; elsewhere the content appears without a
 * transition. Variant (data-variant): none (a list on the surface color) or ghost (no border, no
 * surface, no inline padding; a rule between adjacent ghost items). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where(details[data-slean='accordion']) {
		display: block;
		padding-inline: var(--slean-space-4);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-radius-lg);
		background: var(--slean-surface);
		color: var(--slean-fg);
		interpolate-size: allow-keywords;
	}

	/* Joined: an item that follows another shares its border and corners. */
	:where(
		details[data-slean='accordion']:not([data-variant='ghost'])
			+ details[data-slean='accordion']:not([data-variant='ghost'])
	) {
		border-block-start: 0;
		border-start-start-radius: 0;
		border-start-end-radius: 0;
	}

	:where(
		details[data-slean='accordion']:not([data-variant='ghost']):has(
				+ details[data-slean='accordion']:not([data-variant='ghost'])
			)
	) {
		border-end-start-radius: 0;
		border-end-end-radius: 0;
	}

	:where(details[data-slean='accordion'] > summary) {
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: var(--slean-space-3);
		min-block-size: var(--slean-control-height-lg);
		margin-inline: calc(var(--slean-space-4) * -1);
		padding-block: var(--slean-space-2);
		padding-inline: var(--slean-space-4);
		border-radius: calc(var(--slean-radius-lg) - 1px);
		font-weight: var(--slean-font-weight-medium);
		list-style: none;
		cursor: pointer;
		user-select: none;
		-webkit-tap-highlight-color: transparent;
		transition: background-color var(--slean-duration-fast) var(--slean-ease);
	}

	:where(details[data-slean='accordion'] > summary)::-webkit-details-marker {
		display: none;
	}

	/* The summary keeps the item's corners where the item has them, so the hover fill and the inset
	 * focus ring never cross the border. */
	:where(details[data-slean='accordion'][open] > summary) {
		border-end-start-radius: 0;
		border-end-end-radius: 0;
	}

	:where(
		details[data-slean='accordion']:not([data-variant='ghost'])
			+ details[data-slean='accordion']:not([data-variant='ghost'])
			> summary
	) {
		border-start-start-radius: 0;
		border-start-end-radius: 0;
	}

	:where(
		details[data-slean='accordion']:not([data-variant='ghost']):has(
				+ details[data-slean='accordion']:not([data-variant='ghost'])
			)
			> summary
	) {
		border-end-start-radius: 0;
		border-end-end-radius: 0;
	}

	:where(details[data-slean='accordion'] > summary)::after {
		content: '';
		flex: none;
		inline-size: 1rem;
		block-size: 1rem;
		background: currentColor;
		mask: var(--slean-icon-chevron-down) center / contain no-repeat;
		opacity: 0.7;
		transition: rotate var(--slean-duration-normal) var(--slean-ease);
	}

	:where(details[data-slean='accordion'][open] > summary)::after {
		rotate: 180deg;
	}

	:where(details[data-slean='accordion'] > summary:focus-visible) {
		outline: var(--slean-focus-ring);
		outline-offset: calc(var(--slean-focus-ring-width) * -1);
	}

	@media (hover: hover) {
		:where(details[data-slean='accordion'] > summary:hover) {
			background: var(--slean-muted);
		}

		:where(details[data-slean='accordion'] > summary:hover)::after {
			opacity: 1;
		}
	}

	/* The content: padded at the block end, the height animated where interpolate-size exists. The
	 * padding moves with the height so a closed item takes no space. */
	:where(details[data-slean='accordion'])::details-content {
		block-size: 0;
		padding-block-end: 0;
		overflow: clip;
		transition:
			block-size var(--slean-duration-normal) var(--slean-ease),
			padding-block-end var(--slean-duration-normal) var(--slean-ease),
			content-visibility var(--slean-duration-normal) allow-discrete;
	}

	:where(details[data-slean='accordion'][open])::details-content {
		block-size: auto;
		padding-block-end: var(--slean-space-4);
	}

	@supports not selector(::details-content) {
		:where(details[data-slean='accordion'][open]) {
			padding-block-end: var(--slean-space-4);
		}
	}

	:where(details[data-slean='accordion'] > [data-slean-part='panel']) {
		display: block;
		min-inline-size: 0;
	}

	/* Ghost: the bare show-and-hide section, no border, surface or inline padding. */
	:where(details[data-slean='accordion'][data-variant='ghost']) {
		padding-inline: 0;
		border: 0;
		border-radius: 0;
		background: transparent;
	}

	:where(details[data-slean='accordion'][data-variant='ghost'] > summary) {
		min-block-size: var(--slean-control-height-md);
		margin-inline: 0;
		padding-inline: 0;
		border-radius: var(--slean-radius-sm);
	}

	:where(details[data-slean='accordion'][data-variant='ghost'] > summary:focus-visible) {
		outline-offset: var(--slean-focus-ring-offset);
	}

	@media (hover: hover) {
		:where(details[data-slean='accordion'][data-variant='ghost'] > summary:hover) {
			background: transparent;
		}
	}

	:where(
		details[data-slean='accordion'][data-variant='ghost']
			+ details[data-slean='accordion'][data-variant='ghost']
	) {
		border-block-start: 1px solid var(--slean-border);
	}

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