/* ═══════════════════════════════════════════════════════════════
   RESET
═══════════════════════════════════════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ═══════════════════════════════════════════════════════════════
   DARK THEME (default)
═══════════════════════════════════════════════════════════════ */
:root {
  --accent:        #6366f1;
  --accent-hover:  #4f52e0;
  --accent-dim:    rgba(99,102,241,.15);
  --accent-glow:   rgba(99,102,241,.30);
  --surface:       #16181f;
  --surface-2:     #1e2130;
  --surface-3:     #252840;
  --border:        rgba(255,255,255,.08);
  --text-1:        #f0f0f5;
  --text-2:        #9095a8;
  --text-3:        #5a5f72;
  --user-bubble:   #6366f1;
  --bot-bubble:    #1e2130;
  --bot-border:    rgba(255,255,255,.08);
  --scrollbar:     #252840;
  --shadow:        0 24px 64px rgba(0,0,0,.6), 0 0 0 1px rgba(255,255,255,.06);
  --r-xl: 20px;
  --r-md: 14px;
  --r-sm:  8px;
  --ease: .2s cubic-bezier(.4,0,.2,1);
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

/* ═══════════════════════════════════════════════════════════════
   LIGHT THEME
═══════════════════════════════════════════════════════════════ */
[data-theme="light"] {
  --surface:    #ffffff;
  --surface-2:  #f2f2f7;
  --surface-3:  #e5e5ea;
  --border:     rgba(0,0,0,.08);
  --text-1:     #111118;
  --text-2:     #58586b;
  --text-3:     #9090a0;
  --bot-bubble: #f2f2f7;
  --bot-border: rgba(0,0,0,.07);
  --scrollbar:  #c8c8d8;
  --shadow:     0 16px 48px rgba(0,0,0,.14), 0 0 0 1px rgba(0,0,0,.07);
}

/* theme icon swap */
html[data-theme="dark"]  .icon-sun  { display: block; }
html[data-theme="dark"]  .icon-moon { display: none;  }
html[data-theme="light"] .icon-moon { display: block; }
html[data-theme="light"] .icon-sun  { display: none;  }

/* expand icon swap */
.panel:not(.expanded) .icon-collapse { display: none;  }
.panel:not(.expanded) .icon-expand   { display: block; }
.panel.expanded        .icon-expand  { display: none;  }
.panel.expanded        .icon-collapse{ display: block; }

/* ═══════════════════════════════════════════════════════════════
   BODY
═══════════════════════════════════════════════════════════════ */
body {
  font-family: var(--font);
  min-height: 100dvh;
  background: #0b0d14;
}
html[data-theme="light"] body { background: #e8e8f0; }

/* ═══════════════════════════════════════════════════════════════
   LAUNCHER — pill LEFT, FAB RIGHT
═══════════════════════════════════════════════════════════════ */
.launcher {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 9999;
  display: flex;
  align-items: center;
  flex-direction: row;   /* ← pill on left, button on right */
  gap: 10px;
  pointer-events: none;
}
.launcher > * { pointer-events: auto; }

.launcher-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-1);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 14px;
  border-radius: 100px;
  white-space: nowrap;
  box-shadow: 0 4px 20px rgba(0,0,0,.18);
  user-select: none;
  transition: opacity var(--ease), transform var(--ease);
}
.launcher-pill.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(6px);
}

.pulse-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
  animation: pulse 2.2s ease infinite;
}
@keyframes pulse {
  0%,100% { box-shadow: 0 0 0 0   rgba(34,197,94,.55); }
  50%      { box-shadow: 0 0 0 6px rgba(34,197,94,0);   }
}

.launcher-btn {
  width: 56px; height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 28px var(--accent-glow), 0 2px 6px rgba(0,0,0,.3);
  position: relative;
  transition: transform var(--ease), box-shadow var(--ease);
}
.launcher-btn:hover  { transform: scale(1.08); box-shadow: 0 10px 36px var(--accent-glow); }
.launcher-btn:active { transform: scale(.94); }

.launcher-btn svg {
  width: 22px; height: 22px;
  position: absolute;
  transition: opacity var(--ease), transform var(--ease);
}
.icon-chat  { opacity: 1; transform: scale(1)   rotate(0deg); }
.icon-close { opacity: 0; transform: scale(.55) rotate(-45deg); }
.launcher-btn.is-open .icon-chat  { opacity: 0; transform: scale(.55) rotate(45deg); }
.launcher-btn.is-open .icon-close { opacity: 1; transform: scale(1)   rotate(0deg); }

