/* ==========================================================================
   BAF Assistant - Floating Chat Widget (fixed launcher, mobile sheet panel)
   ========================================================================== */

/* -------------------------- Design Tokens -------------------------- */
:root {
  --bafw-brand: #006a4e;
  --bafw-accent: #f42a41;

  --bafw-bg: #ffffff;
  --bafw-panel-bg: #f4f7f9;
  --bafw-header-bg: #ffffff;

  --bafw-text: #212529;
  --bafw-text-light: #5f6368;
  --bafw-border: #dee2e6;

  --bafw-bubble-bot: #ffffff;
  --bafw-bubble-user: #e1f2ff;

  --bafw-shadow: 0 5px 20px rgba(0,0,0,0.15);
  --bafw-radius: 12px;
  --bafw-font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --bafw-safe-bottom: env(safe-area-inset-bottom, 0px);
  --bafw-safe-right:  env(safe-area-inset-right, 0px);
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  :root {
    --bafw-bg: #202124;
    --bafw-panel-bg: #2a2b2e;
    --bafw-header-bg: #343538;
    --bafw-text: #e8eaed;
    --bafw-text-light: #9aa0a6;
    --bafw-border: #44474a;
    --bafw-bubble-bot: #3c4043;
    --bafw-bubble-user: #004a77;
    --bafw-shadow: 0 5px 25px rgba(0,0,0,0.35);
  }
}

/* -------------------------- Root container -------------------------- */
.bafw-root {
  position: fixed;                       /* pin the whole widget */
  bottom: calc(20px + var(--bafw-safe-bottom));
  right:  calc(20px + var(--bafw-safe-right));
  z-index: 2147483000;                   /* very high */
  font-family: var(--bafw-font);
  font-size: 16px;
  line-height: 1.5;
  pointer-events: none;                    /* children decide */
}

/* -------------------------- Launcher (always fixed BR) --------------- */
html .bafw-launcher {
  position: fixed !important;
  bottom: calc(20px + var(--bafw-safe-bottom)) !important;
  right:  calc(20px + var(--bafw-safe-right)) !important;
  top: auto !important;
  left: auto !important;
  /* MODIFIED: Swapped transform for a 3D flip animation */
  z-index: 2147483647 !important;

  display: inline-flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;

  background-color: var(--bafw-brand);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  padding: 0;
  box-shadow: var(--bafw-shadow);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden; /* Keep pseudo-elements contained */

  /* --- ADDED: 3D Flip & Pulse Animation --- */
  perspective: 1000px; /* Enables 3D space */
  transform-style: preserve-3d; /* Children respect 3D space */
  transform: rotateY(0deg); /* Start facing forward */
  /* Define transitions for the flip and color change */
  transition: transform 0.4s cubic-bezier(.7,0,.3,1), background-color 0.4s ease;
  
  /* Add the inviting pulse animation */
  animation: bafw-pulse 2s infinite cubic-bezier(0.66, 0, 0, 1);
}

.bafw-launcher:hover { 
  /* MODIFIED: Keep transform for hover, but don't fight the 3D flip */
  transform: scale(1.05) rotateY(var(--bafw-flip, 0deg)); 
  box-shadow: 0 8px 25px rgba(0,0,0,0.2); 
}
.bafw-launcher:active { 
  transform: scale(0.98) rotateY(var(--bafw-flip, 0deg)); 
}
.bafw-launcher:focus-visible {
  outline: 2px solid #fff; outline-offset: 3px;
  box-shadow: 0 0 0 3px rgba(0,106,78,.45), var(--bafw-shadow);
}

/* --- MODIFIED: Launcher Icon (Front Face) --- */
.bafw-launcher img { 
  width: 32px; height: 32px; display: block;
  transition: none; /* Transition is on the parent */
  /* ADDED: 3D properties */
  backface-visibility: hidden; /* Hide this when it's flipped */
  transform: rotateY(0deg); /* Explicitly be the front face */
}

.bafw-launcher-text { display: none; }


/* --- ADDED: CSS-based 'X' icon (Back Face) --- */
/* --- MODIFIED: CSS-based 'X' icon (Back Face) now replaced with 'Close' text --- */

/* Remove or comment out the 'X' icon styles: */
/*
.bafw-launcher::before,
.bafw-launcher::after {
  content: '';
  position: absolute;
  width: 24px;
  height: 3.5px;
  border-radius: 3px;
  background: #fff;
  top: 50%;
  left: 50%;
  backface-visibility: hidden;
  opacity: 1;
}
.bafw-launcher::before {
  transform: translate(-50%, -50%) rotateY(180deg) rotate(45deg);
}
.bafw-launcher::after {
  transform: translate(-50%, -50%) rotateY(180deg) rotate(-45deg);
}
*/

