/* =============================================================================
   chatbot.css — global "Project advisor" widget
   -----------------------------------------------------------------------------
   States:
     [data-state="closed"]      Launcher only (default on first load)
     [data-state="open"]        Panel visible, conversation thread
     [data-state="capture"]     Lead-capture form replaces composer
     [data-state="error"]       Reachability problem; offers retry/email
     [data-state="success"]     Confirmation after lead submit
   The widget is fixed to the viewport and respects safe-area on iOS.
   Tone: helpful project advisor, not "live chat agent". Never auto-opens.
   ========================================================================== */

.chatbot {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 90;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  /* Sit below the nav (z=100) so an open menu always wins. */
  /* When the cookie banner is mounted (it sits at the bottom edge),
   * lift the chatbot above it so the launcher and the banner don't
   * stack on the same pixels at narrow widths. The selector targets
   * <body> with a sibling chatbot — JS adds `data-cookie="open"` on
   * <body> while the banner is visible. */
  transition: bottom .25s ease;
}
body[data-cookie="open"] .chatbot {
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 96px);
}

/* -- Launcher (closed state) ------------------------------------------------ */
.chatbot__launcher {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--fg-0);
  color: var(--bg-0);
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-weight: 600;
  border: 1px solid var(--fg-0);
  box-shadow: 0 16px 40px -10px var(--accent-glow);
  min-height: 44px;
  transition: transform .25s cubic-bezier(.2,.7,.2,1), box-shadow .25s;
}
.chatbot__launcher:hover { transform: translateY(-2px); box-shadow: 0 22px 60px -10px var(--accent-glow); }
.chatbot__launcher:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.chatbot__launcher svg { width: 16px; height: 16px; }
.chatbot__launcher__pulse {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 var(--accent-glow);
  animation: bot-pulse 1.8s ease-out infinite;
}
@keyframes bot-pulse {
  0%   { box-shadow: 0 0 0 0   color-mix(in oklab, var(--accent) 50%, transparent); }
  70%  { box-shadow: 0 0 0 10px transparent; }
  100% { box-shadow: 0 0 0 0   transparent; }
}
@media (prefers-reduced-motion: reduce) {
  .chatbot__launcher__pulse { animation: none; }
  .chatbot__launcher { transition: none; }
}

.chatbot[data-state="open"]    .chatbot__launcher,
.chatbot[data-state="capture"] .chatbot__launcher,
.chatbot[data-state="error"]   .chatbot__launcher,
.chatbot[data-state="success"] .chatbot__launcher { display: none; }

/* -- Panel ------------------------------------------------------------------ */
.chatbot__panel {
  width: min(380px, calc(100vw - 32px));
  /* iOS Safari < 15.4 doesn't support dvh; the value silently fails
   * and the panel collapses to 0px. Set a vh fallback first, then
   * override with dvh in supporting browsers. */
  height: min(560px, calc(100vh - 32px));
  display: none;
  flex-direction: column;
  background: var(--bg-1);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.65);
  overflow: hidden;
}
.chatbot:not([data-state="closed"]) .chatbot__panel { display: flex; }

/* Header */
.chatbot__head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in oklab, var(--bg-1) 92%, transparent);
}
.chatbot__head__l { display: flex; align-items: center; gap: 10px; min-width: 0; }

/* dvh upgrade — modern browsers get the dynamic viewport that excludes
 * iOS Safari's collapsing URL bar. Older WebKit ignores the rule and
 * keeps the vh fallback above. */
@supports (height: 100dvh) {
  .chatbot__panel { height: min(560px, calc(100dvh - 32px)); }
}
.chatbot__head__avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--grad);
  display: grid; place-items: center;
  color: #fff;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.chatbot__head__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--fg-0);
}
.chatbot__head__sub {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--fg-2);
  margin-top: 2px;
  display: inline-flex; align-items: center; gap: 6px;
}
.chatbot__head__sub::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #34d399;
}
.chatbot__head__close {
  width: 36px; height: 36px;
  border-radius: 999px;
  display: grid; place-items: center;
  color: var(--fg-1);
  border: 1px solid var(--line);
  background: transparent;
  transition: color .2s, background .2s, border-color .2s;
}
.chatbot__head__close:hover { color: var(--fg-0); background: var(--line); border-color: var(--line-strong); }
.chatbot__head__close svg { width: 14px; height: 14px; }

/* Body / log */
.chatbot__body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 14px;
  line-height: 1.5;
  scroll-behavior: smooth;
}
@media (prefers-reduced-motion: reduce) {
  .chatbot__body { scroll-behavior: auto; }
}

/* Messages */
.chatbot__msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: color-mix(in oklab, var(--bg-2) 60%, transparent);
  color: var(--fg-0);
}
.chatbot__msg--bot  { border-top-left-radius: 4px; }
.chatbot__msg--user {
  align-self: flex-end;
  background: var(--fg-0);
  color: var(--bg-0);
  border-color: var(--fg-0);
  border-top-right-radius: 4px;
}
.chatbot__msg--meta {
  align-self: center;
  background: transparent;
  border: 0;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--fg-2);
  text-transform: uppercase;
}

