/**
 * Chat Widget Styles
 * Persian RTL Chat Widget with full customization support
 */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

:root {
  --primary-color: #4f46e5;
  --secondary-color: #7c3aed;
  --background-color: #ffffff;
  --widget-background: #f8fafc;
  --font-color: #1e293b;
  --user-bubble-color: #4f46e5;
  --bot-bubble-color: #f1f5f9;
  --font-family: Vazir, 'Vazirmatn', sans-serif;
  --border-color: #e2e8f0;
  --send-button-color: #4f46e5;
  --floating-button-color: #ffffff;
  --floating-button-bg-color: #4f46e5;
  --header-color: #4f46e5;

  --kb: 0px;
  --composer-height: 60px;
  --chat-base-font-size: 15px;
  --line-height: 1.9;
}

#chat-widget-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  z-index: 2147483647;
  direction: rtl;
  isolation: isolate;
  font-family: var(--font-family);
  opacity: 1;
  visibility: visible;
}

/* Fullscreen mode - اصلی */
#chat-widget-container.fullscreen-mode {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
}

#chat-widget-container.fullscreen-mode #chat-window {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 100% !important;
  max-height: 100% !important;
  border-radius: 0 !important;
  box-shadow: none !important;
  transform: none !important;
  opacity: 1 !important;
  visibility: visible !important;
}

#chat-widget-container.fullscreen-mode #chat-toggle-button {
  display: none !important;
}

/* Fullscreen mode - basic styles */
#chat-widget-container.fullscreen-mode .chat-header {
  border-radius: 0 !important;
}

#chat-widget-container.fullscreen-mode .chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  padding-bottom: calc(var(--composer-height) + var(--kb));
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

#chat-widget-container.fullscreen-mode .chat-form {
  position: fixed !important;
  left: 0 !important;
  right: 0 !important;
  bottom: var(--kb) !important;
  border-radius: 0 !important;
  padding: 15px 20px;
  padding-bottom: max(env(safe-area-inset-bottom), 15px);
  border-top: 1px solid var(--border-color);
  background-color: var(--background-color);
  will-change: bottom;
  transform: translateZ(0);
  z-index: 100;
}

/* Modern template fullscreen mode */
#chat-widget-container.modern-template.fullscreen-mode .top-nav {
  border-radius: 0 !important;
  flex-shrink: 0;
}

#chat-widget-container.modern-template.fullscreen-mode #chat-window {
  display: flex !important;
  flex-direction: column !important;
}

#chat-widget-container.modern-template.fullscreen-mode .chat-messages {
  flex: 1 1 auto !important;
  min-height: 0 !important;
  padding: 20px 15px calc(var(--composer-height) + var(--kb) - 5px) 15px !important;
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior: contain !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
}

#chat-widget-container.modern-template.fullscreen-mode .chat-form {
  position: fixed !important;
  left: 0 !important;
  right: 0 !important;
  bottom: var(--kb) !important;
  z-index: 100 !important;
  padding-bottom: max(env(safe-area-inset-bottom), 10px) !important;
  will-change: bottom !important;
  transform: translateZ(0) !important;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.5) !important;
  flex-shrink: 0 !important;
}

#chat-toggle-button {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 20px rgba(0, 0, 0, .15), 0 3px 6px rgba(0, 0, 0, .1);
  transition: all .3s ease;
  animation: pulse 2s infinite;
  position: relative;
  overflow: hidden;
}

#chat-toggle-button:hover {
  transform: translateY(-5px) scale(1.1);
  box-shadow: 0 15px 25px rgba(0, 0, 0, .2), 0 5px 10px rgba(0, 0, 0, .15);
  animation-play-state: paused;
}

#chat-toggle-button svg {
  width: 32px;
  height: 32px;
  color: var(--floating-button-color);
  position: absolute;
  transition: transform .3s ease-in-out, opacity .3s ease-in-out;
}

#chat-icon-open {
  transform: scale(1);
  opacity: 1;
}

#chat-icon-close {
  transform: scale(0);
  opacity: 0;
}

#chat-toggle-button.open #chat-icon-open {
  transform: scale(0);
  opacity: 0;
}

#chat-toggle-button.open #chat-icon-close {
  transform: scale(1);
  opacity: 1;
}

@keyframes pulse {
  0% {
    transform: scale(.95);
    box-shadow: 0 0 0 0 rgba(79, 70, 229, .7);
  }

  70% {
    transform: scale(1);
    box-shadow: 0 0 0 10px rgba(79, 70, 229, 0);
  }

  100% {
    transform: scale(.95);
    box-shadow: 0 0 0 0 rgba(79, 70, 229, 0);
  }
}

