/* Milestone B, issue B3b-2 — widget chat UI.
   Logical properties throughout (margin-inline-*, text-align: start, etc.)
   so the exact same rules serve both dir="rtl" (Arabic, default) and
   dir="ltr" (English) without a separate stylesheet — real RTL, not a
   mirrored LTR afterthought, per ux-arabic-rtl.md. */

:root {
  --chat-bg: #ffffff;
  --chat-fg: #1a1a1a;
  --chat-muted: #6b7280;
  --chat-border: #e5e7eb;
  --chat-accent: #0f766e;
  --chat-accent-dark: #0c5d56;
  --chat-accent-fg: #ffffff;
  --chat-bubble-in: #f3f4f6;
  --chat-error: #b91c1c;
  --chat-radius: 10px;
  --chat-radius-lg: 18px;
  --chat-online: #34d399;
  --chat-shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --chat-shadow-md: 0 4px 14px rgba(15, 23, 42, 0.1);
}

@media (prefers-reduced-motion: no-preference) {
  .chat__bubble {
    animation: chat-bubble-in 220ms ease-out both;
  }
  .chat__spinner {
    animation: chat-spin 900ms linear infinite;
  }
}

@keyframes chat-bubble-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes chat-spin {
  to {
    transform: rotate(360deg);
  }
}

* {
  box-sizing: border-box;
}

/* Must outrank every other rule below that sets display on a state/composer
   element (.chat__state, .chat__messages, .chat__composer all set their own
   `display`) — otherwise the browser's low-specificity UA-stylesheet
   [hidden]{display:none} rule loses to them and a "hidden" state renders
   visibly stacked on top of the active one, even though the hidden attribute
   and JS state are both correct. Verified this was a real, reproducing bug
   before adding this rule (multiple state divs had offsetParent !== null
   simultaneously), not a defensive guess. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", system-ui, sans-serif;
  color: var(--chat-fg);
  background: var(--chat-bg);
  /* Latin digits everywhere, always — per ux-arabic-rtl.md — enforced in
     chat.js's formatting logic, not by font choice here. */
}

.chat {
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-height: 320px;
}

.chat__header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: linear-gradient(160deg, var(--chat-accent), var(--chat-accent-dark));
  color: var(--chat-accent-fg);
  box-shadow: var(--chat-shadow-sm);
  position: relative;
  z-index: 1;
}

.chat__avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
}

.chat__avatar svg {
  width: 19px;
  height: 19px;
}

.chat__header-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.chat__title {
  font-weight: 600;
  font-size: 0.98rem;
  line-height: 1.3;
}

.chat__status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.82);
}

.chat__status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--chat-online);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25);
  flex-shrink: 0;
}

.chat__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #fafafa;
}

.chat__state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 100%;
  text-align: center;
  color: var(--chat-muted);
  font-size: 0.9rem;
  padding: 0 24px;
}

.chat__state--error,
.chat__state--offline,
.chat__state--expired {
  color: var(--chat-error);
}

.chat__spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2.5px solid var(--chat-border);
  border-top-color: var(--chat-accent);
}

.chat__messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 100%;
}

.chat__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex: 1;
  min-height: 220px;
  text-align: center;
  color: var(--chat-muted);
  font-size: 0.88rem;
  margin: 0;
}

.chat__empty-icon {
  width: 44px;
  height: 44px;
  color: var(--chat-border);
}

.chat__empty-text {
  margin: 0;
  max-width: 220px;
}

.chat__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat__bubble {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: var(--chat-radius-lg);
  line-height: 1.45;
  word-wrap: break-word;
  font-size: 0.93rem;
  box-shadow: var(--chat-shadow-sm);
}

.chat__bubble--inbound {
  align-self: flex-end;
  background: var(--chat-accent);
  color: var(--chat-accent-fg);
  border-end-end-radius: 4px;
}

.chat__bubble--outbound {
  align-self: flex-start;
  background: var(--chat-bubble-in);
  color: var(--chat-fg);
  border-end-start-radius: 4px;
}

.chat__composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--chat-border);
  background: var(--chat-bg);
}

.chat__input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--chat-border);
  border-radius: 999px;
  font-size: 0.93rem;
  text-align: start;
  background: #f9fafb;
  transition: border-color 150ms ease, background 150ms ease;
}

.chat__input:focus-visible {
  outline: none;
  border-color: var(--chat-accent);
  background: var(--chat-bg);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.chat__send {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--chat-accent);
  color: var(--chat-accent-fg);
  cursor: pointer;
  transition: background 150ms ease, transform 100ms ease;
}

.chat__send-icon {
  width: 18px;
  height: 18px;
}

/* Points toward the reading-forward direction in each writing mode — same
   logical-property discipline as this file's bubble radii above, since a
   literal SVG path can only be authored pointing one physical way. */
[dir="rtl"] .chat__send-icon {
  transform: scaleX(-1);
}

.chat__send:hover:not(:disabled) {
  background: var(--chat-accent-dark);
}

.chat__send:active:not(:disabled) {
  transform: scale(0.94);
}

.chat__send:focus-visible {
  outline: 2px solid var(--chat-fg);
  outline-offset: 2px;
}

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

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

/* Typing indicator (2026-09-18) — shown while a real reply is being
   generated, which can now genuinely take a few seconds. Styled AS an
   outbound (agent-side) bubble, unlike .chat__notice below (which is
   deliberately the interface speaking, not the agent) — this IS the agent,
   just not finished yet. Reduced opacity + italic signal "transient",
   never mistakable for a settled reply. */
.chat__typing {
  align-self: flex-start;
  max-width: 82%;
  padding: 9px 13px;
  border-radius: var(--chat-radius-lg);
  border-end-start-radius: 4px;
  background: var(--chat-bubble-in);
  color: var(--chat-fg);
  opacity: 0.65;
  font-style: italic;
  font-size: 0.93rem;
  box-shadow: var(--chat-shadow-sm);
}

/* System notice (2026-09-02) — shown when the customer's message was accepted
   but no AI provider could answer it. Deliberately NOT styled as a chat
   bubble: it is the interface speaking, not the agent, and must not be
   mistakable for a reply. */
.chat__notice {
  align-self: center;
  max-width: 90%;
  margin: 0.5rem 0;
  padding: 0.5rem 0.75rem;
  border-radius: 0.5rem;
  background: #f1f5f9;
  color: #475569;
  font-size: 0.875rem;
  text-align: center;
  list-style: none;
}