/* ═══════════════════════════════════════════════════════════════
   PANEL
   Key fix: anchor from BOTTOM, height uses dvh minus bottom offset
   minus a top clearance so it never goes behind the browser toolbar.
═══════════════════════════════════════════════════════════════ */
.panel {
  position: fixed;
  /* sits 12px above the launcher (56px tall) + 28px gap from edge */
  bottom: calc(56px + 28px + 12px);   /* = 96px */
  right: 28px;
  z-index: 9998;

  width: 370px;

  /*
   * Max height = full dynamic viewport height
   *   minus the space below the panel (96px)
   *   minus 16px top breathing room (keeps top edge visible)
   * This guarantees the panel NEVER touches or hides behind the browser toolbar.
   */
  max-height: calc(100dvh - 96px - 16px);
  height: 560px;  /* desired height — clamped by max-height above */

  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  /* hidden */
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(.96);
  pointer-events: none;

  transition:
    opacity       .22s cubic-bezier(.4,0,.2,1),
    transform     .22s cubic-bezier(.4,0,.2,1),
    visibility    0s   linear .22s,
    width         .26s cubic-bezier(.4,0,.2,1),
    height        .26s cubic-bezier(.4,0,.2,1),
    max-height    .26s cubic-bezier(.4,0,.2,1);
}

/* open */
.panel.is-open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
  pointer-events: all;
  transition:
    opacity       .22s cubic-bezier(.4,0,.2,1),
    transform     .22s cubic-bezier(.4,0,.2,1),
    visibility    0s   linear 0s,
    width         .26s cubic-bezier(.4,0,.2,1),
    height        .26s cubic-bezier(.4,0,.2,1),
    max-height    .26s cubic-bezier(.4,0,.2,1);
}

/* expanded — grow to ~50% viewport, stay centred */
.panel.is-open.expanded {
  width: clamp(340px, 50vw, 680px);
  height: calc(100dvh - 96px - 16px);   /* fill all available vertical space */
  bottom: calc(56px + 28px + 12px);     /* keep same bottom anchor */
  right: 28px;
  /* horizontally centre in the right half feels natural; or centre fully: */
  left: 50%;
  right: auto;
  transform: translateX(-50%);
}

/* ═══════════════════════════════════════════════════════════════
   HEADER
═══════════════════════════════════════════════════════════════ */
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--accent-dim); border: 1.5px solid rgba(99,102,241,.35);
  color: var(--accent); display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.avatar svg { width: 18px; height: 18px; }
.brand-text { display: flex; flex-direction: column; gap: 2px; }
.brand-name   { font-size: 14px; font-weight: 600; color: var(--text-1); line-height: 1; }
.brand-status { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-2); }
.status-dot   { width: 6px; height: 6px; border-radius: 50%; background: #22c55e; flex-shrink: 0; }
.header-actions { display: flex; gap: 2px; }
.action-btn {
  width: 32px; height: 32px; border: none; background: transparent;
  border-radius: var(--r-sm); color: var(--text-3); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--ease), color var(--ease);
}
.action-btn:hover { background: var(--surface-2); color: var(--text-1); }
.action-btn svg   { width: 15px; height: 15px; }

/* ═══════════════════════════════════════════════════════════════
   MESSAGES
═══════════════════════════════════════════════════════════════ */
.messages {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 4px;
  scroll-behavior: smooth; overscroll-behavior: contain;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 4px; }

.welcome-card {
  display: flex; flex-direction: column; align-items: center;
  text-align: center; padding: 18px 12px 14px; margin-bottom: 4px;
}
.welcome-avatar {
  width: 50px; height: 50px; border-radius: 50%;
  background: var(--accent-dim); border: 1.5px solid rgba(99,102,241,.35);
  color: var(--accent); display: flex; align-items: center; justify-content: center;
  margin-bottom: 11px;
}
.welcome-avatar svg { width: 24px; height: 24px; }
.welcome-title { font-size: 15px; font-weight: 600; color: var(--text-1); margin-bottom: 4px; }
.welcome-sub   { font-size: 12.5px; color: var(--text-2); line-height: 1.5; }

