/* Pharma Plus instant search widget.
 *
 * Mirrors the WoodMart search form pixel for pixel — same height, border,
 * radius, padding, icon-button geometry, dropdown surface, suggestion metrics
 * and typography — by reading the theme's own CSS variables. Every variable
 * carries a fallback, so on another theme the widget still lands on a plain,
 * sensible form instead of breaking.
 *
 * Theme variables used (all optional):
 *   --wd-form-height, --wd-form-brd-color, --wd-form-brd-color-focus,
 *   --wd-form-brd-radius, --wd-form-bg, --wd-form-color,
 *   --wd-form-placeholder-color, --wd-search-form-width, --wd-search-sp,
 *   --wd-search-font-size, --wd-brd-radius, --wd-scroll-h,
 *   --bgcolor-white, --bgcolor-gray-100, --brdcolor-gray-300,
 *   --color-gray-800, --color-gray-600
 */

.pp-search-widget {
	position: relative;
	/* The native header search element is `flex: 1 1 auto` with an optional
	 * max-width; when the theme leaves --wd-search-form-width unset the field
	 * simply fills the slot it was dropped into. */
	width: 100%;
	max-width: var(--wd-search-form-width, none);
	min-width: 0;
	/* The theme's own search form pins its field to 46px (the site-wide form
	 * height is a different token), so the widget resolves the same height
	 * here — where --pp-search-btn is computed — and nothing outside this
	 * subtree is affected. */
	--wd-form-height: 46px;
	--pp-search-sp: var(--wd-search-sp, 15px);
	--pp-search-fs: var(--wd-search-font-size, 0.92em);
	--pp-search-btn: var(--wd-form-height, 42px);
	--pp-search-brd: var(--wd-form-brd-color, rgba(0, 0, 0, 0.1));
	--pp-search-radius: var(--wd-form-brd-radius, 4px);
	--pp-search-white: var(--bgcolor-white, #fff);
	--pp-search-hover: var(--bgcolor-gray-100, #f7f7f7);
	--pp-search-line: var(--brdcolor-gray-300, rgba(0, 0, 0, 0.105));
}

.pp-search-form {
	position: relative;
	margin: 0;
}

/* The opener and the sheet dismiss button belong to the phone header layout
 * only. Everywhere else they are out of the flow entirely, so the inline field
 * keeps its original geometry. */
.pp-search-opener,
.pp-search-close {
	display: none;
}

/* The panel wrapper is pure structure for the mobile sheet. Collapsing it with
 * `display: contents` takes it out of the box tree, so on desktop (and in page
 * content) the form and the dropdown are laid out exactly as if the wrapper
 * were not there — no extra box, no changed metrics. */
.pp-search-panel {
	display: contents;
}

.pp-search-field {
	position: relative;
	display: block;
}

/* ------------------------------- the field ------------------------------ */

.pp-search-widget .pp-search-input {
	box-sizing: border-box;
	width: 100%;
	height: var(--pp-search-btn);
	margin: 0;
	/* Native metrics: the theme's own field padding, and an end padding that
	 * reserves the icon button plus the clear button. Font size, weight and
	 * line-height are deliberately not declared, so the theme's own input
	 * rules keep speaking (they are more specific than this one). */
	padding-block: 0;
	padding-inline: 15px calc(var(--pp-search-btn) + 30px);
	border: var(--wd-form-brd-width, 1px) solid var(--pp-search-brd);
	border-radius: var(--pp-search-radius);
	background-color: var(--wd-form-bg, transparent);
	background-image: none;
	box-shadow: none;
	color: var(--wd-form-color, #767676);
	font-family: inherit;
	appearance: none;
	-webkit-appearance: none;
	outline: none;
	transition: border-color 0.2s ease;
}

.pp-search-widget .pp-search-input::placeholder {
	color: var(--wd-form-placeholder-color, var(--wd-form-color, #767676));
	opacity: 1;
}

.pp-search-widget .pp-search-input:focus {
	border-color: var(--wd-form-brd-color-focus, var(--pp-search-brd));
	box-shadow: none;
	outline: none;
}

/* We draw our own clear control; the browser's built-in one would double it. */
.pp-search-widget .pp-search-input::-webkit-search-cancel-button,
.pp-search-widget .pp-search-input::-webkit-search-decoration {
	appearance: none;
	-webkit-appearance: none;
}

/* The native header field never collapses below its configured width. */
@media (min-width: 1025px) {
	.whb-header .pp-search-input,
	[class*="whb-col"] .pp-search-input {
		min-width: calc(var(--wd-search-form-width, 310px) - 20px);
	}
}

/* ---------------------------- inside the field -------------------------- */

.pp-search-submit,
.pp-search-clear {
	position: absolute;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0;
	border: 0;
	/* The theme gives every bare <button> a 42px min-height; the native search
	 * icons are 24px tall, so that floor has to go. */
	min-height: 0;
	background: none;
	box-shadow: none;
	color: var(--wd-form-color, #767676);
	line-height: 1;
	cursor: pointer;
	transition: opacity 0.2s ease;
}

/* Square icon button inside the field, filling its full height. */
.pp-search-submit {
	inset-block: 0;
	inset-inline-end: 0;
	width: var(--pp-search-btn);
	height: auto;
	/* Native icon size: form height / 2.3. */
	font-size: calc(var(--pp-search-btn) / 2.3);
}

.pp-search-submit svg {
	display: block;
	width: 1em;
	height: 1em;
}

.pp-search-submit:hover,
.pp-search-clear:hover {
	opacity: 0.7;
}

/* Sits just inside the button, separated from the text by a hairline. */
.pp-search-clear {
	top: 50%;
	inset-inline-end: var(--pp-search-btn);
	transform: translateY(-50%);
	display: none;
	min-width: 30px;
	height: 24px;
	padding-inline-end: 10px;
	border-inline-end: var(--wd-form-brd-width, 1px) solid var(--pp-search-brd);
	font-size: calc(var(--pp-search-btn) / 3);
}

.pp-search-clear svg {
	display: block;
	width: 1em;
	height: 1em;
}

.pp-search-field.is-filled .pp-search-clear {
	display: flex;
}

.pp-search-submit:focus-visible,
.pp-search-clear:focus-visible,
.pp-search-widget .pp-search-input:focus-visible {
	outline: 2px solid var(--color-gray-800, #333);
	outline-offset: 2px;
}

/* ------------------------------- dropdown ------------------------------- */

.pp-search-results {
	position: absolute;
	top: 100%;
	/* Inset to the rounded corners of the field, like .wd-dropdown-results. */
	inset-inline: calc(var(--wd-brd-radius, 0px) / 1.5);
	z-index: 380;
	display: none;
	max-height: var(--wd-scroll-h, 50dvh);
	overflow: hidden auto;
	margin: 0;
	background-color: var(--pp-search-white);
	border: 0;
	border-radius: var(--wd-brd-radius, var(--pp-search-radius));
	box-shadow: 0 0 3px rgba(0, 0, 0, 0.15);
	color: var(--color-gray-800, #333);
	text-align: start;
	scrollbar-width: thin;
	scrollbar-color: rgba(0, 0, 0, 0.12) rgba(0, 0, 0, 0.05);
}

.pp-search-results::-webkit-scrollbar {
	width: 5px;
}

.pp-search-results::-webkit-scrollbar-track {
	background-color: rgba(0, 0, 0, 0.05);
}

.pp-search-results::-webkit-scrollbar-thumb {
	background-color: rgba(0, 0, 0, 0.12);
}

/* The one authored moment: the panel fades in and settles, the way the
 * theme's own dropdowns open. */
.pp-search-widget.is-open .pp-search-results {
	display: block;
	animation: pp-search-open 0.4s cubic-bezier(0.19, 1, 0.22, 1) both;
}

@keyframes pp-search-open {
	from {
		opacity: 0;
		transform: translateY(15px);
	}

	to {
		opacity: 1;
		transform: none;
	}
}

/* ------------------------------ suggestion ------------------------------ */

.pp-search-group-title,
.pp-search-section-title {
	/* Native rows overlap borders by a pixel instead of doubling them. */
	margin-block: -1px;
	margin-inline: 0;
	padding: var(--pp-search-sp);
	background-color: var(--pp-search-white);
	border-block: var(--wd-form-brd-width, 1px) solid var(--pp-search-line);
	color: var(--color-gray-900, #242424);
	font-size: var(--pp-search-fs);
	font-weight: 600;
	line-height: 1;
	text-transform: uppercase;
}

/* Suggestion columns, exactly like .wd-suggestions-group. */
.pp-search-group-items {
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

.pp-search-item {
	display: flex;
	align-items: center;
	gap: 15px;
	min-width: 0;
	/* Native rows overlap their borders by one pixel so they never double. */
	margin-bottom: -1px;
	padding: var(--pp-search-sp);
	border: var(--wd-form-brd-width, 1px) solid var(--pp-search-line);
	border-inline-start: 0;
	background-color: var(--pp-search-white);
	/* Native rows are a single type line tall; each child sets its own. */
	line-height: 1;
	transition: background-color 0.25s ease;
}

.pp-search-item:hover,
.pp-search-item.pp-search-active {
	background-color: var(--pp-search-hover);
}

.pp-search-item-link {
	display: flex;
	flex: 1 1 auto;
	align-items: center;
	gap: 15px;
	min-width: 0;
	color: inherit;
	text-decoration: none;
}

.pp-search-item:hover .pp-search-item-link,
.pp-search-item.pp-search-active .pp-search-item-link {
	color: inherit;
	text-decoration: none;
}

/* Native thumbs shrink to their image instead of being stretched. */
.pp-search-item-thumb {
	flex: 0 0 auto;
	max-width: 65px;
	overflow: hidden;
	background: none;
}

.pp-search-item-thumb img {
	display: block;
	max-width: 100%;
	height: auto;
	border-radius: calc(var(--wd-brd-radius, 0px) / 1.5);
}

.pp-search-item-main {
	display: flex;
	flex: 1 1 auto;
	flex-direction: column;
	gap: 2px;
	min-width: 0;
}

.pp-search-item-title {
	overflow: hidden;
	font-size: var(--pp-search-fs);
	font-weight: var(--wd-entities-title-font-weight, 600);
	line-height: 1.4;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* The theme paints <mark> as a padded inline-block badge (5px 8px); inside a
 * suggestion that inflates the row, so the highlight is reset to plain inline
 * emphasis and keeps the suggestion at its native height. */
.pp-search-mark {
	display: inline;
	padding: 0;
	background: none;
	color: inherit;
	font-weight: 700;
	line-height: inherit;
	text-decoration: underline;
}

/* The theme paints prices in its primary colour at full size, not in the
 * suggestion's smaller type. */
.pp-search-item-price {
	color: var(--wd-primary-color, var(--color-gray-600, #666));
	font-size: 1em;
	font-weight: 600;
	line-height: 1;
	white-space: nowrap;
}

/* --------------------------- quick actions ------------------------------ */

.pp-search-action {
	display: inline-flex;
	flex: 0 0 auto;
	align-items: center;
	justify-content: center;
	gap: 4px;
	padding: 6px 12px;
	border: 0;
	border-radius: var(--wd-brd-radius, var(--pp-search-radius));
	/* The theme gives bare buttons a 42px floor; a row action has to stay
	 * inside the suggestion's own 64px line box. */
	min-height: 0;
	background-color: var(--btn-bgcolor, #f3f3f3);
	color: var(--btn-color, #3e3e3e);
	font-size: 11px;
	font-weight: 600;
	line-height: 1.4;
	text-transform: uppercase;
	white-space: nowrap;
	text-decoration: none;
	cursor: pointer;
	transition: background-color 0.25s ease, color 0.25s ease;
}

.pp-search-add:hover,
.pp-search-options:hover {
	background-color: var(--color-gray-800, #333);
	color: var(--pp-search-white);
	text-decoration: none;
}

.pp-search-action:focus-visible {
	outline: 2px solid var(--color-gray-800, #333);
	outline-offset: 2px;
}

.pp-search-add-icon {
	font-size: 12px;
	line-height: 1;
}

.pp-search-add.is-loading {
	opacity: 0.65;
	cursor: progress;
}

.pp-search-add.is-added {
	background-color: #edfaef;
	color: #1d5c28;
}

.pp-search-add.is-error {
	background-color: #fcf0f1;
	color: #8a2424;
}

.pp-search-action-oos {
	background-color: transparent;
	color: var(--color-gray-400, #a5a5a5);
	font-weight: 500;
	cursor: default;
}

/* WooCommerce's add-to-cart.js injects this link inside the row after a
 * successful quick add; keep it compact so the row never breaks. */
.pp-search-item .added_to_cart {
	flex: 0 0 auto;
	color: var(--color-gray-600, #666);
	font-size: var(--pp-search-fs);
	text-decoration: underline;
	white-space: nowrap;
}

/* --------------------- popular / recent searches ------------------------ */

.pp-search-trending {
	padding-inline: var(--pp-search-sp);
	border-top: 1px solid var(--pp-search-line);
}

.pp-search-trending + .pp-search-trending {
	margin-top: -1px;
}

.pp-search-trending .pp-search-section-title {
	margin-inline: calc(-1 * var(--pp-search-sp));
	padding-block: var(--pp-search-sp);
	padding-inline: var(--pp-search-sp);
	border-block: 0;
	border-bottom: 1px solid var(--pp-search-line);
}

.pp-search-trending-list {
	display: flex;
	flex-wrap: wrap;
	gap: 5px;
	margin: 0;
	padding: var(--pp-search-sp) 0;
	list-style: none;
}

.pp-search-trending-item {
	padding: 10px 16px;
	border: 1px solid var(--pp-search-line);
	border-radius: var(--wd-brd-radius, var(--pp-search-radius));
	background-color: var(--pp-search-white);
	color: var(--color-gray-800, #333);
	min-height: 0;
	font-family: inherit;
	font-size: 12px;
	font-weight: 600;
	line-height: 1;
	text-align: start;
	cursor: pointer;
	transition: background-color 0.25s ease;
}

.pp-search-trending-item:hover {
	background-color: var(--pp-search-hover);
}

/* --------------------------- footer and states -------------------------- */

.pp-search-viewall {
	display: block;
	padding-inline: var(--pp-search-sp);
	border-block: 1px solid var(--pp-search-line);
	background-color: var(--pp-search-white);
	color: var(--color-gray-800, #333);
	font-size: var(--pp-search-fs);
	font-weight: 600;
	line-height: 50px;
	text-align: center;
	text-decoration: none;
	text-transform: uppercase;
	transition: background-color 0.25s ease;
}

.pp-search-viewall:hover {
	background-color: var(--pp-search-hover);
	color: var(--color-gray-800, #333);
	text-decoration: none;
}

.pp-search-empty {
	padding: var(--pp-search-sp);
	background-color: var(--pp-search-white);
	color: var(--color-gray-600, #666);
	font-size: var(--pp-search-fs);
}

/* The theme's loading ring: a hairline open circle, rotated. */
.pp-search-spinner {
	width: 32px;
	height: 32px;
	margin: var(--pp-search-sp) auto;
	border: 1px solid transparent;
	border-inline-start-color: var(--color-gray-800, #333);
	border-radius: 50%;
	animation: pp-search-spin 450ms infinite linear;
}

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

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

@media (max-width: 1024px) {
	.pp-search-widget {
		--pp-search-sp: 12px;
	}

	/* iOS Safari zooms the whole page whenever a focused field renders below
	 * 16px, and the theme's own field is 14px. The instant search is typed into
	 * far more often than a checkout form, so the phone size is pinned. */
	.pp-search-widget .pp-search-input {
		font-size: 16px;
		padding-inline-end: calc(var(--pp-search-btn) + 42px);
	}

	/* The desktop clear button is 30×24 — under every touch guideline. */
	.pp-search-clear {
		min-width: 40px;
		height: 40px;
		padding-inline-end: 8px;
	}

	/* One suggestion per line on a phone: two 195px columns truncate every
	 * title to "ویتامین …" and crowd the add-to-cart action. Tablets still get
	 * two or three columns. */
	.pp-search-group-items {
		grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
	}

	.pp-search-item-thumb {
		max-width: 45px;
	}

	/* Rows and chips are tapped, not hovered. */
	.pp-search-action {
		padding: 10px 14px;
		font-size: 12px;
	}

	.pp-search-trending-item {
		padding: 9px 14px;
		font-size: 12px;
	}

	/* The native suggestion type steps down as one block on small screens. */
	.pp-search-item-title,
	.pp-search-item-price {
		font-size: 0.9em;
	}
}

/* --------------------- phone header: the search sheet -------------------- */

/* A phone header row is already fully occupied by the burger, the logo and the
 * cart, so a header instance of the widget puts only its icon there and opens
 * this sheet below the header — the pattern WoodMart's own mobile search uses
 * (`.wd-search-full-screen`): anchored under the header, opaque, scroll-locked,
 * dismissed with ✕, Escape or a tap outside. Page-level instances are untouched
 * and keep the inline field with its dropdown. */
@media (max-width: 1024px) {
	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget {
		width: auto;
	}

	/* The theme pads every header text element by 10px per side; the icon is
	 * already a 40px target, so the padding would only widen a full row. */
	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .wd-header-text:has(.pp-search-widget) {
		flex: 0 0 auto;
		padding-inline: 0;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-opener {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 40px;
		height: 40px;
		padding: 0;
		border: 0;
		background: none;
		box-shadow: none;
		color: var(--wd-header-el-color, #333);
		font-size: 20px; /* the header tool icons are 20px */
		line-height: 1;
		cursor: pointer;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-panel {
		display: none;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded {
		--pp-search-sp: 15px; /* the theme's own mobile search spacing */
		--pp-search-btn: 70px; /* and its mobile form height */
		--pp-search-close-w: 52px;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-panel {
		position: fixed;
		top: var(--pp-search-top, 60px);
		inset-inline: 0;
		bottom: 0;
		z-index: 400;
		display: grid;
		grid-template-areas: "close form" "results results";
		/* The close column is a fixed size rather than `auto`: an auto track
		 * absorbs the container's free space before the `fr` track is
		 * resolved, which would leave the field at its min-content width. */
		grid-template-columns: var(--pp-search-close-w) minmax(0, 1fr);
		grid-template-rows: auto minmax(0, 1fr);
		overflow: hidden;
		background-color: var(--pp-search-white);
		box-shadow: 0 0 14px rgba(0, 0, 0, 0.08);
		animation: pp-search-sheet 0.5s cubic-bezier(0.19, 1, 0.22, 1) both;
	}

	@keyframes pp-search-sheet {
		from {
			transform: translate3d(0, 100%, 0);
		}

		to {
			transform: none;
		}
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-close {
		grid-area: close;
		display: flex;
		align-items: center;
		justify-content: center;
		width: var(--pp-search-close-w);
		height: 70px;
		padding: 0;
		border: 0;
		border-inline-end: 1px solid var(--pp-search-line);
		background: none;
		box-shadow: none;
		color: var(--color-gray-800, #333);
		font-size: 22px;
		line-height: 1;
		cursor: pointer;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-form {
		grid-area: form;
	}

	/* Native mobile full-screen search settings: 70px form, 20px type. */
	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-input {
		height: 70px;
		padding-inline: 15px calc(var(--pp-search-btn) + 42px);
		border: 0;
		border-radius: 0;
		background-color: transparent;
		font-size: 20px;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-submit {
		width: 70px;
		font-size: 26px;
	}

	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-clear {
		inset-inline-end: 70px;
	}

	/* Inside the sheet the results are a scrolling region of the sheet itself
	 * rather than a dropdown: the field stays put while the list scrolls. */
	:is(.whb-header, .whb-main-header, .whb-clone, .whb-row) .pp-search-widget.is-expanded .pp-search-results {
		grid-area: results;
		position: static;
		display: block;
		max-height: none;
		overflow: auto;
		border-top: 1px solid var(--pp-search-line);
		border-radius: 0;
		box-shadow: none;
		animation: none;
		overscroll-behavior: contain;
		-webkit-overflow-scrolling: touch;
	}
}

/* The open sheet covers the page, so the page behind it must not scroll —
 * exactly what the theme does for its own full-screen search. */
html.pp-search-locked,
html.pp-search-locked body {
	overflow: hidden;
}

/* On a 320–400px phone the logo (179px), the burger, the search icon and the
 * cart add up to more than the header row: the row overflowed and the cart was
 * clipped off the screen — with or without the search icon. The logo is the only
 * element that can give, so it follows a shrinking row instead of pushing the
 * controls out. `width: 100%` lets it shrink, while the theme's own inline
 * `max-width` still keeps it from growing past its configured size. Nothing
 * above 400px is touched. */
@media (max-width: 400px) {
	.whb-general-header .whb-mobile-center {
		min-width: 0;
	}

	.whb-general-header .whb-mobile-center .wd-logo img {
		width: 100%;
		height: auto;
	}
}

/* On very narrow widgets keep the icon affordance and drop the label. */
@media (max-width: 360px) {
	.pp-search-add {
		padding: 6px 9px;
	}

	.pp-search-add-text {
		display: none;
	}

	.pp-search-add-icon {
		font-size: 13px;
	}
}

@media (prefers-reduced-motion: reduce) {
	.pp-search-results,
	.pp-search-widget.is-open .pp-search-results,
	.pp-search-widget.is-expanded .pp-search-panel {
		animation: none;
	}

	.pp-search-spinner {
		animation-duration: 1.4s;
	}
}