/* --- ADDED/MODIFIED: 'Close' text (Back Face) --- */
.bafw-launcher::before {
  content: 'Close'; /* 1. Add the word "Close" */
  position: absolute;
  color: #fff; /* Set the color to white (or whatever your background is) */
  font-family: sans-serif; /* Set a readable font */
  font-size: 16px; /* Adjust font size as needed */
  font-weight: bold; /* Make it stand out */
  text-align: center;
  white-space: nowrap; /* Keep the word on one line */
  
  /* Positioning and Movement */
  top: 50%; /* Start from the center */
  left: 50%;
  transform: translate(-50%, -75%) rotateY(180deg); /* 2. Move it up and rotate for the back face */
  /* -50% centers horizontally. -75% moves it up (original center is -50%; -75% is 25% higher) */
  
  /* 3D properties for the back face */
  backface-visibility: hidden; /* Hide this when it's facing away */
  opacity: 1; /* Always visible on its own face */
}

/* Ensure the ::after element is not interfering. 
   If your system doesn't use ::after for anything else, you can ignore it, 
   or explicitly set its content to none if it causes issues. */
.bafw-launcher::after {
  content: none !important;
}


/* --- MODIFIED: Animate launcher to its 'open' state --- */
.bafw-panel.is-open ~ .bafw-launcher { 
  background-color: var(--bafw-accent); /* Change to red when open */
  
  /* --- This is the 3D FLIP animation --- */
  transform: rotateY(180deg);
  /* Store the flip state in a CSS variable for hover to use */
  --bafw-flip: 180deg; 
  
  /* --- Turn off the pulse animation when open --- */
  animation: none;
}


/* -------------------------- Panel (desktop/tablet) ------------------- */
.bafw-panel {
  position: fixed;
  bottom: calc(90px + var(--bafw-safe-bottom));     /* above launcher */
  right:  calc(20px + var(--bafw-safe-right));
  pointer-events: auto;

  width: 360px;
  max-width: calc(100vw - 30px);
  max-height: calc(100vh - 100px);
  background: var(--bafw-panel-bg);
  border-radius: var(--bafw-radius);
  box-shadow: var(--bafw-shadow);
  display: flex;
  flex-direction: column;

  opacity: 0;
  visibility: hidden;
  transform: translateY(20px) scale(0.96);
  transform-origin: bottom right;
  transition: opacity .22s ease, transform .22s ease, visibility .22s;
}
.bafw-panel.is-open { opacity: 1; visibility: visible; transform: translateY(0) scale(1); }

@media (prefers-reduced-motion: reduce) {
  .bafw-panel, .bafw-launcher { transition: opacity .2s, visibility .2s; animation: none; }
}

/* -------------------------- Panel header ----------------------------- */
.bafw-header {
  display: flex; justify-content: space-between; align-items: center; gap: .75rem;
  padding: 12px 16px; background: var(--bafw-header-bg);
  border-bottom: 1px solid var(--bafw-border);
  border-top-left-radius: var(--bafw-radius);
  border-top-right-radius: var(--bafw-radius);
}
.bafw-brand { font-weight: 600; color: var(--bafw-text); }
.bafw-sub { font-size: .8rem; color: var(--bafw-text-light); }
.bafw-close {
  background: none; border: none; font-size: 24px; line-height: 1;
  color: var(--bafw-text-light); cursor: pointer; padding: 0 4px; border-radius: 6px;
  /* ADDED: Transition for hover animation */
  transition: transform .2s ease, color .2s ease, background-color .2s ease;
}
.bafw-close:hover { 
  color: var(--bafw-accent); background: rgba(0,0,0,.04); 
  /* ADDED: Stylish rotate on hover */
  transform: rotate(90deg);
}
.bafw-close:focus-visible { outline: 2px solid var(--bafw-accent); outline-offset: 2px; }
.bafw-header-actions { display: flex; gap: .5rem; align-items: center; margin-left: auto; }
.bafw-iconbtn { border: 0; background: transparent; cursor: pointer; width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center; border-radius: 50%;
  color: var(--bafw-text-light); }
.bafw-iconbtn:hover { background: rgba(0,0,0,.06); }
.bafw-iconbtn[disabled] { opacity: .5; cursor: not-allowed; }
.bafw-iconbtn svg { width: 18px; height: 18px; }

