/* SibLearn Chat Widget — Modern Redesign 2026 */

/* ── Variables ───────────────────────────────────────── */
:root {
  --cw-primary:       #7c3aed;
  --cw-primary-dark:  #5b21b6;
  --cw-accent:        #a855f7;
  --cw-header-bg:     linear-gradient(135deg, #4c1d95 0%, #7c3aed 60%, #9333ea 100%);
  --cw-send-btn-bg:   linear-gradient(135deg, #7c3aed, #a855f7);
  --cw-bubble-out:    linear-gradient(135deg, #6d28d9 0%, #7c3aed 100%);
  --cw-bubble-in:     #1e1b2e;
  --cw-chat-bg:       #13111e;
  --cw-panel-bg:      #1a1727;
  --cw-input-bg:      #211e30;
  --cw-input-bg-focus:#26203e;
  --cw-text:          #ece9f7;
  --cw-text-muted:    #8b85a8;
  --cw-time:          #6b6689;
  --cw-border:        rgba(255,255,255,.06);
  --cw-chip-bg:       rgba(124,58,237,.15);
  --cw-chip-border:   rgba(168,85,247,.3);
  --cw-sep-bg:        rgba(255,255,255,.05);
  --cw-shadow:        0 24px 64px -8px rgba(0,0,0,.7), 0 0 0 1px rgba(124,58,237,.15);
  --cw-radius:        20px;
  --cw-font:          'Vazirmatn', 'IRANSans', Tahoma, sans-serif;
  --cw-fab-size:      58px;
}

/* ── Light theme ─────────────────────────────────────── */
.cw-theme-light {
  --cw-chat-bg:       #f4f2fb;
  --cw-panel-bg:      #ffffff;
  --cw-bubble-in:     #ffffff;
  --cw-input-bg:      #ede9f8;
  --cw-input-bg-focus:#e6e0f6;
  --cw-text:          #1e1832;
  --cw-text-muted:    #6b6589;
  --cw-time:          #9b95b8;
  --cw-border:        rgba(20,10,40,.07);
  --cw-chip-bg:       rgba(124,58,237,.08);
  --cw-chip-border:   rgba(124,58,237,.25);
  --cw-sep-bg:        rgba(20,10,40,.05);
  --cw-shadow:        0 20px 56px -8px rgba(60,30,100,.25), 0 0 0 1px rgba(124,58,237,.12);
}

/* ── FAB ─────────────────────────────────────────────── */
.chat-fab {
  position: fixed;
  bottom: 24px;
  left: 24px;
  width: var(--cw-fab-size);
  height: var(--cw-fab-size);
  border-radius: 50%;
  background: var(--cw-send-btn-bg);
  color: #fff;
  border: none;
  cursor: pointer;
  z-index: 99999;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 24px rgba(124,58,237,.55), 0 2px 8px rgba(0,0,0,.3);
  transition: transform .3s cubic-bezier(.34,1.56,.64,1), box-shadow .3s;
  animation: fabEnter .5s cubic-bezier(.34,1.56,.64,1) both;
}
.chat-fab:hover {
  transform: scale(1.1);
  box-shadow: 0 10px 32px rgba(124,58,237,.7), 0 2px 8px rgba(0,0,0,.3);
}
.chat-fab.is-open { display: none !important; }

@keyframes fabEnter {
  from { transform: translateY(60px) scale(.3); opacity: 0; }
  to   { transform: translateY(0) scale(1);    opacity: 1; }
}

.chat-fab-icon { display: flex; align-items: center; justify-content: center; }

/* ── Badge ───────────────────────────────────────────── */
.chat-badge {
  position: absolute;
  top: -4px;
  left: -4px;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  border: 2px solid #0d0d18;
  display: none;
}
.chat-badge.active,
.chat-badge.has-unread { display: flex; }

/* ── Widget container ────────────────────────────────── */
.chat-widget {
  position: fixed;
  bottom: 96px;
  left: 24px;
  width: 370px;
  max-width: calc(100vw - 32px);
  height: 580px;
  max-height: calc(100vh - 120px);
  background: var(--cw-panel-bg);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 99998;
  font-family: var(--cw-font);
  direction: rtl;
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px) scale(.97);
  transform-origin: bottom left;
  transition: opacity .25s ease, transform .25s cubic-bezier(.34,1.2,.64,1);
  contain: layout;         /* جلوگیری از overflow خارج از widget */
}
.chat-widget.is-open {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0) scale(1);
}
@keyframes widgetOpen {
  from { opacity: 0; transform: translateY(20px) scale(.96); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ── Header ──────────────────────────────────────────── */
.chat-header {
  background: var(--cw-header-bg);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  position: relative;
  /* در RTL: آیتم اول سمت راست، آخری سمت چپ */
}
.chat-header::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 1px;
  background: rgba(255,255,255,.08);
}

.chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,.18);
  border: 2px solid rgba(255,255,255,.3);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  order: 1;       /* در RTL: سمت راست */
}
.chat-header-avatar svg { width: 20px; height: 20px; }

