/* ══════════════════════════════════════════════════════════════
   Chat-widget — AI-агент Аня для pmkelm.ru
   Стиль соответствует основному сайту (Steel-blue + IBM Plex Mono + Jost)
   ══════════════════════════════════════════════════════════════ */

/* === FAB (кнопка вызова) === */
.chat-fab {
  position: fixed;
  bottom: 1.4rem;
  right: 1.4rem;
  z-index: 9999;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--ac) 0%, #6a7e9a 100%);
  border: 1px solid rgba(140, 196, 230, 0.4);
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 0 0 1px rgba(140, 196, 230, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bg);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}
[data-theme="light"] .chat-fab {
  background: linear-gradient(145deg, #3A5268 0%, #2a3e52 100%);
  color: #F0EEE9;
}
.chat-fab:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.42), 0 0 0 1px rgba(140, 196, 230, 0.25);
}
.chat-fab svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chat-fab .chat-fab-pulse {
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid rgba(140, 196, 230, 0.5);
  animation: chat-fab-pulse 2.4s ease-out infinite;
  pointer-events: none;
}
@keyframes chat-fab-pulse {
  0% { transform: scale(1); opacity: 0.7; }
  100% { transform: scale(1.4); opacity: 0; }
}
.chat-fab.open .chat-fab-pulse { display: none; }
.chat-fab .chat-fab-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #4AB870;
  border: 2px solid var(--bg);
  z-index: 1;
}

/* === Окно чата === */
.chat-window {
  position: fixed;
  bottom: 6rem;
  right: 1.4rem;
  z-index: 9998;
  width: 380px;
  max-width: calc(100vw - 2rem);
  height: 600px;
  max-height: calc(100vh - 8rem);
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.45),
    0 0 0 1px rgba(140, 196, 230, 0.10);
  display: none;
  flex-direction: column;
  overflow: hidden;
  font-family: 'Jost', sans-serif;
}
.chat-window.open {
  display: flex;
  animation: chat-slide-up 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
@keyframes chat-slide-up {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Mobile — fullscreen с учётом клавиатуры и safe-area iPhone (Dynamic Island, Home Indicator) */
@media (max-width: 36rem) {
  .chat-window {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    height: 100vh;          /* fallback */
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }
  .chat-fab {
    bottom: calc(1rem + env(safe-area-inset-bottom));
    right: calc(1rem + env(safe-area-inset-right));
    width: 56px;
    height: 56px;
  }
  /* Шапка чата уезжает под Dynamic Island без этого */
  .chat-header {
    padding-top: calc(1rem + env(safe-area-inset-top));
    padding-left: calc(1.2rem + env(safe-area-inset-left));
    padding-right: calc(1.2rem + env(safe-area-inset-right));
  }
  /* Поле ввода упирается в Home Indicator без этого */
  .chat-footer {
    padding-bottom: calc(0.8rem + env(safe-area-inset-bottom));
    padding-left: calc(1.2rem + env(safe-area-inset-left));
    padding-right: calc(1.2rem + env(safe-area-inset-right));
  }
  .chat-body {
    padding-left: calc(1.2rem + env(safe-area-inset-left));
    padding-right: calc(1.2rem + env(safe-area-inset-right));
  }
}

/* Блокируем скролл body только на мобайле — на десктопе чат сбоку, страница должна скроллиться */
@media (max-width: 36rem) {
  body.chat-open {
    overflow: hidden;
    position: fixed;
    inset: 0;
    width: 100%;
    overscroll-behavior: contain;
  }
}

/* === Заголовок чата === */
.chat-header {
  padding: 1rem 1.2rem;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
}
.chat-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--ai) 0%, var(--ac) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Onest', sans-serif;
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--bg);
  flex-shrink: 0;
  position: relative;
}
.chat-avatar::after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  background: #4AB870;
  border-radius: 50%;
  border: 2px solid var(--bg);
}
.chat-header-text {
  flex: 1;
  min-width: 0;
}
.chat-header-name {
  font-family: 'Jost', sans-serif;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--tx);
  line-height: 1.2;
}
.chat-header-status {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #4AB870;
  margin-top: 2px;
}
.chat-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--td);
  cursor: pointer;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
  flex-shrink: 0;
}
.chat-close:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--tx);
}
.chat-close svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; }
.chat-clear {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--td);
  cursor: pointer;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
  flex-shrink: 0;
  opacity: 0.5;
}
.chat-clear:hover { background: rgba(255, 255, 255, 0.06); color: var(--tx); opacity: 1; }
.chat-clear svg { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* === Body — список сообщений === */
.chat-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  background: var(--bg2);
}
.chat-body::-webkit-scrollbar { width: 6px; }
.chat-body::-webkit-scrollbar-track { background: transparent; }
.chat-body::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.chat-msg {
  max-width: 85%;
  padding: 0.65rem 0.9rem;
  border-radius: 12px;
  font-size: 0.88rem;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.chat-msg-bot {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--tx);
  border-bottom-left-radius: 2px;
}
.chat-msg-user {
  align-self: flex-end;
  background: linear-gradient(145deg, var(--ac) 0%, #6a7e9a 100%);
  color: var(--bg);
  border-bottom-right-radius: 2px;
}
[data-theme="light"] .chat-msg-user {
  background: linear-gradient(145deg, #3A5268 0%, #2a3e52 100%);
  color: #F0EEE9;
}
.chat-msg-meta {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.6rem;
  color: var(--td);
  opacity: 0.5;
  margin-top: 4px;
  letter-spacing: 0.05em;
}
.chat-msg-bot .chat-msg-meta { text-align: left; }
.chat-msg-user .chat-msg-meta { text-align: right; }

/* "Печатает..." индикатор */
.chat-typing {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.7rem 0.9rem;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  border-bottom-left-radius: 2px;
}
.chat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--td);
  opacity: 0.5;
  animation: chat-dot 1.4s ease-in-out infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.18s; }
