/* ============================================================
   БИНЭКСТ — чат-виджет для основного сайта. v3.1
   
   v3.1: кнопка бота на мобильных поднята над таббаром сайта
         (переменная --bnx-tabbar-h) + safe-area для iPhone;
         окно чата не перекрывает таббар и не выходит за экран.
   v3.0: позиции списком (карточки) вместо таблиц.
   v2.9: таблицы (fallback, если агент всё же прислал).
   v2.8: компактные быстрые действия + ссылка «очистить историю».
   v2.6: кнопка-бот с анимацией (моргание, антенна).
   v2.4: перетаскивание окна за заголовок.
   ============================================================ */

/* высота нижнего таббара сайта — правьте здесь, если перекрывает */
:root {
    --bnx-tabbar-h: 64px;
}

/* ---------- контейнер ---------- */
#bnx-ai-chat {
    position: fixed;
    z-index: 99999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ---------- плавающая кнопка-бот ---------- */
.bnx-chat-fab {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0066cc 0%, #0052a3 100%);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
    z-index: 99999;
}

.bnx-chat-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 102, 204, 0.6);
}

.bnx-chat-fab svg {
    fill: #fff;
}

/* ---------- бейдж корзины на кнопке ---------- */
.bnx-chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    background: #ff4444;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    box-shadow: 0 2px 6px rgba(255, 68, 68, 0.4);
}

/* ---------- окно чата ---------- */
.bnx-chat-window {
    position: fixed;
    bottom: 96px;
    right: 20px;
    width: 380px;
    height: 560px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    transition: opacity 0.3s, transform 0.3s, visibility 0.3s;
    z-index: 100000;
}

.bnx-chat-window.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

/* ---------- заголовок ---------- */
.bnx-chat-header {
    background: #0066cc;
    color: #fff;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-radius: 12px 12px 0 0;
}

.bnx-chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    flex-shrink: 0;
}

.bnx-chat-header-info {
    flex: 1;
    min-width: 0;
}

.bnx-chat-title {
    font-weight: 600;
    font-size: 16px;
}

.bnx-chat-subtitle {
    font-size: 12px;
    opacity: 0.9;
}

/* ---------- ссылка «очистить историю» ---------- */
.bnx-chat-clear-text {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.75);
    font-size: 10px;
    cursor: pointer;
    padding: 4px 6px;
    text-decoration: underline;
    text-underline-offset: 2px;
    white-space: nowrap;
    transition: color 0.2s;
    flex-shrink: 0;
}

.bnx-chat-clear-text:hover {
    color: #fff;
}

.bnx-chat-close {
    background: none;
    border: none;
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.8;
    transition: opacity 0.2s;
    flex-shrink: 0;
}

.bnx-chat-close:hover {
    opacity: 1;
}

/* ---------- компактные кнопки быстрых действий ---------- */
.bnx-chat-quick-actions {
    display: flex;
    gap: 6px;
    padding: 6px 10px;
    background: #f9f9f9;
    border-bottom: 1px solid #eee;
}

.bnx-chat-quick-action {
    flex: 1;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 4px 6px;
    cursor: pointer;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 5px;
    transition: background 0.2s, border-color 0.2s;
}

.bnx-chat-quick-action:hover {
    background: #f0f8ff;
    border-color: #0066cc;
}

.bnx-quick-icon {
    font-size: 14px;
    line-height: 1;
}

.bnx-quick-label {
    font-size: 11px;
    color: #333;
    white-space: nowrap;
}

/* ---------- сообщения ---------- */
.bnx-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 0;
}

.bnx-chat-message {
    display: flex;
    gap: 8px;
}

.bnx-chat-message.bnx-ai {
    align-items: flex-start;
}

.bnx-chat-message.bnx-user {
    align-items: flex-start;
    justify-content: flex-end;
}

