/**
 * Unified FiSense · Gwen chatbox — floating panel for all LFE product simulators.
 * Visual design matches client FiSensechatbox.html (Gwen V5 shell).
 * Fully responsive: phone sheet, tablet card, desktop float; safe-area + keyboard aware.
 */
.lfe-qwen-fab,
.lfe-gwen-fab {
  position: fixed;
  right: max(12px, env(safe-area-inset-right, 0px));
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  z-index: 9000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

.lfe-gwen-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 8999;
  background: rgba(15, 23, 42, 0.42);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

.lfe-gwen-backdrop.open {
  display: block;
}

.lfe-qwen-panel,
.lfe-gwen-panel {
  width: min(400px, calc(100vw - 24px));
  max-height: min(560px, calc(100dvh - 96px - env(safe-area-inset-bottom, 0px)));
  background: #ffffff;
  border-radius: 28px;
  box-shadow: 0 20px 60px rgba(0, 20, 40, 0.16);
  display: none;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid #e2e8f0;
  overscroll-behavior: contain;
}

.lfe-qwen-panel.open,
.lfe-gwen-panel.open {
  display: flex;
}

.lfe-qwen-head,
.lfe-gwen-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 12px 12px 16px;
  padding-top: max(14px, env(safe-area-inset-top, 0px));
  background: #0b2b44;
  color: #fff;
  flex-shrink: 0;
}

.lfe-gwen-logo {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #60a5fa);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
}

.lfe-qwen-head .lfe-gwen-title,
.lfe-gwen-head .lfe-gwen-title {
  flex: 1;
  min-width: 0;
}

