/* chatbot.css — Chatbot Widget Styles (streaming-friendly, Markdown-ready)
   ---------------------------------------------------------------------- */
/* app/static/css/chatbot.css */
#chatbot {
  --chat-surface: var(--chat-surface-color);
  --chat-bot-bg: var(--chat-bot-bg-color);
  --chat-bot-fg: var(--chat-bot-fg-color);
  --chat-user-bg: var(--chat-user-bg-color);
  --chat-user-fg: var(--chat-user-fg-color);
  --chat-border: var(--chat-border-color);
  --chat-divider: var(--chat-divider-color);
  --chat-input-bg: var(--chat-input-bg-color);
}

/* ------------------------------------------------------------------ */
/* Chatbot Container (desktop)                                        */
/* ------------------------------------------------------------------ */
#chatbot {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: min(392px, calc(100vw - 32px));
  max-width: 100%;
  height: min(680px, calc(100vh - 64px));
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  background: rgb(0 0 0 / 0%);
  border: 1px solid var(--chat-border);
  border-radius: 24px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(18px);
  overflow: hidden;
  transition: transform 0.45s cubic-bezier(0.33, 1, 0.68, 1), opacity 0.35s ease;
  transform: translateY(calc(100% + 32px));
  opacity: 0;
  pointer-events: none;
  z-index: 9999;
}

#chatbot.active {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

#chat-overlay {
  position: fixed;
  inset: 0;
  background: var(--chat-overlay-color);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: 9998;
}

#chat-overlay.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* ------------------------------------------------------------------ */
/* Header                                                             */
/* ------------------------------------------------------------------ */
#chat-header {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  padding: 1.25rem 1.5rem;
  background: linear-gradient(201deg, var(--primary-color), var(--secondary-color));
  color: #fff;
}

#chat-avatar {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  width: 100%;
}

#chat-avatar img {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  object-fit: cover;
  flex-shrink: 0;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.22);
}

.chat-avatar-meta {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

#chat-avatar h2 {
  font-size: 1.1rem;
  margin: 0;
  color: #fff;
  font-weight: 600;
}

#chat-avatar p {
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.78);
  font-weight: 500;
  margin: 0;
}

#close-chat {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.14);
  color: #ffffff;
  font-size: 22px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

#close-chat:hover {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.6);
  transform: scale(1.05);
}

#close-chat:active {
  transform: scale(0.95);
}

#close-chat:focus-visible {
  outline: 2px solid #ffffff;
  outline-offset: 2px;
}

/* ------------------------------------------------------------------ */
/* Body                                                               */
/* ------------------------------------------------------------------ */
#chat-body {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  /* background: var(--chat-surface); */
}

#chat-messages {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem 1.5rem;
  overflow-y: auto;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb-color) transparent;
}

#chat-messages::-webkit-scrollbar {
  width: 6px;
}

#chat-messages::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, var(--secondary-color), rgba(4, 67, 40, 0.75));
  border-radius: 999px;
}

#chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

#chat-input {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb-color) transparent;
}

#chat-input::-webkit-scrollbar {
  width: 6px;
}

#chat-input::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, rgba(132, 227, 175, 0.85), var(--secondary-color));
  border-radius: 999px;
}

#chat-input::-webkit-scrollbar-track {
  background: transparent;
}

/* ------------------------------------------------------------------ */
/* Form                                                               */
/* ------------------------------------------------------------------ */
#chat-form {
  margin: 0 !important;
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  padding: 0.85rem 1.25rem 1.25rem;
  border-top: 1px solid var(--chat-divider);
  background: linear-gradient(180deg, transparent 0%, var(--chat-surface) 65%);
  backdrop-filter: blur(12px);
}

#chat-input {
  flex: 1;
  padding: 0.85rem 1rem;
  border: 1px solid transparent;
  border-radius: 18px;
  font-size: 0.95rem;
  line-height: 1.45;
  color: var(--chat-bot-fg);
  background: var(--chat-input-bg);
  outline: none;
  transition: border 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
  resize: none;
  overflow-y: auto;
  min-height: 75px;
  max-height: 140px;
}

#chat-input::placeholder {
  color: rgba(230, 237, 243, 0.6);
}

#chat-input:focus {
  border-color: rgba(132, 227, 175, 0.65);
  box-shadow: 0 0 0 3px rgba(132, 227, 175, 0.18);
  background: rgba(255, 255, 255, 0.12);
}

#send-button {
  background: linear-gradient(135deg, var(--secondary-color), var(--primary-color));
  color: #fff !important;
  border: none;
  width: 52px;
  height: 52px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 20px;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
  box-shadow: inset 0 0 0 0 #212931;
}

#send-button:hover {
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 12px 24px rgba(8, 80, 93, 0.35);
}

#send-button:focus-visible {
  outline: 2px solid rgba(132, 227, 175, 0.8);
  outline-offset: 2px;
}

#send-button:active {
  transform: scale(0.95);
}

