/* Live chat: the website widget (launcher + panel) and the shared message thread / composer (chat.js). */

/* ---------- launcher ---------- */
.chat-launcher {
  position: fixed; right: 1rem; bottom: max(1rem, calc(env(safe-area-inset-bottom) + 0.5rem)); z-index: 50;
  display: flex; align-items: center; justify-content: center; width: 3.75rem; height: 3.75rem;
  border: 0; border-radius: 999px; background: var(--brand-secondary); color: #fff; box-shadow: var(--shadow-fab); cursor: pointer;
  transition: transform 0.15s ease;
}
.chat-launcher:hover { transform: scale(1.05); }
.chat-launcher:focus-visible { outline: 3px solid var(--brand-primary); outline-offset: 3px; }
.chat-launcher-icon { width: 1.75rem; height: 1.75rem; }
.chat-status-dot { position: absolute; right: 0.125rem; top: 0.125rem; width: 0.875rem; height: 0.875rem; border: 2px solid #fff; border-radius: 999px; background: #a3a3a3; }
.chat-status-dot.is-online { background: var(--state-success); }
.chat-unread { position: absolute; left: -0.25rem; top: -0.25rem; min-width: 1.25rem; padding: 0 0.375rem; border-radius: 999px; background: var(--brand-primary); color: #fff; font-size: var(--text-caption); font-weight: 600; line-height: 1.25rem; text-align: center; }

/* ---------- panel (phones: full visual viewport; larger: docked above the button) ---------- */
.chat-panel {
  position: fixed; z-index: 50; inset: 0 0 auto 0; height: var(--chat-vh, 100dvh);
  display: flex; flex-direction: column; overflow: hidden; background: var(--bg-surface); color: var(--text-primary);
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.25); outline: none;
}
.chat-panel[hidden] { display: none; }
.chat-panel-head { display: flex; flex-shrink: 0; align-items: flex-start; gap: 0.75rem; padding: max(0.75rem, env(safe-area-inset-top)) 1rem 0.75rem; background: var(--brand-deep); color: #fff; }
.chat-panel-head img { height: 2.25rem; width: auto; margin-top: 0.25rem; flex-shrink: 0; }
.chat-panel-title { flex: 1; min-width: 0; }
.chat-panel-title h2 { color: #fff; font-family: var(--font-sans); font-size: var(--text-small); font-weight: 700; letter-spacing: 0.025em; text-transform: uppercase; }
.chat-panel-title p { margin: 0; font-size: var(--text-caption); color: rgb(255 255 255 / 0.8); }
.chat-online { display: inline-flex; align-items: center; gap: 0.375rem; margin-top: 0.25rem !important; color: #fff !important; }
.chat-online-dot { width: 0.5rem; height: 0.5rem; border-radius: 999px; background: #a3a3a3; }
.chat-online-dot.is-online { background: var(--state-success); }
.chat-panel-actions { display: flex; flex-shrink: 0; gap: 0.25rem; }
.chat-head-button { display: flex; align-items: center; justify-content: center; width: 2.5rem; height: 2.5rem; border: 0; border-radius: var(--radius); background: transparent; color: #fff; cursor: pointer; }
.chat-head-button:hover { background: rgb(255 255 255 / 0.1); }
.chat-head-button:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }
.chat-loading { margin: auto; font-size: var(--text-small); color: var(--text-muted); }

.chat-welcome { display: flex; flex: 1; min-height: 0; flex-direction: column; gap: 1rem; overflow-y: auto; overscroll-behavior: contain; padding: 1rem; }
.chat-intro h3 { font-size: 1.125rem; }
.chat-intro p { margin: 0.25rem 0 0; font-size: var(--text-small); color: var(--text-muted); }
.chat-topics { display: grid; gap: 0.5rem; }
.chat-topics button { display: flex; align-items: center; gap: 0.75rem; min-height: 3rem; padding: 0 0.75rem; border: 1px solid var(--border-default); border-radius: var(--radius-xl); background: var(--bg-surface); color: var(--text-primary); font-size: var(--text-small); font-weight: 500; text-align: left; cursor: pointer; transition: border-color 0.15s ease, background-color 0.15s ease; }
.chat-topics button:hover { border-color: var(--brand-secondary); background: color-mix(in srgb, var(--brand-secondary) 5%, white); }
.chat-topic-icon { display: flex; align-items: center; justify-content: center; width: 2rem; height: 2rem; flex-shrink: 0; border-radius: var(--radius); background: color-mix(in srgb, var(--brand-secondary) 10%, white); color: var(--brand-secondary-strong); }
.chat-welcome-foot { margin-top: auto; }
.chat-privacy { margin: 0; font-size: var(--text-caption); color: var(--text-muted); }
.chat-privacy a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

.chat-start { display: flex; flex: 1; min-height: 0; flex-direction: column; }
.chat-start-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 0.75rem; padding: 1rem; }
.chat-start .field label { font-size: var(--text-small); font-weight: 500; color: var(--text-primary); }
.chat-start .field input, .chat-start .field select { min-height: 2.75rem; }
.chat-optional { font-weight: 400; color: var(--text-muted); }
.chat-back { display: inline-flex; align-items: center; gap: 0.25rem; min-height: 2.25rem; width: fit-content; padding: 0; border: 0; background: none; color: var(--text-muted); font-size: var(--text-small); cursor: pointer; }
.chat-back:hover { color: var(--text-primary); }
.chat-form-error { margin: 0; padding: 0.5rem 0.75rem; border-radius: var(--radius-md); background: color-mix(in srgb, var(--state-error) 10%, white); color: var(--state-error-text); font-size: var(--text-small); }
.chat-start-foot { flex-shrink: 0; padding: 0.75rem 0.75rem max(0.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border-default); }
.chat-submit { width: 100%; }

.chat-pane { display: flex; flex: 1; min-height: 0; flex-direction: column; }
/* The agent helping the visitor (live-chat-presence.md §identity). */
.chat-agent { display: flex; align-items: center; gap: 0.75rem; padding: 0.625rem 1rem; border-bottom: 1px solid var(--border-default); background: var(--bg-muted, #f5f7fb); }
.chat-avatar.chat-agent-avatar { display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; width: 2.5rem; height: 2.5rem; border-radius: 50%; overflow: hidden; background: var(--brand-primary); color: #fff; font-size: 0.875rem; font-weight: 700; }
.chat-avatar.chat-agent-avatar img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: center; }
.chat-agent-text { display: flex; flex-direction: column; min-width: 0; font-size: var(--text-caption); color: var(--text-muted); line-height: 1.35; }
.chat-agent-text strong { font-size: var(--text-small); color: var(--text-primary); overflow-wrap: anywhere; }
.chat-agent-text > span { overflow-wrap: anywhere; }
.chat-agent-presence { font-weight: 600; }
.chat-agent-presence.is-online { color: #0d6a39; }
.chat-agent-presence.is-busy, .chat-agent-presence.is-away { color: #8a4b00; }
.chat-pane-status { display: flex; flex-shrink: 0; align-items: center; gap: 0.5rem; padding: 0.5rem 1rem; border-bottom: 1px solid var(--border-default); font-size: var(--text-caption); color: var(--text-muted); }
.chat-status-line { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-pane-body { display: flex; flex: 1; min-height: 0; flex-direction: column; }
.chat-new { flex-shrink: 0; padding: 0.75rem 0.75rem max(0.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border-default); }
.chat-new .btn { width: 100%; }
.chat-mini { min-height: 2rem; padding: 0 0.625rem; font-size: var(--text-caption); }
.chat-ghost { background: transparent; color: var(--text-primary); }
.chat-ghost:hover { background: var(--bg-muted); }

/* ---------- thread ---------- */
.chat-thread { display: flex; flex: 1; min-height: 0; flex-direction: column; gap: 0.75rem; overflow-y: auto; overscroll-behavior: contain; padding: 1rem 0.75rem; outline: none; }
.chat-thread:focus-visible { box-shadow: inset 0 0 0 2px var(--brand-accent); }
.chat-connection { display: inline-flex; align-items: center; gap: 0.5rem; margin: 0 auto; padding: 0.25rem 0.75rem; border-radius: 999px; background: color-mix(in srgb, var(--state-warning) 10%, white); font-size: var(--text-caption); }
.chat-empty { margin: auto; font-size: var(--text-small); color: var(--text-muted); }
.chat-list { display: flex; flex-direction: column; gap: 0.75rem; list-style: none; margin: 0; padding: 0; }
.chat-day { margin: 0.25rem 0; text-align: center; font-size: var(--text-caption); color: var(--text-muted); }
.chat-system { align-self: center; max-width: 90%; padding: 0.375rem 0.75rem; border-radius: var(--radius); background: var(--bg-muted); color: var(--text-muted); font-size: var(--text-caption); text-align: center; }
.chat-message { display: flex; flex-direction: column; gap: 0.25rem; max-width: 85%; }
.chat-message.is-mine { align-items: flex-end; align-self: flex-end; }
.chat-message.is-theirs { align-items: flex-start; align-self: flex-start; }
.chat-sender, .chat-meta { padding: 0 0.25rem; font-size: var(--text-caption); color: var(--text-muted); display: inline-flex; flex-wrap: wrap; align-items: center; gap: 0.25rem; }
/* Sender identity (live-chat-presence.md): photo or initials, name, role · organisation. */
.chat-sender strong { color: var(--text-secondary); font-weight: 600; }
.chat-sender-role { color: var(--text-muted); }
.chat-avatar { display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; width: 1.75rem; height: 1.75rem; border-radius: 50%; overflow: hidden; background: var(--brand-primary); color: #fff; font-size: 0.6875rem; font-weight: 700; line-height: 1; }
.chat-avatar img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: center; }
.chat-bubble { padding: 0.5rem 0.875rem; border-radius: 1rem; font-size: var(--text-small); white-space: pre-wrap; overflow-wrap: anywhere; }
.is-mine > .chat-bubble { border-bottom-right-radius: 0.25rem; background: var(--brand-primary); color: #fff; }
.is-theirs > .chat-bubble { border-bottom-left-radius: 0.25rem; border: 1px solid var(--border-default); background: var(--bg-surface); color: var(--text-primary); }
.chat-bubble.is-pending { background: color-mix(in srgb, var(--brand-primary) 60%, white); }
.chat-bubble.is-failed { border: 1px solid color-mix(in srgb, var(--state-error) 60%, white); background: color-mix(in srgb, var(--state-error) 10%, white); color: var(--text-primary); }
.chat-failed { color: var(--state-error-text); justify-content: flex-end; }
.chat-typing { display: inline-flex; align-items: center; gap: 0.5rem; align-self: flex-start; margin: 0; padding: 0.375rem 0.75rem; border-radius: 999px; background: var(--bg-muted); font-size: var(--text-caption); color: var(--text-muted); }
.chat-dots { display: inline-flex; gap: 0.125rem; }
.chat-dots span { width: 0.375rem; height: 0.375rem; border-radius: 999px; background: var(--text-muted); animation: chat-bounce 1s infinite; }
.chat-dots span:nth-child(2) { animation-delay: 150ms; }
.chat-dots span:nth-child(3) { animation-delay: 300ms; }
@keyframes chat-bounce { 0%, 100% { transform: translateY(-25%); } 50% { transform: none; } }

/* ---------- composer ---------- */
.chat-composer { position: sticky; bottom: 0; display: flex; flex-direction: column; gap: 0.5rem; padding: 0.75rem 0.75rem max(0.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border-default); background: var(--bg-surface); }
.chat-composer[hidden] { display: none; }
.chat-composer-row { display: flex; align-items: flex-end; gap: 0.5rem; }
.chat-input { flex: 1; min-height: 2.75rem; max-height: 10rem; resize: none; padding: 0.625rem 0.75rem; border: 1px solid var(--border-strong); border-radius: var(--radius-md); background: var(--bg-surface); font-size: var(--text-small); }
.chat-input:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand-accent) 50%, transparent); outline-offset: 0; border-color: var(--brand-accent); }
.chat-send { display: flex; align-items: center; justify-content: center; flex-shrink: 0; width: 2.75rem; height: 2.75rem; border: 0; border-radius: var(--radius); background: var(--brand-primary); color: #fff; cursor: pointer; }
.chat-send:disabled { opacity: 0.5; cursor: not-allowed; }
.chat-hint { margin: 0; font-size: var(--text-caption); color: var(--text-muted); }

@media (min-width: 640px) {
  .chat-launcher { right: 1.5rem; }
  .chat-panel { inset: auto 1.5rem 6rem auto; width: 25rem; height: min(38rem, calc(100dvh - 8rem)); border: 1px solid var(--border-default); border-radius: 1rem; }
  .chat-thread { padding: 1rem; }
  .chat-message { max-width: 75%; }
}
@media (max-width: 639.98px) {
  body.chat-open .chat-launcher { display: none; }
}
@media (max-height: 499.98px) {
  .chat-panel { inset: 0 0 auto 0; width: 100%; height: var(--chat-vh, 100dvh); border: 0; border-radius: 0; }
  body.chat-open .chat-launcher { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .chat-launcher:hover { transform: none; }
  .chat-dots span { animation: none; }
}
/* Staff internal notes in a chat (admin inbox only: clients and visitors never receive them). */
.chat-message.is-internal .chat-bubble { border: 1px dashed color-mix(in srgb, var(--state-warning) 60%, white); background: color-mix(in srgb, var(--state-warning) 8%, white); color: var(--text-primary); }
