:root {
  /* Основной синий цвет Jivo */
  --chat-primary: #3e77b9;
  /* Цвет при наведении (чуть темнее базового) */
  --chat-primary-hover: #336299;
  /* Нежно-голубой для фона сообщений посетителя (прозрачность 12%) */
  --chat-visitor-bg: rgba(62, 119, 185, 0.12);
  /* Тень для свернутой кнопки (с синим оттенком) */
  --chat-shadow-collapsed: rgba(62, 119, 185, 0.35);
  /* Цвет свечения для анимации */
  --chat-glow-color: rgba(62, 119, 185, 0.5);

  --chat-bg: #F4F6F9;
  --chat-text: #222;
  --chat-border: #E2E5E9;
}

* {
  box-sizing: border-box;
}

/* --- Главный контейнер виджета --- */
.chat-widget {
  position: fixed;
  right: 24px;
  bottom: 24px;
  /* По умолчанию задаем размеры маленькой кнопки */
  width: 60px;
  height: 60px;
  border-radius: 30px;
  background: #fff;
  box-shadow: 0 5px 20px var(--chat-shadow-collapsed);
  overflow: hidden;
  z-index: 9999;
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease, height 0.3s ease, border-radius 0.3s ease, box-shadow 0.3s ease;
  
  /* Скрываем внутренности по умолчанию */
  animation: chat-widget-pulse 2.5s infinite;
}

/* Состояние, когда чат ОТКРЫТ (убираем принудительные размеры кнопки) */
.chat-widget:not(.is-collapsed) {
  width: 360px;
  height: auto; /* или 500px, в зависимости от контента */
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  animation: none; /* отключаем пульсацию при открытии */
}

/* Скрываем блоки, если чат свернут (или по умолчанию) */
.chat-widget.is-collapsed .chat-widget__body,
.chat-widget.is-collapsed .chat-widget__footer,
.chat-widget:not(.is-collapsed === false) .chat-widget__body { 
  /* Чтобы не дублировать код, просто убедитесь, что body/footer скрыты */
  display: none; 
}

/* В открытом состоянии показываем всё */
.chat-widget:not(.is-collapsed) .chat-widget__body,
.chat-widget:not(.is-collapsed) .chat-widget__footer {
  display: flex;
}

@keyframes chat-widget-pulse {
  0% { box-shadow: 0 0 0 0 rgba(62, 119, 185, 0.7); }
  70% { box-shadow: 0 0 0 15px rgba(62, 119, 185, 0); }
  100% { box-shadow: 0 0 0 0 rgba(62, 119, 185, 0); }
}

.chat-widget.is-collapsed .chat-widget__body,
.chat-widget.is-collapsed .chat-widget__footer {
  display: none;
}

/* --- Шапка --- */
.chat-widget__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 20px;
  background: var(--chat-primary);
  color: #fff;
  transition: all 0.3s ease;
}

.chat-widget.is-collapsed .chat-widget__header {
  padding: 0;
  width: 60px;
  height: 60px;
  justify-content: center;
  cursor: pointer;
}

.chat-widget__header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-widget.is-collapsed .chat-widget__header-info {
  display: none;
}

.chat-widget__avatar {
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-widget__avatar svg {
  width: 20px;
  height: 20px;
}

.chat-widget__title {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.2;
}

.chat-widget__status {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 2px;
}

/* Кнопка переключения */
.chat-widget__toggle {
  background: transparent;
  border: 0;
  color: transparent !important;
  font-size: 0;
  width: 32px;
  height: 32px;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-widget.is-collapsed .chat-widget__toggle {
  width: 100%;
  height: 100%;
}

.chat-widget__toggle::before {
  content: '';
  position: absolute;
  width: 24px;
  height: 24px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  transition: transform 0.3s ease;
}

.chat-widget__toggle::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23ffffff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='18' y1='6' x2='6' y2='18'/%3E%3Cline x1='6' y1='6' x2='18' y2='18'/%3E%3C/svg%3E");
}

.chat-widget.is-collapsed .chat-widget__toggle::before {
  width: 28px;
  height: 28px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23ffffff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z'/%3E%3C/svg%3E");
}

/* --- Тело и сообщения --- */
.chat-widget__body {
  background: var(--chat-bg);
  padding: 16px;
  height: 400px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding-right: 4px;
  display: flex;
  flex-direction: column;
}

.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 10px; }

.chat-msg {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 14px;
  margin-bottom: 12px;
  font-size: 14px;
  line-height: 1.4;
  word-break: break-word;
  position: relative;
}

.chat-msg--visitor {
  background: var(--chat-visitor-bg);
  color: var(--chat-text);
  margin-left: auto;
  border-bottom-right-radius: 4px;
  border: 1px solid rgba(62, 119, 185, 0.2);
}

.chat-msg--operator {
  background: #fff;
  color: var(--chat-text);
  margin-right: auto;
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.03);
  border: 1px solid #eaedf4;
}

.chat-msg__time {
  font-size: 11px;
  opacity: 0.6;
  margin-top: 6px;
  text-align: right;
}

/* --- Форма сбора контактов --- */
.chat-profile {
  background: #fff;
  border: 1px solid var(--chat-border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.03);
  text-align: center;
}

.chat-profile--hidden { display: none; }

