/* AI Contact Agent — frontend (form + WhatsApp-style chat) v1.0.1 */

.aca-form-wrap {
	--aca-bg: #0a1220;
	--aca-surface: #0f1a2b;
	--aca-border: rgba(148, 163, 184, 0.14);
	--aca-text: #f1f5f9;
	--aca-muted: #94a3b8;
	--aca-accent: #5b9fd4;
	--aca-accent-2: #c9a962;
	--aca-ok: #34d399;
	--aca-err: #f87171;
	--aca-r: 12px;
	margin: 2.5rem 0;
	font-family: "DM Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
	color: var(--aca-text);
}

.aca-form-card {
	background: linear-gradient(180deg, rgba(15, 26, 43, 0.95), rgba(10, 18, 32, 0.98));
	border: 1px solid var(--aca-border);
	border-radius: 16px;
	padding: clamp(1.25rem, 3vw, 2rem);
	box-shadow: 0 24px 64px rgba(0, 0, 0, 0.35);
	position: relative;
	overflow: hidden;
}

.aca-form-card::before {
	content: "";
	position: absolute;
	inset: 0 0 auto 0;
	height: 3px;
	background: linear-gradient(90deg, var(--aca-accent), var(--aca-accent-2));
}

.aca-form-title {
	margin: 0 0 0.35rem;
	font-size: clamp(1.35rem, 2.5vw, 1.75rem);
	font-weight: 600;
	letter-spacing: -0.02em;
}

.aca-form-sub {
	margin: 0 0 1.5rem;
	color: var(--aca-muted);
	font-size: 0.95rem;
	line-height: 1.55;
}

.aca-form-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 1rem 1.1rem;
}

.aca-field-full { grid-column: 1 / -1; }

.aca-field label {
	display: block;
	font-size: 0.78rem;
	letter-spacing: 0.04em;
	text-transform: uppercase;
	color: var(--aca-muted);
	margin-bottom: 0.4rem;
}

.aca-field input,
.aca-field select,
.aca-field textarea {
	width: 100%;
	background: rgba(5, 10, 18, 0.65);
	border: 1px solid var(--aca-border);
	border-radius: 8px;
	padding: 0.72rem 0.9rem;
	color: var(--aca-text);
	outline: none;
	transition: border-color .2s, box-shadow .2s;
}

.aca-field input:focus,
.aca-field select:focus,
.aca-field textarea:focus {
	border-color: rgba(91, 159, 212, 0.55);
	box-shadow: 0 0 0 3px rgba(91, 159, 212, 0.15);
}

.aca-field textarea { resize: vertical; min-height: 120px; }

.aca-schedule-box {
	background: rgba(91, 159, 212, 0.06);
	border: 1px dashed rgba(91, 159, 212, 0.25);
	border-radius: 10px;
	padding: 1rem;
}

.aca-check {
	display: flex !important;
	align-items: center;
	gap: 0.55rem;
	text-transform: none !important;
	letter-spacing: 0 !important;
	font-size: 0.92rem !important;
	color: var(--aca-text) !important;
	cursor: pointer;
}

.aca-schedule-fields { margin-top: 0.9rem; }

.aca-form-footer {
	margin-top: 1.35rem;
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 1rem;
}

.aca-btn-submit {
	appearance: none;
	border: 0;
	cursor: pointer;
	background: linear-gradient(135deg, #3b82f6, #5b9fd4 55%, #7dd3fc);
	color: #041018;
	font-weight: 600;
	padding: 0.85rem 1.5rem;
	border-radius: 999px;
	font-size: 0.95rem;
	letter-spacing: 0.01em;
	transition: transform .15s, filter .15s, opacity .15s;
}

.aca-btn-submit:hover { filter: brightness(1.05); transform: translateY(-1px); }
.aca-btn-submit:disabled { opacity: 0.65; cursor: wait; transform: none; }

.aca-form-note {
	margin: 0;
	font-size: 0.78rem;
	color: var(--aca-muted);
	max-width: 28rem;
}

.aca-form-alert {
	margin-top: 1rem;
	padding: 0.85rem 1rem;
	border-radius: 8px;
	font-size: 0.92rem;
}

.aca-form-alert.is-ok {
	background: rgba(52, 211, 153, 0.12);
	border: 1px solid rgba(52, 211, 153, 0.35);
	color: #a7f3d0;
}

.aca-form-alert.is-err {
	background: rgba(248, 113, 113, 0.12);
	border: 1px solid rgba(248, 113, 113, 0.35);
	color: #fecaca;
}

.aca-hp {
	position: absolute !important;
	left: -9999px !important;
	width: 1px; height: 1px;
	overflow: hidden;
}

@media (max-width: 640px) {
	.aca-form-grid { grid-template-columns: 1fr; }
}

/* ——— Chat widget ———
   hidden attribute must win over display:flex (critical bug fix) */
.aca-chat-root [hidden],
.aca-chat-root[hidden],
#aca-chat-panel[hidden],
#aca-chat-typing[hidden],
#aca-chat-messages[hidden],
#aca-chat-input[hidden],
#aca-chat-prefill[hidden] {
	display: none !important;
}

