@import url('https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@300;400;600;700;800&display=swap');

:host {
  all: initial;
  font-family: var(--chat--font-family, 'Libre Franklin'), system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

*, *::before, *::after {
  box-sizing: border-box;
}

:root,
.root {
  /* Colors */
  --chat--color-primary: #F37536;
  --chat--color-primary-shade-50: #DF6337;
  --chat--color-primary-shade-100: #DF6337;
  --chat--color-secondary: #13403B;
  --chat--color-secondary-shade-50: #0B3437;
  --chat--color-on-primary: #FFF;
  
  /* Typography */
  --chat--font-family: 'Libre Franklin', sans-serif;

  /* Dimensions */
  --chat--window--width: 400px;
  --chat--window--height: 700px;

  /* Panel/Ink/Borders (Light Defaults) */
  --panel: #ffffff;
  --ink: #0f172a;
  --border: #d8e2ee;
  --shadow: 0 12px 30px rgba(2, 6, 23, .14);

  /* Backgrounds (passed from index.html or defaults) */
  --bg-base-1: var(--chat--bg-base-1, #FFFDFC);
  --bg-base-2: var(--chat--bg-base-2, #FFF6F0);
  --bg-accent: var(--chat--bg-accent, rgba(243, 117, 54, .08));
  --bg-accent-2: var(--chat--bg-accent-2, rgba(11, 52, 55, .06));
}

/* =========================================
   Toggle Button
   ========================================= */
.chat-toggle {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9998;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  background: var(--chat--color-primary);
  display: grid;
  place-items: center;
  box-shadow: 0 6px 18px rgba(243, 117, 54, .25);
  transition: transform .2s ease, box-shadow .2s, opacity .2s;
  overflow: hidden;
}

.chat-toggle.hidden {
  display: none !important;
  opacity: 0;
  pointer-events: none;
}

.chat-toggle:hover {
  transform: scale(1.05);
  box-shadow: 0 10px 24px rgba(243, 117, 54, .32);
}

.chat-toggle img {
  width: 100%;
  height: 85%;
  object-fit: cover;
  border-radius: 90%;
}

@keyframes pulse-bubble {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 6px 18px rgba(243, 117, 54, .25);
  }
  50% {
    transform: scale(1.08);
    box-shadow: 0 10px 24px rgba(243, 117, 54, .35);
  }
}

/* =========================================
   Chatbox Window
   ========================================= */
.chatbox {
  position: fixed;
  right: 24px;
  bottom: 20px;
  background: var(--panel);
  width: var(--chat--window--width, 400px);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transform: translateY(20px);
  transition: all .25s ease;
  height: var(--chat--window--height, 600px);
}

.chatbox.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* Header */
.chat-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  color: #fff;
  background: linear-gradient(180deg, var(--chat--color-primary-shade-50) 0%, var(--chat--color-primary) 100%);
  border-bottom: 3px solid var(--chat--color-secondary);
}

.brand-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  overflow: hidden;
  flex: 0 0 44px;
  border: 2px solid rgba(243, 117, 54, .9);
  background: #fff;
  display: grid;
  place-items: center;
}

.brand-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.titles {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  gap: 2px;
}

.title {
  font-weight: 800;
  font-size: 1.02rem;
}

.subtitle {
  font-size: .86rem;
  opacity: .92;
}

.close-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}

/* Body */
.chat-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 8px 14px 14px;
  background:
    radial-gradient(720px 380px at 110% 120%, var(--bg-accent), transparent 60%),
    radial-gradient(520px 280px at -20% -40%, var(--bg-accent-2), transparent 65%),
    linear-gradient(180deg, var(--bg-base-1) 0%, var(--bg-base-2) 100%);
}

/* =========================================
   Themes (Dark & Auto)
   ========================================= */
/* Explicit Dark Theme */
:host([theme="dark"]) .root {
  --panel: #0F1718;
  --ink: #E8EEF6;
  --border: #1f2a30;
  --shadow: 0 16px 36px rgba(0, 0, 0, .45);
}

