/**
 * AcademyFlow Chat Widget — Frontend Customer Chat
 * Brand-aligned design: AcademyFlow orange + modern card style
 */

/* ── CSS variables ─────────────────────────────────────────────────────── */
.afinbox-chat-widget {
	--afl-brand:       #e07b39;
	--afl-brand-dark:  #c96a2a;
	--afl-brand-light: #fdf0e8;
	--afl-accent:      #1e293b;
	--afl-green:       #22c55e;
	--afl-bg:          #f8fafc;
	--afl-white:       #ffffff;
	--afl-border:      #e2e8f0;
	--afl-text:        #1e293b;
	--afl-muted:       #64748b;
	--afl-shadow:      0 8px 32px rgba(0,0,0,0.14);
	--afl-radius:      18px;

	position: fixed;
	bottom: 24px;
	right: 24px;
	z-index: 999999;
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

/* ── Toggle button ─────────────────────────────────────────────────────── */
.afinbox-chat-widget-button {
	width: 64px;
	height: 64px;
	border-radius: 50%;
	background: linear-gradient(135deg, var(--afl-brand) 0%, var(--afl-brand-dark) 100%);
	border: none;
	cursor: pointer;
	box-shadow: 0 4px 20px rgba(224, 123, 57, 0.5);
	display: flex;
	align-items: center;
	justify-content: center;
	transition: transform 0.25s ease, box-shadow 0.25s ease;
	position: relative;
	animation: afinbox-pulse 2.5s infinite;
}

.afinbox-chat-widget-button:hover {
	transform: scale(1.08);
	box-shadow: 0 6px 28px rgba(224, 123, 57, 0.65);
}

.afinbox-chat-widget-button.is-open {
	animation: none;
	background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
	box-shadow: 0 4px 20px rgba(239, 68, 68, 0.45);
}

@keyframes afinbox-pulse {
	0%,100% { box-shadow: 0 4px 20px rgba(224,123,57,0.5); }
	50%      { box-shadow: 0 4px 36px rgba(224,123,57,0.8); }
}

.afinbox-chat-widget-button .dashicons {
	color: var(--afl-white);
	font-size: 28px;
	width: 28px;
	height: 28px;
	transition: transform 0.3s ease;
}

.afinbox-chat-widget-button.is-open .dashicons {
	transform: rotate(90deg);
}

.afinbox-chat-widget-button .badge {
	position: absolute;
	top: -4px;
	right: -4px;
	background: #ef4444;
	color: var(--afl-white);
	border-radius: 50%;
	width: 20px;
	height: 20px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 11px;
	font-weight: 700;
	border: 2px solid var(--afl-white);
}

/* ── Nudge bubble ──────────────────────────────────────────────────────── */
#afinbox-chat-nudge {
	position: fixed;
	bottom: 100px;
	right: 24px;
	background: var(--afl-white);
	color: var(--afl-text);
	padding: 14px 20px 14px 16px;
	border-radius: 16px 16px 4px 16px;
	box-shadow: 0 4px 24px rgba(0,0,0,0.13);
	z-index: 999998;
	font-size: 14px;
	max-width: 270px;
	line-height: 1.6;
	display: none;
	animation: afinbox-nudge-in 0.4s ease;
	cursor: pointer;
	border-left: 3px solid var(--afl-brand);
}

#afinbox-chat-nudge::after {
	content: '';
	position: absolute;
	bottom: -8px;
	right: 20px;
	width: 16px;
	height: 16px;
	background: var(--afl-white);
	transform: rotate(45deg);
	border-right: 1px solid var(--afl-border);
	border-bottom: 1px solid var(--afl-border);
}

#afinbox-nudge-close {
	position: absolute;
	top: 6px;
	right: 8px;
	background: none;
	border: none;
	font-size: 16px;
	color: #9ca3af;
	cursor: pointer;
	padding: 2px;
	line-height: 1;
	transition: color 0.15s;
}

#afinbox-nudge-close:hover { color: #4b5563; }

@keyframes afinbox-nudge-in {
	from { opacity: 0; transform: translateY(10px) scale(0.95); }
	to   { opacity: 1; transform: translateY(0)    scale(1);    }
}

