/* =============================================================================
   TechVaults AI Chat Widget  v1.6.0
   ALL selectors are scoped under #tva-root or #tva-btn / #tva-panel.
   Nothing in this file touches elements outside the widget DOM.
   The widget is appended to <body> as a sibling — never wrapping site content.
   ============================================================================= */

/* ── Design tokens (scoped to widget root) ───────────────────────────────── */
#tva-root {
  --tva-red:        #bc0004;
  --tva-red-dk:     #a00003;
  --tva-red-lt:     #ffdad6;
  --tva-red-xlt:    #fff0ef;
  --tva-on-red:     #ffffff;
  --tva-surface:    #f7f2f2;
  --tva-surface-0:  #ffffff;
  --tva-surface-1:  #eededd;
  --tva-ink:        #201a1a;
  --tva-ink-m:      #6b4f4f;
  --tva-border:     #ddc8c8;
  --tva-wa:         #25d366;
  --tva-wa-dk:      #1da851;
  --tva-r4:  4px;
  --tva-r8:  8px;
  --tva-r12: 12px;
  --tva-r16: 16px;
  --tva-r24: 24px;
  --tva-r99: 9999px;
  --tva-sh-fab: 0 3px 8px rgba(0,0,0,.25), 0 6px 16px rgba(0,0,0,.15);
  --tva-sh-pan: 0 12px 32px rgba(0,0,0,.18), 0 4px 8px rgba(0,0,0,.12);
  --tva-ease:   cubic-bezier(0.2,0,0,1);
  --tva-font:   'Google Sans', Roboto, Arial, sans-serif;
  --tva-z:      2147483647;

  /* Widget root itself is just a positioning anchor — invisible */
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: var(--tva-z);
  pointer-events: none; /* children opt-in via pointer-events: auto */
}

/* ── Scoped reset — ONLY elements inside #tva-root ───────────────────────── */
/* Using a single combined selector so this NEVER touches the host page.    */
/* We intentionally do NOT use the bare * selector or any global reset.     */
#tva-root #tva-btn,
#tva-root #tva-panel,
#tva-root #tva-btn *,
#tva-root #tva-panel * {
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  /* Font scoped here — NOT using !important to avoid leaking */
  font-family: var(--tva-font);
  line-height: normal;
}

/* Scoped margin & padding resets for elements that have default browser spacing */
#tva-root p,
#tva-root ul,
#tva-root ol,
#tva-root li,
#tva-root h1,
#tva-root h2,
#tva-root h3,
#tva-root h4,
#tva-root h5,
#tva-root h6,
#tva-root figure,
#tva-root blockquote,
#tva-root dl,
#tva-root dd {
  margin: 0;
  padding: 0;
}

#tva-root button,
#tva-root input,
#tva-root textarea,
#tva-root select {
  margin: 0;
  padding: 0;
  border: none;
  background: none;
}

/* Restore pointer events for interactive widget children */
#tva-root #tva-btn,
#tva-root #tva-panel {
  pointer-events: auto;
}

/* ── FAB LAUNCHER ────────────────────────────────────────────────────────── */
#tva-root #tva-btn {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: var(--tva-z);
  width: 58px;
  height: 58px;
  border-radius: 16px;
  background: var(--tva-red);
  color: var(--tva-on-red);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--tva-sh-fab);
  transition: transform .2s var(--tva-ease), box-shadow .2s;
  outline: none;
  /* Ensure no host-page styles leak in */
  text-decoration: none;
  font-size: 0; /* prevent text flash before SVG loads */
}
#tva-root #tva-btn:hover  { transform: scale(1.07); }
#tva-root #tva-btn:focus-visible {
  outline: 3px solid var(--tva-red);
  outline-offset: 3px;
}

/* Icon swap: open (bot SVG) → close (× SVG) */
#tva-root #tva-btn .tva-ico-open,
#tva-root #tva-btn .tva-ico-close {
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity .2s var(--tva-ease), transform .2s var(--tva-ease);
}
#tva-root #tva-btn .tva-ico-close {
  position: absolute;
  opacity: 0;
  transform: rotate(-90deg) scale(.75);
}
#tva-root #tva-btn .tva-ico-open svg,
#tva-root #tva-btn .tva-ico-close svg { display: block; }
#tva-root #tva-btn .tva-ico-close svg { stroke: #fff; }

#tva-root.tva-open #tva-btn .tva-ico-open  { opacity: 0; transform: rotate(90deg) scale(.75); }
#tva-root.tva-open #tva-btn .tva-ico-close { opacity: 1; transform: none; }

