/* Round 4 — catalogue-native box building.
 *
 * Built on the existing Preview3 tokens only (--surface, --line, --accent,
 * Inter Tight, tabular numerals). No new colour, no new type scale, no new
 * looping animation: selection is a high-frequency interaction, so it is
 * instant and uses transform/opacity only.
 */

/* ==========================================================================
 * 1. Box controls on a catalogue card
 * ======================================================================= */

/* The mobile card is a flex column whose children are explicitly re-ordered
   (card-top 1 … compare row 7). An unordered child would jump to the FRONT of
   that list, so the box controls claim the next slot rather than order 0. */
.pp-box {
	order: 8;
	position: relative;
	z-index: 1;
	display: grid;
	gap: var(--s2);
	margin-top: var(--s3);
	padding-top: var(--s3);
	border-top: 1px solid var(--line);
}

.pp-box-lead {
	margin: 0;
	color: var(--quiet);
	font: 600 11px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	letter-spacing: .04em;
	text-transform: uppercase;
}

.pp-box-option {
	display: flex;
	align-items: center;
	gap: var(--s2);
	min-height: 44px;
}

.pp-box-option .pbo-variant {
	flex: 0 0 auto;
	min-width: 58px;
	color: var(--text);
	font: 600 13px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}

/* The add button is a JavaScript affordance. With scripting off the real
   number field is shown instead, so the form still posts a real box. */
.pbo-add { display: none; }
.p3box-js .pp-box-option[data-box-qty="0"] .pbo-add { display: inline-flex; }
.p3box-js .pp-box-option[data-box-qty="0"] .pbo-step { display: none; }

.pbo-add {
	flex: 1 1 auto;
	align-items: center;
	justify-content: center;
	gap: 7px;
	min-height: 44px;
	padding: 8px 14px;
	border: 1px solid color-mix(in srgb, var(--accent) 42%, var(--line-strong));
	border-radius: var(--r-pill);
	background: color-mix(in srgb, var(--accent) 10%, transparent);
	color: var(--accent-text);
	font: 600 13px/1.2 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	letter-spacing: -.005em;
	cursor: pointer;
	transition: border-color 160ms var(--ease), transform 120ms var(--ease);
}
.pbo-add:hover { border-color: var(--accent-bright); }
.pbo-add:active { transform: scale(.985); }
.pbo-add:disabled { opacity: .45; cursor: not-allowed; }
.pbo-plus { font-size: 16px; line-height: 1; }

.pbo-step {
	display: inline-flex;
	flex: 1 1 auto;
	align-items: center;
	justify-content: space-between;
	gap: 6px;
	padding: 3px;
	border: 1px solid color-mix(in srgb, var(--accent) 46%, var(--line-strong));
	border-radius: var(--r-pill);
	background: var(--surface-raised);
}

.pbo-btn {
	min-width: 44px;
	min-height: 44px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	padding: 0;
	border: 0;
	border-radius: var(--r-pill);
	background: transparent;
	color: var(--text);
	font: 600 17px/1 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	cursor: pointer;
	transition: color 140ms var(--ease);
}
.pbo-btn:hover:not(:disabled) { color: var(--accent-text); }
.pbo-btn:disabled { opacity: .35; cursor: not-allowed; }

/* WooCommerce's own field styling (style.css: .woocommerce input[type="number"])
   is !important and would otherwise swallow this control, so the quantity field
   states its own geometry at matching weight rather than being left invisible. */
.pp-box .pbo-qty,
.woocommerce .pp-box .pbo-qty {
	width: 46px !important;
	min-width: 46px;
	min-height: 44px !important;
	padding: 0 !important;
	border: 0 !important;
	border-radius: var(--r-pill) !important;
	background: transparent !important;
	color: var(--text) !important;
	font: 700 15px/1.1 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
	text-align: center;
	-moz-appearance: textfield;
}
.pbo-qty::-webkit-outer-spin-button,
.pbo-qty::-webkit-inner-spin-button { margin: 0; -webkit-appearance: none; }

/* Honestly disabled: still visible, still readable, with the exact reason. */
.pp-box-option.is-blocked {
	gap: var(--s2);
	padding: 9px 12px;
	border: 1px dashed var(--line-strong);
	border-radius: var(--r-md);
	background: color-mix(in srgb, var(--surface-raised) 60%, transparent);
}
.pbo-why {
	min-width: 0;
	color: var(--muted);
	font: 500 12px/1.4 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	letter-spacing: .005em;
}