.chat-header-info {
  flex: 1;
  min-width: 0;
  order: 2;
  text-align: right;
}
.chat-header-title {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-header-status {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  font-size: 12px;
  color: rgba(255,255,255,.8);
  margin-top: 2px;
}
.chat-header-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #6b7280;
  flex-shrink: 0;
  order: 2;
}
.chat-header-status-dot.online {
  background: #34d399;
  box-shadow: 0 0 0 2px rgba(52,211,153,.3);
  animation: onlinePulse 2s infinite;
}
.chat-header-status span:last-child { order: 1; }

@keyframes onlinePulse {
  0%,100% { box-shadow: 0 0 0 2px rgba(52,211,153,.3); }
  50%      { box-shadow: 0 0 0 4px rgba(52,211,153,.15); }
}

.chat-header-actions {
  flex-shrink: 0;
  order: 3;       /* در RTL: سمت چپ */
}
.chat-header-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s;
}
.chat-header-btn:hover { background: rgba(255,255,255,.25); }

/* ── Messages area ───────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 16px 10px;
  background: var(--cw-chat-bg);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  scroll-behavior: smooth;
}
/* همه فرزندان مستقیم عرض کامل بگیرن و جمع بشن */
.chat-messages > * { min-width: 0; max-width: 100%; }
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(124,58,237,.25);
  border-radius: 4px;
}

/* ── Date separator ──────────────────────────────────── */
.chat-date-sep {
  text-align: center;
  font-size: 11.5px;
  color: var(--cw-text-muted);
  margin: 8px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-date-sep::before,
.chat-date-sep::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--cw-sep-bg);
}

/* ── Welcome screen ──────────────────────────────────── */
.chat-welcome {
  text-align: center;
  padding: 24px 20px 12px;
  animation: bubbleIn .4s ease;
  width: 100%;
  box-sizing: border-box;
  overflow: hidden;
}
.chat-welcome-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6d28d9, #a855f7);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 14px;
  box-shadow: 0 6px 20px rgba(124,58,237,.4);
  color: #fff;
  flex-shrink: 0;
}
.chat-welcome-icon svg { width: 28px; height: 28px; }
.chat-welcome h4 {
  margin: 0 0 6px;
  font-size: 17px;
  font-weight: 700;
  color: var(--cw-text);
}
.chat-welcome p {
  margin: 0;
  font-size: 13px;
  color: var(--cw-text-muted);
  line-height: 1.6;
}

/* ── Quick replies ───────────────────────────────────── */
.chat-quick {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  padding: 4px 20px 16px;
  animation: bubbleIn .3s ease;
  width: 100%;
  box-sizing: border-box;
}
.chat-quick-chip {
  background: var(--cw-chip-bg);
  border: 1px solid var(--cw-chip-border);
  color: var(--cw-accent);
  border-radius: 20px;
  padding: 7px 14px;
  font-size: 13px;
  font-family: var(--cw-font);
  cursor: pointer;
  transition: background .2s, border-color .2s, color .2s;
  white-space: nowrap;
}
.chat-quick-chip:hover {
  background: rgba(168,85,247,.25);
  border-color: var(--cw-accent);
  color: #fff;
}

