/*
 * 📝 МОДУЛЬ: Оформление чата, v2.3.0.
 * 🎯 НАЗНАЧЕНИЕ: компактный диалог на ПК и мобильном экране.
 * 🧩 СВЯЗИ: chat_widget.html; отдельное подключение из base.html.
 * ⚠️ ПРАВИЛА: собственные селекторы, native dialog и видимый фокус.
 * 🔄 ПОТОК: FAB → вертикальная панель на месте кнопки → история → закрытие.
 * 🎨 ЛОГИРОВАНИЕ: не применяется.
 *             2.3.0 (2026-10-01) — карточки с иконками, акценты и доступные состояния.
 * 🆕 ИСТОРИЯ: 1.0.0 (2026-09-30) — первый интерфейс.
 *             2.1.0 (2026-09-30) — закрепление слева внизу, единый размер экранов.
 *             2.2.0 (2026-10-01) — перенос вправо, кнопка открытия 64 × 64 px.
 */
.chat-fab {
    position: fixed;
    right: 20px;
    bottom: calc(24px + env(safe-area-inset-bottom));
    z-index: 1000;
    display: grid;
    place-items: center;
    width: 64px;
    height: 64px;
    border: 0;
    border-radius: 50%;
    background: #e93636;
    color: #fff;
    box-shadow: 0 8px 24px #e9363633, 0 2px 6px #20263214;
    transition: transform .18s ease, box-shadow .18s ease;
    cursor: pointer;
}

.chat-dialog,
.chat-dialog * {
    box-sizing: border-box;
}

.chat-fab svg {
    width: 30px;
    height: 30px;
}

.chat-dialog {
    transform-origin: bottom right;
    position: fixed;
    inset: auto 20px calc(24px + env(safe-area-inset-bottom)) auto;
    z-index: 10010;
    width: min(340px, calc(100vw - 40px));
    min-width: min(300px, calc(100vw - 40px));
    max-width: calc(100vw - 40px);
    min-height: min(320px, calc(100dvh - 48px - env(safe-area-inset-bottom)));
    height: min(520px, calc(100dvh - 48px - env(safe-area-inset-bottom)));
    max-height: calc(100dvh - 48px - env(safe-area-inset-bottom));
    margin: 0;
    padding: 16px;
    overflow: auto;
    border: 1px solid #e5e7eb;
    border-radius: 24px;
    background: #fff;
    color: #30343b;
    box-shadow: 0 20px 64px #20263226, 0 2px 8px #2026320a;
}

.chat-dialog[open] {
    display: flex;
    flex-direction: column;
}

/* Отдельный нижний маркер изменения размера, без наложения на сообщения. */
#chat-resize {
    cursor: nesw-resize;
    touch-action: none;
}

.chat-fab[hidden] {
    display: none;
}

#chat-conversation:not([hidden]) {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.chat-menu {
    margin-top: 16px;
    flex: 1;
    overflow-y: auto;
}

.chat-choice {
    display: flex;
    flex-direction: column;
    gap: 5px;
    width: 100%;
    margin: 10px 0;
    padding: 16px 30px 16px 68px;
    position: relative;
    min-height: 80px;
    text-align: left;
    font: inherit;
    color: inherit;
    background: #fff;
    border: 1px solid #d2ceca;
    border-radius: 12px;
    cursor: pointer;
}

.chat-choice span {
    color: #68717c;
    font-size: 14px;
    overflow-wrap: anywhere;
}

/* Мягкие карточки и единый акцент для выбора диалога. */
.chat-choice {
    border-color: #eceef1;
    transition: background-color .18s ease, border-color .18s ease;
}

.chat-choice:hover {
    background: #fafafa;
    border-color: #e9bcbc;
}

#chat-support {
    background: #fff7f7;
    border-color: #f4dcdc;
}

.chat-list-heading {
    margin: 20px 0 8px;
    font-size: 12px;
    letter-spacing: .05em;
    text-transform: uppercase;
    color: #747b85;
    font-weight: 700;
}

#chat-support {
    position: sticky;
    top: 0;
    z-index: 1;
}

#chat-more {
    padding: 10px;
    border: 1px solid #d2ceca;
    border-radius: 10px;
    background: #fff;
    font: inherit;
    cursor: pointer;
}

.chat-dialog [hidden] {
    display: none;
}

