/* @svelte-lean/styles — sortable (Tier 2; the drag, keys and buttons come from
 * @svelte-lean/primitives/sortable). Markup: <ul data-slean="sortable"> whose children carrying
 * data-slean-value are the items, each with a [data-slean-part="handle"] button and optional
 * [data-slean-part="previous"] / [data-slean-part="next"] buttons. Options:
 * data-slean-orientation="horizontal" lays the items out in a row. States written by the behavior:
 * data-slean-dragging and data-slean-settling on the root, data-slean-lifted on the dragged item;
 * positions during a drag are inline transforms. Color: data-color on the root or an ancestor
 * colors the focused handle and the lifted item's border (primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='sortable']) {
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-1);
		margin: 0;
		padding: 0;
		list-style: none;
		color: var(--slean-fg);
	}

	:where([data-slean='sortable'][data-slean-orientation='horizontal']) {
		flex-direction: row;
		align-items: center;
	}

	/* An item is a row: the handle, its content, its buttons. */
	:where([data-slean='sortable'] > [data-slean-value]) {
		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: var(--slean-space-1) var(--slean-space-2);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-radius-md);
		background: var(--slean-surface);
	}

	/* Handle, previous and next: square icon buttons drawn from the icon tokens. */
	:where([data-slean='sortable'] [data-slean-part='handle']),
	:where([data-slean='sortable'] [data-slean-part='previous']),
	:where([data-slean='sortable'] [data-slean-part='next']) {
		display: inline-grid;
		flex: none;
		place-items: center;
		inline-size: var(--slean-control-height-xs);
		block-size: var(--slean-control-height-xs);
		margin: 0;
		padding: 0;
		border: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		color: var(--slean-fg-muted);
		cursor: pointer;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			color var(--slean-duration-fast) var(--slean-ease);
	}

	/* The handle takes the pointer for the drag: no scroll, no selection, no tap flash. */
	:where([data-slean='sortable'] [data-slean-part='handle']) {
		cursor: grab;
		touch-action: none;
		user-select: none;
		-webkit-user-select: none;
		-webkit-tap-highlight-color: transparent;
	}

	:where([data-slean='sortable'] [data-slean-part='handle'])::before,
	:where([data-slean='sortable'] [data-slean-part='previous'])::before,
	:where([data-slean='sortable'] [data-slean-part='next'])::before {
		content: '';
		inline-size: 1rem;
		block-size: 1rem;
		background: currentColor;
		mask: var(--slean-icon-grip) center / contain no-repeat;
	}

	:where([data-slean='sortable'] [data-slean-part='previous'])::before {
		mask-image: var(--slean-icon-chevron-up);
	}

	:where([data-slean='sortable'] [data-slean-part='next'])::before {
		mask-image: var(--slean-icon-chevron-down);
	}

	:where(
		[data-slean='sortable'][data-slean-orientation='horizontal'] [data-slean-part='previous']
	)::before {
		mask-image: var(--slean-icon-chevron-left);
	}

	:where(
		[data-slean='sortable'][data-slean-orientation='horizontal'] [data-slean-part='next']
	)::before {
		mask-image: var(--slean-icon-chevron-right);
	}

	/* A row flowing right to left points its buttons the other way. */
	:where(
		[data-slean='sortable'][data-slean-orientation='horizontal']
			[data-slean-part='previous']:dir(rtl)
	)::before,
	:where(
		[data-slean='sortable'][data-slean-orientation='horizontal'] [data-slean-part='next']:dir(rtl)
	)::before {
		scale: -1 1;
	}

	@media (hover: hover) {
		:where(
			[data-slean='sortable']
				[data-slean-part='handle']:not(:disabled, [aria-disabled='true']):hover
		),
		:where(
			[data-slean='sortable']
				[data-slean-part='previous']:not(:disabled, [aria-disabled='true']):hover
		),
		:where(
			[data-slean='sortable'] [data-slean-part='next']:not(:disabled, [aria-disabled='true']):hover
		) {
			background: var(--slean-muted-hover);
			color: var(--slean-fg);
		}
	}

	:where([data-slean='sortable'] [data-slean-part='handle']:focus-visible),
	:where([data-slean='sortable'] [data-slean-part='previous']:focus-visible),
	:where([data-slean='sortable'] [data-slean-part='next']:focus-visible) {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
		color: var(--slean-color-main);
	}

	:where([data-slean='sortable'] [data-slean-part='handle']:disabled),
	:where([data-slean='sortable'] [data-slean-part='handle'][aria-disabled='true']),
	:where([data-slean='sortable'] [data-slean-part='previous']:disabled),
	:where([data-slean='sortable'] [data-slean-part='previous'][aria-disabled='true']),
	:where([data-slean='sortable'] [data-slean-part='next']:disabled),
	:where([data-slean='sortable'] [data-slean-part='next'][aria-disabled='true']) {
		cursor: default;
		opacity: 0.4;
	}

	/* During a drag: the other items slide out of the way, the lifted one is raised above them. */
	:where([data-slean='sortable'][data-slean-dragging]) {
		cursor: grabbing;
		user-select: none;
		-webkit-user-select: none;
	}

	:where([data-slean='sortable'][data-slean-dragging] [data-slean-part='handle']) {
		cursor: grabbing;
	}

	:where([data-slean='sortable'][data-slean-dragging] > [data-slean-value]) {
		transition: transform var(--slean-duration-normal) var(--slean-ease);
	}

	:where([data-slean='sortable'][data-slean-settling] > [data-slean-value]) {
		transition: none;
	}

	:where([data-slean='sortable'] > [data-slean-value][data-slean-lifted]) {
		position: relative;
		z-index: 1;
		border-color: var(--slean-color-main);
		box-shadow: var(--slean-elevation-3);
	}

	@media (pointer: coarse) {
		:where([data-slean='sortable'] [data-slean-part='handle']),
		:where([data-slean='sortable'] [data-slean-part='previous']),
		:where([data-slean='sortable'] [data-slean-part='next']) {
			inline-size: var(--slean-control-height-sm);
			block-size: var(--slean-control-height-sm);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='sortable'] [data-slean-part='handle'])::before,
		:where([data-slean='sortable'] [data-slean-part='previous'])::before,
		:where([data-slean='sortable'] [data-slean-part='next'])::before {
			background: ButtonText;
		}

		:where([data-slean='sortable'] > [data-slean-value][data-slean-lifted]) {
			border-color: Highlight;
		}
	}
}