.pp-card.is-box-chosen {
	border-color: color-mix(in srgb, var(--accent) 62%, var(--line));
	box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 28%, transparent);
}
.pp-box-option.is-chosen .pbo-variant { color: var(--accent-text); }

/* ==========================================================================
 * 2. Box-mode band on the Products page
 * ======================================================================= */

.box-mode-band {
	display: grid;
	gap: 6px;
	margin: 0 0 var(--s5);
	padding: var(--s4) var(--s5);
	border: 1px solid color-mix(in srgb, var(--accent) 34%, var(--line));
	border-radius: var(--r-md);
	background: color-mix(in srgb, var(--accent) 6%, var(--surface));
}
.box-mode-band p {
	margin: 0;
	max-width: 74ch;
	color: var(--muted);
	font: 400 14px/1.6 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ==========================================================================
 * 3. The persistent tray
 * ======================================================================= */

.p3-box-tray {
	position: fixed;
	z-index: 220;
	left: clamp(12px, 2.4vw, 26px);
	right: clamp(12px, 2.4vw, 26px);
	bottom: calc(clamp(12px, 2.4vw, 26px) + env(safe-area-inset-bottom));
	display: flex;
	flex-direction: column;
	max-width: var(--container);
	margin-inline: auto;
	border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--line-strong));
	border-radius: var(--r-lg);
	background: color-mix(in srgb, var(--surface) 94%, transparent);
	-webkit-backdrop-filter: blur(14px);
	backdrop-filter: blur(14px);
	box-shadow: 0 22px 54px rgba(0, 0, 0, .5);
	scroll-margin-top: calc(var(--nav-h) + 24px);
}
.p3-box-tray.is-full {
	border-color: color-mix(in srgb, var(--accent) 60%, var(--line-strong));
}

/* Keep the last row of the catalogue reachable above the docked tray, and keep
   anything scrolled to clear of both the sticky header and the tray. */
body.p3box-on { padding-bottom: calc(var(--p3box-h, 84px) + var(--s5)); }
body.p3box-on .pp-card { scroll-margin-block: calc(var(--nav-h) + 16px) calc(var(--p3box-h, 84px) + 16px); }

/* The site's own fixed chrome steps over the tray, exactly the way it already
   steps over the compare tray. --p3box-h is the tray's MEASURED height, not a
   guessed constant; 96px is the theme's own allowance for the compare tray
   (pharma-pass.css). Stacking order from the bottom edge upwards:
   box tray -> compare tray -> support pill. */
body.p3box-on:not(.is-compare) .tg-support,
body.p3box-on:not(.is-compare) .compare-tray {
	bottom: calc(var(--p3box-h, 84px) + clamp(12px, 2.4vw, 26px) + 12px + env(safe-area-inset-bottom));
}
body.p3box-on.compare-active:not(.is-compare) .tg-support {
	bottom: calc(var(--p3box-h, 84px) + 96px + clamp(12px, 2.4vw, 26px) + 12px + env(safe-area-inset-bottom));
}
/* An open overlay owns the screen; the tray gets out of its way. */
body.overlay-open .p3-box-tray { visibility: hidden; opacity: 0; pointer-events: none; }

/* While a box is actually being assembled, the customer is tapping add/remove
   controls all over a scrolling grid, and a button fixed to the corner lands on
   them wherever the scroll happens to stop — measured at 375, 390 and 768, and
   the same class of bug Round 3 hit. Lifting the pill clears the tray but not a
   mid-page card, because the page scrolls underneath it and the pill does not.
   So for the duration of box building the pill steps aside, exactly the way
   `.overlay-open` already stands the compare tray down. It is not moved, not
   restyled and not removed: it returns the moment the box is emptied or
   submitted, and on an idle Products page it behaves as it always has. */
body.p3box-active .tg-support {
	visibility: hidden;
	opacity: 0;
	pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
	body.p3box-on .tg-support { transition: opacity 160ms var(--ease), visibility 160ms var(--ease); }
}

/* --- idle state ---------------------------------------------------------- */

