:root {
  --bg-light: #f8fafc;
  --panel-bg: #ffffff;
  --panel-border: #e2e8f0;
  --primary-accent: #3b82f6;
  --primary-hover: #2563eb;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --agent-bubble-bg: #f1f5f9;
  --agent-bubble-text: #1e293b;
  --user-bubble-bg: #2563eb;
  --user-bubble-text: #ffffff;
  --input-bg: #f8fafc;
  --font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-light);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
}

/* Glassmorphism / Clean Panel */
.glass-panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.04);
}

/* Layout Grid */
.app-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 20px;
  width: 95vw;
  max-width: 1180px;
  height: 90vh;
  max-height: 840px;
  z-index: 1;
}

/* Sidebar Panel */
.sidebar-panel {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-y: auto;
}

.brand-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-badge {
  width: 40px;
  height: 40px;
  background: var(--primary-accent);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 20px;
  color: #fff;
}

.brand-name {
  font-size: 18px;
  font-weight: 800;
  color: var(--text-main);
  letter-spacing: -0.3px;
}
.brand-tag {
  font-size: 11px;
  color: var(--primary-accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sidebar-card {
  background: var(--bg-light);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: #10b981;
  margin-bottom: 6px;
}
.status-dot {
  width: 8px;
  height: 8px;
  background-color: #10b981;
  border-radius: 50%;
}

.sidebar-text {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* Promo Banner */
.promo-banner {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 14px;
  padding: 14px;
  text-align: center;
}
.promo-badge {
  font-size: 12px;
  font-weight: 700;
  color: #2563eb;
}
.promo-code {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: #1e3a8a;
  margin: 4px 0;
}
.promo-desc {
  font-size: 11px;
  color: var(--text-muted);
}

/* Quick Scenarios */
.quick-scenarios {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}
.scenario-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.chip-btn {
  background: #ffffff;
  border: 1px solid var(--panel-border);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}
.chip-btn:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  transform: translateX(2px);
}

.sidebar-footer {
  margin-top: auto;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  border: none;
  transition: all 0.2s ease;
}
.btn-secondary {
  background: #ffffff;
  color: var(--text-muted);
  border: 1px solid var(--panel-border);
}
.btn-secondary:hover {
  background: #fef2f2;
  color: #ef4444;
  border-color: #fca5a5;
}
.full-width {
  width: 100%;
}

/* Chat Window */
.chat-window {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #ffffff;
}

.agent-profile {
  display: flex;
  align-items: center;
  gap: 12px;
}
.avatar-ring {
  position: relative;
}
.agent-avatar {
  width: 40px;
  height: 40px;
  background: var(--primary-accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 16px;
  color: #fff;
}
.online-badge {
  width: 10px;
  height: 10px;
  background-color: #10b981;
  border: 2px solid #ffffff;
  border-radius: 50%;
  position: absolute;
  bottom: 0;
  right: 0;
}

.agent-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-main);
}
.agent-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.header-badges {
  display: flex;
  gap: 8px;
}
.badge {
  font-size: 11px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
}
.badge-purple {
  background: #f3e8ff;
  color: #7e22ce;
  border: 1px solid #e9d5ff;
}
.badge-cyan {
  background: #e0f2fe;
  color: #0369a1;
  border: 1px solid #bae6fd;
}

/* Messages Area */
.chat-messages {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: #ffffff;
  scroll-behavior: smooth;
}

.message-row {
  display: flex;
  gap: 12px;
  max-width: 82%;
  animation: fadeInMsg 0.25s ease;
}
@keyframes fadeInMsg {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.agent-row {
  align-self: flex-start;
}
.user-row {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.agent-row .msg-avatar {
  background: var(--primary-accent);
  color: #fff;
}
.user-row .msg-avatar {
  background: #e2e8f0;
  color: #475569;
}

.msg-bubble {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.6;
  position: relative;
}
.agent-bubble {
  background: var(--agent-bubble-bg);
  border: 1px solid var(--panel-border);
  border-top-left-radius: 4px;
  color: var(--agent-bubble-text);
}
.user-bubble {
  background: var(--user-bubble-bg);
  border-top-right-radius: 4px;
  color: var(--user-bubble-text);
}

.msg-bubble p {
  margin-bottom: 6px;
}
.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble ul, .msg-bubble ol {
  margin-left: 18px;
  margin-bottom: 6px;
}

.bubble-time {
  font-size: 10px;
  opacity: 0.6;
  margin-top: 4px;
  text-align: right;
}

.msg-img {
  max-width: 220px;
  border-radius: 10px;
  margin-bottom: 6px;
  border: 1px solid var(--panel-border);
}

/* Typing Indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  align-items: center;
}
.dot {
  width: 6px;
  height: 6px;
  background-color: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out both;
}
.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Image Preview */
.image-preview-area {
  padding: 8px 24px;
  background: #f8fafc;
  border-top: 1px solid var(--panel-border);
}
.preview-item {
  position: relative;
  display: inline-block;
}
.preview-item img {
  width: 54px;
  height: 54px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--primary-accent);
}
.btn-remove-img {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #ef4444;
  color: white;
  border: none;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Input Area */
.chat-input-container {
  padding: 16px 24px;
  background: #ffffff;
  border-top: 1px solid var(--panel-border);
}

.chat-form {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 4px 10px;
  transition: all 0.2s ease;
}
.chat-form:focus-within {
  border-color: var(--primary-accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-main);
  font-size: 14px;
  font-family: inherit;
  padding: 10px 4px;
  outline: none;
}
.chat-input::placeholder {
  color: var(--text-muted);
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}
.btn-icon:hover {
  color: var(--primary-accent);
  background: #f1f5f9;
}

.btn-send {
  background: var(--primary-accent);
  color: white;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}
.btn-send:hover {
  background: var(--primary-hover);
}

.hidden {
  display: none !important;
}

/* Бургер и крестик живут только в мобильной раскладке */
.btn-menu,
.drawer-close,
.drawer-backdrop {
  display: none;
}

/* Interactive Quick Reply Buttons */
.quick-reply-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
  margin-bottom: 6px;
}

.quick-reply-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #ffffff;
  border: 1.5px solid #cbd5e1;
  color: #1e293b;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  font-family: inherit;
}

.quick-reply-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.quick-reply-btn.btn-yes:hover {
  background: #f0fdf4;
  border-color: #22c55e;
  color: #15803d;
}

.quick-reply-btn.btn-no:hover {
  background: #fef2f2;
  border-color: #ef4444;
  color: #b91c1c;
}

.quick-reply-btn:active {
  transform: translateY(0);
}

/* Live-чат: реплики менеджера и системные плашки */
.manager-avatar {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: #ffffff;
}

.manager-bubble {
  background: #eef2ff !important;
  border: 1px solid #c7d2fe;
}

.manager-label {
  font-size: 11px;
  font-weight: 600;
  color: #4338ca;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.system-notice {
  align-self: center;
  margin: 8px auto;
  padding: 6px 14px;
  background: #f1f5f9;
  color: #475569;
  border-radius: 999px;
  font-size: 12px;
  text-align: center;
  max-width: 80%;
}

/* Баннер ожидания менеджера с позицией в очереди */
.queue-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 20px 10px;
  padding: 10px 14px;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 12px;
  color: #c2410c;
  font-size: 13px;
  font-weight: 500;
}

