/* @svelte-lean/styles — checkbox (Tier 0, native <input type="checkbox">), ADR 0011 §1.11.
 * Markup: <label><input type="checkbox" data-slean="checkbox"> Label</label>; with role="switch"
 * the same input is drawn as a switch: <input type="checkbox" role="switch" data-slean="checkbox">.
 * State is the native :checked / :indeterminate / :disabled / :user-invalid. The box draws the
 * check or minus icon masked on ::before; the switch is a track (the input) and a thumb (::before);
 * ::after enlarges the pointer target on coarse pointers.
 * Looks: data-color on the input or an ancestor (primary by default); data-size="small|large"
 * (medium without the attribute). */
@layer slean.reset, slean.tokens, slean.base, slean.components, slean.compositions, slean.utilities;

@layer slean.components {
	:where([data-slean='checkbox']) {
		appearance: none;
		position: relative;
		flex: none;
		margin: 0;
		vertical-align: middle;
		cursor: pointer;
	}

	/* The box. */
	:where([data-slean='checkbox']:not([role='switch'])) {
		display: inline-grid;
		place-content: center;
		inline-size: 1.125rem;
		block-size: 1.125rem;
		border: 1px solid var(--slean-control-border);
		border-radius: var(--slean-radius-sm);
		background: var(--slean-control-bg);
		color: var(--slean-color-contrast-text);
		transition:
			background-color var(--slean-duration-fast) var(--slean-ease),
			border-color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='checkbox']:not([role='switch']))::before {
		content: '';
		inline-size: 0.875rem;
		block-size: 0.875rem;
		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='checkbox']:not([role='switch']):checked),
	:where([data-slean='checkbox']:not([role='switch']):indeterminate) {
		border-color: var(--slean-color-main);
		background: var(--slean-color-main);
	}

	:where([data-slean='checkbox']:not([role='switch']):checked)::before,
	:where([data-slean='checkbox']:not([role='switch']):indeterminate)::before {
		opacity: 1;
	}

	:where([data-slean='checkbox']:not([role='switch']):indeterminate)::before {
		mask-image: var(--slean-icon-minus);
	}

	:where([data-slean='checkbox']:not([role='switch'])[data-size='small']) {
		inline-size: 1rem;
		block-size: 1rem;
	}

	:where([data-slean='checkbox']:not([role='switch'])[data-size='small'])::before {
		inline-size: 0.75rem;
		block-size: 0.75rem;
	}

	:where([data-slean='checkbox']:not([role='switch'])[data-size='large']) {
		inline-size: 1.375rem;
		block-size: 1.375rem;
	}

	:where([data-slean='checkbox']:not([role='switch'])[data-size='large'])::before {
		inline-size: 1.125rem;
		block-size: 1.125rem;
	}

	:where([data-slean='checkbox']:not([role='switch']):user-invalid),
	:where([data-slean='checkbox']:not([role='switch'])[aria-invalid='true']) {
		border-color: var(--slean-error-main);
	}

	/* Disabled: the muted surface of every disabled control; a checked box keeps its mark in the
	 * muted text color. */
	:where([data-slean='checkbox']:not([role='switch']):disabled) {
		border-color: var(--slean-control-border-disabled);
		background: var(--slean-control-bg-disabled);
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	/* The switch: the input is the track, ::before the thumb, which moves along the inline axis. */
	:where([data-slean='checkbox'][role='switch']) {
		display: inline-block;
		inline-size: 2.25rem;
		block-size: 1.25rem;
		border: 0;
		border-radius: var(--slean-radius-full);
		background: var(--slean-control-border);
		transition: background-color var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='checkbox'][role='switch'])::before {
		content: '';
		position: absolute;
		inset-block-start: 0.125rem;
		inset-inline-start: 0.125rem;
		inline-size: 1rem;
		block-size: 1rem;
		border-radius: 50%;
		background: var(--slean-surface);
		box-shadow: var(--slean-elevation-1);
		transition: inset-inline-start var(--slean-duration-fast) var(--slean-ease);
	}

	:where([data-slean='checkbox'][role='switch']:checked) {
		background: var(--slean-color-main);
	}

	:where([data-slean='checkbox'][role='switch']:checked)::before {
		inset-inline-start: calc(100% - 1.125rem);
	}

	/* Small: for a dense row of settings or a table cell; large for a touch-first settings page. */
	:where([data-slean='checkbox'][role='switch'][data-size='small']) {
		inline-size: 1.625rem;
		block-size: 1rem;
	}

	:where([data-slean='checkbox'][role='switch'][data-size='small'])::before {
		inline-size: 0.75rem;
		block-size: 0.75rem;
	}

	:where([data-slean='checkbox'][role='switch'][data-size='small']:checked)::before {
		inset-inline-start: calc(100% - 0.875rem);
	}

	:where([data-slean='checkbox'][role='switch'][data-size='large']) {
		inline-size: 2.75rem;
		block-size: 1.5rem;
	}

	:where([data-slean='checkbox'][role='switch'][data-size='large'])::before {
		inline-size: 1.25rem;
		block-size: 1.25rem;
	}

	:where([data-slean='checkbox'][role='switch'][data-size='large']:checked)::before {
		inset-inline-start: calc(100% - 1.375rem);
	}

	:where([data-slean='checkbox'][role='switch']:user-invalid),
	:where([data-slean='checkbox'][role='switch'][aria-invalid='true']) {
		box-shadow: inset 0 0 0 1px var(--slean-error-main);
	}

	/* Disabled: the track takes the muted surface with a border, the thumb the muted text color. */
	:where([data-slean='checkbox'][role='switch']:disabled) {
		background: var(--slean-control-bg-disabled);
		box-shadow: inset 0 0 0 1px var(--slean-control-border-disabled);
		cursor: not-allowed;
	}

	:where([data-slean='checkbox'][role='switch']:disabled)::before {
		background: var(--slean-border-strong);
		box-shadow: none;
	}

	:where([data-slean='checkbox'][role='switch']:checked:disabled) {
		background: color-mix(in oklch, var(--slean-color-main) 35%, var(--slean-control-bg-disabled));
		box-shadow: none;
	}

	:where([data-slean='checkbox'][role='switch']:checked:disabled)::before {
		background: var(--slean-control-bg-disabled);
	}

	/* The label around the input: the control and its text on one line, with a gap. */
	:where(label:has(> [data-slean='checkbox'])) {
		display: inline-flex;
		align-items: center;
		gap: var(--slean-space-2);
		color: var(--slean-fg);
		font-size: var(--slean-text-sm);
		line-height: var(--slean-leading);
		cursor: pointer;
	}

	:where(label:has(> [data-slean='checkbox']:disabled)) {
		color: var(--slean-fg-muted);
		cursor: not-allowed;
	}

	@media (hover: hover) {
		:where([data-slean='checkbox']:not([role='switch'], :disabled, :checked, :indeterminate):hover),
		:where(
			label:hover
				> [data-slean='checkbox']:not([role='switch'], :disabled, :checked, :indeterminate)
		) {
			border-color: var(--slean-color-main);
		}

		:where([data-slean='checkbox'][role='switch']:not(:disabled, :checked):hover),
		:where(label:hover > [data-slean='checkbox'][role='switch']:not(:disabled, :checked)) {
			background: var(--slean-fg-muted);
		}

		:where([data-slean='checkbox'][role='switch']:checked:not(:disabled):hover),
		:where(label:hover > [data-slean='checkbox'][role='switch']:checked:not(:disabled)) {
			background: var(--slean-color-dark);
		}
	}

	@media (pointer: coarse) {
		:where([data-slean='checkbox'])::after {
			content: '';
			position: absolute;
			inset: calc((var(--slean-control-height-md) - 100%) / -2);
		}
	}

	@media (forced-colors: active) {
		:where([data-slean='checkbox']) {
			forced-color-adjust: none;
			background: Canvas;
		}

		:where([data-slean='checkbox']:not([role='switch'])) {
			border-color: ButtonText;
			color: HighlightText;
		}

		:where([data-slean='checkbox']:not([role='switch']))::before {
			forced-color-adjust: none;
		}

		:where([data-slean='checkbox']:not([role='switch']):checked),
		:where([data-slean='checkbox']:not([role='switch']):indeterminate) {
			border-color: Highlight;
			background: Highlight;
		}

		:where([data-slean='checkbox']:not([role='switch']):disabled) {
			border-color: GrayText;
			background: Canvas;
			color: GrayText;
		}

		:where([data-slean='checkbox'][role='switch']) {
			border: 1px solid ButtonText;
		}

		:where([data-slean='checkbox'][role='switch'])::before {
			background: ButtonText;
			box-shadow: none;
		}

		:where([data-slean='checkbox'][role='switch']:checked) {
			border-color: Highlight;
			background: Highlight;
		}

		:where([data-slean='checkbox'][role='switch']:checked)::before {
			background: HighlightText;
		}

		:where([data-slean='checkbox'][role='switch']:disabled) {
			border-color: GrayText;
			background: Canvas;
		}

		:where([data-slean='checkbox'][role='switch']:disabled)::before {
			background: GrayText;
		}
	}
}
