/**
 * Chatrpillar frontend chat.
 *
 * Two layouts share one set of components: .chatrpillar--floating is the fixed
 * bubble, .chatrpillar--inline is the shortcode's embedded panel. Colors come
 * from custom properties, so a theme can restyle everything by overriding them
 * on .chatrpillar.
 */

.chatrpillar {
	/* Set from the plugin settings, inline on this element. */
	--chatrpillar-header-bg: #2f6f4e;
	--chatrpillar-user-bg: #2f6f4e;

	/* Focus rings follow the header color. */
	--chatrpillar-accent: var( --chatrpillar-header-bg );
	--chatrpillar-accent-text: #ffffff;

	/* Source links keep a conventional link blue rather than the header color:
	   a light brand color is unreadable on the white message background, and
	   visitors expect links to look like links. */
	--chatrpillar-link: #1155cc;
	--chatrpillar-surface: #ffffff;
	--chatrpillar-surface-alt: #f4f5f7;
	--chatrpillar-text: #1d2327;
	--chatrpillar-muted: #61676d;
	--chatrpillar-border: #e2e4e7;
	--chatrpillar-error-bg: #fdf2f2;
	--chatrpillar-error-border: #f0c3c3;
	--chatrpillar-error-text: #8a1f1f;
	--chatrpillar-shadow: 0 12px 32px rgba( 15, 23, 32, 0.18 );
	--chatrpillar-radius: 16px;
	--chatrpillar-panel-height: 540px;

	/* Distance from the screen edges; overridden inline from the settings. */
	--chatrpillar-offset-x: 20px;
	--chatrpillar-offset-y: 20px;

	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
	font-size: 15px;
	line-height: 1.5;
	color: var( --chatrpillar-text );
}

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

.chatrpillar__sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect( 0, 0, 0, 0 );
	white-space: nowrap;
	border: 0;
}

/* Layout: floating ----------------------------------------------------- */

.chatrpillar--floating {
	position: fixed;
	z-index: 99999;
	display: flex;
}

/* Corner placement. The bubble is last in flow for the bottom corners and
   first for the top ones, so the panel always opens away from the edge and the
   bubble itself never moves when the chat opens. */

.chatrpillar--pos-bottom-right,
.chatrpillar--pos-bottom-center,
.chatrpillar--pos-bottom-left {
	bottom: var( --chatrpillar-offset-y );
	flex-direction: column-reverse;
}

/* Centring without a transform on purpose: a transformed ancestor makes a
   descendant's position:fixed resolve against it, which would break the
   full-width panel on small screens. Spanning the viewport and centring the
   children achieves the same thing, and pointer-events keeps the rest of the
   page clickable either side of the bubble. */
.chatrpillar--pos-bottom-center {
	right: 0;
	left: 0;
	align-items: center;
	pointer-events: none;
}

.chatrpillar--pos-bottom-center > * {
	pointer-events: auto;
}

.chatrpillar--pos-top-right,
.chatrpillar--pos-top-left {
	top: var( --chatrpillar-offset-y );
	flex-direction: column;
}

.chatrpillar--pos-bottom-right,
.chatrpillar--pos-top-right {
	right: var( --chatrpillar-offset-x );
	align-items: flex-end;
}

.chatrpillar--pos-bottom-left,
.chatrpillar--pos-top-left {
	left: var( --chatrpillar-offset-x );
	align-items: flex-start;
}

.chatrpillar--floating .chatrpillar__panel {
	width: 380px;
	max-width: calc( 100vw - 40px );
	height: var( --chatrpillar-panel-height );
	max-height: calc( 100vh - 120px );
	box-shadow: var( --chatrpillar-shadow );
	animation: chatrpillar-rise 0.18s ease-out;
}

.chatrpillar--pos-bottom-right .chatrpillar__panel,
.chatrpillar--pos-bottom-left .chatrpillar__panel {
	margin-bottom: 12px;
}

.chatrpillar--pos-top-right .chatrpillar__panel,
.chatrpillar--pos-top-left .chatrpillar__panel {
	margin-top: 12px;
	animation-name: chatrpillar-drop;
}

/* Layout: inline (shortcode) ------------------------------------------- */

.chatrpillar--inline {
	display: block;
	width: 100%;
	margin: 1.5em 0;
}

.chatrpillar--inline .chatrpillar__panel {
	width: 100%;
	height: var( --chatrpillar-panel-height );
	max-height: 80vh;
}

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

