/*
 * Chat styles, on top of Bootstrap 5. Colours come from CSS variables that each page sets
 * from the funeral home's settings (see Theme.java).
 *
 * The agent page runs inside StationMaster's WebKit (Safari 12 era): no flexbox "gap",
 * no "inset", no ":is()" in the rules shared with staff pages.
 */
:root {
	--chat-header-bg: #1f3a5f;
	--chat-header-fg: #ffffff;
	--chat-bg: #f5f6f8;
	--chat-fg: #212529;
	--chat-composer-bg: #ffffff;
	--chat-bubble-customer: #d2e9ff;
	--chat-bubble-client: #dff3d0;
	--chat-bubble-asd: #ffffff;
	--chat-bubble-system: #fde8e8;
}

[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--chat-bg); color: var(--chat-fg); }
/* The full-page chat sits on the funeral home's page colour (BackgroundColor). */
body.standalone { background: var(--chat-page-bg, #e9ecef); }

.chat-app {
	display: flex;
	flex-direction: column;
	height: 100%;
	background: var(--chat-bg);
	overflow: hidden;
}
body.standalone .chat-app {
	max-width: 720px;
	margin: 0 auto;
	box-shadow: 0 0 24px rgba(0, 0, 0, .12);
}

/* ---- header ---- */
.chat-header {
	display: flex;
	align-items: center;
	flex: 0 0 auto;
	padding: 10px 12px;
	background: var(--chat-header-bg);
	color: var(--chat-header-fg);
}
.chat-header-text { flex: 1 1 auto; min-width: 0; }
.chat-title { font-size: 1rem; font-weight: 600; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status { font-size: .75rem; opacity: .85; min-height: 1em; }
.chat-header-btn {
	flex: 0 0 auto;
	margin-left: 8px;
	padding: 4px 10px;
	border: 1px solid currentColor;
	border-radius: 999px;
	background: transparent;
	color: inherit;
	font-size: .8rem;
	line-height: 1.4;
}
.chat-header-btn:hover { background: rgba(255, 255, 255, .15); }
.chat-header-btn:focus-visible, .chat-star:focus-visible { outline: 3px solid #ffbf47; outline-offset: 2px; }
.chat-icon-btn { border-color: transparent; padding: 4px 6px; }

/* ---- body ---- */
.chat-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; }
.chat-body[hidden], .chat-composer[hidden] { display: none; }
.chat-instructions { font-size: .95rem; }
.chat-hp { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }

.chat-btn-primary { background: var(--chat-header-bg); color: var(--chat-header-fg); border-color: var(--chat-header-bg); }
.chat-btn-primary:hover, .chat-btn-primary:focus { background: var(--chat-header-bg); color: var(--chat-header-fg); filter: brightness(1.12); }
.chat-btn-primary:disabled { background: var(--chat-header-bg); color: var(--chat-header-fg); opacity: .5; }

.chat-banner { flex: 0 0 auto; padding: 6px 12px; font-size: .85rem; text-align: center; background: #fff3cd; color: #664d03; }
.chat-banner.error { background: #f8d7da; color: #842029; }

.chat-messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 12px; }
.chat-messages:focus { outline: none; }

/* ---- messages ---- */
.msg { display: flex; flex-direction: column; align-items: flex-start; margin-bottom: 10px; }
.msg-meta { font-size: .72rem; color: #5c636a; margin: 0 6px 2px; }
.msg-bubble {
	max-width: 85%;
	padding: 8px 12px;
	border-radius: 16px;
	border-bottom-left-radius: 4px;
	background: var(--chat-bubble-asd);
	box-shadow: 0 1px 1px rgba(0, 0, 0, .08);
	word-wrap: break-word;
	overflow-wrap: break-word;
	white-space: normal;
}
.msg-bubble a { word-break: break-all; }
.msg.dir0 .msg-bubble { background: var(--chat-bubble-customer); }
.msg.dir1 .msg-bubble { background: var(--chat-bubble-client); }
.msg.dir2 .msg-bubble { background: var(--chat-bubble-asd); }
.msg.dir3 { align-items: center; }
.msg.dir3 .msg-bubble { background: transparent; box-shadow: none; color: #5c636a; font-size: .82rem; text-align: center; padding: 2px 8px; }
.msg.dir3 .msg-meta { display: none; }

/* The viewer's own messages sit on the right. */
.msg.mine { align-items: flex-end; }
.msg.mine .msg-bubble { border-bottom-left-radius: 16px; border-bottom-right-radius: 4px; }
.msg.pending .msg-bubble { opacity: .6; }
.msg.failed .msg-bubble { outline: 2px solid #dc3545; }
.msg-retry { border: 0; background: none; padding: 0; margin: 2px 6px 0; font-size: .75rem; color: #b02a37; text-decoration: underline; }

.chat-typing { flex: 0 0 auto; min-height: 1.4em; padding: 0 16px 4px; font-size: .8rem; color: #5c636a; font-style: italic; }

.chat-leave { flex: 0 0 auto; margin: 0 12px 10px; padding: 10px 12px; background: #fff; border: 1px solid #dee2e6; border-radius: 12px; }

/* ---- composer ---- */
.chat-composer {
	display: flex;
	align-items: flex-end;
	flex: 0 0 auto;
	/* Roomy on purpose: visitors were missing the input box. */
	padding: 12px 12px 20px;
	padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
	background: var(--chat-composer-bg);
	border-top: 1px solid #dee2e6;
	box-shadow: 0 -2px 6px rgba(0, 0, 0, .06);
}
.chat-composer textarea { flex: 1 1 auto; resize: none; max-height: 120px; border-radius: 18px; font-size: 16px; /* 16px stops iOS zooming on focus */ }
.chat-send { flex: 0 0 auto; margin-left: 8px; width: 40px; height: 40px; padding: 0; border-radius: 50%; display: flex; align-items: center; justify-content: center; }

/* ---- rating ---- */
.chat-stars { display: flex; }
.chat-star { border: 0; background: none; padding: 0 4px; font-size: 2rem; line-height: 1; color: #c7ccd1; cursor: pointer; }
.chat-star.on { color: #f5a623; }

@media (prefers-reduced-motion: no-preference) {
	.chat-messages { scroll-behavior: smooth; }
}

/* ================= staff pages (agent, funeral home, dashboard) ================= */
body.staff { background: #e9ecef; }
body.staff .chat-app { max-width: none; }
.staff-info { flex: 0 0 auto; padding: 6px 12px; background: #fff; border-bottom: 1px solid #dee2e6; font-size: .9rem; }
.staff-info span + span { margin-left: 16px; }
.staff-page { color: #5c636a; }
.staff-tools { flex: 0 0 auto; padding: 8px 12px; background: #f8f9fa; border-top: 1px solid #dee2e6; }
.staff-tools select { display: inline-block; width: auto; max-width: 100%; margin: 0 6px 6px 0; }
.staff-composer { display: flex; align-items: flex-end; flex: 0 0 auto; padding: 10px 12px 18px; background: var(--chat-composer-bg); border-top: 1px solid #dee2e6; }
.staff-composer textarea { flex: 1 1 auto; min-height: 84px; font-size: 1rem; }
.staff-buttons { flex: 0 0 auto; margin-left: 8px; width: 96px; }
.staff-buttons .btn { display: block; width: 100%; margin-bottom: 4px; }
body.staff .msg-bubble { max-width: 92%; }
body.staff .msg-meta { font-size: .78rem; }
/* The customer's words are what the agent must not miss. */
body.staff .msg.dir0 .msg-bubble { font-size: 1.1rem; }
.staff-ended { flex: 0 0 auto; padding: 8px 12px; background: #f8d7da; color: #842029; font-weight: 600; }
.staff-waiting { background: #fff3cd; }

.dash-grid { display: flex; flex-wrap: wrap; padding: 8px; }
.dash-card { width: 300px; margin: 8px; padding: 10px 12px; background: #fff; border-radius: 10px; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.dash-card.closing { opacity: .45; }
.dash-card h2 { font-size: 1rem; margin: 0; }
.dash-card time { font-size: .75rem; color: #5c636a; }
.dash-card p { margin: 6px 0 0; font-size: .85rem; word-wrap: break-word; }
