/* ==========================================================================
   Assertiv AI Voice Agent — Frontend Widget Stylesheet
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

/* ---- Custom Properties (overridable per-agent via JS) ---- */
:root {
  --aai-primary:        #6366f1;
  --aai-primary-dark:   #4f46e5;
  --aai-primary-glow:   rgba(99, 102, 241, 0.35);
  --aai-surface:        #ffffff;
  --aai-surface-2:      #f8fafc;
  --aai-border:         #e2e8f0;
  --aai-text:           #0f172a;
  --aai-text-2:         #64748b;
  --aai-text-inv:       #ffffff;
  --aai-radius-lg:      20px;
  --aai-radius-md:      12px;
  --aai-radius-sm:      8px;
  --aai-shadow:         0 4px 24px rgba(0,0,0,.12), 0 1px 4px rgba(0,0,0,.06);
  --aai-shadow-btn:     0 8px 24px var(--aai-primary-glow);
  --aai-font:           'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --aai-btn-size:       60px;
  --aai-panel-w:        360px;
  --aai-panel-h:        520px;
  --aai-z:              2147483647;
}

/* ============================================================
   LAUNCHER BUTTON
   ============================================================ */

.aai-launcher {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: var(--aai-z);
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.aai-launcher.aai-pos-bottom-left {
  right: auto;
  left: 24px;
}

.aai-launcher__btn {
  width: var(--aai-btn-size);
  height: var(--aai-btn-size);
  border-radius: 50%;
  background: var(--aai-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--aai-shadow-btn);
  transition: transform .2s cubic-bezier(.34,1.56,.64,1), box-shadow .2s ease;
  position: relative;
  flex-shrink: 0;
  padding: 0;
}

.aai-launcher__btn:hover {
  transform: scale(1.08);
  box-shadow: 0 10px 32px var(--aai-primary-glow);
}

.aai-launcher__btn:active {
  transform: scale(0.95);
}

.aai-launcher__btn svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: #fff;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: opacity .15s, transform .15s;
}

.aai-launcher__btn .aai-icon-mic  { opacity: 1; }
.aai-launcher__btn .aai-icon-close {
  position: absolute;
  opacity: 0;
  transform: rotate(-90deg);
}

.aai-launcher--open .aai-launcher__btn .aai-icon-mic   { opacity: 0; transform: rotate(90deg); }
.aai-launcher--open .aai-launcher__btn .aai-icon-close { opacity: 1; transform: rotate(0); }

/* Pulsing ring when recording */
.aai-launcher__btn::before {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--aai-primary);
  opacity: 0;
  transform: scale(1);
  transition: none;
}

.aai-launcher--recording .aai-launcher__btn::before {
  animation: aai-pulse-ring 1.4s ease-out infinite;
}

@keyframes aai-pulse-ring {
  0%   { opacity: .6; transform: scale(1); }
  100% { opacity: 0;  transform: scale(1.5); }
}

/* Label pill */
.aai-launcher__label {
  background: var(--aai-surface);
  color: var(--aai-text);
  font-family: var(--aai-font);
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 99px;
  box-shadow: var(--aai-shadow);
  white-space: nowrap;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}

.aai-launcher--open .aai-launcher__label {
  opacity: 0;
  transform: translateX(8px);
  pointer-events: none;
}

/* ============================================================
   PANEL
   ============================================================ */