/* ── Pre-chat form ───────────────────────────────────── */
.chat-prechat {
  padding: 10px 16px 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: bubbleIn .3s ease;
}
.chat-prechat-field label {
  display: block;
  font-size: 12px;
  color: var(--cw-text-muted);
  margin-bottom: 5px;
}
.chat-prechat-field input {
  width: 100%;
  box-sizing: border-box;
  font-family: var(--cw-font);
  font-size: 14px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--cw-chip-border);
  background: var(--cw-input-bg);
  color: var(--cw-text);
  outline: none;
  transition: border-color .2s, background .2s;
}
.chat-prechat-field input:focus {
  border-color: var(--cw-accent);
  background: var(--cw-input-bg-focus);
}
.chat-prechat-submit {
  background: var(--cw-send-btn-bg);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 11px;
  font-size: 14px;
  font-family: var(--cw-font);
  font-weight: 600;
  cursor: pointer;
  transition: opacity .2s;
}
.chat-prechat-submit:hover { opacity: .88; }

/* ── Messages ────────────────────────────────────────── */
.chat-msg {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 82%;
  animation: bubbleIn .25s ease;
}
/* visitor = کاربر = سمت راست (RTL: flex-start) */
.chat-msg.visitor,
.chat-msg.out {
  align-self: flex-start;
  flex-direction: row;
}
/* admin = پشتیبان = سمت چپ (RTL: flex-end) */
.chat-msg.admin,
.chat-msg.in {
  align-self: flex-end;
  flex-direction: row-reverse;
}

/* avatar پشتیبان */
.chat-msg.admin .msg-avatar,
.chat-msg.in .msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6d28d9, #9333ea);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  align-self: flex-end;
}
.chat-msg.admin .msg-avatar svg,
.chat-msg.in .msg-avatar svg { width: 14px; height: 14px; }

/* avatar کاربر: مخفی */
.chat-msg.visitor .msg-avatar,
.chat-msg.out .msg-avatar { display: none; }

/* وقتی فرستنده تکراری: avatar invisible نه hidden */
.chat-msg.no-avatar .msg-avatar { visibility: hidden; }

/* msg-body wrapper (اگه JS اضافه کنه) */
.msg-body { display: contents; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
}
.chat-msg.visitor .msg-bubble,
.chat-msg.out .msg-bubble {
  background: var(--cw-bubble-out);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.chat-msg.admin .msg-bubble,
.chat-msg.in .msg-bubble {
  background: var(--cw-bubble-in);
  color: var(--cw-text);
  border: 1px solid var(--cw-border);
  border-bottom-left-radius: 5px;
}

.msg-text { display: block; }
.msg-text img, .msg-image {
  max-width: 100%;
  border-radius: 10px;
  display: block;
  margin-top: 4px;
  cursor: pointer;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 4px;
}
.chat-msg.visitor .msg-meta,
.chat-msg.out .msg-meta { justify-content: flex-start; }
.chat-msg.admin .msg-meta,
.chat-msg.in  .msg-meta  { justify-content: flex-end; }

.msg-time { font-size: 11px; color: var(--cw-time); }
.msg-ticks { font-size: 11px; color: rgba(255,255,255,.45); }
.msg-ticks.read { color: #34d399; }
.msg-failed {
  font-size: 11px;
  color: #f87171;
  display: flex;
  align-items: center;
  gap: 3px;
  cursor: pointer;
}

/* ── Typing indicator ────────────────────────────────── */
.chat-typing {
  display: none;
  align-items: flex-end;
  gap: 8px;
  align-self: flex-end;   /* RTL: سمت چپ */
  flex-direction: row-reverse;
  margin-bottom: 4px;
}
.chat-typing.active { display: flex; }

.chat-typing .msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6d28d9, #9333ea);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}
.chat-typing .msg-avatar svg { width: 14px; height: 14px; }

.chat-typing-bubble {
  background: var(--cw-bubble-in);
  border: 1px solid var(--cw-border);
  border-radius: 18px;
  border-bottom-left-radius: 5px;
  padding: 12px 16px;
  display: flex;
  gap: 5px;
  align-items: center;
}
.chat-typing-bubble span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cw-text-muted);
  display: block;
  animation: typingDot 1.2s infinite ease-in-out;
}
.chat-typing-bubble span:nth-child(2) { animation-delay: .15s; }
.chat-typing-bubble span:nth-child(3) { animation-delay: .30s; }

