/**
 * WP Support Agent - visitor chat widget.
 *
 * Every colour and dimension comes from a custom property set on #wpsa-root,
 * so theming is one variable block rather than a generated selector list.
 */

#wpsa-root {
	--wpsa-shadow: 0 24px 64px -12px rgba(15, 23, 42, .28), 0 0 0 1px rgba(15, 23, 42, .05);
	--wpsa-bubble-radius: 14px;
	--wpsa-font-size: 15px;
	--wpsa-z: 99990;

	position: relative;
	font-family: var(--wpsa-font, inherit);
	font-size: var(--wpsa-font-size, 15px);
	line-height: 1.55;
	-webkit-font-smoothing: antialiased;
}

/*
 * The reset is wrapped in :where() so it contributes no specificity at all.
 * Written as "#wpsa-root button" it scored 101 against the 10 of a class
 * like .wpsa-launcher, so the reset's "background: none" beat every styled
 * button in the widget and the launcher rendered with no colour at all.
 */
:where(#wpsa-root *),
:where(#wpsa-root *::before),
:where(#wpsa-root *::after) {
	box-sizing: border-box;
}

:where(#wpsa-root button) {
	font: inherit;
	color: inherit;
	margin: 0;
	cursor: pointer;
	border: 0;
	background: none;
	-webkit-appearance: none;
	appearance: none;
}

#wpsa-root [hidden] {
	display: none !important;
}

/*
 * An SVG's width and height attributes are presentation hints, so any CSS on
 * the page targeting svg beats them - and a busy site has plenty. These are
 * deliberately specific enough to win, and set no fill or stroke so the
 * icons' own attributes still control their appearance.
 */
#wpsa-root svg {
	display: block;
	flex: none;
	width: auto;
	height: auto;
	max-width: none;
	max-height: none;
	vertical-align: middle;
}

#wpsa-root .wpsa-avatar-img,
#wpsa-root .wpsa-header-avatar img {
	width: 100%;
	height: 100%;
	max-width: 100%;
	object-fit: cover;
	border-radius: 50%;
}

.wpsa-launcher-open,
.wpsa-launcher-close {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 100%;
	height: 100%;
}

.wpsa-launcher-open .wpsa-avatar-img {
	width: 62%;
	height: 62%;
}

.wpsa-sr {
	position: absolute !important;
	width: 1px;
	height: 1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	white-space: nowrap;
	border: 0;
}

/* ---------------------------------------------------------------- Launcher */

.wpsa-launcher {
	position: fixed;
	z-index: var(--wpsa-z);
	display: flex;
	align-items: center;
	justify-content: center;
	width: var(--wpsa-launcher);
	height: var(--wpsa-launcher);
	color: var(--wpsa-primary-ink);
	background: var(--wpsa-primary);
	border-radius: 50%;
	box-shadow: var(--wpsa-shadow);
	transition: transform .18s ease, box-shadow .18s ease;
}

.wpsa-launcher:hover {
	transform: translateY(-2px);
}

.wpsa-launcher:focus-visible {
	outline: 3px solid var(--wpsa-primary);
	outline-offset: 3px;
}

.wpsa-position-bottom-right .wpsa-launcher { right: var(--wpsa-offset-x); bottom: var(--wpsa-offset-y); }
.wpsa-position-bottom-left  .wpsa-launcher { left:  var(--wpsa-offset-x); bottom: var(--wpsa-offset-y); }
.wpsa-position-top-right    .wpsa-launcher { right: var(--wpsa-offset-x); top:    var(--wpsa-offset-y); }
.wpsa-position-top-left     .wpsa-launcher { left:  var(--wpsa-offset-x); top:    var(--wpsa-offset-y); }

.wpsa-launcher-close { display: none; }

/* Only the floating launcher turns into a close cross. */
.wpsa-placement-floating.is-open .wpsa-launcher-open { display: none; }
.wpsa-placement-floating.is-open .wpsa-launcher-close { display: block; }

/* ---------------------------------------------------------------- Greeting */

