/**
 * ShopRep AI Chat Widget — Production-quality layout
 * Structure: chat-root (flex column) > header | [error-banner] | messages | input | footer
 * - No overflow within widget
 * - Input pinned to bottom
 * - Only messages area scrolls (internal scroll)
 * - Auto-scroll to bottom on new messages
 * - Long words/URLs wrap (word-break, overflow-wrap)
 */

/* ========== Reset / Base (scoped) ========== */
.shoprep-ai-widget,
.shoprep-ai-widget *,
.shoprep-ai-widget *::before,
.shoprep-ai-widget *::after {
	box-sizing: border-box;
}

.shoprep-ai-widget {
	position: fixed;
	/* Safe-area: add env() so launcher stays clear of notch/home indicator */
	bottom: calc(24px + env(safe-area-inset-bottom, 0px));
	right: calc(24px + env(safe-area-inset-right, 0px));
	z-index: 999999;
	font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	font-size: 14px;
	line-height: 1.5;
	--shoprep-primary: #2271b1;
	--shoprep-primary-light: rgba(34, 113, 177, 0.08);
	--shoprep-primary-hover: rgba(34, 113, 177, 0.12);
	--shoprep-bg: #ffffff;
	--shoprep-text: #1e293b;
	--shoprep-text-muted: #64748b;
	--shoprep-border: #e2e8f0;
	--shoprep-radius: 16px;
	--shoprep-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
	--shoprep-shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.12), 0 0 1px rgba(0, 0, 0, 0.08);
}

.shoprep-ai-widget--bottom-left {
	right: auto;
	left: calc(24px + env(safe-area-inset-left, 0px));
}

.shoprep-ai-widget--bottom-left .shoprep-ai-panel {
	right: auto;
	left: 0;
}

/* ========== Toggle Button ========== */
.shoprep-ai-toggle {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	min-width: 60px;
	height: 60px;
	padding: 0 18px;
	border-radius: 30px;
	background: var(--shoprep-primary);
	color: white;
	border: none;
	cursor: pointer;
	box-shadow: var(--shoprep-shadow);
	transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
	font-size: 15px;
	font-weight: 600;
}

.shoprep-ai-toggle:hover {
	transform: translateY(-1px);
	box-shadow: 0 6px 12px -2px rgba(0, 0, 0, 0.12), 0 12px 30px -8px rgba(0, 0, 0, 0.15);
}

.shoprep-ai-toggle:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-bg), 0 0 0 4px var(--shoprep-primary);
}

.shoprep-ai-toggle:active {
	transform: translateY(0);
}

.shoprep-ai-toggle-icon {
	width: 24px;
	height: 24px;
	flex-shrink: 0;
}

.shoprep-ai-toggle-label {
	white-space: nowrap;
}

.shoprep-ai-widget--minimal .shoprep-ai-toggle {
	border-radius: 50%;
	min-width: 56px;
	padding: 0;
}

.shoprep-ai-widget--minimal .shoprep-ai-toggle-label {
	display: none;
}

.shoprep-ai-widget--minimal .shoprep-ai-toggle-icon {
	width: 26px;
	height: 26px;
}

/* ========== Panel = Chat Root (REQUIRED FLEX LAYOUT) ========== */
.shoprep-ai-panel.shoprep-ai-chat-root {
	position: absolute;
	bottom: 76px;
	right: 0;
	width: min(384px, calc(100vw - 48px));
	height: min(80vh, 540px);
	min-height: 340px;
	max-height: min(80vh, 540px);
	background: var(--shoprep-bg);
	border-radius: 20px;
	box-shadow: var(--shoprep-shadow-lg);
	border: 1px solid rgba(0, 0, 0, 0.06);
	overflow: hidden;
	display: flex;
	flex-direction: column;
}

.shoprep-ai-panel[hidden] {
	display: none !important;
}

.shoprep-ai-widget--bottom-left .shoprep-ai-panel {
	right: auto;
	left: 0;
}

/* ========== Chat Header (flex: 0 0 auto) ========== */
.shoprep-ai-chat-header {
	flex: 0 0 auto;
	height: 60px;
	min-height: 60px;
	padding: 0 20px;
	background: var(--shoprep-bg);
	color: var(--shoprep-text);
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 16px;
	border-bottom: 1px solid var(--shoprep-border);
}

.shoprep-ai-chat-header__brand {
	display: flex;
	flex-direction: column;
	gap: 2px;
	min-width: 0;
	flex: 1;
}

