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

// This remove the margins set here: https://github.com/WordPress/gutenberg/blob/17e5c2d870d84fb6de48dcd5bc3c38cd0c0fb0d0/packages/block-library/src/editor.scss#L56
// It removes the margins around blocks when a BlockPreview is rendered in the block style selector
.block-editor-block-styles .block-editor-block-list__block {
	margin: 0;
}

/**
 * Cross-Block Selection
 */

@keyframes selection-overlay__fade-in-animation {
	from {
		opacity: 0;
	}
	to {
		opacity: 0.4;
	}
}

// Hide selections on this element, otherwise Safari will include it stacked
// under your actual selection.
// This uses a CSS hack to show the rules to Safari only. Failing here is okay,
// it just makes the selection indication slightly less precise. That makes this
// hack a progressive enhancement.
/* stylelint-disable -- Stylelint is disabled to allow the hack to work. */
_::-webkit-full-page-media, _:future, :root .block-editor-block-list__layout::selection,
_::-webkit-full-page-media, _:future, :root [data-has-multi-selection="true"] .block-editor-block-list__layout::selection {
	background-color: transparent;
}
/* stylelint-enable */


// Note to developers refactoring this, please test navigation mode, and
// multi selection and hovering the block switcher to highlight the block.
// Also be sure to test partial selections in Safari, as it draws the
// selection marker with an entirely different model than Blink.
.block-editor-block-list__layout {
	position: relative;

	// Block multi selection
	.block-editor-block-list__block.is-multi-selected:not(.is-partially-selected) {
		// Hide the native selection indicator, for the selection, and children.
		&::selection,
		& ::selection {
			background: transparent;
		}

		// Draw a spot color overlay.
		&::after {
			content: "";
			position: absolute;
			z-index: 1;
			pointer-events: none;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			background: var(--wp-admin-theme-color);
			opacity: 0.4;

			@media not ( prefers-reduced-motion ) {

				// Animate.
				animation: selection-overlay__fade-in-animation 0.1s ease-out;
				animation-fill-mode: forwards;
			}

			// Show outline in high contrast mode.
			outline: 2px solid transparent;
		}

		// Don't show the highlight focus style when multi selected.
		&.is-highlighted::after {
			outline-color: transparent;
		}
	}

	// Block highlight, and navigation mode, and focus.
	// This piece of code has gone back and forth between using a pseudo element
	// vs. styling the block directly. Ultimately we likely need a pseudo element
	// (unless styles like focus and selection overlay can be rendered as separate elements),
	// since things like border-radius need to be able to be set on the block itself.
	.block-editor-block-list__block.is-highlighted,
	.block-editor-block-list__block.is-highlighted ~ .is-multi-selected,
	.block-editor-block-list__block:not([contenteditable="true"]):focus {
		outline: none;

		// We're using a pseudo element to overflow placeholder borders
		// and any border inside the block itself.
		&::after {
			@include selected-block-focus();
			z-index: 1;
		}
	}


	// Hidden blocks.
	// In order for the block toolbar to render correctly, blocks cannot be hidden.
	// Instead, use styles to visually hide blocks.
	.block-editor-block-list__block.is-block-hidden {
		visibility: hidden;
		overflow: hidden;
		height: 0;
		border: none !important;
		padding: 0 !important;
		opacity: 0;
		margin-top: 0 !important;
		margin-bottom: 0 !important;
	}

	&.is-layout-flex:not(.is-vertical) > .is-block-hidden {
		width: 0;
		height: auto;
		align-self: stretch;
		white-space: nowrap !important;
		margin-left: 0 !important;
		margin-right: 0 !important;
	}

	// Re-enable it on components inside.
	[class^="components-"] {
		user-select: text;
	}
}