.chatrpillar__launcher {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 56px;
	height: 56px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: var( --chatrpillar-header-bg );
	color: var( --chatrpillar-accent-text );
	box-shadow: var( --chatrpillar-shadow );
	cursor: pointer;
	transition: transform 0.15s ease, filter 0.15s ease;
}

.chatrpillar__launcher:hover {
	filter: brightness( 0.92 );
	transform: translateY( -2px );
}

.chatrpillar__launcher:focus-visible {
	outline: 2px solid var( --chatrpillar-accent );
	outline-offset: 3px;
}

.chatrpillar__launcher-close {
	display: none;
}

.chatrpillar.is-open .chatrpillar__launcher-icon {
	display: none;
}

.chatrpillar.is-open .chatrpillar__launcher-close {
	display: flex;
}

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

.chatrpillar__panel {
	display: flex;
	flex-direction: column;
	overflow: hidden;
	border: 1px solid var( --chatrpillar-border );
	border-radius: var( --chatrpillar-radius );
	background: var( --chatrpillar-surface );
}

.chatrpillar__panel[hidden] {
	display: none;
}

@keyframes chatrpillar-rise {
	from {
		opacity: 0;
		transform: translateY( 8px );
	}

	to {
		opacity: 1;
		transform: translateY( 0 );
	}
}

@keyframes chatrpillar-drop {
	from {
		opacity: 0;
		transform: translateY( -8px );
	}

	to {
		opacity: 1;
		transform: translateY( 0 );
	}
}

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

.chatrpillar__header {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 12px 12px 12px 14px;
	background: var( --chatrpillar-header-bg );
	color: var( --chatrpillar-accent-text );
}

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

