/**
 * Front-end course AI helper widget.
 *
 * A floating launcher (bottom-right) that opens a compact chat panel on single
 * course pages. Self-contained and scoped under .ldh-cai so it never collides
 * with the theme or the course page layout.
 */

.ldh-cai {
	--cai-accent: #4f46e5;
	--cai-accent-2: #7c3aed;
	--cai-ink: #0f172a;
	--cai-muted: #64748b;
	--cai-surface: #ffffff;
	--cai-surface-2: #f6f7fb;
	--cai-border: #e6e8f0;
	--cai-user: #4f46e5;
	--cai-radius: 20px;
	--cai-shadow: 0 24px 60px -12px rgba(15, 23, 42, 0.35), 0 8px 20px -8px rgba(15, 23, 42, 0.25);

	position: fixed;
	right: clamp(14px, 3vw, 28px);
	bottom: clamp(14px, 3vw, 28px);
	z-index: 99990;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
	line-height: 1.5;
}

.ldh-cai *,
.ldh-cai *::before,
.ldh-cai *::after {
	box-sizing: border-box;
}

/* -------------------------------------------------------------- Launcher */

.ldh-cai-launcher {
	display: inline-flex;
	align-items: center;
	gap: 10px;
	padding: 12px 20px 12px 14px;
	border: 0;
	border-radius: 999px;
	background: linear-gradient(135deg, var(--cai-accent), var(--cai-accent-2));
	color: #fff;
	font-size: 15px;
	font-weight: 700;
	letter-spacing: 0.01em;
	cursor: pointer;
	box-shadow: var(--cai-shadow);
	transition: transform 0.18s ease, box-shadow 0.18s ease, opacity 0.18s ease;
	max-width: 78vw;
}

.ldh-cai-launcher:hover {
	transform: translateY(-2px);
	box-shadow: 0 30px 70px -14px rgba(79, 70, 229, 0.55), 0 10px 22px -8px rgba(15, 23, 42, 0.3);
}

.ldh-cai-launcher:focus-visible {
	outline: 3px solid rgba(124, 58, 237, 0.5);
	outline-offset: 3px;
}

.ldh-cai-launcher-ic {
	display: inline-flex;
	width: 28px;
	height: 28px;
	align-items: center;
	justify-content: center;
	border-radius: 50%;
	background: rgba(255, 255, 255, 0.18);
}

.ldh-cai-launcher-ic svg {
	width: 17px;
	height: 17px;
}