.lfe-qwen-head strong,
.lfe-gwen-head strong {
  display: block;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lfe-qwen-head .lfe-gwen-sub,
.lfe-gwen-head .lfe-gwen-sub {
  font-size: 10px;
  opacity: 0.7;
  font-weight: 400;
  letter-spacing: 0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lfe-qwen-status,
.lfe-gwen-status {
  background: rgba(255, 255, 255, 0.12);
  padding: 4px 10px;
  border-radius: 40px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.3px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  flex-shrink: 0;
  white-space: nowrap;
}

.lfe-qwen-status.live,
.lfe-gwen-status.live {
  background: rgba(16, 185, 129, 0.28);
}

.lfe-qwen-status.thinking,
.lfe-gwen-status.thinking {
  background: rgba(251, 191, 36, 0.35);
}

.lfe-gwen-close {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  min-width: 40px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.lfe-gwen-close:hover,
.lfe-gwen-close:focus-visible {
  background: rgba(255, 255, 255, 0.22);
}

.lfe-gwen-close:focus-visible {
  outline: 2px solid #60a5fa;
  outline-offset: 2px;
}

.lfe-gwen-progress {
  height: 3px;
  background: #e9edf2;
  flex-shrink: 0;
}

.lfe-gwen-progress .fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #60a5fa);
  width: 8%;
  transition: width 0.6s ease;
}

.lfe-qwen-msgs,
.lfe-gwen-msgs {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #fafcff;
  min-height: 140px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.lfe-qwen-msg,
.lfe-gwen-msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  overflow-wrap: anywhere;
  animation: lfe-gwen-fadeUp 0.3s ease;
}

@keyframes lfe-gwen-fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.lfe-qwen-msg.user,
.lfe-gwen-msg.user {
  align-self: flex-end;
  background: #0b2b44;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.lfe-qwen-msg.bot,
.lfe-qwen-msg.gwen,
.lfe-gwen-msg.bot,
.lfe-gwen-msg.gwen {
  align-self: flex-start;
  background: #eef3f9;
  color: #1a2a3a;
  border-bottom-left-radius: 4px;
}

.lfe-qwen-msg.system,
.lfe-gwen-msg.system {
  align-self: center;
  background: #f1f5f9;
  color: #475569;
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 40px;
  max-width: 95%;
  text-align: center;
}

.lfe-qwen-msg.thinking,
.lfe-gwen-msg.thinking {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #475569;
  font-style: italic;
}

.lfe-qwen-msg.streaming,
.lfe-gwen-msg.streaming {
  white-space: pre-wrap;
}

.lfe-gwen-prototype {
  background: #fef3c7;
  color: #92400e;
  text-align: center;
  font-size: 10px;
  font-weight: 600;
  padding: 5px 8px;
  letter-spacing: 0.3px;
  border-bottom: 1px solid #fcd34d;
  max-width: min(380px, calc(100vw - 24px));
  margin: 0 0 6px auto;
  border-radius: 12px 12px 0 0;
}

.lfe-qwen-fab:has(.lfe-gwen-prototype) .lfe-gwen-panel,
.lfe-gwen-fab:has(.lfe-gwen-prototype) .lfe-gwen-panel {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.lfe-qwen-msg.streaming::after,
.lfe-gwen-msg.streaming::after {
  content: '|';
  display: inline-block;
  margin-left: 2px;
  animation: lfe-gwen-caret 1s step-end infinite;
  color: #0b2b44;
}

@keyframes lfe-gwen-caret {
  50% { opacity: 0; }
}

.lfe-qwen-thinking-label,
.lfe-gwen-thinking-label {
  font-weight: 600;
  font-style: normal;
  color: #0b2b44;
}

.lfe-qwen-dots,
.lfe-gwen-dots {
  display: inline-flex;
  gap: 5px;
  align-items: center;
}

.lfe-qwen-dots i,
.lfe-gwen-dots i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #7a8a9e;
  display: inline-block;
  animation: lfe-gwen-pulse 1.3s infinite ease-in-out;
}

.lfe-qwen-dots i:nth-child(2),
.lfe-gwen-dots i:nth-child(2) { animation-delay: 0.2s; }
.lfe-qwen-dots i:nth-child(3),
.lfe-gwen-dots i:nth-child(3) { animation-delay: 0.4s; }

@keyframes lfe-gwen-pulse {
  0%, 60%, 100% { transform: scale(0.6); opacity: 0.4; }
  30% { transform: scale(1); opacity: 1; }
}

.lfe-qwen-compose,
.lfe-gwen-compose {
  display: flex;
  gap: 8px;
  padding: 8px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid #e9edf2;
  background: #fff;
  align-items: flex-end;
  flex-shrink: 0;
}

.lfe-qwen-compose input,
.lfe-gwen-compose input,
.lfe-qwen-compose textarea,
.lfe-gwen-compose textarea {
  flex: 1;
  min-width: 0;
  border: 1.5px solid #dce1e9;
  border-radius: 18px;
  padding: 10px 14px;
  min-height: 44px;
  max-height: 120px;
  font-size: 16px; /* iOS: avoid zoom on focus */
  line-height: 1.35;
  outline: none;
  background: #f8fafc;
  transition: border-color 0.2s, background 0.2s;
  font-family: inherit;
  resize: none;
  -webkit-appearance: none;
  appearance: none;
}

.lfe-qwen-compose input:focus,
.lfe-gwen-compose input:focus,
.lfe-qwen-compose textarea:focus,
.lfe-gwen-compose textarea:focus {
  border-color: #3b82f6;
  background: #fff;
}

.lfe-qwen-compose input:disabled,
.lfe-gwen-compose input:disabled,
.lfe-qwen-compose textarea:disabled,
.lfe-gwen-compose textarea:disabled {
  opacity: 0.5;
  background: #f0f3f8;
  cursor: not-allowed;
}

.lfe-qwen-compose button#lfeQwenSend,
.lfe-gwen-compose button#lfeQwenSend {
  border: none;
  background: #0b2b44;
  color: #fff;
  border-radius: 40px;
  padding: 0 18px;
  height: 44px;
  min-width: 72px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.lfe-qwen-compose button#lfeQwenSend:hover:not(:disabled),
.lfe-gwen-compose button#lfeQwenSend:hover:not(:disabled) {
  background: #1a3f5c;
}

.lfe-qwen-compose button#lfeQwenSend:disabled,
.lfe-gwen-compose button#lfeQwenSend:disabled {
  opacity: 0.4;
  cursor: default;
}

.lfe-qwen-toggle,
.lfe-gwen-toggle {
  border: none;
  background: #0b2b44;
  color: #fff;
  border-radius: 999px;
  padding: 12px 18px;
  min-height: 48px;
  min-width: 48px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(11, 43, 68, 0.35);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.lfe-qwen-toggle:hover,
.lfe-gwen-toggle:hover {
  background: #1a3f5c;
}

.lfe-qwen-toggle:focus-visible,
.lfe-gwen-toggle:focus-visible,
.lfe-qwen-compose button:focus-visible,
.lfe-gwen-compose button:focus-visible {
  outline: 2px solid #60a5fa;
  outline-offset: 2px;
}

body.lfe-gwen-chat-open {
  overflow: hidden;
  touch-action: none;
}

/* Phones: almost full-screen sheet */
@media (max-width: 640px) {
  .lfe-qwen-fab,
  .lfe-gwen-fab {
    right: 0;
    left: 0;
    bottom: 0;
    align-items: stretch;
    padding: 0;
  }

  .lfe-qwen-fab:not(:has(.lfe-gwen-panel.open)),
  .lfe-gwen-fab:not(:has(.lfe-gwen-panel.open)) {
    right: max(12px, env(safe-area-inset-right, 0px));
    left: auto;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    align-items: flex-end;
  }

  .lfe-qwen-panel,
  .lfe-gwen-panel {
    width: 100%;
    max-width: 100%;
    height: min(92dvh, calc(100dvh - env(safe-area-inset-top, 0px)));
    max-height: min(92dvh, calc(100dvh - env(safe-area-inset-top, 0px)));
    border-radius: 20px 20px 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
  }

  .lfe-qwen-fab:has(.lfe-gwen-prototype) .lfe-gwen-panel,
  .lfe-gwen-fab:has(.lfe-gwen-prototype) .lfe-gwen-panel {
    border-radius: 0;
  }

  .lfe-gwen-prototype {
    max-width: 100%;
    margin: 0;
    border-radius: 0;
  }

  .lfe-qwen-msgs,
  .lfe-gwen-msgs {
    min-height: 0;
  }

  .lfe-qwen-msg,
  .lfe-gwen-msg {
    max-width: 94%;
    font-size: 15px;
  }

  .lfe-qwen-toggle,
  .lfe-gwen-toggle {
    align-self: flex-end;
    margin: 0 max(12px, env(safe-area-inset-right, 0px))
      calc(12px + env(safe-area-inset-bottom, 0px)) 0;
  }

  .lfe-qwen-fab:has(.lfe-gwen-panel.open) .lfe-qwen-toggle,
  .lfe-gwen-fab:has(.lfe-gwen-panel.open) .lfe-gwen-toggle {
    display: none;
  }

  .lfe-qwen-status,
  .lfe-gwen-status {
    display: none;
  }
}

@media (min-width: 641px) and (max-width: 900px) {
  .lfe-qwen-panel,
  .lfe-gwen-panel {
    width: min(420px, calc(100vw - 32px));
    max-height: min(620px, calc(100dvh - 100px));
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .lfe-qwen-panel.open,
  .lfe-gwen-panel.open {
    width: min(520px, calc(100vw - 24px));
    max-height: calc(100dvh - 24px);
    height: calc(100dvh - 24px);
  }

  .lfe-qwen-head,
  .lfe-gwen-head {
    padding: 8px 10px;
  }

  .lfe-gwen-logo {
    width: 30px;
    height: 30px;
    font-size: 13px;
  }

  .lfe-gwen-sub {
    display: none;
  }

  .lfe-qwen-compose,
  .lfe-gwen-compose {
    padding: 6px 10px calc(6px + env(safe-area-inset-bottom, 0px));
  }
}

@media (prefers-reduced-motion: reduce) {
  .lfe-qwen-msg,
  .lfe-gwen-msg,
  .lfe-qwen-dots i,
  .lfe-gwen-dots i,
  .lfe-qwen-msg.streaming::after,
  .lfe-gwen-msg.streaming::after {
    animation: none !important;
  }

  .lfe-gwen-progress .fill {
    transition: none;
  }
}