.pbt-idle {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: var(--s3) var(--s4);
	padding: var(--s3) var(--s4);
}
.pbt-idle-copy { display: grid; gap: 2px; margin: 0; min-width: 0; }
.pbt-idle-copy b {
	color: var(--text);
	font: 600 15px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-idle-copy span {
	color: var(--muted);
	font: 400 13px/1.5 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-idle-actions { display: flex; flex-wrap: wrap; gap: var(--s2); }
.pbt-start { min-height: 44px; padding: 8px 16px; font-size: 13px; white-space: nowrap; }

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

.pbt-bar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--s3) var(--s4);
	padding: var(--s3) var(--s4);
}

.pbt-sizes {
	display: inline-flex;
	flex: 0 0 auto;
	gap: 4px;
	padding: 4px;
	border: 1px solid var(--line-strong);
	border-radius: var(--r-pill);
	background: var(--surface-raised);
}
.pbt-size {
	display: inline-flex;
	align-items: center;
	gap: 7px;
	min-height: 44px;
	padding: 6px 14px;
	border: 0;
	border-radius: var(--r-pill);
	background: transparent;
	color: var(--muted);
	font: 600 13px/1.2 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	white-space: nowrap;
	cursor: pointer;
	transition: color 140ms var(--ease), background-color 140ms var(--ease);
}
.pbt-size.is-selected { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--text); }
.pbt-size .pbs-off {
	color: var(--accent-text);
	font-size: 12px;
	font-variant-numeric: tabular-nums;
}

.pbt-progress { display: grid; flex: 1 1 190px; gap: 7px; min-width: 0; }
.pbt-count { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px var(--s2); margin: 0; }
.pbt-count b {
	color: var(--text);
	font: 700 15px/1.2 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}
.pbt-count span {
	color: var(--muted);
	font: 500 12px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-slots { display: flex; flex-wrap: wrap; gap: 5px; }
.pbt-slot {
	width: 16px;
	height: 5px;
	border-radius: var(--r-pill);
	background: var(--line-strong);
	transition: background-color 140ms var(--ease);
}
.pbt-slot.is-filled { background: var(--accent); }

.pbt-money { display: grid; flex: 0 0 auto; gap: 1px; text-align: right; }
.pbt-total {
	color: var(--text);
	font: 700 19px/1.2 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}
.pbt-saveline {
	color: var(--accent-text);
	font: 600 12px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}

.pbt-actions { display: flex; flex: 0 0 auto; align-items: center; gap: var(--s2); }
.pbt-toggle {
	min-height: 44px;
	padding: 8px 14px;
	border: 1px solid var(--line-strong);
	border-radius: var(--r-pill);
	background: transparent;
	color: var(--muted);
	font: 600 13px/1.2 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	white-space: nowrap;
	cursor: pointer;
	transition: color 140ms var(--ease), border-color 140ms var(--ease);
}
.pbt-toggle:hover { border-color: var(--accent); color: var(--text); }
.pbt-submit { min-height: 48px; padding: 10px 18px; font-size: 14px; white-space: nowrap; }
.pbt-submit:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: none; }
.pbt-submit.is-busy { opacity: .6; }

/* --- the edit panel ------------------------------------------------------ */

