#chatToggle {
  position: fixed;
  left: 24px;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  width: 60px;
  height: 60px;
  border: none !important;
  border-radius: 20px;
  background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%) !important;
  color: #ffffff !important;
  display: grid !important;
  place-items: center !important;
  cursor: pointer;
  opacity: 1 !important;
  visibility: visible !important;
  filter: none !important;
  box-shadow:
    0 14px 34px rgba(79, 70, 229, 0.38),
    0 0 0 1px rgba(255, 255, 255, 0.24) inset,
    0 2px 0 rgba(255, 255, 255, 0.32) inset !important;
  transition:
    transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.25s ease;
  z-index: 1002;
  font-family: "Vazirmatn", system-ui, -apple-system, "Segoe UI", sans-serif;
}

#chatToggle:hover {
  transform: translateY(-3px) scale(1.06);
  box-shadow:
    0 20px 40px rgba(79, 70, 229, 0.45),
    0 0 0 1px rgba(255, 255, 255, 0.3) inset !important;
}

#chatToggle:active {
  transform: scale(0.94);
  transition-duration: 0.1s;
}

#chatToggle svg,
#chatToggle i {
  width: 24px !important;
  height: 24px !important;
  display: block !important;
  color: #ffffff !important;
  fill: none !important;
  stroke: currentColor !important;
  stroke-width: 2 !important;
  opacity: 1 !important;
  flex-shrink: 0;
}

#chatWidget {
  direction: rtl;

  --chat-card: rgba(255, 255, 255, 0.96);
  --chat-line: rgba(148, 163, 184, 0.22);
  --chat-text: #0f172a;
  --chat-muted: #475569;
  --chat-muted-2: #64748b;
  --chat-primary: #4f46e5;
  --chat-primary-2: #6366f1;
  --chat-primary-3: #3730a3;
  --chat-primary-soft: rgba(79, 70, 229, 0.12);
  --chat-shadow-sm: 0 4px 12px rgba(15, 23, 42, 0.08);
  --chat-shadow-md: 0 12px 28px rgba(15, 23, 42, 0.14);
  --chat-shadow-lg: 0 24px 48px rgba(15, 23, 42, 0.18);
  --chat-radius-xl: 28px;
  --chat-radius-lg: 22px;
  --chat-radius-md: 16px;
  --chat-radius-sm: 12px;
  --chat-font: "Vazirmatn", system-ui, -apple-system, "Segoe UI", sans-serif;
  --chat-ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --chat-ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  color: var(--chat-text);
  font-family: var(--chat-font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#chatWidget,
#chatWidget *,
#chatWidget *::before,
#chatWidget *::after {
  box-sizing: border-box;
}

@media (prefers-color-scheme: dark) {
  #chatWidget {
    --chat-card: rgba(15, 23, 42, 0.96);
    --chat-line: rgba(148, 163, 184, 0.18);
    --chat-text: #f8fafc;
    --chat-muted: #cbd5e1;
    --chat-muted-2: #94a3b8;
    --chat-primary: #818cf8;
    --chat-primary-2: #6366f1;
    --chat-primary-3: #c7d2fe;
    --chat-primary-soft: rgba(129, 140, 248, 0.16);
    --chat-shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.28);
    --chat-shadow-md: 0 12px 28px rgba(0, 0, 0, 0.38);
    --chat-shadow-lg: 0 24px 48px rgba(0, 0, 0, 0.48);
  }
}

.chat-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.28);
  backdrop-filter: blur(10px) saturate(1.3);
  -webkit-backdrop-filter: blur(10px) saturate(1.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
  z-index: 1000;
}

.chat-backdrop.show {
  opacity: 1;
  pointer-events: auto;
}

#chatContainer {
  position: fixed;
  left: 24px;
  bottom: calc(96px + env(safe-area-inset-bottom, 0px));
  width: 440px;
  max-width: calc(100vw - 40px);
  height: 640px;
  max-height: calc(100dvh - 140px);
  display: flex;
  flex-direction: column;
  border-radius: var(--chat-radius-xl);
  overflow: hidden;
  background: rgba(255, 255, 255, 0.96) !important;
  border: 1px solid rgba(226, 232, 240, 0.95) !important;
  box-shadow:
    0 24px 48px rgba(15, 23, 42, 0.16),
    0 0 0 1px rgba(255, 255, 255, 0.85) inset !important;
  backdrop-filter: blur(22px) saturate(1.35);
  -webkit-backdrop-filter: blur(22px) saturate(1.35);
  transform: translateY(20px) scale(0.96);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition:
    transform 0.35s var(--chat-ease),
    opacity 0.3s ease,
    visibility 0.3s;
  z-index: 1001;
  font-family: var(--chat-font);
  color: var(--chat-text);
}