.bnx-chat-avatar-small {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.bnx-chat-bubble {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}

.bnx-chat-message.bnx-ai .bnx-chat-bubble {
    background: #f5f5f5;
    color: #333;
    border-bottom-left-radius: 4px;
}

.bnx-chat-message.bnx-user .bnx-chat-bubble {
    background: #0066cc;
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* ---------- подсказки ---------- */
.bnx-chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.bnx-chat-suggestion {
    background: #f0f8ff;
    border: 1px solid #0066cc;
    color: #0066cc;
    padding: 8px 12px;
    border-radius: 16px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.2s;
}

.bnx-chat-suggestion:hover {
    background: #0066cc;
    color: #fff;
}

/* ---------- уведомление при долгом ответе ---------- */
.bnx-chat-long-response {
    display: none;
    background: #fff3cd;
    color: #856404;
    padding: 8px 16px;
    font-size: 13px;
    text-align: center;
    border-top: 1px solid #ffeaa7;
}

/* ---------- индикатор набора ---------- */
.bnx-chat-typing {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 0 16px 8px;
}

.bnx-chat-typing-dots {
    display: flex;
    gap: 4px;
    padding: 8px 12px;
    background: #f5f5f5;
    border-radius: 16px;
}

.bnx-chat-typing-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #999;
    animation: bnx-typing 1.4s infinite;
}

.bnx-chat-typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.bnx-chat-typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bnx-typing {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-8px); }
}

/* ---------- поле ввода ---------- */
.bnx-chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid #eee;
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.bnx-chat-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 10px 14px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    max-height: 100px;
    overflow-y: auto;
    min-width: 0;
}

.bnx-chat-input:focus {
    outline: none;
    border-color: #0066cc;
}

.bnx-chat-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #0066cc;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    transition: background 0.2s;
    flex-shrink: 0;
}

.bnx-chat-send:hover {
    background: #0052a3;
}

/* ---------- v2.4: перетаскивание окна ---------- */
.bnx-chat-header {
    cursor: move;
    user-select: none;
    -webkit-user-select: none;
}

.bnx-chat-header.dragging {
    cursor: grabbing;
}

.bnx-chat-window.dragging {
    transition: none;
    box-shadow: 0 12px 50px rgba(0, 0, 0, 0.35);
}

.bnx-chat-window.dragging,
.bnx-chat-window.dragging * {
    transition: none !important;
}

/* ---------- v2.6: кнопка-бот с анимацией ---------- */
.bnx-chat-fab:hover .bnx-bot-svg {
    animation: bnx-bot-wiggle 0.6s ease-in-out;
}

.bnx-bot-eye {
    animation: bnx-bot-blink 4s infinite;
    transform-origin: center;
}

@keyframes bnx-bot-blink {
    0%, 90%, 100% { transform: scaleY(1); }
    93%, 96% { transform: scaleY(0.1); }
}

.bnx-bot-antenna {
    animation: bnx-bot-pulse 2s infinite;
}

@keyframes bnx-bot-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

@keyframes bnx-bot-wiggle {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-8deg); }
    75% { transform: rotate(8deg); }
}

/* ---------- v2.9: таблицы (fallback) ---------- */
.bnx-chat-table {
    width: 100%;
    border-collapse: collapse;
    margin: 8px 0;
    font-size: 12px;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.bnx-chat-table th {
    background: #0066cc;
    color: #fff;
    padding: 6px 8px;
    text-align: left;
    font-weight: 600;
    white-space: nowrap;
}

.bnx-chat-table td {
    padding: 6px 8px;
    border-bottom: 1px solid #e0e0e0;
    color: #333;
    vertical-align: top;
}

.bnx-chat-table tbody tr:nth-child(even) {
    background: #f5f9ff;
}

.bnx-chat-table tbody tr:hover {
    background: #e8f2ff;
}

/* ---------- v3.0: позиции списком ---------- */
.bnx-item-name {
    font-weight: 600;
    color: #222;
    margin-top: 8px;
    line-height: 1.4;
}

.bnx-item-meta {
    font-size: 11px;
    color: #667788;
    margin: 2px 0 4px;
    line-height: 1.5;
}

.bnx-item-name:first-child {
    margin-top: 0;
}
.bnx-chat-size {
    flex: 0 0 auto;
    width: 28px; height: 28px;
    border: none;
    background: transparent;
    color: #ffffff;                 /* было #666 — теперь белая */
    font-size: 17px;
    line-height: 1;
    cursor: pointer;
    border-radius: 4px;
    opacity: .85;
    transition: background .15s, opacity .15s, transform .2s;
}
.bnx-chat-size:hover {
    background: rgba(255,255,255,.18);   /* светлая подсветка на синем */
    opacity: 1;
}
.bnx-chat-size:active { transform: scale(.92); }
#bnx-ai-chat.bnx-expanded .bnx-chat-window {
    width: 720px; height: 700px;
    max-width: 95vw; max-height: 92vh;
}
#bnx-ai-chat.bnx-expanded .bnx-chat-messages { font-size: 15px; }
#bnx-ai-chat.bnx-expanded .bnx-chat-bubble  { padding: 12px 16px; line-height: 1.5; }
#bnx-ai-chat.bnx-expanded .bnx-chat-input   { font-size: 15px; }

