/* Чат-консультант: кнопка в углу экрана и окно переписки.
   Цвета берутся из переменных main.css, чтобы чат менялся вместе с сайтом.
   z-index 1030 — над шапкой сайта (999), но под модальными окнами (1040+). */

.chat-widget [hidden] {
    display: none !important;
}

/* ── Кнопка запуска ──────────────────────────────────────────────────── */

.chat-launcher {
    position: fixed;
    right: 30px;
    bottom: 30px;
    z-index: 1030;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--primary);
    color: var(--secondary);
    font-size: 26px;
    box-shadow: 0 10px 26px rgba(253, 172, 83, 0.45);
    cursor: pointer;
    transition: background-color 0.2s ease, transform 0.2s ease;
    /* Три волны после загрузки, дальше кнопка не отвлекает от страницы. */
    animation: chat-launcher-pulse 2.4s ease-out 1s 3;
}

.chat-launcher:hover,
.chat-launcher:focus-visible {
    background: var(--primary-hover);
    transform: translateY(-2px);
}

.chat-launcher-close,
.chat-widget.is-open .chat-launcher-open {
    display: none;
}

.chat-widget.is-open .chat-launcher-close {
    display: inline;
}

@keyframes chat-launcher-pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(253, 172, 83, 0.6);
    }

    70%,
    100% {
        box-shadow: 0 0 0 16px rgba(253, 172, 83, 0);
    }
}

/* Кнопка «наверх» живёт в том же углу: поднимаем её над кнопкой чата
   и выравниваем по центру с ней. */
body:has(.chat-widget) .back-to-top {
    right: 36px;
    bottom: 104px;
}

/* ── Окно ────────────────────────────────────────────────────────────── */

.chat-panel {
    position: fixed;
    right: 30px;
    bottom: 104px;
    z-index: 1030;
    display: flex;
    flex-direction: column;
    width: 380px;
    height: min(620px, calc(100vh - 140px));
    overflow: hidden;
    border-radius: 14px;
    background: var(--white);
    box-shadow: 0 18px 48px rgba(19, 19, 19, 0.22);
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px);
    /* visibility переключается после анимации закрытия, иначе окно
       исчезало бы рывком. */
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0s linear 0.2s;
}

.chat-widget.is-open .chat-panel {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 10px 14px 18px;
    background: var(--secondary);
    color: var(--white);
}

.chat-avatar {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: var(--secondary);
    font-size: 18px;
}

.chat-heading {
    flex: 1;
    min-width: 0;
}

.chat-title {
    margin: 0;
    color: var(--white);
    font-size: 1rem;
    font-weight: 700;
    line-height: 1.3;
}

.chat-subtitle {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0;
    color: rgba(255, 255, 255, 0.72);
    font-size: 0.8125rem;
    line-height: 1.35;
}

/* Зелёная точка «на связи». */
.chat-subtitle::before {
    content: "";
    flex: none;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #3ecf8e;
}

.chat-close {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--white);
    font-size: 30px;
    line-height: 1;
    opacity: 0.8;
    cursor: pointer;
}

.chat-close:hover,
.chat-close:focus-visible {
    background: rgba(255, 255, 255, 0.1);
    opacity: 1;
}

/* ── Переписка ───────────────────────────────────────────────────────── */

.chat-log {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 10px;
    padding: 18px 16px;
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--light);
}

.chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 0.9375rem;
    line-height: 1.5;
    /* Переносы строк из ответа сохраняются, длинные ссылки не рвут окно. */
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.chat-message--bot {
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    background: var(--white);
    color: var(--dark);
    box-shadow: 0 2px 8px rgba(31, 31, 31, 0.06);
}

.chat-message--user {
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    background: var(--primary);
    color: var(--secondary);
}

.chat-typing {
    display: inline-flex;
    gap: 4px;
    padding: 14px 16px;
}

.chat-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted);
    opacity: 0.4;
    animation: chat-typing 1.2s ease-in-out infinite;
}

.chat-typing span:nth-child(2) {
    animation-delay: 0.15s;
}