.chatrpillar__title {
	flex: 1 1 auto;
	overflow: hidden;
	font-size: 15px;
	font-weight: 600;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.chatrpillar__icon-button {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 30px;
	height: 30px;
	padding: 0;
	border: 0;
	border-radius: 8px;
	background: transparent;
	color: inherit;
	cursor: pointer;
	opacity: 0.85;
}

.chatrpillar__icon-button:hover {
	background: rgba( 255, 255, 255, 0.16 );
	opacity: 1;
}

.chatrpillar__icon-button:focus-visible {
	outline: 2px solid var( --chatrpillar-accent-text );
	outline-offset: -2px;
}

/* Messages ------------------------------------------------------------- */

.chatrpillar__messages {
	flex: 1 1 auto;
	padding: 16px;
	overflow-y: auto;
	background: var( --chatrpillar-surface-alt );
	overscroll-behavior: contain;
}

.chatrpillar__message {
	display: flex;
	margin-bottom: 12px;
}

.chatrpillar__message--user {
	justify-content: flex-end;
}

.chatrpillar__bubble {
	max-width: 85%;
	padding: 10px 13px;
	border-radius: 14px;
	white-space: pre-wrap;
	overflow-wrap: anywhere;
}

.chatrpillar__message--assistant .chatrpillar__bubble {
	border: 1px solid var( --chatrpillar-border );
	border-bottom-left-radius: 4px;
	background: var( --chatrpillar-surface );
}

.chatrpillar__message--user .chatrpillar__bubble {
	border-bottom-right-radius: 4px;
	background: var( --chatrpillar-user-bg );
	color: var( --chatrpillar-accent-text );
}

.chatrpillar__message--error .chatrpillar__bubble {
	border: 1px solid var( --chatrpillar-error-border );
	background: var( --chatrpillar-error-bg );
	color: var( --chatrpillar-error-text );
}

.chatrpillar__sources {
	margin: 6px 0 0;
	padding: 0;
	font-size: 13px;
	list-style: none;
}

.chatrpillar__sources-label {
	margin: 0 0 2px;
	color: var( --chatrpillar-muted );
	font-size: 12px;
	text-transform: uppercase;
	letter-spacing: 0.04em;
}

.chatrpillar__sources a {
	color: var( --chatrpillar-link );
	text-decoration: underline;
}

.chatrpillar__sources a:hover,
.chatrpillar__sources a:focus-visible {
	color: var( --chatrpillar-link-hover, #0b3d91 );
}

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

.chatrpillar__typing {
	display: flex;
	gap: 4px;
	align-items: center;
	height: 20px;
}

.chatrpillar__typing span {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var( --chatrpillar-muted );
	animation: chatrpillar-blink 1.2s infinite ease-in-out;
}

.chatrpillar__typing span:nth-child( 2 ) {
	animation-delay: 0.18s;
}

.chatrpillar__typing span:nth-child( 3 ) {
	animation-delay: 0.36s;
}

@keyframes chatrpillar-blink {
	0%,
	80%,
	100% {
		opacity: 0.25;
	}

	40% {
		opacity: 1;
	}
}

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

.chatrpillar__form {
	display: flex;
	gap: 8px;
	align-items: flex-end;
	padding: 10px;
	margin: 0;
	border-top: 1px solid var( --chatrpillar-border );
	background: var( --chatrpillar-surface );
}

.chatrpillar__input {
	flex: 1 1 auto;
	max-height: 120px;
	min-height: 40px;
	padding: 9px 12px;
	border: 1px solid var( --chatrpillar-border );
	border-radius: 12px;
	background: var( --chatrpillar-surface );
	color: var( --chatrpillar-text );
	font: inherit;
	resize: none;
}

.chatrpillar__input:focus {
	border-color: var( --chatrpillar-accent );
	outline: none;
}

.chatrpillar__send {
	display: flex;
	flex: 0 0 auto;
	align-items: center;
	justify-content: center;
	width: 40px;
	height: 40px;
	padding: 0;
	border: 0;
	border-radius: 12px;
	background: var( --chatrpillar-header-bg );
	color: var( --chatrpillar-accent-text );
	cursor: pointer;
	transition: filter 0.15s ease;
}

.chatrpillar__send:hover:not( :disabled ) {
	filter: brightness( 0.92 );
}

.chatrpillar__send:disabled {
	cursor: not-allowed;
	opacity: 0.5;
}

/* Editor notice -------------------------------------------------------- */

.chatrpillar-notice {
	padding: 12px 14px;
	border: 1px dashed #c3c4c7;
	border-radius: 8px;
	background: #f6f7f7;
	color: #50575e;
}

/* Small screens -------------------------------------------------------- */

@media ( max-width: 480px ) {
	/* Spacing comes from the settings at every width, so a value chosen to
	   clear a sticky bar still clears it on a phone. */

	.chatrpillar--floating .chatrpillar__panel {
		position: fixed;
		right: 0;
		left: 0;
		width: 100%;
		max-width: 100%;
		height: 82vh;
		max-height: 82vh;
		margin: 0;
		border: 0;
	}

	.chatrpillar--pos-bottom-right .chatrpillar__panel,
	.chatrpillar--pos-bottom-center .chatrpillar__panel,
	.chatrpillar--pos-bottom-left .chatrpillar__panel {
		bottom: 0;
		border-radius: var( --chatrpillar-radius ) var( --chatrpillar-radius ) 0 0;
	}

	.chatrpillar--pos-top-right .chatrpillar__panel,
	.chatrpillar--pos-top-left .chatrpillar__panel {
		top: 0;
		border-radius: 0 0 var( --chatrpillar-radius ) var( --chatrpillar-radius );
	}

	.chatrpillar--inline .chatrpillar__panel {
		max-height: 70vh;
	}
}

/* Dark palette --------------------------------------------------------- *
 * Applied when the visitor prefers dark and the instance has not been
 * pinned to light, or when theme="dark" pins it explicitly.
 */

@media ( prefers-color-scheme: dark ) {
	.chatrpillar:not( .chatrpillar--light ) {
		--chatrpillar-link: #8ab4f8;
		--chatrpillar-link-hover: #aecbfa;
		--chatrpillar-surface: #1f2327;
		--chatrpillar-surface-alt: #16191c;
		--chatrpillar-text: #e8eaed;
		--chatrpillar-muted: #9aa0a6;
		--chatrpillar-border: #333940;
		--chatrpillar-error-bg: #33201f;
		--chatrpillar-error-border: #6b2b2b;
		--chatrpillar-error-text: #f3b8b8;
		--chatrpillar-shadow: 0 12px 32px rgba( 0, 0, 0, 0.5 );
	}
}

.chatrpillar--dark {
	--chatrpillar-link: #8ab4f8;
	--chatrpillar-link-hover: #aecbfa;
	--chatrpillar-surface: #1f2327;
	--chatrpillar-surface-alt: #16191c;
	--chatrpillar-text: #e8eaed;
	--chatrpillar-muted: #9aa0a6;
	--chatrpillar-border: #333940;
	--chatrpillar-error-bg: #33201f;
	--chatrpillar-error-border: #6b2b2b;
	--chatrpillar-error-text: #f3b8b8;
	--chatrpillar-shadow: 0 12px 32px rgba( 0, 0, 0, 0.5 );
}

@media ( prefers-reduced-motion: reduce ) {
	.chatrpillar__panel,
	.chatrpillar__launcher,
	.chatrpillar__typing span {
		animation: none;
		transition: none;
	}
}