.wpsa-greeting {
	position: fixed;
	z-index: var(--wpsa-z);
	display: flex;
	align-items: flex-start;
	gap: 6px;
	max-width: min(260px, calc(100vw - 32px));
	padding: 12px 14px;
	color: var(--wpsa-text);
	background: var(--wpsa-surface);
	border: 1px solid var(--wpsa-border);
	border-radius: calc(var(--wpsa-radius) * .9);
	box-shadow: var(--wpsa-shadow);
	animation: wpsa-rise .28s ease both;
}

.wpsa-position-bottom-right .wpsa-greeting { right: var(--wpsa-offset-x); bottom: calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); }
.wpsa-position-bottom-left  .wpsa-greeting { left:  var(--wpsa-offset-x); bottom: calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); }
.wpsa-position-top-right    .wpsa-greeting { right: var(--wpsa-offset-x); top:    calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); }
.wpsa-position-top-left     .wpsa-greeting { left:  var(--wpsa-offset-x); top:    calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); }

.wpsa-greeting-text {
	flex: 1;
	font-size: 14px;
	text-align: left;
}

.wpsa-greeting-dismiss {
	flex: none;
	padding: 0 2px;
	font-size: 18px;
	line-height: 1;
	color: var(--wpsa-muted);
}

/* ------------------------------------------------------------------- Panel */

.wpsa-panel {
	position: fixed;
	z-index: calc(var(--wpsa-z) - 1);
	display: flex;
	flex-direction: column;
	width: min(var(--wpsa-panel-w), calc(100vw - 32px));
	max-height: min(var(--wpsa-panel-h), calc(100dvh - 40px));
	height: auto;
	overflow: hidden;
	color: var(--wpsa-text);
	background: var(--wpsa-surface);
	border-radius: var(--wpsa-radius);
	box-shadow: var(--wpsa-shadow);
	opacity: 0;
	transform: translateY(12px) scale(.98);
	transform-origin: bottom right;
	transition: opacity .18s ease, transform .18s ease;
}

#wpsa-root.is-open .wpsa-panel {
	opacity: 1;
	transform: none;
}

/* Keep the icon and label on an in-page bar whatever its state. */
.wpsa-expands .wpsa-launcher-open,
.wpsa-expands.is-open .wpsa-launcher-open {
	display: flex;
}

.wpsa-expands .wpsa-launcher-close {
	display: none;
}

.wpsa-position-bottom-right .wpsa-panel { right: var(--wpsa-offset-x); bottom: calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); }
.wpsa-position-bottom-left  .wpsa-panel { left:  var(--wpsa-offset-x); bottom: calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); transform-origin: bottom left; }
.wpsa-position-top-right    .wpsa-panel { right: var(--wpsa-offset-x); top:    calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); transform-origin: top right; }
.wpsa-position-top-left     .wpsa-panel { left:  var(--wpsa-offset-x); top:    calc(var(--wpsa-offset-y) + var(--wpsa-launcher) + 14px); transform-origin: top left; }

.wpsa-placement-inline .wpsa-panel {
	position: static;
	width: 100%;
	max-width: 100%;
	max-height: var(--wpsa-panel-h);
	height: var(--wpsa-panel-h);
	opacity: 1;
	transform: none;
	border: 1px solid var(--wpsa-border);
	box-shadow: none;
}

/* ------------------------------------------------------------------ Header */

.wpsa-header {
	display: flex;
	flex: none;
	gap: 10px;
	align-items: center;
	padding: 12px 12px 12px 14px;
	color: var(--wpsa-primary-ink);
	background: var(--wpsa-primary);
}

.wpsa-header-avatar {
	display: flex;
	flex: none;
	align-items: center;
	justify-content: center;
	width: 38px;
	height: 38px;
	overflow: hidden;
	background: rgba(255, 255, 255, .18);
	border-radius: 50%;
}

.wpsa-header-avatar img {
	width: 100%;
	height: 100%;
	object-fit: cover;
}

.wpsa-header-meta {
	display: flex;
	flex: 1;
	min-width: 0;
	flex-direction: column;
}

