/* @svelte-lean/styles — radio group (Tier 0, native <input type="radio"> sharing a name).
 * Markup: <fieldset data-slean="radio-group"><legend>…</legend>
 *   <label><input type="radio" name="…"> Label</label> …</fieldset>.
 * The fieldset is unstyled apart from its spacing; the radios draw a round mark with borders and
 * ::before, and ::after enlarges the pointer target on coarse pointers. State is the native
 * :checked / :disabled / :user-invalid; the browser owns selection and arrow keys. Option:
 * data-slean-orientation="horizontal" puts the options on one wrapping row.
 * data-variant="segmented" (ADR 0011 §1.3) draws the same radios as the segments of one bar (Ant
 * Segmented, MUI exclusive ToggleButtonGroup): the labels form the bar, one equal-width column
 * each; each radio covers its label, transparent and focusable, so a click anywhere on a segment
 * checks it. The checked segment is label:has(:checked), the keyboard ring label:has(:focus-visible).
 * data-variant="swatch" draws the radios as a grid of color squares, each label's color in
 * --slean-swatch (style="--slean-swatch:#ef4444" on the label), the radio covering it and drawing
 * a check mark (contrast-color() of the swatch where supported, else on a surface disc) and the
 * checked square ringed in the text color; arrow keys, the single tab stop and the form value stay
 * native. Each swatch radio needs an aria-label (the color's name).
 * Looks: data-color on the group or an ancestor (primary by default); data-size="small|large";
 * data-full-width (a segmented bar fills its container). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='radio-group']) {
		display: flex;
		flex-direction: column;
		gap: var(--slean-space-2);
		min-inline-size: 0;
		margin: 0;
		padding: 0;
		border: 0;
	}

	:where([data-slean='radio-group'][data-slean-orientation='horizontal']) {
		flex-direction: row;
		flex-wrap: wrap;
		column-gap: var(--slean-space-4);
	}

	:where([data-slean='radio-group'][data-slean-orientation='horizontal'] legend) {
		inline-size: 100%;
	}

	:where([data-slean='radio-group'] legend) {
		padding: 0;
		margin-block-end: var(--slean-space-1);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		font-weight: var(--slean-font-weight-medium);
	}

	:where([data-slean='radio-group'] label) {
		display: inline-flex;
		align-items: center;
		gap: var(--slean-space-2);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		cursor: pointer;
	}

	/* The radio mark (every look but the segmented bar). */
	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']
	) {
		appearance: none;
		position: relative;
		display: inline-grid;
		place-content: center;
		flex: none;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		margin: 0;
		border: 1px solid var(--slean-control-border);
		border-radius: var(--slean-radius-full);
		background: var(--slean-control-bg);
		color: var(--slean-color-contrast-text);
		vertical-align: middle;
		cursor: pointer;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			border-color var(--slean-duration-fast) var(--slean-ease);
	}

	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']
	)::before {
		content: '';
		inline-size: 0.5rem;
		block-size: 0.5rem;
		border-radius: var(--slean-radius-full);
		background: currentColor;
		opacity: 0;
		transition: opacity var(--slean-duration-fast) var(--slean-ease);
	}

	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']:checked
	) {
		border-color: var(--slean-color-main);
		background: var(--slean-color-main);
	}

	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']:checked
	)::before {
		opacity: 1;
	}

	:where(
		[data-slean='radio-group']:not(
				[data-variant='segmented'],
				[data-variant='swatch']
			)[data-size='small']
			input[type='radio']
	) {
		inline-size: 1rem;
		block-size: 1rem;
	}

	:where(
		[data-slean='radio-group']:not(
				[data-variant='segmented'],
				[data-variant='swatch']
			)[data-size='small']
			input[type='radio']
	)::before {
		inline-size: 0.4375rem;
		block-size: 0.4375rem;
	}

	:where(
		[data-slean='radio-group']:not(
				[data-variant='segmented'],
				[data-variant='swatch']
			)[data-size='large']
			input[type='radio']
	) {
		inline-size: 1.375rem;
		block-size: 1.375rem;
	}

	:where(
		[data-slean='radio-group']:not(
				[data-variant='segmented'],
				[data-variant='swatch']
			)[data-size='large']
			input[type='radio']
	)::before {
		inline-size: 0.625rem;
		block-size: 0.625rem;
	}

	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']:user-invalid
	),
	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio'][aria-invalid='true']
	) {
		border-color: var(--slean-error-main);
	}

	/* Disabled: the muted surface of every disabled control; a checked radio keeps its dot in the
	 * muted text color. */
	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			input[type='radio']:disabled
	),
	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch']):disabled
			input[type='radio']
	) {
		border-color: var(--slean-control-border-disabled);
		background: var(--slean-control-bg-disabled);
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
			label:has(input[type='radio']:disabled)
	),
	:where(
		[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch']):disabled
			label
	) {
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	@media (hover: hover) {
		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				label:hover
				> input[type='radio']:not(:disabled, :checked)
		) {
			border-color: var(--slean-color-main);
		}
	}

	/* data-variant="segmented": one bar of equal segments. */
	:where([data-slean='radio-group'][data-variant='segmented']) {
		display: inline-grid;
		grid-auto-flow: column;
		grid-auto-columns: 1fr;
		gap: 0;
	}

	:where([data-slean='radio-group'][data-variant='segmented'][data-full-width]) {
		display: grid;
		inline-size: 100%;
	}

	:where([data-slean='radio-group'][data-variant='segmented'] legend) {
		margin-block-end: var(--slean-space-2);
	}

	:where([data-slean='radio-group'][data-variant='segmented'] label) {
		position: relative;
		isolation: isolate;
		justify-content: center;
		min-block-size: var(--slean-control-height-md);
		padding-block: 0;
		padding-inline: var(--slean-space-4);
		background: var(--slean-muted);
		color: var(--slean-fg-muted);
		font-weight: var(--slean-font-weight-medium);
		line-height: 1.25;
		white-space: nowrap;
		user-select: none;
		-webkit-tap-highlight-color: transparent;
		transition: color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='radio-group'][data-variant='segmented'] label:first-of-type) {
		border-start-start-radius: var(--slean-radius-md);
		border-end-start-radius: var(--slean-radius-md);
	}

	:where([data-slean='radio-group'][data-variant='segmented'] label:last-of-type) {
		border-start-end-radius: var(--slean-radius-md);
		border-end-end-radius: var(--slean-radius-md);
	}

	:where([data-slean='radio-group'][data-variant='segmented'][data-size='small'] label) {
		min-block-size: var(--slean-control-height-sm);
		padding-inline: var(--slean-space-3);
	}

	:where([data-slean='radio-group'][data-variant='segmented'][data-size='large'] label) {
		min-block-size: var(--slean-control-height-lg);
		padding-inline: var(--slean-space-5);
		font-size: var(--slean-text-md);
	}

	/* The raised surface of the checked segment, inset in the bar and behind the text. */
	:where([data-slean='radio-group'][data-variant='segmented'] label)::before {
		content: '';
		position: absolute;
		inset: 0.1875rem;
		z-index: -1;
		border-radius: var(--slean-radius-sm);
		background: transparent;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	/* The radio covers the segment: transparent, focusable, and the target of every click. */
	:where([data-slean='radio-group'][data-variant='segmented'] input[type='radio']) {
		appearance: none;
		position: absolute;
		inset: 0;
		margin: 0;
		opacity: 0;
		cursor: inherit;
	}

	/* The checked segment: the raised surface, its text in the group's color. */
	:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:checked)) {
		color: var(--slean-color-main);
	}

	:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:checked))::before {
		background: var(--slean-surface);
		box-shadow:
			0 0 0 1px var(--slean-border),
			var(--slean-elevation-1);
	}

	:where(
		[data-slean='radio-group'][data-variant='segmented'] label:has(input:focus-visible)
	)::before {
		outline: var(--slean-focus-ring);
		outline-offset: 0;
	}

	@media (hover: hover) {
		:where(
			[data-slean='radio-group'][data-variant='segmented']:not(:disabled)
				label:hover:not(:has(input:disabled, input:checked))
		) {
			color: var(--slean-fg);
		}
	}

	:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:disabled)),
	:where([data-slean='radio-group'][data-variant='segmented']:disabled label) {
		opacity: 0.5;
		cursor: not-allowed;
	}

	/* data-variant="swatch": a grid of color squares. Each label carries its color in
	 * --slean-swatch (an inline style); its radio covers it, transparent and focusable, and draws
	 * the check mark on ::before. The check takes contrast-color() of the swatch where the browser
	 * has it; elsewhere it sits on a disc of the surface color, readable on any swatch. The checked
	 * swatch also gets a ring in the text color, so the choice does not rest on the check alone. */
	:where([data-slean='radio-group'][data-variant='swatch']) {
		display: grid;
		grid-template-columns: repeat(auto-fill, var(--slean-control-height-xs));
		gap: var(--slean-space-2);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] legend) {
		margin-block-end: var(--slean-space-2);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] label) {
		position: relative;
		display: block;
		inline-size: var(--slean-control-height-xs);
		block-size: var(--slean-control-height-xs);
		border-radius: var(--slean-radius-sm);
		background: var(--slean-swatch);
		box-shadow: inset 0 0 0 1px color-mix(in oklch, currentColor 16%, transparent);
		-webkit-tap-highlight-color: transparent;
		transition:
			scale var(--slean-duration-fast) var(--slean-ease),
			box-shadow var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='radio-group'][data-variant='swatch'][data-size='small']) {
		grid-template-columns: repeat(auto-fill, 1.5rem);
	}

	:where([data-slean='radio-group'][data-variant='swatch'][data-size='small'] label) {
		inline-size: 1.5rem;
		block-size: 1.5rem;
	}

	:where([data-slean='radio-group'][data-variant='swatch'][data-size='large']) {
		grid-template-columns: repeat(auto-fill, var(--slean-control-height-md));
	}

	:where([data-slean='radio-group'][data-variant='swatch'][data-size='large'] label) {
		inline-size: var(--slean-control-height-md);
		block-size: var(--slean-control-height-md);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']) {
		appearance: none;
		position: absolute;
		inset: 0;
		display: grid;
		place-content: center;
		margin: 0;
		border: 0;
		border-radius: inherit;
		background: transparent;
		color: var(--slean-fg);
		cursor: pointer;
	}

	:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio'])::before {
		content: '';
		inline-size: 1rem;
		block-size: 1rem;
		background: currentColor;
		mask: var(--slean-icon-check) center / contain no-repeat;
		opacity: 0;
		transition: opacity var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']:checked) {
		background: radial-gradient(
			circle,
			var(--slean-surface) 0.625rem,
			transparent calc(0.625rem + 0.5px)
		);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']:checked)::before {
		opacity: 1;
	}

	@supports (color: contrast-color(transparent)) {
		:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']) {
			color: contrast-color(var(--slean-swatch));
		}

		:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']:checked) {
			background: transparent;
		}
	}

	:where([data-slean='radio-group'][data-variant='swatch'] label:has(input:checked)) {
		box-shadow:
			inset 0 0 0 1px color-mix(in oklch, currentColor 16%, transparent),
			0 0 0 2px var(--slean-surface),
			0 0 0 4px var(--slean-fg);
	}

	:where([data-slean='radio-group'][data-variant='swatch'] label:has(input:focus-visible)) {
		outline: var(--slean-focus-ring);
		outline-offset: var(--slean-focus-ring-offset);
	}

	@media (hover: hover) {
		:where(
			[data-slean='radio-group'][data-variant='swatch']:not(:disabled)
				label:hover:not(:has(input:disabled))
		) {
			scale: 1.1;
		}
	}

	:where([data-slean='radio-group'][data-variant='swatch'] label:has(input:disabled)),
	:where([data-slean='radio-group'][data-variant='swatch']:disabled label) {
		opacity: 0.4;
		cursor: not-allowed;
	}

	:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']:disabled),
	:where([data-slean='radio-group'][data-variant='swatch']:disabled input[type='radio']) {
		cursor: not-allowed;
	}

	@media (pointer: coarse) {
		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				input[type='radio']
		)::after {
			content: '';
			position: absolute;
			inset: calc((var(--slean-control-height-md) - 100%) / -2);
		}
	}

	@media (forced-colors: active) {
		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				input[type='radio']
		) {
			forced-color-adjust: none;
			border-color: ButtonText;
			background: Canvas;
			color: Highlight;
		}

		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				input[type='radio']:checked
		) {
			border-color: Highlight;
			background: Canvas;
		}

		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				input[type='radio']:disabled
		),
		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch']):disabled
				input[type='radio']
		) {
			border-color: GrayText;
			background: Canvas;
			color: GrayText;
		}

		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch'])
				label:has(input[type='radio']:disabled)
		),
		:where(
			[data-slean='radio-group']:not([data-variant='segmented'], [data-variant='swatch']):disabled
				label
		) {
			color: GrayText;
		}

		:where([data-slean='radio-group'][data-variant='segmented'] label) {
			forced-color-adjust: none;
			background: Canvas;
			color: ButtonText;
			box-shadow: inset 0 0 0 1px ButtonText;
		}

		:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:checked)) {
			color: HighlightText;
		}

		:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:checked))::before {
			background: Highlight;
			box-shadow: none;
		}

		/* The ring is Highlight, like the checked fill: around the checked segment it takes the
		 * text color. */
		:where(
			[data-slean='radio-group'][data-variant='segmented'] label:has(input:checked:focus-visible)
		)::before {
			outline-color: CanvasText;
		}

		:where([data-slean='radio-group'][data-variant='segmented'] label:has(input:disabled)),
		:where([data-slean='radio-group'][data-variant='segmented']:disabled label) {
			color: GrayText;
			box-shadow: inset 0 0 0 1px GrayText;
			opacity: 1;
		}

		/* A swatch keeps its color: it is the content, not decoration. */
		:where([data-slean='radio-group'][data-variant='swatch'] label) {
			forced-color-adjust: none;
			box-shadow: inset 0 0 0 1px CanvasText;
		}

		:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']) {
			forced-color-adjust: none;
			color: CanvasText;
		}

		:where([data-slean='radio-group'][data-variant='swatch'] input[type='radio']:checked) {
			background: radial-gradient(circle, Canvas 0.625rem, transparent calc(0.625rem + 0.5px));
		}

		:where([data-slean='radio-group'][data-variant='swatch'] label:has(input:checked)) {
			box-shadow:
				inset 0 0 0 1px CanvasText,
				0 0 0 2px Canvas,
				0 0 0 4px Highlight;
		}
	}
}
