/**
 * AIcore chat widget — theme design tokens + MessageScroller layout.
 * Brand: primary-600 #d32a2a, ink #222, surface #F8F8F8, line-soft #EDEDED.
 */

.aicore-widget {
	--aicore-primary: #d32a2a;
	--aicore-primary-hover: #ad2323;
	--aicore-primary-soft: #fef3f3;
	--aicore-ring: rgba(240, 48, 48, 0.15);
	--aicore-ink: #222222;
	--aicore-muted: #9ca3af;
	--aicore-surface: #f8f8f8;
	--aicore-line: #dedede;
	--aicore-line-soft: #ededed;
	font-family: "Inter Tight", Inter, "Arial Narrow", MiSans, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
	font-size: 14px;
	line-height: 1.6;
	box-sizing: border-box;
}
.aicore-widget *, .aicore-widget *::before, .aicore-widget *::after { box-sizing: border-box; }

.aicore-floating {
	position: fixed;
	bottom: 24px;
	right: 24px;
	z-index: 999999;
}

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

.aicore-bubble {
	position: relative;
	width: 56px;
	height: 56px;
	border-radius: 50%;
	background: var(--aicore-primary);
	color: #fff;
	border: none;
	cursor: pointer;
	box-shadow: 0 4px 14px rgba(211, 42, 42, 0.28);
	transition: background 0.2s ease, transform 0.2s ease;
	display: flex;
	align-items: center;
	justify-content: center;
}
.aicore-bubble:hover { background: var(--aicore-primary-hover); transform: scale(1.04); }
.aicore-bubble-icon {
	position: absolute;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: opacity 0.2s ease, transform 0.2s ease;
}
.aicore-bubble-icon svg { width: 26px; height: 26px; }
.aicore-bubble-icon--close { opacity: 0; transform: rotate(-90deg) scale(0.5); }
.is-open .aicore-bubble-icon--chat { opacity: 0; transform: rotate(90deg) scale(0.5); }
.is-open .aicore-bubble-icon--close { opacity: 1; transform: rotate(0) scale(1); }

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

.aicore-panel {
	position: absolute;
	bottom: 72px;
	right: 0;
	width: 400px;
	max-width: calc(100vw - 32px);
	height: min(640px, 78vh);
	background: #fff;
	border: 1px solid var(--aicore-line-soft);
	border-radius: 20px;
	box-shadow: 0 16px 40px rgba(34, 34, 34, 0.14);
	display: flex;
	flex-direction: column;
	overflow: hidden;
	transform-origin: bottom right;
	transition: opacity 0.2s ease, transform 0.2s ease;
}
.aicore-panel.is-collapsed {
	opacity: 0;
	pointer-events: none;
	transform: scale(0.92) translateY(10px);
}
.aicore-inline .aicore-panel {
	position: static;
	width: 100%;
	height: 640px;
	max-height: none;
	box-shadow: none;
	border-radius: 14px;
}

/* ---- Header ---- */

.aicore-header {
	background: #fff;
	color: var(--aicore-ink);
	padding: 14px 16px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
	border-bottom: 1px solid var(--aicore-line-soft);
	flex-shrink: 0;
}
.aicore-header-info { display: flex; align-items: center; gap: 10px; min-width: 0; }
.aicore-header-avatar {
	width: 36px;
	height: 36px;
	border-radius: 50%;
	background: var(--aicore-primary-soft);
	color: var(--aicore-primary);
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}
.aicore-header-avatar svg { width: 18px; height: 18px; }
.aicore-header-text { display: flex; flex-direction: column; min-width: 0; }
.aicore-header-title {
	font-weight: 600;
	font-size: 15px;
	line-height: 1.3;
	color: var(--aicore-ink);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
.aicore-header-status {
	display: flex;
	align-items: center;
	gap: 5px;
	font-size: 11px;
	color: var(--aicore-muted);
}
.aicore-header-status i {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: #22c55e;
	box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.2);
}
.aicore-header-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
.aicore-icon-btn {
	width: 32px;
	height: 32px;
	border-radius: 8px;
	background: transparent;
	border: none;
	color: #6b7280;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.15s ease, color 0.15s ease;
}
.aicore-icon-btn:hover { color: var(--aicore-ink); background: var(--aicore-surface); }
.aicore-icon-btn svg { width: 16px; height: 16px; }

/* ---- MessageScroller frame ---- */

.aicore-scroller {
	position: relative;
	flex: 1;
	min-height: 0;
	display: flex;
	flex-direction: column;
	background: var(--aicore-surface);
}