.wpsa-header-name {
	overflow: hidden;
	font-size: 15px;
	font-weight: 600;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.wpsa-header-status {
	font-size: 12.5px;
	opacity: .8;
}

.wpsa-header-btn {
	display: flex;
	flex: none;
	align-items: center;
	justify-content: center;
	width: 32px;
	height: 32px;
	border-radius: 8px;
	opacity: .82;
	transition: background .15s ease, opacity .15s ease;
}

.wpsa-header-btn:hover {
	background: rgba(255, 255, 255, .16);
	opacity: 1;
}

.wpsa-header-btn:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 1px;
}

/* --------------------------------------------------------------------- Log */

.wpsa-log {
	display: flex;
	flex: 1;
	flex-direction: column;
	gap: 10px;
	min-height: 120px;
	padding: 16px 14px;
	overflow-y: auto;
	overscroll-behavior: contain;
	scrollbar-width: thin;
}

.wpsa-msg {
	max-width: 86%;
	padding: 10px 13px;
	overflow-wrap: anywhere;
	border-radius: var(--wpsa-bubble-radius, 14px);
	animation: wpsa-rise .2s ease both;
}

.wpsa-msg--agent {
	background: var(--wpsa-bubble);
	border-bottom-left-radius: 5px;
	align-self: flex-start;
}

.wpsa-msg--user {
	color: var(--wpsa-primary-ink);
	background: var(--wpsa-primary);
	border-bottom-right-radius: 5px;
	align-self: flex-end;
}

.wpsa-msg--error {
	color: #991b1b;
	background: #fef2f2;
	border: 1px solid #fecaca;
	align-self: stretch;
	max-width: 100%;
	font-size: 14px;
}

.wpsa-msg > :first-child { margin-top: 0; }
.wpsa-msg > :last-child { margin-bottom: 0; }

.wpsa-msg p { margin: 0 0 .6em; }
.wpsa-msg h4,
.wpsa-msg h5 { margin: .8em 0 .4em; font-size: 1em; font-weight: 700; }
.wpsa-msg ul,
.wpsa-msg ol { margin: .3em 0 .7em; padding-left: 1.25em; }
.wpsa-msg li { margin: .2em 0; }
.wpsa-msg a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.wpsa-msg code {
	padding: .1em .35em;
	font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
	font-size: .88em;
	background: rgba(127, 127, 127, .18);
	border-radius: 4px;
}
.wpsa-msg pre {
	padding: 10px 12px;
	margin: .5em 0;
	overflow-x: auto;
	background: rgba(127, 127, 127, .14);
	border-radius: 8px;
}
.wpsa-msg pre code { padding: 0; background: none; }
.wpsa-msg blockquote {
	padding-left: .8em;
	margin: .5em 0;
	border-left: 3px solid var(--wpsa-border);
	color: var(--wpsa-muted);
}
.wpsa-msg hr { margin: .8em 0; border: 0; border-top: 1px solid var(--wpsa-border); }

/* Typing indicator. CSS only, and it stops the moment the reply starts. */

.wpsa-typing {
	display: flex;
	gap: 4px;
	align-items: center;
	padding: 14px 13px;
}

.wpsa-typing i {
	width: 7px;
	height: 7px;
	background: var(--wpsa-muted);
	border-radius: 50%;
	animation: wpsa-bounce 1.2s infinite ease-in-out;
}

.wpsa-typing i:nth-child(2) { animation-delay: .16s; }
.wpsa-typing i:nth-child(3) { animation-delay: .32s; }

/* --------------------------------------------------------------- Utilities */

.wpsa-suggestions {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	align-self: flex-start;
	max-width: 100%;
}

.wpsa-chip {
	padding: 7px 12px;
	font-size: 13.5px;
	color: var(--wpsa-primary);
	background: var(--wpsa-primary-soft);
	border-radius: 999px;
	transition: filter .15s ease;
}

.wpsa-chip:hover { filter: brightness(.94); }

