/* Desa AI chat widget. Theme via --desa-* variables. */
.desa-chat {
	--desa-primary: #2563eb;
	--desa-primary-hover: #1d4ed8;
	--desa-on-primary: #ffffff;
	--desa-bg: #ffffff;
	--desa-surface: #f3f4f6;
	--desa-text: #111827;
	--desa-muted: #6b7280;
	--desa-border: #e5e7eb;
	--desa-error-bg: #fef2f2;
	--desa-error: #b91c1c;
	--desa-radius: 16px;
	--desa-z: 99990;

	position: fixed;
	bottom: 20px;
	right: 20px;
	z-index: var(--desa-z);
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	font-size: 15px;
	line-height: 1.5;
	color: var(--desa-text);
	-webkit-font-smoothing: antialiased;
}
.desa-chat--left { right: auto; left: 20px; }
.desa-chat *, .desa-chat *::before, .desa-chat *::after { box-sizing: border-box; }
.desa-chat [hidden] { display: none !important; }
.desa-chat button { font: inherit; cursor: pointer; }
.desa-icon { display: inline-flex; line-height: 0; }

@media (prefers-color-scheme: dark) {
	.desa-chat {
		--desa-bg: #1f2937;
		--desa-surface: #374151;
		--desa-text: #f9fafb;
		--desa-muted: #9ca3af;
		--desa-border: #4b5563;
		--desa-error-bg: #451a1a;
		--desa-error: #fca5a5;
	}
}

/* Launcher: Desa's avatar in a ring of the brand color */
.desa-launcher {
	position: relative;
	display: block;
	width: 64px; height: 64px;
	margin-left: auto;
	padding: 3px;
	border: 0; border-radius: 50%;
	background: var(--desa-primary); color: var(--desa-on-primary);
	box-shadow: 0 8px 24px rgba(0, 0, 0, .22);
	transition: transform .15s ease, background-color .15s ease;
}
.desa-chat--left .desa-launcher { margin-left: 0; margin-right: auto; }
.desa-launcher:hover { background: var(--desa-primary-hover); transform: translateY(-2px) scale(1.03); }
.desa-launcher:focus-visible, .desa-chat :focus-visible { outline: 3px solid var(--desa-primary); outline-offset: 2px; }
.desa-launcher-avatar, .desa-launcher-avatar img, .desa-launcher-avatar .desa-icon {
	display: block; width: 100%; height: 100%; border-radius: 50%; object-fit: cover; background: #fff;
}
.desa-launcher-close { display: none; width: 100%; height: 100%; align-items: center; justify-content: center; }
.desa-launcher-close svg { width: 26px; height: 26px; }
.desa-launcher-badge {
	position: absolute; right: -2px; bottom: -2px;
	display: flex; align-items: center; justify-content: center;
	width: 24px; height: 24px; border-radius: 50%;
	background: var(--desa-primary); color: var(--desa-on-primary);
	border: 2px solid #fff;
}
.desa-launcher-badge svg { width: 13px; height: 13px; }
.desa-chat.is-open .desa-launcher-avatar, .desa-chat.is-open .desa-launcher-badge { display: none; }
.desa-chat.is-open .desa-launcher-close { display: flex; }
.desa-chat.has-teaser .desa-launcher::after {
	content: ""; position: absolute; inset: -4px; border-radius: 50%;
	border: 2px solid var(--desa-primary);
	animation: desa-pulse 1.6s ease-out 3;
	pointer-events: none;
}
@keyframes desa-pulse { from { opacity: .8; transform: scale(1); } to { opacity: 0; transform: scale(1.35); } }

