/* ==========================================================================
   AI News Assistant — Chat Widget
   Vanilla CSS, no preprocessor.
   Uses CSS custom properties for theming (light / dark).
   ========================================================================== */

/* ── Widget root ──────────────────────────────────────────────────────────── */

.aina-widget {
	/* Light theme tokens (default) */
	--aina-primary:             #6366f1;
	--aina-primary-hover:       #4f46e5;
	--aina-primary-focus-ring:  rgba( 99, 102, 241, 0.25 );
	--aina-bg:                  #ffffff;
	--aina-surface:             #f9fafb;
	--aina-border:              #e5e7eb;
	--aina-text:                #111827;
	--aina-text-muted:          #6b7280;
	--aina-user-bg:             var( --aina-primary );
	--aina-user-text:           #ffffff;
	--aina-bot-bg:              #f3f4f6;
	--aina-bot-text:            #111827;
	--aina-input-bg:            #ffffff;
	--aina-input-border:        #d1d5db;
	--aina-header-bg:           var( --aina-primary );
	--aina-header-text:         #ffffff;
	--aina-error-bg:            #fef2f2;
	--aina-error-border:        #fca5a5;
	--aina-error-text:          #991b1b;
	--aina-radius:              12px;
	--aina-radius-sm:           8px;
	--aina-shadow:              0 4px 24px rgba( 0, 0, 0, 0.10 );

	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
	font-size: 15px;
	line-height: 1.5;
	color: var( --aina-text );
	background: var( --aina-bg );
	border-radius: var( --aina-radius );
	box-shadow: var( --aina-shadow );
	display: flex;
	flex-direction: column;
	overflow: hidden;
	width: 100%;
	max-width: 700px;
	min-height: 400px;
	max-height: 680px;
	box-sizing: border-box;
}

.aina-widget *,
.aina-widget *::before,
.aina-widget *::after {
	box-sizing: inherit;
}

/* ── Dark theme ───────────────────────────────────────────────────────────── */

.aina-theme-dark {
	--aina-bg:            #1f2937;
	--aina-surface:       #111827;
	--aina-border:        #374151;
	--aina-text:          #f9fafb;
	--aina-text-muted:    #9ca3af;
	--aina-bot-bg:        #374151;
	--aina-bot-text:      #f9fafb;
	--aina-input-bg:      #374151;
	--aina-input-border:  #4b5563;
	--aina-header-bg:     #111827;
	--aina-error-bg:      #450a0a;
	--aina-error-border:  #7f1d1d;
	--aina-error-text:    #fca5a5;
}

/* ── Accessibility: screen-reader-only text ───────────────────────────────── */

.aina-widget .screen-reader-text {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect( 0, 0, 0, 0 );
	white-space: nowrap;
	border: 0;
}

/* ── Header ───────────────────────────────────────────────────────────────── */

.aina-header {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 14px 18px;
	background: var( --aina-header-bg );
	color: var( --aina-header-text );
	flex-shrink: 0;
}

.aina-avatar {
	width: 32px;
	height: 32px;
	border-radius: 50%;
	object-fit: cover;
	flex-shrink: 0;
	display: block;
}

.aina-avatar-default {
	width: 32px;
	height: 32px;
	border-radius: 50%;
	background: rgba( 255, 255, 255, 0.20 );
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	color: inherit;
}

.aina-assistant-name {
	font-weight: 600;
	font-size: 15px;
}

/* ── Messages log ─────────────────────────────────────────────────────────── */

.aina-messages {
	flex: 1;
	overflow-y: auto;
	padding: 16px;
	display: flex;
	flex-direction: column;
	gap: 12px;
	scroll-behavior: smooth;
	background: var( --aina-bg );
	min-height: 0;
}

.aina-messages:focus {
	outline: none;
}

/* ── Empty / welcome state ────────────────────────────────────────────────── */

.aina-empty-state {
	display: flex;
	align-items: center;
	justify-content: center;
	flex: 1;
	padding: 24px 16px;
	text-align: center;
}

.aina-empty-state[hidden] {
	display: none;
}

.aina-welcome-msg {
	margin: 0;
	color: var( --aina-text-muted );
	font-size: 14px;
	line-height: 1.65;
	max-width: 320px;
}

/* ── Message bubbles ──────────────────────────────────────────────────────── */

.aina-message {
	display: flex;
	max-width: 85%;
	animation: aina-fadein 0.18s ease;
}

@keyframes aina-fadein {
	from { opacity: 0; transform: translateY( 5px ); }
	to   { opacity: 1; transform: translateY( 0 ); }
}

.aina-message-user {
	align-self: flex-end;
}

.aina-message-assistant {
	align-self: flex-start;
	flex-direction: column;
}

.aina-message-content {
	padding: 10px 14px;
	border-radius: var( --aina-radius-sm );
	font-size: 14px;
	line-height: 1.65;
	word-break: break-word;
	overflow-wrap: break-word;
}