.aicore-scroller-viewport {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	overflow-x: hidden;
	overscroll-behavior: contain;
	-webkit-overflow-scrolling: touch;
	scroll-behavior: smooth;
	outline: none;
	scrollbar-width: thin;
	scrollbar-color: rgba(156, 163, 175, 0.28) transparent;
}
.aicore-scroller-viewport:focus-visible {
	box-shadow: inset 0 0 0 2px var(--aicore-ring);
}
.aicore-scroller-viewport::-webkit-scrollbar { width: 3px; }
.aicore-scroller-viewport::-webkit-scrollbar-button { display: none; width: 0; height: 0; }
.aicore-scroller-viewport::-webkit-scrollbar-track { background: transparent; }
.aicore-scroller-viewport::-webkit-scrollbar-thumb {
	background: rgba(156, 163, 175, 0.28);
	border-radius: 999px;
}
.aicore-scroller-viewport::-webkit-scrollbar-thumb:hover { background: rgba(107, 114, 128, 0.5); }

.aicore-scroller-content {
	display: flex;
	flex-direction: column;
	gap: 16px;
	padding: 16px 14px 20px;
	min-height: 100%;
}

/* Transcript rows — full width, off-screen paint skipped */
.aicore-scroller-item {
	width: 100%;
	display: flex;
	flex-direction: column;
	content-visibility: auto;
	contain-intrinsic-size: auto 72px;
}
.aicore-scroller-item--user { align-items: flex-end; }
.aicore-scroller-item--ai { align-items: flex-start; }

/* User turn entrance — rises from live edge, no height animation */
.aicore-scroller-item.is-entering {
	animation: aicore-turn-enter 0.28s ease both;
}
@keyframes aicore-turn-enter {
	from { opacity: 0; transform: translateY(12px); }
	to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
	.aicore-scroller-item.is-entering { animation: none; }
	.aicore-scroller-viewport { scroll-behavior: auto; }
}