#chatContainer.open {
  transform: translateY(0) scale(1);
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

#chatContainer.maximized {
  top: 16px;
  left: 16px;
  right: 16px;
  bottom: 16px;
  width: auto !important;
  height: auto !important;
  max-width: calc(100% - 32px);
  max-height: calc(100% - 32px);
  border-radius: 32px;
  z-index: 1002;
}

#chatContainer .chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.98) !important;
  border-bottom: 1px solid rgba(148, 163, 184, 0.16) !important;
  flex-shrink: 0;
  box-shadow: 0 1px 0 rgba(79, 70, 229, 0.05);
}

#chatContainer .header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

#chatContainer .brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(79, 70, 229, 0.1), rgba(99, 102, 241, 0.18)) !important;
  color: var(--chat-primary) !important;
  display: grid;
  place-items: center;
  box-shadow:
    0 6px 16px rgba(79, 70, 229, 0.14),
    0 0 0 1px rgba(79, 70, 229, 0.12) !important;
  transition: transform 0.2s var(--chat-ease-spring);
  opacity: 1 !important;
}

#chatContainer .brand-mark:hover {
  transform: rotate(-6deg) scale(1.04);
}

#chatContainer .header-title {
  font-size: 15px;
  font-weight: 800;
  color: var(--chat-text) !important;
  letter-spacing: -0.02em;
}

#chatContainer .header-sub {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #16a34a !important;
  font-size: 12px;
  font-weight: 700;
}

#chatContainer .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 8px rgba(34, 197, 94, 0.55);
  animation: chat-pulse 2.5s ease-in-out infinite;
}

#chatContainer .header-actions {
  display: flex;
  gap: 6px;
}

#chatContainer .icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid rgba(203, 213, 225, 0.9) !important;
  border-radius: 12px;
  background: #ffffff !important;
  color: #0f172a !important;
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 1 !important;
  filter: none !important;
  transition: all 0.2s var(--chat-ease);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.04);
}

#chatContainer .icon-btn:hover {
  background: rgba(79, 70, 229, 0.06) !important;
  color: var(--chat-primary) !important;
  transform: translateY(-1px);
  border-color: rgba(79, 70, 229, 0.22) !important;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.08);
}

#chatContainer .icon-btn:active {
  transform: scale(0.94);
}

#chatContainer .icon-btn svg,
#chatContainer .icon-btn i,
#chatContainer .brand-mark svg,
#chatContainer .msg-avatar svg,
#chatContainer #sendBtn svg,
#chatToggle svg {
  display: block !important;
  color: currentColor !important;
  fill: none !important;
  stroke: currentColor !important;
  stroke-width: 2 !important;
  opacity: 1 !important;
}

#chatContainer .chat-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background:
    radial-gradient(ellipse 70% 40% at 50% 0%, rgba(79, 70, 229, 0.035) 0%, transparent 60%),
    radial-gradient(ellipse 50% 30% at 80% 100%, rgba(99, 102, 241, 0.025) 0%, transparent 50%);
}

#chatContainer .chat-messages {
  flex: 1;
  padding: 16px 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  direction: rtl;
}

#chatContainer .chat-messages::-webkit-scrollbar {
  width: 4px;
}

#chatContainer .chat-messages::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.35);
  border-radius: 99px;
}

#chatContainer .welcome {
  margin: auto;
  max-width: 270px;
  text-align: center;
  padding: 28px 22px;
  border-radius: 26px;
  background: rgba(255, 255, 255, 0.9) !important;
  border: 1px solid rgba(226, 232, 240, 0.95) !important;
  box-shadow:
    0 8px 24px rgba(15, 23, 42, 0.06),
    0 0 0 1px rgba(255,255,255,0.65) inset !important;
  backdrop-filter: blur(14px);
  animation: chat-fadeUp 0.5s var(--chat-ease) both;
  color: var(--chat-text);
}

#chatContainer .welcome .avatar {
  width: 72px;
  height: 72px;
  margin: 0 auto 14px;
  border-radius: 22px;
  background: linear-gradient(135deg, rgba(79, 70, 229, 0.08), rgba(99, 102, 241, 0.18)) !important;
  display: grid;
  place-items: center;
  color: var(--chat-primary) !important;
  box-shadow: 0 8px 20px rgba(79, 70, 229, 0.12);
}

#chatContainer .welcome h3,
#chatContainer .welcome p {
  color: inherit;
}

#chatContainer .msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  animation: chat-fadeUp 0.28s var(--chat-ease) both;
}

#chatContainer .msg-row.user {
  flex-direction: row-reverse;
}

#chatContainer .msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--chat-primary), var(--chat-primary-2)) !important;
  color: #ffffff !important;
  flex-shrink: 0;
  opacity: 1 !important;
  filter: none !important;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.24) !important;
}