.chat-profile__title {
  font-size: 14px;
  font-weight: 600;
  color: var(--chat-text);
  margin-bottom: 12px;
}

.chat-input,
.chat-textarea {
  width: 100%;
  background: #f9fafb;
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  padding: 12px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.chat-input { margin-bottom: 8px; }
.chat-input:focus, .chat-textarea:focus { border-color: var(--chat-primary); }

.chat-profile__actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.chat-btn {
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: all 0.3s ease;
}

.chat-btn--primary {
  background: var(--chat-primary);
  color: #fff;
  box-shadow: 0 4px 10px var(--chat-glow-color);
  /* Пульсация для кнопки начала диалога */
  animation: chat-btn-pulse 2s infinite;
}

@keyframes chat-btn-pulse {
  0% { box-shadow: 0 0 0 0 rgba(62, 119, 185, 0.6); }
  70% { box-shadow: 0 0 0 8px rgba(62, 119, 185, 0); }
  100% { box-shadow: 0 0 0 0 rgba(62, 119, 185, 0); }
}

.chat-btn--primary:hover { 
  background: var(--chat-primary-hover); 
  transform: translateY(-1px);
  box-shadow: 0 6px 15px rgba(62, 119, 185, 0.5);
}

.chat-btn--light {
  background: #f3f4f6;
  color: #4b5563;
}

/* --- Подвал (Ввод сообщения) --- */
.chat-widget__footer {
  padding: 12px 16px;
  border-top: 1px solid var(--chat-border);
  background: #fff;
}

.chat-widget__composer {
  display: flex;
  gap: 30px;
  align-items: center;
}

.chat-textarea {
  flex: 1; /* Растягивается на всё доступное пространство */
  min-height: 40px;
  max-height: 120px; /* Чтобы чат не улетел за экран при очень длинном тексте */
  padding: 10px 12px;
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  resize: none; /* Убираем ручное растягивание пользователем */
  outline: none;
  background: #f9fafb;
  font-family: inherit;
  box-sizing: border-box;
  transition: border-color 0.2s;
}

.chat-textarea:focus { border-color: transparent; }

.chat-upload {
  color: #9ca3af;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: color 0.2s;
}
.chat-upload:hover { color: var(--chat-primary); }
.chat-upload input { display: none; }
.chat-upload svg { width: 22px; height: 22px; }

.chat-btn--send {
  background: var(--chat-primary);
  color: #fff;
  border: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px var(--chat-glow-color);
}

.chat-btn--send:hover {
  background: var(--chat-primary-hover);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(62, 119, 185, 0.6);
}
.chat-btn--send svg { width: 18px; height: 18px; margin-left: -2px; }

.chat-widget__hint {
  font-size: 11px;
  color: #6b7280;
  margin-top: 6px;
  min-height: 14px;
  text-align: center;
}

/* Прикрепленные изображения */
.chat-attachment { display: block; margin-top: 8px; cursor: pointer; }
.chat-attachment img { max-width: 100%; max-height: 140px; border-radius: 8px; display: block; }
.chat-attachment__name { font-size: 12px; color: var(--chat-primary); margin-top: 4px; text-decoration: underline; }

/* Модальное окно картинок */
.chat-lightbox { position: fixed; inset: 0; display: none; z-index: 10001; }
.chat-lightbox.is-open { display: block; }
.chat-lightbox__backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .85); }
.chat-lightbox__dialog {
  position: relative; width: min(92vw, 960px); height: min(90vh, 760px);
  margin: 5vh auto; display: flex; align-items: center; justify-content: center;
}
.chat-lightbox__dialog img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.chat-lightbox__btn {
  position: absolute; right: 0; top: -40px; background: transparent; color: #fff;
  border: 0; font-size: 36px; cursor: pointer;
}

@media (max-width: 768px) {
  /* Контейнер на весь экран */
  .chat-widget:not(.is-collapsed) {
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
	display: flex;
    flex-direction: column;
}

	/* Убедитесь, что когда класс НЕ установлен, виджет имеет полные размеры */
.chat-widget:not(.is-collapsed) {
  width: 360px;
  height: auto;
  border-radius: 16px;
}
	
  /* Шапка становится выше для удобства нажатия */
  .chat-widget:not(.is-collapsed) .chat-widget__header {
    
    height: auto;
  }

  /* Тело чата занимает всё оставшееся пространство */
  .chat-widget:not(.is-collapsed) .chat-widget__body {
    flex: 1;
    height: auto; /* Позволяем flex-box управлять высотой */
    padding-bottom: 20px;
  }

  /* Сообщения */
  .chat-msg {
    max-width: 90%; /* Больше ширины на узких экранах */
    font-size: 16px; /* Предотвращаем зум Safari при фокусе на input */
  }

  /* Футер (поле ввода) фиксируем снизу с учетом Safe Area */
  .chat-widget:not(.is-collapsed) .chat-widget__footer {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)) 16px;
    background: #fff;
    border-top: 1px solid var(--chat-border);
  }

  .chat-textarea {
    font-size: 16px; /* Обязательно 16px, иначе iOS будет приближать экран при вводе */
  }

  /* Кнопка закрытия/сворачивания в мобильной версии */
  .chat-widget__toggle {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

/* Дополнительный фикс для предотвращения скролла основного сайта под чатом */
body.chat-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
}