/* =============================================================================
   Bafha AgentFlow Chatbot Widget
   Mobile-first, 2-stage (lead form -> chat)
   ============================================================================= */

.bafha-af-chat,
.bafha-af-chat * {
	box-sizing: border-box;
}

.bafha-af-chat {
	--af-bg: #ffffff;
	--af-fg: #0f172a;
	--af-muted: #64748b;
	--af-border: #e2e8f0;
	--af-accent: #1e293b;
	--af-accent-hover: #0f172a;
	--af-user-bg: #1e293b;
	--af-user-fg: #ffffff;
	--af-bot-bg: #f1f5f9;
	--af-bot-fg: #0f172a;
	--af-radius: 16px;
	--af-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25), 0 0 0 1px rgb(0 0 0 / 0.05);
	--af-vh: 100dvh;

	bottom: 24px;
	right: 24px;
	font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
	font-size: 14px;
	color: var(--af-fg);
	position: fixed;
	z-index: 2147483647; /* max 32-bit signed int; beat every page builder / popup plugin */
}

/* -----------------------------------------------------------------------------
   Toggle button
   ----------------------------------------------------------------------------- */
.bafha-af-chat__toggle {
	align-items: center;
	background: var(--af-accent);
	border: 0;
	border-radius: 50%;
	color: #fff;
	cursor: pointer;
	display: flex;
	height: 56px;
	justify-content: center;
	padding: 0;
	transition: background 0.2s ease, transform 0.2s ease;
	width: 56px;
	box-shadow: 0 10px 25px -5px rgb(0 0 0 / 0.2);
}

.bafha-af-chat__toggle:hover {
	filter: brightness(0.9);
	transform: translateY(-2px);
}

.bafha-af-chat__toggle svg {
	width: 26px;
	height: 26px;
}

.bafha-af-chat--open .bafha-af-chat__toggle {
	transform: scale(0.92);
}

/* -----------------------------------------------------------------------------
   Panel
   ----------------------------------------------------------------------------- */
.bafha-af-chat__panel {
	background: var(--af-bg);
	border: 1px solid var(--af-border);
	border-radius: var(--af-radius);
	bottom: 76px;
	box-shadow: var(--af-shadow);
	display: flex;
	flex-direction: column;
	height: min(620px, calc(100vh - 120px));
	overflow: hidden;
	position: absolute;
	right: 0;
	width: min(380px, calc(100vw - 48px));
}

.bafha-af-chat__panel[hidden] {
	display: none;
}

/* -----------------------------------------------------------------------------
   Header
   ----------------------------------------------------------------------------- */
.bafha-af-chat__header {
	align-items: center;
	background: var(--af-bg);
	border-bottom: 1px solid var(--af-border);
	display: flex;
	flex-shrink: 0;
	gap: 8px;
	justify-content: space-between;
	padding: 14px 16px;
}

