@use '../00-abstract/index' as *;
@use '../tokens/variables';

/* Widgets - Checkbox */
////
/// @group Widgets-Checkbox
/// Widgets - Checkbox

///
[data-checkbox] {
	// ─── Component CSS API ─────────────────────────────────────────────
	--osui-checkbox-size: #{variables.$token-scale-600};
	--osui-checkbox-background: var(--color-background-input);
	--osui-checkbox-border-color: var(--color-border-input);
	// Same hover tier as .form-control[data-input], so a checkbox and an input
	// react identically to the pointer
	--osui-checkbox-hover-border-color: var(--color-border-input-hover);
	--osui-checkbox-border-radius: var(--border-radius-xs, #{variables.$token-shape-soft-xs});
	--osui-checkbox-checked-color: var(--color-primary);
	// ───────────────────────────────────────────────────────────────────

	height: var(--osui-checkbox-size);
	padding: variables.$token-scale-0;
	width: var(--osui-checkbox-size);

	&:before {
		background: var(--osui-checkbox-background);
		border: variables.$token-border-size-025 solid var(--osui-checkbox-border-color);
		border-radius: var(--osui-checkbox-border-radius);
		height: calc(var(--osui-checkbox-size) - 2px);
		opacity: 1;
		transition:
			background-color variables.$token-transition-time-200 variables.$token-transition-curve-linear,
			border-color variables.$token-transition-time-200 variables.$token-transition-curve-linear;
		width: calc(var(--osui-checkbox-size) - 2px);
	}

	&:hover {
		&:before {
			border-color: var(--osui-checkbox-hover-border-color);
		}
	}

	&:checked {
		&:before {
			background: var(--osui-checkbox-checked-color);
			border: variables.$token-border-size-025 solid var(--osui-checkbox-checked-color);
		}

		&:after {
			// physical sides: these borders draw the check glyph under the platform's
			// physical rotate transform — a checkmark must not mirror in RTL
			border: variables.$token-border-size-075 solid var(--color-text-inverse);
			border-right: variables.$token-border-size-0 !important;
			border-top: variables.$token-border-size-0 !important;
			display: block;
			height: variables.$token-scale-100;
			left: 5px; // physical: positions the rotated glyph
			top: 7px;
			width: variables.$token-scale-300;
		}
	}

	&[disabled] {
		pointer-events: none;

		&:before,
		&:checked:before {
			background-color: var(--color-border-subtle);
			border: variables.$token-border-size-025 solid var(--color-border);
		}

		&:checked {
			&:after {
				border: variables.$token-border-size-075 solid var(--color-text-disabled);
			}
		}
	}
}

// Responsive --------------------------------------------------------------------
///
.tablet,
.phone {
	[data-checkbox] {
		--osui-checkbox-size: #{variables.$token-scale-800};

		&:checked {
			&:after {
				left: 9px; // physical: positions the rotated check glyph (see above)
				top: 11px;
			}
		}
	}
}

// Accessibility -----------------------------------------------------------------
///
.has-accessible-features {
	[data-checkbox] {
		border-radius: var(--osui-checkbox-border-radius);

		&:hover {
			&:before {
				border-color: var(--osui-checkbox-hover-border-color);
			}
		}

		&[disabled]:checked {
			&:before {
				border-color: var(--color-border);
			}
		}

		&:checked {
			&:before {
				border-color: var(--osui-checkbox-checked-color);
			}
		}

		&:focus-visible {
			@include a11y-focus-shadow();

			&:before {
				border-color: var(--osui-checkbox-border-color);
			}

			&:checked:before {
				border-color: var(--osui-checkbox-checked-color);
			}
		}
	}
}