.shoprep-ai-chat-header__title {
	font-weight: 600;
	font-size: 16px;
	letter-spacing: -0.01em;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	color: var(--shoprep-text);
}

.shoprep-ai-chat-header__status {
	display: flex;
	align-items: center;
	gap: 6px;
	font-size: 12px;
	font-weight: 500;
	color: var(--shoprep-text-muted);
}

.shoprep-ai-chat-header__status-dot {
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: #22c55e;
	box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.3);
	animation: shoprep-status-pulse 2s ease-in-out infinite;
	flex-shrink: 0;
}

@keyframes shoprep-status-pulse {
	0%, 100% { opacity: 1; }
	50% { opacity: 0.7; }
}

.shoprep-ai-chat-header__close {
	background: var(--shoprep-primary-light);
	border: none;
	color: var(--shoprep-text-muted);
	width: 36px;
	height: 36px;
	min-width: 36px;
	flex-shrink: 0;
	border-radius: 10px;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.15s ease, color 0.15s ease;
}

.shoprep-ai-chat-header__close:hover {
	background: var(--shoprep-primary-hover);
	color: var(--shoprep-text);
}

.shoprep-ai-chat-header__close:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-primary);
}

/* ========== Error Banner (flex: 0 0 auto, shown on API failure) ========== */
.shoprep-ai-error-banner {
	flex: 0 0 auto;
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 10px 14px;
	background: #fef2f2;
	border-bottom: 1px solid #fecaca;
	color: #991b1b;
	font-size: 13px;
	line-height: 1.4;
}

.shoprep-ai-error-banner[hidden] {
	display: none !important;
}

.shoprep-ai-error-banner__text {
	flex: 1;
	min-width: 0;
	word-break: break-word;
	overflow-wrap: anywhere;
}

.shoprep-ai-error-banner__dismiss {
	flex-shrink: 0;
	width: 28px;
	height: 28px;
	padding: 0;
	background: transparent;
	border: none;
	color: inherit;
	font-size: 20px;
	line-height: 1;
	cursor: pointer;
	opacity: 0.8;
	border-radius: 6px;
	transition: opacity 0.15s ease, background 0.15s ease;
}

.shoprep-ai-error-banner__dismiss:hover {
	opacity: 1;
	background: rgba(153, 27, 27, 0.1);
}

.shoprep-ai-error-banner__dismiss:focus {
	outline: none;
	box-shadow: 0 0 0 2px currentColor;
}

/* ========== Pending Human Banner (handoff in progress) ========== */
.shoprep-ai-pending-human-banner {
	flex: 0 0 auto;
	display: flex;
	align-items: center;
	padding: 10px 14px;
	background: var(--shoprep-primary-light);
	border-bottom: 1px solid var(--shoprep-border);
	color: var(--shoprep-primary);
	font-size: 13px;
	line-height: 1.4;
}

.shoprep-ai-pending-human-banner[hidden] {
	display: none !important;
}

.shoprep-ai-pending-human-banner__text {
	flex: 1;
	min-width: 0;
	word-break: break-word;
	overflow-wrap: anywhere;
}

/* ========== Chat Messages (flex: 1 1 auto; ONLY THIS SCROLLS) ========== */
.shoprep-ai-chat-messages {
	flex: 1 1 auto;
	min-height: 0;
	overflow-y: auto;
	overflow-x: hidden;
	padding: 16px;
	background: #f8fafc;
	display: flex;
	flex-direction: column;
	gap: 12px;
	-webkit-overflow-scrolling: touch;
	scroll-behavior: smooth;
	overflow-anchor: auto;
}

/* ========== Message Bubbles (user right, agent left, max-width 75%) ========== */
.shoprep-ai-msg {
	max-width: 75%;
	padding: 12px 16px;
	border-radius: 18px;
	font-size: 14px;
	line-height: 1.5;
	word-break: break-word;
	overflow-wrap: anywhere;
	overflow: hidden;
	transition: opacity 0.15s ease;
	flex-shrink: 0;
}