/* -------------------------- Messages ------------------------------- */
.bafw-body {
  flex-grow: 1; overflow-y: auto; padding: 16px;
  color: var(--bafw-text); background: var(--bafw-panel-bg); scroll-behavior: smooth;
}
.bafw-msg { display: flex; margin-bottom: 12px; }
.bafw-msg-user { justify-content: flex-end; }
.bafw-bubble { max-width: 85%; padding: 10px 14px; border-radius: 18px; word-wrap: break-word; word-break: break-word; }
.bafw-msg-bot .bafw-bubble { background: var(--bafw-bubble-bot); color: var(--bafw-text); border-top-left-radius: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.bafw-msg-user .bafw-bubble { background: var(--bafw-bubble-user); color: var(--bafw-text); border-top-right-radius: 6px; }
.bafw-body a { color: var(--bafw-brand); text-decoration: underline; }
.bafw-system, .bafw-typing { font-size: .8rem; text-align: center; color: var(--bafw-text-light); margin-bottom: 12px; }
.bafw-typing::after { content: '…'; animation: bafw-dots 1.4s infinite; }
@keyframes bafw-dots { 0%{content:'.'} 33%{content:'..'} 66%{content:'...'} }

/* -------------------------- Form ---------------------------------- */
.bafw-form { padding: 12px 16px; background: var(--bafw-header-bg); border-top: 1px solid var(--bafw-border);
  border-bottom-left-radius: var(--bafw-radius); border-bottom-right-radius: var(--bafw-radius); }
.bafw-input-wrap { display: flex; align-items: center; gap: 8px; }
.bafw-input { flex-grow: 1; border: 1px solid var(--bafw-border); background-color: var(--bafw-bg); color: var(--bafw-text);
  border-radius: 20px; padding: 8px 14px; font-size: 1rem; outline: none; }
.bafw-input::placeholder { color: var(--bafw-text-light); }
.bafw-input:focus { border-color: var(--bafw-brand); box-shadow: 0 0 0 3px rgba(0,106,78,.20); }
.bafw-send { background: var(--bafw-brand); color: #fff; border: none; border-radius: 50%; width: 38px; height: 38px;
  cursor: pointer; font-weight: 700; font-size: 1.1rem; flex-shrink: 0; display: inline-grid; place-items: center; }
.bafw-send::before { content: '➤'; }
.bafw-send:hover { filter: brightness(1.05); }
.bafw-send:active { transform: translateY(1px); }
.bafw-send:focus-visible { outline: 2px solid #fff; box-shadow: 0 0 0 3px rgba(0,106,78,.45); }
.bafw-options { padding-top: 8px; text-align: right; }
.bafw-checkbox-label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: .8rem; color: var(--bafw-text-light); }
.bafw-checkbox { accent-color: var(--bafw-brand); }
.bafw-mic { border: 0; background: transparent; cursor: pointer; width: 38px; height: 38px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; color: var(--bafw-text-light); }
.bafw-mic svg { width: 18px; height: 18px; }
.bafw-mic.is-listening { box-shadow: 0 0 0 3px rgba(255,0,0,.2); }

/* --- ADDED: Keyframes for the pulse animation --- */
@keyframes bafw-pulse {
  0% {
    box-shadow: var(--bafw-shadow), 0 0 0 0 rgba(0, 106, 78, 0.6);
  }
  70% {
    box-shadow: var(--bafw-shadow), 0 0 0 12px rgba(0, 106, 78, 0);
  }
  100% {
    box-shadow: var(--bafw-shadow), 0 0 0 0 rgba(0, 106, 78, 0);
  }
}


/* -------------------------- Mobile: full-screen sheet -------------- */
@media (max-width: 480px) {
  html .bafw-launcher {
    bottom: calc(15px + env(safe-area-inset-bottom, 0px)) !important;
    right:  calc(15px + env(safe-area-inset-right, 0px)) !important;
  }

  .bafw-panel {
    left: 0; right: 0; top: 0; bottom: 0;
    width: 100vw; max-width: 100vw;
    height: 100vh; max-height: 100vh;
    border-radius: 0;
    transform: translateY(100%);         /* slide up from bottom */
    box-shadow: none;
  }
  .bafw-panel.is-open { transform: translateY(0); }
  .bafw-body { padding: 14px; }
}

/* -------------------------- Print ---------------------------------- */
@media print {
  .bafw-root, .bafw-launcher, .bafw-panel { display: none !important; }
}