/**
 * BiaToHost Live Chat Widget — Crisp-Inspired Modern Styling
 * Optimized for high performance, RTL Persian typography (IRANSansX / Vazirmatn), and dynamic Light/Dark mode sync.
 */

:root {
  --biachat-font: "IRANSansX", "Vazirmatn", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  
  /* Light Mode Defaults */
  --biachat-primary: #1E40D8;
  --biachat-primary-hover: #1D35B5;
  --biachat-primary-glow: rgba(30, 64, 216, 0.35);
  --biachat-gradient: linear-gradient(135deg, #1E40D8 0%, #3B82F6 100%);
  
  --biachat-bg: #FFFFFF;
  --biachat-surface: #F8FAFC;
  --biachat-text: #0F172A;
  --biachat-text-muted: #64748B;
  --biachat-border: #E2E8F0;
  
  --biachat-operator-bubble: #F1F5F9;
  --biachat-operator-text: #0F172A;
  --biachat-operator-border: #E2E8F0;
  
  --biachat-user-bubble: linear-gradient(135deg, #1E40D8 0%, #2563EB 100%);
  --biachat-user-text: #FFFFFF;
  
  --biachat-input-bg: #F8FAFC;
  --biachat-input-border: #E2E8F0;
  --biachat-input-text: #0F172A;
  
  --biachat-chip-bg: #EFF6FF;
  --biachat-chip-border: #DBEAFE;
  --biachat-chip-text: #1E40D8;
  
  --biachat-shadow: 0 20px 45px -10px rgba(15, 23, 42, 0.15), 0 10px 20px -5px rgba(0, 0, 0, 0.08);
  --biachat-radius: 20px;
}

/* Dark Mode (Synced with BiaToHost [data-theme="dark"], [data-bs-theme="dark"], .dark, or manual toggle) */
html[data-theme="dark"] #biachat-root,
html[data-bs-theme="dark"] #biachat-root,
body.dark #biachat-root,
#biachat-root.biachat-theme-dark,
[data-theme="dark"] .biachat-window,
.dark .biachat-window {
  --biachat-primary: #2B44FF;
  --biachat-primary-hover: #4B63FF;
  --biachat-primary-glow: rgba(43, 68, 255, 0.45);
  --biachat-gradient: linear-gradient(135deg, #1E40D8 0%, #3B82F6 100%);
  
  --biachat-bg: #0B101B;
  --biachat-surface: #131B2E;
  --biachat-text: #F8FAFC;
  --biachat-text-muted: #94A3B8;
  --biachat-border: #1F293D;
  
  --biachat-operator-bubble: #172136;
  --biachat-operator-text: #F1F5F9;
  --biachat-operator-border: #24314C;
  
  --biachat-user-bubble: linear-gradient(135deg, #2563EB 0%, #1D4ED8 100%);
  --biachat-user-text: #FFFFFF;
  
  --biachat-input-bg: #0F172A;
  --biachat-input-border: #1F293D;
  --biachat-input-text: #F8FAFC;
  
  --biachat-chip-bg: #162238;
  --biachat-chip-border: #223252;
  --biachat-chip-text: #93C5FD;
  
  --biachat-shadow: 0 24px 50px -10px rgba(0, 0, 0, 0.75), 0 12px 24px -5px rgba(0, 0, 0, 0.5);
}

/* System Dark Mode Fallback (if no explicit theme is declared) */
@media (prefers-color-scheme: dark) {
  #biachat-root:not(.biachat-theme-light) {
    --biachat-primary: #2B44FF;
    --biachat-primary-hover: #4B63FF;
    --biachat-primary-glow: rgba(43, 68, 255, 0.45);
    
    --biachat-bg: #0B101B;
    --biachat-surface: #131B2E;
    --biachat-text: #F8FAFC;
    --biachat-text-muted: #94A3B8;
    --biachat-border: #1F293D;
    
    --biachat-operator-bubble: #172136;
    --biachat-operator-text: #F1F5F9;
    --biachat-operator-border: #24314C;
    
    --biachat-user-bubble: linear-gradient(135deg, #2563EB 0%, #1D4ED8 100%);
    --biachat-user-text: #FFFFFF;
    
    --biachat-input-bg: #0F172A;
    --biachat-input-border: #1F293D;
    --biachat-input-text: #F8FAFC;
    
    --biachat-chip-bg: #162238;
    --biachat-chip-border: #223252;
    --biachat-chip-text: #93C5FD;
    
    --biachat-shadow: 0 24px 50px -10px rgba(0, 0, 0, 0.75);
  }
}

/* Widget Root Container */
#biachat-root {
  position: fixed;
  bottom: 24px;
  left: 24px;
  z-index: 9999999;
  font-family: var(--biachat-font);
  direction: rtl;
  text-align: right;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#biachat-root[data-position="right"] {
  left: auto;
  right: 24px;
}

/* Floating Launcher Button */
.biachat-launcher {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: var(--biachat-gradient);
  box-shadow: 0 8px 28px rgba(30, 64, 216, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  outline: none;
  transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.28s ease;
  position: relative;
  user-select: none;
}

.biachat-launcher:hover {
  transform: scale(1.08);
  box-shadow: 0 12px 34px rgba(30, 64, 216, 0.6);
}

.biachat-launcher:active {
  transform: scale(0.95);
}

.biachat-launcher svg {
  width: 28px;
  height: 28px;
  fill: #FFFFFF;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
}

.biachat-launcher .icon-close {
  display: none;
  position: absolute;
}

.biachat-open .biachat-launcher .icon-chat {
  display: none;
}

.biachat-open .biachat-launcher .icon-close {
  display: block;
  transform: rotate(90deg);
}

.biachat-open .biachat-launcher:hover .icon-close {
  transform: rotate(180deg);
}

/* Unread Badge */
.biachat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #EF4444;
  color: #FFFFFF;
  font-size: 11px;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.55);
  border: 2px solid #FFFFFF;
  animation: biachatPulse 2s infinite;
}

@keyframes biachatPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

/* Chat Main Window */
.biachat-window {
  position: absolute;
  bottom: 78px;
  left: 0;
  width: 380px;
  height: 600px;
  max-height: calc(100vh - 110px);
  background: var(--biachat-bg);
  border-radius: var(--biachat-radius);
  box-shadow: var(--biachat-shadow);
  border: 1px solid var(--biachat-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px) scale(0.96);
  transform-origin: bottom left;
  transition: opacity 0.25s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
              visibility 0.25s,
              background 0.25s,
              border-color 0.25s;
}

#biachat-root[data-position="right"] .biachat-window {
  left: auto;
  right: 0;
  transform-origin: bottom right;
}

.biachat-open .biachat-window {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* Window Header */
.biachat-header {
  background: var(--biachat-gradient);
  color: #FFFFFF;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
}

.biachat-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.biachat-team-avatars {
  display: flex;
  align-items: center;
  position: relative;
}

.biachat-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  background: #1E3A8A;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FFFFFF;
  font-weight: 700;
  font-size: 12px;
  margin-left: -10px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  overflow: hidden;
}

.biachat-avatar:first-child {
  margin-left: 0;
}

.biachat-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.biachat-online-dot {
  position: absolute;
  bottom: 0;
  right: 24px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #10B981;
  border: 2px solid #FFFFFF;
}

.biachat-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.biachat-header-btn {
  background: rgba(255, 255, 255, 0.16);
  border: none;
  border-radius: 8px;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FFFFFF;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
}

.biachat-header-btn:hover {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(1.05);
}

.biachat-header-btn:active {
  transform: scale(0.92);
}

.biachat-header-info h3 {
  margin: 0;
  font-size: 15.5px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.biachat-header-info p {
  margin: 2px 0 0;
  font-size: 12px;
  opacity: 0.92;
  display: flex;
  align-items: center;
  gap: 6px;
}

.biachat-status-pulse {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #34D399;
  display: inline-block;
  box-shadow: 0 0 8px #34D399;
}

/* Chat Body */
.biachat-body {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  background: var(--biachat-bg);
  transition: background 0.25s;
}

.biachat-body::-webkit-scrollbar {
  width: 5px;
}
.biachat-body::-webkit-scrollbar-thumb {
  background: var(--biachat-border);
  border-radius: 4px;
}

/* Welcome Card */
.biachat-welcome-card {
  background: var(--biachat-surface);
  border: 1px solid var(--biachat-border);
  border-radius: 14px;
  padding: 14px 16px;
  text-align: center;
  color: var(--biachat-text);
  margin-bottom: 6px;
  transition: background 0.25s, border-color 0.25s;
}

.biachat-welcome-card h4 {
  margin: 0 0 6px;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--biachat-primary);
}

.biachat-welcome-card p {
  margin: 0;
  font-size: 12px;
  color: var(--biachat-text-muted);
  line-height: 1.55;
}

/* Messages */
.biachat-msg-row {
  display: flex;
  flex-direction: column;
  max-width: 84%;
  animation: biachatFadeIn 0.2s ease-out;
}

@keyframes biachatFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.biachat-msg-row.user {
  align-self: flex-start;
}

.biachat-msg-row.operator {
  align-self: flex-end;
}

.biachat-msg-author {
  font-size: 11px;
  color: var(--biachat-text-muted);
  margin-bottom: 4px;
  padding: 0 4px;
  font-weight: 500;
}

.biachat-msg-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 13px;
  line-height: 1.6;
  word-break: break-word;
  position: relative;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  transition: background 0.2s, border-color 0.2s;
}

