/* ===========================================================================
 * PrayerPrompt — Haven AI Prayer Companion
 * assets/css/companion.css
 *
 * Floating trigger button + chat overlay for logged-in members.
 * Loads only when is_user_logged_in() — see inc/enqueue.php.
 *
 * All colours are pulled from base.css tokens so the theme palette
 * stays in sync. See :root declarations in assets/css/base.css.
 *
 * Z-index stack:
 *   9998 — overlay
 *   9999 — trigger button
 * =========================================================================== */

/* ── Trigger button ───────────────────────────────────────────────────────── */
.pp-companion-trigger {
	position: fixed;
	bottom: 24px;
	right: 24px;
	z-index: 9999;

	width: 56px;
	height: 56px;
	padding: 0;
	border: none;
	border-radius: 50%;

	background: var(--color-red);
	color: var(--bg-white);

	display: flex;
	align-items: center;
	justify-content: center;

	box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
	cursor: pointer;
	transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.pp-companion-trigger:hover,
.pp-companion-trigger:focus-visible {
	background: var(--color-red-hover);
	transform: scale(1.05);
	box-shadow: 0 6px 20px rgba(0, 0, 0, 0.22);
}

.pp-companion-trigger:focus-visible {
	outline: 3px solid var(--bg-white);
	outline-offset: 2px;
}

.pp-companion-trigger svg {
	display: block;
}

.pp-companion-hidden {
	display: none !important;
}

/* ── Overlay container ────────────────────────────────────────────────────── */
.pp-companion-overlay {
	position: fixed;
	bottom: 88px;
	right: 24px;
	z-index: 9998;

	width: 380px;
	max-width: calc(100vw - 32px);
	height: 560px;
	max-height: calc(100vh - 120px);

	background: var(--bg-white);
	border: 1px solid var(--bg-card);
	border-radius: 16px;
	box-shadow: 0 8px 40px rgba(0, 0, 0, 0.15);

	display: flex;
	flex-direction: column;
	overflow: hidden;

	opacity: 0;
	transform: translateY(16px);
	pointer-events: none;
	transition: opacity 0.2s ease, transform 0.2s ease;

	font-family: var(--font-sans);
	color: var(--text-primary);
}

.pp-companion-overlay.is-open {
	opacity: 1;
	transform: translateY(0);
	pointer-events: auto;
}

/* The HTML starts with [hidden] so the dialog is fully removed from
 * the a11y tree until JS opens it. JS removes the attribute, then adds
 * .is-open on the next frame to trigger the opacity transition. */
.pp-companion-overlay[hidden] {
	display: none;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
.pp-companion-header {
	height: 56px;
	flex-shrink: 0;
	background: var(--color-navy);
	padding: 0 16px;
	border-radius: 16px 16px 0 0;

	display: flex;
	align-items: center;
	gap: 8px;

	color: var(--bg-white);
}

.pp-companion-header h3 {
	margin: 0;
	font-family: var(--font-sans);
	font-size: 15px;
	font-weight: 500;
	color: var(--bg-white);
	letter-spacing: 0.01em;
}

.pp-companion-close {
	margin-left: auto;
	background: none;
	border: none;
	color: rgba(255, 255, 255, 0.7);
	font-size: 24px;
	line-height: 1;
	cursor: pointer;
	/* 10px 14px gives a ~44x52 clickable area, meeting WCAG 2.5.5 AAA. */
	padding: 10px 14px;
	border-radius: 6px;
	transition: color 0.15s ease, background 0.15s ease;
}

.pp-companion-close:hover,
.pp-companion-close:focus-visible {
	color: var(--bg-white);
	background: rgba(255, 255, 255, 0.1);
}

.pp-companion-close:focus-visible {
	outline: 2px solid var(--bg-white);
	outline-offset: 1px;
}

/* ── Messages area ───────────────────────────────────────────────────────── */
.pp-companion-messages {
	flex: 1;
	overflow-y: auto;
	padding: 16px;

	display: flex;
	flex-direction: column;
	gap: 12px;

	background: var(--bg-white);
	scroll-behavior: smooth;
	-webkit-overflow-scrolling: touch;
}

.pp-companion-messages::-webkit-scrollbar {
	width: 6px;
}
.pp-companion-messages::-webkit-scrollbar-track {
	background: transparent;
}
.pp-companion-messages::-webkit-scrollbar-thumb {
	background: var(--bg-card);
	border-radius: 3px;
}
.pp-companion-messages::-webkit-scrollbar-thumb:hover {
	background: var(--bg-card-hover);
}

/* ── Message bubbles ─────────────────────────────────────────────────────── */
.pp-companion-msg {
	max-width: 85%;
	padding: 10px 14px;
	font-size: 14px;
	line-height: 1.55;
	word-wrap: break-word;
	overflow-wrap: break-word;
	white-space: pre-wrap; /* Preserves \n\n from API responses as paragraph breaks */
}

.pp-companion-msg.user {
	align-self: flex-end;
	background: var(--color-navy);
	color: var(--bg-white);
	border-radius: 16px 16px 4px 16px;
}

.pp-companion-msg.assistant {
	align-self: flex-start;
	background: var(--bg-page);
	color: var(--color-navy);
	border-radius: 16px 16px 16px 4px;
}

.pp-companion-msg.error {
	background: var(--bg-red-tint);
	color: var(--color-burgundy);
	border: 1px solid var(--border-red);
}

/* ── Typing indicator ────────────────────────────────────────────────────── */
.pp-companion-typing {
	align-self: flex-start;
	background: var(--bg-page);
	color: var(--color-navy);
	border-radius: 16px 16px 16px 4px;
	padding: 12px 16px;

	display: inline-flex;
	align-items: center;
	gap: 4px;
}

.pp-companion-typing span {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--color-navy);
	opacity: 0.4;
	animation: pp-companion-bounce 1.2s infinite ease-in-out;
}

.pp-companion-typing span:nth-child(2) { animation-delay: 0.15s; }
.pp-companion-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes pp-companion-bounce {
	0%, 60%, 100% {
		transform: translateY(0);
		opacity: 0.4;
	}
	30% {
		transform: translateY(-5px);
		opacity: 1;
	}
}

/* ── Inline CTAs (crisis, upgrade) — base + modifiers ────────────────────── */
.pp-companion-cta {
	align-self: stretch;
	display: block;
	width: 100%;
	margin-top: 4px;
	padding: 12px 16px;

	border: none;
	border-radius: 8px;

	font-family: inherit;
	font-size: 14px;
	font-weight: 500;
	text-align: center;
	text-decoration: none;
	cursor: pointer;
	transition: background 0.15s ease;

	color: var(--bg-white);
}

/* Uses red-hover (#d4101e) as base for 5.4:1 contrast on 14px white
 * text — the brighter --color-red fails AA at body-text size. Hover
 * deepens to burgundy for a visible state change and higher contrast. */
.pp-companion-cta--crisis {
	background: var(--color-red-hover);
}
.pp-companion-cta--crisis:hover,
.pp-companion-cta--crisis:focus-visible {
	background: var(--color-burgundy);
	color: var(--bg-white);
}
.pp-companion-cta--crisis:focus-visible {
	outline: 2px solid var(--color-navy);
	outline-offset: 2px;
}

.pp-companion-cta--upgrade {
	background: var(--color-navy);
}
.pp-companion-cta--upgrade:hover,
.pp-companion-cta--upgrade:focus-visible {
	background: var(--bg-navy-dark);
	color: var(--bg-white);
}
.pp-companion-cta--upgrade:focus-visible {
	outline: 2px solid var(--color-red);
	outline-offset: 2px;
}

/* ── Guest mode CTA wrapper ──────────────────────────────────────────────── */
/* The .pp-companion-overlay--guest variant has no input row, so the
 * messages area takes the full height below the header. The CTA
 * buttons are stacked vertically and pushed to the bottom via auto
 * margin so they sit above the overlay's bottom edge with a clear
 * gap from the welcome text. */
.pp-companion-guest-cta-wrap {
	display: flex;
	flex-direction: column;
	gap: 8px;
	margin-top: auto;
	padding-top: 16px;
}

.pp-companion-overlay--guest .pp-companion-messages {
	/* Give the guest overlay a subtle parchment background so the
	 * welcome blurb feels like a warm landing surface, not a blank
	 * chat window. */
	background: var(--bg-page);
}

.pp-companion-overlay--guest .pp-companion-msg.assistant {
	background: var(--bg-white);
	border: 1px solid var(--bg-card);
}

/* ── Input row ───────────────────────────────────────────────────────────── */
.pp-companion-input-row {
	flex-shrink: 0;
	min-height: 56px;
	border-top: 1px solid var(--bg-card);
	padding: 8px 12px;
	background: var(--bg-white);

	display: flex;
	gap: 8px;
	align-items: flex-end;
}

.pp-companion-input {
	flex: 1;
	border: 1px solid var(--bg-card);
	border-radius: 20px;
	padding: 10px 16px;

	font-family: inherit;
	font-size: 14px;
	line-height: 1.5;
	color: var(--text-primary);
	background: var(--bg-white);

	resize: none;
	max-height: 120px;
	min-height: 40px;
	overflow-y: auto;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.pp-companion-input::placeholder {
	/* --text-muted (#777) fails WCAG AA at 4.48:1 — use --text-secondary
	 * (#444) for 9.9:1 contrast on white. */
	color: var(--text-secondary);
}

.pp-companion-input:focus {
	outline: none;
	border-color: var(--color-navy);
	box-shadow: 0 0 0 3px rgba(12, 35, 53, 0.08);
}

.pp-companion-send {
	/* 44x44 meets WCAG 2.5.5 Target Size (AAA) — matters for older
	 * members and motor impairments. */
	width: 44px;
	height: 44px;
	flex-shrink: 0;
	padding: 0;

	background: var(--color-red);
	color: var(--bg-white);
	border: none;
	border-radius: 50%;

	display: flex;
	align-items: center;
	justify-content: center;

	cursor: pointer;
	transition: background 0.15s ease, transform 0.15s ease;
}

.pp-companion-send:hover,
.pp-companion-send:focus-visible {
	background: var(--color-red-hover);
}

.pp-companion-send:active {
	transform: scale(0.95);
}

.pp-companion-send:focus-visible {
	outline: 2px solid var(--color-navy);
	outline-offset: 2px;
}

.pp-companion-send:disabled {
	background: var(--text-light);
	cursor: not-allowed;
}

.pp-companion-send svg {
	display: block;
	margin-left: -1px; /* Optical centering — send arrow is asymmetric */
}

/* ── Mobile — full-screen overlay ────────────────────────────────────────── */
@media (max-width: 600px) {
	.pp-companion-overlay {
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100%;
		max-width: 100%;
		max-height: 100%;
		border-radius: 0;
		border: none;
	}

	.pp-companion-header {
		border-radius: 0;
	}

	.pp-companion-trigger {
		bottom: 16px;
		right: 16px;
	}
}

/* ── Hide bottom-strip floats while the Haven overlay is open ─────────────
 *
 * When Haven opens (especially in guest mode on mobile, where it becomes
 * full-screen) the existing bottom-strip elements — Ko-fi's "Support us"
 * widget and Haven's own trigger — would otherwise sit on top of the
 * overlay and cover the "Sign in" / "Create a free account" CTAs at
 * the bottom of the guest view. Hiding them while .pp-haven-open is on
 * the body element fixes the overlap without touching layout anywhere
 * else.
 *
 * We target Ko-fi via their standard overlay IDs / iframe src pattern
 * so the hide works regardless of which version of the Ko-fi plugin or
 * embed script is active. */
body.pp-haven-open .pp-companion-trigger,
body.pp-haven-open #kofi-widget-overlay,
body.pp-haven-open .kofi-widget-overlay,
body.pp-haven-open [id^="kofi"],
body.pp-haven-open iframe[src*="ko-fi.com"],
body.pp-haven-open .floating-chat-kofi-popup-iframe,
body.pp-haven-open .kofi-button {
	display: none !important;
}

/* ── Reduced motion ──────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
	.pp-companion-overlay,
	.pp-companion-trigger,
	.pp-companion-send,
	.pp-companion-close,
	.pp-companion-cta,
	.pp-companion-input {
		transition: none;
	}

	.pp-companion-typing span {
		animation: none;
		opacity: 0.6;
	}

	.pp-companion-trigger:hover,
	.pp-companion-trigger:focus-visible {
		transform: none;
	}

	.pp-companion-messages {
		scroll-behavior: auto;
	}
}

/* ══════════════════════════════════════════════════════════════════
 * HAVEN FLOATING PROMO — bottom-center slide-up, site-wide
 *
 * Appears 8 seconds after DOMContentLoaded for any visitor (guest or
 * member) who has not yet dismissed it and has not yet opened Haven
 * this session. Click the card -> opens the Haven overlay. Click the
 * close (x) -> dismisses via localStorage and the widget won't show
 * again until we publish a new promo.
 *
 * Hidden whenever the Haven overlay is open to avoid visual conflict.
 * ════════════════════════════════════════════════════════════════ */

.pp-haven-promo {
	position: fixed;
	left: 50%;
	bottom: 20px;
	transform: translate(-50%, 120%);
	z-index: 9997; /* Below Haven overlay (9998) and trigger (9999) */

	width: min(420px, calc(100vw - 32px));
	padding: 14px 48px 14px 18px;

	display: flex;
	align-items: center;
	gap: 14px;

	background: var(--color-navy);
	color: var(--bg-white);
	border: 1px solid var(--color-navy);
	border-radius: 14px;
	box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);

	font-family: var(--font-sans);
	cursor: pointer;
	opacity: 0;
	pointer-events: none;
	transition: transform 0.35s cubic-bezier(.2, .9, .25, 1.1),
	            opacity 0.3s ease;
}

.pp-haven-promo.is-visible {
	transform: translate(-50%, 0);
	opacity: 1;
	pointer-events: auto;
}

.pp-haven-promo:hover,
.pp-haven-promo:focus-visible {
	outline: none;
	background: var(--bg-navy-dark);
}

.pp-haven-promo__icon {
	flex-shrink: 0;
	width: 38px;
	height: 38px;
	border-radius: 50%;
	background: var(--color-red-hover);
	display: flex;
	align-items: center;
	justify-content: center;
	color: var(--bg-white);
}

.pp-haven-promo__icon svg {
	width: 18px;
	height: 18px;
}

.pp-haven-promo__text {
	flex: 1;
	min-width: 0;
	line-height: 1.35;
}

.pp-haven-promo__headline {
	display: block;
	font-size: 14px;
	font-weight: 600;
	color: var(--bg-white);
}

.pp-haven-promo__sub {
	display: block;
	font-size: 12px;
	color: rgba(255, 255, 255, 0.75);
	margin-top: 2px;
}

.pp-haven-promo__close {
	position: absolute;
	top: 8px;
	right: 8px;
	width: 28px;
	height: 28px;
	padding: 0;
	background: transparent;
	border: none;
	color: rgba(255, 255, 255, 0.6);
	font-size: 20px;
	line-height: 1;
	cursor: pointer;
	border-radius: 6px;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: color 0.15s ease, background 0.15s ease;
}

.pp-haven-promo__close:hover,
.pp-haven-promo__close:focus-visible {
	color: var(--bg-white);
	background: rgba(255, 255, 255, 0.1);
	outline: none;
}

/* Mobile: put the promo under the site header as a top-anchored
 * sticky notification. The bottom of the viewport is left entirely
 * free for Ko-fi and Haven's own trigger button. Slides DOWN from
 * above the header rather than up from below.
 *
 * Both hidden and visible transforms are declared INSIDE the media
 * query with equivalent specificity to the desktop rules, so we
 * don't rely on cascade order to win — simpler and more robust. */
@media (max-width: 600px) {
	.pp-haven-promo {
		bottom: auto;
		top: 68px; /* 60px site header + 8px breathing gap */
		/* Hidden state: slides UP out of view above the header. */
		transform: translate(-50%, -200%);
		padding: 11px 40px 11px 14px;
		max-width: calc(100vw - 24px);
		border-radius: 12px;
	}
	/* Visible state on mobile: slide DOWN into place just under the
	 * header. This rule has the same class count as the desktop
	 * equivalent but lives inside the media query so it wins on
	 * source order when mobile width matches. */
	.pp-haven-promo.is-visible {
		transform: translate(-50%, 0);
	}
	.pp-haven-promo__icon {
		width: 32px;
		height: 32px;
	}
	.pp-haven-promo__icon svg {
		width: 16px;
		height: 16px;
	}
	.pp-haven-promo__headline { font-size: 13px; }
	.pp-haven-promo__sub      { font-size: 11px; }
	.pp-haven-promo__close    {
		top: 6px;
		right: 6px;
		width: 24px;
		height: 24px;
		font-size: 18px;
	}
}

@media (prefers-reduced-motion: reduce) {
	.pp-haven-promo {
		transition: opacity 0.2s ease;
	}
}
