/**
 * Mobile Mechanic AI Assistant — Chat Widget Styles
 *
 * Self-contained CSS for the frontend chat widget.
 * Uses CSS custom properties so the color can be themed dynamically.
 *
 * Design principles:
 *  - Namespaced (.mmai-*) so it can't conflict with theme styles
 *  - Heavy use of CSS variables for easy theming
 *  - Mobile-first responsive
 *  - Accessible focus states
 *  - Smooth but performant animations
 *
 * @package MobileMechanicAI
 * @since   1.0.0
 */

/* ============================================================
   Root container
   - Positioned fixed in bottom-right
   - High z-index so it floats above page content
   - CSS variables defined here for child elements to inherit
   ============================================================ */
.mmai-widget-root {
	--mmai-color: #1e40af;            /* Default; overridden by inline style */
	--mmai-color-dark: #1e3a8a;
	--mmai-color-light: #3b82f6;
	--mmai-bg: #ffffff;
	--mmai-bg-alt: #f9fafb;
	--mmai-border: #e5e7eb;
	--mmai-text: #111827;
	--mmai-text-muted: #6b7280;
	--mmai-user-bubble: var(--mmai-color);
	--mmai-user-text: #ffffff;
	--mmai-bot-bubble: #f3f4f6;
	--mmai-bot-text: #111827;
	--mmai-shadow: 0 10px 40px rgba(0, 0, 0, 0.16);
	--mmai-shadow-soft: 0 4px 12px rgba(0, 0, 0, 0.08);
	--mmai-radius: 16px;
	--mmai-radius-sm: 8px;
	--mmai-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
		Oxygen, Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif;

	position: fixed;
	bottom: 20px;
	right: 20px;
	z-index: 999990;                  /* Below WP admin bar (999999) but above everything else */
	font-family: var(--mmai-font);
	font-size: 14px;
	line-height: 1.5;
	color: var(--mmai-text);
}

/* Reset everything inside the widget so theme styles can't bleed in */
.mmai-widget-root,
.mmai-widget-root * {
	box-sizing: border-box;
}

.mmai-widget-root button {
	font-family: inherit;
	font-size: inherit;
	cursor: pointer;
	border: none;
	background: none;
	color: inherit;
	padding: 0;
	margin: 0;
	-webkit-appearance: none;
	appearance: none;
}

.mmai-widget-root button:focus-visible {
	outline: 2px solid var(--mmai-color);
	outline-offset: 2px;
}

/* ============================================================
   Floating toggle button
   ============================================================ */
.mmai-toggle-btn {
	width: 60px;
	height: 60px;
	border-radius: 50%;
	background: var(--mmai-color);
	color: #ffffff;
	box-shadow: var(--mmai-shadow);
	display: flex;
	align-items: center;
	justify-content: center;
	transition: transform 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
	position: relative;
}

.mmai-toggle-btn:hover {
	background: var(--mmai-color-dark);
	transform: scale(1.05);
	box-shadow: 0 12px 48px rgba(0, 0, 0, 0.22);
}

.mmai-toggle-btn:active {
	transform: scale(0.98);
}

/* Show/hide chat vs close icon based on state */
.mmai-toggle-btn .mmai-icon-close {
	display: none;
}

.mmai-widget-root.mmai-open .mmai-toggle-btn .mmai-icon-chat {
	display: none;
}

.mmai-widget-root.mmai-open .mmai-toggle-btn .mmai-icon-close {
	display: block;
}

/* Notification dot */
.mmai-toggle-dot {
	position: absolute;
	top: 4px;
	right: 4px;
	width: 12px;
	height: 12px;
	background: #ef4444;
	border: 2px solid var(--mmai-bg);
	border-radius: 50%;
	display: none;
}

.mmai-widget-root.mmai-has-notification:not(.mmai-open) .mmai-toggle-dot {
	display: block;
	animation: mmai-pulse 2s infinite;
}

@keyframes mmai-pulse {
	0%, 100% { transform: scale(1); }
	50%      { transform: scale(1.2); }
}

/* ============================================================
   Chat window
   ============================================================ */
.mmai-chat-window {
	position: absolute;
	bottom: 76px;
	right: 0;
	width: 380px;
	max-width: calc(100vw - 40px);
	height: 600px;
	max-height: calc(100vh - 120px);
	background: var(--mmai-bg);
	border-radius: var(--mmai-radius);
	box-shadow: var(--mmai-shadow);
	display: flex;
	flex-direction: column;
	overflow: hidden;
	transform-origin: bottom right;
	opacity: 0;
	transform: translateY(20px) scale(0.95);
	transition: opacity 0.25s ease, transform 0.25s ease;
	pointer-events: none;
}

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

/* The [hidden] attribute is removed by JS once initialized */
.mmai-chat-window[hidden] {
	display: none;
}