.block-editor-block-list__layout .block-editor-block-list__block {
	// With `position: static`, Safari marks a full-width selection rectangle, including margins.
	// With `position: relative`, Safari marks an inline selection rectangle, similar to that of
	// Blink based browsers, but it also does "crop" the marker, which can result in a small line
	// from the preceding paragraph showing, which is effectively the above selection bleeding in.
	// We choose relative, as that matches the multi-selection, which is limited to the block footprint.
	position: relative;

	// Break long strings of text without spaces so they don't overflow the block.
	overflow-wrap: break-word;

	pointer-events: auto;

	&.is-editing-disabled {
		pointer-events: none;
	}

	&.has-negative-margin {
		&.is-selected,
		&.has-child-selected {
			// Bring the selected block forward so we can see it.
			z-index: z-index(".block-editor-block-list__block.is-selected");
		}
	}

	/**
	 * Notices
	 */
	.components-placeholder .components-with-notices-ui {
		margin: -10px 0 12px 0;
	}

	.components-with-notices-ui {
		margin: 0 0 12px 0;
		width: 100%;
	}

	// Warnings
	&.has-warning {
		min-height: $grid-unit-60;

		// When a block has a warning, you shouldn't be able to manipulate the contents.
		> * {
			pointer-events: none;
			user-select: none;
		}

		// Allow the warning action buttons to be manipulable.
		.block-editor-warning {
			pointer-events: all;
		}
	}

	// Clear floats.
	&[data-clear="true"] {
		float: none;
	}

	&:not([draggable="true"]),
	&:not([data-draggable="true"]) {
		cursor: default;
	}

	&[draggable="true"],
	&[data-draggable="true"] {
		cursor: grab;
	}

	// Dragging multi selected blocks is not supported yet.
	&.is-multi-selected {
		cursor: default;
	}

	&[contenteditable],
	[contenteditable] {
		cursor: text;
	}
}

.is-outline-mode .block-editor-block-list__block:not(.remove-outline) {
	&.is-hovered:not(.is-selected),
	&:not(.rich-text):not([contenteditable="true"]).is-selected {

		&::after {
			@include selected-block-focus();
		}
	}
}

// Colorize outlines for template parts and synced patterns (.is-reusable).
.is-outline-mode .block-editor-block-list__block:not(.remove-outline).wp-block-template-part,
.is-outline-mode .block-editor-block-list__block:not(.remove-outline).is-reusable {

	&.is-hovered::after,
	&.is-selected::after,
	&.is-highlighted::after {
		outline-color: var(--wp-block-synced-color);
	}

	&.block-editor-block-list__block:not([contenteditable]):focus {
		&::after {
			outline-color: var(--wp-block-synced-color);
		}
	}
}

// Add fade in/out background for editable blocks in synced patterns.
@keyframes block-editor-is-editable__animation {
	from {
		background-color: rgba(var(--wp-admin-theme-color--rgb), 0.1);
	}
	to {
		background-color: rgba(var(--wp-admin-theme-color--rgb), 0);
	}
}

@keyframes block-editor-is-editable__animation_reduce-motion {
	0% {
		background-color: rgba(var(--wp-admin-theme-color--rgb), 0.1);
	}
	99% {
		background-color: rgba(var(--wp-admin-theme-color--rgb), 0.1);
	}
	100% {
		background-color: rgba(var(--wp-admin-theme-color--rgb), 0);
	}
}

.is-root-container:not([inert]) .block-editor-block-list__block.is-selected .block-editor-block-list__block.has-editable-outline::after {
	animation-name: block-editor-is-editable__animation;
	animation-duration: 0.8s;
	animation-timing-function: ease-out;
	animation-delay: 0.1s;
	animation-fill-mode: backwards;
	bottom: 0;
	content: "";
	left: 0;
	pointer-events: none;
	position: absolute;
	right: 0;
	top: 0;

	// Unique reduced-motion affordance to ensure the effect is still visible when reduce motion is enabled.
	@media (prefers-reduced-motion: reduce) {
		animation-name: block-editor-is-editable__animation_reduce-motion;
		animation-delay: 0s;
	}
}

// Spotlight mode. Fade out blocks unless they contain a selected block.
.is-focus-mode .block-editor-block-list__block:not(.has-child-selected) {
	opacity: 0.2;
	@media not ( prefers-reduced-motion ) {
		transition: opacity 0.1s linear;
	}

	// Nested blocks should never be faded. If the parent block is already faded
	// out, it shouldn't be faded out more. If the parent block in not faded
	// out, it shouldn't be faded out either because the block as a whole,
	// including inner blocks, should be focused.
	.block-editor-block-list__block,
	&.is-selected,
	&.is-multi-selected {
		opacity: 1;
	}
}