/* ── Chat window ───────────────────────────────────────────────────────── */
.afinbox-chat-widget-container {
	position: fixed;
	bottom: 100px;
	right: 24px;
	width: 380px;
	max-width: calc(100vw - 32px);
	height: 560px;
	max-height: calc(100vh - 140px);
	background: var(--afl-white);
	border-radius: var(--afl-radius);
	box-shadow: var(--afl-shadow);
	display: none;
	flex-direction: column;
	overflow: hidden;
	z-index: 999998;
	border: 1px solid var(--afl-border);
}

.afinbox-chat-widget-container.active {
	display: flex;
	animation: afinbox-slide-up 0.3s cubic-bezier(0.34,1.3,0.64,1);
}

@keyframes afinbox-slide-up {
	from { opacity: 0; transform: translateY(24px) scale(0.97); }
	to   { opacity: 1; transform: translateY(0)    scale(1);    }
}

/* ── Header ────────────────────────────────────────────────────────────── */
.afinbox-chat-widget-header {
	background: linear-gradient(135deg, var(--afl-brand) 0%, var(--afl-brand-dark) 100%);
	color: var(--afl-white);
	padding: 16px 18px;
	display: flex;
	align-items: center;
	gap: 12px;
	flex-shrink: 0;
	position: relative;
}

/* subtle pattern overlay */
.afinbox-chat-widget-header::before {
	content: '';
	position: absolute;
	inset: 0;
	background: url("data:image/svg+xml,%3Csvg width='40' height='40' viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23fff' fill-opacity='0.04'%3E%3Ccircle cx='20' cy='20' r='10'/%3E%3C/g%3E%3C/svg%3E");
	pointer-events: none;
	border-radius: var(--afl-radius) var(--afl-radius) 0 0;
}

/* search bar in header */
.afinbox-chat-widget-search {
	margin: 0 18px 12px;
	position: relative;
	flex-shrink: 0;
}

.afinbox-chat-widget-search input {
	width: 100%;
	padding: 8px 12px 8px 34px;
	border-radius: 20px;
	border: none;
	background: rgba(255,255,255,0.22);
	color: var(--afl-white);
	font-size: 13px;
	outline: none;
	transition: background 0.2s;
	box-sizing: border-box;
}

.afinbox-chat-widget-search input::placeholder { color: rgba(255,255,255,0.7); }
.afinbox-chat-widget-search input:focus { background: rgba(255,255,255,0.3); }

.afinbox-chat-widget-search::before {
	content: '🔍';
	position: absolute;
	left: 10px;
	top: 50%;
	transform: translateY(-50%);
	font-size: 13px;
	pointer-events: none;
}

.afinbox-chat-header-avatar {
	width: 44px;
	height: 44px;
	border-radius: 50%;
	background: rgba(255,255,255,0.22);
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 22px;
	flex-shrink: 0;
	position: relative;
	border: 2px solid rgba(255,255,255,0.35);
}

.afinbox-chat-header-avatar::after {
	content: '';
	position: absolute;
	bottom: 1px;
	right: 1px;
	width: 11px;
	height: 11px;
	border-radius: 50%;
	background: var(--afl-green);
	border: 2px solid var(--afl-white);
}

.afinbox-chat-header-info { flex: 1; }

.afinbox-chat-widget-header h3 {
	margin: 0 0 2px;
	font-size: 15px;
	font-weight: 700;
	line-height: 1.2;
}

.afinbox-chat-header-status {
	font-size: 12px;
	opacity: 0.9;
	display: flex;
	align-items: center;
	gap: 5px;
}

.afinbox-chat-header-status::before {
	content: '';
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--afl-green);
	display: inline-block;
	animation: afinbox-status-blink 2s infinite;
}

@keyframes afinbox-status-blink {
	0%,100% { opacity: 1; }
	50%      { opacity: 0.5; }
}

.afinbox-chat-header-ai-badge {
	background: rgba(255,255,255,0.2);
	border: 1px solid rgba(255,255,255,0.35);
	border-radius: 20px;
	padding: 3px 10px;
	font-size: 11px;
	font-weight: 600;
	letter-spacing: 0.3px;
	white-space: nowrap;
}

