/* @svelte-lean/styles — tree (Tier 1; keyboard behavior comes from @svelte-lean/primitives/tree).
 * Markup: <ul role="tree" aria-label="…" data-slean="tree"> of <li role="treeitem"> items, each
 * with a [data-slean-part="label"] and, for a parent, aria-expanded and a <ul role="group"> of
 * children. State: aria-expanded (a collapsed item's group is hidden), aria-selected, the roving
 * tabindex, aria-disabled. The item is focused but holds its children, so the focus ring is drawn
 * on its label instead of around the whole subtree. Color: data-color on the root or an ancestor
 * tints the selected item (the soft tones; primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='tree']),
	:where([data-slean='tree'] [role='group']) {
		display: flex;
		flex-direction: column;
		gap: 1px;
		margin: 0;
		padding: 0;
		list-style: none;
	}

	:where([data-slean='tree']) {
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
	}

	/* Children are indented under the parent's chevron, with a guide line. */
	:where([data-slean='tree'] [role='group']) {
		margin-block-start: 1px;
		margin-inline-start: calc(var(--slean-space-2) + 0.5rem);
		padding-inline-start: var(--slean-space-1);
		border-inline-start: 1px solid var(--slean-border);
	}

	:where([data-slean='tree'] [aria-expanded='false'] > [role='group']) {
		display: none;
	}

	:where([data-slean='tree'] [role='treeitem']:focus-visible) {
		outline-style: none;
	}

	:where([data-slean='tree'] [data-slean-part='label']) {
		display: flex;
		align-items: center;
		gap: var(--slean-space-2);
		min-block-size: var(--slean-control-height-sm);
		padding-block: var(--slean-space-1);
		padding-inline: calc(var(--slean-space-2) + 1rem + var(--slean-space-2)) var(--slean-space-2);
		border-radius: var(--slean-radius-sm);
		cursor: default;
		user-select: none;
		-webkit-tap-highlight-color: transparent;
	}

	:where([data-slean='tree'] [aria-expanded] > [data-slean-part='label']) {
		padding-inline-start: var(--slean-space-2);
	}

	/* The chevron of a parent: the chevron icon, pointing at the children (inline end) while
	 * collapsed and down at them while expanded. */
	:where([data-slean='tree'] [aria-expanded] > [data-slean-part='label'])::before {
		content: '';
		flex: none;
		inline-size: 1rem;
		block-size: 1rem;
		background: var(--slean-fg-muted);
		mask: var(--slean-icon-chevron-right) center / contain no-repeat;
		transition: rotate var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='tree'] [aria-expanded='true'] > [data-slean-part='label'])::before {
		rotate: 90deg;
	}

	:where([data-slean='tree'] [aria-expanded='false'] > [data-slean-part='label']:dir(rtl))::before {
		scale: -1 1;
	}

	@media (hover: hover) {
		:where(
			[data-slean='tree']
				[role='treeitem']:not([aria-disabled='true'])
				> [data-slean-part='label']:hover
		) {
			background: var(--slean-muted);
		}
	}

	/* Keyboard focus is the row's fill, as a hover; forced colors paint the transparent outline. */
	:where([data-slean='tree'] [role='treeitem']:focus-visible > [data-slean-part='label']) {
		background: var(--slean-option-active);
		outline: var(--slean-focus-ring);
		outline-color: transparent;
		outline-offset: calc(-1 * var(--slean-focus-ring-width));
	}

	:where([data-slean='tree'] [aria-selected='true'] > [data-slean-part='label']) {
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
	}

	:where(
		[data-slean='tree']
			[role='treeitem'][aria-selected='true']:focus-visible
			> [data-slean-part='label']
	) {
		box-shadow: inset 0 0 0 1px var(--slean-color-soft-text);
	}

	:where([data-slean='tree'] [aria-disabled='true'] > [data-slean-part='label']) {
		opacity: 0.5;
		cursor: not-allowed;
	}

	@media (pointer: coarse) {
		:where([data-slean='tree'] [data-slean-part='label']) {
			min-block-size: var(--slean-control-height-md);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='tree'] [aria-selected='true'] > [data-slean-part='label']) {
			background: Highlight;
			color: HighlightText;
			forced-color-adjust: none;
		}

		:where([data-slean='tree'] [aria-disabled='true'] > [data-slean-part='label']) {
			color: GrayText;
			opacity: 1;
		}

		:where([data-slean='tree'] [aria-expanded] > [data-slean-part='label'])::before {
			forced-color-adjust: none;
			background: currentColor;
		}
	}
}