.shoprep-ai-msg.user {
	align-self: flex-end;
	background: var(--shoprep-primary);
	color: white;
	border-bottom-right-radius: 4px;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.shoprep-ai-msg.agent {
	align-self: flex-start;
	background: white;
	color: var(--shoprep-text);
	border: 1px solid var(--shoprep-border);
	border-bottom-left-radius: 4px;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.shoprep-ai-msg__agent-label {
	display: block;
	font-size: 11px;
	font-weight: 600;
	color: var(--shoprep-primary);
	margin-bottom: 4px;
}

.shoprep-ai-msg p,
.shoprep-ai-msg li,
.shoprep-ai-msg a {
	word-break: break-word;
	overflow-wrap: anywhere;
}

.shoprep-ai-msg p {
	margin: 0 0 0.5em 0;
}

.shoprep-ai-msg p:last-child {
	margin-bottom: 0;
}

.shoprep-ai-msg ul,
.shoprep-ai-msg ol {
	margin: 0.5em 0;
	padding-left: 1.25em;
}

.shoprep-ai-msg li {
	margin: 0.25em 0;
}

.shoprep-ai-msg a {
	color: inherit;
	text-decoration: underline;
}

.shoprep-ai-msg.user a {
	opacity: 0.9;
}

/* ========== Cart Cards (agent message with cart_data) ========== */
.shoprep-ai-cart-cards {
	margin-top: 12px;
	display: flex;
	flex-direction: column;
	gap: 8px;
}

.shoprep-ai-cart-card {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 10px 12px;
	background: var(--shoprep-primary-light);
	border-radius: 10px;
	border: 1px solid var(--shoprep-border);
}

.shoprep-ai-cart-card__media {
	flex-shrink: 0;
}

.shoprep-ai-cart-card__img {
	width: 48px;
	height: 48px;
	object-fit: cover;
	border-radius: 8px;
}

.shoprep-ai-cart-card__body {
	flex: 1;
	min-width: 0;
	font-size: 13px;
	line-height: 1.4;
}

.shoprep-ai-cart-card__body a {
	color: var(--shoprep-primary);
	text-decoration: none;
	font-weight: 500;
}

.shoprep-ai-cart-card__body a:hover {
	text-decoration: underline;
}

.shoprep-ai-cart-card__qty {
	color: var(--shoprep-text-muted);
	font-size: 12px;
}

.shoprep-ai-cart-card__total {
	font-weight: 600;
	color: var(--shoprep-text);
	margin-left: 4px;
}

.shoprep-ai-cart-total {
	margin-top: 4px;
	padding-top: 8px;
	border-top: 1px solid var(--shoprep-border);
	font-weight: 600;
	font-size: 14px;
	color: var(--shoprep-text);
}

.shoprep-ai-checkout-wrap {
	margin-top: 12px;
}

.shoprep-ai-checkout-btn {
	display: inline-block;
	padding: 10px 20px;
	background: var(--shoprep-primary);
	color: white;
	border-radius: 22px;
	font-size: 14px;
	font-weight: 600;
	text-decoration: none;
	transition: filter 0.15s ease;
}

.shoprep-ai-checkout-btn:hover {
	filter: brightness(1.08);
}

.shoprep-ai-checkout-btn:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-bg), 0 0 0 4px var(--shoprep-primary);
}

/* ========== Quick Reply Buttons ========== */
.shoprep-ai-quick-replies {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin-top: 12px;
}

.shoprep-ai-quick-reply-btn {
	padding: 8px 14px;
	background: var(--shoprep-primary-light);
	color: var(--shoprep-primary);
	border: 1px solid var(--shoprep-border);
	border-radius: 20px;
	font-size: 13px;
	font-weight: 500;
	font-family: inherit;
	cursor: pointer;
	transition: background 0.15s ease, color 0.15s ease;
}

.shoprep-ai-quick-reply-btn:hover {
	background: var(--shoprep-primary-hover);
}

.shoprep-ai-quick-reply-btn:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-primary);
}

/* ========== Typing Indicator ========== */
.shoprep-ai-typing {
	align-self: flex-start;
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 14px 18px;
	background: white;
	border: 1px solid var(--shoprep-border);
	border-radius: 18px;
	border-bottom-left-radius: 4px;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
	flex-shrink: 0;
}

.shoprep-ai-typing-dot {
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: var(--shoprep-text-muted);
	animation: shoprep-typing-bounce 1.4s ease-in-out infinite both;
}

.shoprep-ai-typing-dot:nth-child(1) { animation-delay: -0.32s; }
.shoprep-ai-typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes shoprep-typing-bounce {
	0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
	40% { transform: scale(1); opacity: 1; }
}

