@use "@wordpress/base-styles/animations" as *;
@use "@wordpress/base-styles/mixins" as *;
@use "@wordpress/base-styles/variables";
@use "@wordpress/base-styles/z-index" as *;

$modal-header-height: calc(#{variables.$header-height} + var(--wpds-dimension-padding-sm));

body.modal-open {
	overflow: hidden;
}

// The scrim behind the modal window.
.components-modal__screen-overlay {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background-color: rgba(0, 0, 0, 0.35);
	z-index: z-index(".components-modal__screen-overlay");
	display: flex;
	// This animates the appearance of the backdrop.
	@include animation__fade-in($speed: var(--wpds-motion-duration-sm), $easing: var(--wpds-motion-easing-subtle));

	&.is-animating-out {
		// Note: it's important that the fade-out animation doesn't end after the
		// modal frame's disappear animation, because the component will be removed
		// from the DOM when that animation ends.
		@include animation__fade-out($speed: var(--wpds-motion-duration-sm), $delay: var(--wpds-motion-duration-xs), $easing: var(--wpds-motion-easing-subtle));
	}
}

// The modal window element.
.components-modal__frame {
	@include reset;
	// On smaller screens, render as a bottom sheet: edge-to-edge, anchored
	// to the bottom of the viewport. `align-self: flex-end` both anchors
	// the modal to the bottom of the flex overlay and breaks the default
	// `align-items: stretch`, so the modal hugs its content height.
	align-self: flex-end;
	margin: 0;
	width: 100%;
	max-height: calc(100% - var(--wpds-dimension-gap-3xl));
	background: var(--wpds-color-background-surface-neutral-strong);
	box-shadow: variables.$elevation-large;

	// Keep the window boundary visible when forced-colors mode removes its shadow.
	@media (forced-colors: active) {
		outline: var(--wpds-border-width-xs) solid CanvasText;
	}

	border-radius: var(--wpds-border-radius-lg) var(--wpds-border-radius-lg) 0 0;
	overflow: hidden;
	// Have the content element fill the vertical space yet not overflow.
	display: flex;
	color: var(--wpds-color-foreground-content-neutral);
	// Animate the modal frame/contents appearing on the page.
	animation-name: components-modal__appear-animation;
	animation-fill-mode: forwards;
	animation-timing-function: var(--wpds-motion-easing-expressive);

	// Ensure all headings use the proper editor text color, overriding wp-admin common.css
	h1,
	h2,
	h3 {
		color: var(--wpds-color-foreground-content-neutral);
	}

	@media not (prefers-reduced-motion) {
		animation-duration: var(--wpds-motion-duration-md);
	}

	.components-modal__screen-overlay.is-animating-out & {
		animation-name: components-modal__disappear-animation;
		animation-timing-function: var(--wpds-motion-easing-expressive);
	}

	// Show a centered modal on bigger screens.
	@include break-small() {
		align-self: auto;
		border-radius: var(--wpds-border-radius-lg);
		margin: auto;
		width: auto;
		min-width: var(--wpds-dimension-surface-width-sm);
		max-width: calc(100% - var(--wpds-dimension-gap-lg) * 2);
		max-height: calc(100% - #{variables.$header-height * 2});

		&.has-size-small,
		&.has-size-medium,
		&.has-size-large {
			width: 100%;
		}

		&.has-size-small {
			max-width: var(--wpds-dimension-surface-width-md);
		}
		&.has-size-medium {
			max-width: var(--wpds-dimension-surface-width-lg);
		}
		&.has-size-large {
			max-width: var(--wpds-dimension-surface-width-2xl);
		}

	}

	@include break-large() {
		max-height: 70%;
	}

	&.is-full-screen {
		// Full-screen modals cover the entire viewport on mobile,
		// overriding the bottom-sheet defaults.
		margin: 0;
		width: 100%;
		height: 100%;
		max-height: none;
		border-radius: 0;

		// When full screen, make sure the children container is full height.
		:where(.components-modal__content) {
			display: flex;
			// If this container is scrollable, bottom padding won't apply so we use margin instead.
			margin-bottom: var(--wpds-dimension-gap-xl);
			padding-bottom: 0;
			// This edge sits above the frame's rounded bottom corners.
			border-bottom-left-radius: 0;
			border-bottom-right-radius: 0;

			> :last-child {
				flex: 1;
			}
		}

		@include break-small() {
			// Reset the mobile bottom-sheet overrides so the desktop
			// full-screen modal is centered with rounded corners.
			margin: auto;
			border-radius: var(--wpds-border-radius-lg);
			width: calc(100% - var(--wpds-dimension-gap-lg) * 2);
			height: calc(100% - var(--wpds-dimension-gap-lg) * 2);
		}

		@include break-medium() {
			width: calc(100% - var(--wpds-dimension-gap-3xl) * 2);
			height: calc(100% - var(--wpds-dimension-gap-3xl) * 2);
			max-width: none;
		}
	}
}

// On mobile, the modal slides up from the bottom of the viewport.
@keyframes components-modal__appear-animation {
	from {
		opacity: 0;
		transform: translateY(100%);
	}
	to {
		opacity: 1;
		transform: translateY(0);
	}
}

// Note: this animation is also used in the animationend JS event listener.
// Make sure that the animation name is kept in sync across the two files.
@keyframes components-modal__disappear-animation {
	from {
		opacity: 1;
		transform: translateY(0);
	}
	to {
		opacity: 0;
		transform: translateY(100%);
	}
}

// On bigger screens, the modal scales in/out instead of sliding. The keyframe
// names are intentionally reused so the JS animationend listener
// (use-modal-exit-animation.ts) keeps working unchanged.
@include break-small() {
	@keyframes components-modal__appear-animation {
		from {
			opacity: 0;
			transform: scale(0.9);
		}
		to {
			opacity: 1;
			transform: scale(1);
		}
	}

	@keyframes components-modal__disappear-animation {
		from {
			opacity: 1;
			transform: scale(1);
		}
		to {
			opacity: 0;
			transform: scale(0.9);
		}
	}
}

// Fix header to the top so it is always there to provide context to the modal
// if the content needs to be scrolled (for example, on the keyboard shortcuts
// modal screen).
.components-modal__header {
	box-sizing: border-box;
	border-bottom: var(--wpds-border-width-xs) solid transparent;
	padding: var(--wpds-dimension-padding-2xl);
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
	height: $modal-header-height;
	width: 100%;
	// Stack above modal content that may overlap the header.
	z-index: 10;
	position: absolute;
	top: 0;
	left: 0;

	.components-modal__header-heading {
		font-size: var(--wpds-typography-font-size-xl);
		font-weight: var(--wpds-typography-font-weight-emphasis);
	}

	h1 {
		line-height: 1;
		margin: 0;
	}

	.components-modal__content.has-scrolled-content:not(.hide-header) & {
		border-bottom-color: var(--wpds-color-stroke-surface-neutral);
	}

	& + p {
		margin-top: 0;
	}
}

.components-modal__header-heading-container {
	align-items: center;
	flex-grow: 1;
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
}

// Modal contents.
.components-modal__content {
	flex: 1;
	margin-top: $modal-header-height;
	// Small top padding required to avoid cutting off the visible outline when the first child element is focusable.
	padding: var(--wpds-dimension-padding-xs) var(--wpds-dimension-padding-2xl) var(--wpds-dimension-padding-2xl);
	overflow: auto;
	// Follow the frame where it clips the inset focus outline.
	border-bottom-left-radius: inherit;
	border-bottom-right-radius: inherit;

	&.hide-header {
		margin-top: 0;
		padding-top: var(--wpds-dimension-padding-2xl);
		border-top-left-radius: inherit;
		border-top-right-radius: inherit;
	}

	&.is-scrollable:focus-visible {
		@include outset-ring__focus();
		// Keep the ring inside the modal frame, which clips overflowing content.
		outline-offset: calc(-1 * var(--wpds-border-width-focus));
	}
}
