/**
 * ThemeFlex AI Chat Widget — v4.0
 * Vanilla CSS, no framework dependencies.
 * Mobile-first, Lighthouse-safe (no render-blocking).
 */

/* ===================================================================
   CSS Custom Properties (inherit from ThemeFlex design system)
   =================================================================== */
:root {
	--tf-chat-accent:      var(--tf-primary, #4f46e5);
	--tf-chat-accent-dark: var(--tf-primary-dark, #3730a3);
	--tf-chat-radius:      1rem;
	--tf-chat-shadow:      0 8px 32px rgba(0,0,0,.18);
	--tf-chat-width:       360px;
	--tf-chat-height:      520px;
	--tf-chat-z:           9999;
	--tf-chat-font:        var(--tf-font-body, system-ui, -apple-system, sans-serif);
	--tf-chat-size:        0.9rem;
	--tf-chat-bg:          #ffffff;
	--tf-chat-surface:     #f7f7f8;
	--tf-chat-border:      #e5e7eb;
	--tf-chat-text:        #111827;
	--tf-chat-muted:       #6b7280;
	--tf-chat-ai-bg:       #f0f0ff;
	--tf-chat-user-bg:     var(--tf-chat-accent);
	--tf-chat-user-text:   #ffffff;
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
	:root {
		--tf-chat-bg:      #1c1c1e;
		--tf-chat-surface: #2c2c2e;
		--tf-chat-border:  #3a3a3c;
		--tf-chat-text:    #f5f5f7;
		--tf-chat-muted:   #aeaeb2;
		--tf-chat-ai-bg:   #2a2a4a;
	}
}

/* ===================================================================
   Toggle Button (bottom-right FAB)
   =================================================================== */
.tf-chat-toggle {
	position: fixed;
	bottom: 1.5rem;
	right: 1.5rem;
	z-index: var(--tf-chat-z);
	width: 3.25rem;
	height: 3.25rem;
	border-radius: 50%;
	background: var(--tf-chat-accent);
	color: #fff;
	border: none;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: var(--tf-chat-shadow);
	transition: transform 0.2s ease, background 0.2s ease;
	outline-offset: 3px;
}

.tf-chat-toggle:hover {
	background: var(--tf-chat-accent-dark);
	transform: scale(1.08);
}

.tf-chat-toggle:focus-visible {
	outline: 2px solid var(--tf-chat-accent);
}

.tf-chat-toggle svg {
	width: 1.5rem;
	height: 1.5rem;
	pointer-events: none;
}

/* Hide close icon by default, show when open */
.tf-chat-toggle .tf-icon-close { display: none; }
.tf-chat-toggle .tf-icon-chat  { display: block; }
.tf-chat--open .tf-chat-toggle .tf-icon-close { display: block; }
.tf-chat--open .tf-chat-toggle .tf-icon-chat  { display: none; }

/* ===================================================================
   Chat Window
   =================================================================== */
.tf-chat-window {
	position: fixed;
	bottom: 5.5rem;
	right: 1.5rem;
	z-index: var(--tf-chat-z);
	width: var(--tf-chat-width);
	max-height: var(--tf-chat-height);
	background: var(--tf-chat-bg);
	border-radius: var(--tf-chat-radius);
	box-shadow: var(--tf-chat-shadow);
	border: 1px solid var(--tf-chat-border);
	display: flex;
	flex-direction: column;
	overflow: hidden;
	font-family: var(--tf-chat-font);
	font-size: var(--tf-chat-size);
	color: var(--tf-chat-text);

	/* Hidden by default */
	opacity: 0;
	transform: translateY(12px) scale(0.97);
	pointer-events: none;
	transition: opacity 0.22s ease, transform 0.22s ease;
}

.tf-chat--open .tf-chat-window {
	opacity: 1;
	transform: translateY(0) scale(1);
	pointer-events: auto;
}

/* Mobile: full-width bottom sheet */
@media (max-width: 480px) {
	.tf-chat-window {
		bottom: 0;
		right: 0;
		left: 0;
		width: 100%;
		border-radius: var(--tf-chat-radius) var(--tf-chat-radius) 0 0;
		max-height: 80dvh;
	}

	.tf-chat-toggle {
		bottom: 1rem;
		right: 1rem;
	}
}

/* ===================================================================
   Header
   =================================================================== */
.tf-chat-header {
	display: flex;
	align-items: center;
	gap: 0.625rem;
	padding: 0.875rem 1rem;
	background: var(--tf-chat-accent);
	color: #fff;
	flex-shrink: 0;
}

.tf-chat-header-avatar {
	width: 2rem;
	height: 2rem;
	border-radius: 50%;
	background: rgba(255,255,255,.25);
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}

.tf-chat-header-avatar svg {
	width: 1.125rem;
	height: 1.125rem;
}

.tf-chat-header-title {
	font-weight: 600;
	font-size: 0.95rem;
	line-height: 1.2;
}

.tf-chat-header-status {
	font-size: 0.75rem;
	opacity: 0.85;
}

/* ===================================================================
   Messages area
   =================================================================== */
.tf-chat-messages {
	flex: 1;
	overflow-y: auto;
	padding: 1rem;
	display: flex;
	flex-direction: column;
	gap: 0.75rem;
	scroll-behavior: smooth;
}

.tf-chat-messages::-webkit-scrollbar { width: 4px; }
.tf-chat-messages::-webkit-scrollbar-track { background: transparent; }
.tf-chat-messages::-webkit-scrollbar-thumb { background: var(--tf-chat-border); border-radius: 2px; }

/* Message bubbles */
.tf-msg {
	max-width: 85%;
	line-height: 1.5;
	word-break: break-word;
}

.tf-msg-ai {
	align-self: flex-start;
	background: var(--tf-chat-ai-bg);
	border-radius: 0 var(--tf-chat-radius) var(--tf-chat-radius) var(--tf-chat-radius);
	padding: 0.6rem 0.875rem;
	color: var(--tf-chat-text);
}

.tf-msg-user {
	align-self: flex-end;
	background: var(--tf-chat-user-bg);
	border-radius: var(--tf-chat-radius) 0 var(--tf-chat-radius) var(--tf-chat-radius);
	padding: 0.6rem 0.875rem;
	color: var(--tf-chat-user-text);
}

/* Typing indicator */
.tf-msg-typing {
	display: flex;
	align-items: center;
	gap: 4px;
	padding: 0.75rem 1rem;
}

.tf-msg-typing span {
	display: block;
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--tf-chat-muted);
	animation: tf-bounce 1.2s infinite ease-in-out;
}

.tf-msg-typing span:nth-child(2) { animation-delay: 0.2s; }
.tf-msg-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes tf-bounce {
	0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
	40%            { transform: scale(1.2); opacity: 1; }
}

/* Contact escalation link */
.tf-msg-escalate {
	font-size: 0.8rem;
	margin-top: 0.35rem;
}

.tf-msg-escalate a {
	color: var(--tf-chat-accent);
	text-decoration: underline;
}

/* Consent notice */
.tf-chat-consent {
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;
	padding: 1.5rem 1rem;
	gap: 0.75rem;
	color: var(--tf-chat-muted);
	font-size: 0.85rem;
}

.tf-chat-consent svg {
	width: 2rem;
	height: 2rem;
	opacity: 0.5;
}

.tf-consent-btn {
	background: var(--tf-chat-accent);
	color: #fff;
	border: none;
	border-radius: 0.5rem;
	padding: 0.5rem 1rem;
	cursor: pointer;
	font-size: 0.875rem;
	font-weight: 500;
	transition: background 0.2s;
}

.tf-consent-btn:hover {
	background: var(--tf-chat-accent-dark);
}

/* ===================================================================
   Input area
   =================================================================== */
.tf-chat-input-area {
	border-top: 1px solid var(--tf-chat-border);
	padding: 0.75rem;
	display: flex;
	gap: 0.5rem;
	background: var(--tf-chat-bg);
	flex-shrink: 0;
}

.tf-chat-input {
	flex: 1;
	border: 1px solid var(--tf-chat-border);
	border-radius: 0.5rem;
	padding: 0.55rem 0.75rem;
	font-family: var(--tf-chat-font);
	font-size: var(--tf-chat-size);
	background: var(--tf-chat-surface);
	color: var(--tf-chat-text);
	resize: none;
	min-height: 2.4rem;
	max-height: 6rem;
	line-height: 1.4;
	outline: none;
	transition: border-color 0.15s;
}

.tf-chat-input:focus {
	border-color: var(--tf-chat-accent);
}

.tf-chat-input::placeholder {
	color: var(--tf-chat-muted);
}

.tf-chat-send {
	width: 2.5rem;
	height: 2.5rem;
	border-radius: 0.5rem;
	background: var(--tf-chat-accent);
	color: #fff;
	border: none;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	transition: background 0.2s, opacity 0.2s;
	align-self: flex-end;
}

.tf-chat-send:hover { background: var(--tf-chat-accent-dark); }
.tf-chat-send:disabled { opacity: 0.5; cursor: not-allowed; }

.tf-chat-send svg {
	width: 1.1rem;
	height: 1.1rem;
}

/* ===================================================================
   Footer
   =================================================================== */
.tf-chat-footer {
	text-align: center;
	padding: 0.4rem 0.75rem 0.6rem;
	font-size: 0.7rem;
	color: var(--tf-chat-muted);
	border-top: 1px solid var(--tf-chat-border);
	flex-shrink: 0;
}

/* Accessibility: reduced motion */
@media (prefers-reduced-motion: reduce) {
	.tf-chat-window,
	.tf-chat-toggle,
	.tf-msg-typing span {
		transition: none;
		animation: none;
	}
}