#chatContainer .msg-row.bot .msg-avatar {
  background: rgba(79, 70, 229, 0.1) !important;
  color: var(--chat-primary) !important;
  border: 1px solid rgba(79, 70, 229, 0.14) !important;
  box-shadow: none !important;
}

#chatContainer .bubble-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 84%;
  min-width: 80px;
}

#chatContainer .msg-row.user .bubble-wrap {
  align-items: flex-end;
}

#chatContainer .bubble {
  padding: 10px 16px;
  border-radius: var(--chat-radius-lg);
  font-size: 13.5px;
  line-height: 1.75;
  word-break: break-word;
  white-space: pre-wrap;
  transition: box-shadow 0.2s;
  text-align: right;
  opacity: 1 !important;
  filter: none !important;
}

#chatContainer .bubble.user {
  background: linear-gradient(135deg, #4f46e5, #6366f1) !important;
  color: #ffffff !important;
  border-bottom-right-radius: 6px;
  box-shadow:
    0 10px 22px rgba(79, 70, 229, 0.28),
    0 0 0 1px rgba(255,255,255,0.16) inset !important;
}

#chatContainer .bubble.bot {
  background: #ffffff !important;
  color: #0f172a !important;
  border: 1px solid rgba(226, 232, 240, 0.95) !important;
  border-bottom-left-radius: 6px;
  box-shadow:
    0 4px 12px rgba(15, 23, 42, 0.06),
    0 0 0 1px rgba(255,255,255,0.7) inset !important;
  overflow-x: auto;
}

#chatContainer .bubble.bot a {
  color: var(--chat-primary) !important;
  text-decoration: underline;
  text-decoration-color: rgba(79, 70, 229, 0.4);
  text-underline-offset: 2px;
}

#chatContainer .source-card {
  margin-top: 6px;
  padding: 8px 12px;
  background: rgba(79, 70, 229, 0.05) !important;
  border-right: 3px solid var(--chat-primary);
  border-radius: 12px;
  font-size: 11px;
  color: var(--chat-muted) !important;
  direction: rtl;
}

#chatContainer .source-title {
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--chat-primary-3) !important;
  font-size: 12px;
}

#chatContainer .source-card a {
  color: var(--chat-primary) !important;
  text-decoration: none;
  font-weight: 600;
}

#chatContainer .msg-time {
  font-size: 10px;
  color: var(--chat-muted-2) !important;
  padding: 0 4px;
}

#chatContainer .copy-msg-btn {
  align-self: flex-start;
  background: transparent;
  border: none;
  color: var(--chat-muted-2) !important;
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 20px;
  transition: all 0.2s;
  width: fit-content;
  font-family: var(--chat-font);
}

#chatContainer .copy-msg-btn:hover {
  background: rgba(79, 70, 229, 0.08);
  color: var(--chat-primary) !important;
}

#chatContainer .typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 12px 16px;
  background: #ffffff !important;
  border: 1px solid rgba(226, 232, 240, 0.95) !important;
  border-radius: var(--chat-radius-lg);
  border-bottom-left-radius: 6px;
  box-shadow: var(--chat-shadow-sm);
}

#chatContainer .typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--chat-primary), var(--chat-primary-2));
  animation: chat-bounce 1.3s ease-in-out infinite;
}

#chatContainer .suggestions {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  overflow-x: auto;
  scrollbar-width: none;
  border-top: 1px solid rgba(148, 163, 184, 0.08);
  background: rgba(255, 255, 255, 0.72) !important;
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}

#chatContainer .suggestions::-webkit-scrollbar {
  display: none;
}

#chatContainer .chip {
  white-space: nowrap;
  border: 1px solid rgba(79, 70, 229, 0.14) !important;
  background: rgba(79, 70, 229, 0.05) !important;
  color: var(--chat-primary-3) !important;
  padding: 6px 14px;
  border-radius: 100px;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s var(--chat-ease);
  font-family: var(--chat-font);
}

#chatContainer .chip:hover {
  background: rgba(79, 70, 229, 0.1) !important;
  transform: translateY(-1px);
  border-color: rgba(79, 70, 229, 0.24) !important;
  box-shadow: 0 4px 10px rgba(79, 70, 229, 0.08);
}

/* ---------- ورودی ---------- */
#chatContainer .input-area {
  padding: 10px 12px 14px;
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  background: rgba(255, 255, 255, 0.98) !important;
  border-top: 1px solid rgba(148, 163, 184, 0.12) !important;
  flex-shrink: 0;
}

#chatContainer .input-shell {
  display: flex;
  flex-direction: row-reverse;
  align-items: flex-end;
  gap: 8px;
  padding: 8px;
  border-radius: 24px;
  background: #ffffff !important;
  border: 1.5px solid rgba(203, 213, 225, 0.9) !important;
  transition: all 0.25s ease;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.05) !important;
}