.aca-chat-root {
	--aca-wa: #25D366;
	--aca-accent: #5b9fd4;
	--aca-fab-size: 56px;
	--aca-panel-w: min(360px, calc(100vw - 2rem));
	--aca-panel-h: min(520px, calc(100dvh - 6.5rem));
	position: fixed;
	bottom: max(1rem, env(safe-area-inset-bottom, 0px));
	z-index: 99999;
	font-family: "DM Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
	font-size: 15px;
	line-height: 1.45;
	pointer-events: none; /* só FAB/painel capturam clique */
}

.aca-chat-root.aca-pos-right { right: max(1rem, env(safe-area-inset-right, 0px)); }
.aca-chat-root.aca-pos-left { left: max(1rem, env(safe-area-inset-left, 0px)); }

.aca-chat-root .aca-chat-fab,
.aca-chat-root .aca-chat-panel,
.aca-chat-root .aca-chat-backdrop {
	pointer-events: auto;
}

/* Backdrop (mobile / desktop quando aberto) */
.aca-chat-backdrop {
	display: none;
	position: fixed;
	inset: 0;
	background: rgba(2, 6, 14, 0.45);
	backdrop-filter: blur(2px);
	z-index: -1;
	border: 0;
	padding: 0;
	cursor: pointer;
}

.aca-chat-root.is-open .aca-chat-backdrop {
	display: block;
}

.aca-chat-fab {
	width: var(--aca-fab-size);
	height: var(--aca-fab-size);
	border-radius: 50%;
	border: 0;
	cursor: pointer;
	background: var(--aca-wa);
	color: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 8px 24px rgba(37, 211, 102, 0.4), 0 2px 8px rgba(0,0,0,0.2);
	position: relative;
	transition: transform .2s cubic-bezier(0.22, 1, 0.36, 1), box-shadow .2s, background .2s;
	z-index: 2;
}

.aca-chat-fab:hover { transform: scale(1.05); }
.aca-chat-fab:focus-visible {
	outline: 2px solid #fff;
	outline-offset: 3px;
}

.aca-chat-root.is-open .aca-chat-fab {
	background: #1f2937;
	box-shadow: 0 6px 18px rgba(0,0,0,0.3);
}

.aca-fab-icon { display: flex; align-items: center; justify-content: center; }
.aca-fab-close { display: none; }
.aca-chat-root.is-open .aca-fab-wa { display: none; }
.aca-chat-root.is-open .aca-fab-close { display: flex; }

.aca-fab-pulse {
	position: absolute;
	inset: -3px;
	border-radius: 50%;
	border: 2px solid var(--aca-wa);
	animation: aca-pulse 2.4s ease-out infinite;
	pointer-events: none;
	opacity: 0.55;
}
.aca-chat-root.is-open .aca-fab-pulse { display: none; }

@keyframes aca-pulse {
	0% { transform: scale(0.95); opacity: 0.65; }
	100% { transform: scale(1.3); opacity: 0; }
}

/* Painel — fechado por padrão via [hidden] + !important acima */
.aca-chat-panel {
	position: absolute;
	bottom: calc(var(--aca-fab-size) + 12px);
	width: var(--aca-panel-w);
	height: var(--aca-panel-h);
	max-width: 100vw;
	max-height: calc(100dvh - 5rem);
	background: #0b141a;
	border-radius: 16px;
	overflow: hidden;
	flex-direction: column;
	box-shadow: 0 20px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.06);
	transform-origin: bottom right;
	z-index: 1;
}