.wpsa-rate {
	display: flex;
	gap: 8px;
	align-items: center;
	align-self: flex-start;
	padding: 8px 4px;
	font-size: 13.5px;
	color: var(--wpsa-muted);
}

.wpsa-rate button {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 30px;
	height: 30px;
	border: 1px solid var(--wpsa-border);
	border-radius: 8px;
}

.wpsa-rate button:hover { border-color: var(--wpsa-primary); color: var(--wpsa-primary); }

.wpsa-consent {
	flex: none;
	padding: 14px;
	font-size: 13.5px;
	color: var(--wpsa-muted);
	border-top: 1px solid var(--wpsa-border);
}

.wpsa-consent p { margin: 0 0 10px; }

.wpsa-btn {
	padding: 8px 14px;
	font-size: 14px;
	font-weight: 600;
	color: var(--wpsa-primary-ink);
	background: var(--wpsa-primary);
	border-radius: 9px;
}

/* ---------------------------------------------------------------- Composer */

.wpsa-composer {
	display: flex;
	flex: none;
	gap: 8px;
	align-items: flex-end;
	padding: 10px 10px 10px 14px;
	border-top: 1px solid var(--wpsa-border);
}

.wpsa-input {
	flex: 1;
	max-height: 120px;
	padding: 8px 0;
	overflow-y: auto;
	font: inherit;
	font-size: 15px;
	color: inherit;
	resize: none;
	background: none;
	border: 0;
}

.wpsa-input:focus { outline: none; }
.wpsa-input::placeholder { color: var(--wpsa-muted); }

.wpsa-send {
	display: flex;
	flex: none;
	align-items: center;
	justify-content: center;
	width: 38px;
	height: 38px;
	color: var(--wpsa-primary-ink);
	background: var(--wpsa-primary);
	border-radius: 10px;
	transition: opacity .15s ease;
}

.wpsa-send[disabled] {
	cursor: not-allowed;
	opacity: .45;
}

.wpsa-send:focus-visible {
	outline: 2px solid var(--wpsa-primary);
	outline-offset: 2px;
}

/* ------------------------------------------------------------------- Badge */

.wpsa-badge {
	flex: none;
	padding: 7px 14px 9px;
	font-size: 11.5px;
	text-align: center;
	border-top: 1px solid var(--wpsa-border);
}

.wpsa-badge a {
	color: var(--wpsa-muted);
	text-decoration: none;
}

.wpsa-badge a:hover { text-decoration: underline; }

/* -------------------------------------------------------------- Animations */

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