.aai-panel {
  position: fixed;
  bottom: calc(var(--aai-btn-size) + 24px + 14px);
  right: 24px;
  z-index: var(--aai-z);
  width: var(--aai-panel-w);
  max-width: calc(100vw - 32px);
  max-height: var(--aai-panel-h);
  background: var(--aai-surface);
  border-radius: var(--aai-radius-lg);
  box-shadow: var(--aai-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(.96);
  pointer-events: none;
  transition: opacity .22s ease, transform .22s cubic-bezier(.34,1.56,.64,1);
  font-family: var(--aai-font);
}

/* Position Variations for Launcher */
.aai-launcher.aai-pos-bottom-right { bottom: 24px; right: 24px; top: auto; left: auto; }
.aai-launcher.aai-pos-bottom-left  { bottom: 24px; left: 24px; top: auto; right: auto; }
.aai-launcher.aai-pos-top-right    { top: 24px; right: 24px; bottom: auto; left: auto; }
.aai-launcher.aai-pos-top-left     { top: 24px; left: 24px; bottom: auto; right: auto; }
.aai-launcher.aai-pos-center-right { top: 50%; right: 24px; bottom: auto; left: auto; transform: translateY(-50%); }
.aai-launcher.aai-pos-center-left  { top: 50%; left: 24px; bottom: auto; right: auto; transform: translateY(-50%); }

/* Position Variations for Panel */
.aai-panel.aai-pos-bottom-right { bottom: calc(var(--aai-btn-size) + 38px); right: 24px; top: auto; left: auto; }
.aai-panel.aai-pos-bottom-left  { bottom: calc(var(--aai-btn-size) + 38px); left: 24px; top: auto; right: auto; }
.aai-panel.aai-pos-top-right    { top: calc(var(--aai-btn-size) + 38px); right: 24px; bottom: auto; left: auto; }
.aai-panel.aai-pos-top-left     { top: calc(var(--aai-btn-size) + 38px); left: 24px; bottom: auto; right: auto; }
.aai-panel.aai-pos-center-right { top: 50%; right: calc(var(--aai-btn-size) + 38px); bottom: auto; left: auto; transform: translateY(-50%) scale(.96); }
.aai-panel.aai-pos-center-left  { top: 50%; left: calc(var(--aai-btn-size) + 38px); bottom: auto; right: auto; transform: translateY(-50%) scale(.96); }

.aai-panel.aai-pos-center-right.aai-panel--visible,
.aai-panel.aai-pos-center-left.aai-panel--visible {
  transform: translateY(-50%) scale(1);
}

.aai-panel--visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Header */
.aai-panel__header {
  padding: 16px 18px 14px;
  background: linear-gradient(135deg, var(--aai-primary), var(--aai-primary-dark));
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.aai-panel__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,.2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

.aai-panel__avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.aai-panel__avatar svg {
  width: 22px;
  height: 22px;
  stroke: #fff;
  stroke-width: 1.8;
  fill: none;
}

.aai-panel__info {
  flex: 1;
  min-width: 0;
}

.aai-panel__name {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

.aai-panel__status {
  font-size: 11px;
  color: rgba(255,255,255,.8);
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.aai-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4ade80;
  flex-shrink: 0;
}

.aai-status-dot--recording {
  background: #f87171;
  animation: aai-blink .8s step-end infinite;
}

@keyframes aai-blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: .2; }
}

.aai-panel__close {
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}

.aai-panel__close:hover {
  background: rgba(255,255,255,.25);
}

.aai-panel__close svg {
  width: 16px;
  height: 16px;
  stroke: #fff;
  stroke-width: 2.5;
  fill: none;
}

/* Body — messages */
.aai-panel__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.aai-panel__body::-webkit-scrollbar { width: 4px; }
.aai-panel__body::-webkit-scrollbar-thumb { background: var(--aai-border); border-radius: 99px; }

/* Messages */
.aai-msg {
  display: flex;
  gap: 8px;
  animation: aai-msg-in .2s ease;
}

@keyframes aai-msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.aai-msg--agent { align-self: flex-start; }
.aai-msg--user  { align-self: flex-end; flex-direction: row-reverse; }

.aai-msg__bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--aai-radius-md);
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--aai-text);
  background: var(--aai-surface-2);
  border: 1px solid var(--aai-border);
  word-break: break-word;
}

.aai-msg--user .aai-msg__bubble {
  background: var(--aai-primary);
  color: #fff;
  border-color: transparent;
}

/* Typing indicator */
.aai-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  background: var(--aai-surface-2);
  border: 1px solid var(--aai-border);
  border-radius: var(--aai-radius-md);
  width: fit-content;
}

.aai-typing__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--aai-primary);
  animation: aai-dot 1.2s ease-in-out infinite;
}

.aai-typing__dot:nth-child(2) { animation-delay: .2s; }
.aai-typing__dot:nth-child(3) { animation-delay: .4s; }

@keyframes aai-dot {
  0%, 80%, 100% { transform: scale(1); opacity: .6; }
  40%            { transform: scale(1.4); opacity: 1; }
}

/* Footer — mic / text input */
.aai-panel__footer {
  padding: 12px 14px;
  border-top: 1px solid var(--aai-border);
  background: var(--aai-surface);
  flex-shrink: 0;
}

/* Voice controls */
.aai-voice-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
}

.aai-mic-btn {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--aai-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--aai-shadow-btn);
  transition: transform .15s ease, box-shadow .15s ease;
  position: relative;
}

.aai-mic-btn:hover  { transform: scale(1.06); }
.aai-mic-btn:active { transform: scale(.94); }

.aai-mic-btn svg {
  width: 22px;
  height: 22px;
  stroke: #fff;
  stroke-width: 2;
  fill: none;
  pointer-events: none;
}

.aai-mic-btn--recording {
  background: #ef4444;
  box-shadow: 0 6px 24px rgba(239,68,68,.4);
}

.aai-mic-btn--recording::after {
  content: '';
  position: absolute;
  inset: -5px;
  border-radius: 50%;
  border: 2px solid #ef4444;
  animation: aai-pulse-ring 1.2s ease-out infinite;
}

.aai-hint {
  font-size: 11px;
  color: var(--aai-text-2);
  text-align: center;
  margin-top: 6px;
}

/* Text input row */
.aai-text-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.aai-text-input {
  flex: 1;
  padding: 9px 12px;
  font-size: 13px;
  font-family: var(--aai-font);
  border: 1.5px solid var(--aai-border);
  border-radius: var(--aai-radius-sm);
  outline: none;
  resize: none;
  min-height: 38px;
  max-height: 80px;
  overflow-y: auto;
  transition: border-color .15s;
  color: var(--aai-text);
  background: var(--aai-surface);
}

.aai-text-input:focus {
  border-color: var(--aai-primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.1);
}