/* Proactive teaser bubble */
.desa-teaser {
	position: absolute; bottom: 78px; right: 0;
	display: flex; align-items: flex-start; gap: 4px;
	width: max-content; max-width: min(300px, calc(100vw - 40px));
	padding: 12px 8px 12px 14px;
	background: var(--desa-bg); color: var(--desa-text);
	border: 1px solid var(--desa-border); border-radius: 16px 16px 4px 16px;
	box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
	animation: desa-teaser-in .35s cubic-bezier(.2, .9, .3, 1.2);
}
.desa-chat--left .desa-teaser { right: auto; left: 0; border-radius: 16px 16px 16px 4px; }
@keyframes desa-teaser-in { from { opacity: 0; transform: translateY(10px) scale(.9); } to { opacity: 1; transform: none; } }
.desa-teaser-body {
	display: flex; flex-direction: column; gap: 2px;
	padding: 0; border: 0; background: none; color: inherit;
	font-size: 14px; line-height: 1.4; text-align: left;
}
.desa-teaser-name { font-size: 13px; color: var(--desa-primary); }
.desa-teaser-close {
	flex: none; display: flex; align-items: center; justify-content: center;
	width: 24px; height: 24px; margin-top: -4px; padding: 0;
	border: 0; border-radius: 50%; background: transparent; color: var(--desa-muted);
}
.desa-teaser-close:hover { background: var(--desa-surface); color: var(--desa-text); }
.desa-teaser-close svg { width: 14px; height: 14px; }
.desa-chat.is-open .desa-teaser { display: none; }

/* Panel */
.desa-panel {
	display: flex; flex-direction: column;
	width: 380px; max-width: calc(100vw - 40px);
	height: 600px; max-height: calc(100vh - 110px);
	margin-bottom: 12px;
	background: var(--desa-bg);
	border: 1px solid var(--desa-border);
	border-radius: var(--desa-radius);
	box-shadow: 0 16px 48px rgba(0, 0, 0, .2);
	overflow: hidden;
	animation: desa-in .2s ease-out;
}
@keyframes desa-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }

.desa-header {
	display: flex; align-items: center; gap: 10px;
	padding: 12px 12px 12px 16px;
	background: var(--desa-primary); color: var(--desa-on-primary);
}
.desa-avatar {
	display: flex; align-items: center; justify-content: center;
	width: 40px; height: 40px; flex: none;
	border-radius: 50%; background: #fff; overflow: hidden;
	box-shadow: 0 0 0 2px rgba(255, 255, 255, .5);
}
.desa-avatar img { width: 100%; height: 100%; object-fit: cover; }
.desa-titles { flex: 1; min-width: 0; }
.desa-title { font-weight: 700; font-size: 16px; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.desa-subtitle { font-size: 12px; opacity: .85; }
.desa-header-btn {
	display: flex; align-items: center; justify-content: center;
	width: 36px; height: 36px; padding: 0;
	border: 0; border-radius: 8px;
	background: transparent; color: inherit;
}
.desa-header-btn:hover { background: rgba(255, 255, 255, .18); }
.desa-header-btn:focus-visible { outline-color: var(--desa-on-primary); outline-offset: -3px; }

.desa-badge { padding: 6px 12px; background: #fef3c7; color: #92400e; font-size: 12px; text-align: center; }
.desa-chat .desa-beta {
	display: inline-block; margin: 0 0 0 6px; padding: 1px 6px; vertical-align: 2px;
	border: 1px solid currentColor; border-radius: 6px;
	background: transparent; color: inherit;
	font-family: inherit; font-size: 9px; font-weight: 700; line-height: 1.4; letter-spacing: .06em; opacity: .9;
}
.desa-chat button.desa-beta { cursor: help; }
.desa-chat button.desa-beta:hover, .desa-chat button.desa-beta[aria-expanded="true"] { opacity: 1; background: rgba(255, 255, 255, .2); }
.desa-chat button.desa-beta:focus-visible { outline: 2px solid var(--desa-on-primary); outline-offset: 2px; }
.desa-panel { position: relative; }
.desa-beta-pop {
	position: absolute; top: 58px; left: 60px; right: 16px; z-index: 2;
	padding: 10px 12px;
	background: var(--desa-bg); color: var(--desa-text);
	border: 1px solid var(--desa-border); border-radius: 10px;
	box-shadow: 0 8px 24px rgba(0, 0, 0, .15);
	font-size: 12.5px; line-height: 1.45;
}
.desa-beta-pop::before {
	content: ""; position: absolute; top: -6px; left: 40px;
	width: 10px; height: 10px; transform: rotate(45deg);
	background: inherit; border-left: 1px solid var(--desa-border); border-top: 1px solid var(--desa-border);
}

/* Messages */
.desa-messages {
	flex: 1; overflow-y: auto; overscroll-behavior: contain;
	padding: 16px; display: flex; flex-direction: column; gap: 10px;
}
.desa-msg { display: flex; flex-direction: column; max-width: 88%; }
.desa-msg--user { align-self: flex-end; align-items: flex-end; }
.desa-msg--assistant, .desa-msg--error { align-self: flex-start; }
.desa-msg--assistant { flex-direction: row; align-items: flex-end; gap: 8px; max-width: 92%; }
.desa-msg-avatar { flex: none; width: 28px; height: 28px; border-radius: 50%; object-fit: cover; background: #fff; }
.desa-msg-body { display: flex; flex-direction: column; min-width: 0; }
.desa-bubble {
	padding: 10px 14px; border-radius: 14px;
	word-wrap: break-word; overflow-wrap: anywhere;
}
.desa-msg--user .desa-bubble { background: var(--desa-primary); color: var(--desa-on-primary); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.desa-msg--assistant .desa-bubble { background: var(--desa-surface); border-bottom-left-radius: 4px; }
.desa-msg--error .desa-bubble { background: var(--desa-error-bg); color: var(--desa-error); }
.desa-bubble p { margin: 0 0 8px; }
.desa-bubble p:last-child, .desa-bubble ul:last-child, .desa-bubble ol:last-child { margin-bottom: 0; }
.desa-bubble ul, .desa-bubble ol { margin: 0 0 8px; padding-left: 20px; }
.desa-bubble li { margin: 2px 0; }
.desa-bubble a { color: inherit; text-decoration: underline; }
.desa-bubble code { padding: 1px 4px; border-radius: 4px; background: rgba(0, 0, 0, .08); font-size: .9em; }
.desa-cite { font-size: .7em; margin-left: 1px; }
.desa-cite a { text-decoration: none; padding: 0 3px; border-radius: 4px; background: rgba(0, 0, 0, .1); }

.desa-sources { display: flex; flex-wrap: wrap; gap: 4px 8px; margin-top: 6px; font-size: 12px; color: var(--desa-muted); }
.desa-sources a { color: var(--desa-primary); text-decoration: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.desa-sources a:hover { text-decoration: underline; }
.desa-retry {
	align-self: flex-start; margin-top: 6px; padding: 4px 12px;
	border: 1px solid var(--desa-border); border-radius: 999px;
	background: var(--desa-bg); color: var(--desa-text); font-size: 13px;
}

.desa-typing .desa-bubble { display: flex; gap: 4px; padding: 14px; }
.desa-typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--desa-muted); animation: desa-dot 1.2s infinite ease-in-out; }
.desa-typing span:nth-child(2) { animation-delay: .15s; }
.desa-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes desa-dot { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

/* Suggestions */
.desa-suggestions { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 10px; }
.desa-chip {
	padding: 6px 12px;
	border: 1px solid var(--desa-primary); border-radius: 999px;
	background: transparent; color: var(--desa-primary);
	font-size: 13px; text-align: left;
}
.desa-chip:hover { background: var(--desa-primary); color: var(--desa-on-primary); }

/* Input */
.desa-form {
	display: flex; align-items: flex-end; gap: 8px;
	padding: 10px 12px; border-top: 1px solid var(--desa-border);
}
.desa-input {
	flex: 1; min-height: 42px; max-height: 140px; margin: 0;
	padding: 10px 12px;
	border: 1px solid var(--desa-border); border-radius: 12px;
	background: var(--desa-bg); color: var(--desa-text);
	font: inherit; line-height: 1.4; resize: none;
}
.desa-input:focus { outline: none; border-color: var(--desa-primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--desa-primary) 25%, transparent); }
.desa-send {
	display: flex; align-items: center; justify-content: center;
	width: 42px; height: 42px; flex: none; padding: 0;
	border: 0; border-radius: 12px;
	background: var(--desa-primary); color: var(--desa-on-primary);
}
.desa-send:hover { background: var(--desa-primary-hover); }
.desa-send:disabled { opacity: .5; cursor: not-allowed; }

/* Contact actions */
.desa-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 8px; }
.desa-action {
	display: inline-flex; align-items: center; gap: 6px;
	padding: 6px 10px; border-radius: 999px;
	background: var(--desa-surface); color: var(--desa-text);
	font-size: 13px; font-weight: 500; text-decoration: none;
}
.desa-action:hover { background: var(--desa-border); }
.desa-action .desa-icon svg { width: 16px; height: 16px; }
.desa-action--whatsapp .desa-icon { color: #25d366; }
.desa-actions--inline { padding: 0; margin-top: 8px; }
.desa-actions--inline .desa-action { background: var(--desa-bg); border: 1px solid var(--desa-border); }
.desa-actions--inline .desa-action:hover { border-color: var(--desa-primary); color: var(--desa-primary); }
.desa-action--map .desa-icon { color: #ea4335; }

/* Lead form */
.desa-lead {
	display: flex; flex-direction: column; gap: 8px;
	margin-top: 8px; padding: 12px;
	background: var(--desa-bg); border: 1px solid var(--desa-border); border-radius: 12px;
	font-size: 14px;
}
.desa-lead-title { margin: 0; font-weight: 600; }
.desa-lead-field { display: flex; flex-direction: column; gap: 3px; font-size: 13px; color: var(--desa-muted); }
.desa-lead input:not([type="radio"]):not([type="checkbox"]), .desa-lead textarea {
	width: 100%; margin: 0; padding: 8px 10px;
	border: 1px solid var(--desa-border); border-radius: 8px;
	background: var(--desa-bg); color: var(--desa-text); font: inherit; font-size: 14px;
}
.desa-lead input:focus, .desa-lead textarea:focus { outline: none; border-color: var(--desa-primary); }
.desa-lead-methods { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 0; padding: 0; border: 0; min-width: 0; }
.desa-lead-methods legend { width: 100%; margin-bottom: 4px; padding: 0; font-size: 13px; color: var(--desa-muted); }
.desa-lead-method { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.desa-lead-consent { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 4px 6px; font-size: 12px; color: var(--desa-muted); }
.desa-lead-consent input { margin: 2px 0 0; }
.desa-lead-consent span { flex: 1; min-width: 70%; }
.desa-lead-consent a { color: var(--desa-primary); }
.desa-lead-error { margin: 0; color: var(--desa-error); font-size: 13px; }
.desa-lead-error:empty { display: none; }
.desa-lead-buttons { display: flex; gap: 8px; align-items: center; }
.desa-lead-send {
	padding: 8px 16px; border: 0; border-radius: 999px;
	background: var(--desa-primary); color: var(--desa-on-primary); font-weight: 600;
}
.desa-lead-send:hover { background: var(--desa-primary-hover); }
.desa-lead-send:disabled { opacity: .6; }
.desa-lead-no { padding: 8px 4px; border: 0; background: none; color: var(--desa-muted); text-decoration: underline; }
.desa-hp { position: absolute !important; left: -9999px !important; width: 1px; height: 1px; opacity: 0; }

.desa-disclaimer { padding: 0 12px 8px; font-size: 11px; color: var(--desa-muted); text-align: center; }

/* Mobile: full screen */
@media (max-width: 480px) {
	.desa-chat { bottom: 12px; right: 12px; }
	.desa-chat--left { left: 12px; }
	.desa-chat.is-open { inset: 0; }
	.desa-chat.is-open .desa-launcher { display: none; }
	.desa-launcher { width: 58px; height: 58px; }
	.desa-teaser { bottom: 70px; }
	.desa-panel { position: fixed; inset: 0; width: 100%; max-width: none; height: 100%; max-height: none; margin: 0; border-radius: 0; border: 0; }
	.desa-input { font-size: 16px; } /* Prevents iOS zoom on focus. */
}

@media (prefers-reduced-motion: reduce) {
	.desa-panel, .desa-typing span, .desa-launcher, .desa-teaser { animation: none; transition: none; }
	.desa-chat.has-teaser .desa-launcher::after { display: none; }
}

@media print {
	.desa-chat { display: none; }
}

/* Privacy controls stay within the widget color and focus system. */
#desa-chat .desa-privacy { text-align: left; max-height: 120px; overflow-y: auto; }
#desa-chat .desa-privacy a { color: inherit; text-decoration: underline; }
#desa-chat .desa-erase { display: block; margin: 6px auto 0; border: 0; background: transparent; color: inherit; cursor: pointer; text-decoration: underline; font: inherit; }
#desa-chat .desa-erase:disabled { opacity: .5; cursor: default; }
