:root {
  --maxlc-color: #2563eb;
  --maxlc-color-dark: #1d4ed8;
  --maxlc-radius: 20px;
}

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

.maxlc-hidden {
  display: none !important;
}

/* ── ROOT ─────────────────────────────────── */
#maxlc-root {
  position: fixed;
  right: 20px;
  bottom: 80px;
  z-index: 99999;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
}

/* ── BACKDROP (показывается только на мобильном) ── */
.maxlc-backdrop {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0);
  transition: background 0.3s ease;
  pointer-events: none;
  cursor: pointer;
}

.maxlc-backdrop.maxlc-visible {
  background: rgba(0, 0, 0, 0.45);
  pointer-events: auto;
}

/* ── КНОПКА-ТРИГГЕР ───────────────────────── */
.maxlc-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: var(--maxlc-color);
  color: #fff;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 8px 28px rgba(37, 99, 235, 0.4);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Бейдж "есть новое сообщение" */
.maxlc-toggle {
  position: relative;
}

.maxlc-toggle .maxlc-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 999px;
  border: 2px solid #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  animation: maxlc-badge-pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes maxlc-badge-pop {
  from { transform: scale(0); }
  to   { transform: scale(1); }
}

.maxlc-toggle:hover {
  background: var(--maxlc-color-dark);
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 14px 38px rgba(37, 99, 235, 0.5);
}

.maxlc-toggle:active {
  transform: scale(0.95);
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.3);
}

/* ── ПАНЕЛЬ ───────────────────────────────── */
.maxlc-panel {
  position: absolute;
  bottom: calc(100% + 14px);
  right: 0;
  width: 360px;
  max-width: calc(100vw - 40px);
  height: 520px;
  background: #fff;
  border-radius: var(--maxlc-radius);
  box-shadow: 0 20px 64px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  opacity: 0;
  pointer-events: none;
}

.maxlc-panel.maxlc-open {
  opacity: 1;
  pointer-events: auto;
  -webkit-transition: opacity 0.15s ease;
  transition: opacity 0.15s ease;
}

/* ── DRAG HANDLE (только мобильный) ─────── */
.maxlc-drag-handle {
  display: none;
  flex-shrink: 0;
  background: var(--maxlc-color);
  padding: 10px 0 0;
}

.maxlc-drag-handle::after {
  content: '';
  display: block;
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.4);
  margin: 0 auto 8px;
}