:host([theme="dark"]) .chat-body {
  background:
    radial-gradient(700px 360px at 110% 120%, var(--bg-accent), transparent 60%),
    radial-gradient(520px 280px at -20% -40%, var(--bg-accent-2), transparent 65%),
    linear-gradient(180deg, var(--bg-base-1, #0E1719) 0%, var(--bg-base-2, #0A1214) 100%);
}

:host([theme="dark"]) .row.bot .bubble {
  background: var(--chat--color-secondary);
  color: #EAF2F4;
}

:host([theme="dark"]) .chat-input {
  background: #0F1718;
}

:host([theme="dark"]) .chat-input input {
  background: #0B1214;
  color: #E8EEF6;
}

/* Auto Theme (prefers-color-scheme) */
@media (prefers-color-scheme: dark) {
  :host([theme="auto"]) .root {
    --panel: #0F1718;
    --ink: #0f172a;
    --border: #1f2a30;
    --shadow: 0 16px 36px rgba(0, 0, 0, .45);
  }

  :host([theme="auto"]) .chat-body {
    background:
      radial-gradient(700px 360px at 110% 120%, var(--bg-accent), transparent 60%),
      radial-gradient(520px 280px at -20% -40%, var(--bg-accent-2), transparent 65%),
      linear-gradient(180deg, var(--bg-base-1, #0E1719) 0%, var(--bg-base-2, #0A1214) 100%);
  }
}

/* =========================================
   Messages / Bubbles
   ========================================= */
.row {
  display: flex;
  gap: 10px;
  margin: 10px 0;
}

.row.user {
  justify-content: flex-end;
}

.row.bot {
  justify-content: flex-start;
}

.bubble {
  max-width: 82%;
  padding: 12px 14px;
  border-radius: 16px;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--border);
  line-height: 1.55;
  font-size: 15.5px;
  white-space: pre-wrap;
  word-break: break-word;
  opacity: 0;
  animation: bubble-in .18s ease forwards;
  box-shadow: 0 6px 18px rgba(2, 6, 23, .08);
}

@keyframes bubble-in {
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Bot specific bubble styles */
.row.bot .bubble {
  background: var(--chat--color-secondary);
  color: #fff;
  border-color: transparent;
  font-size: 16px;
  line-height: 1.7;
}

.row.bot .bubble a {
  color: var(--chat--color-primary);
  text-decoration: underline;
}

/* User specific bubble styles */
.row.user .bubble {
  background: #fff;
  color: var(--ink);
  border-color: #e6edf6;
}

/* "New" bubble pulse effect */
.bubble.new {
  animation: bubble-in .18s ease forwards, pulseGlow 2s ease infinite;
}

@keyframes pulseGlow {
  0% { box-shadow: 0 0 0 rgba(243, 117, 54, 0); }
  40% { box-shadow: 0 0 18px rgba(243, 117, 54, .65); }
  100% { box-shadow: 0 0 0 rgba(243, 117, 54, 0); }
}

/* Skeleton Loading Bubble */
.bubble.skeleton {
  position: relative;
  background: linear-gradient(90deg, #eef3f9 25%, #f6f9ff 37%, #eef3f9 63%);
  background-size: 400% 100%;
  border-color: transparent;
  min-height: 56px;
  animation: shimmer 1.5s infinite linear;
}

@keyframes shimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

/* Streaming Bubble */
.bubble.streaming {
  position: relative;
  background: var(--chat--color-secondary);
  color: #fff;
  border-color: transparent;
  overflow: hidden;
  box-shadow: 0 8px 22px rgba(2, 6, 23, .10);
  transition: height 180ms ease, box-shadow 3s ease-in-out;
  animation: bubble-in .18s ease forwards, bubbleBreathe 3s ease-in-out infinite;
  min-height: 56px;
}

@keyframes bubbleBreathe {
  0%, 100% { box-shadow: 0 8px 22px rgba(2, 6, 23, .10); }
  50% { box-shadow: 0 10px 26px rgba(2, 6, 23, .16); }
}

.stream-wrap {
  display: inline;
}

.stream-text .unit {
  opacity: 0;
  animation: unitIn .24s ease forwards;
}

@keyframes unitIn {
  from { opacity: .1; filter: blur(.4px); }
  to { opacity: 1; filter: blur(0); }
}

/* Typing Caret */
.typing-caret {
  display: inline-block;
  width: 8px;
  height: 1.1em;
  margin-left: 2px;
  transform: translateY(2px);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .0);
  border-left: 2px solid currentColor;
  animation: caretBlink 1.2s steps(2, start) infinite;
}

@keyframes caretBlink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

/* =========================================
   Quick Replies
   ========================================= */
.btns {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.qr {
  appearance: none;
  border: 1px solid rgba(11, 52, 55, .25);
  border-radius: 999px;
  background: linear-gradient(180deg, #ffffff, #fff6f0);
  color: #0B3437;
  font-weight: 800;
  padding: 11px 13px;
  text-align: left;
  cursor: pointer;
  transition: transform .05s ease, box-shadow .2s, background .2s;
  box-shadow: 0 4px 14px rgba(11, 52, 55, .10);
}

.qr:hover {
  background: var(--chat--color-primary, #F37536);
  color: var(--chat--color-on-primary, #FFF);
  box-shadow: 0 6px 18px rgba(11, 52, 55, .14);
}

.qr:active {
  transform: scale(.985);
}

.qr:disabled {
  opacity: .6;
  cursor: not-allowed;
}

/* =========================================
   Input Area
   ========================================= */
.chat-input {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--border);
  background: #fff;
}

.chat-input input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  font-size: 15px;
  outline: none;
  background: #fff;
  color: var(--ink);
}

.chat-input button {
  min-width: 96px;
  border: 0;
  border-radius: 12px;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(180deg, var(--chat--color-primary), var(--chat--color-primary-shade-50));
  box-shadow: 0 6px 16px rgba(243, 117, 54, .35);
  cursor: pointer;
}

.chat-input button:disabled {
  filter: grayscale(.2);
  opacity: .85;
  cursor: not-allowed;
}

/* =========================================
   Loaders & Hints
   ========================================= */
.dots-loader {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 20px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chat--color-primary);
  animation: dot-bounce 1.4s infinite;
  box-shadow: 0 0 0 4px rgba(243, 117, 54, .18);
}

.dot:nth-child(2) { animation-delay: .15s; }
.dot:nth-child(3) { animation-delay: .30s; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: translateY(0) scale(.8); opacity: .6; }
  40% { transform: translateY(-3px) scale(1); opacity: 1; }
}

.hint-bubble {
  position: fixed;
  bottom: 100px;
  right: 24px;
  background: var(--chat--color-primary);
  color: #00143a;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .18);
  opacity: 0;
  transform: translateY(10px);
  transition: all .4s ease;
  z-index: 10000;
  pointer-events: none;
  white-space: nowrap;
}

.hint-bubble.show {
  opacity: 1;
  transform: translateY(0);
}

/* =========================================
   Accessibility & Mobile
   ========================================= */
@media (prefers-reduced-motion: reduce) {
  .bubble.streaming {
    transition: height 0ms linear;
    animation: none;
  }
  .bubble.done {
    animation: none;
  }
  .typing-caret {
    animation: none;
  }
}

/* Mobile */
@media screen and (max-width: 768px) {
  :host .root {
    --chat--window--width: calc(100vw - 2rem);
    --chat--window--height: calc(100vh - 4rem);
  }
}

/* Small mobile devices */
@media screen and (max-width: 480px) {
  :host .root {
    --chat--window--width: calc(100vw - 1rem);
    --chat--window--height: calc(100vh - 2rem);
  }
}
