/* Чат-виджет: FAB снизу справа + раскрывающаяся панель.
   Не зависит от landing.js / loan_modal.css — изолированный feature. */

/* Cookie-баннер из Nuxt-разметки сидит в нижнем-левом углу и на узких
   экранах залезает на нашу FAB. Поднимаем его над кнопкой чата, пока
   баннер виден. После принятия cookie скрывается, и правило не мешает. */
@media (max-width: 768px) {
  .cookie {
    bottom: calc(72px + max(12px, env(safe-area-inset-bottom))) !important;
  }
}

/* Когда панель чата открыта (.chat-widget.is-open — добавляется из JS),
   она должна перекрывать всё, включая cookie-баннер (у которого z-index 999999
   из Nuxt-CSS). И сам cookie прячем — он бесполезен поверх чата. */
.chat-widget.is-open { z-index: 10000000; }
html.chat-widget-open .cookie { display: none !important; }

.chat-widget {
  position: fixed;
  /* Учёт безопасной зоны на iPhone (home indicator) и устройствах с
     закруглёнными краями (Android edge-to-edge). */
  right: max(24px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 9999;
  font-family: Rubik, Roboto, Helvetica, sans-serif;
  color: #282d3c;
  -webkit-tap-highlight-color: transparent;
}

.chat-widget__fab {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: #1fadff;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(31, 173, 255, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background 0.2s;
  position: relative;
  -webkit-appearance: none;
          appearance: none;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}
.chat-widget__fab:hover { background: #009bf5; transform: scale(1.04); }
.chat-widget__fab:active { transform: scale(0.96); }
.chat-widget__fab:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}
.chat-widget__fab svg { width: 26px; height: 26px; fill: currentColor; }

.chat-widget__badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: #ea4e3d;
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 20px;
}
.chat-widget__badge[data-count]:not([data-count="0"]) { display: flex; }

.chat-widget__panel {
  position: absolute;
  right: 0;
  bottom: 72px;
  width: 360px;
  max-width: calc(100vw - 32px);
  height: 520px;
  /* iOS Safari: 100vh захватывает URL-bar, панель уходит за экран. dvh решает,
     но для старых движков (Safari < 16, WebView Android < 108) держим vh-фолбэк. */
  max-height: calc(100vh - 120px);
  max-height: calc(100dvh - 120px);
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(40, 40, 40, 0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
  animation: chat-widget-pop 0.16s ease;
}
/* `display: flex` выше перекрывает UA-стиль [hidden]{display:none}.
   Явное правило с более высокой специфичностью возвращает скрытие. */
.chat-widget__panel[hidden] { display: none; }
@keyframes chat-widget-pop {
  from { opacity: 0; transform: scale(0.96) translateY(8px); }
  to   { opacity: 1; transform: scale(1)    translateY(0); }
}

.chat-widget__header {
  background: #1fadff;
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.chat-widget__title { font-size: 16px; font-weight: 500; }
.chat-widget__subtitle { font-size: 12px; opacity: 0.85; margin-top: 2px; }
.chat-widget__close {
  background: transparent;
  border: none;
  color: #fff;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  /* Минимально комфортная зона нажатия для пальца. */
  min-width: 32px;
  min-height: 32px;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.15s;
  -webkit-appearance: none;
          appearance: none;
  -webkit-tap-highlight-color: transparent;
}
.chat-widget__close:hover { background: rgba(255, 255, 255, 0.18); }
.chat-widget__close:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.chat-widget__messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* Сглаживание скролла на старых WebView Android */
  overscroll-behavior: contain;
  padding: 16px;
  background: #f4f6fb;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-widget__empty {
  margin: auto;
  color: #778095;
  font-size: 13px;
  text-align: center;
  line-height: 1.5;
}

.chat-widget__msg {
  max-width: 80%;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.chat-widget__msg--user {
  align-self: flex-end;
  background: #1fadff;
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-widget__msg--admin {
  align-self: flex-start;
  background: #fff;
  color: #282d3c;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(40, 40, 40, 0.06);
}
.chat-widget__msg-time {
  display: block;
  font-size: 10px;
  opacity: 0.7;
  margin-top: 4px;
}

.chat-widget__form {
  display: flex;
  gap: 8px;
  padding: 12px;
  /* На полноэкранном чате (≤360px) низ формы должен учитывать home indicator. */
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid #d9dce3;
}
.chat-widget__textarea {
  flex: 1;
  resize: none;
  border: 1px solid #d9dce3;
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  /* iOS Safari масштабирует страницу при фокусе на input < 16px. */
  font-size: 16px;
  line-height: 1.4;
  max-height: 100px;
  /* 48px: 22.4px line-height + 20px вертикальный padding + 2px бордера =
     44.4px content area нужно ≥ 22.4px, иначе нижняя строка плейсхолдера
     визуально срезается на iPhone SE (320×568). 48 даёт безопасный запас. */
  min-height: 48px;
  outline: none;
  background: #f4f6fb;
  color: #282d3c;
  -webkit-appearance: none;
          appearance: none;
  box-sizing: border-box;
}
.chat-widget__textarea:focus { background: #fff; border-color: #1fadff; }
.chat-widget__send {
  border: none;
  background: #1fadff;
  color: #fff;
  border-radius: 10px;
  padding: 0 16px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  align-self: flex-end;
  height: 48px;
  transition: background 0.15s;
  -webkit-appearance: none;
          appearance: none;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}
.chat-widget__send:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}
.chat-widget__send:hover:not(:disabled) { background: #009bf5; }
.chat-widget__send:disabled { background: #a3abbd; cursor: not-allowed; }

.chat-widget__error {
  color: #ea4e3d;
  font-size: 12px;
  padding: 0 12px 8px;
  background: #fff;
  display: none;
}
.chat-widget__error.is-visible { display: block; }

@media (max-width: 480px) {
  .chat-widget {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
  }
  .chat-widget__panel {
    width: calc(100vw - 24px);
    height: calc(100vh - 100px);
    height: calc(100dvh - 100px);
    max-height: calc(100vh - 100px);
    max-height: calc(100dvh - 100px);
    bottom: 72px;
  }
}

/* Совсем узкие экраны (старые телефоны / split-screen): панель занимает
   практически весь экран, чтобы клавиатура и сообщения помещались. */
@media (max-width: 360px) {
  .chat-widget__panel {
    position: fixed;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
  }
}

/* Уважаем «уменьшить движение» в системе. */
@media (prefers-reduced-motion: reduce) {
  .chat-widget__panel { animation: none; }
  .chat-widget__fab { transition: none; }
}