/* Badge */
#tva-root #tva-btn .tva-badge {
  position: absolute;
  top: -5px; right: -5px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #e53935;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  font-family: var(--tva-font);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
  pointer-events: none;
  line-height: 1;
}

/* ── CHAT PANEL ──────────────────────────────────────────────────────────── */
#tva-root #tva-panel {
  position: fixed;
  bottom: 94px;
  right: 24px;
  z-index: var(--tva-z);
  width: 370px;
  max-width: calc(100vw - 32px);
  height: 600px;
  max-height: calc(100vh - 112px);
  background: var(--tva-surface-0);
  border-radius: var(--tva-r24);
  box-shadow: var(--tva-sh-pan);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Hidden by default */
  opacity: 0;
  pointer-events: none;
  transform: translateY(20px) scale(.95);
  transform-origin: bottom right;
  transition: opacity .25s var(--tva-ease), transform .25s var(--tva-ease);
}
#tva-root.tva-open #tva-panel {
  opacity: 1;
  pointer-events: auto;
  transform: none;
}

/* ── HEADER ──────────────────────────────────────────────────────────────── */
#tva-root #tva-hdr {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: var(--tva-red);
  flex-shrink: 0;
}

#tva-root .tva-hdr-av {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(255,255,255,.18);
  border: 2px solid rgba(255,255,255,.35);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}
#tva-root .tva-hdr-av svg { width: 34px; height: 34px; display: block; }

#tva-root .tva-hdr-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
#tva-root .tva-hdr-name {
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#tva-root .tva-hdr-status {
  font-size: 12px;
  color: rgba(255,255,255,.82);
  display: flex;
  align-items: center;
  gap: 5px;
  line-height: 1;
}
#tva-root .tva-hdr-status .tva-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #69f0ae;
  flex-shrink: 0;
  display: inline-block;
}

#tva-root .tva-close {
  background: transparent;
  border: none;
  cursor: pointer;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}
#tva-root .tva-close:hover { background: rgba(255,255,255,.18); }
#tva-root .tva-close:focus-visible { outline: 2px solid rgba(255,255,255,.7); }
#tva-root .tva-close svg { stroke: rgba(255,255,255,.9); display: block; }

/* ── MESSAGES AREA ───────────────────────────────────────────────────────── */
#tva-root #tva-msgs {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
  background: var(--tva-surface);
}
#tva-root #tva-msgs::-webkit-scrollbar       { width: 4px; }
#tva-root #tva-msgs::-webkit-scrollbar-track { background: transparent; }
#tva-root #tva-msgs::-webkit-scrollbar-thumb { background: var(--tva-border); border-radius: 4px; }

/* Message rows — each row is a full-width flex container */
#tva-root .tva-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  /* Vertical breathing room between separate messages */
  margin-bottom: 6px;
}
/* Tighten spacing between consecutive messages from the same sender */
#tva-root .tva-row + .tva-row { margin-bottom: 2px; }
/* Extra gap when sender switches */
#tva-root .tva-row-bot + .tva-row-usr,
#tva-root .tva-row-usr + .tva-row-bot { margin-top: 8px; }

#tva-root .tva-row-bot { flex-direction: row;         justify-content: flex-start; }
#tva-root .tva-row-usr { flex-direction: row-reverse; justify-content: flex-start; }

/* Avatar */
#tva-root .tva-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--tva-red-lt);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  align-self: flex-end;
  overflow: hidden;
  /* Keep avatar anchored so it doesn't collapse on short messages */
  min-width: 32px;
}
#tva-root .tva-avatar svg { width: 24px; height: 24px; display: block; }
#tva-root .tva-av-hide    { visibility: hidden; }

/* Bubble wrapper — constrains max width for both sides */
#tva-root .tva-bub-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  /* avatar(32) + gap(8) + side-padding(16) = 56px reserved */
  max-width: calc(100% - 56px);
}

/* Bubbles — core spacing fix */
#tva-root .tva-bub {
  padding: 11px 14px;
  border-radius: var(--tva-r16);
  font-size: 14px;
  line-height: 1.65;
  word-break: break-word;
  overflow-wrap: break-word;
  /* Use block so the bubble takes the wrapper's full constrained width
     when text is long, and shrinks to content when text is short */
  display: block;
  /* Hard cap so no single bubble fills the whole panel */
  max-width: 100%;
  font-family: var(--tva-font);
}