.queue-banner.hidden {
  display: none;
}

.queue-spinner {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  border: 2px solid #fdba74;
  border-top-color: #c2410c;
  border-radius: 50%;
  animation: queue-spin 0.9s linear infinite;
}

@keyframes queue-spin {
  to { transform: rotate(360deg); }
}

/* Ввод заблокирован, пока бот формирует ответ */
.chat-form.is-locked {
  opacity: 0.75;
}

.chat-input:disabled {
  cursor: not-allowed;
  background: transparent;
}

.chat-input:disabled::placeholder {
  font-style: italic;
}

.btn-send:disabled,
.btn-icon:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.quick-reply-btn:disabled,
.chip-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* Кнопка отмены ожидания ответа бота */
.btn-cancel {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  border: 1px solid #fecaca;
  border-radius: 14px;
  background: #fef2f2;
  color: #dc2626;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-cancel:hover {
  background: #fee2e2;
  border-color: #fca5a5;
}

.btn-cancel.hidden {
  display: none;
}

/* Кнопка повтора ответа под последним сообщением бота */
.retry-row {
  display: flex;
  justify-content: flex-start;
  padding-left: 52px;
  margin-top: -4px;
}

.retry-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 999px;
  background: #ffffff;
  color: #475569;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.retry-btn:hover {
  border-color: #6366f1;
  color: #4f46e5;
  background: #f5f3ff;
}

