/*
 * GDI Search on the site. Kept small and low-specificity (:where) so themes and block supports win.
 * Colours follow the theme via currentColor and theme.json presets.
 */

.gdi-search-bar {
	position: relative;
	--gdi-search-dropdown-width: 100%;
	--gdi-search-shift: 0px;
}

/* Not :where() here: theme form styles (e.g. .gdi-form label) must not make the label visible. */
.gdi-search-bar .screen-reader-text {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

.gdi-search-bar__form {
	display: flex;
	align-items: stretch;
}

.gdi-search-bar__field {
	position: relative;
	display: flex;
	flex: 1;
	align-items: center;
	min-width: 0;
}

.gdi-search-bar__icon {
	display: block;
	flex: none;
	width: 20px;
	height: 20px;
	max-width: none;
}

.gdi-search-bar__field .gdi-search-bar__icon {
	position: absolute;
	left: 0.9em;
	opacity: 0.6;
	pointer-events: none;
}

/* Field look (border, radius, padding, colours) comes from the theme's .gdi-form styles; only make room for the icon.
   Three classes so it beats the theme's `:is(.gdi-form) input:not(...)` padding shorthand (0,2,1). */
.gdi-search-bar .gdi-search-bar__field .gdi-search-bar__input {
	width: 100%;
	box-sizing: border-box;
	padding-left: 44px;
}

.gdi-search-bar__input::-webkit-search-cancel-button {
	cursor: pointer;
}

.gdi-search-bar__submit {
	display: inline-flex;
	align-items: center;
	gap: 0.4em;
	white-space: nowrap;
	cursor: pointer;
}

/* Toggle mode: the form grows out to the left while the button shrinks away. Plain button by default, colours and font from the block. */

.gdi-search-bar.is-toggle {
	display: flex;
	align-items: center;
	width: fit-content;
	max-width: 100%;
	--gdi-search-toggle-width: 320px;
}

/* Padding + negative margin keep the input's focus ring inside the clipped area. */
.gdi-search-bar.is-toggle .gdi-search-bar__form {
	flex: none;
	box-sizing: content-box;
	width: 0;
	margin: -4px;
	padding: 4px;
	overflow: hidden;
	visibility: hidden;
	opacity: 0;
	transition: width 0.3s ease, opacity 0.2s ease, visibility 0s linear 0.3s;
}

.gdi-search-bar.is-toggle.is-expanded .gdi-search-bar__form {
	width: min(var(--gdi-search-toggle-width), calc(100vw - 32px));
	visibility: visible;
	opacity: 1;
	transition: width 0.3s ease, opacity 0.2s ease, visibility 0s;
}

/* --gdi-search-toggle-size is the button's width, set by search.js, so max-width has a length to animate from. */
.gdi-search-bar.is-toggle .gdi-search-bar__toggle {
	max-width: var(--gdi-search-toggle-size, none);
	overflow: hidden;
	transition: max-width 0.3s ease, padding 0.3s ease, opacity 0.2s ease, visibility 0s;
}

.gdi-search-bar.is-toggle.is-expanded .gdi-search-bar__toggle {
	max-width: 0;
	padding-right: 0;
	padding-left: 0;
	visibility: hidden;
	opacity: 0;
	transition: max-width 0.3s ease, padding 0.3s ease, opacity 0.2s ease, visibility 0s linear 0.3s;
}

@media (prefers-reduced-motion: reduce) {
	.gdi-search-bar.is-toggle .gdi-search-bar__form,
	.gdi-search-bar.is-toggle .gdi-search-bar__toggle {
		transition: none;
	}
}

:where(.gdi-search-bar__toggle) {
	display: inline-flex;
	flex: none;
	align-items: center;
	gap: 0.4em;
	margin: 0;
	padding: 0.5em;
	border: 0;
	border-radius: 6px;
	background: none;
	color: inherit;
	font: inherit;
	white-space: nowrap;
	cursor: pointer;
}

:where(.gdi-search-bar__toggle):hover {
	background: color-mix(in srgb, currentColor 8%, transparent);
}

.gdi-search-bar__toggle:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

.gdi-search-bar.is-loading .gdi-search-bar__field .gdi-search-bar__icon {
	animation: gdi-search-pulse 0.9s ease-in-out infinite;
}

@keyframes gdi-search-pulse {
	50% { opacity: 0.15; }
}

/* Panel: under the bar, width and alignment from the block settings, shifted into the viewport by search.js. */

.gdi-search-bar__panel {
	position: absolute;
	top: calc(100% + 8px);
	left: 0;
	z-index: 1000;
	width: min(var(--gdi-search-dropdown-width), calc(100vw - 32px));
	transform: translateX(var(--gdi-search-shift));
}

.gdi-search-bar__panel[hidden] {
	display: none;
}

.gdi-search-bar.is-dropdown-container {
	--gdi-search-dropdown-width: var(--wp--style--global--wide-size, 1200px);
}

.gdi-search-bar.is-dropdown-align-center .gdi-search-bar__panel {
	left: 50%;
	transform: translateX(calc(-50% + var(--gdi-search-shift)));
}

.gdi-search-bar.is-dropdown-align-right .gdi-search-bar__panel {
	right: 0;
	left: auto;
}

:where(.wp-block-gdi-search-dropdown) {
	box-sizing: border-box;
	max-height: var(--gdi-search-max-height, 70vh);
	overflow-y: auto;
	overscroll-behavior: contain;
	padding: 1.25rem;
	border-radius: 8px;
	background: var(--wp--preset--color--base, #fff);
	color: var(--wp--preset--color--contrast, inherit);
	box-shadow: 0 12px 32px rgb(0 0 0 / 0.16);
}

@media (prefers-reduced-motion: no-preference) {
	.gdi-search-bar.is-open .wp-block-gdi-search-dropdown {
		animation: gdi-search-in 0.14s ease-out;
	}
}

@keyframes gdi-search-in {
	from { opacity: 0; transform: translateY(-4px); }
}

.gdi-search-dropdown__live[aria-busy="true"] {
	opacity: 0.6;
	transition: opacity 0.2s ease;
}

.gdi-search-bar__panel :focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* Lists of terms: suggestions, popular and recent searches. */

:where(.gdi-search-suggestions, .gdi-search-popular, .gdi-search-recent__list) {
	margin: 0;
	padding: 0;
	list-style: none;
}

:where(.gdi-search-suggestions__item, .gdi-search-popular__item, .gdi-search-recent__item, .gdi-search-recent__clear) {
	display: block;
	width: 100%;
	margin: 0;
	padding: 0.35em 0.5em;
	border: 0;
	border-radius: 6px;
	background: none;
	color: inherit;
	font: inherit;
	text-align: left;
	cursor: pointer;
}

:where(.gdi-search-suggestions__item, .gdi-search-popular__item, .gdi-search-recent__item):hover,
:where(.gdi-search-suggestions__item, .gdi-search-popular__item, .gdi-search-recent__item):focus-visible {
	background: color-mix(in srgb, currentColor 8%, transparent);
}

.gdi-search-suggestions mark {
	background: none;
	color: inherit;
	font-weight: 700;
}

:where(.gdi-search-popular.is-style-pills) {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
}

:where(.gdi-search-popular.is-style-pills .gdi-search-popular__item) {
	width: auto;
	padding: 0.3em 0.9em;
	border: 1px solid color-mix(in srgb, currentColor 25%, transparent);
	border-radius: 9999px;
}

:where(.gdi-search-recent__clear) {
	width: auto;
	margin-top: 0.25rem;
	font-size: 0.85em;
	opacity: 0.7;
	text-decoration: underline;
}

.gdi-search-recent[hidden] {
	display: none;
}

/* Live results */

:where(.gdi-search-dropdown__live .wp-block-post-template) {
	margin: 0;
	padding: 0;
	list-style: none;
}

:where([data-gdi-position]) {
	border-radius: 6px;
}

:where([data-gdi-position]):hover,
:where([data-gdi-position]):focus-within {
	background: color-mix(in srgb, currentColor 6%, transparent);
}

:where(.gdi-search-result-count, .gdi-search-view-all) {
	margin: 0;
}