/* ========== Welcome Message ========== */
.shoprep-ai-welcome {
	align-self: flex-start;
	max-width: 75%;
	padding: 14px 18px;
	background: white;
	border: 1px solid var(--shoprep-border);
	border-left: 3px solid var(--shoprep-primary);
	border-radius: 18px;
	border-bottom-left-radius: 4px;
	font-size: 14px;
	line-height: 1.55;
	color: var(--shoprep-text);
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
	word-break: break-word;
	overflow-wrap: anywhere;
	flex-shrink: 0;
}

.shoprep-ai-welcome p,
.shoprep-ai-welcome li {
	word-break: break-word;
	overflow-wrap: anywhere;
}

.shoprep-ai-welcome p {
	margin: 0 0 0.5em 0;
}

.shoprep-ai-welcome p:last-child {
	margin-bottom: 0;
}

/* ========== Error State ========== */
.shoprep-ai-msg.shoprep-ai-msg--error {
	align-self: flex-start;
	background: #fef2f2;
	border-color: #fecaca;
	color: #991b1b;
}

/* ========== Chat Input (flex: 0 0 auto — pinned to bottom) ========== */
.shoprep-ai-chat-input {
	flex: 0 0 auto;
	min-width: 0;
	padding: 14px 18px 16px;
	border-top: 1px solid var(--shoprep-border);
	background: var(--shoprep-bg);
	display: flex;
	align-items: center;
	gap: 10px;
}

.shoprep-ai-chat-input__field {
	flex: 1;
	min-width: 0;
	height: 44px;
	min-height: 44px;
	max-height: 120px;
	padding: 10px 18px;
	border: 1px solid var(--shoprep-border);
	border-radius: 22px;
	font-size: 14px;
	font-family: inherit;
	line-height: 1.4;
	resize: none;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
	background: #f8fafc;
}

.shoprep-ai-chat-input__field:focus {
	outline: none;
	border-color: var(--shoprep-primary);
	box-shadow: 0 0 0 3px var(--shoprep-primary-light);
	background: var(--shoprep-bg);
}

.shoprep-ai-chat-input__field::placeholder {
	color: var(--shoprep-text-muted);
}

.shoprep-ai-chat-input__field:disabled {
	opacity: 0.7;
	cursor: not-allowed;
}

.shoprep-ai-chat-input__send {
	flex-shrink: 0;
	width: 44px;
	height: 44px;
	min-width: 44px;
	min-height: 44px;
	padding: 0;
	background: var(--shoprep-primary);
	color: white;
	border: none;
	border-radius: 50%;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.15s ease, transform 0.15s ease;
}

.shoprep-ai-chat-input__send:hover:not(:disabled) {
	filter: brightness(1.08);
	transform: scale(1.03);
}

.shoprep-ai-chat-input__send:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-bg), 0 0 0 4px var(--shoprep-primary);
}

.shoprep-ai-chat-input__send:active:not(:disabled) {
	transform: scale(0.97);
}

.shoprep-ai-chat-input__send:disabled {
	opacity: 0.5;
	cursor: not-allowed;
	pointer-events: none;
}

/* ========== Chat Footer ========== */
.shoprep-ai-chat-footer {
	flex: 0 0 auto;
	min-width: 0;
	padding: 8px 18px 12px;
	text-align: center;
	font-size: 11px;
	color: #94a3b8;
	background: var(--shoprep-bg);
}

/* ========== Timed-out state (conversation ended) ========== */
.shoprep-ai-timed-out {
	flex: 0 0 auto;
	padding: 16px 18px;
	border-top: 1px solid var(--shoprep-border);
	background: var(--shoprep-bg);
	text-align: center;
}

.shoprep-ai-timed-out__text {
	margin: 0 0 12px;
	font-size: 14px;
	color: var(--shoprep-text-muted);
}

.shoprep-ai-timed-out__btn {
	padding: 10px 20px;
	background: var(--shoprep-primary);
	color: white;
	border: none;
	border-radius: 22px;
	font-size: 14px;
	font-family: inherit;
	cursor: pointer;
	transition: filter 0.15s ease;
}

.shoprep-ai-timed-out__btn:hover {
	filter: brightness(1.08);
}

.shoprep-ai-timed-out__btn:focus {
	outline: none;
	box-shadow: 0 0 0 2px var(--shoprep-bg), 0 0 0 4px var(--shoprep-primary);
}

/* ========== Style Variants ========== */
.shoprep-ai-widget--minimal .shoprep-ai-panel {
	border-radius: 12px;
}

.shoprep-ai-widget--minimal .shoprep-ai-msg {
	border-radius: 12px;
}

