:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;

  --brand: #0b6b2e;
  --brand-dark: #0f0f0f;
  --brand-mid: #04a247;
  --brand-soft: #e4f7ea;
  --ink: #14251c;
  --ink-soft: #4a5952;
  --muted: #6b7a71;
  --line: #e3e9e4;
  --surface: #ffffff;
  --field: #fafbfa;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow-card: 0 30px 70px -18px rgba(18, 54, 34, .22), 0 3px 10px rgba(18, 54, 34, .05);
  --shadow-soft: 0 10px 24px rgba(18, 54, 34, .08);
  --shadow-launcher: 0 16px 32px -10px rgba(18, 54, 34, .38), 0 3px 8px rgba(18, 54, 34, .12);

  background:
    radial-gradient(1100px 560px at 10% -12%, #dcefe3 0%, transparent 58%),
    radial-gradient(900px 520px at 105% 0%, #eaf7ee 0%, transparent 55%),
    linear-gradient(180deg, #f5f9f6 0%, #eef3ef 100%);
  color: var(--ink);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
button, textarea { font: inherit; }

.shell { min-height: 100vh; }

/* ---------- Floating widget shell ---------- */

.widget-root {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
}

.chat-launcher {
  position: relative;
  flex: none;
  width: 60px;
  height: 60px;
  border-radius: 999px;
  border: 0;
  padding: 0;
  background: #fff;
  box-shadow: var(--shadow-launcher);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform .15s ease, box-shadow .15s ease;
}
.chat-launcher:hover { transform: scale(1.04); box-shadow: 0 20px 38px -10px rgba(18, 54, 34, .44), 0 4px 10px rgba(18, 54, 34, .14); }
.chat-launcher[hidden] { display: none; }
.chat-launcher:active { transform: scale(.98); }
.launcher-logo { width: 42px; height: 42px; object-fit: contain; pointer-events: none; }
.launcher-status-dot {
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 14px;
  height: 14px;
  border-radius: 999px;
  background: var(--brand-mid);
  border: 2.5px solid #fff;
}

.launcher-tooltip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px 10px 12px;
  border-radius: 24px;
  background: #fff;
  box-shadow: 0 12px 30px -8px rgba(18, 54, 34, .28), 0 2px 8px rgba(18, 54, 34, .08);
  font-size: 13.5px;
  font-weight: 650;
  color: var(--ink);
  transition: opacity .18s ease, transform .18s ease;
}
.tooltip-icon {
  width: 26px;
  height: 26px;
  border-radius: 999px;
  background: var(--brand-soft);
  padding: 4px;
  object-fit: contain;
}
.widget-root.open .launcher-tooltip,
.launcher-tooltip[hidden] { display: none; }

/* ---------- Chat window ---------- */

.chat-card {
  position: relative;
  width: 380px;
  min-width: 360px;
  max-width: 400px;
  height: min(650px, calc(100vh - 48px));
  /* Flex column rather than fixed grid rows: #reconnect-banner and
     #typing-indicator are conditionally [hidden], so a positional
     grid-template-rows list hands the 1fr track to whichever child happens to
     be rendering fourth (the composer, when both are hidden) instead of the
     message list. */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid rgba(20, 58, 38, .08);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}
.chat-card[hidden] { display: none; }
/* Only the message list takes the leftover height; every other row keeps its
   natural size so the composer stays pinned to the bottom from the very first
   turn, before any messages exist. */
.chat-card > *:not(.messages) { flex: none; }

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 17px 16px;
}

.header-title-row { display: flex; align-items: center; gap: 12px; min-width: 0; }

.avatar {
  position: relative;
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 999px;
  background: var(--brand-soft);
  display: grid;
  place-items: center;
}
.brand-mark { width: 26px; height: 26px; object-fit: contain; }
.connection-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 11px;
  height: 11px;
  border-radius: 999px;
  background: #7d8b83;
  border: 2px solid #fff;
  transition: background .2s ease;
}
.connection-dot.online { background: var(--brand-mid); animation: pulse-dot 2.4s ease-in-out infinite; }
.connection-dot.waiting { background: #d79320; }
.connection-dot.human { background: #1f6bdb; }
@keyframes pulse-dot {
  0%, 100% { box-shadow: 0 0 0 0 rgba(4, 162, 71, .18); }
  50% { box-shadow: 0 0 0 4px rgba(4, 162, 71, 0); }
}

.header-title-text { min-width: 0; }
h1 { margin: 0; font-size: 15.5px; font-weight: 700; letter-spacing: -.01em; }
.status-line { margin: 2px 0 0; color: var(--brand); font-weight: 600; font-size: 11.5px; }

.header-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.icon-button {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.icon-button:hover { background: var(--brand-soft); color: var(--ink); }

.menu-wrap { position: relative; }
.menu-popover {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 190px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-soft);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 5;
}
.menu-popover[hidden] { display: none; }
.menu-item {
  border: 0;
  background: transparent;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
}
.menu-item:hover { background: var(--brand-soft); }

.tagline {
  text-align: center;
  padding: 8px 16px 14px;
  border-bottom: 1px solid var(--line);
}
.tagline-title { margin: 0; font-size: 14px; font-weight: 700; color: var(--ink); }
.tagline-sub { margin: 3px 0 0; font-size: 11px; color: var(--muted); }

/* ---------- Messages ---------- */

.messages {
  /* min-height:0 lets this shrink below its content so overflow-y actually
     scrolls instead of stretching the card. */
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  background: #fbfcfb;
  scrollbar-width: thin;
  scrollbar-color: #c7d5cc transparent;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: #c7d5cc; border-radius: 999px; }
.messages::-webkit-scrollbar-track { background: transparent; }

.message {
  position: relative;
  max-width: 84%;
  border-radius: 16px;
  padding: 12px 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  font-size: 14.5px;
  margin-top: 16px;
}
.messages > .message:first-child { margin-top: 0; }
.message p { margin: 0; }
.message p + p { margin-top: .6em; }

.message-label { margin-bottom: 5px; font-size: 11px; font-weight: 750; letter-spacing: .04em; opacity: .68; }

.assistant-message {
  align-self: flex-start;
  margin-left: 34px;
  background: #f4f6f4;
  color: var(--ink);
  border-bottom-left-radius: 5px;
}
.assistant-message .message-label { display: none; }
.assistant-message::before {
  content: "";
  position: absolute;
  left: -34px;
  top: 0;
  width: 26px;
  height: 26px;
  border-radius: 999px;
  background: var(--brand-soft) url("/static/img/logo-icon.png") center/60% no-repeat;
}
.assistant-message::after {
  content: "AI · Answer";
  display: block;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid #e6ebe7;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .02em;
  color: var(--muted);
}
/* Cluster consecutive assistant turns: hide the repeated avatar and tighten spacing. */
.assistant-message + .assistant-message { margin-top: 6px; }
.assistant-message + .assistant-message::before { content: none; }

.user-message {
  align-self: flex-end;
  background: linear-gradient(160deg, var(--brand-mid), var(--brand-dark));
  color: #fff;
  border-bottom-right-radius: 5px;
  box-shadow: 0 8px 20px -6px rgba(20, 58, 38, .35);
}
.user-message + .user-message { margin-top: 6px; }
.read-receipt { display: block; margin-top: 6px; text-align: right; font-size: 10.5px; color: var(--muted); }

.sources {
  margin-top: 10px;
  border-top: 1px solid #e3e9e5;
  padding-top: 8px;
  font-size: 13px;
}
.sources summary { cursor: pointer; font-weight: 700; color: #286d48; }
.sources summary:hover { color: var(--brand-dark); }
.sources ul { margin: 8px 0 0; padding-left: 18px; }
.sources a { color: #1d6240; overflow-wrap: anywhere; }

.inline-sources { margin-top: 10px; font-size: 13px; color: #506158; }
.inline-sources a { color: #1d6240; font-weight: 700; overflow-wrap: anywhere; }

/* ---------- Typing indicator ---------- */

.typing-indicator {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 0 16px 10px;
}
.typing-indicator[hidden] { display: none; }
.typing-avatar {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 999px;
  background: var(--brand-soft) url("/static/img/logo-icon.png") center/60% no-repeat;
}
.typing-bubble {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 11px 14px;
  border-radius: 16px;
  border-bottom-left-radius: 5px;
  background: #f4f6f4;
}
.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--ink-soft);
  opacity: .5;
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { opacity: .45; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

/* ---------- Composer ---------- */

.composer { padding: 14px 16px 8px; }

.composer-field {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 64px;
  border: 1px solid #e0e6e1;
  border-radius: 26px;
  background: var(--field);
  padding: 12px 50px 12px 16px;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
.composer-field:focus-within { border-color: var(--brand-mid); background: #fff; box-shadow: 0 0 0 3px rgba(4, 162, 71, .12); }

textarea {
  width: 100%;
  max-height: 84px;
  resize: none;
  border: 0;
  padding: 0;
  outline: none;
  background: transparent;
  font-size: 14.5px;
  line-height: 1.4;
  color: var(--ink);
}
textarea::placeholder { color: #98a49d; }

#send-button {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--brand);
  opacity: .45;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: opacity .15s ease, transform .1s ease;
}
#send-button:hover:not(:disabled) { opacity: .7; }
#send-button.active { opacity: 1; }
#send-button:active:not(:disabled) { transform: translateY(-50%) scale(.92); }
#send-button:disabled { cursor: wait; opacity: .3; }

/* ---------- Footer ---------- */

.status-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 0 20px;
  color: var(--muted);
  font-size: 10.5px;
  min-height: 14px;
}

.brand-footer {
  text-align: center;
  padding: 6px 16px 14px;
  font-size: 10.5px;
  color: var(--muted);
}
.brand-footer strong { color: var(--ink); font-weight: 750; }

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

.system-message { align-self: center; max-width: 92%; background: #edf3ef; border: 1px solid #d8e3dc; color: #526158; font-size: 13px; text-align: center; box-shadow: none; }
.system-message .message-label { display: none; }
.human-message { align-self: flex-start; background: #eef4ff; border: 1px solid #d1ddf3; border-bottom-left-radius: 5px; }

.message-meta { margin-top: 6px; font-size: 10px; opacity: .62; }

.message-list { margin: 0; padding-left: 20px; }
.message-list li { margin: 2px 0; }

.reconnect-banner {
  padding: 8px 16px;
  font-size: 12px;
  text-align: center;
  color: #8a5a12;
  background: #fdf3de;
  border-top: 1px solid #f2e2b8;
  border-bottom: 1px solid #f2e2b8;
}

.message-failed { box-shadow: 0 0 0 1px #e2b3a8 inset; }
.message-retry {
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #a33f2c;
}
/* The failed state also applies to user-message bubbles, which use a dark
   green background -- the default reddish label color assumes a light
   background and is low-contrast there, so give it a light variant. */
.user-message.message-failed { box-shadow: 0 0 0 2px #ffb9a3 inset; }
.user-message .message-retry { color: #ffe4da; }
.retry-button {
  border: 1px solid #d98a76;
  background: #fff;
  color: #a33f2c;
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: background .15s ease;
}
.retry-button:hover { background: #fdf1ee; }

@media (prefers-reduced-motion: no-preference) {
  .message { animation: message-in .2s ease-out; }
}
@keyframes message-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .connection-dot.online { animation: none; }
  .typing-dot { animation: none; opacity: .8; }
}

/* ---------- Mobile ---------- */

@media (max-width: 640px) {
  .widget-root { right: 0; bottom: 0; left: 0; align-items: stretch; gap: 0; }
  .chat-launcher { align-self: flex-end; margin: 0 16px 16px 0; }
  .launcher-tooltip { align-self: flex-end; margin-right: 16px; }
  .chat-card {
    width: 100%;
    min-width: 0;
    max-width: none;
    height: 100dvh;
    height: 100vh;
    border-radius: 0;
    border: 0;
  }
  .message { max-width: 90%; }
}