.retry-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Кнопка повтора после отмены — по центру, под системной плашкой */
.retry-row--center {
  justify-content: center;
  padding-left: 0;
  margin-top: 2px;
}

/* ===== Голосовой ввод ===== */

/* Кнопка микрофона в строке ввода */
.btn-voice.is-recording {
  color: #dc2626;
  background: #fef2f2;
}

.btn-voice.is-recording svg {
  animation: voice-pulse 1.2s ease-in-out infinite;
}

@keyframes voice-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.55; transform: scale(0.88); }
}

/* Полоса состояния записи над формой */
.voice-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding: 8px 12px;
  border: 1px solid #fecaca;
  border-radius: 12px;
  background: #fef2f2;
  font-size: 13px;
  color: #b91c1c;
}

.voice-bar.hidden {
  display: none;
}

/* Расшифровка: запись уже закончилась, ждём ответ сервера */
.voice-bar.is-transcribing {
  border-color: #bfdbfe;
  background: #eff6ff;
  color: #1d4ed8;
}

/* Ошибка записи или распознавания */
.voice-bar.is-error {
  border-color: #fed7aa;
  background: #fff7ed;
  color: #c2410c;
}

.voice-dot {
  width: 9px;
  height: 9px;
  flex-shrink: 0;
  border-radius: 50%;
  background: currentColor;
  animation: voice-pulse 1.2s ease-in-out infinite;
}

/* У ошибки ничего не происходит — мигать нечему */
.voice-bar.is-error .voice-dot {
  animation: none;
}

.voice-bar.is-transcribing .voice-dot {
  border-radius: 50%;
  background: transparent;
  width: 12px;
  height: 12px;
  border: 2px solid #bfdbfe;
  border-top-color: #1d4ed8;
  animation: queue-spin 0.9s linear infinite;
}

.voice-status {
  flex: 1;
  min-width: 0;
}

.voice-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.voice-bar.is-transcribing .voice-timer,
.voice-bar.is-error .voice-timer,
.voice-bar.is-transcribing .voice-actions,
.voice-bar.is-error .voice-actions {
  display: none;
}

.voice-actions {
  display: flex;
  gap: 6px;
}

