/* @svelte-lean/styles — toggle (Tier 1; the click behavior comes from @svelte-lean/primitives/toggle).
 * Markup: <button type="button" data-slean="toggle" aria-pressed="false">Bold</button>.
 * State is aria-pressed ("true", "false", "mixed"); :disabled and aria-disabled look the same.
 * Variant (data-variant): contained (default; pressed is filled with the current color),
 * outlined (pressed is the soft tint with a line in the color), text (no line; pressed is the
 * soft tint). Color (data-color): the pressed color, primary by default. Sizes (data-size):
 * small, medium (default), large. Modifiers: data-icon-only (square, name from aria-label).
 * Parts: start-icon and end-icon, as on the button. */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='toggle']) {
		appearance: none;
		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-3);
		border: 1px solid var(--slean-border);
		border-radius: var(--slean-button-radius);
		background: transparent;
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		font-weight: var(--slean-font-weight-medium);
		line-height: 1.25;
		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);
	}

	:where([data-slean='toggle'][data-variant='text']) {
		border-color: transparent;
	}

	:where([data-slean='toggle'][data-size='small']) {
		min-block-size: var(--slean-control-height-sm);
		padding-inline: var(--slean-space-2);
	}

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

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

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

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

	:where([data-slean='toggle'] [data-slean-part='start-icon']),
	:where([data-slean='toggle'] [data-slean-part='end-icon']) {
		display: inline-flex;
		flex: none;
		font-size: 1.25rem;
	}

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

	/* Pressed, contained: filled with the current color. */
	:where([data-slean='toggle'][aria-pressed='true']) {
		border-color: var(--slean-color-main);
		background: var(--slean-color-main);
		color: var(--slean-color-contrast-text);
	}

	/* Pressed, outlined and text: the soft tint of the current color, outlined with a line in a
	 * tint of it, so the state does not rely on the fill alone and does not read as a focus ring. */
	:where([data-slean='toggle'][data-variant='outlined'][aria-pressed='true']),
	:where([data-slean='toggle'][data-variant='text'][aria-pressed='true']) {
		background: var(--slean-color-soft);
		color: var(--slean-color-soft-text);
	}

	/* The line is a tint of the main tone, for neutral too: the hairline of --slean-color-border
	 * is the unpressed border, and the state would rest on the fill alone. */
	:where([data-slean='toggle'][data-variant='outlined'][aria-pressed='true']) {
		border-color: color-mix(in oklch, var(--slean-color-main) 50%, transparent);
	}

	:where([data-slean='toggle'][data-variant='text'][aria-pressed='true']) {
		border-color: transparent;
	}

	/* Mixed: a dashed line on the muted fill. */
	:where([data-slean='toggle'][aria-pressed='mixed']) {
		border-style: dashed;
		background: var(--slean-muted);
	}

	@media (hover: hover) {
		:where([data-slean='toggle']:hover:not(:disabled, [aria-disabled='true'])) {
			background: var(--slean-muted);
		}

		:where(
			[data-slean='toggle'][aria-pressed='true']:hover:not(:disabled, [aria-disabled='true'])
		) {
			background: var(--slean-color-dark);
		}

		:where(
			[data-slean='toggle'][data-variant='outlined'][aria-pressed='true']:hover:not(
					:disabled,
					[aria-disabled='true']
				)
		),
		:where(
			[data-slean='toggle'][data-variant='text'][aria-pressed='true']:hover:not(
					:disabled,
					[aria-disabled='true']
				)
		) {
			background: color-mix(in oklch, var(--slean-color-soft), var(--slean-color-main) 10%);
		}
	}

	:where([data-slean='toggle']:active:not(:disabled, [aria-disabled='true'])) {
		background: var(--slean-muted-hover);
	}

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

	:where(
		[data-slean='toggle'][data-variant='outlined'][aria-pressed='true']:active:not(
				:disabled,
				[aria-disabled='true']
			)
	),
	:where(
		[data-slean='toggle'][data-variant='text'][aria-pressed='true']:active:not(
				:disabled,
				[aria-disabled='true']
			)
	) {
		background: color-mix(in oklch, var(--slean-color-soft), var(--slean-color-main) 18%);
	}

	/* Disabled: the muted surface every disabled control shares. */
	:where([data-slean='toggle']:disabled),
	:where([data-slean='toggle'][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='toggle'][data-variant='text']:disabled),
	:where([data-slean='toggle'][data-variant='text'][aria-disabled='true']) {
		border-color: transparent;
		background: transparent;
	}

	/* Forced colors replace the fills with Canvas; the pressed state keeps a visible fill in the
	 * system highlight colors. */
	@media (forced-colors: active) {
		:where([data-slean='toggle']) {
			border-color: ButtonText;
		}

		:where([data-slean='toggle'][aria-pressed='true']) {
			forced-color-adjust: none;
			border-color: Highlight;
			background: Highlight;
			color: HighlightText;
		}

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