#chatContainer .input-shell:focus-within {
  border-color: var(--chat-primary) !important;
  box-shadow:
    0 0 0 3px rgba(79, 70, 229, 0.1),
    0 4px 16px rgba(79, 70, 229, 0.08) !important;
}

#chatContainer #userInput {
  flex: 1 1 auto;
  min-width: 0;
  margin: 0;
  padding: 8px 6px;
  border: none !important;
  outline: none !important;
  box-shadow: none !important;
  resize: none;
  background: transparent !important;
  font-size: 14px;
  line-height: 1.55;
  max-height: 120px;
  color: #0f172a !important;
  caret-color: var(--chat-primary);
  font-family: var(--chat-font);
  direction: rtl;
  text-align: right;
  opacity: 1 !important;
}

#chatContainer #userInput::placeholder {
  color: #475569 !important;
  opacity: 1 !important;
}

#chatContainer #sendBtn {
  flex: 0 0 40px;
  width: 40px;
  min-width: 40px;
  height: 40px;
  min-height: 40px;
  margin: 0;
  align-self: flex-end;
  border-radius: 14px;
  background: linear-gradient(135deg, #4f46e5, #6366f1) !important;
  border: none !important;
  color: #ffffff !important;
  display: grid !important;
  place-items: center !important;
  cursor: pointer;
  opacity: 1 !important;
  visibility: visible !important;
  filter: none !important;
  transition: all 0.2s var(--chat-ease-spring);
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.3) !important;
}

#chatContainer #sendBtn:hover {
  filter: brightness(1.08) !important;
  transform: scale(1.05);
  box-shadow: 0 8px 18px rgba(79, 70, 229, 0.36) !important;
}

#chatContainer #sendBtn:active {
  transform: scale(0.94);
  transition-duration: 0.08s;
}

#chatContainer #sendBtn:disabled {
  opacity: 0.42 !important;
  cursor: not-allowed;
  transform: none;
  box-shadow: none !important;
  background: #94a3b8 !important;
  color: #ffffff !important;
}

#chatContainer #sendBtn svg,
#chatContainer #sendBtn i {
  width: 18px !important;
  height: 18px !important;
  display: block !important;
  color: #ffffff !important;
  fill: none !important;
  stroke: currentColor !important;
  stroke-width: 2 !important;
  opacity: 1 !important;
  flex-shrink: 0;
}

/* ---------- انیمیشن‌ها ---------- */
@keyframes chat-pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
    box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.4);
  }
  50% {
    transform: scale(0.88);
    opacity: 0.65;
    box-shadow: 0 0 0 4px rgba(34, 197, 94, 0);
  }
}

@keyframes chat-bounce {
  0%, 70%, 100% {
    transform: translateY(0) scale(1);
  }
  35% {
    transform: translateY(-7px) scale(0.9);
  }
}

@keyframes chat-fadeUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes chat-slideUp {
  from {
    transform: translateY(100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (max-width: 900px) {
  #chatContainer {
    width: min(440px, calc(100vw - 24px));
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    left: 12px;
  }

  #chatToggle {
    left: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }
}

@media (max-width: 560px) {
  #chatContainer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    height: 92dvh;
    max-height: 92dvh;
    border-radius: 24px 24px 0 0;
    transform: translateY(100%);
    opacity: 0;
    visibility: hidden;
  }

  #chatContainer.open {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
    animation: chat-slideUp 0.32s var(--chat-ease) both;
  }

  #chatContainer.maximized {
    top: 0;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }

  #chatContainer::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    background: rgba(148, 163, 184, 0.35);
    border-radius: 99px;
    margin: 10px auto 0;
    flex-shrink: 0;
  }

  #chatContainer .chat-header {
    padding: 10px 14px 12px;
    margin-top: -4px;
  }

  #chatContainer .header-title { font-size: 14px; }
  #chatContainer .brand-mark { width: 40px; height: 40px; border-radius: 14px; }
  #chatContainer .msg-avatar { width: 28px; height: 28px; border-radius: 10px; }
  #chatContainer .bubble-wrap { max-width: 90%; }
  #chatContainer .bubble { padding: 9px 13px; font-size: 13px; }
  #chatContainer .suggestions { padding: 8px 10px; }
  #chatContainer .chip { font-size: 12px; padding: 6px 10px; }

  #chatContainer .input-area {
    padding: 8px 10px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  #chatToggle {
    left: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    width: 56px;
    height: 56px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #chatContainer .status-dot,
  #chatContainer .typing span {
    animation: none;
  }

  #chatWidget *,
  #chatWidget *::before,
  #chatWidget *::after,
  #chatToggle,
  #chatContainer,
  .chat-backdrop {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}