@media (max-width: 768px) {
    #bnx-ai-chat.bnx-expanded .bnx-chat-window {
        width: 100vw; height: 100vh;
        max-width: 100vw; max-height: 100vh;
        border-radius: 0;
    }
}

/* ============================================================
   АДАПТИВ: кнопка и окно ВЫШЕ таббара сайта
   ============================================================ */
@media (max-width: 480px) {
    /* кнопка: над таббаром + safe-area для iPhone */
    .bnx-chat-fab {
        width: 52px;
        height: 52px;
        right: 14px;
        bottom: 84px; /* fallback для старых браузеров */
        bottom: calc(var(--bnx-tabbar-h) + 20px + constant(safe-area-inset-bottom, 0px));
        bottom: calc(var(--bnx-tabbar-h) + 20px + env(safe-area-inset-bottom, 0px));
    }

    /* окно: выше кнопки, не выходит за верх экрана */
    .bnx-chat-window {
        right: 14px;
        left: auto;
        width: calc(100vw - 28px);
        height: auto;
        max-height: 60vh;
        bottom: 148px; /* fallback */
        bottom: calc(var(--bnx-tabbar-h) + 84px + constant(safe-area-inset-bottom, 0px));
        bottom: calc(var(--bnx-tabbar-h) + 84px + env(safe-area-inset-bottom, 0px));
    }

    .bnx-chat-quick-actions {
        padding: 5px 8px;
        gap: 4px;
    }

    .bnx-quick-icon { font-size: 12px; }
    .bnx-quick-label { font-size: 10px; }

    .bnx-chat-clear-text {
        font-size: 9px;
        padding: 3px 4px;
    }

    .bnx-chat-bubble {
        max-width: 90%;
        font-size: 13px;
    }

    .bnx-chat-table { font-size: 11px; }
    .bnx-chat-table th,
    .bnx-chat-table td { padding: 4px 6px; }
}

@media (max-width: 360px) {
    .bnx-chat-fab {
        width: 48px;
        height: 48px;
        right: 10px;
        bottom: 80px; /* fallback */
        bottom: calc(var(--bnx-tabbar-h) + 16px + constant(safe-area-inset-bottom, 0px));
        bottom: calc(var(--bnx-tabbar-h) + 16px + env(safe-area-inset-bottom, 0px));
    }

    .bnx-chat-window {
        right: 10px;
        width: calc(100vw - 20px);
        bottom: 140px; /* fallback */
        bottom: calc(var(--bnx-tabbar-h) + 76px + constant(safe-area-inset-bottom, 0px));
        bottom: calc(var(--bnx-tabbar-h) + 76px + env(safe-area-inset-bottom, 0px));
    }
}
/* ---------- голосовой ввод ---------- */
.bnx-chat-mic {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #f0f0f0;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: background 0.2s, color 0.2s, transform 0.2s;
    flex-shrink: 0;
}

.bnx-chat-mic:hover {
    background: #e0e0e0;
}

.bnx-chat-mic svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

/* запись активна */
.bnx-chat-mic.recording {
    background: #ff4444;
    color: #fff;
    animation: bnx-mic-pulse 1.2s infinite;
}

@keyframes bnx-mic-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 68, 68, 0.5); }
    50%      { box-shadow: 0 0 0 8px rgba(255, 68, 68, 0); }
}

/*不支持 — скрываем */
.bnx-chat-mic.unsupported {
    display: none;
}

/* подсказка над кнопкой при ошибке доступа */
.bnx-mic-hint {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: #fff;
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

.bnx-mic-hint.show {
    opacity: 1;
}

/* ---------- v3.2: дисклеймер ИИ внизу окна ---------- */
.bnx-chat-disclaimer {
    padding: 8px 16px 12px;
    font-size: 10px;
    line-height: 1.4;
    color: #9aa0a6;
    text-align: center;
    border-top: 1px solid #f0f2f5;
    background: #fafbfc;
    user-select: none;
    flex-shrink: 0;          /* окно — flex-column: не даём дисклеймеру сжаться */
}

#bnx-ai-chat.bnx-expanded .bnx-chat-disclaimer {
    font-size: 11px;
    padding: 10px 20px 14px;
}

@media (max-width: 480px) {
    .bnx-chat-disclaimer {
        font-size: 9px;
        padding: 6px 12px 10px;
    }
}