.ldh-cai-launcher-label {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

/* A soft attention pulse until first opened. */
.ldh-cai:not(.is-open) .ldh-cai-launcher::after {
	content: "";
	position: absolute;
	inset: 0;
	border-radius: 999px;
	box-shadow: 0 0 0 0 rgba(124, 58, 237, 0.45);
	animation: ldh-cai-pulse 2.6s ease-out infinite;
	pointer-events: none;
}

@keyframes ldh-cai-pulse {
	0% { box-shadow: 0 0 0 0 rgba(124, 58, 237, 0.4); }
	70% { box-shadow: 0 0 0 16px rgba(124, 58, 237, 0); }
	100% { box-shadow: 0 0 0 0 rgba(124, 58, 237, 0); }
}

.ldh-cai.is-open .ldh-cai-launcher {
	display: none;
}

/* ----------------------------------------------------------------- Panel */

.ldh-cai-panel {
	position: absolute;
	right: 0;
	bottom: 0;
	width: min(400px, calc(100vw - 28px));
	height: min(620px, calc(100vh - 40px));
	max-height: 82vh;
	display: flex;
	flex-direction: column;
	background: var(--cai-surface);
	border: 1px solid var(--cai-border);
	border-radius: var(--cai-radius);
	box-shadow: var(--cai-shadow);
	overflow: hidden;
	transform-origin: bottom right;
	animation: ldh-cai-in 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes ldh-cai-in {
	from { opacity: 0; transform: translateY(14px) scale(0.96); }
	to { opacity: 1; transform: translateY(0) scale(1); }
}

/* Header */

.ldh-cai-head {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	padding: 14px 14px 14px 16px;
	background: linear-gradient(135deg, var(--cai-accent), var(--cai-accent-2));
	color: #fff;
}

.ldh-cai-head-id {
	display: flex;
	align-items: center;
	gap: 11px;
	min-width: 0;
}

.ldh-cai-avatar {
	display: inline-flex;
	width: 38px;
	height: 38px;
	flex: 0 0 auto;
	align-items: center;
	justify-content: center;
	border-radius: 12px;
	background: rgba(255, 255, 255, 0.18);
}

.ldh-cai-avatar svg {
	width: 22px;
	height: 22px;
}

.ldh-cai-head-id h2 {
	margin: 0;
	font-size: 16px;
	font-weight: 800;
	color: #fff;
	line-height: 1.2;
}

.ldh-cai-head-id p {
	margin: 2px 0 0;
	font-size: 12px;
	color: rgba(255, 255, 255, 0.82);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.ldh-cai-close {
	display: inline-flex;
	width: 34px;
	height: 34px;
	flex: 0 0 auto;
	align-items: center;
	justify-content: center;
	border: 0;
	border-radius: 10px;
	background: rgba(255, 255, 255, 0.14);
	color: #fff;
	cursor: pointer;
	transition: background 0.15s ease;
}

.ldh-cai-close:hover {
	background: rgba(255, 255, 255, 0.28);
}

.ldh-cai-close svg {
	width: 18px;
	height: 18px;
}

/* Thread */

.ldh-cai-thread {
	flex: 1 1 auto;
	overflow-y: auto;
	padding: 16px;
	background: var(--cai-surface-2);
	display: flex;
	flex-direction: column;
	gap: 12px;
	scroll-behavior: smooth;
}

.ldh-cai-msg {
	display: flex;
	max-width: 90%;
}

.ldh-cai-msg.is-user {
	align-self: flex-end;
}

.ldh-cai-msg.is-ai {
	align-self: flex-start;
}

.ldh-cai-bubble {
	padding: 11px 14px;
	border-radius: 16px;
	font-size: 14.5px;
	color: var(--cai-ink);
	background: var(--cai-surface);
	border: 1px solid var(--cai-border);
	box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
	word-wrap: break-word;
	overflow-wrap: anywhere;
}

.ldh-cai-msg.is-ai .ldh-cai-bubble {
	border-bottom-left-radius: 5px;
}

.ldh-cai-msg.is-user .ldh-cai-bubble {
	background: linear-gradient(135deg, var(--cai-accent), var(--cai-accent-2));
	color: #fff;
	border-color: transparent;
	border-bottom-right-radius: 5px;
}

.ldh-cai-bubble p {
	margin: 0 0 8px;
}

.ldh-cai-bubble p:last-child {
	margin-bottom: 0;
}

.ldh-cai-bubble ul,
.ldh-cai-bubble ol {
	margin: 6px 0;
	padding-left: 20px;
}

.ldh-cai-bubble li {
	margin: 3px 0;
}

.ldh-cai-bubble strong {
	font-weight: 700;
}

.ldh-cai-bubble a {
	color: var(--cai-accent);
	font-weight: 600;
	text-decoration: underline;
}

.ldh-cai-msg.is-user .ldh-cai-bubble a {
	color: #fff;
}

.ldh-cai-error {
	color: #b91c1c;
	font-size: 14px;
}

/* Welcome + suggestion chips */

.ldh-cai-welcome .ldh-cai-bubble {
	background: var(--cai-surface);
}

.ldh-cai-chips {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin-top: 10px;
}

.ldh-cai-chip {
	border: 1px solid var(--cai-border);
	background: var(--cai-surface);
	color: var(--cai-accent);
	font-size: 13px;
	font-weight: 600;
	padding: 8px 12px;
	border-radius: 999px;
	cursor: pointer;
	text-align: left;
	transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.ldh-cai-chip:hover {
	background: rgba(79, 70, 229, 0.07);
	border-color: rgba(79, 70, 229, 0.4);
	transform: translateY(-1px);
}

/* Enroll CTA inside the welcome/thread */

.ldh-cai-enroll {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	margin-top: 12px;
	padding: 11px 18px;
	border-radius: 12px;
	background: linear-gradient(135deg, #10b981, #059669);
	color: #fff;
	font-size: 14.5px;
	font-weight: 700;
	text-decoration: none;
	box-shadow: 0 8px 18px -8px rgba(5, 150, 105, 0.6);
	transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.ldh-cai-enroll:hover {
	transform: translateY(-1px);
	box-shadow: 0 12px 24px -8px rgba(5, 150, 105, 0.7);
	color: #fff;
}

.ldh-cai-enroll svg {
	width: 16px;
	height: 16px;
}

/* Typing indicator */

.ldh-cai-typing {
	display: inline-flex;
	gap: 4px;
	align-items: center;
	padding: 2px 0;
}

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

.ldh-cai-typing span:nth-child(2) { animation-delay: 0.15s; }
.ldh-cai-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes ldh-cai-bounce {
	0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
	30% { transform: translateY(-5px); opacity: 1; }
}

/* Composer */

.ldh-cai-form {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 12px;
	background: var(--cai-surface);
	border-top: 1px solid var(--cai-border);
}

.ldh-cai-form textarea {
	flex: 1 1 auto;
	resize: none;
	border: 1px solid var(--cai-border);
	border-radius: 12px;
	padding: 9px 13px;
	margin: 0;
	font-size: 14.5px;
	font-family: inherit;
	line-height: 1.4;
	color: var(--cai-ink);
	background: var(--cai-surface-2);
	/* Two lines by default; grows via JS up to max-height. */
	min-height: 62px;
	height: 62px;
	max-height: 120px;
	box-sizing: border-box;
	overflow-y: hidden;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.ldh-cai-form textarea:focus {
	outline: none;
	border-color: var(--cai-accent);
	background: var(--cai-surface);
	box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.14);
}

.ldh-cai-send {
	position: relative;
	flex: 0 0 auto;
	display: inline-flex;
	width: 40px;
	height: 40px;
	padding: 0;
	margin: 0;
	align-items: center;
	justify-content: center;
	border: 0;
	border-radius: 12px;
	background: linear-gradient(135deg, var(--cai-accent), var(--cai-accent-2));
	color: #fff;
	cursor: pointer;
	transition: transform 0.15s ease, opacity 0.15s ease;
}

/* Pin the paper-plane glyph to the exact centre of the button, out of flow so
   no theme rule (text-align, margins, justify-content) can shift it sideways. */
.ldh-cai-send svg {
	position: absolute;
	top: 50%;
	left: 50%;
	width: 20px;
	height: 20px;
	margin: 0;
	transform: translate(-50%, -50%);
}

.ldh-cai-send:hover {
	transform: translateY(-1px);
}

.ldh-cai-send:disabled {
	opacity: 0.55;
	cursor: default;
	transform: none;
}

.ldh-cai-foot {
	margin: 0;
	padding: 0 14px 12px;
	background: var(--cai-surface);
	font-size: 11px;
	color: var(--cai-muted);
	text-align: center;
}

/* Scrollbar */

.ldh-cai-thread::-webkit-scrollbar {
	width: 8px;
}

.ldh-cai-thread::-webkit-scrollbar-thumb {
	background: rgba(100, 116, 139, 0.3);
	border-radius: 8px;
}

/* -------------------------------------------------------------- Mobile */

@media (max-width: 480px) {
	.ldh-cai {
		right: 12px;
		bottom: 12px;
	}

	.ldh-cai-launcher-label {
		display: none;
	}

	.ldh-cai-launcher {
		padding: 14px;
	}

	.ldh-cai-panel {
		position: fixed;
		right: 0;
		bottom: 0;
		width: 100vw;
		height: 100dvh;
		max-height: none;
		border-radius: 0;
	}
}

/* -------------------------------------------------------------- Dark mode */

@media (prefers-color-scheme: dark) {
	.ldh-cai {
		--cai-ink: #e8eaf2;
		--cai-muted: #9aa4b8;
		--cai-surface: #171a24;
		--cai-surface-2: #10121a;
		--cai-border: #2a2f3d;
		--cai-shadow: 0 24px 60px -12px rgba(0, 0, 0, 0.6), 0 8px 20px -8px rgba(0, 0, 0, 0.5);
	}
}

@media (prefers-reduced-motion: reduce) {
	.ldh-cai *,
	.ldh-cai *::before,
	.ldh-cai *::after {
		animation: none !important;
		transition: none !important;
	}
}