#send-button:disabled {
  background: rgba(255, 255, 255, 0.25);
  cursor: not-allowed;
  box-shadow: none;
}

/* ------------------------------------------------------------------ */
/* Message Bubbles                                                     */
/* ------------------------------------------------------------------ */
.message {
  max-width: 85%;
  padding: 0.85rem 1rem;
  border-radius: 18px;
  font-size: 0.95rem;
  word-wrap: break-word;
  display: inline-block;
  line-height: 1.5;
  box-shadow: var(--shadow-sm);
  border: 1px solid transparent;
  animation: msg-in 0.25s ease-out;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.user-message {
  align-self: flex-end;
  background: var(--chat-user-bg);
  color: var(--chat-user-fg);
  border-radius: 18px 18px 6px 18px;
  border-color: rgba(255, 255, 255, 0.15);
  text-align: left;
}

.bot-message {
  align-self: flex-start;
  background: var(--chat-bot-bg);
  color: var(--chat-bot-fg);
  border-radius: 18px 18px 18px 6px;
  border-color: rgba(255, 255, 255, 0.08);
  text-align: left;
}

.message-time {
  display: block;
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.45);
  margin-top: 6px;
}

.user-message .message-time {
  text-align: right;
}

/* Links inside bubbles */
.message a {
  color: #84e3af;
  text-decoration: underline;
}

.message a:hover {
  color: #c8f3dd;
}

/* ------------------------------------------------------------------ */
/* Typing Bubble (professional look, reused for streaming)            */
/* ------------------------------------------------------------------ */
.typing-bubble {
  align-self: flex-start;
  background: var(--chat-bot-bg);
  color: var(--chat-bot-fg);
  border-radius: 18px 18px 18px 6px;
  padding: 0.85rem 1rem;
  max-width: 70%;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow-sm);
}

.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.typing-dots .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  animation: chat-dot 1.2s infinite ease-in-out;
  transform-origin: center bottom;
}

.typing-dots .dot:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-dots .dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes chat-dot {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.7;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* (Legacy) remove old grey pill typing style if present */
.typing {
  display: none !important;
}

/* ------------------------------------------------------------------ */
/* Floating Chat Icon                                                  */
/* ------------------------------------------------------------------ */
#chat-icon {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: linear-gradient(135deg, var(--secondary-color), var(--primary-color));
  color: white;
  width: 64px;
  height: 64px;
  border-radius: 20px;
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 28px;
  box-shadow: 0 16px 32px rgba(8, 80, 93, 0.35);
  transition: box-shadow 0.3s ease;
  animation: chat-icon-bounce 3.6s ease-in-out 1.2s infinite;
  z-index: 9998;
}

#chat-icon:hover,
#chat-icon:focus-visible {
  animation-play-state: paused;
  transform: translateY(-6px) scale(1.05);
  box-shadow: 0 20px 44px rgba(8, 80, 93, 0.5);
}

#chat-icon:focus-visible {
  outline: 3px solid rgba(132, 227, 175, 0.55);
  outline-offset: 4px;
}

#chat-icon:active {
  animation-play-state: paused;
  transform: scale(0.95);
}

/* Robot icon inside launcher */
#chat-icon i {
  font-size: 30px;
  color: #fff;
  line-height: 1;
}

@keyframes chat-icon-bounce {
  0%,
  60%,
  100% {
    transform: translate3d(0, 0, 0) scale(1);
    box-shadow: 0 16px 32px rgba(8, 80, 93, 0.35);
  }
  12% {
    transform: translate3d(0, -10px, 0) scale(1.04);
    box-shadow: 0 26px 38px rgba(8, 80, 93, 0.45);
  }
  26% {
    transform: translate3d(0, -4px, 0) scale(0.99);
    box-shadow: 0 12px 26px rgba(8, 80, 93, 0.32);
  }
  36% {
    transform: translate3d(0, -12px, 0) scale(1.06);
    box-shadow: 0 28px 44px rgba(8, 80, 93, 0.48);
  }
  48% {
    transform: translate3d(0, 2px, 0) scale(0.98);
    box-shadow: 0 10px 24px rgba(8, 80, 93, 0.3);
  }
}

@media (prefers-reduced-motion: reduce) {
  #chat-icon {
    animation: none;
  }
}

/* Hide launcher when chat is open */
#chatbot.active ~ #chat-icon {
  display: none;
}

/* ------------------------------------------------------------------ */
/* Notifications & Connection Errors (used by chat.js)                 */
/* ------------------------------------------------------------------ */
.chat-notification {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--chat-surface);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 0.875rem;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
  transform: translateY(-20px);
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 1000;
  border-left: 3px solid var(--secondary-color);
  color: var(--chat-bot-fg);
}

.chat-notification.show {
  transform: translateY(0);
  opacity: 1;
}

.chat-notification.success {
  color: #84e3af;
  border-left-color: #84e3af;
}