#tva-root .tva-bub-bot {
  background: var(--tva-surface-0);
  color: var(--tva-ink);
  border: 1px solid var(--tva-border);
  border-bottom-left-radius: var(--tva-r4);
}
#tva-root .tva-bub-usr {
  background: var(--tva-red);
  color: #fff;
  border-bottom-right-radius: var(--tva-r4);
  white-space: pre-wrap;
}

/* Timestamp — sits just below the bubble with a small top gap */
#tva-root .tva-time {
  font-size: 11px;
  color: var(--tva-ink-m);
  opacity: .55;
  line-height: 1;
  /* 2px top margin separates it cleanly from the bubble bottom */
  margin-top: 2px;
  padding: 0 2px;
  font-family: var(--tva-font);
}
#tva-root .tva-row-usr .tva-time { text-align: right; align-self: flex-end; }
#tva-root .tva-row-bot .tva-time { text-align: left;  align-self: flex-start; }

/* ── MARKDOWN RENDERING ──────────────────────────────────────────────────── */
#tva-root .tva-md { white-space: normal; }

#tva-root .tva-md .tva-md-p {
  margin: 0 0 7px 0;
  line-height: 1.6;
}
#tva-root .tva-md .tva-md-p:last-child { margin-bottom: 0; }

#tva-root .tva-md .tva-md-h {
  font-weight: 600;
  margin: 6px 0 3px;
  line-height: 1.3;
  color: var(--tva-ink);
}

#tva-root .tva-md .tva-md-ul {
  margin: 3px 0 7px;
  padding-left: 18px;
  list-style: disc;
}
#tva-root .tva-md .tva-md-ol {
  margin: 3px 0 7px;
  padding-left: 20px;
  list-style: decimal;
}
#tva-root .tva-md .tva-md-ul:last-child,
#tva-root .tva-md .tva-md-ol:last-child { margin-bottom: 0; }

#tva-root .tva-md li {
  margin-bottom: 3px;
  line-height: 1.55;
}
#tva-root .tva-md li:last-child { margin-bottom: 0; }

#tva-root .tva-md .tva-md-code {
  background: rgba(188,0,4,.07);
  border: 1px solid rgba(188,0,4,.15);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12.5px;
  font-family: 'Courier New', Courier, monospace;
}
#tva-root .tva-md .tva-pre {
  background: #1e1e2e;
  color: #cdd6f4;
  border-radius: var(--tva-r8);
  padding: 10px 12px;
  margin: 5px 0;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.5;
}
#tva-root .tva-md .tva-pre code {
  background: none;
  border: none;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
}

#tva-root .tva-md .tva-md-a {
  color: var(--tva-red);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}
#tva-root .tva-md .tva-md-a:hover { opacity: .8; }

#tva-root .tva-md strong { font-weight: 600; }
#tva-root .tva-md em     { font-style: italic; }
#tva-root .tva-md br     { display: block; margin: 2px 0; }

/* ── TYPING INDICATOR ────────────────────────────────────────────────────── */
#tva-root .tva-typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 12px 14px;
  background: var(--tva-surface-0);
  border: 1px solid var(--tva-border);
  border-radius: var(--tva-r16);
  border-bottom-left-radius: var(--tva-r4);
}
#tva-root .tva-typing .tva-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--tva-ink-m);
  display: inline-block;
  animation: tva-bounce 1.2s infinite;
}
#tva-root .tva-typing .tva-dot:nth-child(2) { animation-delay: .2s; }
#tva-root .tva-typing .tva-dot:nth-child(3) { animation-delay: .4s; }

@keyframes tva-bounce {
  0%, 80%, 100% { transform: translateY(0);   opacity: .4; }
  40%            { transform: translateY(-5px); opacity: 1; }
}

/* ── QUICK REPLIES ───────────────────────────────────────────────────────── */
#tva-root #tva-qr {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 14px 6px;
  background: var(--tva-surface);
  border-top: 1px solid var(--tva-border);
  flex-shrink: 0;
}
#tva-root .tva-qr-btn {
  background: var(--tva-surface-0);
  border: 1.5px solid var(--tva-border);
  color: var(--tva-red);
  border-radius: var(--tva-r99);
  padding: 6px 13px;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--tva-font);
  cursor: pointer;
  white-space: nowrap;
  line-height: 1.4;
  transition: background .15s, border-color .15s;
}
#tva-root .tva-qr-btn:hover {
  background: var(--tva-red-lt);
  border-color: var(--tva-red);
}
#tva-root .tva-qr-btn:focus-visible {
  outline: 2px solid var(--tva-red);
  outline-offset: 2px;
}

