/* ============================================================
   AI4D Chatbot Widget — chatbot.css
   Drawer variant: slides in from the right edge.
   ============================================================ */

/* ── CSS Custom Properties ────────────────────────────────── */
:root {
	--ai4d-primary:       #4f46e5;
	--ai4d-primary-dark:  #4338ca;
	--ai4d-primary-light: #eef2ff;
	--ai4d-gradient:      linear-gradient(135deg, #ad46ff, #2b7fff);
	--ai4d-gradient-dark: linear-gradient(135deg, #9533e8, #1a6ef0);
	--ai4d-bot-bg:        #f1f5f9;
	--ai4d-user-bg:       #4f46e5;
	--ai4d-user-text:     #ffffff;
	--ai4d-bot-text:      #1e293b;
	--ai4d-surface:       #ffffff;
	--ai4d-border:        #e2e8f0;
	--ai4d-muted:         #94a3b8;
	--ai4d-drawer-w:      420px;
	--ai4d-z-backdrop:    99998;
	--ai4d-z-drawer:      99999;
	--ai4d-z-tab:         99997;
	--ai4d-transition:    0.32s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Screen-reader only ───────────────────────────────────── */
.ai4d-sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border-width: 0;
}

/* ── Root wrapper (no layout — children are all fixed) ───── */
#ai4d-chatbot-root * ,
#ai4d-chatbot-root *::before,
#ai4d-chatbot-root *::after {
	box-sizing: border-box;
}

/* ── Right-edge tab trigger ───────────────────────────────── */
.ai4d-tab {
	/* Stick to the right edge, vertically centred */
	position: fixed;
	right: 0;
	top: 42%;
	transform: translateY(-50%);
	z-index: var(--ai4d-z-tab);

	/* Pill shape — rounded left side, flush with right edge */
	border-radius: 14px 0 0 14px;
	border: none;
	padding: 18px 11px;

	background: var(--ai4d-gradient);
	color: #ffffff;
	cursor: pointer;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 8px;

	/* Left-facing shadow gives depth against the page */
	box-shadow: -4px 0 18px rgba(79, 70, 229, 0.40);

	/* Slide-out animation when drawer opens */
	transition: transform var(--ai4d-transition), opacity var(--ai4d-transition);
}

.ai4d-tab:hover {
	background: var(--ai4d-gradient-dark);
}

.ai4d-tab:focus-visible {
	outline: 3px solid var(--ai4d-primary);
	outline-offset: 3px;
}

/* Hide the tab behind the drawer once open */
.ai4d-tab.is-open {
	transform: translateY(-50%) translateX(110%);
	opacity: 0;
	pointer-events: none;
}

.ai4d-tab__icon {
	width: 32px;
	height: 32px;
	flex-shrink: 0;
}

.ai4d-tab__label {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	font-size: 10px;
	font-weight: 700;
	letter-spacing: 0.06em;
	text-transform: uppercase;
	line-height: 1;
}

/* ── Backdrop overlay ─────────────────────────────────────── */
.ai4d-backdrop {
	position: fixed;
	inset: 0;
	z-index: var(--ai4d-z-backdrop);
	background: rgba(15, 23, 42, 0.45);
	backdrop-filter: blur(2px);
	-webkit-backdrop-filter: blur(2px);
	opacity: 0;
	pointer-events: none;
	transition: opacity var(--ai4d-transition);
}

.ai4d-backdrop.is-open {
	opacity: 1;
	pointer-events: auto;
}

/* ── Drawer panel ─────────────────────────────────────────── */
.ai4d-drawer {
	position: fixed;
	top: 0;
	right: 0;
	height: 100vh;
	height: 100dvh; /* dynamic viewport on mobile */
	width: var(--ai4d-drawer-w);
	max-width: 100vw;
	z-index: var(--ai4d-z-drawer);

	background: var(--ai4d-surface);
	border-radius: 16px 0 0 16px;
	box-shadow: -8px 0 40px rgba(0, 0, 0, 0.18), -2px 0 8px rgba(0, 0, 0, 0.08);

	display: flex;
	flex-direction: column;
	overflow: hidden;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
	font-size: 14px;

	/* Hidden state — off-screen to the right */
	transform: translateX(100%);
	transition: transform var(--ai4d-transition);
}

.ai4d-drawer.is-open {
	transform: translateX(0);
}

/* ── Drawer header ────────────────────────────────────────── */
.ai4d-drawer__header {
	flex-shrink: 0;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 16px 18px;
	background: var(--ai4d-gradient);
	color: #ffffff;
	border-radius: 16px 0 0 0; /* match drawer's top-left radius */
}

.ai4d-drawer__header-info {
	display: flex;
	align-items: center;
	gap: 12px;
}

.ai4d-header-avatar {
	width: 44px;
	height: 44px;
	background: rgba(255, 255, 255, 0.18);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	padding: 6px;
}

.ai4d-header-avatar svg {
	width: 100%;
	height: 100%;
}

.ai4d-drawer__title {
	margin: 0;
	font-weight: 700;
	font-size: 15px;
	line-height: 1.2;
}

.ai4d-drawer__subtitle {
	margin: 3px 0 0;
	font-size: 12px;
	opacity: 0.82;
	display: flex;
	align-items: center;
	gap: 5px;
	line-height: 1;
}

.ai4d-online-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: #4ade80; /* green-400 */
	display: inline-block;
	box-shadow: 0 0 0 2px rgba(74, 222, 128, 0.35);
	animation: ai4d-pulse-dot 2.5s ease-in-out infinite;
}