.msg {
  display: flex; flex-direction: column; max-width: 80%; gap: 3px;
  animation: fadeUp .18s ease both;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg.user { align-self: flex-end;   align-items: flex-end; }
.msg.bot  { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 9px 13px; border-radius: var(--r-md);
  font-size: 13.5px; line-height: 1.56; word-break: break-word;
}
.msg.user .bubble { background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot  .bubble { background: var(--bot-bubble); color: var(--text-1); border: 1px solid var(--bot-border); border-bottom-left-radius: 4px; }
.bubble a    { color: var(--accent); text-decoration: underline; }
.bubble code { background: rgba(99,102,241,.12); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.bubble pre  { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; margin-top: 6px; font-size: 12px; }
.meta { font-size: 10px; color: var(--text-3); padding: 0 2px; }

.typing-bubble {
  background: var(--bot-bubble); border: 1px solid var(--bot-border);
  border-radius: var(--r-md); border-bottom-left-radius: 4px;
  padding: 11px 15px; display: flex; gap: 5px; align-items: center;
}
.typing-bubble span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-3);
  animation: dot .9s infinite;
}
.typing-bubble span:nth-child(2) { animation-delay: .16s; }
.typing-bubble span:nth-child(3) { animation-delay: .32s; }
@keyframes dot {
  0%,60%,100% { transform: translateY(0); }
  30%          { transform: translateY(-5px); }
}

/* ═══════════════════════════════════════════════════════════════
   CHIPS
═══════════════════════════════════════════════════════════════ */
.chips {
  display: flex; gap: 6px; padding: 2px 14px 10px;
  overflow-x: auto; flex-shrink: 0; -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 12px; border-radius: 100px;
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text-2); font-family: var(--font); font-size: 12px;
  font-weight: 500; white-space: nowrap; cursor: pointer;
  transition: background var(--ease), color var(--ease), border-color var(--ease);
}
.chip:hover { background: var(--accent-dim); color: var(--text-1); border-color: rgba(99,102,241,.4); }

/* ═══════════════════════════════════════════════════════════════
   FOOTER
═══════════════════════════════════════════════════════════════ */
.panel-footer {
  border-top: 1px solid var(--border);
  padding: 10px 14px 12px; flex-shrink: 0;
  display: flex; flex-direction: column; gap: 7px;
  background: var(--surface);
}
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--r-md); padding: 8px 8px 8px 13px;
  transition: border-color var(--ease), box-shadow var(--ease);
}
.composer:focus-within {
  border-color: rgba(99,102,241,.5);
  box-shadow: 0 0 0 3px rgba(99,102,241,.10);
}
.composer textarea {
  flex: 1; background: transparent; border: none; outline: none; resize: none;
  font-family: var(--font); font-size: 13.5px; color: var(--text-1);
  line-height: 1.5; max-height: 108px; overflow-y: auto; padding: 0;
}
.composer textarea::placeholder { color: var(--text-3); }
.send-btn {
  width: 34px; height: 34px; border-radius: var(--r-sm); border: none;
  background: var(--accent); color: #fff; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--ease), transform var(--ease), opacity var(--ease);
}
.send-btn:hover:not(:disabled)  { background: var(--accent-hover); transform: scale(1.06); }
.send-btn:active:not(:disabled) { transform: scale(.93); }
.send-btn:disabled              { opacity: .32; cursor: not-allowed; }
.send-btn svg { width: 15px; height: 15px; }
.footer-meta {
  display: flex; justify-content: space-between;
  font-size: 10px; color: var(--text-3); padding: 0 2px;
}
.footer-hint { opacity: .65; }

/* ═══════════════════════════════════════════════════════════════
   TABLET ≤ 768px
═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .panel { width: 320px; }
}

/* ═══════════════════════════════════════════════════════════════
   MOBILE ≤ 540px — bottom sheet
═══════════════════════════════════════════════════════════════ */
@media (max-width: 540px) {
  .launcher        { bottom: 16px; right: 16px; }
  .launcher-pill   { display: none; }

  .panel {
    left: 0 !important; right: 0 !important;
    bottom: 0 !important; top: auto !important;
    width: 100% !important;
    /* leave 60px gap at top so status bar / browser toolbar stays clear */
    height: calc(100dvh - 60px) !important;
    max-height: calc(100dvh - 60px) !important;
    border-radius: 20px 20px 0 0 !important;
    transform: translateY(100%) !important;
  }
  .panel.is-open {
    transform: translateY(0) !important;
    opacity: 1 !important;
  }
  .panel.is-open.expanded {
    height: calc(100dvh - 60px) !important;
    transform: translateY(0) !important;
    left: 0 !important; right: 0 !important; top: auto !important;
  }
}

/* ═══════════════════════════════════════════════════════════════
   REDUCED MOTION
═══════════════════════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}