.shoprep-ai-widget--minimal .shoprep-ai-msg.user {
	border-bottom-right-radius: 4px;
}

.shoprep-ai-widget--minimal .shoprep-ai-msg.agent {
	border-bottom-left-radius: 4px;
}

.shoprep-ai-widget--bubble .shoprep-ai-panel {
	border-radius: 24px;
	overflow: hidden;
}

.shoprep-ai-widget--bubble .shoprep-ai-chat-header {
	border-radius: 24px 24px 0 0;
}

.shoprep-ai-widget--bubble .shoprep-ai-msg {
	border-radius: 20px;
}

.shoprep-ai-widget--bubble .shoprep-ai-msg.user {
	border-bottom-right-radius: 6px;
}

.shoprep-ai-widget--bubble .shoprep-ai-msg.agent {
	border-bottom-left-radius: 6px;
}

.shoprep-ai-widget--bubble .shoprep-ai-chat-messages {
	background: #f1f5f9;
}

.shoprep-ai-widget--card .shoprep-ai-panel {
	border-radius: 12px;
	border: 1px solid var(--shoprep-border);
}

.shoprep-ai-widget--card .shoprep-ai-chat-header {
	border-radius: 12px 12px 0 0;
}

.shoprep-ai-widget--card .shoprep-ai-msg {
	border-radius: 8px;
}

.shoprep-ai-widget--card .shoprep-ai-msg.user {
	border-bottom-right-radius: 2px;
}

.shoprep-ai-widget--card .shoprep-ai-msg.agent {
	border-bottom-left-radius: 2px;
}

.shoprep-ai-widget--modern .shoprep-ai-toggle {
	animation: shoprep-pulse 2s ease-in-out infinite;
}

@keyframes shoprep-pulse {
	0%, 100% { box-shadow: var(--shoprep-shadow); }
	50% { box-shadow: 0 6px 14px -2px rgba(0, 0, 0, 0.12); }
}

.shoprep-ai-widget--modern .shoprep-ai-chat-messages {
	background: linear-gradient(180deg, #f8fafc 0%, #f1f5f9 100%);
}

/* ========== Mobile-safe / Responsive (< 640px) ========== */
@media (max-width: 639px) {
	/* Launcher: always fully visible, respect safe areas */
	.shoprep-ai-widget {
		bottom: calc(16px + env(safe-area-inset-bottom, 0px));
		right: calc(16px + env(safe-area-inset-right, 0px));
	}

	.shoprep-ai-widget--bottom-left {
		right: auto;
		left: calc(16px + env(safe-area-inset-left, 0px));
	}

	/* Panel: viewport-fixed for reliable full-width, centered, avoids 100vh keyboard issues */
	.shoprep-ai-panel.shoprep-ai-chat-root {
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		width: min(
			calc(100vw - 24px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
			420px
		);
		max-width: 420px;
		height: min(70vh, 560px);
		max-height: min(70vh, 560px);
		min-height: 280px;
		bottom: calc(72px + env(safe-area-inset-bottom, 0px));
		right: auto;
	}

	/* Use dvh when supported (excludes mobile keyboard) */
	@supports (height: 100dvh) {
		.shoprep-ai-panel.shoprep-ai-chat-root {
			height: min(70dvh, 560px);
			max-height: min(70dvh, 560px);
		}
	}

	/* Message bubbles: slightly wider on small screens */
	.shoprep-ai-msg,
	.shoprep-ai-welcome {
		max-width: 85%;
	}

	/* Prevent iOS zoom on input focus (font-size >= 16px); touch-action reduces tap delay */
	.shoprep-ai-chat-input__field {
		font-size: 16px;
		touch-action: manipulation;
	}

	/* Input/footer/timed-out: extra padding for safe area */
	.shoprep-ai-chat-input {
		padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
	}

	.shoprep-ai-chat-footer {
		padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
	}

	.shoprep-ai-timed-out {
		padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
	}
}

/* Extra-small screens: ensure launcher never clips */
@media (max-width: 360px) {
	.shoprep-ai-widget {
		bottom: calc(12px + env(safe-area-inset-bottom, 0px));
		right: calc(12px + env(safe-area-inset-right, 0px));
	}

	.shoprep-ai-widget--bottom-left {
		left: calc(12px + env(safe-area-inset-left, 0px));
	}

	.shoprep-ai-panel.shoprep-ai-chat-root {
		width: min(
			calc(100vw - 16px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
			420px
		);
		max-width: 420px;
	}
}