.ai4d-drawer__close {
	width: 34px;
	height: 34px;
	border: none;
	background: rgba(255, 255, 255, 0.16);
	color: #ffffff;
	border-radius: 10px;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	transition: background 0.15s ease;
}

.ai4d-drawer__close:hover        { background: rgba(255, 255, 255, 0.28); }
.ai4d-drawer__close:focus-visible { outline: 2px solid rgba(255, 255, 255, 0.7); outline-offset: 2px; }

.ai4d-drawer__close svg {
	width: 17px;
	height: 17px;
	pointer-events: none;
}

/* ── Messages area ────────────────────────────────────────── */
.ai4d-drawer__messages {
	flex: 1 1 auto;
	overflow-y: auto;
	padding: 18px 16px;
	display: flex;
	flex-direction: column;
	gap: 12px;
	background: #f8fafc;
	scroll-behavior: smooth;
}

.ai4d-drawer__messages::-webkit-scrollbar       { width: 5px; }
.ai4d-drawer__messages::-webkit-scrollbar-track  { background: transparent; }
.ai4d-drawer__messages::-webkit-scrollbar-thumb  { background: var(--ai4d-border); border-radius: 99px; }

/* ── Message bubbles ──────────────────────────────────────── */
.ai4d-message {
	display: flex;
	flex-direction: column;
	max-width: 80%;
	animation: ai4d-slide-in 0.22s ease;
}

.ai4d-message--user  { align-self: flex-end;   align-items: flex-end; }
.ai4d-message--bot   { align-self: flex-start;  align-items: flex-start; }
.ai4d-message--error { align-self: center;      align-items: center; }

.ai4d-message__bubble {
	padding: 10px 15px;
	border-radius: 18px;
	word-break: break-word;
	white-space: pre-wrap;
	line-height: 1.55;
	font-size: 13.5px;
}

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

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

/* ── Rich (markdown-rendered) bot bubbles ─────────────────── */
.ai4d-message__bubble--rich {
	/* Default pre-wrap is for plain text; HTML controls its own spacing now. */
	white-space: normal;
}

.ai4d-message__bubble--rich > *:first-child { margin-top: 0; }
.ai4d-message__bubble--rich > *:last-child  { margin-bottom: 0; }

.ai4d-message__bubble--rich p {
	margin: 0 0 8px;
}

.ai4d-message__bubble--rich ul,
.ai4d-message__bubble--rich ol {
	margin: 4px 0 8px;
	padding-left: 20px;
}

.ai4d-message__bubble--rich li {
	margin: 2px 0;
}

.ai4d-message__bubble--rich li > p {
	margin: 0;
}

.ai4d-message__bubble--rich h1,
.ai4d-message__bubble--rich h2,
.ai4d-message__bubble--rich h3,
.ai4d-message__bubble--rich h4,
.ai4d-message__bubble--rich h5,
.ai4d-message__bubble--rich h6 {
	margin: 10px 0 6px;
	font-weight: 600;
	line-height: 1.3;
}

.ai4d-message__bubble--rich h1 { font-size: 1.15em; }
.ai4d-message__bubble--rich h2 { font-size: 1.10em; }
.ai4d-message__bubble--rich h3 { font-size: 1.05em; }
.ai4d-message__bubble--rich h4,
.ai4d-message__bubble--rich h5,
.ai4d-message__bubble--rich h6 { font-size: 1em; }

.ai4d-message__bubble--rich strong { font-weight: 700; }
.ai4d-message__bubble--rich em     { font-style: italic; }