/* Jump-to-latest — MessageScrollerButton */
.aicore-scroller-button {
	position: absolute;
	left: 50%;
	bottom: 12px;
	transform: translateX(-50%);
	z-index: 2;
	display: inline-flex;
	align-items: center;
	gap: 4px;
	height: 34px;
	padding: 0 14px;
	border-radius: 999px;
	border: 1px solid var(--aicore-line);
	background: #fff;
	color: var(--aicore-ink);
	font-size: 12px;
	font-weight: 500;
	cursor: pointer;
	box-shadow: 0 4px 14px rgba(34, 34, 34, 0.1);
	transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
.aicore-scroller-button:hover {
	background: var(--aicore-surface);
	border-color: var(--aicore-line);
	transform: translateX(-50%) translateY(-1px);
}
.aicore-scroller-button svg { width: 14px; height: 14px; }
.aicore-scroller[data-autoscrolling="true"] .aicore-scroller-button {
	pointer-events: none;
	opacity: 0.7;
}

/* ---- Message bubbles ---- */

.aicore-message {
	display: flex;
	flex-direction: column;
	max-width: min(92%, 320px);
}
.aicore-message--user { align-items: flex-end; }
.aicore-message--ai { align-items: flex-start; max-width: 100%; }

.aicore-bubble-text {
	padding: 10px 14px;
	border-radius: 14px;
	word-wrap: break-word;
	overflow-wrap: break-word;
	min-width: 0;
	max-width: 100%;
}
.aicore-message--user .aicore-bubble-text {
	background: var(--aicore-ink);
	color: #fff;
	border-bottom-right-radius: 4px;
	white-space: pre-wrap;
}
.aicore-message--ai .aicore-bubble-text {
	background: #fff;
	color: var(--aicore-ink);
	border: 1px solid var(--aicore-line-soft);
	border-bottom-left-radius: 4px;
}

/* Markdown */
.aicore-bubble-text p { margin: 0 0 8px; }
.aicore-bubble-text p:last-child { margin-bottom: 0; }
.aicore-bubble-text ul, .aicore-bubble-text ol { margin: 0 0 8px; padding-left: 20px; }
.aicore-bubble-text ul:last-child, .aicore-bubble-text ol:last-child { margin-bottom: 0; }
.aicore-bubble-text li { margin: 3px 0; }
.aicore-heading { font-weight: 600; margin: 10px 0 6px; line-height: 1.4; color: var(--aicore-ink); }
.aicore-heading:first-child { margin-top: 0; }
.aicore-heading--1, .aicore-heading--2 { font-size: 15px; }
.aicore-heading--3, .aicore-heading--4 { font-size: 14px; }
.aicore-bubble-text a {
	color: var(--aicore-primary);
	text-decoration: underline;
	text-underline-offset: 2px;
	word-break: break-all;
}
.aicore-bubble-text a:hover { color: var(--aicore-primary-hover); }
.aicore-message--user .aicore-bubble-text a { color: #fff; }

.aicore-code {
	background: #111827;
	color: #f3f4f6;
	border-radius: 8px;
	padding: 10px 12px;
	margin: 8px 0;
	overflow-x: auto;
	font-size: 12.5px;
	line-height: 1.55;
}
.aicore-code code {
	font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
	background: none;
	color: inherit;
	padding: 0;
	white-space: pre;
}
.aicore-inline-code {
	font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
	background: #f3f4f6;
	color: #db2777;
	border-radius: 4px;
	padding: 1px 5px;
	font-size: 12.5px;
}
.aicore-message--user .aicore-inline-code {
	background: rgba(255, 255, 255, 0.18);
	color: #fff;
}

/* ---- Meta / sources ---- */

.aicore-msg-meta {
	display: flex;
	gap: 6px;
	margin-top: 4px;
	opacity: 0;
	transition: opacity 0.15s ease;
}
.aicore-scroller-item:hover .aicore-msg-meta { opacity: 1; }
.aicore-copy {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	background: transparent;
	border: none;
	color: var(--aicore-muted);
	font-size: 11px;
	cursor: pointer;
	padding: 2px 6px;
	border-radius: 6px;
	transition: color 0.15s ease, background 0.15s ease;
}
.aicore-copy:hover { color: var(--aicore-ink); background: var(--aicore-line-soft); }
.aicore-copy.is-copied { color: #16a34a; }
.aicore-copy svg { width: 12px; height: 12px; }

.aicore-sources {
	display: flex;
	flex-direction: column;
	gap: 6px;
	margin-top: 8px;
	width: 100%;
	max-width: 100%;
}
.aicore-sources-label {
	font-size: 11px;
	color: var(--aicore-muted);
	padding: 0 2px;
}
.aicore-sources-row {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
}

.aicore-source-chip {
	display: inline-flex;
	align-items: center;
	max-width: 100%;
	padding: 6px 12px;
	background: var(--aicore-surface);
	color: var(--aicore-ink);
	font-size: 12px;
	border-radius: 8px;
	text-decoration: none;
	border: 1px solid var(--aicore-line-soft);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	transition: border-color 0.15s ease, background 0.15s ease;
}
a.aicore-source-chip:hover {
	background: #fff;
	border-color: var(--aicore-line);
}

.aicore-download-chip {
	display: inline-flex;
	align-items: center;
	max-width: 100%;
	padding: 6px 12px;
	background: var(--aicore-primary);
	color: #fff;
	font-size: 12px;
	font-weight: 500;
	line-height: 1.4;
	border-radius: 8px;
	text-decoration: none;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	transition: background 0.15s ease;
}
.aicore-download-chip:hover { background: var(--aicore-primary-hover); }

/* ---- Typing indicator ---- */

.aicore-typing {
	flex-direction: row;
	gap: 4px;
	padding: 12px 16px;
	background: #fff;
	border: 1px solid var(--aicore-line-soft);
	border-radius: 14px;
	border-bottom-left-radius: 4px;
	width: fit-content;
}
.aicore-typing span {
	width: 7px;
	height: 7px;
	background: var(--aicore-muted);
	border-radius: 50%;
	display: inline-block;
	animation: aicore-blink 1.2s infinite ease-in-out;
}
.aicore-typing span:nth-child(2) { animation-delay: 0.2s; }
.aicore-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes aicore-blink {
	0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
	40% { opacity: 1; transform: translateY(-2px); }
}

/* ---- Composer ---- */

.aicore-form {
	display: flex;
	align-items: flex-end;
	gap: 8px;
	padding: 12px 14px;
	border-top: 1px solid var(--aicore-line-soft);
	background: #fff;
	flex-shrink: 0;
}
.aicore-input {
	flex: 1;
	padding: 10px 14px;
	border: 1px solid var(--aicore-line);
	border-radius: 12px;
	outline: none;
	font-size: 14px;
	font-family: inherit;
	line-height: 1.5;
	resize: none;
	max-height: 110px;
	background: var(--aicore-surface);
	color: var(--aicore-ink);
	transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.aicore-input::placeholder { color: var(--aicore-muted); }
.aicore-input:focus {
	border-color: #f03030;
	box-shadow: 0 0 0 3px var(--aicore-ring);
	background: #fff;
}
.aicore-send {
	width: 40px;
	height: 40px;
	flex-shrink: 0;
	background: var(--aicore-primary);
	color: #fff;
	border: none;
	border-radius: 50%;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.15s ease;
}
.aicore-send:hover:not(:disabled) { background: var(--aicore-primary-hover); }
.aicore-send:disabled { opacity: 0.5; cursor: not-allowed; }
.aicore-send svg { width: 17px; height: 17px; margin-right: 1px; }

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

@media (max-width: 480px) {
	.aicore-floating .aicore-panel {
		position: fixed;
		inset: 0;
		width: 100%;
		max-width: none;
		height: 100%;
		max-height: none;
		border-radius: 0;
		border: none;
	}
	.aicore-floating .aicore-panel .aicore-header { padding-top: max(14px, env(safe-area-inset-top)); }
	.aicore-floating .aicore-panel .aicore-form { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
}
