/**
 * Wiz Core CRM, visitor chat widget.
 *
 * The accent colour is injected per render as the --wiz-chat-accent custom
 * property, so a site can rebrand the widget without touching this file. Two
 * readable variants are injected alongside it, --wiz-chat-on-accent for text
 * drawn on the accent, and --wiz-chat-accent-text for the accent used as text
 * or as a line on white, because a light accent such as amber fails contrast
 * in both of those places.
 */

.wiz-chat {
	--wiz-chat-accent: #2563eb;
	--wiz-chat-on-accent: #ffffff;
	--wiz-chat-accent-text: var(--wiz-chat-accent);
	/*
	 * Set here, not inherited. A theme that centres its body copy centred the chat
	 * too: every message, the name in the header and the button labels came out
	 * middle-aligned, which reads as a different product.
	 */
	text-align: left;
	display: flex;
	flex-direction: column;
	width: 360px;
	height: 520px;
	max-height: calc(100vh - 120px);
	background: #ffffff;
	border-radius: 12px;
	box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
	overflow: hidden;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
	font-size: 15px;
	line-height: 1.5;
	color: #0f172a;
	box-sizing: border-box;
}

.wiz-chat *,
.wiz-chat *::before,
.wiz-chat *::after {
	box-sizing: border-box;
}

.wiz-chat--inline {
	width: 100%;
	max-width: 420px;
	height: 460px;
	margin: 0 auto;
	border: 1px solid #e2e8f0;
	box-shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.08);
}

/* Header */
.wiz-chat__header {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 12px 14px;
	background: var(--wiz-chat-accent);
	color: var(--wiz-chat-on-accent);
	flex: 0 0 auto;
}

.wiz-chat__avatar {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 30px;
	height: 30px;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.22);
	font-weight: 700;
	flex: 0 0 auto;
}