/* Typing indicator (3 dots) */
.chatbot__typing {
  display: inline-flex; gap: 4px;
  padding: 12px 14px;
  align-self: flex-start;
  background: color-mix(in oklab, var(--bg-2) 60%, transparent);
  border: 1px solid var(--line);
  border-radius: 14px;
  border-top-left-radius: 4px;
}
.chatbot__typing span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--fg-2);
  animation: bot-typing 1.2s ease-in-out infinite;
}
.chatbot__typing span:nth-child(2) { animation-delay: 0.15s; }
.chatbot__typing span:nth-child(3) { animation-delay: 0.30s; }
@keyframes bot-typing {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30%           { opacity: 1;   transform: translateY(-3px); }
}
@media (prefers-reduced-motion: reduce) {
  .chatbot__typing span { animation: none; opacity: 0.6; }
}

/* Quick replies */
.chatbot__quick {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 16px 12px;
}
.chatbot__quick button {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  color: var(--fg-1);
  background: color-mix(in oklab, var(--bg-1) 70%, transparent);
  min-height: 32px;
  transition: color .2s, border-color .2s, background .2s;
}
.chatbot__quick button:hover { color: var(--fg-0); border-color: var(--fg-3); background: var(--line); }

/* Composer */
.chatbot__composer {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 12px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: color-mix(in oklab, var(--bg-1) 92%, transparent);
}
.chatbot__composer textarea {
  flex: 1 1 auto;
  resize: none;
  min-height: 44px;
  max-height: 140px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: var(--bg-0);
  color: var(--fg-0);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
  transition: border-color .2s, box-shadow .2s;
}
.chatbot__composer textarea::placeholder { color: var(--fg-3); }
.chatbot__composer textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--accent) 28%, transparent);
}
.chatbot__send {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--fg-0);
  color: var(--bg-0);
  display: grid; place-items: center;
  border: 1px solid var(--fg-0);
  transition: transform .2s, box-shadow .2s, opacity .2s;
}
.chatbot__send:hover { transform: translateY(-1px); box-shadow: 0 10px 30px var(--accent-glow); }
.chatbot__send:disabled { opacity: 0.4; cursor: not-allowed; }
.chatbot__send svg { width: 16px; height: 16px; }

/* Escape hatch button shown above the inline capture form — hands off to
 * the slide-in project popup. Styled as a small text link rather than a
 * button so it doesn't compete with the primary "Send" button below. */
.chatbot__capture__alt {
  align-self: flex-start;
  background: transparent;
  border: 0;
  padding: 4px 0;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 0.25em;
  cursor: pointer;
  transition: color .2s;
}
.chatbot__capture__alt:hover { color: var(--fg-0); }
.chatbot__capture__alt:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Lead-capture state — replaces composer with a tiny form */
.chatbot__capture {
  padding: 16px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: color-mix(in oklab, var(--bg-1) 92%, transparent);
}
.chatbot__capture h4 {
  font-size: 14px;
  color: var(--fg-0);
  letter-spacing: -0.01em;
}
.chatbot__capture .field { gap: 6px; }
.chatbot__capture .field__control { min-height: 40px; padding: 8px 12px; font-size: 14px; }
.chatbot__capture__row { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.chatbot__capture__legal {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--fg-3);
  text-transform: uppercase;
}
.chatbot__capture__legal a { color: var(--fg-1); text-decoration: underline; }

/* Error & success banners shown above composer */
.chatbot__banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  margin: 0 16px 8px;
  border-radius: 10px;
  font-size: 13px;
  border: 1px solid;
}
.chatbot__banner--error {
  color: #fecaca;
  background: rgba(248, 113, 113, 0.1);
  border-color: rgba(248, 113, 113, 0.4);
}
.chatbot__banner--success {
  color: #bbf7d0;
  background: rgba(52, 211, 153, 0.10);
  border-color: rgba(52, 211, 153, 0.40);
}
.chatbot__banner button {
  margin-left: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.1em;
  color: inherit;
  text-decoration: underline;
}

/* State-conditional reveals — keep DOM simple, drive everything from data-state */
.chatbot__capture,
.chatbot__banner--error,
.chatbot__banner--success { display: none; }
.chatbot[data-state="capture"] .chatbot__capture        { display: flex; }
.chatbot[data-state="capture"] .chatbot__composer       { display: none; }
.chatbot[data-state="error"]   .chatbot__banner--error  { display: flex; }
.chatbot[data-state="success"] .chatbot__banner--success{ display: flex; }

/* Mobile: panel becomes (almost) full screen so nothing is hidden under the keyboard */
@media (max-width: 480px) {
  .chatbot { right: 12px; bottom: 12px; left: 12px; align-items: stretch; }
  .chatbot__panel { width: 100%; height: min(78vh, 640px); }
  @supports (height: 100dvh) {
    .chatbot__panel { height: min(78dvh, 640px); }
  }
}

/* sr-only utility (also defined in shared.css if missing) */
.chatbot .sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}