.chat-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    flex: none;
    padding-bottom: 12px;
    border-bottom: 1px solid #eceef1;
}

.chat-heading h2 {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    overflow-wrap: anywhere;
}

.chat-heading button {
    display: grid;
    place-items: center;
    flex: none;
    width: 40px;
    height: 40px;
    border: 0;
    background: transparent;
    color: #454b55;
    padding: 0;
    border-radius: 12px;
    cursor: pointer;
}

.chat-notice,
.chat-status {
    margin: 12px 0;
    color: #68717c;
    font-size: 13px;
}

.chat-history {
    flex: 1;
    min-height: 60px;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 8px;
    border-radius: 16px;
    background: #fff;
    scrollbar-width: thin;
    scrollbar-color: #cbd0d7 transparent;
}

.chat-message {
    margin: 8px 0;
    padding: 10px 12px;
    border-radius: 12px;
    background: #fff;
    border: 1px solid #e9ecf0;
    box-shadow: 0 2px 4px #20263204;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    line-height: 1.5;
    margin-right: 24px;
}

.chat-message--guest {
    margin-left: 20px;
    background: #fff;
    border-color: #e9ecf0;
    margin-right: 0;
    border-bottom-right-radius: 4px;
}

.chat-message small {
    display: block;
    margin-top: 6px;
    color: #68717c;
    font-size: 11px;
}

#chat-form textarea {
    display: block;
    width: 100%;
    margin: 8px 0;
    padding: 12px 14px;
    border: 1px solid #d8dce2;
    border-radius: 14px;
    background: #fafbfc;
    color: #30343b;
    line-height: 1.5;
    resize: none;
    font: inherit;
    font-size: 16px;
}

#chat-send {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    font: inherit;
    font-weight: 600;
    width: 100%;
    padding: 10px 18px;
    border: 0;
    border-radius: 10px;
    background: #e93636;
    color: #fff;
    cursor: pointer;
}

#chat-form label {
    font-size: 13px;
    color: #68717c;
}

#chat-send:hover:not(:disabled) {
    background: #d92d2d;
}

.chat-status:empty,
.chat-notice:empty {
    display: none;
}

#chat-send:disabled {
    opacity: .55;
    cursor: wait;
}

.chat-dialog :focus-visible,
.chat-fab:focus-visible {
    outline: 2px solid #b52323;
    outline-offset: 3px;
}

@media (max-width: 480px) {
    .chat-fab {
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom));
    }

    .chat-dialog {
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom));
        width: calc(100vw - 24px);
        max-width: calc(100vw - 24px);
        min-width: min(300px, calc(100vw - 24px));
        min-height: min(360px, calc(100dvh - 24px - env(safe-area-inset-bottom)));
        height: min(600px, calc(100dvh - 24px - env(safe-area-inset-bottom)));
        max-height: calc(100dvh - 24px - env(safe-area-inset-bottom));
        padding: 16px;
    }
}

.chat-heading button:hover {
    background: #f3f4f6;
}

.chat-heading button svg {
    display: block;
    width: 22px;
    height: 22px;
    stroke-width: 2;
}

/* Декоративные значки не заменяют читаемые названия диалогов. */
.chat-choice::before {
    content: "";
    position: absolute;
    left: 16px;
    top: 20px;
    width: 36px;
    height: 36px;
    border-radius: 12px;
    background: #f2f4f7 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='22' height='22' viewBox='0 0 24 24' fill='none' stroke='%2368717c' stroke-width='1.7' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='4' y='5' width='16' height='16' rx='3'/%3E%3Cpath d='M8 3v4m8-4v4M4 11h16m-11 5h6'/%3E%3C/svg%3E") center / 22px no-repeat;
}

#chat-support::before {
    background: #ffe4e4 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='22' height='22' viewBox='0 0 24 24' fill='none' stroke='%23d62e2e' stroke-width='1.7' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 13v-2a8 8 0 0 1 16 0v6a4 4 0 0 1-4 4h-3'/%3E%3Crect x='3' y='11' width='4' height='7' rx='2'/%3E%3Crect x='17' y='11' width='4' height='7' rx='2'/%3E%3C/svg%3E") center / 22px no-repeat;
}

.chat-choice::after {
    content: "";
    position: absolute;
    right: 16px;
    top: calc(50% - 3px);
    width: 6px;
    height: 6px;
    border-top: 1.5px solid #929aa5;
    border-right: 1.5px solid #929aa5;
    transform: rotate(45deg);
}

