/**
 * HappenBoard Search — dialog styles.
 *
 * Everything visual is driven by the custom properties in the first rule.
 * Override them from the Site Editor's additional CSS to restyle without
 * touching the plugin. Neutrals are derived from currentColor so the dialog
 * follows the theme in both light and dark; the accent, radii and shadow read
 * the theme presets, so a palette change carries over on its own.
 */

.hb-search {
	--hbs-max-width: 640px;
	--hbs-radius: var(--wp--custom--radius--lg, 12px);
	--hbs-gap: 1rem;
	--hbs-bg: Canvas;
	--hbs-fg: CanvasText;
	--hbs-border: color-mix(in srgb, currentColor 14%, transparent);
	--hbs-muted: color-mix(in srgb, currentColor 62%, transparent);
	--hbs-hover: color-mix(in srgb, currentColor 7%, transparent);
	--hbs-active: color-mix(in srgb, currentColor 12%, transparent);
	--hbs-accent: var(--wp--preset--color--primary, #16181D);
	--hbs-shadow: var(--wp--preset--shadow--lg, 0 24px 60px rgba(0, 0, 0, 0.24));
}

/* ------------------------------------------------------------- trigger -- */

.hb-search-trigger {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	color: inherit;
	text-decoration: none;
	border-radius: var(--wp--custom--radius--sm, 6px);
	cursor: pointer;
}

.hb-search-trigger:focus-visible {
	outline: 2px solid var(--hbs-accent, currentColor);
	outline-offset: 3px;
}

/* The theme renders the icon inside; keep it from adding its own spacing. */
.hb-search-trigger .wp-block-outermost-icon-block {
	margin: 0;
	pointer-events: none;
}

.hb-visually-hidden {
	position: absolute !important;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
}

/* -------------------------------------------------------------- dialog -- */

dialog.hb-search {
	width: min(var(--hbs-max-width), calc(100vw - 2rem));
	max-width: none;
	padding: 0;
	border: 1px solid var(--hbs-border);
	border-radius: var(--hbs-radius);
	background: var(--hbs-bg);
	color: var(--hbs-fg);
	box-shadow: var(--hbs-shadow);
	/* Sit near the top like a command palette rather than dead centre. */
	margin: clamp(1rem, 8vh, 6rem) auto auto;
	overflow: visible;
}

dialog.hb-search::backdrop {
	background: rgba(0, 0, 0, 0.45);
	backdrop-filter: blur(2px);
}

dialog.hb-search[open] {
	animation: hbs-in 140ms ease-out;
}

@keyframes hbs-in {
	from { opacity: 0; transform: translateY(-6px); }
	to   { opacity: 1; transform: none; }
}

/* Stop the page behind from scrolling while the dialog is up. */
html.hb-search-open {
	overflow: hidden;
}

.hb-search__form {
	display: flex;
	flex-direction: column;
	min-height: 0;
	max-height: min(70vh, 40rem);
}

/* ----------------------------------------------------------------- bar -- */

.hb-search__bar {
	display: flex;
	align-items: center;
	gap: 0.6rem;
	padding: 0.85rem var(--hbs-gap);
	border-block-end: 1px solid var(--hbs-border);
}

.hb-search__icon {
	flex: 0 0 auto;
	width: 20px;
	height: 20px;
	color: var(--hbs-muted);
}

.hb-search__input {
	flex: 1 1 auto;
	min-width: 0;
	border: 0;
	background: none;
	color: inherit;
	font-size: 1.0625rem;
	line-height: 1.5;
	padding: 0.15rem 0;
	appearance: none;
}

.hb-search__input:focus {
	outline: none;
}

.hb-search__input::placeholder {
	color: var(--hbs-muted);
}

/* Hide the browser's own clear button: it fights the close button. */
.hb-search__input::-webkit-search-cancel-button {
	display: none;
}

.hb-search__shortcut {
	flex: 0 0 auto;
	font: inherit;
	font-size: 0.75rem;
	line-height: 1;
	padding: 0.3em 0.5em;
	color: var(--hbs-muted);
	border: 1px solid var(--hbs-border);
	border-radius: var(--wp--custom--radius--xs, 4px);
	white-space: nowrap;
}

.hb-search__close {
	flex: 0 0 auto;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 32px;
	height: 32px;
	padding: 0;
	border: 0;
	border-radius: var(--wp--custom--radius--sm, 6px);
	background: none;
	color: var(--hbs-muted);
	cursor: pointer;
}

.hb-search__close svg { width: 20px; height: 20px; }
.hb-search__close:hover { background: var(--hbs-hover); color: inherit; }
.hb-search__close:focus-visible { outline: 2px solid var(--hbs-accent); outline-offset: 2px; }

.hb-search__hint {
	margin: 0;
	padding: 0.5rem var(--hbs-gap) 0;
	font-size: 0.8125rem;
	color: var(--hbs-muted);
}

.hb-search__hint:empty { display: none; }

/* ------------------------------------------------------------- results -- */

.hb-search__results {
	flex: 1 1 auto;
	min-height: 0;
	overflow-y: auto;
	overscroll-behavior: contain;
	padding: 0.5rem;
	scrollbar-width: thin;
}

.hb-search__results[aria-busy="true"] { opacity: 0.55; }

.hb-search__group + .hb-search__group { margin-block-start: 0.5rem; }

.hb-search__group-title {
	margin: 0;
	padding: 0.4rem 0.6rem 0.25rem;
	font-size: 0.7rem;
	font-weight: 600;
	letter-spacing: 0.06em;
	text-transform: uppercase;
	color: var(--hbs-muted);
}

.hb-search__option {
	display: flex;
	align-items: center;
	gap: 0.75rem;
	padding: 0.55rem 0.6rem;
	border-radius: var(--wp--custom--radius--md, 8px);
	color: inherit;
	text-decoration: none;
	cursor: pointer;
}

.hb-search__option:hover { background: var(--hbs-hover); }

.hb-search__option.is-active {
	background: var(--hbs-active);
	/* A colour change alone would fail for anyone who cannot perceive it. */
	box-shadow: inset 3px 0 0 var(--hbs-accent);
}

.hb-search__option-title {
	flex: 1 1 auto;
	min-width: 0;
	font-size: 0.95rem;
	line-height: 1.4;
	overflow-wrap: anywhere;
}

/* Content type badge, pushed to the end of the row by the title's flex-grow. */
.hb-search__option-type {
	flex: 0 0 auto;
	padding: 0.25em 0.55em;
	border: 1px solid var(--hbs-border);
	border-radius: var(--wp--custom--radius--pill, 999px);
	font-size: 0.6875rem;
	line-height: 1.4;
	color: var(--hbs-muted);
	white-space: nowrap;
}

.hb-search__option:hover .hb-search__option-type,
.hb-search__option.is-active .hb-search__option-type {
	border-color: color-mix(in srgb, currentColor 30%, transparent);
	color: inherit;
}

.hb-search__option mark {
	background: color-mix(in srgb, var(--hbs-accent) 22%, transparent);
	color: inherit;
	border-radius: var(--wp--custom--radius--xs, 4px);
	padding: 0 0.1em;
}

.hb-search__recent {
	display: block;
	width: 100%;
	text-align: start;
	padding: 0.5rem 0.6rem;
	border: 0;
	border-radius: var(--wp--custom--radius--md, 8px);
	background: none;
	color: inherit;
	font: inherit;
	cursor: pointer;
}

.hb-search__recent:hover { background: var(--hbs-hover); }
.hb-search__recent:focus-visible { outline: 2px solid var(--hbs-accent); outline-offset: -2px; }

.hb-search__clear {
	display: inline-block;
	margin: 0.25rem 0 0 0.6rem;
	padding: 0.25rem 0;
	border: 0;
	background: none;
	color: var(--hbs-muted);
	font: inherit;
	font-size: 0.75rem;
	text-decoration: underline;
	cursor: pointer;
}

.hb-search__clear:hover { color: inherit; }
.hb-search__clear:focus-visible { outline: 2px solid var(--hbs-accent); outline-offset: 2px; }

.hb-search__empty {
	margin: 0;
	padding: 1.25rem 0.6rem;
	text-align: center;
	color: var(--hbs-muted);
}

/* -------------------------------------------------------------- footer -- */

.hb-search__footer {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.75rem;
	padding: 0.6rem var(--hbs-gap);
	border-block-start: 1px solid var(--hbs-border);
	font-size: 0.75rem;
	color: var(--hbs-muted);
}

.hb-search__keys kbd {
	display: inline-block;
	font: inherit;
	padding: 0.15em 0.4em;
	margin-inline-end: 0.15em;
	border: 1px solid var(--hbs-border);
	border-radius: var(--wp--custom--radius--xs, 4px);
}

.hb-search__all {
	color: var(--hbs-accent);
	white-space: nowrap;
}

.hb-search__all[hidden] { display: none; }

/* ---------------------------------------------------------- responsive -- */

@media (max-width: 600px) {
	dialog.hb-search {
		width: 100vw;
		max-width: 100vw;
		height: 100dvh;
		max-height: 100dvh;
		margin: 0;
		border: 0;
		border-radius: 0;
	}

	.hb-search__form { max-height: 100dvh; height: 100dvh; }

	/* Keep the tap target comfortable and stop iOS zooming on focus. */
	.hb-search__input { font-size: 1rem; }
	.hb-search__shortcut { display: none; }
	.hb-search__keys { display: none; }
	.hb-search__footer { justify-content: flex-end; }
}

/* ------------------------------------------------------------ prefs -- */

@media (prefers-reduced-motion: reduce) {
	dialog.hb-search[open] { animation: none; }
	.hb-search__results { scroll-behavior: auto; }
}

@media (prefers-contrast: more) {
	.hb-search { --hbs-border: currentColor; }
	.hb-search__option.is-active { outline: 2px solid currentColor; outline-offset: -2px; }
}

@media (prefers-color-scheme: dark) {
	.hb-search { --hbs-shadow: 0 24px 60px rgba(0, 0, 0, 0.6); }
	dialog.hb-search::backdrop { background: rgba(0, 0, 0, 0.6); }
}
