/* @svelte-lean/styles — slider (ADR 0011 §1.12). Two forms share the thumb:
 * - Bare (Tier 0): <input type="range" data-slean="slider" min="0" max="100" step="1" value="40">.
 * - Group (Tier 1, @svelte-lean/primitives/slider/register): <div role="group" data-slean="slider">
 *   around one or two <input type="range"> (two: the range mode). The inputs are stacked on the
 *   root, which draws the rail and the filled segment from --slean-range-start and
 *   --slean-range-end (written by the behavior, or rangeStyle() on the server; 0 and 100 until
 *   then); with two inputs only the thumbs take the pointer.
 * Bare slider:
 * The track and the thumb are drawn from the tokens through ::-webkit-slider-runnable-track and
 * ::-webkit-slider-thumb (Chrome, Safari) and ::-moz-range-track and ::-moz-range-thumb
 * (Firefox). Only Firefox draws the filled part of the track without script
 * (::-moz-range-progress). data-variant="native" keeps the platform's own drawing, colored with
 * accent-color: a filled track in Chrome and Firefox, and the tick marks of a <datalist> named by
 * the list attribute. Option: data-slean-orientation="vertical" (writing-mode: vertical-lr;
 * direction: rtl, so the minimum is at the bottom). State is native: :disabled, :focus-visible.
 * Color: data-color on the slider or an ancestor (primary by default). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where(input[data-slean='slider']) {
		appearance: none;
		display: inline-block;
		inline-size: 100%;
		min-inline-size: 6rem;
		block-size: var(--slean-control-height-sm);
		margin: 0;
		padding: 0;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		accent-color: var(--slean-color-main);
		vertical-align: middle;
		cursor: pointer;
		-webkit-tap-highlight-color: transparent;
	}

	:where(input[data-slean='slider']:not([data-variant='native']))::-webkit-slider-runnable-track {
		block-size: 0.25rem;
		border-radius: var(--slean-radius-full);
		background: var(--slean-border);
	}

	:where(input[data-slean='slider']:not([data-variant='native']))::-webkit-slider-thumb {
		appearance: none;
		box-sizing: border-box;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		margin-block-start: calc((0.25rem - 1.125rem) / 2);
		border: 2px solid var(--slean-surface);
		border-radius: 50%;
		background: var(--slean-color-main);
		box-shadow: var(--slean-elevation-1);
		transition: box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-track {
		block-size: 0.25rem;
		border-radius: var(--slean-radius-full);
		background: var(--slean-border);
	}

	:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-progress {
		block-size: 0.25rem;
		border-radius: var(--slean-radius-full);
		background: var(--slean-color-main);
	}

	:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-thumb {
		box-sizing: border-box;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		border: 2px solid var(--slean-surface);
		border-radius: 50%;
		background: var(--slean-color-main);
		box-shadow: var(--slean-elevation-1);
		transition: box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	/* The platform's drawing, colored by accent-color. */
	:where(input[data-slean='slider'][data-variant='native']) {
		appearance: auto;
	}

	@media (hover: hover) {
		:where(
			input[data-slean='slider']:not(:disabled, [data-variant='native']):hover
		)::-webkit-slider-thumb {
			box-shadow:
				var(--slean-elevation-1),
				0 0 0 0.3125rem var(--slean-color-soft);
		}

		:where(
			input[data-slean='slider']:not(:disabled, [data-variant='native']):hover
		)::-moz-range-thumb {
			box-shadow:
				var(--slean-elevation-1),
				0 0 0 0.3125rem var(--slean-color-soft);
		}
	}

	/* Vertical: the inline axis runs from bottom to top, so the minimum is at the bottom. */
	:where(input[data-slean='slider'][data-slean-orientation='vertical']) {
		writing-mode: vertical-lr;
		direction: rtl;
		inline-size: 10rem;
		min-inline-size: 0;
		vertical-align: bottom;
	}

	:where(input[data-slean='slider']:disabled) {
		opacity: 0.5;
		cursor: not-allowed;
	}

	/* The group: the root is the track. The rail runs between the thumb centres, so the
	 * percentages land under the thumbs; the filled segment is the current color. */
	:where(div[data-slean='slider']) {
		position: relative;
		isolation: isolate;
		display: block;
		min-inline-size: 8rem;
		block-size: var(--slean-control-height-sm);
		accent-color: var(--slean-color-main);
	}

	:where(div[data-slean='slider'])::before {
		content: '';
		position: absolute;
		inset-inline: 0.5625rem;
		inset-block-start: 50%;
		block-size: 0.25rem;
		margin-block-start: -0.125rem;
		border-radius: var(--slean-radius-full);
		background: linear-gradient(
			to right,
			var(--slean-border) calc(var(--slean-range-start, 0) * 1%),
			var(--slean-color-main) calc(var(--slean-range-start, 0) * 1%)
				calc(var(--slean-range-end, 100) * 1%),
			var(--slean-border) calc(var(--slean-range-end, 100) * 1%)
		);
	}

	:where(div[data-slean='slider']:dir(rtl))::before {
		background: linear-gradient(
			to left,
			var(--slean-border) calc(var(--slean-range-start, 0) * 1%),
			var(--slean-color-main) calc(var(--slean-range-start, 0) * 1%)
				calc(var(--slean-range-end, 100) * 1%),
			var(--slean-border) calc(var(--slean-range-end, 100) * 1%)
		);
	}

	:where(div[data-slean='slider'] > input[type='range']) {
		appearance: none;
		position: absolute;
		inset: 0;
		inline-size: 100%;
		block-size: 100%;
		margin: 0;
		padding: 0;
		background: none;
		cursor: pointer;
		-webkit-tap-highlight-color: transparent;
	}

	/* Two thumbs: only the thumbs take the pointer, so the lower input does not block the upper. */
	:where(div[data-slean='slider']:has(> input + input) > input[type='range']) {
		pointer-events: none;
	}

	/* Where both thumbs meet past the middle, the start thumb is on top, so it can still move
	 * back; below the middle the end thumb is. Without round() the end thumb stays on top. */
	:where(div[data-slean='slider'] > input[type='range']:first-of-type) {
		z-index: round(down, var(--slean-range-start, 0) / 50);
	}

	:where(div[data-slean='slider'] > input[type='range']:focus-visible) {
		outline-style: none;
	}

	:where(div[data-slean='slider'] > input[type='range'])::-webkit-slider-runnable-track {
		background: none;
	}

	:where(div[data-slean='slider'] > input[type='range'])::-moz-range-track {
		background: none;
	}

	:where(div[data-slean='slider'] > input[type='range'])::-webkit-slider-thumb {
		appearance: none;
		box-sizing: border-box;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		border: 2px solid var(--slean-surface);
		border-radius: 50%;
		background: var(--slean-color-main);
		box-shadow: var(--slean-elevation-1);
		pointer-events: auto;
		cursor: grab;
		transition: box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	:where(div[data-slean='slider'] > input[type='range'])::-moz-range-thumb {
		box-sizing: border-box;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		border: 2px solid var(--slean-surface);
		border-radius: 50%;
		background: var(--slean-color-main);
		box-shadow: var(--slean-elevation-1);
		pointer-events: auto;
		cursor: grab;
		transition: box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	/* The focus ring sits on the focused thumb. */
	:where(div[data-slean='slider'] > input[type='range']:focus-visible)::-webkit-slider-thumb {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
	}

	:where(div[data-slean='slider'] > input[type='range']:focus-visible)::-moz-range-thumb {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
	}

	@media (hover: hover) {
		:where(
			div[data-slean='slider'] > input[type='range']:not(:disabled):hover
		)::-webkit-slider-thumb {
			box-shadow:
				var(--slean-elevation-1),
				0 0 0 0.3125rem var(--slean-color-soft);
		}

		:where(div[data-slean='slider'] > input[type='range']:not(:disabled):hover)::-moz-range-thumb {
			box-shadow:
				var(--slean-elevation-1),
				0 0 0 0.3125rem var(--slean-color-soft);
		}
	}

	/* Disabled: the whole track (the rail, the fill and the thumbs) is dimmed. */
	:where(div[data-slean='slider']:has(> :disabled)) {
		opacity: 0.5;
	}

	:where(div[data-slean='slider'] > input[type='range']:disabled)::-webkit-slider-thumb {
		cursor: not-allowed;
	}

	:where(div[data-slean='slider'] > input[type='range']:disabled)::-moz-range-thumb {
		cursor: not-allowed;
	}

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

		:where(input[data-slean='slider']:not([data-variant='native']))::-webkit-slider-thumb {
			inline-size: 1.5rem;
			block-size: 1.5rem;
			margin-block-start: calc((0.25rem - 1.5rem) / 2);
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-thumb {
			inline-size: 1.5rem;
			block-size: 1.5rem;
		}

		:where(div[data-slean='slider']) {
			block-size: var(--slean-control-height-md);
		}

		:where(div[data-slean='slider'])::before {
			inset-inline: 0.75rem;
		}

		:where(div[data-slean='slider'] > input[type='range'])::-webkit-slider-thumb {
			inline-size: 1.5rem;
			block-size: 1.5rem;
		}

		:where(div[data-slean='slider'] > input[type='range'])::-moz-range-thumb {
			inline-size: 1.5rem;
			block-size: 1.5rem;
		}
	}

	@media (forced-colors: active) {
		:where(input[data-slean='slider']:not([data-variant='native'])) {
			forced-color-adjust: none;
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-webkit-slider-runnable-track {
			background: CanvasText;
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-webkit-slider-thumb {
			border-color: Canvas;
			background: Highlight;
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-track {
			background: CanvasText;
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-progress {
			background: Highlight;
		}

		:where(input[data-slean='slider']:not([data-variant='native']))::-moz-range-thumb {
			border-color: Canvas;
			background: Highlight;
		}

		:where(input[data-slean='slider']:disabled) {
			opacity: 1;
		}

		:where(input[data-slean='slider']:disabled)::-webkit-slider-thumb {
			background: GrayText;
		}

		:where(input[data-slean='slider']:disabled)::-moz-range-thumb {
			background: GrayText;
		}

		:where(input[data-slean='slider']:disabled)::-moz-range-progress {
			background: GrayText;
		}

		:where(div[data-slean='slider'])::before {
			forced-color-adjust: none;
			background: CanvasText;
		}

		:where(div[data-slean='slider'] > input[type='range'])::-webkit-slider-thumb {
			forced-color-adjust: none;
			border-color: Canvas;
			background: Highlight;
		}

		:where(div[data-slean='slider'] > input[type='range'])::-moz-range-thumb {
			forced-color-adjust: none;
			border-color: Canvas;
			background: Highlight;
		}

		:where(div[data-slean='slider']:has(> :disabled)) {
			opacity: 1;
		}

		:where(div[data-slean='slider'] > input[type='range']:disabled)::-webkit-slider-thumb {
			background: GrayText;
		}

		:where(div[data-slean='slider'] > input[type='range']:disabled)::-moz-range-thumb {
			background: GrayText;
		}
	}
}