.afinbox-chat-widget-header .close-btn {
	background: rgba(255,255,255,0.18);
	border: none;
	color: var(--afl-white);
	width: 32px;
	height: 32px;
	border-radius: 50%;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.2s;
	flex-shrink: 0;
}

.afinbox-chat-widget-header .close-btn:hover {
	background: rgba(255,255,255,0.32);
}

/* ── Messages area ─────────────────────────────────────────────────────── */
.afinbox-chat-widget-messages {
	flex: 1;
	overflow-y: auto;
	padding: 16px;
	background: var(--afl-bg);
	display: flex;
	flex-direction: column;
	gap: 12px;
	scrollbar-width: thin;
	scrollbar-color: #cbd5e1 transparent;
}

.afinbox-chat-widget-messages::-webkit-scrollbar { width: 4px; }
.afinbox-chat-widget-messages::-webkit-scrollbar-track { background: transparent; }
.afinbox-chat-widget-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

/* date divider */
.afinbox-chat-date-divider {
	text-align: center;
	font-size: 11px;
	color: var(--afl-muted);
	margin: 4px 0;
	display: flex;
	align-items: center;
	gap: 8px;
}

.afinbox-chat-date-divider::before,
.afinbox-chat-date-divider::after {
	content: '';
	flex: 1;
	height: 1px;
	background: var(--afl-border);
}

.afinbox-chat-widget-message {
	display: flex;
	gap: 10px;
	max-width: 84%;
	animation: afinbox-fade-in 0.25s ease;
}

@keyframes afinbox-fade-in {
	from { opacity: 0; transform: translateY(6px); }
	to   { opacity: 1; transform: translateY(0);   }
}

.afinbox-chat-widget-message.customer {
	align-self: flex-end;
	flex-direction: row-reverse;
}

.afinbox-chat-widget-message.agent {
	align-self: flex-start;
}