.chat-choice strong {
    font-size: 15px;
    line-height: 1.4;
    overflow-wrap: anywhere;
}

#chat-form {
    flex: none;
    padding-top: 12px;
}

#chat-form textarea:focus {
    background: #fff;
    border-color: #e93636;
}

#chat-send svg {
    width: 18px;
    height: 18px;
    flex: none;
}

.chat-fab:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 30px #e9363640;
}

.chat-heading {
    border-top: 3px solid #e93636;
    padding-top: 10px;
}

@media (prefers-reduced-motion: reduce) {
    .chat-fab,
    .chat-choice {
        transition: none;
    }
    .chat-fab:hover {
        transform: none;
    }
}

.chat-resize-footer {
    display: flex;
    flex: none;
    justify-content: flex-start;
    margin-top: 4px;
}

#chat-resize {
    display: grid;
    place-items: center;
    width: 40px;
    height: 32px;
    padding: 0;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: #68717c;
}

#chat-resize:hover {
    background: #f3f4f6;
    color: #30343b;
}

#chat-resize svg {
    display: block;
    width: 22px;
    height: 22px;
}

/* Имя гостя необязательно; сохраняется вместе с отправленным сообщением. */
#chat-guest-name {
    display: block;
    width: 100%;
    margin: 6px 0 12px;
    padding: 10px 12px;
    border: 1px solid #d8dce2;
    border-radius: 12px;
    background: #fafbfc;
    color: #30343b;
    font: inherit;
    font-size: 16px;
}

/* Счётчик входящих: общий на кнопке, текущего диалога в заголовке. */
.chat-unread-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 999px;
    background: #e93636;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
}

#chat-fab-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    border: 2px solid #fff;
    min-width: 26px;
    height: 26px;
    background: #30343b;
}

.chat-unread-badge[hidden] {
    display: none;
}

/* Одна рамка фокуса; высотой поля управляет автоподстройка по тексту. */
#chat-form textarea {
    box-sizing: border-box;
    min-height: 44px;
    height: 44px;
    max-height: 160px;
    padding: 10px 12px;
    resize: none;
    overflow-y: hidden;
}

#chat-form textarea:focus,
#chat-form textarea:focus-visible {
    outline: none;
    box-shadow: none;
    border-color: #e93636;
}

#chat-guest-name:focus,
#chat-guest-name:focus-visible {
    outline: none;
    box-shadow: none;
    border-color: #e93636;
}

/* Маркер не занимает строку: появляется в рабочем углу при наведении. */
.chat-resize-footer {
    position: absolute;
    left: 0;
    bottom: 0;
    margin: 0;
    width: 44px;
    height: 36px;
    z-index: 2;
}

#chat-resize {
    width: 44px;
    height: 36px;
    opacity: 0;
    transition: opacity .15s ease;
}

.chat-resize-footer:hover #chat-resize,
#chat-resize:focus-visible,
#chat-resize:active {
    opacity: 1;
}

/* Полноэкранный чат на телефоне, включая поворот и безопасные отступы. */
@media (max-width: 700px) {
    .chat-dialog {
        inset: 0;
        width: 100% !important;
        min-width: 0;
        max-width: 100%;
        height: 100dvh !important;
        min-height: 0;
        max-height: 100dvh;
        border: 0;
        border-radius: 0;
        padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
        overflow: hidden;
    }
    .chat-resize-footer {
        display: none;
    }
    body:has(.chat-dialog[open]) {
        overflow: hidden;
    }
    .chat-menu {
        min-height: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    #chat-resize {
        transition: none;
    }
}

/* Кнопка чата участвует в общей загрузке даже вне #realContent. */
.chat-fab-skeleton {
    display: none;
    position: fixed;
    right: 20px;
    bottom: calc(24px + env(safe-area-inset-bottom));
    width: 64px;
    height: 64px;
    border-radius: 50%;
    z-index: 1000;
    pointer-events: none;
}

.page-skeleton-loading .chat-fab-skeleton {
    display: block;
}

.page-skeleton-loading .chat-fab,
.page-skeleton-loading .chat-dialog {
    visibility: hidden;
    pointer-events: none;
}

@media (max-width: 480px) {
    .chat-fab-skeleton {
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom));
    }
}
