@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/variables" as *;
@use "../utils/theme-variables" as *;

.root {
	-webkit-tap-highlight-color: transparent;
	align-items: center;
	display: flex;
	justify-content: flex-start;
	padding: 0;
	position: relative;
	touch-action: none;
	width: 100%;
	min-height: 40px;

	&:where(.is-compact) {
		min-height: 32px;
	}
}

// Private prop values. Reset each instance to prevent inherited values leaking in.
.wrapper {
	--_wp-components-range-control-color: initial;
	display: block;
	flex: 1;
	position: relative;
	width: 100%;
	color: var(--_wp-components-range-control-color);
	height: 30px;
	min-height: 30px;
}

// Static tokens do not follow admin color schemes outside a ThemeProvider.
.default-color {
	color: $components-color-accent;
}

.before-icon,
.after-icon {
	display: flex; // Avoid changing the height with line-height.
	margin-top: 4px;
}
.before-icon {
	:where(.root:not(.is-rtl)) & {
		margin-right: 6px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
	:where(.root.is-rtl) & {
		margin-left: 6px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
}
.after-icon {
	:where(.root:not(.is-rtl)) & {
		margin-left: 6px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
	:where(.root.is-rtl) & {
		margin-right: 6px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
}

.rail {
	--_wp-components-range-control-rail-color: initial;
	left: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
	pointer-events: none;
	right: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
	display: block;
	height: 4px;
	position: absolute;
	margin-top: 13px;
	top: 0;
	border-radius: $radius-full;
	background: var(--_wp-components-range-control-rail-color, var(--wpds-color-background-track-neutral));

	&:where(.is-disabled) {
		background: $components-color-gray-100;
	}
}

.track {
	--_wp-components-range-control-track-color: initial;
	border-radius: $radius-full;
	height: 4px;
	pointer-events: none;
	display: block;
	position: absolute;
	margin-top: 13px;
	top: 0;
	background: var(--_wp-components-range-control-track-color);

	&:where(.is-disabled) {
		background: $components-color-gray-400;
	}

	:global(.is-marked) & {
		@media not (prefers-reduced-motion) {
			transition: width ease 0.1s;
		}
	}
}

.marks {
	display: block;
	pointer-events: none;
	position: relative;
	width: 100%;
	user-select: none;
	margin-top: 17px;
}

.mark {
	position: absolute;
	left: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
	top: -4px;
	height: 4px;
	width: 2px;
	transform: translateX(-50%);
	background-color: $components-color-background;
	z-index: 1;
}

.mark-label {
	color: $components-color-gray-300;
	font-size: 11px;
	position: absolute;
	top: 8px;
	white-space: nowrap;
	transform: translateX(-50%);

	&:where(:not(.is-rtl)) {
		left: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}

	&:where(.is-rtl) {
		right: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
		transform: translateX(50%);
	}
	&:where(.is-filled) {
		color: $components-color-gray-700;
	}
}

@mixin thumb-color {
	background: $components-color-accent;
	&:where(.is-disabled) {
		background: $components-color-gray-400;
	}
}

.thumb-wrapper {
	align-items: center;
	display: flex;
	height: 12px;
	justify-content: center;
	margin-top: 9px;
	outline: 0;
	pointer-events: none;
	position: absolute;
	top: 0;
	user-select: none;
	width: 12px;
	border-radius: $radius-round;
	z-index: 3;
	@include thumb-color;
	transform: translateX(4.5px);

	:where(.root:not(.is-rtl)) & {
		margin-left: -10px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}

	:where(.root.is-rtl) & {
		margin-right: -10px; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
		transform: translateX(-4.5px);
	}
	:global(.is-marked) & {
		@media not (prefers-reduced-motion) {
			transition: left ease 0.1s;
		}
	}
}

.thumb {
	align-items: center;
	border-radius: $radius-round;
	height: 100%;
	outline: 0;
	position: absolute;
	user-select: none;
	width: 100%;
	box-shadow:
		0 1px 1px rgba(0, 0, 0, 0.03),
		0 1px 2px rgba(0, 0, 0, 0.02),
		0 3px 3px rgba(0, 0, 0, 0.02),
		0 4px 4px rgba(0, 0, 0, 0.01);
	@include thumb-color;

	&:where(.is-focused) {
		@include outset-ring__focus();
	}
}

.input {
	box-sizing: border-box;
	cursor: var(--wpds-cursor-control);
	display: block;
	height: 100%;
	left: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
	margin: 0 -6px;
	opacity: 0;
	outline: none;
	position: absolute;
	right: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
	top: 0;
	width: calc(100% + 12px);
}

.tooltip {
	background: rgba(0, 0, 0, 0.8);
	border-radius: $radius-small;
	color: #fff;
	font-size: 12px;
	min-width: 32px;
	padding: 4px 8px;
	pointer-events: none;
	position: absolute;
	text-align: center;
	user-select: none;
	line-height: 1.4;
	display: none;
	opacity: 0;
	transform: translateX(-50%);

	&:where(.is-rtl) {
		transform: translateX(50%);
	}
	&:where(.is-visible) {
		display: inline-block;
		opacity: 1;
	}
	&:where(:not(.is-bottom)) {
		top: -80%;
	}
	&:where(.is-bottom) {
		bottom: -80%;
	}
	@media not (prefers-reduced-motion) {
		transition:
			opacity 120ms ease,
			display 120ms ease allow-discrete;
	}
	@starting-style {
		opacity: 0;
	}
}

// Preserve the existing NumberControl composition and reset-button sizing.
.input-number {
	display: inline-block;
	font-size: 13px;
	margin-top: 0;
	:where(.root:not(.is-rtl)) & {
		margin-left: $grid-unit-20 !important; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
	:where(.root.is-rtl) & {
		margin-right: $grid-unit-20 !important; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
}
.action-right {
	display: block;
	margin-top: 0;
	:where(.root:not(.is-rtl)) & {
		margin-left: $grid-unit-10; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}
	:where(.root.is-rtl) & {
		margin-right: $grid-unit-10; /* stylelint-disable-line logical-css/require-logical-properties -- Match the locale-based isRTL() position calculations. */
	}

	button,
	button:global(.is-small) {
		margin-left: 0; /* stylelint-disable-line logical-css/require-logical-properties -- Preserve the existing physical offset alongside RTL positioning. */
		height: 30px;
		min-height: 30px;
	}
}

// CSS-wide keywords must apply to the actual property, not the custom property.
@each $keyword in inherit, initial, unset, revert, revert-layer {
	.color-#{$keyword} {
		color: $keyword;
	}
	.background-#{$keyword} {
		background: $keyword;
	}
}

@media (forced-colors: active) {
	.rail {
		background: GrayText;
	}
	.track,
	.thumb,
	.thumb-wrapper {
		background: CanvasText;
		&:where(.is-disabled) {
			background: GrayText;
		}
	}
}