.biachat-msg-row.user .biachat-msg-bubble {
  background: var(--biachat-user-bubble);
  color: var(--biachat-user-text);
  border-bottom-right-radius: 4px;
}

.biachat-msg-row.operator .biachat-msg-bubble {
  background: var(--biachat-operator-bubble);
  color: var(--biachat-operator-text);
  border: 1px solid var(--biachat-operator-border);
  border-bottom-left-radius: 4px;
}

.biachat-msg-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  margin-top: 4px;
  opacity: 0.75;
}

.biachat-msg-row.user .biachat-msg-meta {
  color: rgba(255, 255, 255, 0.85);
  justify-content: flex-end;
}

.biachat-msg-row.operator .biachat-msg-meta {
  color: var(--biachat-text-muted);
}

/* Typing Indicator */
.biachat-typing {
  display: none;
  align-self: flex-end;
  background: var(--biachat-operator-bubble);
  border: 1px solid var(--biachat-operator-border);
  padding: 8px 14px;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  gap: 4px;
  align-items: center;
}

.biachat-typing.active {
  display: flex;
}

.biachat-typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--biachat-text-muted);
  animation: biachatBlink 1.4s infinite both;
}

.biachat-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.biachat-typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes biachatBlink {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* Quick FAQs / Action Chips */
.biachat-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 0 16px 8px;
  white-space: nowrap;
}