@keyframes wpsa-bounce {
	0%, 60%, 100% { transform: translateY(0); opacity: .45; }
	30%           { transform: translateY(-4px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
	#wpsa-root *,
	#wpsa-root *::before,
	#wpsa-root *::after {
		animation-duration: .01ms !important;
		animation-iteration-count: 1 !important;
		transition-duration: .01ms !important;
	}

	.wpsa-typing i { opacity: .7; }
}

/* ------------------------------------------------------------------ Mobile */

@media (max-width: 560px) {
	/*
	 * The floating panel goes full-bleed on a phone. The in-page placements
	 * are already in flow and must keep their disclosure behaviour, so the
	 * rules below are scoped away from them rather than overridden after.
	 */
	.wpsa-placement-floating .wpsa-panel {
		top: auto;
		right: 8px;
		bottom: 8px;
		left: 8px;
		width: auto;
		max-height: min(78dvh, calc(100dvh - 16px));
	}

	.wpsa-placement-floating.is-open .wpsa-launcher,
	.wpsa-placement-floating.is-open .wpsa-greeting {
		display: none;
	}

	.wpsa-placement-header,
	.wpsa-placement-footer {
		max-width: 100%;
	}

	/* 16px stops iOS zooming the page when the field is focused. */
	.wpsa-input { font-size: 16px; }
}

/* ----------------------------------------------------------------- Sources */

.wpsa-sources {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	align-items: center;
	align-self: flex-start;
	max-width: 100%;
	padding: 2px 4px 4px;
	font-size: 12.5px;
}

.wpsa-sources__label {
	color: var(--wpsa-muted);
}

.wpsa-sources__link {
	max-width: 210px;
	padding: 3px 9px;
	overflow: hidden;
	color: var(--wpsa-muted);
	text-decoration: none;
	text-overflow: ellipsis;
	white-space: nowrap;
	border: 1px solid var(--wpsa-border);
	border-radius: 999px;
	transition: color .15s ease, border-color .15s ease;
}

.wpsa-sources__link:hover,
.wpsa-sources__link:focus {
	color: var(--wpsa-primary);
	border-color: var(--wpsa-primary);
}


/* ============================================================ Style options */

/* Header treatments. "solid" is the default and needs no rule. */

.wpsa-header-tint .wpsa-header {
	color: var(--wpsa-text);
	background: var(--wpsa-primary-soft);
	border-bottom: 1px solid var(--wpsa-border);
}

.wpsa-header-tint .wpsa-header-avatar {
	color: var(--wpsa-primary);
	background: var(--wpsa-surface);
}

.wpsa-header-minimal .wpsa-header {
	color: var(--wpsa-text);
	background: var(--wpsa-surface);
	border-bottom: 1px solid var(--wpsa-border);
}

.wpsa-header-minimal .wpsa-header-avatar {
	color: var(--wpsa-primary);
	background: var(--wpsa-primary-soft);
}

/* Square bubbles lose the conversational tail. */

.wpsa-bubbles-square .wpsa-msg--agent,
.wpsa-bubbles-square .wpsa-msg--user {
	border-bottom-left-radius: var(--wpsa-bubble-radius);
	border-bottom-right-radius: var(--wpsa-bubble-radius);
}

/* Optional avatar beside each agent message. */

.wpsa-has-msg-avatars .wpsa-msg--agent {
	position: relative;
	margin-left: 34px;
}

.wpsa-has-msg-avatars .wpsa-msg--agent::before {
	position: absolute;
	bottom: 0;
	left: -34px;
	display: block;
	width: 26px;
	height: 26px;
	content: "";
	background: var(--wpsa-primary-soft);
	border-radius: 50%;
}

/* A label turns the round launcher into a pill. */

.wpsa-has-label .wpsa-launcher {
	width: auto;
	min-width: var(--wpsa-launcher);
	padding: 0 18px 0 14px;
	gap: 9px;
	border-radius: 999px;
}

.wpsa-launcher-label {
	font-size: 14.5px;
	font-weight: 600;
	white-space: nowrap;
}

.wpsa-has-label .wpsa-launcher-open,
.wpsa-has-label .wpsa-launcher-close {
	width: auto;
	height: auto;
}

/* The floating pill collapses to a circle when open; an in-page bar keeps
   its label, because the label is the only thing naming it. */
.wpsa-placement-floating.wpsa-has-label.is-open .wpsa-launcher-label {
	display: none;
}

/* Glass depends on backdrop-filter; without it the panel stays opaque. */

@supports (backdrop-filter: blur(12px)) or (-webkit-backdrop-filter: blur(12px)) {
	.wpsa-shadow-glass .wpsa-panel {
		background: color-mix(in srgb, var(--wpsa-surface) 82%, transparent);
		-webkit-backdrop-filter: blur(14px) saturate(1.4);
		backdrop-filter: blur(14px) saturate(1.4);
	}
}

/* =============================================================== Placements */

/*
 * Sticky bar, header and footer placements are a disclosure, not a popup:
 * the launcher is a bar in the page, and the chat expands and collapses
 * underneath it in normal flow. The panel has an explicit height token, so
 * max-height animates to an exact value rather than a guess.
 */

.wpsa-expands {
	display: block;
	width: 100%;
}

.wpsa-expands .wpsa-launcher {
	position: static;
	inset: auto;
	display: flex;
	gap: 10px;
	justify-content: flex-start;
	width: 100%;
	min-width: 0;
	height: auto;
	min-height: 46px;
	padding: 11px 16px;
	border-radius: var(--wpsa-radius) var(--wpsa-radius) 0 0;
	box-shadow: none;
}

.wpsa-expands .wpsa-launcher:hover {
	transform: none;
	filter: brightness(1.06);
}

/* Collapsed, the bar is a single rounded element. */
.wpsa-expands:not(.is-open) .wpsa-launcher {
	border-radius: var(--wpsa-radius);
}

.wpsa-expands .wpsa-launcher-label {
	flex: 1;
	text-align: left;
}

.wpsa-launcher-chevron {
	display: flex;
	flex: none;
	align-items: center;
	transition: transform .28s ease;
}

.wpsa-expands.is-open .wpsa-launcher-chevron {
	transform: rotate(180deg);
}

/* The launcher stays put and keeps its icon; there is no cross to swap to. */
.wpsa-expands.is-open .wpsa-launcher-open {
	display: flex;
}

.wpsa-expands .wpsa-launcher-open,
.wpsa-expands .wpsa-launcher-close {
	width: auto;
	height: auto;
}

.wpsa-expands .wpsa-greeting {
	display: none;
}

.wpsa-expands .wpsa-panel {
	position: static;
	inset: auto;
	width: 100%;
	max-width: 100%;
	height: var(--wpsa-panel-h);
	max-height: 0;
	overflow: hidden;
	border: 1px solid var(--wpsa-border);
	border-top: 0;
	border-radius: 0 0 var(--wpsa-radius) var(--wpsa-radius);
	box-shadow: none;
	opacity: 1;
	transform: none;
	transition: max-height .3s ease;
}

.wpsa-expands.is-open .wpsa-panel {
	/* Honour the configured height, but never let an in-page panel swallow
	   the whole viewport - especially the sticky bar, which would leave
	   nothing of the page visible on a short screen. */
	max-height: min( var(--wpsa-panel-h), 62dvh );
}

.wpsa-expands .wpsa-panel {
	height: min( var(--wpsa-panel-h), 62dvh );
}

/* The panel supplies the frame here, so its own header is redundant chrome. */
.wpsa-expands .wpsa-header {
	display: none;
}

.wpsa-expands .wpsa-log {
	padding-top: 14px;
}

/* A sticky bar across the top of the page. */

.wpsa-placement-top_bar {
	position: sticky;
	top: 0;
	z-index: var(--wpsa-z);
}

.wpsa-placement-top_bar .wpsa-launcher,
.wpsa-placement-top_bar:not(.is-open) .wpsa-launcher {
	border-radius: 0;
}

.wpsa-placement-top_bar .wpsa-panel {
	border-right: 0;
	border-left: 0;
	border-radius: 0;
}

/* Anchored inside the theme's header or footer. */

.wpsa-placement-header,
.wpsa-placement-footer {
	max-width: var(--wpsa-panel-w);
	margin: 8px 0;
}

.wpsa-placement-header.is-anchored,
.wpsa-placement-footer.is-anchored {
	margin: 10px 0;
}

/* Embedded in the page by shortcode or block - always open, no launcher. */

.wpsa-placement-inline {
	display: block;
	width: 100%;
	max-width: 100%;
}

/* Per-reply rating */

.wpsa-rate {
	display: flex;
	gap: 6px;
	align-items: center;
	margin-top: 6px;
	font-size: 12.5px;
	color: var(--wpsa-muted);
	opacity: 0;
	transition: opacity .15s ease;
}

.wpsa-msg:hover .wpsa-rate,
.wpsa-rate:focus-within,
.wpsa-rate.is-done { opacity: 1; }

.wpsa-rate__label { margin-right: 2px; }

.wpsa-rate button {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 26px;
	height: 26px;
	color: var(--wpsa-muted);
	border: 1px solid var(--wpsa-border);
	border-radius: 7px;
}

.wpsa-rate button:hover,
.wpsa-rate button:focus-visible {
	color: var(--wpsa-primary);
	border-color: var(--wpsa-primary);
}

@media (pointer: coarse) {
	/* No hover on touch, so the controls stay visible. */
	.wpsa-rate { opacity: 1; }
}
