/*
 * kody-search — minimal frontend CSS
 *
 * Mostly structural: dropdown positioning, spinner, z-index. Typography
 * inherits from the active theme. A handful of rules set sensible
 * defaults so the widget doesn't look broken on themes with minimal
 * base styles (result highlighting, focus ring, dropdown elevation) —
 * these use CSS4 system color keywords (Canvas, Mark, Highlight, etc.)
 * so they adapt to the browser/OS theme instead of hardcoding a brand
 * color, keeping the "no color pickers" design intact.
 */

/* ── Container ─────────────────────────────────────────────────── */

.kody-search-container {
	position: relative;
	display: inline-block;
	width: 100%;
	/*
	 * Forces a proper stacking context onto the widget's own subtree. Without this,
	 * the inline widget's <input> can render on top of an unrelated, taller,
	 * absolutely-positioned overlay elsewhere on the page that geometrically
	 * extends over it — reproduced with WooCommerce/Storefront's mini-cart flyout:
	 * a thin line from the input painted straight through the flyout's contents,
	 * despite the flyout's z-index (999999) being nowhere near ours (we set none)
	 * and its background being fully opaque. `position: relative` with the z-index
	 * left at auto is not enough to stop this — it does not by itself establish a
	 * stacking context, so the browser is left to reconcile the two elements'
	 * compositing layers on its own, and does so incorrectly in this case.
	 * `isolation: isolate` is the standard fix: it forces exactly the stacking
	 * context `position: relative` alone does not, with no side effect on layout.
	 *
	 * This is only about the <input> vs a foreign overlay. The results dropdown is
	 * the other direction — it must escape *this* subtree, not be contained by it —
	 * and does so via the Popover API top layer (see .kody-search-results-wrapper
	 * below), which is unaffected by this stacking context either way.
	 */
	isolation: isolate;
}

.kody-search-input-wrapper {
	position: relative;
	display: flex;
	align-items: center;
}

.kody-search-input-wrapper .kody-search-input {
	width: 100%;
	padding-top: 0.5em;
	padding-bottom: 0.5em;
	padding-left: 2.75rem;
	padding-right: 2.5rem;
}

/* ── Shared design overrides: inline + search-page inputs ──────── */

.kody-search-input-wrapper .kody-search-input,
.kody-search-page-input-wrapper .kody-search-page-input {
	/* <input> does not inherit font-family from the page, so without this the field
	   the visitor types in shows the browser's form font while the results text below
	   it inherits the theme font — a visible mismatch on themes that set a body font. */
	font-family: inherit;
	font-size: var(--kody-search-input-font-size, inherit);
	border-radius: var(--kody-search-input-radius, inherit);
	padding-top: var(--kody-search-input-padding-y, 0.5em);
	padding-bottom: var(--kody-search-input-padding-y, 0.5em);
	/* left/right padding kept hardcoded — icon space must be preserved */
}

.kody-search-input:focus,
.kody-search-page-input:focus,
.kody-search-overlay-search-input:focus {
	outline: 2px solid Highlight;
	outline-offset: 1px;
}

.kody-search-icon {
	position: absolute;
	left: 0.6rem;
	pointer-events: none;
	display: flex;
	align-items: center;
}

.kody-search-clear {
	position: absolute;
	right: 0.5rem;
	background: none;
	border: none;
	cursor: pointer;
	padding: 0.25rem;
	display: flex;
	align-items: center;
}

/* ── Results dropdown ──────────────────────────────────────────── */

