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

/* Patterns - Interaction - Floating Actions */
////
/// @group Patterns-Floating_Actions
/// Patterns - Interaction - Floating Actions

///
.floating {
	&-actions-wrapper,
	&-overlay {
		--osui-floating-actions-layer: var(--layer-global-elevated);
	}

	&-actions-wrapper {
		// ─── Component CSS API ─────────────────────────────────────────────
		--osui-floating-actions-button-background: var(--color-primary);
		--osui-floating-actions-button-background-hover: var(--color-primary-hover);
		// The pressed colour comes from the same role the primary button presses
		// with. Don't reach for --color-primary-selected here: that role means
		// "selection highlight", and a theme is free to make it a faint tint,
		// which would press this button to almost nothing.
		--osui-floating-actions-button-background-press: var(
			--color-primary-active,
			#{variables.$token-bg-primary-base-press}
		);
		--osui-floating-actions-button-border-radius: var(--border-radius-md, #{variables.$token-shape-soft-md});
		--osui-floating-actions-button-color: var(--color-text-inverse);
		--osui-floating-actions-button-shadow: #{variables.$token-elevation-3};
		--osui-floating-actions-item-background: var(--color-background-surface);
		--osui-floating-actions-item-background-hover: #{variables.$token-bg-neutral-subtlest-hover};
		--osui-floating-actions-item-background-press: #{variables.$token-bg-neutral-subtlest-press};
		--osui-floating-actions-item-border-radius: var(--border-radius-md, #{variables.$token-shape-soft-md});
		--osui-floating-actions-item-color: var(--color-text);
		--osui-floating-actions-item-icon-color: #{variables.$token-icon-subtle};
		--osui-floating-actions-item-shadow: #{variables.$token-elevation-2};
		// ───────────────────────────────────────────────────────────────────

		align-items: flex-end;
		bottom: 0;
		display: flex;
		flex-direction: column;
		margin: variables.$token-scale-800;
		position: fixed;
		inset-inline-end: 0;
		will-change: transform, opacity;
		z-index: var(--osui-floating-actions-layer);

		// Service Studio Preview — the editor has no viewport to fix to, so the
		// button is positioned against .content, whose bottom edge is the
		// footer's top edge. The .content-middle rule at the end of this file
		// is what hands the anchor over.
		& {
			-servicestudio-position: absolute;
		}

		// Service Studio Preview — the same lift .bottom-bar-exists gives at
		// runtime, so the gap above a bottom bar looks alike in both.
		.main:has(.bottom-bar) & {
			& {
				-servicestudio-bottom: variables.$token-scale-400;
			}
		}

		&:not(.is--open) {
			pointer-events: none;

			// Service Studio Preview
			& {
				-servicestudio-pointer-events: auto;
			}
		}

		&.is--open {
			.floating-actions-item {
				opacity: 1;
				transform: translateY(0px) translateZ(0) scale(1);
				transition:
					opacity variables.$token-transition-time-200 variables.$token-transition-curve-quick,
					transform variables.$token-transition-time-200 variables.$token-transition-curve-quick;
				transition-delay: calc(var(--delay) * 40ms); // design-spec: stagger delay
			}

			.floating-actions-item-button {
				transform: translateZ(0) scale(1);
			}

			.floating-button {
				transform: rotate(135deg) translateZ(0);
			}
		}

		&.bottom-bar-exists {
			bottom: calc(var(--bottom-bar-size) + variables.$token-scale-400);
		}

		&.no-rotation {
			.floating-button {
				transform: none;
			}
		}

		.slide-from-left-enter .layout-native &,
		.slide-from-left-leave .layout-native &,
		.slide-from-right-enter .layout-native &,
		.slide-from-right-leave .layout-native &,
		.slide-from-top-enter .layout-native &,
		.slide-from-top-leave .layout-native &,
		.slide-from-bottom-enter .layout-native &,
		.slide-from-bottom-leave .layout-native &,
		.fade-enter .layout-native &,
		.fade-leave .layout-native & {
			display: none;
		}
	}

	&-items {
		align-items: flex-end;
		display: flex;
		flex-direction: column;
		padding-block-end: variables.$token-scale-200;
		padding-inline-end: variables.$token-scale-200;
	}

	&-actions-item {
		align-items: center;
		display: flex;
		justify-content: flex-end;
		margin-block-end: variables.$token-scale-400;
		opacity: 0;
		transform: translateY(variables.$token-scale-400) translateZ(0);
		transition:
			opacity variables.$token-transition-time-100 variables.$token-transition-curve-base,
			transform variables.$token-transition-time-100 variables.$token-transition-curve-base;

		// Service Studio Preview
		& {
			-servicestudio-opacity: 1;
			-servicestudio-transform: translateY(0);
		}
	}

	&-actions-item-label {
		color: var(--osui-floating-actions-item-color);
		font-size: variables.$token-font-size-300;
		font-weight: variables.$token-font-weight-medium;
		letter-spacing: variables.$token-font-letter-spacing-100;
		line-height: variables.$token-font-line-height-500;
		text-align: end;
		white-space: nowrap;

		// A Label widget dropped in the placeholder arrives with the platform's
		// form-label type on [data-label], and a declaration on the element
		// overrides what it would inherit from here, so pull the values back down.
		[data-label] {
			color: inherit;
			font-size: inherit;
			font-weight: inherit;
			letter-spacing: inherit;
			line-height: inherit;
		}

		// An empty placeholder has no text to give it a size. Hold the 20px
		// height the design asks for, and enough width for the editor's
		// "Drag Label here." hint to sit on one line without being clipped.
		&:empty {
			& {
				-servicestudio-min-height: variables.$token-scale-500;
				-servicestudio-min-width: variables.$token-scale-2000;
			}
		}
	}

	&-actions-item-button {
		align-items: center;
		background-color: var(--osui-floating-actions-item-background);
		border-radius: var(--osui-floating-actions-item-border-radius);
		box-shadow: var(--osui-floating-actions-item-shadow);
		color: var(--osui-floating-actions-item-icon-color);
		cursor: pointer;
		display: flex;
		font-size: variables.$token-font-size-500;
		height: variables.$token-scale-1000;
		justify-content: center;
		margin-inline-start: variables.$token-scale-300;
		transform: translateZ(0) scale(0.3);
		transition: transform variables.$token-transition-time-200 variables.$token-transition-curve-quick;
		transition-delay: calc(var(--delay) * 40ms); // design-spec: stagger delay
		width: variables.$token-scale-1000;

		// Service Studio Preview
		& {
			-servicestudio-transform: scale(1);
		}

		&:hover {
			--osui-floating-actions-item-background: var(--osui-floating-actions-item-background-hover);
		}

		&:active {
			--osui-floating-actions-item-background: var(--osui-floating-actions-item-background-press);
		}
	}

	&-button {
		align-items: center;
		background-color: var(--osui-floating-actions-button-background);
		border-radius: var(--osui-floating-actions-button-border-radius);
		box-shadow: var(--osui-floating-actions-button-shadow);
		color: var(--osui-floating-actions-button-color);
		cursor: pointer;
		display: flex;
		font-size: variables.$token-font-size-800;
		height: variables.$token-scale-1400;
		justify-content: center;
		pointer-events: auto;
		transform: rotate(0deg) translateZ(0);
		transform-origin: center center;
		transition:
			transform variables.$token-transition-time-200 variables.$token-transition-curve-linear,
			box-shadow variables.$token-transition-time-200 variables.$token-transition-curve-linear;
		width: variables.$token-scale-1400;

		&:hover {
			background-color: var(--osui-floating-actions-button-background-hover);
		}

		&:active {
			background-color: var(--osui-floating-actions-button-background-press);
		}
	}

	&-overlay {
		background-color: variables.$token-backdrop;
		cursor: pointer;
		height: 100vh;
		opacity: 0;
		pointer-events: none;
		position: fixed;
		inset-inline-end: 0;
		top: 0;
		transition: opacity variables.$token-transition-time-200 variables.$token-transition-curve-base;
		width: 100vw;
		z-index: var(--osui-floating-actions-layer);

		// Service Studio Preview
		& {
			-servicestudio-display: none;
		}

		&.is--open {
			opacity: 1;
			pointer-events: auto;
		}
	}
}

// Service Studio Preview — the wrapper above is positioned absolutely, and
// .content-middle would normally catch it, being the nearest positioned
// ancestor. Its box stops well short of the footer though, which strands the
// button mid-screen, so it stands down and lets .content take the anchor.
.content-middle:has(.floating-actions-wrapper) {
	& {
		-servicestudio-position: static;
	}
}
// Responsive --------------------------------------------------------------------
///
.desktop {
	.floating-overlay {
		display: none;
	}
}

///
.phone {
	.layout-native {
		.floating-actions-wrapper {
			margin: variables.$token-scale-400;
		}
	}

	&.ios {
		&.portrait {
			.layout-native {
				.floating-actions-wrapper {
					margin-bottom: var(--os-safe-area-bottom);
				}
			}
		}

		&.landscape {
			.layout-native {
				.floating-actions-wrapper {
					margin-right: calc(var(--os-safe-area-right) + variables.$token-scale-400);
				}
			}
		}
	}
}

///
.tablet {
	&.ios {
		.floating-actions-wrapper {
			&.bottom-bar-exists {
				margin-block-end: 0;
			}
		}
	}
}

// Accessibility --------------------------------------------------------------------
///
.has-accessible-features {
	.floating-button:focus-visible {
		--osui-focus-shadow-gap: #{variables.$token-scale-050};

		@include a11y-focus-shadow;
	}

	.floating-actions-wrapper {
		&.is--open {
			.floating-button:focus-visible {
				@include a11y-focus-shadow;
			}
		}
	}
}
