/* ===== TOGGLE BUTTON ===== */
.chat-toggle-btn {
  position: fixed; bottom: 92px; left: 22px;
  z-index: 99998;
  width: 56px; height: 56px; border-radius: 50%;
  background: #16A34A; color: #fff; border: 0;
  display: grid; place-items: center; cursor: pointer;
  box-shadow: 0 8px 24px rgba(22,163,74,.42);
  transition: transform .25s ease, background .25s ease;
}
.chat-toggle-btn:hover { transform: scale(1.06); background: #15803D; }
.chat-toggle-btn.active { background: #0F3D2E; }
.chat-toggle-btn svg { pointer-events: none; width: 22px; height: 22px; }

.chat-toggle-badge {
  position: absolute; top: -4px; right: -4px;
  min-width: 20px; height: 20px; padding: 0 5px;
  background: #EF4444; color: #fff;
  border-radius: 10px; font-size: .68rem;
  font-weight: 700; display: grid; place-items: center;
  border: 2px solid #fff;
}

/* ===== DESKTOP PANEL ===== */
.chat-panel {
  position: fixed;
  top: 84px; bottom: 160px; left: 22px;
  z-index: 99999;
  width: 380px; max-width: calc(100vw - 32px);
  background: #fff; border-radius: 18px;
  box-shadow: 0 20px 60px rgba(15,61,46,.28);
  display: flex; flex-direction: column;
  overflow: hidden;
  opacity: 0; visibility: hidden;
  transform: translateY(16px) scale(.96);
  transform-origin: bottom left;
  transition: opacity .22s ease, visibility .22s, transform .22s ease;
  font-family: 'Vazirmatn', system-ui, sans-serif;
  direction: rtl;
}
.chat-panel.open {
  opacity: 1; visibility: visible;
  transform: translateY(0) scale(1);
}

/* Header */
.chat-header {
  padding: 12px 14px;
  background: linear-gradient(135deg, #16A34A 0%, #15803D 100%);
  color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
.chat-header-info { display: flex; align-items: center; gap: 10px; }
.chat-avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: rgba(255,255,255,.2);
  display: grid; place-items: center; font-size: 1.15rem;
  border: 1px solid rgba(255,255,255,.28);
}
.chat-header-info strong { display: block; font-size: .92rem; font-weight: 700; }
.chat-status {
  display: flex; align-items: center; gap: 5px;
  font-size: .72rem; opacity: .95;
}
.chat-status.typing { color: #FEF3C7; }
.dot-online {
  width: 7px; height: 7px; border-radius: 50%;
  background: #86EFAC;
  animation: pulse-online 2s infinite;
}
@keyframes pulse-online {
  0%,100% { box-shadow: 0 0 0 2px rgba(134,239,172,.35); }
  50%     { box-shadow: 0 0 0 5px rgba(134,239,172,.1); }
}
.chat-close {
  background: rgba(255,255,255,.15); border: 0; color: #fff;
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer;
  font-size: .9rem; transition: background .2s;
  display: grid; place-items: center;
}
.chat-close:hover { background: rgba(255,255,255,.3); }

/* Body - the flex parent */
.chat-body {
  flex: 1 1 0;
  display: flex; flex-direction: column;
  overflow: hidden; padding: 14px;
  min-height: 0;
}
.chat-body[hidden] { display: none; }

/* Welcome */
.chat-welcome {
  margin: 0 0 14px;
  padding: 12px 14px;
  background: #F0FDF4;
  border-radius: 10px;
  border-right: 3px solid #16A34A;
  flex-shrink: 0;
}
.chat-welcome strong {
  display: block; font-size: .95rem; font-weight: 700;
  color: #0F3D2E; margin-bottom: 3px;
}
.chat-welcome span {
  display: block; font-size: .82rem; color: #4B6359; line-height: 1.6;
}

.chat-start-form { display: flex; flex-direction: column; gap: 10px; }
.chat-start-form input {
  width: 100%; padding: 11px 13px;
  border: 1px solid #E3EDE7; border-radius: 10px;
  font-family: inherit; font-size: .88rem;
  background: #F7FAF8;
  transition: border-color .2s, background .2s, box-shadow .2s;
}
.chat-start-form input:focus {
  outline: 0; border-color: #16A34A; background: #fff;
  box-shadow: 0 0 0 3px rgba(22,163,74,.1);
}
.chat-start-form .btn {
  padding: 11px 14px; font-size: .9rem;
  width: 100%; margin-top: 4px;
}

.chat-error {
  background: #FEE2E2; color: #991B1B;
  padding: 9px 12px; border-radius: 8px;
  font-size: .8rem; text-align: center;
}
.chat-error[hidden] { display: none; }

.chat-privacy {
  margin-top: 12px; font-size: .72rem;
  color: #4B6359; text-align: center; line-height: 1.6;
  flex-shrink: 0;
}

/* Messages list */
.chat-messages-list {
  flex: 1 1 0;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px;
  padding: 4px 2px 8px;
  scroll-behavior: smooth;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
.chat-messages-list::-webkit-scrollbar { width: 5px; }
.chat-messages-list::-webkit-scrollbar-thumb { background: #C7E5D2; border-radius: 3px; }

.chat-msg {
  display: flex; flex-direction: column;
  animation: msgIn .28s ease;
  max-width: 82%;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.chat-msg.from-admin { align-self: flex-start; }
.chat-msg.from-user  { align-self: flex-end; }

.chat-bubble {
  padding: 9px 13px;
  border-radius: 14px;
  font-size: .87rem;
  line-height: 1.7;
  word-wrap: break-word;
  word-break: break-word;
}
.chat-msg.from-admin .chat-bubble {
  background: #F0FDF4; color: #0F2419;
  border-bottom-left-radius: 4px; border: 1px solid #DCFCE7;
}
.chat-msg.from-user .chat-bubble {
  background: #16A34A; color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.system { align-self: center; max-width: 90%; }
.chat-msg.system .chat-bubble {
  background: #FEF3C7; color: #92400E;
  font-size: .78rem; text-align: center;
  border: 1px solid #FDE68A;
}

.chat-meta {
  display: flex; align-items: center; gap: 4px;
  font-size: .68rem; color: #94A3A8;
  margin-top: 2px; padding: 0 6px;
}
.chat-msg.from-user .chat-meta { justify-content: flex-end; }
.chat-msg.from-admin .chat-meta { justify-content: flex-start; }

.chat-ticks {
  display: inline-flex; align-items: center;
  font-size: .78rem; line-height: 1; color: #94A3A8;
  transition: color .25s;
}
.chat-ticks.read { color: #16A34A; }

.chat-typing {
  align-self: flex-start; max-width: 82%;
  display: flex; flex-direction: column;
  animation: msgIn .28s ease;
  margin-top: 2px; flex-shrink: 0;
}
.chat-typing[hidden] { display: none; }
.chat-typing .chat-bubble {
  background: #F0FDF4; border: 1px solid #DCFCE7;
  border-bottom-left-radius: 4px;
  padding: 11px 15px;
  display: inline-flex; gap: 4px; align-items: center;
}
.chat-typing .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #16A34A; display: inline-block;
  animation: typingDot 1.2s infinite;
}
.chat-typing .dot:nth-child(2) { animation-delay: .18s; }
.chat-typing .dot:nth-child(3) { animation-delay: .36s; }
@keyframes typingDot {
  0%,60%,100% { transform: translateY(0); opacity: .45; }
  30%         { transform: translateY(-4px); opacity: 1; }
}

/* Input area */
.chat-input-wrap {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 0 0;
  border-top: 1px solid #E3EDE7;
  margin-top: 8px;
  flex-shrink: 0;
  background: #fff;
}
.chat-input-wrap textarea {
  flex: 1;
  resize: none;
  padding: 10px 12px;
  margin: 0;
  border: 1px solid #E3EDE7;
  border-radius: 12px;
  font-family: inherit;
  font-size: .88rem;
  line-height: 1.5;
  background: #F7FAF8;
  min-height: 42px;
  max-height: 90px;
  transition: border-color .2s, background .2s, box-shadow .2s;
}
.chat-input-wrap textarea:focus {
  outline: 0; border-color: #16A34A;
  background: #fff; box-shadow: 0 0 0 3px rgba(22,163,74,.1);
}
.chat-send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: #16A34A; color: #fff; border: 0;
  display: grid; place-items: center; cursor: pointer;
  transition: background .2s, transform .15s;
  flex-shrink: 0;
}
.chat-send-btn:hover { background: #15803D; }
.chat-send-btn:active { transform: scale(.94); }
.chat-send-btn:disabled { opacity: .5; cursor: not-allowed; }
.chat-send-btn svg { width: 17px; height: 17px; transform: scaleX(-1); }


/* ==================== MOBILE FINAL (v4) ==================== */
@media (max-width: 500px) {
  .chat-toggle-btn {
    bottom: 84px; left: 16px;
    width: 54px; height: 54px;
  }
  .chat-toggle-btn svg { width: 24px; height: 24px; }

  .chat-panel {
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100vw; max-width: 100vw;
    height: 100vh; height: 100dvh;
    border-radius: 0;
    transform: none; transform-origin: unset;
    box-shadow: none;
  }
  .chat-panel.open { transform: none; }

  .chat-header { padding: 12px 14px; }
  .chat-avatar { width: 36px; height: 36px; font-size: 1.05rem; }
  .chat-header-info strong { font-size: .9rem; }
  .chat-status { font-size: .7rem; }
  .chat-close { width: 34px; height: 34px; font-size: 1rem; }

  .chat-body {
    padding: 12px 12px calc(12px + env(safe-area-inset-bottom, 0px)) 12px;
  }

  .chat-welcome {
    padding: 10px 12px; margin-bottom: 12px;
  }
  .chat-welcome strong { font-size: .9rem; }
  .chat-welcome span { font-size: .78rem; }

  .chat-messages-list { gap: 8px; padding: 4px 0 8px; }
  .chat-bubble { padding: 10px 14px; font-size: .9rem; line-height: 1.7; }
  .chat-meta { font-size: .66rem; }

  /* ===== INPUT AREA - LARGE, iOS FRIENDLY ===== */
  .chat-input-wrap {
    padding: 10px 0 0;
    margin-top: 8px;
    gap: 8px;
    background: #fff;
    align-items: flex-end;
  }
  .chat-input-wrap textarea {
    font-size: 16px;         /* ! prevents iOS zoom */
    line-height: 1.5;
    min-height: 46px;
    max-height: 110px;
    padding: 12px 14px;
    border-radius: 14px;
    border: 1.5px solid #E3EDE7;
  }
  .chat-input-wrap textarea:focus {
    border-color: #16A34A;
    box-shadow: 0 0 0 3px rgba(22,163,74,.1);
  }
  .chat-send-btn {
    width: 46px; height: 46px;
    min-width: 46px;
    border-radius: 14px;
  }
  .chat-send-btn svg { width: 20px; height: 20px; }

  /* Chat start form inputs */
  .chat-start-form input {
    font-size: 16px !important;
    padding: 13px 14px;
    min-height: 46px;
  }
  .chat-start-form .btn { padding: 13px; font-size: .95rem; }
}

@media (max-width: 360px) {
  .chat-bubble { font-size: .85rem; padding: 8px 12px; }
  .chat-input-wrap textarea { font-size: 16px; padding: 11px 12px; }
}