.aina-message-user .aina-message-content {
	background: var( --aina-user-bg );
	color: var( --aina-user-text );
	border-bottom-right-radius: 2px;
}

.aina-message-assistant .aina-message-content {
	background: var( --aina-bot-bg );
	color: var( --aina-bot-text );
	border-bottom-left-radius: 2px;
}

/* ── Loading / typing indicator ───────────────────────────────────────────── */

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

.aina-dots {
	display: flex;
	align-items: center;
	gap: 5px;
	padding: 12px 16px;
	background: var( --aina-bot-bg );
	border-radius: var( --aina-radius-sm );
	border-bottom-left-radius: 2px;
}

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

.aina-dots span:nth-child( 2 ) { animation-delay: 0.20s; }
.aina-dots span:nth-child( 3 ) { animation-delay: 0.40s; }

@keyframes aina-bounce {
	0%, 60%, 100% { transform: translateY( 0 ); opacity: 0.45; }
	30%            { transform: translateY( -6px ); opacity: 1; }
}

/* ── Citations ────────────────────────────────────────────────────────────── */

.aina-citations {
	margin-top: 10px;
	padding-top: 10px;
	border-top: 1px solid var( --aina-border );
	width: 100%;
}

.aina-citations-heading {
	margin: 0 0 6px;
	font-size: 11px;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.06em;
	color: var( --aina-text-muted );
}

.aina-citation-list {
	list-style: none;
	margin: 0;
	padding: 0;
	display: flex;
	flex-direction: column;
	gap: 5px;
}

.aina-citation-list li {
	display: flex;
	align-items: baseline;
	gap: 8px;
	font-size: 13px;
}

.aina-citation-link {
	color: var( --aina-primary );
	text-decoration: none;
	font-weight: 500;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	max-width: 280px;
	display: inline-block;
	vertical-align: bottom;
}

.aina-citation-link:hover {
	text-decoration: underline;
}

.aina-citation-link:focus-visible {
	outline: 2px solid var( --aina-primary );
	outline-offset: 2px;
	border-radius: 2px;
}

.aina-citation-date {
	color: var( --aina-text-muted );
	font-size: 12px;
	flex-shrink: 0;
	white-space: nowrap;
}

/* ── Error banner ─────────────────────────────────────────────────────────── */

.aina-error-banner {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 10px 14px;
	background: var( --aina-error-bg );
	border-top: 1px solid var( --aina-error-border );
	color: var( --aina-error-text );
	font-size: 13px;
	flex-shrink: 0;
	line-height: 1.4;
}

.aina-error-banner[hidden] {
	display: none;
}

.aina-error-banner > svg {
	flex-shrink: 0;
}

.aina-error-text {
	flex: 1;
}

.aina-error-dismiss {
	background: none;
	border: none;
	padding: 2px;
	cursor: pointer;
	color: inherit;
	opacity: 0.7;
	display: flex;
	align-items: center;
	flex-shrink: 0;
	border-radius: 3px;
}

.aina-error-dismiss:hover {
	opacity: 1;
}

.aina-error-dismiss:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* ── Suggested question chips ─────────────────────────────────────────────── */

.aina-suggestions {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	padding: 8px 16px 12px;
	background: var( --aina-bg );
	flex-shrink: 0;
}

.aina-suggestions[hidden] {
	display: none;
}

