/* @svelte-lean/styles — button (Tier 0).
 * Markup: <button type="button" data-slean="button" data-variant="outlined" data-color="error"
 * data-size="small">. Variant (data-variant, MUI's names): contained (default), outlined, text.
 * Color (data-color, on the button or an ancestor): primary (default), secondary, error, warning,
 * info, success, neutral; every variant is drawn from the current color tokens. Sizes
 * (data-size): small, medium (default), large. Modifiers: data-full-width (fills its container),
 * data-icon-only (square), aria-busy="true" (loading; the spinner keeps turning, slower, under
 * reduced motion), :disabled / aria-disabled (the muted surface every disabled control shares).
 * Parts: start-icon and end-icon hold an icon before or after the label (MUI startIcon/endIcon);
 * an <svg> child without a part is 1rem (base.css). Theme defaults: --slean-button-radius,
 * --slean-button-font, --slean-button-transform. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	/* Contained, the default: filled with the current color. */
	:where([data-slean='button']) {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		gap: var(--slean-space-2);
		min-block-size: var(--slean-control-height-md);
		padding-block: var(--slean-space-1);
		padding-inline: var(--slean-space-4);
		border: 1px solid transparent;
		border-radius: var(--slean-button-radius);
		background: var(--slean-color-main);
		color: var(--slean-color-contrast-text);
		/* The weight, size and line height come from the theme; the family stays the page's. */
		font: var(--slean-button-font);
		font-family: inherit;
		font-variant-numeric: tabular-nums;
		text-transform: var(--slean-button-transform);
		text-decoration: none;
		white-space: nowrap;
		cursor: pointer;
		user-select: none;
		-webkit-tap-highlight-color: transparent;
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			border-color var(--slean-duration-fast) var(--slean-ease),
			color var(--slean-duration-fast) var(--slean-ease);
	}

	/* Outlined: a line and the text in the current color, no fill. Neutral is MUI's "inherit":
	 * the text color and the border hairline (--slean-color-text, --slean-color-border). */
	:where([data-slean='button'][data-variant='outlined']) {
		border-color: var(--slean-color-border);
		background: transparent;
		color: var(--slean-color-text);
	}

	/* Text: the label in the current color, no line and no fill until hovered. */
	:where([data-slean='button'][data-variant='text']) {
		background: transparent;
		color: var(--slean-color-text);
	}

	/* Sizes. */
	:where([data-slean='button'][data-size='small']) {
		min-block-size: var(--slean-control-height-sm);
		padding-inline: var(--slean-space-3);
		gap: var(--slean-space-1);
	}

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

	:where([data-slean='button'][data-full-width]) {
		inline-size: 100%;
	}

	:where([data-slean='button'][data-icon-only]) {
		inline-size: var(--slean-control-height-md);
		padding-inline: 0;
	}

	:where([data-slean='button'][data-icon-only][data-size='small']) {
		inline-size: var(--slean-control-height-sm);
	}

	:where([data-slean='button'][data-icon-only][data-size='large']) {
		inline-size: var(--slean-control-height-lg);
	}

	/* Icon parts: the icon sits half a step closer to the edge than the label, as in MUI. */
	:where([data-slean='button'] [data-slean-part='start-icon']),
	:where([data-slean='button'] [data-slean-part='end-icon']) {
		display: inline-flex;
		flex: none;
		font-size: 1.25rem;
	}

	:where([data-slean='button'] [data-slean-part='start-icon']) {
		margin-inline-start: calc(var(--slean-space-1) * -1);
	}

	:where([data-slean='button'] [data-slean-part='end-icon']) {
		margin-inline-end: calc(var(--slean-space-1) * -1);
	}

	:where(
		[data-slean='button'] :is([data-slean-part='start-icon'], [data-slean-part='end-icon']) > svg
	) {
		inline-size: 1em;
		block-size: 1em;
	}

	:where([data-slean='button'][data-size='small'] [data-slean-part='start-icon']),
	:where([data-slean='button'][data-size='small'] [data-slean-part='end-icon']) {
		font-size: 1.125rem;
	}

	:where([data-slean='button'][data-size='large'] [data-slean-part='start-icon']),
	:where([data-slean='button'][data-size='large'] [data-slean-part='end-icon']) {
		font-size: 1.375rem;
	}

	/* Pointer states. Hover is limited to devices that have hover. */
	@media (hover: hover) {
		:where([data-slean='button']:hover:not(:disabled, [aria-disabled='true'], [aria-busy='true'])) {
			background: var(--slean-color-dark);
		}

		:where(
			[data-slean='button'][data-variant='outlined']:hover:not(
					:disabled,
					[aria-disabled='true'],
					[aria-busy='true']
				)
		),
		:where(
			[data-slean='button'][data-variant='text']:hover:not(
					:disabled,
					[aria-disabled='true'],
					[aria-busy='true']
				)
		) {
			background: color-mix(in oklch, var(--slean-color-main) 8%, transparent);
		}

		:where(
			[data-slean='button'][data-variant='outlined']:hover:not(
					:disabled,
					[aria-disabled='true'],
					[aria-busy='true']
				)
		) {
			border-color: var(--slean-color-main);
		}
	}

	:where([data-slean='button']:active:not(:disabled, [aria-disabled='true'], [aria-busy='true'])) {
		background: color-mix(in oklch, var(--slean-color-dark), var(--slean-fg) 12%);
	}

	:where(
		[data-slean='button'][data-variant='outlined']:active:not(
				:disabled,
				[aria-disabled='true'],
				[aria-busy='true']
			)
	),
	:where(
		[data-slean='button'][data-variant='text']:active:not(
				:disabled,
				[aria-disabled='true'],
				[aria-busy='true']
			)
	) {
		background: color-mix(in oklch, var(--slean-color-main) 16%, transparent);
	}

	/* Disabled: native :disabled and focusable aria-disabled look the same, the muted surface of
	 * every disabled control. Text buttons keep no surface, only the muted label. */
	:where([data-slean='button']:disabled),
	:where([data-slean='button'][aria-disabled='true']) {
		border-color: var(--slean-control-border-disabled);
		background: var(--slean-control-bg-disabled);
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	:where([data-slean='button'][data-variant='text']:disabled),
	:where([data-slean='button'][data-variant='text'][aria-disabled='true']) {
		border-color: transparent;
		background: transparent;
	}

	/* Loading: aria-busy="true" adds a spinner before the label and blocks pointer feedback. */
	:where([data-slean='button'][aria-busy='true']) {
		cursor: progress;
	}

	:where([data-slean='button'][aria-busy='true'])::before {
		content: '';
		flex: none;
		inline-size: 1em;
		block-size: 1em;
		border: 2px solid currentColor;
		border-inline-end-color: transparent;
		border-radius: 50%;
		animation: slean-spin var(--slean-duration-indicator) linear infinite;
	}

	@keyframes slean-spin {
		to {
			rotate: 1turn;
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='button']) {
			border-color: ButtonText;
		}

		:where([data-slean='button']:disabled),
		:where([data-slean='button'][aria-disabled='true']) {
			border-color: GrayText;
			color: GrayText;
		}
	}
}