.bafha-af-chat__title {
	color: var(--af-fg);
	font-size: 15px;
	font-weight: 600;
	letter-spacing: -0.01em;
	line-height: 1.2;
	margin: 0;
	min-width: 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.bafha-af-chat__subtitle {
	color: var(--af-muted);
	font-size: 12px;
	line-height: 1.3;
	margin-top: 2px;
}

.bafha-af-chat__header-actions {
	display: flex;
	gap: 4px;
}

.bafha-af-chat__icon-btn {
	align-items: center;
	background: transparent;
	border: 0;
	border-radius: 8px;
	color: var(--af-muted);
	cursor: pointer;
	display: flex;
	height: 32px;
	justify-content: center;
	padding: 0;
	transition: background 0.15s ease, color 0.15s ease;
	width: 32px;
}

.bafha-af-chat__icon-btn:hover {
	background: #f1f5f9;
	color: var(--af-fg);
}

.bafha-af-chat__icon-btn svg {
	width: 16px;
	height: 16px;
}

/* -----------------------------------------------------------------------------
   Body (shared scroll container)
   ----------------------------------------------------------------------------- */
.bafha-af-chat__body {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	overscroll-behavior: contain;
	-webkit-overflow-scrolling: touch;
	padding: 20px;
	scroll-behavior: smooth;
}

.bafha-af-chat__body::-webkit-scrollbar {
	width: 6px;
}
.bafha-af-chat__body::-webkit-scrollbar-thumb {
	background: #cbd5e1;
	border-radius: 10px;
}
.bafha-af-chat__body::-webkit-scrollbar-thumb:hover {
	background: #94a3b8;
}

/* -----------------------------------------------------------------------------
   Stage 1: Lead form (compact, placeholder-only)
   ----------------------------------------------------------------------------- */
.bafha-af-chat__sr {
	border: 0;
	clip: rect(0 0 0 0);
	height: 1px;
	margin: -1px;
	overflow: hidden;
	padding: 0;
	position: absolute;
	white-space: nowrap;
	width: 1px;
}

.bafha-af-chat__lead-intro {
	margin: 0 0 12px;
}

.bafha-af-chat__lead-title {
	color: var(--af-fg);
	font-size: 17px;
	font-weight: 700;
	letter-spacing: -0.02em;
	line-height: 1.25;
	margin: 0 0 4px;
}

.bafha-af-chat__lead-text {
	color: var(--af-muted);
	font-size: 13px;
	line-height: 1.45;
	margin: 0;
}

.bafha-af-chat__lead-form {
	display: flex;
	flex-direction: column;
	gap: 8px;
}

.bafha-af-chat__field {
	display: flex;
	flex-direction: column;
}

.bafha-af-chat__text-input {
	background: #f8fafc;
	border: 1px solid var(--af-border);
	border-radius: 10px;
	color: var(--af-fg);
	font-family: inherit;
	font-size: 16px; /* prevents iOS focus zoom */
	height: 42px;
	padding: 0 12px;
	transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
	width: 100%;
}

.bafha-af-chat__text-input::placeholder {
	color: #94a3b8;
}

.bafha-af-chat__text-input:focus {
	background: #fff;
	border-color: var(--af-accent);
	box-shadow: 0 0 0 3px rgb(30 41 59 / 0.1);
	box-shadow: 0 0 0 3px color-mix(in srgb, var(--af-accent) 15%, transparent);
	outline: none;
}

.bafha-af-chat__field--invalid .bafha-af-chat__text-input {
	border-color: #ef4444;
}

.bafha-af-chat__field-error {
	color: #ef4444;
	font-size: 11px;
	line-height: 1.3;
	min-height: 0;
	margin-top: 2px;
}

.bafha-af-chat__field-error:empty {
	display: none;
}

.bafha-af-chat__primary-btn {
	align-items: center;
	background: var(--af-accent);
	border: 0;
	border-radius: 10px;
	color: #fff;
	cursor: pointer;
	display: flex;
	font-family: inherit;
	font-size: 14px;
	font-weight: 600;
	gap: 8px;
	height: 44px;
	justify-content: center;
	margin-top: 4px;
	padding: 0 16px;
	transition: background 0.15s ease, transform 0.1s ease;
}

.bafha-af-chat__primary-btn:hover:not(:disabled) {
	filter: brightness(0.9);
}

.bafha-af-chat__primary-btn:active:not(:disabled) {
	transform: scale(0.98);
}

.bafha-af-chat__primary-btn:disabled {
	cursor: not-allowed;
	opacity: 0.6;
}

.bafha-af-chat__legal {
	color: var(--af-muted);
	font-size: 11px;
	line-height: 1.4;
	margin: 10px 0 0;
	text-align: center;
}

/* -----------------------------------------------------------------------------
   Stage 2: Chat messages
   ----------------------------------------------------------------------------- */
.bafha-af-chat__messages {
	display: flex;
	flex-direction: column;
	gap: 10px;
}

.bafha-af-chat__message {
	border-radius: 14px;
	font-size: 14px;
	line-height: 1.5;
	max-width: 85%;
	padding: 10px 14px;
	white-space: pre-wrap;
	word-wrap: break-word;
	animation: bafha-af-msg-in 0.2s ease-out;
}

@keyframes bafha-af-msg-in {
	from { opacity: 0; transform: translateY(6px); }
	to   { opacity: 1; transform: translateY(0); }
}

.bafha-af-chat__message--user {
	align-self: flex-end;
	background: var(--af-user-bg);
	border-bottom-right-radius: 4px;
	color: var(--af-user-fg);
}

.bafha-af-chat__message--bot {
	align-self: flex-start;
	background: var(--af-bot-bg);
	border-bottom-left-radius: 4px;
	color: var(--af-bot-fg);
}

.bafha-af-chat__message--typing {
	color: var(--af-muted);
	font-style: italic;
}

/* -----------------------------------------------------------------------------
   Chat input form
   ----------------------------------------------------------------------------- */
.bafha-af-chat__form {
	background: var(--af-bg);
	border-top: 1px solid var(--af-border);
	display: flex;
	flex-shrink: 0;
	gap: 8px;
	padding: 10px 12px;
	padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}

.bafha-af-chat__form[hidden] {
	display: none;
}

.bafha-af-chat__input {
	background: #f8fafc;
	border: 1px solid var(--af-border);
	border-radius: 22px;
	color: var(--af-fg);
	flex: 1;
	font-family: inherit;
	font-size: 16px;
	height: 44px;
	min-width: 0;
	padding: 0 16px;
	transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}

.bafha-af-chat__input:focus {
	background: #fff;
	border-color: var(--af-accent);
	box-shadow: 0 0 0 3px rgb(30 41 59 / 0.1);
	box-shadow: 0 0 0 3px color-mix(in srgb, var(--af-accent) 15%, transparent);
	outline: none;
}

.bafha-af-chat__send {
	align-items: center;
	background: var(--af-accent);
	border: 0;
	border-radius: 50%;
	color: #fff;
	cursor: pointer;
	display: flex;
	flex-shrink: 0;
	height: 44px;
	justify-content: center;
	padding: 0;
	transition: background 0.15s ease, transform 0.1s ease;
	width: 44px;
}

.bafha-af-chat__send:hover:not(:disabled) {
	filter: brightness(0.9);
}

.bafha-af-chat__send:active:not(:disabled) {
	transform: scale(0.95);
}

.bafha-af-chat__send svg {
	width: 18px;
	height: 18px;
}

.bafha-af-chat__send:disabled,
.bafha-af-chat__input:disabled {
	cursor: not-allowed;
	opacity: 0.6;
}

/* -----------------------------------------------------------------------------
   Footer (Powered by Bafha)
   ----------------------------------------------------------------------------- */
.bafha-af-chat__footer {
	background: var(--af-bg);
	border-top: 1px solid var(--af-border);
	color: var(--af-muted);
	flex-shrink: 0;
	font-size: 11px;
	line-height: 1.3;
	padding: 6px 12px;
	padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
	text-align: center;
}

.bafha-af-chat__powered-link {
	color: var(--af-muted);
	font-weight: 600;
	text-decoration: none;
}

.bafha-af-chat__powered-link:hover {
	color: var(--af-accent);
	text-decoration: underline;
}

/* When the footer is present, the form should not double-pad the bottom
   safe area — the footer already handles it. */
.bafha-af-chat__form {
	padding-bottom: 10px;
}

/* -----------------------------------------------------------------------------
   Mobile: fullscreen panel + keyboard avoidance
   ----------------------------------------------------------------------------- */
@media (max-width: 600px) {
	.bafha-af-chat {
		bottom: 16px;
		right: 16px;
	}

	/* Align panel with the visual viewport, but stay clear of the
	   notch/status-bar area (safe-area-inset-top) so the device's
	   tinted area shows the site's own theme color, not the panel
	   background. JS keeps --af-top / --af-vh synced with
	   visualViewport.offsetTop / visualViewport.height. */
	.bafha-af-chat__panel {
		border: 0;
		border-radius: 0;
		bottom: auto;
		/* Height equals visual viewport height minus the gap we lose by
		   pushing the panel down to clear the notch. The gap is
		   (max(--af-top, safe-area) - --af-top); the formula below
		   simplifies it so panel bottom always lands on visualViewport
		   bottom, leaving the notch area untouched. */
		height: calc(var(--af-vh, 100dvh) - max(var(--af-top, 0px), env(safe-area-inset-top, 0px)) + var(--af-top, 0px));
		left: 0;
		max-height: calc(var(--af-vh, 100dvh) - max(var(--af-top, 0px), env(safe-area-inset-top, 0px)) + var(--af-top, 0px));
		position: fixed;
		right: 0;
		top: max(var(--af-top, 0px), env(safe-area-inset-top, 0px));
		width: 100vw;
	}
}

@media (prefers-reduced-motion: reduce) {
	.bafha-af-chat *,
	.bafha-af-chat *::before,
	.bafha-af-chat *::after {
		animation: none !important;
		transition: none !important;
	}
}
