/* GoldenRetriever AI Chat widget styles.
   Scoped under .grchat-widget. CSS custom properties prefer the theme's
   own design tokens (--wp--preset--color--primary) when present, then
   fall back to the configured accent colour set on the root via JS,
   then to indigo. */

.grchat-widget {
	--grchat-accent: var( --wp--preset--color--primary, #4f46e5 );
	--grchat-accent-hover: #4338ca;
	--grchat-on-accent: #ffffff;
	--grchat-bg: var( --wp--preset--color--background, #ffffff );
	--grchat-fg: var( --wp--preset--color--foreground, #1f2937 );
	/* Fixed light surface + dark text PAIR for assistant bubbles and the
	   input. Both sides are hardcoded together so the contrast holds on dark
	   block themes — pairing a fixed light background with the theme's
	   (near-white) --grchat-fg produced unreadable white-on-white. */
	--grchat-surface: #f1f3f5;
	--grchat-surface-fg: #1f2937;
	--grchat-input-bg: #f8fafc;
	--grchat-muted: #6b7280;
	--grchat-border: rgba( 0, 0, 0, 0.08 );
	--grchat-shadow: 0 12px 32px rgba( 0, 0, 0, 0.18 );
	--grchat-radius: 14px;
	--grchat-font: var(
		--wp--preset--font-family--body,
		system-ui,
		-apple-system,
		'Segoe UI',
		Roboto,
		sans-serif
	);
	font-family: var( --grchat-font );
	color: var( --grchat-fg );
	line-height: 1.45;
	box-sizing: border-box;
}

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

/* Floating mode */
.grchat-widget--floating {
	position: fixed;
	/* Above the WP admin bar (which is also 99999) so the bubble and the
	   mobile full-screen panel are never partially occluded by it. */
	z-index: 100000;
	bottom: 24px;
}

.grchat-widget--bottom-right {
	right: 24px;
}

.grchat-widget--bottom-left {
	left: 24px;
}

/* Bubble */
.grchat-widget__bubble {
	display: inline-flex;
	align-items: center;
	gap: 8px;
	/* 2px vertical: the 44px circular mark nests flush in the pill's
	   rounded end (avatar-chip style) so the pill stays ~48px tall. */
	padding: 2px 18px 2px 2px;
	min-height: 48px;
	font: inherit;
	font-weight: 600;
	color: var( --grchat-on-accent );
	background: var( --grchat-accent );
	border: 0;
	border-radius: 999px;
	cursor: pointer;
	box-shadow: var( --grchat-shadow );
	transition: transform 0.12s ease, background 0.12s ease;
}

.grchat-widget__bubble:hover,
.grchat-widget__bubble:focus {
	background: var( --grchat-accent-hover );
	outline: none;
}

.grchat-widget__bubble:focus-visible {
	outline: 3px solid var( --grchat-accent );
	outline-offset: 3px;
}

.grchat-widget__bubble:active {
	transform: scale( 0.97 );
}

.grchat-widget__bubble-label {
	display: inline-block;
}

/* Raster bubble icon (<img>) — matches the inline-width of the SVG fallback. */
.grchat-widget__bubble img {
	display: block;
	flex-shrink: 0;
	width: 44px;
	height: 44px;
	object-fit: contain;
}

@media ( max-width: 480px ) {
	.grchat-widget__bubble-label {
		display: none;
	}

	/* Icon-only on small screens: even padding so the pill collapses to
	   a clean 48px circle instead of a lopsided one. */
	.grchat-widget__bubble {
		padding: 2px;
	}
}

/* Panel */
.grchat-widget__panel {
	display: flex;
	flex-direction: column;
	width: 360px;
	max-width: calc( 100vw - 24px );
	height: min( 600px, 80vh );
	background: var( --grchat-bg );
	color: var( --grchat-fg );
	border-radius: var( --grchat-radius );
	box-shadow: var( --grchat-shadow );
	overflow: hidden;
	border: 1px solid var( --grchat-border );
}

.grchat-widget--floating .grchat-widget__panel {
	position: absolute;
	bottom: calc( 100% + 12px );
	opacity: 0;
	transform: translateY( 14px );
	pointer-events: none;
	transition: opacity 0.18s ease, transform 0.18s ease;
}

.grchat-widget--floating.grchat-widget--bottom-right .grchat-widget__panel {
	right: 0;
}

.grchat-widget--floating.grchat-widget--bottom-left .grchat-widget__panel {
	left: 0;
}

.grchat-widget--floating.grchat-widget--open .grchat-widget__panel {
	opacity: 1;
	transform: translateY( 0 );
	pointer-events: auto;
}

.grchat-widget--floating:not( .grchat-widget--open ) .grchat-widget__panel {
	visibility: hidden;
}

.grchat-widget--inline {
	display: block;
	width: 100%;
}

.grchat-widget--inline .grchat-widget__panel {
	position: relative;
	width: 100%;
	max-width: none;
	height: 100%;
	/* Soft floor so a configured height below 480px (e.g. height="300px")
	   is respected instead of overflowing its container. */
	min-height: 200px;
}

/* Mobile full-screen takeover (floating only). Also triggers on short
   viewports (#54): a landscape phone (e.g. 740x360) is wider than 640px,
   so the desktop panel — height min(600px,80vh) anchored above a bubble
   24px off the bottom — rendered its header/close button above the
   viewport, unreachable. Below ~480px tall, take over the full screen. */
@media ( max-width: 640px ), ( max-height: 480px ) {
	.grchat-widget--floating.grchat-widget--open {
		bottom: 0;
		right: 0;
		left: 0;
	}

	.grchat-widget--floating.grchat-widget--open .grchat-widget__panel {
		position: fixed;
		/* inset:0 pins all four edges, so the panel fills the viewport
		   without an explicit width (which avoided a scrollbar-width
		   overflow from 100vw). height uses dvh so the input row is not
		   hidden behind iOS Safari's toolbar; 100vh is the older fallback. */
		inset: 0;
		height: 100vh;
		height: 100dvh;
		max-height: 100vh;
		max-height: 100dvh;
		border-radius: 0;
	}

	.grchat-widget--floating.grchat-widget--open .grchat-widget__bubble {
		display: none;
	}
}

/* Header */
.grchat-widget__header {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 12px 14px;
	border-bottom: 1px solid var( --grchat-border );
	background: var( --grchat-bg );
}

.grchat-widget__title {
	flex: 1 1 auto;
	margin: 0;
	font-size: 15px;
	font-weight: 600;
}

.grchat-widget__header-actions {
	display: flex;
	align-items: center;
	gap: 4px;
}

.grchat-widget__close {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 30px;
	height: 30px;
	padding: 0;
	font: inherit;
	font-size: 18px;
	line-height: 1;
	color: var( --grchat-muted );
	background: transparent;
	border: 0;
	border-radius: 6px;
	cursor: pointer;
}

.grchat-widget__close:hover {
	color: var( --grchat-fg );
	background: rgba( 0, 0, 0, 0.06 );
}

.grchat-widget__close:focus-visible {
	outline: 2px solid var( --grchat-accent );
	outline-offset: 1px;
}

/* Toolbar — text-link row sitting directly above the input rule. */
.grchat-widget__toolbar {
	display: flex;
	flex-direction: row;
	align-items: center;
	padding: 4px 12px;
	margin-bottom: 0;
}

.grchat-widget__email-link,
.grchat-widget__clear-link {
	background: none;
	border: 0;
	padding: 2px 4px;
	font: inherit;
	font-size: 0.8em;
	color: var( --grchat-muted );
	cursor: pointer;
	text-decoration: none;
}

/* Email link reads as a link (underlined); Clear pins to the right
   edge whether or not the email link is present. */
.grchat-widget__email-link {
	text-decoration: underline;
}

.grchat-widget__clear-link {
	margin-left: auto;
}

.grchat-widget__email-link:hover,
.grchat-widget__email-link:focus,
.grchat-widget__clear-link:hover,
.grchat-widget__clear-link:focus {
	text-decoration: underline;
	color: var( --grchat-fg );
}

.grchat-widget__email-link:focus-visible,
.grchat-widget__clear-link:focus-visible {
	outline: 2px solid var( --grchat-accent );
	outline-offset: 1px;
}

/* Log */
.grchat-widget__log {
	flex: 1 1 auto;
	overflow-y: auto;
	padding: 14px;
	background: var( --grchat-bg );
}

.grchat-widget__welcome {
	padding: 0 14px 8px;
	color: var( --grchat-muted );
	font-size: 13px;
}

.grchat-widget--empty .grchat-widget__welcome {
	display: block;
}

.grchat-widget:not( .grchat-widget--empty ) .grchat-widget__welcome {
	display: none;
}

/* Messages */
.grchat-widget__message {
	margin-bottom: 10px;
	display: flex;
}

.grchat-widget__message--user {
	justify-content: flex-end;
}

.grchat-widget__message--assistant,
.grchat-widget__message--error,
.grchat-widget__message--streaming {
	justify-content: flex-start;
}

.grchat-widget__bubble-text {
	max-width: 86%;
	padding: 8px 12px;
	border-radius: 14px;
	font-size: 14px;
	word-wrap: break-word;
	white-space: normal;
}

.grchat-widget__message--user .grchat-widget__bubble-text {
	background: var( --grchat-accent );
	color: var( --grchat-on-accent );
	border-bottom-right-radius: 4px;
}

.grchat-widget__message--assistant .grchat-widget__bubble-text,
.grchat-widget__message--streaming .grchat-widget__bubble-text {
	background: var( --grchat-surface );
	color: var( --grchat-surface-fg );
	border-bottom-left-radius: 4px;
}

.grchat-widget__message--error .grchat-widget__bubble-text {
	background: #fef2f2;
	color: #991b1b;
	border: 1px solid #fecaca;
	border-bottom-left-radius: 4px;
}

.grchat-widget__bubble-text p {
	margin: 0 0 8px;
}

.grchat-widget__bubble-text p:last-child {
	margin-bottom: 0;
}

.grchat-widget__bubble-text a {
	color: var( --grchat-accent );
	text-decoration: underline;
}

/* Explicit visited/hover states: a theme's own `a:visited` rule ties
   our `.class a` selector on specificity and can win on order, turning
   visited links invisible against the bubble (white-on-white in the
   field). The pseudo-class selectors below outrank a bare a:visited. */
.grchat-widget__bubble-text a:visited {
	color: var( --grchat-accent );
}

.grchat-widget__bubble-text a:hover,
.grchat-widget__bubble-text a:focus {
	color: var( --grchat-accent-hover );
}

/* Keyboard users get the same visible focus ring as the widget's other
   interactive elements, even when a theme suppresses default outlines. */
.grchat-widget__bubble-text a:focus-visible {
	outline: 2px solid var( --grchat-accent );
	outline-offset: 1px;
	border-radius: 2px;
}

.grchat-widget__bubble-text code {
	font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
	font-size: 12.5px;
	padding: 1px 4px;
	background: rgba( 0, 0, 0, 0.06 );
	border-radius: 4px;
}

.grchat-widget__bubble-text pre {
	margin: 6px 0;
	padding: 8px 10px;
	background: rgba( 0, 0, 0, 0.06 );
	border-radius: 6px;
	overflow-x: auto;
}

.grchat-widget__bubble-text pre code {
	background: transparent;
	padding: 0;
}

.grchat-widget__bubble-text ul {
	margin: 4px 0 4px 18px;
	padding: 0;
}

.grchat-widget__bubble-text li {
	margin: 2px 0;
}

/* Reply headings (## → h4 etc.) — chat-bubble scale, not page scale. */
.grchat-widget__bubble-text h3,
.grchat-widget__bubble-text h4,
.grchat-widget__bubble-text h5,
.grchat-widget__bubble-text h6 {
	margin: 10px 0 4px;
	line-height: 1.3;
	font-weight: 600;
}

.grchat-widget__bubble-text h3:first-child,
.grchat-widget__bubble-text h4:first-child,
.grchat-widget__bubble-text h5:first-child,
.grchat-widget__bubble-text h6:first-child {
	margin-top: 0;
}

.grchat-widget__bubble-text h3 {
	font-size: 1.05em;
}

.grchat-widget__bubble-text h4 {
	font-size: 1em;
}

.grchat-widget__bubble-text h5,
.grchat-widget__bubble-text h6 {
	font-size: 0.95em;
}

/* Streaming pulse */
.grchat-widget__pulse {
	display: inline-flex;
	gap: 4px;
	align-items: center;
	padding: 2px 0;
}

.grchat-widget__pulse span {
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: var( --grchat-muted );
	opacity: 0.4;
	animation: grchat-pulse 1.2s infinite;
}

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

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

@keyframes grchat-pulse {
	0%,
	80%,
	100% {
		opacity: 0.4;
		transform: scale( 1 );
	}
	40% {
		opacity: 1;
		transform: scale( 1.2 );
	}
}

@media ( prefers-reduced-motion: reduce ) {
	.grchat-widget__pulse span {
		animation: none;
	}
	.grchat-widget--floating .grchat-widget__panel {
		transition: none;
	}
}

.grchat-widget__retry {
	margin-left: 8px;
	padding: 4px 10px;
	font: inherit;
	font-size: 12px;
	color: var( --grchat-accent );
	background: transparent;
	border: 1px solid var( --grchat-accent );
	border-radius: 6px;
	cursor: pointer;
}

.grchat-widget__retry:hover,
.grchat-widget__retry:focus-visible {
	color: var( --grchat-on-accent );
	background: var( --grchat-accent );
	outline: none;
}

/* Inline error toast */
.grchat-widget__inline-error {
	margin: 0 14px;
	padding: 6px 10px;
	background: #fef2f2;
	color: #991b1b;
	border: 1px solid #fecaca;
	border-radius: 6px;
	font-size: 12px;
}

/* Form */
.grchat-widget__form {
	display: grid;
	grid-template-columns: 1fr auto;
	gap: 8px;
	align-items: end;
	padding: 12px 14px;
	border-top: 1px solid var( --grchat-border );
	background: var( --grchat-bg );
	position: relative;
}

.grchat-widget__input {
	width: 100%;
	min-height: 40px;
	max-height: 120px;
	padding: 10px 12px;
	font: inherit;
	font-size: 14px;
	color: var( --grchat-surface-fg );
	background: var( --grchat-input-bg );
	border: 1px solid var( --grchat-border );
	border-radius: 10px;
	resize: vertical;
}

.grchat-widget__input:focus {
	outline: 2px solid var( --grchat-accent );
	outline-offset: 1px;
	border-color: var( --grchat-accent );
}

.grchat-widget__send {
	padding: 0 14px;
	min-height: 40px;
	font: inherit;
	font-weight: 600;
	color: var( --grchat-on-accent );
	background: var( --grchat-accent );
	border: 0;
	border-radius: 10px;
	cursor: pointer;
}

.grchat-widget__send:hover,
.grchat-widget__send:focus-visible {
	background: var( --grchat-accent-hover );
	outline: none;
}

.grchat-widget__send:disabled {
	opacity: 0.6;
	cursor: not-allowed;
}

.grchat-widget__counter {
	position: absolute;
	right: 14px;
	bottom: -18px;
	font-size: 11px;
	color: var( --grchat-muted );
	visibility: hidden;
}

/* Screen-reader only utility (does not collide with theme equivalents). */
.grchat-widget .screen-reader-text {
	position: absolute !important;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect( 0, 0, 0, 0 );
	white-space: nowrap;
	border: 0;
}

/* ── Email-me-this-chat panel ─────────────────────────────────────── */

.grchat-widget__email-panel {
	display: flex;
	flex-direction: column;
	gap: 8px;
	padding: 12px;
	border-top: 1px solid var( --grchat-border );
}

/* The author display:flex above would silently defeat the HTML hidden
   attribute (UA display:none loses to any author display) — restate it
   so the panel genuinely only appears when the email link opens it. */
.grchat-widget__email-panel[hidden] {
	display: none;
}

.grchat-widget__email-input {
	width: 100%;
	box-sizing: border-box;
	padding: 6px 8px;
	border: 1px solid var( --grchat-border );
	border-radius: 6px;
	font-size: inherit;
	background: var( --grchat-input-bg );
	color: var( --grchat-fg );
}

.grchat-widget__email-consent {
	font-size: 0.85em;
}

.grchat-widget__email-actions {
	display: flex;
	gap: 8px;
}

.grchat-widget__email-send {
	padding: 6px 14px;
	background: var( --grchat-accent );
	color: var( --grchat-on-accent );
	border: none;
	border-radius: 6px;
	cursor: pointer;
	font-size: inherit;
}

.grchat-widget__email-send:hover {
	background: var( --grchat-accent-hover );
}

.grchat-widget__email-cancel {
	padding: 6px 14px;
	background: transparent;
	border: 1px solid var( --grchat-border );
	border-radius: 6px;
	cursor: pointer;
	font-size: inherit;
	color: var( --grchat-fg );
}

.grchat-widget__email-status {
	margin: 0;
	font-size: 0.85em;
	color: var( --grchat-muted );
}

/* ── Attribution link (opt-in "Get your own") ─────────────────────── */

.grchat-widget__attribution {
	padding: 4px 12px 8px;
	text-align: center;
	font-size: 0.75em;
	opacity: 0.7;
}

.grchat-widget__attribution a {
	color: inherit;
	text-decoration: none;
}

.grchat-widget__attribution a:hover {
	text-decoration: underline;
}