/* Notification pulse for inactivity message */
#chat-toggle-button.notification-pulse {
  animation: notificationPulse 0.8s ease-in-out 3;
}

@keyframes notificationPulse {

  0%,
  100% {
    transform: scale(1);
    box-shadow: 0 4px 20px rgba(239, 68, 68, 0.4);
  }

  50% {
    transform: scale(1.1);
    box-shadow: 0 8px 30px rgba(239, 68, 68, 0.6), 0 0 0 8px rgba(239, 68, 68, 0.2);
  }
}

#chat-window {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 445px;
  max-width: 90vw;
  height: 625px;
  max-height: 85vh;
  background-color: var(--widget-background);
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform .4s cubic-bezier(.165, .84, .44, 1), opacity .4s ease;
  transform-origin: bottom right;
  opacity: 0;
  visibility: hidden;
  transform: scale(.5) translateY(20px);
  font-size: var(--chat-base-font-size) !important;
  line-height: var(--line-height) !important;
}

#chat-window * {
  font-size: inherit;
  line-height: inherit;
}

#chat-window .message {
  padding: 14px 18px !important;
}

#chat-window.open {
  opacity: 1;
  visibility: visible;
  transform: scale(1) translateY(0);
}

.chat-header {
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  color: #fff;
  padding: 12px 20px;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, .1);
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.chat-header a {
  text-decoration: none;
  line-height: 0;
  color: inherit;
}

.header-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 15px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}

.header-avatar svg {
  width: 44px;
  height: 44px;
}

.header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.header-info {
  flex: 1;
}

.header-info h3 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.header-info p {
  margin: 2px 0 0;
  font-size: .8rem;
  opacity: 1;
  display: flex;
  align-items: center;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.status-dot {
  width: 8px;
  height: 8px;
  background-color: #22c55e;
  border-radius: 50%;
  margin-left: 6px;
  animation: pulse-dot 1.5s infinite;
}

@keyframes pulse-dot {

  0%,
  100% {
    opacity: 1;
  }

  50% {
    opacity: 0.5;
  }
}

.chat-messages {
  flex-grow: 1;
  padding: 15px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.message {
  max-width: 85%;
  padding: 10px 15px;
  border-radius: 18px;
  margin-bottom: 5px;
  line-height: 1.7;
  animation: fadeIn .5s ease;
  position: relative;
  word-break: break-word;
  overflow-wrap: anywhere;
  text-align: right;
}

.message-container {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 4px 8px;
  margin-bottom: 10px;
}

.user-message-container {
  justify-content: flex-end;
}

.bot-message-container {
  justify-content: flex-start;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-message {
  background-color: var(--user-bubble-color);
  color: #fff;
  border-bottom-right-radius: 4px;
  align-self: flex-end;
  text-align: right;
}

.bot-message {
  background-color: var(--bot-bubble-color);
  color: var(--font-color);
  border-bottom-left-radius: 4px;
  align-self: flex-start;
}

.bot-message table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin: 10px 0;
  background-color: white;
}

.bot-message th,
.bot-message td {
  border: 1px solid #d1d5db;
  padding: 8px;
  text-align: right;
}

.bot-message th {
  background-color: #e5e7eb;
}

.bot-message ul,
.bot-message ol {
  margin: .25em 0;
  padding: 0 1rem 0 0;
  list-style: disc inside;
}

.bot-message li {
  margin: .2em 0;
  line-height: 1.6;
}

.bot-message p {
  margin-top: 0.25em;
  margin-bottom: 0.25em;
}

.bot-message p:first-child {
  margin-top: 0;
}

.bot-message p:last-child {
  margin-bottom: 0;
}

.bot-message a {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 500;
  word-break: normal;
  overflow-wrap: anywhere;
}

.bot-message a:hover {
  text-decoration: underline;
}

#typing-indicator {
  display: none;
  align-items: center;
  margin: 0 0 8px 10px;
}

#typing-indicator .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #cbd5e1;
  margin: 0 2px;
  animation: typing 1.5s infinite ease-in-out;
}

#typing-indicator .dot:nth-child(2) {
  animation-delay: .2s;
}

#typing-indicator .dot:nth-child(3) {
  animation-delay: .4s;
}