.voice-action {
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 5px 12px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.voice-action--cancel {
  background: #ffffff;
  border-color: #fecaca;
  color: #b91c1c;
}

.voice-action--cancel:hover {
  background: #fee2e2;
}

.voice-action--stop {
  background: var(--primary-accent);
  color: #ffffff;
}

.voice-action--stop:hover {
  background: var(--primary-hover);
}

/* ===== Мобильная раскладка ===== */
@media (max-width: 768px) {
  body {
    /* На телефоне окно чата занимает экран целиком, без «карточки» по центру */
    display: block;
    overflow: hidden;
  }

  .app-layout {
    display: block;
    width: 100vw;
    max-width: none;
    /*
     * dvh вместо vh: с адресной строкой Safari и Chrome 100vh выше экрана,
     * и поле ввода уезжает под него. --app-height ставит скрипт по
     * visualViewport — это единственный размер, который учитывает
     * выехавшую клавиатуру.
     */
    height: 100vh;
    height: 100dvh;
    height: var(--app-height, 100dvh);
    max-height: none;
  }

  .glass-panel {
    border: none;
    border-radius: 0;
    box-shadow: none;
  }

  .chat-window {
    height: 100%;
  }

  /* --- Сайдбар как выдвижная панель --- */
  .sidebar-panel {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 30;
    width: min(86vw, 320px);
    height: 100%;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 0 0 40px rgba(15, 23, 42, 0.18);
    /* Панель перекрывает чат — сквозь неё не должно быть видно и кликаться */
    background: var(--panel-bg);
  }

  .sidebar-panel.is-open {
    transform: translateX(0);
  }

  .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(15, 23, 42, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  .drawer-backdrop.is-open {
    opacity: 1;
    pointer-events: auto;
  }

  .drawer-close {
    display: flex;
    position: absolute;
    top: 14px;
    right: 14px;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 10px;
    background: #f1f5f9;
    color: var(--text-muted);
    cursor: pointer;
  }

  .btn-menu {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    margin-right: 4px;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--text-main);
    cursor: pointer;
  }

  .btn-menu:active {
    background: #f1f5f9;
  }

  /* --- Шапка: одна строка вместо трёх --- */
  .chat-header {
    justify-content: flex-start;
    padding: 10px 12px;
    gap: 8px;
  }

  .agent-profile {
    flex: 1;
    gap: 10px;
    min-width: 0;
  }

  .agent-profile > div:last-child {
    min-width: 0;
  }

  .agent-avatar {
    width: 34px;
    height: 34px;
    font-size: 14px;
  }

  .agent-title,
  .agent-subtitle {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .agent-title {
    font-size: 14px;
  }

  .agent-subtitle {
    font-size: 11px;
  }

  /* Маркетинговые бейджи на 375px выдавливают название в три строки.
     Промо и преимущества клиент видит в выдвижной панели. */
  .header-badges {
    display: none;
  }

  /* --- Лента сообщений --- */
  .chat-messages {
    padding: 14px 12px;
    gap: 14px;
  }

  .message-row {
    max-width: 94%;
    gap: 8px;
  }

  .msg-avatar {
    width: 28px;
    height: 28px;
    font-size: 11px;
  }

  .msg-bubble {
    padding: 10px 13px;
  }

  .msg-img {
    max-width: 100%;
  }

  .retry-row {
    padding-left: 36px;
  }

  .queue-banner {
    margin: 0 12px 8px;
  }

  /* --- Ввод --- */
  .chat-input-container {
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .chat-form {
    gap: 4px;
    padding: 3px 6px;
  }

  /*
   * Ровно 16px: при меньшем размере Safari на iOS зумит всю страницу,
   * как только клиент ставит курсор в поле, и обратно уже не отъезжает.
   */
  .chat-input {
    font-size: 16px;
    padding: 12px 6px;
  }

  /* Пальцу нужно 44px — столько же, сколько советует Apple HIG */
  .btn-icon {
    width: 44px;
    height: 44px;
    padding: 0;
  }

  .btn-send,
  .btn-cancel {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
  }

  /*
   * Полоса записи в две строки: сверху точка и подпись целиком, снизу
   * таймер и кнопки. В одну строку на 375px подпись сжимается в столбик
   * по два слова.
   */
  .voice-bar {
    flex-wrap: wrap;
    row-gap: 8px;
  }

  /* Пустой элемент нулевой высоты — перенос строки внутри flex-контейнера */
  .voice-bar::after {
    content: "";
    flex-basis: 100%;
    height: 0;
    order: 2;
  }

  /* В этих состояниях таймера и кнопок нет — второй строке взяться неоткуда */
  .voice-bar.is-transcribing::after,
  .voice-bar.is-error::after {
    display: none;
  }

  .voice-dot {
    order: 0;
  }

  .voice-status {
    order: 1;
    /* Именно 0-базис: с basis:auto длинная подпись не влезает рядом с точкой
       и уезжает на отдельную строку, оставляя точку висеть одну. */
    flex: 1 1 0;
  }

  .voice-timer {
    order: 3;
    margin-right: auto;
  }

  .voice-actions {
    order: 4;
  }

  .voice-action {
    padding: 9px 18px;
  }
}

/* Телефон: кнопки ответа во всю ширину пузыря — по «пилюле» в 13px
   пальцем не попасть */
@media (max-width: 560px) {
  .quick-reply-group {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .quick-reply-btn {
    justify-content: center;
    padding: 12px 14px;
    font-size: 14px;
  }
}

/* Узкие телефоны: срезаем последние лишние отступы */
@media (max-width: 380px) {
  .chat-messages {
    padding: 12px 8px;
  }

  .message-row {
    max-width: 100%;
  }

  .chat-input-container {
    padding: 8px calc(8px + env(safe-area-inset-left)) calc(8px + env(safe-area-inset-bottom));
  }
}