.kody-search-results-wrapper {
	position: absolute;
	top: 100%;
	left: 0;
	right: 0;
	z-index: 9999;
	background: Canvas;
	border: 1px solid ButtonBorder;
	color: CanvasText;
	border-radius: var(--kody-search-dropdown-radius, 0);
	font-size: var(--kody-search-dropdown-font-size, inherit);
	box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

/*
 * Popover-API path (template adds popover="manual"; search.js drives showPopover /
 * hidePopover and sets position/left/top/width inline from the input's rect). The
 * top layer clears any ancestor overflow clip or stacking context — the reason the
 * dropdown was being cut off inside site headers. These rules only undo the UA
 * popover defaults; the visual chrome (background/border/shadow/radius) still comes
 * from the rule above. Browsers without the Popover API do not parse :popover-open,
 * so the whole rule is dropped there and the absolute-positioned dropdown is untouched.
 */
.kody-search-results-wrapper:popover-open {
	position: fixed;
	inset: auto;
	margin: 0;
	padding: 0;
	overflow: visible;
	max-width: 100vw;
}

.kody-search-results {
	/* ~5.5 items visible — 6th is sliced, signalling more below */
	max-height: 22rem;
	overflow-y: auto;
}

/* Sticky fade pinned to the scroll-viewport bottom — no JS, no scrollbar dependency */
.kody-search-results::after {
	content: '';
	display: block;
	position: sticky;
	bottom: 0;
	height: 3rem;
	margin-top: -3rem;
	background: linear-gradient( to bottom, transparent, Canvas );
	pointer-events: none;
}

/*
 * Shared result-item styling for all three render surfaces: the inline
 * dropdown (.kody-search-result-item), the modal overlay
 * (.kody-search-overlay-result-item), and the full search page
 * (.kody-search-search-page-result-item) — same markup shape from
 * search.js's renderItemContent(), so one visual language for all three.
 *
 * Selectors are scoped by root wrapper class (not just the bare item
 * class) so this reliably wins against common theme rules like
 * `.page-content a` or `.entry-content a` — a single class selector
 * loses that specificity fight, a two-class one doesn't.
 */
.kody-search-container .kody-search-result-item,
.kody-search-overlay .kody-search-overlay-result-item,
.kody-search-page-container .kody-search-search-page-result-item {
	display: flex;
	align-items: center;
	gap: 0.75rem;
	padding: var(--kody-search-result-padding, 0.5rem) 0.75rem;
	text-decoration: none;
	color: inherit;
	font-size: var(--kody-search-result-font-size, inherit);
}

.kody-search-container .kody-search-result-item:hover,
.kody-search-container .kody-search-result-item:focus,
.kody-search-overlay .kody-search-overlay-result-item:hover,
.kody-search-overlay .kody-search-overlay-result-item:focus,
.kody-search-page-container .kody-search-search-page-result-item:hover,
.kody-search-page-container .kody-search-search-page-result-item:focus {
	background: ButtonFace;
	color: ButtonText;
}

.kody-search-result-item:not(:last-child),
.kody-search-overlay-result-item:not(:last-child) {
	border-bottom: 1px solid ButtonBorder;
}

.kody-search-result-content,
.kody-search-overlay-result-content,
.kody-search-search-page-result-content {
	flex: 1;
	min-width: 0;
}

.kody-search-result-image,
.kody-search-overlay-result-image,
.kody-search-search-page-result-image {
	width: var(--kody-search-result-image-size, 48px);
	height: var(--kody-search-result-image-size, 48px);
	object-fit: cover;
	border-radius: var(--kody-search-result-image-radius, 4px);
	flex-shrink: 0;
}

/* Matched-term highlight — system colors adapt to light/dark automatically. */
.kody-search-container mark,
.kody-search-overlay mark,
.kody-search-page-container mark {
	background: Mark;
	color: MarkText;
	border-radius: 2px;
	padding: 0 0.15em;
}

/* Fallback box for an item with no featured image, so the row doesn't render as a blank gap. */
.kody-search-no-image {
	background: ButtonFace;
	border-radius: var(--kody-search-result-image-radius, 4px);
}


.kody-search-view-all-wrapper {
	border-top: 1px solid ButtonBorder;
	padding: 0.5rem 0.75rem;
}

.kody-search-view-all {
	display: flex;
	align-items: center;
	justify-content: space-between;
	width: 100%;
	box-sizing: border-box;
	gap: 0.5rem;
	font-size: 0.875rem;
	text-decoration: none;
	color: inherit;
}

.kody-search-view-all:hover,
.kody-search-view-all:focus {
	text-decoration: underline;
	color: inherit;
}

/* ── Search page ───────────────────────────────────────────────── */

.kody-search-page-container {
	width: 100%;
	/* Same fix, same reason, as .kody-search-container above: this page's own input
	   wrapper is position: relative with z-index left at auto, the exact pattern that
	   let the inline widget paint through a taller overlay elsewhere on the page. */
	isolation: isolate;
}

.kody-search-page-input-wrapper {
	position: relative;
	display: flex;
	align-items: center;
	margin-bottom: 1rem;
}

.kody-search-page-input-wrapper .kody-search-page-input {
	width: 100%;
	padding-top: 0.5em;
	padding-bottom: 0.5em;
	padding-left: 2.75rem;
	padding-right: 2.5rem;
}

.kody-search-page-icon {
	position: absolute;
	left: 0.6rem;
	pointer-events: none;
	display: flex;
	align-items: center;
}

.kody-search-page-clear {
	position: absolute;
	right: 0.5rem;
	background: none;
	border: none;
	cursor: pointer;
	padding: 0.25rem;
	display: flex;
	align-items: center;
}

/*
 * Empty until there are results, so it must not reserve space — an always-on margin
 * would push the results down by a line on a page nobody has searched yet.
 */
.kody-search-page-count {
	margin: 0;
	font-size: 0.875rem;
	opacity: 0.75;
}

.kody-search-page-count:not(:empty) {
	margin-bottom: 0.75rem;
}

.kody-search-page-results {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
}

/* ── Loading / spinner ─────────────────────────────────────────── */

.kody-search-loading,
.kody-search-page-loading {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	padding: 0.5rem 0.75rem;
	font-size: 0.875rem;
}

@keyframes kody-search-spin {
	to { transform: rotate(360deg); }
}

.kody-search-spinner {
	display: inline-block;
	width: 1rem;
	height: 1rem;
	border: 2px solid currentColor;
	border-top-color: transparent;
	border-radius: 50%;
	animation: kody-search-spin 0.6s linear infinite;
	flex-shrink: 0;
}

/* ── No results ────────────────────────────────────────────────── */

.kody-search-no-results {
	padding: 0.5rem 0.75rem;
	font-size: 0.875rem;
}

/* ── Pagination ────────────────────────────────────────────────── */

.kody-search-page-pagination {
	display: flex;
	gap: 0.25rem;
	margin-top: 1rem;
	flex-wrap: wrap;
}

.kody-search-page-pagination button {
	cursor: pointer;
}

.kody-search-page-pagination .kody-search-active,
.kody-search-overlay-pagination .kody-search-active {
	background: Highlight;
	color: HighlightText;
	border-color: Highlight;
}

/* ── Overlay (modal search, shown when no search page URL is set) ─── */

/*
 * Hidden until search.js adds .kody-search-active. Without the paired rule the
 * dialog was display:flex from the moment it was created, so closeOverlay() —
 * Escape, the close button, a backdrop click — removed a class that styled
 * nothing and the modal stayed up, with body scroll still locked.
 */
.kody-search-overlay {
	position: fixed;
	inset: 0;
	z-index: 100000;
	display: none;
	align-items: flex-start;
	justify-content: center;
	padding: 10vh 1rem 1rem;
	background: rgba(0, 0, 0, 0.5);
	overflow-y: auto;
}

.kody-search-overlay.kody-search-active {
	display: flex;
}

/*
 * Visually-hidden accessible name for the overlay dialog. Not relying on the
 * theme's .screen-reader-text: it is a WordPress convention, not a guarantee.
 */
.kody-search-overlay-title {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

.kody-search-overlay-dialog {
	position: relative;
	width: 100%;
	max-width: 640px;
	max-height: 80vh;
	display: flex;
	flex-direction: column;
	background: #fff;
	border-radius: var(--kody-search-input-radius, 8px);
	box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.kody-search-overlay-close {
	position: absolute;
	top: 0.5rem;
	right: 0.5rem;
	width: 2rem;
	height: 2rem;
	border: none;
	background: transparent;
	font-size: 1.5rem;
	line-height: 1;
	cursor: pointer;
}

.kody-search-overlay-header {
	padding: 1.25rem 1.25rem 0.75rem;
}

.kody-search-overlay-search-input {
	width: 100%;
	padding: 0.6em 0.75em;
	font-family: inherit; /* see .kody-search-input above — <input> needs this explicitly */
	font-size: var(--kody-search-input-font-size, 1rem);
	border-radius: var(--kody-search-input-radius, 4px);
}

.kody-search-overlay-content {
	padding: 0 1.25rem;
	overflow-y: auto;
	flex: 1;
}

.kody-search-overlay-footer {
	padding: 0.75rem 1.25rem 1.25rem;
}

.kody-search-overlay-pagination {
	display: flex;
	gap: 0.25rem;
	flex-wrap: wrap;
	align-items: center;
}

.kody-search-overlay-page-btn {
	cursor: pointer;
}