/* ============================================================
   Header
   ============================================================ */
.mmai-chat-header {
	background: var(--mmai-color);
	color: #ffffff;
	padding: 14px 16px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	flex-shrink: 0;
}

.mmai-header-info {
	display: flex;
	align-items: center;
	gap: 10px;
	min-width: 0;
}

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

.mmai-header-text {
	min-width: 0;
}

.mmai-chat-title {
	margin: 0;
	font-size: 15px;
	font-weight: 600;
	line-height: 1.2;
	color: #ffffff;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.mmai-status {
	display: flex;
	align-items: center;
	gap: 5px;
	font-size: 12px;
	opacity: 0.9;
	margin-top: 2px;
}

.mmai-status-dot {
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: #4ade80;
	box-shadow: 0 0 0 2px rgba(74, 222, 128, 0.3);
}

.mmai-header-controls {
	display: flex;
	align-items: center;
	gap: 4px;
	flex-shrink: 0;
}

/* Header icon buttons */
.mmai-icon-btn {
	width: 32px;
	height: 32px;
	border-radius: 8px;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #ffffff;
	transition: background-color 0.15s ease, opacity 0.15s ease;
}

.mmai-icon-btn:hover:not(:disabled) {
	background: rgba(255, 255, 255, 0.15);
}

.mmai-icon-btn:disabled,
.mmai-icon-btn.mmai-disabled {
	opacity: 0.4;
	cursor: not-allowed;
}

/* Mic button is in the input area, so it has different colors */
.mmai-input-area .mmai-icon-btn {
	color: var(--mmai-text-muted);
}

.mmai-input-area .mmai-icon-btn:hover:not(:disabled) {
	background: var(--mmai-bg-alt);
}

/* ============================================================
   Messages area
   ============================================================ */
.mmai-messages {
	flex: 1;
	overflow-y: auto;
	padding: 16px;
	display: flex;
	flex-direction: column;
	gap: 10px;
	background: var(--mmai-bg-alt);
	scroll-behavior: smooth;
}

/* Custom scrollbar (Webkit) */
.mmai-messages::-webkit-scrollbar {
	width: 6px;
}

.mmai-messages::-webkit-scrollbar-track {
	background: transparent;
}

.mmai-messages::-webkit-scrollbar-thumb {
	background: rgba(0, 0, 0, 0.15);
	border-radius: 3px;
}

.mmai-messages::-webkit-scrollbar-thumb:hover {
	background: rgba(0, 0, 0, 0.3);
}

/* Individual message bubble */
.mmai-message {
	display: flex;
	flex-direction: column;
	max-width: 85%;
	animation: mmai-message-in 0.2s ease-out;
}

@keyframes mmai-message-in {
	from { opacity: 0; transform: translateY(8px); }
	to   { opacity: 1; transform: translateY(0); }
}

.mmai-message-bubble {
	padding: 10px 14px;
	border-radius: var(--mmai-radius);
	word-wrap: break-word;
	overflow-wrap: break-word;
	white-space: pre-wrap;
}

.mmai-message-meta {
	font-size: 11px;
	color: var(--mmai-text-muted);
	margin-top: 4px;
	padding: 0 4px;
}

/* User messages — right aligned, primary color */
.mmai-message-user {
	align-self: flex-end;
	align-items: flex-end;
}

.mmai-message-user .mmai-message-bubble {
	background: var(--mmai-user-bubble);
	color: var(--mmai-user-text);
	border-bottom-right-radius: 4px;
}

/* Bot messages — left aligned, neutral color */
.mmai-message-bot {
	align-self: flex-start;
	align-items: flex-start;
}

.mmai-message-bot .mmai-message-bubble {
	background: var(--mmai-bot-bubble);
	color: var(--mmai-bot-text);
	border-bottom-left-radius: 4px;
}

/* System messages (errors, info) */
.mmai-message-system {
	align-self: center;
	max-width: 90%;
	text-align: center;
}

.mmai-message-system .mmai-message-bubble {
	background: #fef3c7;
	color: #92400e;
	font-size: 13px;
	border-radius: var(--mmai-radius-sm);
}

/* ============================================================
   Typing indicator
   ============================================================ */
.mmai-typing {
	padding: 0 16px 8px;
	background: var(--mmai-bg-alt);
}

.mmai-typing-bubble {
	display: inline-flex;
	gap: 4px;
	padding: 12px 14px;
	background: var(--mmai-bot-bubble);
	border-radius: var(--mmai-radius);
	border-bottom-left-radius: 4px;
}

.mmai-typing-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--mmai-text-muted);
	animation: mmai-typing-bounce 1.4s infinite;
}

.mmai-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.mmai-typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes mmai-typing-bounce {
	0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
	30%           { opacity: 1;   transform: translateY(-4px); }
}