@keyframes typing {
  0% {
    transform: translateY(0px);
  }

  28% {
    transform: translateY(-5px);
  }

  44% {
    transform: translateY(0px);
  }
}

.chat-form {
  display: flex;
  align-items: center;
  padding: 15px;
  border-top: 1px solid var(--border-color);
  background-color: var(--background-color);
  flex-shrink: 0;
  gap: 8px;
}

input,
textarea {
  font-size: 16px;
}

#chat-input {
  font-size: 16px !important;
  line-height: 1.5;
  flex-grow: 1;
  border: 1px solid #cbd5e1;
  border-radius: 20px;
  padding: 10px 15px;
  font-family: var(--font-family);
  transition: border-color .3s, box-shadow .3s;
}

#chat-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .2);
}

#send-button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  color: var(--send-button-color);
  transition: transform .2s, color .2s, background-color .2s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
}

#send-button:hover:not(:disabled) {
  transform: scale(1.1);
  background-color: #f1f5f9;
}

#send-button:disabled {
  color: #94a3b8;
  cursor: not-allowed;
  transform: scale(1);
  background-color: transparent;
}

#send-button svg {
  width: 26px;
  height: 26px;
}

#mobile-close-button {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px;
  margin-right: auto;
  color: white;
}

#mobile-close-button svg {
  width: 28px;
  height: 28px;
}

/* Network Status Indicator */
.network-status {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background-color: #f59e0b;
  color: white;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease;
  z-index: 1000;
}

.network-status.show {
  opacity: 1;
}

.network-status.offline {
  background-color: #ef4444;
}

.network-status.online {
  background-color: #22c55e;
}

/* Modern Template Overrides */
/* رنگ‌های دکمه شاتور و آیکون‌ها به صورت داینامیک از تنظیمات کاربر تولید می‌شوند */
/* لطفاً رنگ‌های ثابت را در اینجا تعریف نکنید */

/* Modern notification pulse */
.modern-template #chat-toggle-button.notification-pulse {
  animation: modernNotificationPulse 0.8s ease-in-out 3 !important;
}

@keyframes modernNotificationPulse {

  0%,
  100% {
    transform: scale(1);
    box-shadow: 0 4px 20px rgba(22, 24, 28, 0.6);
  }

  50% {
    transform: scale(1.1);
    box-shadow: 0 8px 30px rgba(22, 24, 28, 0.8), 0 0 0 8px rgba(231, 233, 234, 0.2);
  }
}

.modern-template .message-container {
  gap: 2px 4px !important;
}

.modern-template .user-message,
.modern-template .bot-message {
  font-family: var(--font-family) !important;
  font-size: var(--chat-base-font-size) !important;
  line-height: var(--line-height) !important;
}

.modern-template .bot-message-actions {
  display: flex !important;
  gap: 2px !important;
  margin-top: 0px !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
}

.modern-template .bot-message-actions .action-button {
  padding: 4px !important;
  margin: 0 !important;
  width: 28px;
  height: 28px;
  transition: color 0.2s ease, background 0.2s ease !important;
}

.modern-template .bot-message-actions .action-button.copied,
.modern-template .bot-message-actions .action-button.liked {
  color: #17c964 !important;
}

.modern-template .bot-message-actions .action-button.disliked {
  color: #f31260 !important;
}

.modern-template .bot-message-actions .action-button.copied:hover,
.modern-template .bot-message-actions .action-button.liked:hover,
.modern-template .bot-message-actions .action-button.disliked:hover {
  background: #1a1a1a !important;
}

.modern-template .chat-messages {
  flex: 1 1 auto !important;
  min-height: 0 !important;
  padding: 20px 15px 50px 15px !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  display: flex !important;
  flex-direction: column !important;
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior: contain !important;
}

.modern-template #typing-indicator {
  margin: 0 0 8px 10px !important;
}

.modern-template #typing-indicator .dot {
  background-color: #71767b !important;
}

.modern-template #chat-input {
  border: 1px solid #2a2a2a !important;
  background: #1a1a1a !important;
  color: #e7e9ea !important;
  font-family: var(--font-family) !important;
  font-size: var(--chat-base-font-size) !important;
  line-height: var(--line-height) !important;
}

.modern-template #chat-input:focus {
  outline: none !important;
  border-color: #3a3a3a !important;
  box-shadow: 0 0 0 2px rgba(58, 58, 58, 0.3) !important;
}

.modern-template #chat-input::placeholder {
  color: #5a5a5a !important;
  font-family: var(--font-family) !important;
  font-size: inherit !important;
  font-weight: inherit !important;
}

