/* Backend AI agent chat: header icon, unread badge and the chat panel. */

/* --- Header menu icon (mirrors core's #tmenu .icon-alert recipe) --- */
#tmenu .icon-agent-chat {
	background: url(../../icons/agent-chat.svg) center center no-repeat;
	background-size: 16px !important;
	display: inline-block;
	margin-bottom: -2px;
	overflow: hidden;
	position: relative;
	text-indent: 28px;
	white-space: nowrap;
	width: 16px;
}

html[data-color-scheme="dark"] #tmenu .icon-agent-chat {
	background-image: url(../../icons/agent-chat--dark.svg);
}

/* Same shape as core's alerts <sup>; important for the JS-updated badge */
#tmenu sup.agent-chat-badge {
	background: var(--header-text, #fff);
	border-radius: 2px;
	color: var(--header-bg, #095f56);
	font-size: 0.6rem;
	font-weight: 400;
	left: 20px;
	padding: 2px;
	position: absolute;
	text-indent: 0;
	top: 5px;
}

/* --- Split view: push the whole backend aside instead of covering it ---
 *
 * The panel is a full-height column at the right edge of the viewport, so the
 * complete Contao UI - header/menu bar included - sits to its left. Shrinking
 * <body> reflows core's static #header and the flex #container (aside#left +
 * main#main) in one go, since #main's width is already percentage-based - no
 * core template changes needed. Popups (modal iframes) load this stylesheet
 * as well and must keep their full width.
 * The variable is kept in sync with the panel width by JS (drag-resize).
 */
html {
	--agent-chat-width: 380px;
}

html.agent-chat-open body:not(.popup) {
	margin-right: var(--agent-chat-width);
	transition: margin-right 0.2s ease;
}

/* --- Panel ---------------------------------------------------------- */
#agent-chat-root {
	--ac-bg: var(--content-bg, #fff);
	--ac-text: var(--text, #222);
	--ac-border: var(--border, #ddd);
	--ac-muted: #999;
	--ac-user-bg: #d9e7e5;
	--ac-assistant-bg: #f1f1f3;
	--ac-accent: #f47c00;
	bottom: 0;
	position: fixed;
	right: 0;
	top: 0;
	transform: translateX(100%);
	transition: transform 0.2s ease;
	width: var(--agent-chat-width);
	z-index: 5;
}

html.agent-chat-open #agent-chat-root {
	transform: translateX(0);
}

html[data-color-scheme="dark"] #agent-chat-root {
	--ac-bg: var(--content-bg, #1f1f22);
	--ac-text: var(--text, #ddd);
	--ac-border: var(--border, #3a3a3f);
	--ac-muted: #888;
	--ac-user-bg: #0e4a43;
	--ac-assistant-bg: #2b2b30;
	--ac-accent: #f47c00;
}

/* Grid: a narrow resizer column spanning the full height, next to a
 * header / messages / composer row stack. The messages row is the only
 * flexible one (1fr); header and composer size to their content, so the
 * composer row grows automatically as the textarea's JS-driven height
 * changes - no absolute positioning or manual height math required. */
#agent-chat-root .agent-chat__panel {
	background: var(--ac-bg);
	border-left: 1px solid var(--ac-border);
	box-shadow: -4px 0 16px rgba(0, 0, 0, 0.12);
	color: var(--ac-text);
	display: grid;
	grid-template-areas:
			"header header"
			"resizer messages"
			"resizer rules"
			"resizer composer";
	grid-template-columns: 4px 1fr;
	grid-template-rows: auto 1fr auto auto;
	height: 100%;
	overflow: hidden;
	width: 100%;
}

/* Drag handle on the panel's left edge to resize --agent-chat-width */
.agent-chat__resizer {
	background: transparent;
	cursor: col-resize;
	grid-area: resizer;
	touch-action: none;
	z-index: 1;
}

.agent-chat__resizer:hover::before,
.agent-chat__resizer--active::before {
	background: var(--ac-accent);
	width: 3px;
}

/* --- Narrow viewports: not enough room to split, fall back to an overlay --- */
@media (max-width: 1100px) {
	html.agent-chat-open body:not(.popup) {
		margin-right: 0;
	}

	#agent-chat-root {
		width: min(var(--agent-chat-width), 92vw);
		z-index: 9999;
	}

	#agent-chat-root .agent-chat__panel {
		box-shadow: -8px 0 32px rgba(0, 0, 0, 0.28);
	}
}

/* --- Header ---------------------------------------------------------- */
.agent-chat__header {
	align-items: center;
	background: var(--header-bg, #095f56);
	box-sizing: border-box;
	color: var(--header-text, #fff);
	display: flex;
	grid-area: header;
	justify-content: space-between;
	min-height: 40px; /* aligns with core's #header, which now sits left of the panel */
	padding: 8px 12px;
}

.agent-chat__title {
	font-weight: 600;
}

.agent-chat__btn {
	background: none;
	border: 0;
	border-radius: 4px;
	color: inherit;
	cursor: pointer;
	line-height: 0;
	margin-left: 2px;
	padding: 5px;
}

.agent-chat__btn:hover {
	background: rgba(255, 255, 255, 0.15);
}

/* --- Messages ---------------------------------------------------------- */
.agent-chat__messages {
	display: flex;
	flex-direction: column;
	gap: 8px;
	grid-area: messages;
	overflow-y: auto;
	padding: 12px;
}

.agent-chat__empty {
	color: var(--ac-muted);
	margin: auto;
	max-width: 80%;
	text-align: center;
}

.agent-chat__msg {
	border-radius: 10px;
	line-height: 1.45;
	max-width: 85%;
	overflow-wrap: break-word;
	padding: 8px 12px;
	white-space: normal;
}

.agent-chat__msg--user {
	align-self: flex-end;
	background: var(--ac-user-bg);
	border-bottom-right-radius: 2px;
}

.agent-chat__msg--assistant {
	align-self: flex-start;
	background: var(--ac-assistant-bg);
	border-bottom-left-radius: 2px;
}

.agent-chat__msg--tool {
	align-self: flex-start;
	background: none;
	box-sizing: border-box;
	color: var(--ac-muted);
	font-size: 0.85em;
	padding: 0 12px;
	width: 100%;
}

.agent-chat__msg--tool[data-tool] {
	cursor: pointer;
}

.agent-chat__msg--tool-expandable .agent-chat__tool-summary:hover {
	text-decoration: underline;
}

.agent-chat__tool-summary {
	align-items: center;
	display: flex;
	gap: 4px;
}

.agent-chat__tool-caret {
	color: var(--ac-muted);
	display: inline-flex;
	margin-left: auto;
	transition: transform 0.15s ease;
}

.agent-chat__msg--tool-expanded .agent-chat__tool-caret {
	transform: rotate(90deg);
}

.agent-chat__tool-details {
	background: rgba(0, 0, 0, 0.04);
	border-radius: 4px;
	margin-top: 6px;
	padding: 8px 10px;
}

.agent-chat__tool-details-label {
	font-size: 0.8em;
	font-weight: 600;
	margin-top: 8px;
	text-transform: uppercase;
}

.agent-chat__tool-details-label:first-child {
	margin-top: 0;
}

.agent-chat__tool-details pre {
	background: rgba(0, 0, 0, 0.06);
	border-radius: 4px;
	font-size: 0.85em;
	margin: 4px 0;
	overflow: auto;
	padding: 8px;
	white-space: pre-wrap;
	word-break: break-word;
}

html[data-color-scheme="dark"] .agent-chat__tool-details {
	background: rgba(255, 255, 255, 0.06);
}

html[data-color-scheme="dark"] .agent-chat__tool-details pre {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__msg--tool-failed {
	color: #c33;
}

.agent-chat__msg--tool-denied {
	color: #c33;
}

.agent-chat__tool-icon--approved {
	color: #2a8f4f;
}

.agent-chat__tool-icon--denied {
	color: #c33;
}

.agent-chat__msg pre {
	background: rgba(0, 0, 0, 0.08);
	border-radius: 4px;
	margin: 6px 0;
	overflow-x: auto;
	padding: 8px;
}

html[data-color-scheme="dark"] .agent-chat__msg pre {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__msg code {
	background: rgba(0, 0, 0, 0.08);
	border-radius: 3px;
	padding: 1px 4px;
}

.agent-chat__msg pre code {
	background: none;
	padding: 0;
}

html[data-color-scheme="dark"] .agent-chat__msg code {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__msg ul,
.agent-chat__msg ol {
	margin: 4px 0;
	padding-left: 1.4em;
}

.agent-chat__msg li {
	margin: 2px 0;
}

.agent-chat__heading {
	display: block;
	font-weight: 600;
	margin: 6px 0 2px;
}

.agent-chat__msg a {
	color: var(--ac-accent);
	text-decoration: underline;
}

.agent-chat__msg table {
	border-collapse: collapse;
	display: block;
	font-size: 0.9em;
	margin: 6px 0;
	max-width: 100%;
	overflow-x: auto;
}

.agent-chat__msg th,
.agent-chat__msg td {
	border: 1px solid var(--ac-border);
	padding: 4px 8px;
	text-align: left;
}

.agent-chat__msg th {
	background: rgba(0, 0, 0, 0.06);
	font-weight: 600;
}

html[data-color-scheme="dark"] .agent-chat__msg th {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__notice {
	align-self: center;
	color: var(--ac-muted);
	font-size: 0.85em;
	font-style: italic;
	max-width: 90%;
	text-align: center;
}

/* Typing indicator */
.agent-chat__dots {
	display: inline-flex;
	gap: 4px;
}

.agent-chat__dots span {
	animation: agent-chat-bounce 1.2s infinite;
	background: var(--ac-muted);
	border-radius: 50%;
	display: inline-block;
	height: 6px;
	width: 6px;
}

.agent-chat__dots span:nth-child(2) { animation-delay: 0.15s; }
.agent-chat__dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes agent-chat-bounce {
	0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
	30% { transform: translateY(-4px); opacity: 1; }
}

/* Unread divider */
.agent-chat__divider {
	align-items: center;
	color: var(--ac-accent);
	display: flex;
	font-size: 0.75em;
	gap: 8px;
	text-transform: uppercase;
}

.agent-chat__divider::before,
.agent-chat__divider::after {
	background: var(--ac-accent);
	content: "";
	flex: 1;
	height: 1px;
	opacity: 0.5;
}

/* --- Approval card ---------------------------------------------------------- */
.agent-chat__approval {
	background: var(--ac-bg);
	border: 1px solid var(--ac-accent);
	border-radius: 10px;
	padding: 10px 12px;
}

.agent-chat__approval-title {
	font-weight: 600;
	margin-bottom: 6px;
}

.agent-chat__approval-request + .agent-chat__approval-request {
	border-top: 1px solid var(--ac-border);
	margin-top: 8px;
	padding-top: 8px;
}

.agent-chat__approval-args {
	background: rgba(0, 0, 0, 0.06);
	border-radius: 4px;
	font-size: 0.85em;
	margin: 6px 0;
	max-height: 160px;
	overflow: auto;
	padding: 6px 8px;
}

html[data-color-scheme="dark"] .agent-chat__approval-args {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__approve,
.agent-chat__reject {
	border: 0;
	border-radius: 4px;
	cursor: pointer;
	margin-right: 6px;
	padding: 5px 14px;
}

.agent-chat__approve {
	background: var(--ac-accent);
	color: #fff;
}

.agent-chat__reject {
	background: transparent;
	border: 1px solid var(--ac-border);
	color: var(--ac-text);
}

.agent-chat__approval-request--decided .agent-chat__approval-buttons {
	display: none;
}

.agent-chat__approval-request--decided::after {
	color: var(--ac-accent);
	content: "✓ approved";
	font-size: 0.85em;
}

.agent-chat__approval-request--rejected::after {
	color: #c33;
	content: "✕ rejected";
}

/* Rule-covered request in a mixed batch: pre-approved and locked */
.agent-chat__approval-request--auto::after {
	color: var(--ac-accent);
	content: "✓ auto-approved (always allow)";
	font-size: 0.85em;
}

.agent-chat__approval-request--decided .agent-chat__approval-always {
	display: none;
}

.agent-chat__approval-always {
	cursor: pointer;
	display: block;
	font-size: 0.85em;
	margin-top: 6px;
	opacity: 0.85;
}

.agent-chat__approval-always input {
	margin-right: 4px;
	vertical-align: -1px;
}

/* --- "Always allow" rule chips above the composer ----------------------- */
.agent-chat__rules {
	border-top: 1px solid var(--ac-border);
	font-size: 0.8em;
	grid-area: rules;
	padding: 6px 12px;
}

.agent-chat__rules-label {
	opacity: 0.7;
}

.agent-chat__rule-chip {
	background: rgba(0, 0, 0, 0.06);
	border-radius: 10px;
	display: inline-block;
	margin: 2px 4px 2px 0;
	padding: 1px 8px;
}

html[data-color-scheme="dark"] .agent-chat__rule-chip {
	background: rgba(255, 255, 255, 0.08);
}

.agent-chat__rule-chip button {
	background: transparent;
	border: 0;
	color: inherit;
	cursor: pointer;
	font-size: 1em;
	margin-left: 4px;
	opacity: 0.7;
	padding: 0;
}

.agent-chat__rule-chip button:hover {
	opacity: 1;
}

/* --- Composer ---------------------------------------------------------- */
.agent-chat__composer {
	align-items: flex-end;
	border-top: 1px solid var(--ac-border);
	display: grid;
	gap: 8px;
	grid-area: composer;
	grid-template-columns: 1fr auto;
	padding: 10px 12px;
}

.agent-chat__input {
	background: var(--ac-bg);
	border: 1px solid var(--ac-border);
	border-radius: 6px;
	color: var(--ac-text);
	font: inherit;
	max-height: 140px;
	overflow-y: auto;
	padding: 7px 10px;
	resize: none;
}

.agent-chat__input:focus {
	border-color: var(--ac-accent);
	outline: none;
}

.agent-chat__send {
	background: var(--ac-accent);
	border: 0;
	border-radius: 6px;
	color: #fff;
	cursor: pointer;
	line-height: 0;
	padding: 9px 12px;
}

.agent-chat__send:disabled {
	cursor: default;
	opacity: 0.5;
}

/* --- Tool call detail accordion is styled inline above --------------- */