.chat-typing span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes chat-typing {
    0%,
    60%,
    100% {
        opacity: 0.4;
        transform: none;
    }

    30% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

.chat-notice {
    align-self: center;
    max-width: 92%;
    margin: 0;
    color: var(--muted);
    font-size: 0.8125rem;
    line-height: 1.4;
    text-align: center;
}

.chat-notice--error {
    color: #b42318;
}

.chat-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.chat-chip {
    padding: 7px 14px;
    border: 1.5px solid var(--primary);
    border-radius: 999px;
    background: rgba(253, 172, 83, 0.12);
    color: var(--primary-readable);
    font: inherit;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.3;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.chat-chip:hover,
.chat-chip:focus-visible {
    background: var(--primary);
    color: var(--secondary);
}

/* ── Форма заявки внутри переписки ───────────────────────────────────── */

.chat-lead {
    display: flex;
    flex: none;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--white);
}

.chat-lead-title {
    margin: 0;
    color: var(--secondary);
    font-weight: 700;
    line-height: 1.35;
}

.chat-lead .form-control {
    min-height: 44px;
}

.chat-lead-consent {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin: 0;
    font-size: 0.8125rem;
    line-height: 1.4;
    cursor: pointer;
}

.chat-lead-consent input {
    flex: none;
    width: 18px;
    height: 18px;
    margin-top: 1px;
    accent-color: var(--primary-readable);
}

.chat-lead-error {
    margin: 0;
    color: #b42318;
    font-size: 0.8125rem;
    line-height: 1.4;
}

.chat-lead-error:empty {
    display: none;
}

/* ── Поле ввода и подвал ─────────────────────────────────────────────── */

.chat-captcha {
    padding: 0 12px;
    background: var(--white);
}

/* Cloudflare попросил отметить галочку: подсказка над ней, а «печатает»
   прячем — ответ сейчас ждёт посетителя, а не бота. */
.chat-captcha-hint {
    margin: 0;
    padding: 10px 16px 8px;
    border-top: 1px solid var(--line);
    background: var(--white);
    color: var(--dark);
    font-size: 0.8125rem;
    line-height: 1.4;
}

.chat-widget.is-verifying .chat-captcha {
    padding-bottom: 8px;
}

.chat-widget.is-verifying .chat-typing {
    display: none;
}

.chat-composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 12px 8px;
    border-top: 1px solid var(--line);
    background: var(--white);
}

.chat-input {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    max-height: 140px;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 22px;
    color: var(--dark);
    /* 16px — меньше iOS увеличивает страницу при фокусе на поле. */
    font: inherit;
    font-size: 16px;
    line-height: 1.4;
    resize: none;
    overflow-y: auto;
}

.chat-input:focus,
.chat-input:focus-visible {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 0.2rem var(--focus-ring);
}

.chat-input:disabled {
    background: var(--light);
    cursor: not-allowed;
}

.chat-send {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--primary);
    color: var(--secondary);
    font-size: 17px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.chat-send:hover,
.chat-send:focus-visible {
    background: var(--primary-hover);
}

.chat-send:disabled {
    opacity: 0.5;
    cursor: default;
}

.chat-footer {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 4px 12px;
    padding: 0 16px calc(10px + env(safe-area-inset-bottom));
    background: var(--white);
    color: var(--muted);
    font-size: 0.75rem;
    line-height: 1.4;
}

.chat-footer-note {
    flex-basis: 100%;
    margin: 0;
}

.chat-footer-link {
    padding: 0;
    border: 0;
    background: none;
    color: var(--primary-readable);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.chat-footer-link:hover,
.chat-footer-link:focus-visible {
    text-decoration: underline;
}

/* ── Адаптив ─────────────────────────────────────────────────────────── */

/* Планшеты: те же отступы от края, что у кнопки «наверх» в main.css. */
@media (max-width: 767.98px) {
    .chat-launcher {
        right: 18px;
        bottom: calc(18px + env(safe-area-inset-bottom));
        width: 56px;
        height: 56px;
        font-size: 24px;
    }

    .chat-panel {
        right: 18px;
        bottom: calc(86px + env(safe-area-inset-bottom));
    }

    body:has(.chat-widget) .back-to-top {
        right: 22px;
        bottom: calc(86px + env(safe-area-inset-bottom));
    }
}

/* Телефон и низкий экран (телефон боком): окно на весь экран. Кнопку
   запуска прячем — закрывает крестик в шапке. Высоту по видимой области
   подгоняет chat.js, чтобы поле ввода не уезжало под клавиатуру. */
@media (max-width: 575.98px), (max-height: 500px) {
    .chat-panel {
        inset: 0;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        border-radius: 0;
        transform: translateY(24px);
    }

    .chat-widget.is-open .chat-launcher {
        display: none;
    }

    body.chat-open {
        overflow: hidden;
    }

    .chat-message {
        max-width: 90%;
    }
}

@media (prefers-reduced-motion: reduce) {
    .chat-launcher,
    .chat-panel,
    .chat-typing span {
        animation: none;
        transition: none;
    }
}

@media print {
    .chat-widget {
        display: none !important;
    }
}
