/**
 * AI Support Chat - виджет. Дизайн по макету SupportChat (tusovka-ds), пиксель-в-пиксель.
 * Палитра через --aisc-* (фолбэк на --tusovka-* темы, подставляется инлайном из настроек).
 * Позиция left/right и отступ снизу (--aisc-bottom) задаются инлайном. Бренд-градиент
 * = --aisc-primary -> --tusovka-secondary. Контент рендерится только через textContent.
 */

/* ── Бренд-primary (лаунчер/хедер/бабл/send) — СПЛОШНОЙ по канону sunday (был градиент) ── */
:root { --aisc-grad: var(--aisc-primary, #f5820a); }

/* ── 1. Лаунчер ── */
.aisc-toggle {
    position: fixed;
    bottom: var(--aisc-bottom, 88px);
    z-index: 99989;
    width: 56px;
    height: 56px;
    border: 0;
    border-radius: 50%;
    background: var(--aisc-grad);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 14px 30px -8px rgba(245, 130, 10, 0.6);
    animation: aiscPulse 2.4s ease-out infinite;
    transition: transform 0.18s ease, box-shadow 0.26s ease;
}
.aisc-toggle:hover { transform: translateY(-3px) scale(1.04); }
.aisc-toggle:active { transform: scale(0.96); }
.aisc-toggle[hidden] { display: none; }
.aisc-toggle .aisc-icon-close { display: none; }
.aisc-toggle.is-open .aisc-icon-chat { display: none; }
.aisc-toggle.is-open .aisc-icon-close { display: block; }
.aisc-toggle.is-open { animation: none; }

/* ── 2. Панель ── */
.aisc-panel {
    position: fixed;
    bottom: calc(var(--aisc-bottom, 88px) + 68px);
    z-index: 99992;
    width: 360px;
    max-width: calc(100vw - 48px);
    /* Учитываем нижний отступ (лаунчер над виджетом доступности) + запас сверху,
       иначе на невысоком окне хедер панели уходит выше вьюпорта. */
    max-height: calc(100vh - var(--aisc-bottom, 88px) - 88px);
    display: flex;
    flex-direction: column;
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 30px 70px -20px rgba(17, 24, 39, 0.5);
    border: 1px solid rgba(17, 24, 39, 0.06);
    font-family: var(--tusovka-font-sans, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif);
    font-size: 14px;
    line-height: 1.6;
    color: #1f2937;
    animation: aiscPanel 0.3s cubic-bezier(0.34, 1.3, 0.5, 1) both;
}
.aisc-panel[hidden] { display: none; }

/* ── 3. Хедер ── */
.aisc-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 15px 16px;
    background: var(--aisc-grad);
    color: #fff;
    flex: none;
}
.aisc-head-avatar {
    position: relative;
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    border: 2px solid rgba(255, 255, 255, 0.5);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}
.aisc-head-online {
    position: absolute;
    bottom: -1px;
    inset-inline-end: -1px;
    width: 13px;
    height: 13px;
    border-radius: 50%;
    background: #22c55e;
    border: 2.5px solid #fff;
}
.aisc-head-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.aisc-head-title { font-weight: 800; font-size: 16px; letter-spacing: -0.01em; }
.aisc-head-presence { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; opacity: 0.95; }
.aisc-presence-dot { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; flex: none; }
.aisc-head-close {
    width: 36px; height: 36px; flex: none;
    border: 0; border-radius: 11px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.16s ease;
}
.aisc-head-close:hover { background: rgba(255, 255, 255, 0.16); }

/* ── 4. Статус-баннер (эскалация/закрытие) ── */
.aisc-status {
    flex: none;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 11px 15px;
    font-size: 12.5px;
    font-weight: 600;
    background: #f7f8fc;
    color: #6b7280;
    border-bottom: 1px solid #eef0f6;
    animation: aiscBubble 0.28s ease both;
}
.aisc-status[hidden] { display: none; }
.aisc-status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex: none; opacity: 0.7; }
.aisc-status--escalated { background: #ecfdf5; color: #065f46; border-bottom-color: #a7f3d0; }
.aisc-status--escalated::before { background: #10b981; opacity: 1; }
.aisc-status--closed { background: #f7f8fc; color: #6b7280; border-bottom-color: #eef0f6; }

/* ── 5. Тред сообщений ── */
.aisc-messages {
    flex: 1;
    min-height: 200px;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #f7f8fc;
}

.aisc-msg { animation: aiscBubble 0.28s ease both; }

/* бот/оператор - слева, мини-аватар + белый пузырь с хвостом top-left */
.aisc-msg--ai, .aisc-msg--operator {
    align-self: flex-start;
    max-width: 84%;
    display: flex;
    gap: 8px;
}
.aisc-msg--ai::before, .aisc-msg--operator::before {
    content: '';
    width: 26px; height: 26px; flex: none;
    margin-top: 14px;
    border-radius: 50%;
    background: var(--aisc-grad);
}
.aisc-msg--operator::before { background: linear-gradient(135deg, #10b981, #22c55e); }
.aisc-msg-body { min-width: 0; }
.aisc-msg-label { display: block; font-size: 11px; font-weight: 700; color: #9ca3af; margin: 0 0 3px 3px; }
.aisc-msg--ai .aisc-msg-text, .aisc-msg--operator .aisc-msg-text {
    display: block;
    background: #fff;
    border: 1px solid #eef0f6;
    border-radius: 4px 15px 15px 15px;
    padding: 10px 13px;
    font-size: 14px;
    color: #1f2937;
    box-shadow: 0 4px 12px -8px rgba(17, 24, 39, 0.3);
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

/* пользователь - справа, градиентный пузырь с хвостом top-right */
.aisc-msg--user { align-self: flex-end; max-width: 84%; }
.aisc-msg--user .aisc-msg-text {
    display: block;
    background: var(--aisc-grad);
    color: #fff;
    border-radius: 15px 4px 15px 15px;
    padding: 10px 13px;
    font-size: 14px;
    box-shadow: 0 8px 18px -10px rgba(245, 130, 10, 0.6);
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

/* системное - центр, серый пилл */
.aisc-msg--system { align-self: center; max-width: 88%; }
.aisc-msg--system .aisc-msg-body { text-align: center; }
.aisc-msg--system .aisc-msg-text {
    display: inline-block;
    background: #eef0f6;
    color: #4b5563;
    font-size: 12px;
    padding: 6px 13px;
    border-radius: 999px;
}

/* typing-индикатор */
.aisc-typing {
    align-self: flex-start;
    display: flex;
    gap: 8px;
    align-items: flex-end;
    animation: aiscBubble 0.28s ease both;
}
.aisc-typing::before {
    content: '';
    width: 26px; height: 26px; flex: none;
    border-radius: 50%;
    background: var(--aisc-grad);
}
.aisc-typing-dots {
    background: #fff;
    border: 1px solid #eef0f6;
    border-radius: 4px 15px 15px 15px;
    padding: 12px 14px;
    display: inline-flex;
    gap: 4px;
    align-items: center;
    box-shadow: 0 4px 12px -8px rgba(17, 24, 39, 0.3);
}
.aisc-typing-dots span { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #9ca3af; animation: aiscDot 1.2s infinite ease-in-out; }
.aisc-typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.aisc-typing-dots span:nth-child(3) { animation-delay: 0.36s; }

/* ── 6. FAQ-темы (сценарный режим) ── */
.aisc-topics-head { font-size: 12px; font-weight: 700; color: #9ca3af; margin: 2px 0 1px 3px; align-self: stretch; }
.aisc-topics { display: flex; flex-direction: column; gap: 8px; align-self: stretch; animation: aiscBubble 0.28s ease both; }
.aisc-topic {
    display: flex;
    align-items: center;
    gap: 11px;
    text-align: start;
    border: 1.5px solid #e5e7eb;
    background: #fff;
    cursor: pointer;
    font: inherit;
    padding: 11px 13px;
    border-radius: 13px;
    box-shadow: 0 4px 12px -10px rgba(17, 24, 39, 0.3);
    transition: transform 0.15s ease, border-color 0.18s ease, background 0.18s ease, box-shadow 0.2s ease;
}
.aisc-topic:hover { transform: translateY(-2px); border-color: #f8dcc1; box-shadow: 0 10px 22px -14px rgba(245, 130, 10, 0.5); }
.aisc-topic-label { flex: 1; font-weight: 700; font-size: 13.5px; color: #1f2937; }
.aisc-topic::after { content: ''; width: 7px; height: 7px; flex: none; border-right: 2.4px solid #c7cbd8; border-top: 2.4px solid #c7cbd8; transform: rotate(45deg); }
.aisc-topic--escalate { border-color: #fed7aa; background: #fff7ed; }
.aisc-topic--escalate .aisc-topic-label { color: #9a3412; font-weight: 800; }

/* ── 7. Эскалация (amber-чип «Связаться с человеком») ── */
.aisc-escalate-row { flex: none; padding: 0 16px; }
.aisc-escalate {
    width: 100%;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 11px;
    text-align: start;
    border: 1.5px solid #fed7aa;
    background: #fff7ed;
    cursor: pointer;
    font: inherit;
    padding: 11px 13px;
    border-radius: 13px;
    transition: transform 0.15s ease, box-shadow 0.2s ease;
}
.aisc-escalate:hover { transform: translateY(-2px); box-shadow: 0 10px 22px -14px rgba(245, 158, 11, 0.5); }
.aisc-escalate[hidden] { display: none; }
.aisc-escalate-ic {
    width: 34px; height: 34px; flex: none;
    border-radius: 10px;
    background: rgba(245, 158, 11, 0.16);
    color: #b45309;
    display: flex; align-items: center; justify-content: center;
}
.aisc-escalate-tx { flex: 1; min-width: 0; }
.aisc-escalate-t { display: block; font-weight: 800; font-size: 13.5px; color: #9a3412; }
.aisc-escalate-s { display: block; font-size: 11.5px; color: #b45309; }

/* ── 8. Композер ── */
.aisc-form {
    flex: none;
    display: flex;
    align-items: flex-end;
    gap: 9px;
    padding: 11px 12px;
    border-top: 1px solid #eef0f6;
    background: #fff;
}
.aisc-inputwrap {
    flex: 1;
    min-width: 0;
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f4f5fb;
    border: 1.5px solid #e5e7eb;
    border-radius: 13px;
    padding: 0 6px 0 13px;
    transition: border-color 0.18s ease;
}
.aisc-inputwrap:focus-within { border-color: var(--aisc-primary, #f5820a); box-shadow: 0 0 0 3px rgba(245, 130, 10, 0.18); }
.aisc-input {
    flex: 1;
    min-width: 0;
    border: 0;
    background: none;
    outline: none;
    padding: 12px 0;
    font: inherit;
    font-size: 14px;
    color: #1f2937;
}
.aisc-emoji {
    width: 32px; height: 32px; flex: none;
    border: 0; border-radius: 9px;
    background: none;
    color: #9ca3af;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: color 0.16s ease, background 0.16s ease;
}
.aisc-emoji:hover { color: #6b7280; background: rgba(17, 24, 39, 0.04); }
.aisc-send {
    width: 44px; height: 44px; flex: none;
    border: 0; border-radius: 13px;
    color: #fff;
    background: var(--aisc-grad);
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 8px 20px -6px rgba(245, 130, 10, 0.55);
    transition: transform 0.16s ease, box-shadow 0.26s ease, filter 0.2s ease;
}
.aisc-send:hover { transform: translateY(-2px); filter: brightness(1.05); }
.aisc-send:active { transform: translateY(0) scale(0.97); }
.aisc-send[disabled] { opacity: 0.55; cursor: default; transform: none; filter: none; }

.aisc-microcopy { flex: none; text-align: center; font-size: 10.5px; color: #6b7280; padding: 0 0 9px; background: #fff; }

/* ── Время под сообщением + дата-разделитель ── */
.aisc-msg-time { display: block; font-size: 10.5px; color: #6b7280; margin: 3px 4px 0; }
.aisc-msg--user .aisc-msg-time { text-align: right; }
.aisc-msg--ai .aisc-msg-time, .aisc-msg--operator .aisc-msg-time { text-align: left; }
.aisc-datesep { align-self: center; margin: 4px 0 2px; }
.aisc-datesep span {
    display: inline-block;
    background: #e9ebf3; color: #4b5563;
    font-size: 11px; font-weight: 600;
    padding: 3px 12px; border-radius: 999px;
}

/* ── Эмодзи-пикер ── */
.aisc-emoji-pop {
    position: absolute; bottom: calc(100% + 8px); right: 0;
    width: 236px; max-height: 184px; overflow-y: auto;
    background: #fff; border: 1px solid #eef0f6; border-radius: 12px;
    box-shadow: 0 14px 34px -12px rgba(17, 24, 39, 0.4);
    padding: 8px; z-index: 6;
    display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px;
}
.aisc-emoji-pop[hidden] { display: none; }
.aisc-emoji-item {
    border: 0; background: none; cursor: pointer;
    font-size: 18px; line-height: 1; padding: 5px; border-radius: 8px;
}
.aisc-emoji-item:hover { background: rgba(245, 130, 10, 0.12); }
.aisc-emoji-item:focus-visible { outline: 2px solid var(--aisc-primary, #f5820a); outline-offset: 1px; }

/* ── Вложения-картинки ── */
.aisc-attach {
    width: 32px; height: 32px; flex: none;
    border: 0; border-radius: 9px; background: none; color: #9ca3af;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    transition: color 0.16s ease, background 0.16s ease;
}
.aisc-attach:hover { color: #6b7280; background: rgba(17, 24, 39, 0.04); }
.aisc-attach:focus-visible { outline: 2px solid var(--aisc-primary, #f5820a); outline-offset: 2px; }

.aisc-pending { flex: none; display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 14px 0; background: #fff; }
.aisc-pending[hidden] { display: none; }
.aisc-pend { position: relative; width: 56px; height: 56px; border-radius: 10px; overflow: hidden; border: 1px solid #eef0f6; }
.aisc-pend img { width: 100%; height: 100%; object-fit: cover; display: block; }
.aisc-pend-x {
    position: absolute; top: 2px; right: 2px; width: 18px; height: 18px;
    border: 0; border-radius: 50%; background: rgba(17, 24, 39, 0.65); color: #fff;
    font-size: 13px; line-height: 1; cursor: pointer; padding: 0;
    display: flex; align-items: center; justify-content: center;
}

.aisc-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.aisc-msg--user .aisc-imgs { justify-content: flex-end; }
.aisc-img { display: block; border-radius: 12px; overflow: hidden; max-width: 190px; border: 1px solid rgba(0, 0, 0, 0.06); }
.aisc-imgs--multi .aisc-img { max-width: 110px; }
.aisc-img img { display: block; width: 100%; height: auto; max-height: 220px; object-fit: cover; }

/* ── Кнопки в шапке (история) ── */
.aisc-head-btn {
    width: 34px; height: 34px; flex: none;
    border: 0; border-radius: 11px;
    background: rgba(255, 255, 255, 0.12); color: #fff; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.16s ease;
}
.aisc-head-btn:hover { background: rgba(255, 255, 255, 0.18); }
.aisc-head-btn:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

/* ── История переписок ── */
.aisc-panel--history .aisc-escalate-row,
.aisc-panel--history .aisc-form,
.aisc-panel--history .aisc-microcopy,
.aisc-panel--history .aisc-pending { display: none !important; }
.aisc-hist-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 10px; }
.aisc-hist-title { font-size: 14px; font-weight: 800; color: #1f2937; }
.aisc-hist-new { border: 0; background: none; color: var(--aisc-primary, #f5820a); font-weight: 700; font-size: 13px; cursor: pointer; padding: 4px 6px; border-radius: 8px; }
.aisc-hist-new:hover { background: rgba(245, 130, 10, 0.08); }
.aisc-hist-empty { text-align: center; color: #9ca3af; font-size: 13px; padding: 24px 8px; }
.aisc-hist-item {
    display: flex; flex-direction: column; gap: 4px; width: 100%; text-align: left;
    border: 1px solid #eef0f6; border-radius: 12px; background: #fff;
    padding: 11px 13px; margin-bottom: 8px; cursor: pointer;
    transition: border-color 0.16s ease, box-shadow 0.16s ease;
}
.aisc-hist-item:hover { border-color: #d7d9e6; box-shadow: 0 6px 16px -12px rgba(17, 24, 39, 0.4); }
.aisc-hist-ex { font-size: 13.5px; color: #1f2937; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.aisc-hist-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.aisc-hist-meta > span:first-child { font-size: 11.5px; color: #9ca3af; }
.aisc-hist-badge { font-size: 11px; font-weight: 700; padding: 2px 9px; border-radius: 999px; white-space: nowrap; }
.aisc-hist-badge--closed { background: #eef0f6; color: #6b7280; }
.aisc-hist-badge--escalated { background: #fef3c7; color: #b45309; }
.aisc-hist-badge--ai { background: #eaf1f6; color: #dc7000; }
.aisc-hist-item:focus-visible, .aisc-hist-new:focus-visible { outline: 2px solid var(--aisc-primary, #f5820a); outline-offset: 2px; }

/* ── Видимый фокус для клавиатуры (WCAG 2.4.7) ── */
.aisc-topic:focus-visible, .aisc-escalate:focus-visible, .aisc-emoji:focus-visible {
    outline: 2px solid var(--aisc-primary, #f5820a);
    outline-offset: 2px;
}
/* Фокус на круглых кнопках: белая обводка на светлом фоне страницы физически не
   видна, а страховочное оранжевое кольцо через box-shadow гасили и keyframes
   aiscPulse (лаунчер пульсирует именно box-shadow'ом), и !important темы. Поэтому
   двойной контур с !important: белый внутри - чтобы читался на оранжевой кнопке,
   тёмный снаружи - чтобы читался на светлом фоне (контраст >7:1). */
.aisc-toggle:focus-visible, .aisc-send:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
    box-shadow: 0 0 0 3px #fff, 0 0 0 6px #1f2937 !important;
}
@media (forced-colors: active) {
    .aisc-toggle:focus-visible, .aisc-send:focus-visible {
        outline: 3px solid Highlight;
        outline-offset: 3px;
    }
}
.aisc-head-close:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }

/* ── Анимации ── */
@keyframes aiscPulse {
    0%   { box-shadow: 0 14px 30px -8px rgba(245, 130, 10, 0.6), 0 0 0 0 rgba(245, 130, 10, 0.5); }
    70%  { box-shadow: 0 14px 30px -8px rgba(245, 130, 10, 0.6), 0 0 0 16px rgba(245, 130, 10, 0); }
    100% { box-shadow: 0 14px 30px -8px rgba(245, 130, 10, 0.6), 0 0 0 0 rgba(245, 130, 10, 0); }
}
@keyframes aiscPanel {
    from { opacity: 0; transform: translateY(18px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes aiscBubble {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes aiscDot {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30%           { transform: translateY(-4px); opacity: 1; }
}

/* ── Полноэкранный режим на узких экранах ── */
@media (max-width: 480px) {
    .aisc-panel {
        inset: 0 !important;
        width: auto !important;
        max-width: none !important;
        max-height: none !important;
        border-radius: 0 !important;
    }
}

/* ── Уважение к prefers-reduced-motion ── */
@media (prefers-reduced-motion: reduce) {
    .aisc-toggle, .aisc-panel, .aisc-msg, .aisc-topics, .aisc-status,
    .aisc-typing, .aisc-topic, .aisc-escalate, .aisc-send, .aisc-head-close,
    .aisc-emoji, .aisc-inputwrap, .aisc-typing-dots span {
        animation: none !important;
        transition: none !important;
    }
}