/* Só quando aberto e não hidden */
.aca-chat-root.is-open .aca-chat-panel:not([hidden]) {
	display: flex;
	animation: aca-panel-in .2s cubic-bezier(0.22, 1, 0.36, 1);
}

.aca-pos-left .aca-chat-panel { transform-origin: bottom left; left: 0; right: auto; }
.aca-pos-right .aca-chat-panel { right: 0; left: auto; }

@keyframes aca-panel-in {
	from { opacity: 0; transform: translateY(10px) scale(0.97); }
	to { opacity: 1; transform: translateY(0) scale(1); }
}

.aca-chat-header {
	display: flex;
	align-items: center;
	gap: 0.65rem;
	padding: 0.75rem 0.85rem;
	background: linear-gradient(135deg, #075e54, #128c7e 55%, var(--aca-accent));
	color: #fff;
	flex-shrink: 0;
}

.aca-chat-avatar {
	width: 36px;
	height: 36px;
	border-radius: 50%;
	background: rgba(255,255,255,0.18);
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}

.aca-chat-head-text { min-width: 0; flex: 1; }
.aca-chat-title {
	font-weight: 600;
	font-size: 0.95rem;
	line-height: 1.2;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
.aca-chat-status {
	font-size: 0.7rem;
	opacity: 0.92;
	display: flex;
	align-items: center;
	gap: 0.3rem;
	margin-top: 2px;
}
.aca-chat-status.is-typing-label .aca-status-text::after {
	content: " · digitando…";
	opacity: 0.9;
}
.aca-dot {
	width: 7px; height: 7px; border-radius: 50%;
	background: #4ade80;
	box-shadow: 0 0 0 2px rgba(74, 222, 128, 0.25);
	flex-shrink: 0;
}

.aca-chat-minimize {
	margin-left: 0.25rem;
	background: rgba(255,255,255,0.12);
	border: 0;
	color: #fff;
	width: 32px;
	height: 32px;
	border-radius: 50%;
	font-size: 1.25rem;
	line-height: 1;
	cursor: pointer;
	opacity: 0.95;
	flex-shrink: 0;
	display: flex;
	align-items: center;
	justify-content: center;
}
.aca-chat-minimize:hover { background: rgba(255,255,255,0.22); }

.aca-chat-prefill {
	padding: 1rem;
	background: #0b141a;
	flex: 1;
	overflow: auto;
	min-height: 0;
}

.aca-prefill-lead {
	color: #cbd5e1;
	font-size: 0.9rem;
	line-height: 1.5;
	margin: 0 0 0.9rem;
}

.aca-prefill-fields {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	margin-bottom: 0.85rem;
}

.aca-prefill-fields input {
	background: #1a2632;
	border: 1px solid rgba(148, 163, 184, 0.15);
	border-radius: 8px;
	padding: 0.65rem 0.8rem;
	color: #f1f5f9;
	outline: none;
	font-size: 0.92rem;
	width: 100%;
	box-sizing: border-box;
}

.aca-chat-start {
	width: 100%;
	border: 0;
	border-radius: 999px;
	padding: 0.75rem 1rem;
	background: var(--aca-wa);
	color: #fff;
	font-weight: 600;
	cursor: pointer;
	font-size: 0.92rem;
}
.aca-chat-start:hover { filter: brightness(1.05); }

.aca-chat-messages {
	flex: 1;
	overflow-y: auto;
	overflow-x: hidden;
	padding: 0.85rem 0.75rem;
	background:
		radial-gradient(ellipse at top, rgba(18, 140, 126, 0.08), transparent 50%),
		#0b141a;
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	min-height: 0;
	-webkit-overflow-scrolling: touch;
}

.aca-msg {
	max-width: 85%;
	padding: 0.55rem 0.75rem 0.4rem;
	border-radius: 10px;
	font-size: 0.88rem;
	line-height: 1.5;
	position: relative;
	word-wrap: break-word;
	overflow-wrap: anywhere;
}

.aca-msg-user {
	align-self: flex-end;
	background: #005c4b;
	color: #e7fffa;
	border-bottom-right-radius: 3px;
	white-space: pre-wrap;
}

.aca-msg-bot {
	align-self: flex-start;
	background: #1f2c34;
	color: #e2e8f0;
	border-bottom-left-radius: 3px;
	white-space: normal;
}

.aca-msg-body { display: block; }

/* Markdown formatado nas mensagens do bot */
.aca-msg-bot .aca-md-p {
	margin: 0 0 0.4em;
}
.aca-msg-bot .aca-md-p:last-child { margin-bottom: 0; }
.aca-msg-bot .aca-md-br {
	height: 0.45em;
}
.aca-msg-bot .aca-md-h {
	font-weight: 700;
	margin: 0.35em 0 0.3em;
	line-height: 1.3;
}
.aca-msg-bot .aca-md-h1 { font-size: 1.08em; }
.aca-msg-bot .aca-md-h2 { font-size: 1.02em; }
.aca-msg-bot .aca-md-h3 { font-size: 0.98em; opacity: 0.95; }
.aca-msg-bot .aca-md-list {
	margin: 0.25em 0 0.45em;
	padding-left: 1.25em;
}
.aca-msg-bot ul.aca-md-list { list-style: disc; }
.aca-msg-bot ol.aca-md-list { list-style: decimal; }
.aca-msg-bot .aca-md-list li {
	margin: 0.15em 0;
	padding-left: 0.15em;
}
.aca-msg-bot strong {
	font-weight: 700;
	color: #f8fafc;
}
.aca-msg-bot em {
	font-style: italic;
	opacity: 0.95;
}
.aca-msg-bot .aca-md-code {
	font-family: ui-monospace, "IBM Plex Mono", Consolas, monospace;
	font-size: 0.86em;
	background: rgba(0,0,0,0.35);
	padding: 0.1em 0.35em;
	border-radius: 4px;
}
.aca-msg-bot .aca-md-link,
.aca-msg-user .aca-md-link {
	color: #7dd3fc;
	text-decoration: underline;
	word-break: break-all;
}
.aca-msg-user .aca-md-link { color: #a5f3fc; }

.aca-msg-time {
	display: block;
	font-size: 0.62rem;
	opacity: 0.5;
	margin-top: 0.25rem;
	text-align: right;
}

/* Typing — só visível sem [hidden] e com classe is-active */
.aca-chat-typing {
	display: none;
	gap: 4px;
	padding: 0.25rem 1rem 0.55rem;
	background: #0b141a;
	align-items: center;
	flex-shrink: 0;
}

.aca-chat-typing.is-active:not([hidden]) {
	display: flex;
}

.aca-chat-typing span {
	width: 6px; height: 6px;
	border-radius: 50%;
	background: #64748b;
	animation: aca-bounce 1.2s infinite ease-in-out;
}
.aca-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.aca-chat-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes aca-bounce {
	0%, 80%, 100% { transform: translateY(0); opacity: 0.45; }
	40% { transform: translateY(-3px); opacity: 1; }
}

.aca-chat-input {
	display: flex;
	align-items: flex-end;
	gap: 0.4rem;
	padding: 0.5rem 0.55rem;
	background: #1f2c34;
	border-top: 1px solid rgba(255,255,255,0.04);
	flex-shrink: 0;
}

.aca-chat-input textarea {
	flex: 1;
	resize: none;
	max-height: 88px;
	min-height: 38px;
	border: 0;
	border-radius: 18px;
	padding: 0.55rem 0.85rem;
	background: #2a3942;
	color: #f1f5f9;
	outline: none;
	font: inherit;
	font-size: 0.9rem;
	line-height: 1.35;
	width: 100%;
	box-sizing: border-box;
}

.aca-chat-send {
	width: 38px;
	height: 38px;
	border-radius: 50%;
	border: 0;
	background: var(--aca-wa);
	color: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
	cursor: pointer;
	flex-shrink: 0;
	transition: filter .15s, opacity .15s;
}
.aca-chat-send:disabled { opacity: 0.45; cursor: not-allowed; }
.aca-chat-send:hover:not(:disabled) { filter: brightness(1.08); }

.aca-chat-footer {
	text-align: center;
	font-size: 0.62rem;
	color: #64748b;
	padding: 0.3rem 0.5rem 0.45rem;
	background: #0b141a;
	flex-shrink: 0;
}

/* Desktop médio */
@media (min-width: 768px) {
	.aca-chat-root {
		--aca-fab-size: 58px;
		--aca-panel-w: 380px;
		--aca-panel-h: min(560px, calc(100dvh - 7rem));
	}
	.aca-chat-root.is-open .aca-chat-backdrop {
		display: none; /* sem overlay no desktop */
	}
}

/* Trava scroll da página quando o chat está aberto (mobile) */
html.aca-chat-open,
body.aca-chat-open {
	overflow: hidden !important;
	height: 100% !important;
	touch-action: none;
}
body.aca-chat-open {
	/* iOS: evita bounce da página atrás do chat */
	position: fixed;
	width: 100%;
	left: 0;
	right: 0;
}

/* Mobile: tela inteira até fechar */
@media (max-width: 768px) {
	.aca-chat-root {
		--aca-fab-size: 54px;
		bottom: max(0.75rem, env(safe-area-inset-bottom, 0px));
	}
	.aca-chat-root.aca-pos-right { right: max(0.75rem, env(safe-area-inset-right, 0px)); }
	.aca-chat-root.aca-pos-left { left: max(0.75rem, env(safe-area-inset-left, 0px)); }

	/* Root ocupa a tela inteira quando aberto */
	.aca-chat-root.is-open {
		inset: 0;
		right: 0 !important;
		left: 0 !important;
		bottom: 0 !important;
		top: 0 !important;
		width: 100%;
		height: 100%;
		height: 100dvh;
		pointer-events: auto;
		z-index: 2147483000;
	}

	.aca-chat-root.is-open .aca-chat-backdrop {
		display: none; /* painel já é fullscreen */
	}

	/* Esconde o FAB enquanto o painel está aberto — fecha pelo × do header */
	.aca-chat-root.is-open .aca-chat-fab {
		display: none !important;
	}

	.aca-chat-root.is-open .aca-chat-panel:not([hidden]),
	.aca-chat-panel {
		position: fixed !important;
		inset: 0 !important;
		left: 0 !important;
		right: 0 !important;
		top: 0 !important;
		bottom: 0 !important;
		width: 100% !important;
		height: 100% !important;
		height: 100dvh !important;
		max-width: 100vw !important;
		max-height: 100dvh !important;
		border-radius: 0 !important;
		display: flex;
		flex-direction: column;
		box-shadow: none;
	}

	/* Só mostra o painel se estiver aberto */
	.aca-chat-root:not(.is-open) .aca-chat-panel {
		display: none !important;
	}

	.aca-chat-root.is-open .aca-chat-panel:not([hidden]) {
		display: flex !important;
	}

	.aca-chat-header {
		padding: 0.85rem 1rem;
		padding-top: max(0.85rem, env(safe-area-inset-top, 0px));
		flex-shrink: 0;
	}

	.aca-chat-minimize {
		width: 40px;
		height: 40px;
		font-size: 1.5rem;
	}

	.aca-chat-messages {
		flex: 1 1 auto;
		min-height: 0;
		overflow-y: auto;
		-webkit-overflow-scrolling: touch;
		overscroll-behavior: contain;
		padding: 1rem 0.9rem;
		touch-action: pan-y;
	}

	.aca-chat-prefill {
		flex: 1 1 auto;
		min-height: 0;
		overflow-y: auto;
		-webkit-overflow-scrolling: touch;
		padding: 1.1rem;
		padding-bottom: 1.5rem;
	}

	.aca-chat-input {
		flex-shrink: 0;
		padding: 0.65rem 0.75rem;
		padding-bottom: max(0.65rem, env(safe-area-inset-bottom, 0px));
	}

	.aca-chat-footer {
		flex-shrink: 0;
		padding-bottom: max(0.35rem, env(safe-area-inset-bottom, 0px));
	}

	.aca-msg { max-width: 92%; font-size: 0.95rem; }
}

/* Telas muito baixas (ainda fullscreen no mobile) */
@media (max-width: 768px) and (max-height: 500px) {
	.aca-chat-footer { display: none; }
	.aca-chat-header { padding: 0.5rem 0.75rem; }
}

/* Prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
	.aca-fab-pulse,
	.aca-chat-typing span,
	.aca-chat-root.is-open .aca-chat-panel:not([hidden]) {
		animation: none !important;
	}
}