/* ── INPUT FORM ──────────────────────────────────────────────────────────── */
#tva-root #tva-form {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--tva-surface-0);
  border-top: 1px solid var(--tva-border);
  flex-shrink: 0;
}

#tva-root #tva-inp {
  flex: 1;
  min-width: 0;
  height: 42px;
  border: 1.5px solid var(--tva-border);
  border-radius: var(--tva-r99);
  padding: 10px 16px;
  font-size: 14px;
  font-family: var(--tva-font);
  color: var(--tva-ink);
  background: var(--tva-surface);
  outline: none;
  line-height: 1.4;
  transition: border-color .18s, background .18s;
  /* Override any theme input styles */
  box-shadow: none;
  -webkit-appearance: none;
  appearance: none;
}
#tva-root #tva-inp::placeholder { color: var(--tva-ink-m); opacity: .6; }
#tva-root #tva-inp:focus        { border-color: var(--tva-red); background: #fff; }
#tva-root #tva-inp:disabled     { opacity: .5; cursor: not-allowed; }

#tva-root .tva-send {
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  border-radius: var(--tva-r12);
  background: var(--tva-red);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, transform .15s;
  box-shadow: none;
  -webkit-appearance: none;
  appearance: none;
}
#tva-root .tva-send svg    { stroke: #fff; display: block; }
#tva-root .tva-send:hover  { background: var(--tva-red-dk); transform: scale(1.06); }
#tva-root .tva-send:focus-visible { outline: 2px solid var(--tva-red); outline-offset: 3px; }
#tva-root .tva-send:disabled { opacity: .4; cursor: not-allowed; transform: none; }

/* ── FOOTER / WHATSAPP ───────────────────────────────────────────────────── */
#tva-root #tva-foot {
  background: var(--tva-surface-0);
  padding: 8px 12px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid var(--tva-border);
  flex-shrink: 0;
}
#tva-root .tva-wa {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 24px;
  background: var(--tva-wa);
  color: #fff;
  border-radius: var(--tva-r99);
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 600;
  font-family: var(--tva-font);
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(37,211,102,.3);
  transition: background .15s;
}
#tva-root .tva-wa:hover { background: var(--tva-wa-dk); }
#tva-root .tva-wa svg   { flex-shrink: 0; display: block; }

/* ── LEAD CAPTURE CARD ───────────────────────────────────────────────────── */
#tva-root .tva-lead-card {
  background: #fff;
  border: 1.5px solid var(--tva-border);
  border-radius: var(--tva-r16);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 280px;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}
#tva-root .tva-lead-card p {
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--tva-ink);
  margin: 0;
  font-family: var(--tva-font);
}
#tva-root .tva-lead-field-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
#tva-root .tva-lead-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--tva-ink-m);
  text-transform: uppercase;
  letter-spacing: .4px;
  font-family: var(--tva-font);
}
#tva-root .tva-lead-field {
  border: 1.5px solid var(--tva-border);
  border-radius: var(--tva-r8);
  padding: 9px 12px;
  font-size: 13.5px;
  font-family: var(--tva-font);
  color: var(--tva-ink);
  background: var(--tva-surface);
  outline: none;
  width: 100%;
  height: 40px;
  box-shadow: none;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color .18s, background .18s;
}
#tva-root .tva-lead-field::placeholder { color: var(--tva-ink-m); opacity: .55; }
#tva-root .tva-lead-field:focus        { border-color: var(--tva-red); background: #fff; }
#tva-root .tva-lead-field.tva-err      { border-color: #e53935; }

#tva-root .tva-lead-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background: var(--tva-red);
  color: #fff;
  border: none;
  border-radius: var(--tva-r8);
  padding: 0 16px;
  font-size: 13.5px;
  font-weight: 600;
  font-family: var(--tva-font);
  cursor: pointer;
  width: 100%;
  height: 42px;
  box-shadow: none;
  -webkit-appearance: none;
  appearance: none;
  transition: background .15s;
}
#tva-root .tva-lead-btn svg    { stroke: #fff; display: block; }
#tva-root .tva-lead-btn:hover  { background: var(--tva-red-dk); }
#tva-root .tva-lead-btn:disabled { opacity: .5; cursor: not-allowed; }

/* Screen-reader only */
#tva-root .tva-sr {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ── MOBILE ──────────────────────────────────────────────────────────────── */
@media (max-width: 440px) {
  #tva-root #tva-panel {
    bottom: 0; right: 0;
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border-top-left-radius: var(--tva-r24);
    border-top-right-radius: var(--tva-r24);
  }
  #tva-root #tva-btn {
    bottom: 16px;
    right: 16px;
  }
}