.afinbox-chat-widget-message-avatar {
	width: 32px;
	height: 32px;
	border-radius: 50%;
	background: linear-gradient(135deg, var(--afl-brand) 0%, var(--afl-brand-dark) 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	color: var(--afl-white);
	font-weight: 700;
	font-size: 13px;
	flex-shrink: 0;
}

.afinbox-chat-widget-message.agent .afinbox-chat-widget-message-avatar {
	background: linear-gradient(135deg, #334155 0%, #1e293b 100%);
}

.afinbox-chat-widget-message-content {
	background: var(--afl-white);
	padding: 10px 14px;
	border-radius: 4px 14px 14px 14px;
	box-shadow: 0 1px 3px rgba(0,0,0,0.07);
	border: 1px solid var(--afl-border);
}

.afinbox-chat-widget-message.customer .afinbox-chat-widget-message-content {
	background: linear-gradient(135deg, var(--afl-brand) 0%, var(--afl-brand-dark) 100%);
	color: var(--afl-white);
	border-radius: 14px 4px 14px 14px;
	border: none;
	box-shadow: 0 2px 8px rgba(224,123,57,0.3);
}

.afinbox-chat-widget-message-text {
	margin: 0;
	font-size: 14px;
	line-height: 1.55;
	color: var(--afl-text);
	word-break: break-word;
}

.afinbox-chat-widget-message-text a { color: var(--afl-brand); text-decoration: underline; }

.afinbox-chat-widget-message.customer .afinbox-chat-widget-message-text { color: var(--afl-white); }
.afinbox-chat-widget-message.customer .afinbox-chat-widget-message-text a { color: rgba(255,255,255,0.85); }

.afinbox-chat-widget-message-time {
	font-size: 11px;
	color: var(--afl-muted);
	margin-top: 5px;
	display: flex;
	align-items: center;
	gap: 4px;
}

.afinbox-chat-widget-message.customer .afinbox-chat-widget-message-time {
	color: rgba(255,255,255,0.72);
	justify-content: flex-end;
}

/* ── Typing indicator ──────────────────────────────────────────────────── */
.afinbox-chat-typing .afinbox-chat-widget-message-content {
	padding: 12px 16px;
	display: flex;
	gap: 5px;
	align-items: center;
}

.afinbox-typing-dot {
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: #94a3b8;
	animation: afinbox-dot-bounce 1.4s infinite ease-in-out;
}

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

@keyframes afinbox-dot-bounce {
	0%,60%,100% { transform: translateY(0);    opacity: 0.5; }
	30%         { transform: translateY(-8px); opacity: 1;   }
}

/* ── Input area ────────────────────────────────────────────────────────── */
.afinbox-chat-widget-input-area {
	padding: 12px 14px;
	background: var(--afl-white);
	border-top: 1px solid var(--afl-border);
	flex-shrink: 0;
	display: flex;
	gap: 10px;
	align-items: flex-end;
}

.afinbox-chat-widget-attach-btn {
	background: none;
	border: none;
	color: var(--afl-muted);
	cursor: pointer;
	padding: 6px;
	border-radius: 8px;
	transition: color 0.15s, background 0.15s;
	display: flex;
	align-items: center;
	flex-shrink: 0;
}

.afinbox-chat-widget-attach-btn:hover {
	color: var(--afl-brand);
	background: var(--afl-brand-light);
}

.afinbox-chat-widget-input {
	flex: 1;
	padding: 11px 16px;
	border: 1.5px solid var(--afl-border);
	border-radius: 24px;
	font-size: 14px;
	resize: none;
	max-height: 100px;
	font-family: inherit;
	transition: border-color 0.2s, box-shadow 0.2s;
	color: var(--afl-text);
	background: var(--afl-bg);
	line-height: 1.45;
}

.afinbox-chat-widget-input:focus {
	outline: none;
	border-color: var(--afl-brand);
	background: var(--afl-white);
	box-shadow: 0 0 0 3px rgba(224,123,57,0.14);
}

.afinbox-chat-widget-send-btn {
	width: 44px;
	height: 44px;
	border-radius: 50%;
	background: linear-gradient(135deg, var(--afl-brand) 0%, var(--afl-brand-dark) 100%);
	border: none;
	color: var(--afl-white);
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: transform 0.2s, box-shadow 0.2s;
	flex-shrink: 0;
	box-shadow: 0 2px 8px rgba(224,123,57,0.35);
}

.afinbox-chat-widget-send-btn:hover {
	transform: scale(1.07);
	box-shadow: 0 4px 16px rgba(224,123,57,0.5);
}

.afinbox-chat-widget-send-btn:disabled {
	opacity: 0.4;
	cursor: not-allowed;
	transform: none;
	box-shadow: none;
}

.afinbox-chat-widget-send-btn .dashicons {
	font-size: 20px;
	width: 20px;
	height: 20px;
}

/* ── Powered-by footer ─────────────────────────────────────────────────── */
.afinbox-chat-widget-footer {
	text-align: center;
	padding: 6px 0 8px;
	font-size: 11px;
	color: #cbd5e1;
	background: var(--afl-white);
	border-top: 1px solid #f1f5f9;
	flex-shrink: 0;
}

.afinbox-chat-widget-footer a {
	color: var(--afl-brand);
	text-decoration: none;
	font-weight: 600;
}

/* ── Empty state ───────────────────────────────────────────────────────── */
.afinbox-chat-widget-empty {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	flex: 1;
	color: var(--afl-muted);
	text-align: center;
	padding: 40px 20px;
}

.afinbox-chat-widget-empty .dashicons {
	font-size: 48px;
	width: 48px;
	height: 48px;
	margin-bottom: 16px;
	opacity: 0.35;
}

/* ── RTL support ───────────────────────────────────────────────────────── */
[dir="rtl"] .afinbox-chat-widget          { right: auto; left: 24px; }
[dir="rtl"] .afinbox-chat-widget-container { right: auto; left: 24px; }
[dir="rtl"] #afinbox-chat-nudge            { right: auto; left: 24px; border-radius: 16px 16px 16px 4px; }

/* ── Mobile ────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
	.afinbox-chat-widget-container {
		width: calc(100vw - 24px);
		height: calc(100vh - 100px);
		bottom: 84px;
		right: 12px;
	}
	.afinbox-chat-widget { bottom: 16px; right: 12px; }
	#afinbox-chat-nudge  { right: 16px; max-width: calc(100vw - 80px); }
}