/* ============================================================
   Input area
   ============================================================ */
.mmai-input-area {
	display: flex;
	align-items: flex-end;
	gap: 6px;
	padding: 10px 12px;
	border-top: 1px solid var(--mmai-border);
	background: var(--mmai-bg);
	flex-shrink: 0;
}

.mmai-mic-btn {
	width: 38px;
	height: 38px;
	flex-shrink: 0;
	transition: background-color 0.15s ease, color 0.15s ease;
}

/* Mic listening state — pulsing red */
.mmai-mic-btn.mmai-listening {
	background: #ef4444 !important;
	color: #ffffff !important;
	animation: mmai-mic-pulse 1.2s infinite;
}

@keyframes mmai-mic-pulse {
	0%   { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
	70%  { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
	100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Speaker on/off icon swap */
.mmai-icon-speaker-off { display: none; }
#mmai-speaker-btn[aria-pressed="false"] .mmai-icon-speaker-on  { display: none; }
#mmai-speaker-btn[aria-pressed="false"] .mmai-icon-speaker-off { display: block; }

/* Speaker actively speaking — subtle pulse around the icon */
#mmai-speaker-btn.mmai-speaking {
	animation: mmai-speaker-pulse 1.0s infinite;
}

@keyframes mmai-speaker-pulse {
	0%, 100% { background: rgba(255, 255, 255, 0.15); }
	50%      { background: rgba(255, 255, 255, 0.35); }
}

/* Voice transcript preview (interim results shown in input) */
.mmai-input.mmai-voice-interim {
	color: var(--mmai-text-muted);
	font-style: italic;
}

/* "Listening..." label appearing under the input */
.mmai-voice-status {
	display: none;
	padding: 4px 12px 0;
	font-size: 12px;
	color: #dc2626;
	font-weight: 500;
	background: var(--mmai-bg);
}

.mmai-widget-root.mmai-voice-active .mmai-voice-status {
	display: block;
}

.mmai-input {
	flex: 1;
	min-height: 38px;
	max-height: 120px;
	padding: 9px 12px;
	border: 1px solid var(--mmai-border);
	border-radius: 20px;
	background: var(--mmai-bg);
	color: var(--mmai-text);
	font-family: inherit;
	font-size: 14px;
	line-height: 1.4;
	resize: none;
	outline: none;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.mmai-input:focus {
	border-color: var(--mmai-color);
	box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.12);
}

.mmai-input::placeholder {
	color: var(--mmai-text-muted);
}

.mmai-send-btn {
	width: 38px;
	height: 38px;
	border-radius: 50%;
	background: var(--mmai-color);
	color: #ffffff;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	transition: background-color 0.15s ease, opacity 0.15s ease, transform 0.1s ease;
}

.mmai-send-btn:hover:not(:disabled) {
	background: var(--mmai-color-dark);
}

.mmai-send-btn:active:not(:disabled) {
	transform: scale(0.95);
}

.mmai-send-btn:disabled {
	opacity: 0.4;
	cursor: not-allowed;
}

/* ============================================================
   Footer
   ============================================================ */
.mmai-chat-footer {
	padding: 6px 12px 8px;
	text-align: center;
	background: var(--mmai-bg);
	border-top: 1px solid var(--mmai-border);
	color: var(--mmai-text-muted);
	font-size: 11px;
	flex-shrink: 0;
}

/* ============================================================
   Mobile responsive (under 480px)
   ============================================================ */
@media (max-width: 480px) {
	.mmai-widget-root {
		bottom: 12px;
		right: 12px;
	}

	.mmai-toggle-btn {
		width: 56px;
		height: 56px;
	}

	.mmai-chat-window {
		bottom: 70px;
		right: 0;
		width: calc(100vw - 24px);
		height: calc(100vh - 90px);
		max-height: 640px;
	}
}

/* On really small screens, make the chat fullscreen-ish */
@media (max-width: 380px) {
	.mmai-chat-window {
		position: fixed;
		bottom: 70px;
		right: 12px;
		left: 12px;
		width: auto;
		max-width: none;
	}
}

/* ============================================================
   Hidden attribute respect
   ============================================================ */
.mmai-widget-root[hidden],
.mmai-chat-window[hidden] {
	display: none !important;
}

/* ============================================================
   Testing-mode badge (admin-only widget visibility indicator)
   Shown at all times when testing_mode is on. Positioned to NOT overlap
   the chat bubble or the chat window.
   ============================================================ */
.mmai-testing-badge {
	position: absolute;
	bottom: -12px;
	right: -8px;
	background: #f59e0b;
	color: #1f2937;
	font-size: 10px;
	font-weight: 700;
	letter-spacing: 0.5px;
	padding: 5px 9px;
	border-radius: 4px;
	box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
	white-space: nowrap;
	z-index: 999992;          /* Above the chat bubble */
	pointer-events: none;
	border: 2px solid #fff;
}

/* When chat window is open, move the badge above it so it stays visible */
.mmai-widget-root.mmai-open .mmai-testing-badge {
	bottom: auto;
	top: -12px;
	right: -8px;
}

@media (max-width: 480px) {
	.mmai-testing-badge {
		font-size: 9px;
		padding: 3px 6px;
	}
}

/* ============================================================
   Reduced motion (accessibility)
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
	.mmai-widget-root *,
	.mmai-widget-root *::before,
	.mmai-widget-root *::after {
		animation-duration: 0.01ms !important;
		animation-iteration-count: 1 !important;
		transition-duration: 0.01ms !important;
	}
}

/* ================================================================
 * v1.5.1 — Fullscreen takeover mode
 * ================================================================
 * When the widget root has the `.mmai-fullscreen` class, the chat
 * window expands to fill the entire viewport and the floating
 * toggle bubble is hidden. The chat IS the website.
 *
 * These rules are appended at the end so they take precedence over
 * the earlier widget-mode rules without modifying them.
 * ================================================================ */

/* Hide the floating toggle bubble entirely in fullscreen mode. */
.mmai-fullscreen .mmai-toggle-btn {
	display: none !important;
}

/* The widget root, in fullscreen mode, anchors to the entire viewport. */
.mmai-fullscreen.mmai-widget-root {
	position: fixed;
	inset: 0;             /* top/right/bottom/left: 0 — full viewport */
	width: 100vw;
	height: 100vh;
	height: 100dvh;       /* dvh accounts for mobile browser chrome */
	bottom: auto;
	right: auto;
	z-index: 999999;
}

/* The chat window fills the root completely. */
.mmai-fullscreen .mmai-chat-window {
	position: static;
	width: 100%;
	height: 100%;
	max-width: none;
	max-height: none;
	border-radius: 0;
	box-shadow: none;
	display: flex;
	flex-direction: column;
	transform: none;
	opacity: 1;
	pointer-events: auto;
}

/* Override the slide-in animation classes — chat is always visible. */
.mmai-fullscreen.mmai-open .mmai-chat-window,
.mmai-fullscreen .mmai-chat-window {
	transform: none;
	opacity: 1;
	visibility: visible;
}

/* Messages area takes all available vertical space. */
.mmai-fullscreen .mmai-messages {
	flex: 1 1 auto;
	max-height: none;
	height: auto;
}

/* On larger screens, give content a comfortable max-width and center it
 * so the chat doesn't stretch edge-to-edge on a 27" monitor. */
@media (min-width: 900px) {
	.mmai-fullscreen .mmai-chat-header,
	.mmai-fullscreen .mmai-messages,
	.mmai-fullscreen .mmai-typing,
	.mmai-fullscreen .mmai-voice-status,
	.mmai-fullscreen .mmai-input-area,
	.mmai-fullscreen .mmai-chat-footer {
		max-width: 900px;
		margin-left: auto;
		margin-right: auto;
		width: 100%;
		box-sizing: border-box;
	}
}

/* ================================================================
 * Speaker icon visibility — show ON or OFF SVG based on aria-pressed
 * ================================================================
 * The original CSS doesn't toggle these explicitly; this ensures the
 * correct icon is always visible.
 */
.mmai-icon-speaker-on,
.mmai-icon-speaker-off {
	display: none;
}
.mmai-speaker-btn[aria-pressed="true"] .mmai-icon-speaker-on,
#mmai-speaker-btn[aria-pressed="true"] .mmai-icon-speaker-on {
	display: inline-block;
}
.mmai-speaker-btn[aria-pressed="false"] .mmai-icon-speaker-off,
#mmai-speaker-btn[aria-pressed="false"] .mmai-icon-speaker-off {
	display: inline-block;
}

/* ================================================================
 * Tap-to-start overlay
 * ================================================================
 * Shown only when the browser blocks autoplay TTS and we need a
 * user gesture to unlock voice. Covers the chat content with a
 * single big "Tap to start" button.
 */
.mmai-tap-to-start {
	position: absolute;
	inset: 0;
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(255, 255, 255, 0.96);
	z-index: 10;
}

.mmai-tap-to-start[hidden] {
	display: none;
}

.mmai-tap-to-start-btn {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 16px;
	padding: 32px 48px;
	background: var(--mmai-color, #1e40af);
	color: #ffffff;
	border: none;
	border-radius: 16px;
	font-size: 18px;
	font-weight: 600;
	cursor: pointer;
	box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
	transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.mmai-tap-to-start-btn:hover {
	transform: translateY(-2px);
	box-shadow: 0 12px 32px rgba(0, 0, 0, 0.20);
}

.mmai-tap-to-start-btn:active {
	transform: translateY(0);
}