.ai4d-message__bubble--rich a {
	color: var(--ai4d-primary, #4f46e5);
	text-decoration: underline;
	word-break: break-word;
}

.ai4d-message__bubble--rich a:hover {
	text-decoration: none;
}

.ai4d-message__bubble--rich code {
	background: rgba(0, 0, 0, .06);
	border-radius: 4px;
	padding: 1px 5px;
	font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
	font-size: 0.92em;
}

.ai4d-message__bubble--rich pre {
	background: rgba(0, 0, 0, .06);
	border-radius: 8px;
	padding: 10px 12px;
	margin: 6px 0 8px;
	overflow-x: auto;
}

.ai4d-message__bubble--rich pre code {
	background: transparent;
	padding: 0;
	border-radius: 0;
	font-size: 0.9em;
	line-height: 1.5;
	white-space: pre;
}

.ai4d-message__bubble--rich blockquote {
	border-left: 3px solid var(--ai4d-border, #e2e8f0);
	margin: 6px 0;
	padding: 2px 10px;
	color: var(--ai4d-muted, #64748b);
}

.ai4d-message--error .ai4d-message__bubble {
	background: #fef2f2;
	color: #dc2626;
	border-radius: 10px;
	font-size: 12.5px;
	border: 1px solid #fecaca;
}

.ai4d-message__time {
	font-size: 10.5px;
	color: var(--ai4d-muted);
	margin-top: 3px;
	padding: 0 2px;
}

/* ── Empty history notice ─────────────────────────────────── */
.ai4d-empty-notice {
	text-align: center;
	color: var(--ai4d-muted);
	font-size: 13px;
	margin: auto;
	padding: 24px 16px;
}

/* ── Loading & typing indicators ─────────────────────────── */
.ai4d-loading-spinner {
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 5px;
	padding: 40px 0;
	margin: auto;
}

.ai4d-loading-spinner span {
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: var(--ai4d-muted);
	animation: ai4d-bounce 1.2s ease-in-out infinite;
	display: block;
}

.ai4d-loading-spinner span:nth-child(2) { animation-delay: 0.2s; }
.ai4d-loading-spinner span:nth-child(3) { animation-delay: 0.4s; }

.ai4d-typing { align-self: flex-start; }

.ai4d-typing .ai4d-message__bubble {
	background: var(--ai4d-bot-bg);
	border-bottom-left-radius: 4px;
	padding: 12px 16px;
	display: flex;
	align-items: center;
	gap: 4px;
}

.ai4d-typing .ai4d-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--ai4d-muted);
	animation: ai4d-bounce 1.2s ease-in-out infinite;
}

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

/* ── Input footer ─────────────────────────────────────────── */
.ai4d-drawer__footer {
	flex-shrink: 0;
	padding: 12px 14px;
	border-top: 1px solid var(--ai4d-border);
	background: var(--ai4d-surface);
}

.ai4d-input-row {
	display: flex;
	gap: 8px;
	align-items: center;
}

.ai4d-input {
	flex: 1 1 auto;
	border: 1.5px solid var(--ai4d-border);
	border-radius: 24px;
	padding: 10px 16px;
	font-size: 13.5px;
	font-family: inherit;
	color: var(--ai4d-bot-text);
	background: #f8fafc;
	outline: none;
	transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
	line-height: 1.4;
}

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

.ai4d-input:focus {
	border-color: var(--ai4d-primary);
	background: var(--ai4d-surface);
	box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

.ai4d-send-btn {
	width: 40px;
	height: 40px;
	border-radius: 50%;
	border: none;
	background: var(--ai4d-gradient);
	color: #ffffff;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	transition: filter 0.15s ease, transform 0.15s ease;
}

.ai4d-send-btn:hover   { filter: brightness(0.88); transform: scale(1.06); }
.ai4d-send-btn:active  { transform: scale(0.95); }
.ai4d-send-btn:disabled { background: var(--ai4d-border); cursor: not-allowed; transform: none; }
.ai4d-send-btn:focus-visible { outline: 3px solid var(--ai4d-primary); outline-offset: 3px; }

.ai4d-send-btn svg {
	width: 17px;
	height: 17px;
	pointer-events: none;
	transform: translateX(1px);
}

/* ── Keyframes ────────────────────────────────────────────── */
@keyframes ai4d-bounce {
	0%, 60%, 100% { transform: translateY(0); }
	30%           { transform: translateY(-5px); }
}

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

@keyframes ai4d-pulse-dot {
	0%, 100% { opacity: 1; }
	50%       { opacity: 0.5; }
}

/* ── Body scroll lock when drawer is open ─────────────────── */
body.ai4d-no-scroll {
	overflow: hidden;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 600px) {
	:root {
		--ai4d-drawer-w: 100vw;
	}

	.ai4d-drawer {
		border-radius: 0;
	}

	.ai4d-drawer__header {
		border-radius: 0;
	}

	/* Move the tab lower on mobile so it doesn't conflict with browser UI */
	.ai4d-tab {
		top: 55%;
	}
}