@keyframes typingDot {
  0%,60%,100% { transform: translateY(0); opacity: .4; }
  30%          { transform: translateY(-5px); opacity: 1; }
}

/* ── Emoji panel ─────────────────────────────────────── */
.chat-emoji-panel {
  position: absolute;
  bottom: 70px;
  left: 12px;
  right: 12px;
  background: var(--cw-panel-bg);
  border: 1px solid var(--cw-border);
  border-radius: 16px;
  padding: 10px;
  display: none;
  flex-wrap: wrap;
  gap: 4px;
  z-index: 10;
  max-height: 160px;
  overflow-y: auto;
  box-shadow: 0 -4px 20px rgba(0,0,0,.3);
}
.chat-emoji-panel.active { display: flex; }
.chat-emoji-panel button {
  font-size: 20px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  transition: background .15s;
  line-height: 1;
}
.chat-emoji-panel button:hover { background: var(--cw-chip-bg); }

/* ── Upload preview ──────────────────────────────────── */
.chat-upload-preview {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--cw-input-bg);
  border-top: 1px solid var(--cw-border);
}
.chat-upload-preview.active { display: flex; }
.chat-upload-preview img#upThumb {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--cw-chip-border);
}
.chat-upload-preview .up-name {
  flex: 1;
  font-size: 12px;
  color: var(--cw-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-upload-preview .up-remove {
  background: rgba(239,68,68,.15);
  border: none;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  color: #f87171;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  flex-shrink: 0;
}

/* ── Input area ──────────────────────────────────────── */
.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px 14px 12px;
  background: var(--cw-panel-bg);
  border-top: 1px solid var(--cw-border);
  flex-shrink: 0;
}

.chat-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--cw-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color .2s, background .2s;
}
.chat-icon-btn:hover {
  color: var(--cw-accent);
  background: var(--cw-chip-bg);
}

.chat-input {
  flex: 1;
  min-width: 0;
  background: var(--cw-input-bg);
  border: 1.5px solid var(--cw-border);
  border-radius: 20px;
  color: var(--cw-text);
  font-family: var(--cw-font);
  font-size: 14px;
  line-height: 1.4;
  padding: 9px 16px;
  resize: none;
  outline: none;
  max-height: 90px;
  overflow-y: auto;
  transition: border-color .2s, background .2s;
  direction: rtl;
}
.chat-input::placeholder { color: var(--cw-text-muted); }
.chat-input:focus {
  border-color: rgba(124,58,237,.5);
  background: var(--cw-input-bg-focus);
}

.chat-send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--cw-send-btn-bg);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity .2s, transform .2s;
  box-shadow: 0 4px 14px rgba(124,58,237,.4);
}
.chat-send-btn:disabled {
  opacity: .4;
  cursor: not-allowed;
  box-shadow: none;
}
.chat-send-btn:not(:disabled):hover {
  opacity: .9;
  transform: scale(1.08);
}
.chat-send-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transform: none;  /* جهت پیش‌فرض آیکون: به سمت راست‑بالا */
}

/* ── Note / toast ────────────────────────────────────── */
.chat-note {
  position: absolute;
  bottom: 68px;
  left: 12px;
  right: 12px;
  background: rgba(239,68,68,.9);
  color: #fff;
  font-size: 12px;
  padding: 8px 14px;
  border-radius: 10px;
  text-align: center;
  display: none;
  z-index: 6;
  animation: bubbleIn .2s ease;
}
.chat-note.active { display: block; }