.aai-send-btn {
  width: 38px;
  height: 38px;
  border-radius: var(--aai-radius-sm);
  background: var(--aai-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}

.aai-send-btn:hover  { background: var(--aai-primary-dark); }
.aai-send-btn:active { transform: scale(.93); }

.aai-send-btn svg {
  width: 16px;
  height: 16px;
  stroke: #fff;
  stroke-width: 2.2;
  fill: none;
}

/* Tab toggle: Voice / Text */
.aai-mode-toggle {
  display: flex;
  background: var(--aai-surface-2);
  border: 1px solid var(--aai-border);
  border-radius: 99px;
  padding: 3px;
  gap: 2px;
  width: fit-content;
  margin: 0 auto 10px;
}

.aai-mode-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  font-family: var(--aai-font);
  color: var(--aai-text-2);
  padding: 4px 14px;
  border-radius: 99px;
  transition: background .15s, color .15s;
}

.aai-mode-btn--active {
  background: var(--aai-primary);
  color: #fff;
}

/* Privacy / Attribution bar */
.aai-privacy {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 11px;
  color: var(--aai-text-2);
  padding-top: 6px;
}

.aai-privacy a {
  color: var(--aai-primary);
  font-weight: 600;
  text-decoration: none;
  transition: color .15s ease;
}

.aai-privacy a:hover {
  text-decoration: underline;
  color: var(--aai-primary-dark);
}

/* ============================================================
   MOBILE — Full bottom-sheet design
   ============================================================ */

@media (max-width: 600px) {

  /* ── Panel: full-width bottom sheet ──
     Overrides ALL position variants so the panel always slides
     up from the bottom edge as a sheet. The launcher button
     position is intentionally NOT touched here — it stays
     wherever the admin has configured it (bottom-right,
     bottom-left, top-right, etc.).
  ── */
  .aai-panel,
  .aai-panel.aai-pos-bottom-right,
  .aai-panel.aai-pos-bottom-left,
  .aai-panel.aai-pos-top-right,
  .aai-panel.aai-pos-top-left,
  .aai-panel.aai-pos-center-right,
  .aai-panel.aai-pos-center-left {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 80vh !important;
    max-height: 80vh !important;
    border-radius: 20px 20px 0 0 !important;
    /* Safe area padding for notched / home-bar phones */
    padding-bottom: env(safe-area-inset-bottom, 0px);
    /* Slide up from bottom */
    transform: translateY(100%) !important;
    opacity: 1 !important;
    transition: transform .3s cubic-bezier(.32,1,.32,1) !important;
  }

  /* Visible state — slide into view */
  .aai-panel--visible,
  .aai-panel.aai-pos-center-right.aai-panel--visible,
  .aai-panel.aai-pos-center-left.aai-panel--visible {
    transform: translateY(0) !important;
    pointer-events: auto !important;
  }

  /* ── Launcher: reduce bottom margin so it stays above
     the home bar on notched phones, but keep its
     configured left/right/top position intact. ── */
  .aai-launcher {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }
  /* bottom-left variant keeps its left anchor */
  .aai-launcher.aai-pos-bottom-left {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    left: 16px;
  }
  /* bottom-right variant keeps its right anchor */
  .aai-launcher.aai-pos-bottom-right {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    right: 16px;
  }

  /* ── Panel header — drag handle visual ── */
  .aai-panel__header {
    padding: 14px 16px 12px;
    position: relative;
  }

  .aai-panel__header::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 99px;
    background: rgba(255, 255, 255, 0.4);
  }

  /* ── Message body ── */
  .aai-panel__body {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
  }

  .aai-msg__bubble {
    font-size: 14px;
    padding: 10px 13px;
    max-width: 88%;
  }

  /* ── Footer ── */
  .aai-panel__footer {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  /* Larger mic button for touch */
  .aai-mic-btn {
    width: 60px;
    height: 60px;
  }

  .aai-mic-btn svg {
    width: 26px;
    height: 26px;
  }

  /* Prevents iOS auto-zoom on input focus */
  .aai-text-input {
    font-size: 16px;
    padding: 10px 13px;
    min-height: 42px;
  }

  .aai-send-btn {
    width: 42px;
    height: 42px;
  }

  /* Mode toggle — full width */
  .aai-mode-toggle {
    width: 100%;
    justify-content: center;
    margin-bottom: 8px;
  }

  .aai-mode-btn {
    flex: 1;
    text-align: center;
    padding: 6px 10px;
    font-size: 12px;
  }

  .aai-hint {
    font-size: 12px;
    margin-top: 8px;
  }

  .aai-privacy {
    font-size: 10px;
    padding-top: 4px;
  }
}

/* Extra small phones (≤360px) */
@media (max-width: 360px) {
  .aai-panel,
  .aai-panel.aai-pos-bottom-right,
  .aai-panel.aai-pos-bottom-left,
  .aai-panel.aai-pos-top-right,
  .aai-panel.aai-pos-top-left,
  .aai-panel.aai-pos-center-right,
  .aai-panel.aai-pos-center-left {
    height: 85vh !important;
    max-height: 85vh !important;
  }
}
