/* ============================================================
   Chat bubble + panel — fixed on the LEFT side of the screen
   ============================================================ */

/* ── Floating trigger button ─────────────────────────────── */
#chat-bubble-btn {
  position: fixed;
  left: 18px;
  bottom: 24px;
  z-index: 1100;

  width: auto;
  min-width: 52px;
  height: 48px;
  padding: 0 20px;
  border-radius: 999px;
  border: none;
  cursor: pointer;

  background: var(--accent-color);
  color: #fff;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  line-height: 1;

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

  box-shadow: 0 6px 18px rgba(91, 91, 214, 0.4);
  transition: background 0.18s, transform 0.18s, box-shadow 0.18s;
}

#chat-bubble-btn:hover {
  background: var(--accent-strong-color);
  transform: scale(1.07);
  box-shadow: 0 8px 22px rgba(91, 91, 214, 0.5);
}

/* ── Slide-up panel ──────────────────────────────────────── */
#chat-panel {
  position: fixed;
  left: 18px;
  bottom: 84px;
  z-index: 1099;

  width: 340px;
  max-height: 520px;

  background: var(--surface-bg);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.14);

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

  /* closed by default — toggled by Dash callback via style prop */
  visibility: hidden;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}

#chat-panel.chat-panel--open {
  visibility: visible;
  opacity: 1;
  transform: translateY(0);
}

/* ── Panel header ────────────────────────────────────────── */
#chat-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 10px;
  border-bottom: 1px solid var(--border-color);
  background: var(--surface-muted-bg);
  border-radius: 14px 14px 0 0;
  flex-shrink: 0;
}

#chat-panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-color);
  margin: 0;
}

#chat-close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted-color);
  font-size: 18px;
  line-height: 1;
  padding: 0 2px;
  transition: color 0.15s;
}

#chat-close-btn:hover {
  color: var(--text-soft-color);
}

/* ── Message area ────────────────────────────────────────── */
#chat-messages {
  flex: 1 1 auto;
  /* Without min-height:0 a flex child won't shrink below its content size,
     which pushes the input row out of the panel after a few messages. */
  min-height: 0;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.chat-msg {
  max-width: 88%;
  padding: 8px 11px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}

.chat-msg--user {
  align-self: flex-end;
  background: var(--accent-color);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.chat-msg--assistant {
  align-self: flex-start;
  background: var(--surface-subtle-bg);
  color: var(--text-color);
  border-bottom-left-radius: 3px;
}

.chat-msg--error {
  align-self: flex-start;
  background: var(--danger-bg);
  color: var(--danger-text);
  border-bottom-left-radius: 3px;
}

/* Typing indicator dots */
.chat-typing {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  background: var(--surface-subtle-bg);
  border-radius: 10px;
  border-bottom-left-radius: 3px;
}

.chat-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted-color);
  animation: chat-bounce 1.2s infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes chat-bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40%           { transform: translateY(-5px); }
}

/* ── Input row ───────────────────────────────────────────── */
#chat-input-row {
  display: flex;
  gap: 6px;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border-color);
  background: var(--surface-muted-bg);
  border-radius: 0 0 14px 14px;
  flex-shrink: 0;
}

#chat-input {
  flex: 1 1 auto;
  border: 1px solid var(--border-strong-color);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text-color);
  background: var(--surface-bg);
  outline: none;
  transition: border-color 0.15s;
  height: 36px;
  min-width: 0;
}

#chat-input:focus {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

#chat-input:disabled {
  color: var(--text-muted-color);
  background: var(--surface-subtle-bg);
  cursor: not-allowed;
}

#chat-send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: var(--accent-color);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  align-self: flex-end;
}

#chat-send-btn:hover  { background: var(--accent-strong-color); }
#chat-send-btn:active { background: var(--accent-deep-color); }
#chat-send-btn:disabled {
  background: var(--text-muted-color);
  cursor: not-allowed;
}

/* The light-blue dark-theme accent does not provide sufficient contrast with
   white text. Use the dark ink token for chat actions/messages in that theme. */
:root[data-theme="dark"] .chat-msg--user,
:root[data-theme="dark"] #chat-send-btn,
:root[data-theme="dark"] #chat-bubble-btn {
  color: #0f172a;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .chat-msg--user,
  :root:not([data-theme]) #chat-send-btn,
  :root:not([data-theme]) #chat-bubble-btn {
    color: #0f172a;
  }
}

/* ============================================================
   Mobile (≤ 720px): chat opens as a bottom sheet so it stops
   covering the workspace tray, dropdowns, and section controls.
   ============================================================ */
@media (max-width: 720px) {
  /* Pull the trigger button into a corner that doesn't sit over
     the discovery tabs or chart action buttons. */
  #chat-bubble-btn {
    left: auto;
    right: 14px;
    bottom: 16px;
    width: auto;
    min-width: 48px;
    height: 48px;
    padding: 0 16px;
    font-size: 13px;
  }

  /* Bottom sheet: full-width, anchored to the bottom edge. */
  #chat-panel {
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    max-width: 100vw;
    /* Keep half the viewport visible so users can still see the
       page beneath the sheet. */
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.18);
    transform: translateY(16px);
  }

  #chat-panel.chat-panel--open {
    transform: translateY(0);
  }

  #chat-panel-header {
    border-radius: 16px 16px 0 0;
    padding: 14px 16px 12px;
  }

  /* Allow the message area to shrink below content size — without
     min-height: 0 the input row gets pushed out of view once a few
     messages stack up. */
  #chat-messages {
    min-height: 0;
    padding: 12px 16px;
  }

  #chat-input-row {
    /* Sticky so the composer remains visible while the message
       area scrolls. */
    position: sticky;
    bottom: 0;
    padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  }

  #chat-input {
    font-size: 14px; /* Avoid iOS zoom-on-focus. */
    height: 40px;
  }

  #chat-send-btn {
    width: 40px;
    height: 40px;
  }
}