/* ── Animations ──────────────────────────────────────── */
@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Responsive mobile ───────────────────────────────── */
@media (max-width: 600px) {
  .chat-widget {
    top: 0; left: 0; right: 0; bottom: 0;
    width: auto;
    max-width: none;
    height: auto;
    max-height: none;
    border-radius: 0;
    transform-origin: center bottom;
  }
  .chat-fab {
    bottom: 16px;
    left: 16px;
    width: 52px;
    height: 52px;
  }
  .chat-header {
    padding: 12px 14px;
    padding-top: max(12px, env(safe-area-inset-top));
  }
  .chat-messages { padding: 12px 10px 6px; }
  .chat-input-area {
    padding: 8px 10px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
  .chat-input { font-size: 15px; }
  .chat-emoji-panel {
    bottom: calc(62px + env(safe-area-inset-bottom));
  }
  .chat-msg { max-width: 88%; }
}

@media (max-width: 360px) {
  .chat-header-title { font-size: 14px; }
  .chat-quick-chip { font-size: 12px; padding: 6px 11px; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-fab, .chat-welcome-icon, .msg-bubble,
  .chat-header-status-dot.online { animation: none !important; }
}

/* ═══════════════════════════════════════════════════════
   SPACING GUARD — بازگردانی margin/padding
   برخی صفحات (body.page-index/about/blog/...) قانون reset
   `body.page-X * { margin:0; padding:0 }` دارن که با
   specificity (0,1,1) تمام فاصله‌های ویجت رو صفر می‌کنه.
   این بلوک با id-specificity (بالاتر) فاصله‌ها رو برمی‌گردونه.
   ═══════════════════════════════════════════════════════ */
#chatFab .chat-badge { padding: 0 5px; }

#chatWidget .chat-header { padding: 12px 16px; }
#chatWidget .chat-header-status { margin-top: 2px; }
#chatWidget .chat-messages { padding: 20px 16px 10px; }
#chatWidget .chat-date-sep { margin: 8px 0; }

#chatWidget .chat-welcome { padding: 24px 20px 12px; }
#chatWidget .chat-welcome-icon { margin: 0 auto 14px; }
#chatWidget .chat-welcome h4 { margin: 0 0 6px; }

#chatWidget .chat-quick { padding: 4px 20px 16px; }
#chatWidget .chat-quick-chip { padding: 7px 14px; }

#chatWidget .chat-prechat { padding: 10px 16px 6px; }
#chatWidget .chat-prechat-field label { margin-bottom: 5px; }
#chatWidget .chat-prechat-field input { padding: 10px 14px; }
#chatWidget .chat-prechat-submit { padding: 11px; }

#chatWidget .msg-bubble { padding: 10px 14px; }
#chatWidget .msg-text img,
#chatWidget .msg-image { margin-top: 4px; }
#chatWidget .msg-meta { padding: 0 4px; }

#chatWidget .chat-typing-bubble { padding: 12px 16px; }
#chatWidget .chat-emoji-panel { padding: 10px; }
#chatWidget .chat-emoji-panel button { padding: 4px; }
#chatWidget .chat-upload-preview { padding: 8px 14px; }

#chatWidget .chat-input-area { padding: 10px 14px 12px; }
#chatWidget .chat-input { padding: 9px 16px; }
#chatWidget .chat-note { padding: 8px 14px; }

/* موبایل: paddingهای فشرده‌تر با id-specificity */
@media (max-width: 600px) {
  #chatWidget .chat-header { padding: 12px 14px; padding-top: max(12px, env(safe-area-inset-top)); }
  #chatWidget .chat-messages { padding: 12px 10px 6px; }
  #chatWidget .chat-input-area { padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
}
@media (max-width: 360px) {
  #chatWidget .chat-quick-chip { padding: 6px 11px; }
}