/* ── ШАПКА ───────────────────────────────── */
.maxlc-header {
  background: var(--maxlc-color);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.maxlc-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.maxlc-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.maxlc-header-text {
  min-width: 0;
}

.maxlc-title {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

.maxlc-status {
  font-size: 12px;
  opacity: 0.75;
  margin-top: 2px;
}

.maxlc-close {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-left: 8px;
  transition: background 0.15s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

.maxlc-close:hover {
  background: rgba(255, 255, 255, 0.28);
}

.maxlc-close:active {
  transform: scale(0.88);
}

/* ── ПРИВЕТСТВИЕ ─────────────────────────── */
.maxlc-welcome {
  padding: 11px 16px;
  font-size: 13px;
  color: #374151;
  background: #f8fafc;
  border-bottom: 1px solid #e5e7eb;
  flex-shrink: 0;
  line-height: 1.45;
}

/* ── СООБЩЕНИЯ ───────────────────────────── */
.maxlc-messages {
  flex: 1;
  overflow-y: auto;
  background: #f4f5f7;
  padding: 14px 12px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.maxlc-messages::-webkit-scrollbar {
  width: 3px;
}
.maxlc-messages::-webkit-scrollbar-track {
  background: transparent;
}
.maxlc-messages::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 3px;
}

/* ── АНИМАЦИЯ НОВЫХ СООБЩЕНИЙ ────────────── */
@keyframes maxlc-msg-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.maxlc-msg {
  display: flex;
  margin-bottom: 6px;
}

.maxlc-msg.visitor {
  justify-content: flex-end;
}

.maxlc-msg.manager {
  justify-content: flex-start;
}

.maxlc-msg.maxlc-msg-enter {
  animation: maxlc-msg-in 0.22s ease both;
}

/* Сообщение в процессе отправки — чуть прозрачное */
.maxlc-msg[data-status="sending"] .maxlc-bubble {
  opacity: 0.55;
}

/* Кнопка повтора при ошибке */
.maxlc-retry {
  align-self: center;
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  margin-right: 6px;
  border-radius: 50%;
  background: #ef4444;
  color: #fff;
  border: none;
  font-size: 13px;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  animation: maxlc-badge-pop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.maxlc-retry:hover  { background: #dc2626; }
.maxlc-retry:active { transform: scale(0.88); }

/* ── РАЗДЕЛИТЕЛЬ ДАТЫ ────────────────────── */
.maxlc-date-sep {
  display: flex;
  align-items: center;
  margin: 12px 0 8px;
  gap: 8px;
}

.maxlc-date-sep::before,
.maxlc-date-sep::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e0e0e4;
}

.maxlc-date-sep span {
  font-size: 12px;
  color: #9ca3af;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── ПУЗЫРЬКИ ────────────────────────────── */
.maxlc-bubble {
  max-width: 80%;
  padding: 8px 12px 6px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── ВРЕМЯ ───────────────────────────────── */
.maxlc-time {
  display: block;
  font-size: 11px;
  margin-top: 3px;
  line-height: 1;
  text-align: right;
  opacity: 0.65;
}

.maxlc-msg.manager .maxlc-time {
  text-align: left;
}

.maxlc-msg.visitor .maxlc-bubble {
  background: var(--maxlc-color);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.maxlc-msg.manager .maxlc-bubble {
  background: #fff;
  color: #111827;
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

/* ── ФОРМА ───────────────────────────────── */
.maxlc-form {
  padding: 10px 12px;
  border-top: 1px solid #e5e7eb;
  background: #fff;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.maxlc-name,
.maxlc-input {
  width: 100%;
  border: 1.5px solid #e5e7eb;
  border-radius: 12px;
  padding: 10px 13px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  color: #111827;
  background: #fafafa;
  line-height: 1.4;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.maxlc-name:focus,
.maxlc-input:focus {
  border-color: var(--maxlc-color);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
  background: #fff;
}

.maxlc-name::placeholder,
.maxlc-input::placeholder {
  color: #9ca3af;
}

.maxlc-send {
  width: 100%;
  border: none;
  background: var(--maxlc-color);
  color: #fff;
  border-radius: 12px;
  padding: 11px 14px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  min-height: 44px;
  transition: background 0.15s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

.maxlc-send:hover {
  background: var(--maxlc-color-dark);
}

.maxlc-send:active {
  transform: scale(0.97);
}

/* ══════════════════════════════════════════
   МОБИЛЬНЫЕ УСТРОЙСТВА
══════════════════════════════════════════ */
@media (max-width: 600px) {

  /* Root — только якорь для десктопной позиции, на мобиле сбрасываем */
  #maxlc-root {
    position: static;
  }

  /* Backdrop — полноэкранный fixed, по умолчанию не перехватывает клики */
  .maxlc-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 99998;
    pointer-events: none;
  }

  .maxlc-backdrop.maxlc-visible {
    pointer-events: auto;
  }

  /* Кнопка-триггер — независимый fixed элемент */
  .maxlc-toggle {
    position: fixed;
    right: 16px;
    bottom: max(80px, env(safe-area-inset-bottom, 80px));
    z-index: 99999;
  }

  /* Панель — bottom sheet, тоже independent fixed */
  .maxlc-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    height: 90vh;
    height: 90dvh;
    max-height: 90dvh;
    border-radius: 22px 22px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    z-index: 99999;
    pointer-events: none;
    opacity: 1; /* на мобиле не нужен opacity-fade, только slide */
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0);
    will-change: transform;
    -webkit-transition: -webkit-transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
    transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
  }

  .maxlc-panel.maxlc-open {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    pointer-events: auto;
  }

  .maxlc-drag-handle {
    display: none;
  }

  /* Пузырьки чуть шире */
  .maxlc-bubble {
    max-width: 86%;
    font-size: 15px;
  }

  /* font-size: 16px обязателен — иначе iOS зумит страницу при фокусе */
  .maxlc-name,
  .maxlc-input {
    font-size: 16px;
    padding: 12px 14px;
  }

  .maxlc-send {
    font-size: 15px;
    min-height: 50px;
  }

  .maxlc-header {
    padding: 16px 18px;
  }

  .maxlc-avatar {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }

  .maxlc-title {
    font-size: 16px;
  }
}