.chat-typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes chat-dot {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* Quick-reply chips */
.chat-quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0.4rem 0 0.2rem;
}
.chat-quick {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  padding: 0.4rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--ac);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  font-weight: 500;
}
.chat-quick:hover {
  background: rgba(128, 144, 168, 0.12);
  border-color: var(--ac);
  color: var(--tx);
}

/* === Footer — поле ввода === */
.chat-footer {
  padding: 0.8rem 1.2rem;
  border-top: 1px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
}
.chat-input-row {
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
}
.chat-input {
  flex: 1;
  background: var(--bg2);
  border: 1px solid var(--line);
  color: var(--tx);
  font-family: 'Jost', sans-serif;
  font-size: 0.88rem;
  padding: 0.6rem 0.85rem;
  border-radius: 8px;
  outline: none;
  resize: none;
  min-height: 38px;
  max-height: 120px;
  line-height: 1.4;
  transition: border-color 0.15s ease;
}
.chat-input:focus { border-color: var(--ac); }
.chat-input::placeholder { color: var(--td); opacity: 0.5; }
.chat-send {
  width: 38px;
  height: 38px;
  background: var(--ac);
  border: none;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--bg);
  transition: background 0.15s ease, transform 0.1s ease;
  flex-shrink: 0;
}
[data-theme="light"] .chat-send { background: #3A5268; color: #F0EEE9; }
.chat-send:hover { background: var(--ai); }
.chat-send:disabled { opacity: 0.4; cursor: not-allowed; }
.chat-send svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.chat-disclaimer {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.58rem;
  color: var(--td);
  opacity: 0.5;
  text-align: center;
  margin-top: 0.5rem;
  letter-spacing: 0.05em;
}

/* На мобайле скрываем FAB когда окно чата открыто */
@media (max-width: 36rem) {
  .chat-fab {
    transition: opacity 0.2s ease, transform 0.2s ease;
  }
  .chat-fab.open {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.7);
  }
}