.wiz-chat__name {
	font-weight: 600;
	flex: 1 1 auto;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/*
 * A 40px square, not a 20px glyph.
 *
 * Measured on a phone at 360px the close button was 22x20, which is a third of
 * the 44px a thumb needs and the smallest thing on the whole panel. The negative
 * margins keep the icon optically where it was, in the corner of the header,
 * while the button itself is comfortable to hit.
 */
.wiz-chat__close {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 40px;
	height: 40px;
	margin: -8px -10px -8px 0;
	padding: 0;
	border: 0;
	border-radius: 10px;
	background: transparent;
	color: var(--wiz-chat-on-accent);
	font-size: 22px;
	line-height: 1;
	cursor: pointer;
	opacity: 0.85;
}

.wiz-chat__close:focus-visible {
	outline: 2px solid var(--wiz-chat-on-accent);
	outline-offset: 2px;
	opacity: 1;
}

.wiz-chat__close:hover {
	opacity: 1;
}

.wiz-chat--inline .wiz-chat__close {
	display: none;
}

/* Log */
.wiz-chat__log {
	flex: 1 1 auto;
	overflow-y: auto;
	padding: 14px;
	background: #f8fafc;
	display: flex;
	flex-direction: column;
	gap: 8px;
}

.wiz-chat__msg {
	display: flex;
}

.wiz-chat__msg--visitor {
	justify-content: flex-end;
}

.wiz-chat__bubble {
	max-width: 82%;
	padding: 9px 12px;
	border-radius: 12px;
	background: #ffffff;
	border: 1px solid #e2e8f0;
	white-space: pre-wrap;
	word-wrap: break-word;
	overflow-wrap: anywhere;
}

.wiz-chat__msg--visitor .wiz-chat__bubble {
	background: var(--wiz-chat-accent);
	border-color: transparent;
	color: var(--wiz-chat-on-accent);
}

.wiz-chat__msg--system .wiz-chat__bubble {
	background: #fef3c7;
	border-color: #fde68a;
	color: #92400e;
	font-size: 13px;
}

.wiz-chat__msg--agent .wiz-chat__bubble {
	border-color: var(--wiz-chat-accent-text);
}

/* The way out of the chat, so it has to be a thumb-sized row, not a text link. */
.wiz-chat__whatsapp {
	align-self: stretch;
	margin: 0;
	padding: 12px 14px;
	min-height: 44px;
	background: #ffffff;
	border-top: 1px solid #e2e8f0;
	font-weight: 600;
	font-size: 14px;
	color: var(--wiz-chat-accent-text);
	text-decoration: underline;
	flex: 0 0 auto;
}

.wiz-chat__whatsapp[hidden] {
	display: none;
}

/* Form */
.wiz-chat__form {
	display: flex;
	gap: 8px;
	padding: 10px 12px;
	border-top: 1px solid #e2e8f0;
	background: #ffffff;
	flex: 0 0 auto;
}

/*
 * 16px is not a design choice here. Safari on iOS zooms the whole page in when a
 * text field smaller than that takes focus, and the visitor then types into a
 * magnified chat they have to pinch back out of. At 14px every iPhone user met it.
 */
.wiz-chat__input {
	flex: 1 1 auto;
	min-width: 0;
	padding: 9px 11px;
	border: 1px solid #cbd5e1;
	border-radius: 8px;
	font-size: 16px;
	font-family: inherit;
	color: inherit;
}

.wiz-chat__input:focus {
	outline: 2px solid var(--wiz-chat-accent);
	outline-offset: 1px;
}

.wiz-chat__send {
	flex: 0 0 auto;
	padding: 9px 16px;
	border: 0;
	border-radius: 8px;
	background: var(--wiz-chat-accent);
	color: var(--wiz-chat-on-accent);
	font-weight: 600;
	font-family: inherit;
	cursor: pointer;
}

.wiz-chat__send:disabled,
.wiz-chat__input:disabled {
	opacity: 0.6;
	cursor: default;
}

.wiz-chat__privacy {
	margin: 0;
	padding: 0 12px 10px;
	background: #ffffff;
	color: #64748b;
	font-size: 11px;
	flex: 0 0 auto;
}

/* Floating launcher */
.wiz-chat-launcher {
	position: fixed;
	bottom: 20px;
	z-index: 999990;
	display: inline-flex;
	align-items: center;
	gap: 8px;
	padding: 12px 18px;
	border-radius: 999px;
	background: var(--wiz-chat-accent, #2563eb);
	color: var(--wiz-chat-on-accent, #ffffff);
	font-weight: 600;
	text-align: left;
	cursor: pointer;
	box-shadow: 0 8px 20px rgba(15, 23, 42, 0.22);
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.wiz-chat-launcher--right {
	right: 20px;
}

.wiz-chat-launcher--left {
	left: 20px;
}

.wiz-chat-launcher__icon {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 20px;
	height: 20px;
	flex: 0 0 auto;
}

.wiz-chat-launcher__icon svg {
	display: block;
	width: 100%;
	height: 100%;
}

/*
 * It arrives rather than appearing, and gives one slow ring while it waits, so a
 * visitor notices there is somebody to talk to. Both stop when the visitor has
 * opened it, and both are dropped entirely for anyone who has asked their system
 * to reduce motion.
 */
.wiz-chat-launcher {
	isolation: isolate;
}

.wiz-chat-launcher::after {
	content: "";
	position: absolute;
	inset: 0;
	border: 2px solid currentColor;
	border-radius: inherit;
	opacity: 0;
	pointer-events: none;
	animation: wiz-chat-launcher-ring 3.2s ease-out 1.2s infinite;
}

.wiz-chat-launcher:hover::after,
.wiz-chat-launcher:focus-visible::after,
.wiz-chat-launcher[data-open="1"]::after {
	animation: none;
	opacity: 0;
}

.wiz-chat-launcher[data-open="1"] {
	animation: none;
}

@keyframes wiz-chat-launcher-in {
	from {
		opacity: 0;
		transform: translateY(14px) scale(0.94);
	}
	to {
		opacity: 1;
		transform: none;
	}
}

@keyframes wiz-chat-launcher-ring {
	0% {
		opacity: 0.4;
		transform: scale(1);
	}
	70% {
		opacity: 0;
		transform: scale(1.22);
	}
	100% {
		opacity: 0;
		transform: scale(1.22);
	}
}

@media (prefers-reduced-motion: reduce) {
	.wiz-chat-launcher,
	.wiz-chat-launcher::after {
		animation: none !important;
	}
}

.wiz-chat-launcher:focus-visible {
	/* A white ring alone vanishes on a pale page, and a dark one vanishes on the
	   amber accent. The two together read on any background. */
	outline: 3px solid #ffffff;
	outline-offset: 2px;
	box-shadow: 0 0 0 6px rgba(15, 23, 42, 0.45), 0 8px 20px rgba(15, 23, 42, 0.22);
}

.wiz-chat-launcher__panel {
	position: fixed;
	bottom: 82px;
	z-index: 999991;
}

/* Without this the panel is centred on the left edge, because neither side is set. */
.wiz-chat-launcher__panel--right {
	right: 20px;
}

.wiz-chat-launcher__panel--left {
	left: 20px;
}

.wiz-chat-launcher__panel[hidden] {
	display: none;
}

.wiz-chat-launcher__panel + .wiz-chat {
	position: static;
}

.wiz-chat-launcher__panel .wiz-chat {
	box-shadow: 0 16px 40px rgba(15, 23, 42, 0.28);
}

/* Small screens: turn the panel into a bottom sheet. */
@media (max-width: 480px) {
	.wiz-chat-launcher__panel {
		left: 0;
		right: 0;
		/* Clear of the home indicator on a phone with one. */
		bottom: env(safe-area-inset-bottom, 0px);
	}

	.wiz-chat {
		width: 100%;
		/* 100vh on iOS is the height without the browser bar, so 80vh of it leaves
		   the composer under the fold. dvh is the height actually visible. */
		height: 80vh;
		height: 80dvh;
		max-height: 80vh;
		max-height: 80dvh;
		border-radius: 12px 12px 0 0;
	}

	/* 16px is readable at arm's length, and a phone is held closer than a laptop. */
	.wiz-chat__bubble {
		font-size: 16px;
	}

	.wiz-chat__privacy {
		padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
	}

	/*
	 * While the visitor is typing, the sheet takes the whole visible screen.
	 *
	 * With the keyboard up, a sheet anchored to the bottom of an 80dvh box put the
	 * header, and the close button in it, above the top of the screen: the visitor
	 * could type but was looking at a chat with no way out of it and no question in
	 * sight. Filling the visible height keeps the header at the top, the newest
	 * message in the middle and the composer above the keys.
	 *
	 * `:has()` does it with no script; `.is-typing` is the same rule for anything
	 * that does not support it.
	 */
	.wiz-chat:has(.wiz-chat__input:focus),
	.wiz-chat.is-typing {
		height: 100vh;
		max-height: 100vh;
		height: 100dvh;
		max-height: 100dvh;
		border-radius: 0;
	}
}

/* The launcher is fixed to the same corner, so it needs the same inset. */
@supports (padding: env(safe-area-inset-bottom)) {
	.wiz-chat-launcher {
		bottom: calc(20px + env(safe-area-inset-bottom, 0px));
	}
}

/* ---------------------------------------------------------------------------
 * Conversation feel
 *
 * Deliberately WhatsApp-shaped: the visitor's own line carries a clock and then
 * two ticks once the server has it, the other side shows three typing dots before
 * a line lands, and each bubble rises into place rather than appearing.
 * ------------------------------------------------------------------------- */

@keyframes wiz-chat-in {
	from {
		opacity: 0;
		transform: translateY(8px) scale(0.98);
	}

	to {
		opacity: 1;
		transform: none;
	}
}

@keyframes wiz-chat-dot {
	0%,
	60%,
	100% {
		opacity: 0.5;
		transform: translateY(0);
	}

	30% {
		opacity: 1;
		transform: translateY(-3px);
	}
}

@keyframes wiz-chat-panel {
	from {
		opacity: 0;
		transform: translateY(10px) scale(0.98);
	}

	to {
		opacity: 1;
		transform: none;
	}
}

.wiz-chat__msg.is-entering {
	animation: wiz-chat-in 260ms ease-out both;
}

/* The bubble tail, at the foot of the bubble as WhatsApp draws it. */
.wiz-chat__bubble {
	position: relative;
}

.wiz-chat__msg--visitor .wiz-chat__bubble {
	border-bottom-right-radius: 0;
}

.wiz-chat__msg--visitor .wiz-chat__bubble::after {
	position: absolute;
	right: -6px;
	bottom: 0;
	width: 0;
	height: 0;
	border-right: 7px solid transparent;
	border-bottom: 8px solid var(--wiz-chat-accent);
	content: "";
}

.wiz-chat__msg--bot .wiz-chat__bubble {
	border-bottom-left-radius: 0;
}

.wiz-chat__msg--bot .wiz-chat__bubble::after {
	position: absolute;
	left: -6px;
	bottom: 0;
	width: 0;
	height: 0;
	border-bottom: 8px solid #ffffff;
	border-left: 7px solid transparent;
	content: "";
}

/* Time and delivery ticks, floated so they sit at the end of the last line. */
.wiz-chat__meta {
	float: right;
	display: inline-flex;
	align-items: center;
	gap: 3px;
	margin: 5px -2px -3px 8px;
	font-size: 11px;
	line-height: 1;
	opacity: 0.72;
}

.wiz-chat__msg--visitor .wiz-chat__meta {
	opacity: 0.8;
}

.wiz-chat__ticks {
	font-size: 11px;
	letter-spacing: -1px;
}

.wiz-chat__msg.is-pending .wiz-chat__ticks {
	opacity: 0.6;
}

.wiz-chat__bubble--typing {
	display: flex;
	align-items: center;
	gap: 4px;
	padding: 12px 14px;
}

/* Quick replies, the way a chat app offers a tappable answer to its own question. */
.wiz-chat__quick {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
	padding: 2px 0 6px;
	animation: wiz-chat-in 260ms ease-out both;
}

.wiz-chat__quick-hint {
	align-self: center;
	color: #64748b;
	font-size: 11px;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

@media (max-width: 600px) {
	.wiz-chat__quick-hint {
		font-size: 12px;
	}
}

.wiz-chat__quick-skip {
	padding: 8px 18px;
	border: 1px solid var(--wiz-chat-accent-text);
	border-radius: 999px;
	background: #ffffff;
	color: var(--wiz-chat-accent-text);
	font: inherit;
	font-weight: 600;
	cursor: pointer;
}

/* Same thumb rule as the options: the way past an optional question is a button,
   and on a phone it has to be as easy to hit as they are. */
@media (max-width: 600px) {
	.wiz-chat__quick-skip {
		min-height: 44px;
		padding: 10px 20px;
	}
}

.wiz-chat__quick-skip:hover,
.wiz-chat__quick-skip:focus-visible {
	background: var(--wiz-chat-accent);
	color: var(--wiz-chat-on-accent);
}

/* The options for a choice step. Compact pills that wrap, so five of them take
   two or three lines instead of the whole panel, and a long option keeps its
   own line rather than being squeezed. */
.wiz-chat__choices {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	padding: 2px 0 8px;
	animation: wiz-chat-in 260ms ease-out both;
}

.wiz-chat__choice {
	display: inline-flex;
	min-height: 40px;
	align-items: center;
	padding: 9px 14px;
	border: 1px solid var(--wiz-chat-accent-text);
	border-radius: 999px;
	background: #ffffff;
	color: var(--wiz-chat-accent-text);
	font: inherit;
	font-size: 0.92rem;
	font-weight: 600;
	text-align: left;
	cursor: pointer;
	transition: background 160ms ease, color 160ms ease;
}

.wiz-chat__choice:hover,
.wiz-chat__choice:focus-visible {
	background: var(--wiz-chat-accent);
	color: var(--wiz-chat-on-accent);
}

/* A thumb needs 44px, and on a phone this is the main way through the flow. */
@media (max-width: 600px) {
	.wiz-chat__choice {
		min-height: 44px;
	}
}

.wiz-chat__dot {
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: #94a3b8;
	animation: wiz-chat-dot 1.2s ease-in-out infinite;
}

.wiz-chat__dot:nth-child(2) {
	animation-delay: 0.15s;
}

.wiz-chat__dot:nth-child(3) {
	animation-delay: 0.3s;
}

/* Header identity: the name over a presence line. */
.wiz-chat__id {
	display: flex;
	flex-direction: column;
	gap: 2px;
	flex: 1 1 auto;
	min-width: 0;
}

.wiz-chat__presence {
	display: flex;
	align-items: center;
	gap: 5px;
	font-size: 11px;
	line-height: 1.2;
	opacity: 0.92;
}

.wiz-chat__presence-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: #4ade80;
	box-shadow: 0 0 0 2px rgba(74, 222, 128, 0.3);
	flex: 0 0 auto;
}

/* The conversation is with the team rather than with the bot, so a green "we are
   here right now" light would be a promise nobody is holding open. */
.wiz-chat__presence[data-base="handoff"] .wiz-chat__presence-dot {
	background: #fbbf24;
	box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.3);
}

.wiz-chat-launcher {
	animation: wiz-chat-launcher-in 420ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.wiz-chat-launcher__panel:not([hidden]) {
	animation: wiz-chat-panel 220ms ease-out both;
}

@media (prefers-reduced-motion: reduce) {

	.wiz-chat__msg.is-entering,
	.wiz-chat__quick,
	.wiz-chat__dot,
	.wiz-chat-launcher,
	.wiz-chat-launcher__panel:not([hidden]) {
		animation: none;
	}
}

/* Screen-reader-only text, mirroring WordPress core's .screen-reader-text. */
.wiz-chat .screen-reader-text {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}
