/* Chat launcher + modal (CSP-friendly; mobile-first) */

#openChatBtn {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom, 0px));
  z-index: 9999;
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: var(--text-inverse);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  font-size: 24px;
  transition: transform 0.2s ease;
}

#openChatBtn:hover {
  transform: scale(1.04);
}

#chat-modal {
  display: none;
  flex-direction: column;
  position: fixed;
  left: 0;
  right: 0;
  top: auto;
  width: 100%;
  max-width: 100%;
  bottom: 0;
  max-height: min(100dvh, 100vh);
  height: min(100dvh, 100vh);
  z-index: 10000;
  background: var(--chat-bg);
  color: var(--text-inverse);
  border-radius: 0;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--text-inverse) 12%, transparent);
  flex-direction: column;
}

#chat-modal .chat-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top, 0px));
  background: var(--chat-panel);
  border-bottom: 1px solid color-mix(in srgb, var(--text-inverse) 12%, transparent);
  flex-shrink: 0;
}

#chat-modal .chat-modal-header strong {
  display: block;
  color: var(--text-inverse);
  font-size: 16px;
}

#chat-modal .chat-modal-tagline {
  font-size: 11px;
  opacity: 0.7;
  color: var(--chat-text-soft);
}

#chat-modal #closeChatBtn {
  background: transparent;
  border: none;
  color: var(--text-inverse);
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#chat-messages {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  background: var(--chat-bg-deep);
  scrollbar-width: thin;
}

#chat-form {
  display: flex;
  gap: 10px;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
  border-top: 1px solid color-mix(in srgb, var(--text-inverse) 12%, transparent);
  background: var(--chat-panel);
  flex-shrink: 0;
}

#chat-input {
  flex: 1;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid var(--chat-border);
  background: var(--chat-bg);
  color: var(--text-inverse);
  outline: none;
  font-family: inherit;
  font-size: 16px;
  min-height: 44px;
}

#chat-form button[type='submit'] {
  padding: 10px 18px;
  border-radius: 10px;
  border: none;
  background: var(--accent-orange);
  color: var(--text-inverse);
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: filter 0.2s ease;
  font-size: 16px;
  min-height: 44px;
}

#chat-form button[type='submit']:hover {
  filter: brightness(1.06);
}

#chat-messages .chat-msg-user {
  align-self: flex-end;
  background: var(--accent-orange);
  padding: 10px 14px;
  border-radius: 12px 0 12px 12px;
  max-width: 85%;
  margin-left: auto;
}

#chat-messages .chat-msg-assistant {
  align-self: flex-start;
  background: var(--chat-panel);
  padding: 12px 16px;
  border-radius: 0 12px 12px 12px;
  max-width: 85%;
  line-height: 1.4;
}

#chat-messages .chat-msg-loading {
  align-self: flex-start;
  opacity: 0.6;
  font-size: 12px;
  margin-top: 4px;
}

#chat-messages .chat-bill-alert-hint {
  margin-top: 10px;
  padding: 10px;
  border-radius: 10px;
  background: var(--chat-bg);
  border: 1px solid var(--chat-border);
  font-size: 12px;
  line-height: 1.45;
  color: var(--chat-text-soft);
}

#chat-messages .chat-bill-alert-hint .wdp-alert-sub-btn {
  margin: 4px 6px 0 0;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--chat-border-soft);
  background: var(--chat-panel);
  color: var(--chat-text-soft);
  cursor: pointer;
  font-size: 12px;
}

#chat-messages .chat-sources-wrap {
  margin-top: 16px;
  border-top: 1px solid color-mix(in srgb, var(--text-inverse) 12%, transparent);
  padding-top: 12px;
}

#chat-messages .chat-sources-wrap summary {
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  opacity: 0.8;
  margin-bottom: 10px;
  outline: none;
  user-select: none;
}

#chat-messages .sources-list {
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
}

#chat-messages .source-row {
  margin-bottom: 8px;
  padding: 8px;
  background: color-mix(in srgb, var(--text-inverse) 6%, transparent);
  border-radius: 6px;
  font-size: 12px;
}

#chat-messages .source-row a {
  font-weight: 500;
}

@media (min-width: 768px) {
  #chat-modal {
    left: auto;
    top: auto;
    width: 420px;
    max-width: 420px;
    height: auto;
    max-height: 85vh;
    right: 16px;
    bottom: 80px;
    border-radius: 16px;
  }

  #chat-messages {
    max-height: 50vh;
    flex: none;
  }
}

#chat-input,
#chat-input:focus {
  font-size: 16px;
}

#chat-messages a {
  color: var(--chat-link) !important;
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--chat-link) 42%, transparent);
  text-underline-offset: 2px;
}

#chat-messages a:hover {
  color: var(--text-inverse) !important;
  text-decoration-color: var(--chat-link);
}

#chat-messages .citation-link {
  color: var(--chat-link) !important;
}