.aina-suggestion-btn {
	background: var( --aina-surface );
	border: 1px solid var( --aina-border );
	border-radius: 20px;
	padding: 6px 14px;
	font-size: 13px;
	font-family: inherit;
	cursor: pointer;
	color: var( --aina-text );
	line-height: 1.4;
	text-align: left;
	transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.aina-suggestion-btn:hover {
	background: var( --aina-primary-focus-ring );
	border-color: var( --aina-primary );
	color: var( --aina-primary );
}

.aina-suggestion-btn:focus-visible {
	outline: 2px solid var( --aina-primary );
	outline-offset: 2px;
}

/* ── Input form ───────────────────────────────────────────────────────────── */

.aina-form {
	padding: 12px 16px;
	border-top: 1px solid var( --aina-border );
	background: var( --aina-surface );
	flex-shrink: 0;
}

.aina-input-row {
	display: flex;
	align-items: flex-end;
	gap: 8px;
}

.aina-input {
	flex: 1;
	resize: none;
	border: 1px solid var( --aina-input-border );
	border-radius: var( --aina-radius-sm );
	padding: 10px 12px;
	font-size: 14px;
	font-family: inherit;
	line-height: 1.45;
	background: var( --aina-input-bg );
	color: var( --aina-text );
	max-height: 120px;
	overflow-y: auto;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

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

.aina-input:focus {
	outline: none;
	border-color: var( --aina-primary );
	box-shadow: 0 0 0 3px var( --aina-primary-focus-ring );
}

.aina-send-btn {
	background: var( --aina-primary );
	color: #ffffff;
	border: none;
	border-radius: var( --aina-radius-sm );
	padding: 10px 12px;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	transition: background 0.15s ease;
	line-height: 1;
}

.aina-send-btn:hover:not( :disabled ) {
	background: var( --aina-primary-hover );
}

.aina-send-btn:focus-visible {
	outline: 2px solid var( --aina-primary );
	outline-offset: 2px;
}

.aina-send-btn:disabled {
	opacity: 0.40;
	cursor: not-allowed;
}

/* ── Popup close button (inside widget header, popup mode only) ────────────── */

.aina-popup-close {
	margin-left: auto;
	background: none;
	border: none;
	padding: 4px 6px;
	cursor: pointer;
	color: inherit;
	opacity: 0.75;
	display: flex;
	align-items: center;
	flex-shrink: 0;
	border-radius: 4px;
	line-height: 1;
}

.aina-popup-close:hover {
	opacity: 1;
}

.aina-popup-close:focus-visible {
	outline: 2px solid currentColor;
	outline-offset: 2px;
}

/* ── Floating popup root ──────────────────────────────────────────────────── */

.aina-popup-root {
	position: fixed;
	z-index: 9999999;
	bottom: 24px;
}

.aina-popup-position-bottom-right {
	right: 24px;
}

.aina-popup-position-bottom-left {
	left: 24px;
}

/* ── Floating trigger button ─────────────────────────────────────────────── */

.aina-popup-trigger {
	width: 56px;
	height: 56px;
	border-radius: 50%;
	background: var( --aina-popup-primary, #6366f1 );
	color: #ffffff;
	border: none;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 4px 16px rgba( 0, 0, 0, 0.28 );
	transition: transform 0.18s ease, box-shadow 0.18s ease;
	position: relative;
	z-index: 1;
}

.aina-popup-trigger:hover {
	transform: scale( 1.08 );
	box-shadow: 0 6px 24px rgba( 0, 0, 0, 0.34 );
}

.aina-popup-trigger:focus-visible {
	outline: 3px solid var( --aina-popup-primary, #6366f1 );
	outline-offset: 4px;
}

/* Icon switching */

.aina-popup-icon-close {
	display: none;
}

.aina-popup-root.is-open .aina-popup-icon-open {
	display: none;
}

.aina-popup-root.is-open .aina-popup-icon-close {
	display: flex;
}

/* ── Popup panel ─────────────────────────────────────────────────────────── */

.aina-popup-panel {
	position: absolute;
	bottom: 68px;
	width: 380px;
	visibility: hidden;
	opacity: 0;
	transform: scale( 0.92 ) translateY( 12px );
	transform-origin: bottom center;
	transition:
		opacity 0.20s ease,
		transform 0.22s cubic-bezier( 0.34, 1.56, 0.64, 1 ),
		visibility 0s linear 0.22s;
	pointer-events: none;
}

.aina-popup-position-bottom-right .aina-popup-panel {
	right: 0;
}

.aina-popup-position-bottom-left .aina-popup-panel {
	left: 0;
}

.aina-popup-root.is-open .aina-popup-panel {
	visibility: visible;
	opacity: 1;
	transform: scale( 1 ) translateY( 0 );
	transition:
		opacity 0.20s ease,
		transform 0.22s cubic-bezier( 0.34, 1.56, 0.64, 1 ),
		visibility 0s linear 0s;
	pointer-events: all;
}

/* Widget sizing inside popup */

.aina-popup-panel .aina-widget {
	width: 100%;
	max-width: 100%;
	max-height: 520px;
	min-height: 360px;
}

/* ── Reduced motion ───────────────────────────────────────────────────────── */

@media ( prefers-reduced-motion: reduce ) {
	.aina-message { animation: none; }
	.aina-dots span { animation: none; opacity: 1; }
	.aina-messages { scroll-behavior: auto; }
	.aina-popup-panel { transition: none; }
	.aina-popup-trigger { transition: none; }
}

/* ── Responsive ───────────────────────────────────────────────────────────── */

@media ( max-width: 480px ) {
	.aina-widget {
		max-height: 100%;
		border-radius: 0;
		box-shadow: none;
	}

	.aina-message {
		max-width: 92%;
	}

	.aina-citation-link {
		max-width: 200px;
	}

	/* Popup panel: full-width on mobile, 16px margin each side */
	.aina-popup-root {
		bottom: 16px;
	}

	.aina-popup-position-bottom-right {
		right: 16px;
	}

	.aina-popup-position-bottom-left {
		left: 16px;
	}

	.aina-popup-panel {
		width: calc( 100vw - 32px );
		right: 0;
		left: auto;
		bottom: 72px;
	}

	.aina-popup-position-bottom-left .aina-popup-panel {
		left: 0;
		right: auto;
	}

	.aina-popup-panel .aina-widget {
		max-height: 80vh;
	}
}