.chat-notification.error {
  color: #ff6b6b;
  border-left-color: #ff6b6b;
}

.connection-error {
  text-align: center;
  padding: 20px;
  color: #ff6b6b;
  background: var(--chat-input-bg);
  border: 1px solid var(--chat-divider);
  border-radius: 16px;
}

.connection-error button {
  margin-top: 12px;
  padding: 6px 16px;
  background: linear-gradient(135deg, var(--secondary-color), var(--primary-color));
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.connection-error button:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(8, 80, 93, 0.3);
}

/* ------------------------------------------------------------------ */
/* PHONE-ONLY overrides (desktop left untouched)                      */
/* ------------------------------------------------------------------ */
@media (max-width: 768px) {
  #chatbot {
    position: fixed;
    inset: 0;
    bottom: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100%;
    min-height: 0;
    min-width: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    transform: translateY(100%);
    background: var(--bg-dark);
    background-color: var(--bg-dark);
    backdrop-filter: none;
    padding-bottom: env(safe-area-inset-bottom);
    overflow: hidden;
    transition: none !important;
    opacity: 0;
    visibility: hidden;
    z-index: 2147483638;
  }

  body:not(.dark-mode) #chatbot {
    background: var(--bg-primary);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    --chat-surface: var(--bg-primary);
    --chat-bot-bg: #f3f4f6;
    --chat-bot-fg: var(--text-primary);
    --chat-border: rgba(15, 23, 42, 0.08);
    --chat-divider: rgba(15, 23, 42, 0.08);
    --chat-input-bg: rgba(8, 80, 93, 0.08);
  }

  #chatbot.active {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
    z-index: 2147483639;
  }

  /* Freeze the page behind when chat is open (class added by JS) */
  html.chat-open,
  body.chat-open {
    position: fixed;
    inset: 0;
    height: 100dvh;
    width: 100%;
    overflow: hidden !important;
  }

  #chat-header {
    padding: 1rem 1.25rem;
  }

  #chat-avatar {
    gap: 0.75rem;
  }

  #chat-avatar img {
    width: 52px;
    height: 52px;
    border-radius: 14px;
  }

  .chat-avatar-meta h2 {
    font-size: 1.05rem;
  }

  .chat-avatar-meta p {
    font-size: 0.8rem;
  }

  #chat-messages {
    padding: 1rem 1.25rem;
  }

  #chat-form {
    padding: 0.75rem 1rem calc(0.85rem + env(safe-area-inset-bottom));
    gap: 0.65rem;
  }

  #chat-input {
    font-size: 1rem;
    min-height: 80px;
  }

  #chat-icon {
    bottom: 16px;
    right: 16px;
    width: 60px;
    height: 60px;
    border-radius: 18px;
  }

  #chat-overlay {
    display: none;
  }

  body:not(.dark-mode) #chat-body,
  body:not(.dark-mode) #chat-messages {
    background: var(--bg-primary);
    color: var(--text-primary);
  }

  body:not(.dark-mode) #chat-form {
    background: linear-gradient(180deg, transparent 0%, var(--bg-primary) 65%);
    border-top-color: rgba(15, 23, 42, 0.08);
  }

  body:not(.dark-mode) #chat-input {
    color: var(--text-primary);
    border-color: rgba(15, 23, 42, 0.08);
  }

  body:not(.dark-mode) #chat-input::placeholder {
    color: rgba(71, 85, 105, 0.55);
  }

  body:not(.dark-mode) #chat-input:focus {
    background: rgba(132, 227, 175, 0.18);
    border-color: rgba(8, 80, 93, 0.25);
    box-shadow: 0 0 0 3px rgba(132, 227, 175, 0.22);
  }

  body:not(.dark-mode) .bot-message,
  body:not(.dark-mode) .typing-bubble {
    border-color: rgba(15, 23, 42, 0.08);
    color: var(--text-primary);
  }

  body:not(.dark-mode) .message.bot-message code {
    background: rgba(15, 23, 42, 0.08);
  }

  body:not(.dark-mode) .message-time {
    color: rgba(71, 85, 105, 0.65);
  }

  body:not(.dark-mode) .typing-dots .dot {
    background: rgba(71, 85, 105, 0.6);
  }
}

/* ------------------------------------------------------------------ */
/* Markdown-friendly typography inside bot messages                    */
/* ------------------------------------------------------------------ */
.message.bot-message h1,
.message.bot-message h2,
.message.bot-message h3 {
  margin: 0.4rem 0 0.25rem;
  line-height: 1.25;
}

.message.bot-message p {
  margin: 0.5rem 0;
}

.message.bot-message ul,
.message.bot-message ol {
  margin: 0.5rem 1.25rem;
}

.message.bot-message code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, "Courier New", monospace;
  font-size: 0.9em;
  background: rgba(255, 255, 255, 0.08);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.message.bot-message pre code {
  display: block;
  padding: 0.75rem;
  overflow-x: auto;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
}