.pbt-panel {
	order: -1;
	max-height: min(56vh, 460px);
	padding: var(--s4) var(--s4) 0;
	overflow-y: auto;
	overscroll-behavior: contain;
	border-bottom: 1px solid var(--line);
}
.pbt-panel-grid {
	display: grid;
	grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
	gap: var(--s4) var(--s6);
	padding-bottom: var(--s4);
}
.pbt-h {
	margin: 0 0 var(--s3);
	color: var(--quiet);
	font: 600 11px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	letter-spacing: .04em;
	text-transform: uppercase;
}
.pbt-chosen { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.pbt-chip {
	display: grid;
	grid-template-columns: minmax(0, 1fr) auto auto auto;
	align-items: center;
	gap: var(--s2);
	padding: 5px 6px 5px 12px;
	border: 1px solid var(--line);
	border-radius: var(--r-pill);
	background: var(--surface-raised);
}
.pbt-chip.is-empty {
	display: block;
	padding: 12px;
	border-style: dashed;
	color: var(--muted);
	font: 500 13px/1.4 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	text-align: center;
}
.pbc-name {
	min-width: 0;
	overflow: hidden;
	color: var(--text);
	font: 600 13px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.pbc-step { display: inline-flex; align-items: center; gap: 2px; }
.pbc-btn {
	min-width: 44px;
	min-height: 44px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	border: 0;
	border-radius: var(--r-pill);
	background: transparent;
	color: var(--text);
	font: 600 16px/1 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	cursor: pointer;
}
.pbc-btn:hover:not(:disabled) { color: var(--accent-text); }
.pbc-btn:disabled { opacity: .35; cursor: not-allowed; }
.pbc-qty {
	min-width: 20px;
	color: var(--text);
	font: 700 14px/1 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
	text-align: center;
}
.pbc-value {
	color: var(--muted);
	font: 600 13px/1.3 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}
.pbc-drop {
	min-width: 44px;
	min-height: 44px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	border: 0;
	border-radius: var(--r-pill);
	background: transparent;
	color: var(--muted);
	font: 600 16px/1 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	cursor: pointer;
}
.pbc-drop:hover { color: var(--text); }

.pbt-sums { display: grid; gap: 7px; margin: 0 0 var(--s3); }
.pbt-sums > div { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s3); }
.pbt-sums dt {
	color: var(--muted);
	font: 500 13px/1.4 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-sums dd {
	margin: 0;
	color: var(--text);
	font: 600 14px/1.4 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-variant-numeric: tabular-nums;
}
.pbt-sums .is-off dt, .pbt-sums .is-off dd { color: var(--accent-text); }
.pbt-sums .is-total { padding-top: 8px; border-top: 1px solid var(--line); }
.pbt-sums .is-total dt { color: var(--text); font-weight: 600; }
.pbt-sums .is-total dd { font-size: 17px; font-weight: 700; }

.pbt-mode, .pbt-note {
	margin: 0 0 var(--s3);
	color: var(--muted);
	font: 400 12px/1.55 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-clear { min-height: 44px; margin-bottom: var(--s3); padding: 9px 16px; font-size: 13px; }

/* --- live regions -------------------------------------------------------- */

/* The spoken channel. Everything it says is already visible in the bar, so it
   is announced rather than repeated on screen. */
.pbt-say {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	clip-path: inset(50%);
	white-space: nowrap;
}

/* A dropped selection is never silent: this one is seen as well as spoken. */
.pbt-drop {
	order: -2;
	margin: 0;
	padding: 10px var(--s4);
	border-bottom: 1px solid color-mix(in srgb, var(--warn) 34%, var(--line));
	border-radius: var(--r-lg) var(--r-lg) 0 0;
	background: color-mix(in srgb, var(--warn) 10%, transparent);
	color: var(--text);
	font: 500 13px/1.5 "Inter Tight", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.pbt-drop[hidden] { display: none; }

/* ==========================================================================
 * 4. Narrower viewports
 * ======================================================================= */

@media (max-width: 900px) {
	.pbt-panel-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 680px) {
	.p3-box-tray {
		left: 10px;
		right: 10px;
		bottom: calc(10px + env(safe-area-inset-bottom));
		border-radius: var(--r-md);
	}
	.pbt-bar {
		display: grid;
		grid-template-columns: minmax(0, 1fr) auto;
		gap: var(--s2) var(--s3);
		padding: 10px 12px;
	}
	.pbt-sizes { grid-column: 1 / -1; justify-self: start; }
	.pbt-progress { grid-column: 1; }
	.pbt-money { grid-column: 2; align-self: center; }
	.pbt-actions { grid-column: 1 / -1; }
	.pbt-toggle { flex: 0 0 auto; }
	.pbt-submit { flex: 1 1 auto; }
	.pbt-panel { max-height: min(52vh, 400px); padding: var(--s3) var(--s3) 0; }
	.pbt-idle { padding: 10px 12px; }
	.pbt-idle-actions { width: 100%; }
	.pbt-start { flex: 1 1 auto; }
	.pbt-size { padding: 6px 10px; font-size: 12px; }
	.pbt-total { font-size: 17px; }
	.box-mode-band { padding: var(--s3) var(--s4); }
}

@media (max-width: 400px) {
	.pbt-size .pbs-n { display: none; }
	.pbt-size::before {
		content: attr(data-box-size-option);
		font-variant-numeric: tabular-nums;
	}
	.pbc-name { font-size: 12px; }
}

/* ==========================================================================
 * 5. Reduced motion
 * ======================================================================= */

@media (prefers-reduced-motion: reduce) {
	.pp-box-option, .pbo-add, .pbo-btn, .pbt-size, .pbt-slot, .pbt-toggle, .p3-box-tray {
		transition: none !important;
	}
	.pbo-add:active { transform: none; }
}