.modern-template #send-button {
  color: #71767b !important;
}

.modern-template #send-button:hover:not(:disabled) {
  background-color: #1a1a1a !important;
  color: #e7e9ea !important;
}

.modern-template #send-button:disabled {
  color: #71767b !important;
  opacity: 0.5;
}

/* Mobile Responsive Styles */
@media (max-width: 768px) {
  #chat-window.open {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
    box-shadow: none;
    transform: none;
    transition: opacity .3s ease;
    overflow: hidden;
  }

  .chat-header {
    border-top-left-radius: 0;
    border-top-right-radius: 0;
  }

  #chat-window.open #mobile-close-button {
    display: block;
  }

  #chat-window.open+#chat-toggle-button {
    display: none;
  }

  .chat-messages {
    height: 100%;
    padding-bottom: calc(var(--composer-height) + var(--kb));
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .chat-form {
    position: fixed;
    left: 0;
    right: 0;
    bottom: var(--kb);
    padding-bottom: max(env(safe-area-inset-bottom), 10px);
    border-top: 1px solid var(--border-color);
    background-color: var(--background-color);
    will-change: bottom;
    transform: translateZ(0);
  }

  /* Modern Template Mobile Fixes */
  .modern-template #chat-window {
    display: flex !important;
    flex-direction: column !important;
    height: 100vh !important;
    overflow: hidden !important;
  }

  .modern-template .top-nav {
    padding: 16px 16px !important;
    flex-shrink: 0 !important;
  }

  .modern-template .chat-messages {
    flex: 1 1 auto !important;
    height: 100% !important;
    min-height: 0 !important;
    padding: 20px 15px calc(var(--composer-height) + var(--kb) - 5px) 15px !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    box-sizing: border-box !important;
  }

  .modern-template .chat-form {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: var(--kb) !important;
    z-index: 100 !important;
    padding-bottom: max(env(safe-area-inset-bottom), 10px) !important;
    will-change: bottom !important;
    transform: translateZ(0) !important;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.5) !important;
    flex-shrink: 0 !important;
  }

  .modern-template #typing-indicator {
    margin-bottom: 10px !important;
  }

  .modern-template .message-container:last-of-type {
    margin-bottom: 15px !important;
  }

  .modern-template .message-container:has(#typing-indicator) {
    margin-bottom: 15px !important;
  }

  .modern-template .bot-message-container:last-of-type .bot-message-actions {
    margin-bottom: 8px !important;
  }
}

/* ============ Smart RTL/LTR Direction Support ============ */

/* LTR text styling */
.message.ltr-text {
  direction: ltr !important;
  text-align: left !important;
}

.message.rtl-text {
  direction: rtl !important;
  text-align: right !important;
}

/* Input field direction support */
#chat-input.ltr-text,
#chat-input[style*="direction: ltr"] {
  text-align: left !important;
}

#chat-input.rtl-text,
#chat-input[style*="direction: rtl"] {
  text-align: right !important;
}

/* Message container alignment based on text direction */
.user-message-container .user-message.ltr-text {
  border-bottom-left-radius: 4px !important;
  border-bottom-right-radius: 18px !important;
}

.user-message-container .user-message.rtl-text {
  border-bottom-right-radius: 4px !important;
  border-bottom-left-radius: 18px !important;
}

.bot-message-container .bot-message.ltr-text {
  border-bottom-right-radius: 4px !important;
  border-bottom-left-radius: 18px !important;
}

.bot-message-container .bot-message.rtl-text {
  border-bottom-left-radius: 4px !important;
  border-bottom-right-radius: 18px !important;
}

/* LTR lists styling */
.bot-message.ltr-text ul,
.bot-message.ltr-text ol {
  padding: 0 0 0 1rem !important;
  list-style-position: inside;
}

/* RTL lists styling (default) */
.bot-message.rtl-text ul,
.bot-message.rtl-text ol {
  padding: 0 1rem 0 0 !important;
  list-style-position: inside;
}

/* LTR table styling */
.bot-message.ltr-text th,
.bot-message.ltr-text td {
  text-align: left !important;
}

/* RTL table styling */
.bot-message.rtl-text th,
.bot-message.rtl-text td {
  text-align: right !important;
}

/* Modern template adjustments for LTR */
.modern-template .user-message.ltr-text {
  margin-left: auto;
  margin-right: 0;
}

.modern-template .bot-message.ltr-text {
  margin-right: auto;
  margin-left: 0;
}