// When editing a content only section in focus mode, the entire section stays at full opacity,
// the following overrides focus modes usual rule of only the selected block being at full opacity.
.is-focus-mode .block-editor-block-list__block.is-editing-content-only-section.has-child-selected {
	&,
	& .block-editor-block-list__block {
		opacity: 1;
	}
}

.wp-block[data-align="left"] > *,
.wp-block[data-align="right"] > *,
.wp-block.alignleft,
.wp-block.alignright {
	// Without z-index, won't be clickable as "above" adjacent content.
	z-index: z-index("{core/image aligned left or right} .wp-block");
}

.wp-site-blocks > [data-align="left"] {
	float: left;
	margin-right: 2em;
}

.wp-site-blocks > [data-align="right"] {
	float: right;
	margin-left: 2em;
}

.wp-site-blocks > [data-align="center"] {
	justify-content: center;
	margin-left: auto;
	margin-right: auto;
}

/**
 * In-Canvas Inserter
 */

.block-editor-block-list .block-editor-inserter {
	margin: $grid-unit-10;
	cursor: move; // Fallback for IE/Edge < 14
	cursor: grab;
}

@keyframes block-editor-inserter__toggle__fade-in-animation {
	from {
		opacity: 0;
	}
	to {
		opacity: 1;
	}
}

// Hide the appender that sits at the end of block lists, when inside a nested block,
// unless the block itself, or a parent, is selected.
.wp-block .block-list-appender .block-editor-inserter__toggle {
	@media not ( prefers-reduced-motion ) {
		animation: block-editor-inserter__toggle__fade-in-animation 0.1s ease;
		animation-fill-mode: forwards;
	}
}

.block-editor-block-list__block:not(.is-selected):not(.has-child-selected) .block-editor-default-block-appender {
	display: none;

	.block-editor-inserter__toggle {
		opacity: 0;
		transform: scale(0);
	}
}

.block-editor-block-list__block .block-editor-block-list__block-html-textarea {
	display: block;
	margin: 0;
	padding: $grid-unit-15;
	width: 100%;
	border: none;
	outline: none;
	border-radius: $radius-small;
	box-sizing: border-box;
	box-shadow: inset 0 0 0 $border-width $gray-900;
	resize: none;
	overflow: hidden;
	font-family: $editor-html-font;
	font-size: $text-editor-font-size;
	line-height: 1.5;
	// HTML input is always LTR regardless of language.
	/*rtl:ignore*/
	direction: ltr;

	@media not ( prefers-reduced-motion ) {
		transition: padding 0.2s linear;
	}

	&:focus {
		box-shadow: inset 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
	}
}

.block-editor-block-list__zoom-out-separator {
	/* same color as the iframe's background */
	background: $gray-300;
	// Additional -1px is required to avoid sub pixel rounding errors allowing background to show.
	margin-left: -1px;
	margin-right: -1px;
	@media not ( prefers-reduced-motion ) {
		transition: background-color 0.3s ease;
	}
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	font-size: $default-font-size;
	font-family: $default-font;
	color: $black;
	font-weight: normal;

	.is-zoomed-out & {
		// Maintains an absolute font-size by counter-scaling based on the zoom level.
		font-size: calc(#{$default-font-size} / var(--wp-block-editor-iframe-zoom-out-scale));
	}

	&.is-dragged-over {
		background: $gray-400;
	}
}

// In Post Editor allow the separator to occupy the full width by ignoring (cancelling out) the global padding.
.has-global-padding > .block-editor-block-list__zoom-out-separator,
.block-editor-block-list__layout.is-root-container.has-global-padding > .block-editor-block-list__zoom-out-separator {
	max-width: none;
	// Additional -1px is required to avoid sub pixel rounding errors allowing background to show.
	margin: 0 calc(-1 * var(--wp--style--root--padding-right) - 1px) 0 calc(-1 * var(--wp--style--root--padding-left) - 1px) !important;
}

// This only works in Firefox, Chrome and Safari don't accept a custom cursor
// during drag.
.is-dragging {
	cursor: grabbing;
}

.is-preview-mode {
	pointer-events: none;

	.block-editor-block-list__block {
		pointer-events: auto;

		> *:not(.block-editor-block-list__block) {
			pointer-events: none;
		}
	}
}