.biachat-chips::-webkit-scrollbar {
  display: none;
}

.biachat-chip {
  background: var(--biachat-chip-bg);
  border: 1px solid var(--biachat-chip-border);
  color: var(--biachat-chip-text);
  font-size: 11.5px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}

.biachat-chip:hover {
  background: var(--biachat-primary);
  color: #FFFFFF;
  border-color: var(--biachat-primary);
  transform: translateY(-1px);
}

/* Footer / Input Area */
.biachat-footer {
  border-top: 1px solid var(--biachat-border);
  padding: 12px 16px;
  background: var(--biachat-bg);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: background 0.25s, border-color 0.25s;
}

.biachat-input-box {
  display: flex;
  align-items: center;
  background: var(--biachat-input-bg);
  border: 1px solid var(--biachat-input-border);
  border-radius: 24px;
  padding: 4px 6px 4px 12px;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.25s;
}

.biachat-input-box:focus-within {
  border-color: var(--biachat-primary);
  box-shadow: 0 0 0 3px var(--biachat-primary-glow);
}

.biachat-textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: inherit;
  font-size: 13px;
  color: var(--biachat-input-text);
  resize: none;
  height: 24px;
  max-height: 80px;
  line-height: 24px;
  padding: 0;
  direction: rtl;
}

.biachat-textarea::placeholder {
  color: var(--biachat-text-muted);
}

.biachat-send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--biachat-primary);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #FFFFFF;
  transition: background 0.2s, transform 0.15s;
  flex-shrink: 0;
}

.biachat-send-btn:hover {
  background: var(--biachat-primary-hover);
  transform: scale(1.06);
}

.biachat-send-btn:active {
  transform: scale(0.92);
}

.biachat-send-btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
  transform: scaleX(-1);
}

/* Branding Line */
.biachat-branding {
  text-align: center;
  font-size: 10.5px;
  color: var(--biachat-text-muted);
  margin-top: 2px;
}

.biachat-branding a {
  color: var(--biachat-primary);
  text-decoration: none;
  font-weight: 600;
}

/* Responsive Mobile Layout */
@media (max-width: 640px) {
  #biachat-root {
    bottom: 16px;
    left: 16px;
  }
  #biachat-root[data-position="right"] {
    right: 16px;
  }
  .biachat-window {
    position: fixed;
    top: 0;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    height: 100% !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
    border: none !important;
    transform-origin: center bottom !important;
  }
  .biachat-header {
    padding-top: max(16px, env(safe-area-inset-top));
  }
  .biachat-footer {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}
