/* Direct Messages — Telegram-style floating widget (desktop) + fullscreen overlay (mobile).
   ВАЖНО: виджет использует СВОЮ scoped-палитру --dc-* и не наследует цвета темы
   дашборда — иначе тёмный текст светлой темы пропадает на тёмных подложках. */

#directChatWidget {
    /* ── Scoped palette (Telegram light) ── */
    --dc-bg: #ffffff;
    --dc-text: #232e3c;
    --dc-text-muted: #707991;
    --dc-text-dim: #a2acb4;
    --dc-accent: #3390ec;
    --dc-accent-hover: #2b7fd3;
    --dc-out: #3390ec;            /* исходящий пузырь */
    --dc-out-text: #ffffff;
    --dc-in: #ffffff;             /* входящий пузырь */
    --dc-hover: #f4f6fa;
    --dc-border: #e7ebf0;
    --dc-badge: #3390ec;
    --dc-danger: #e53935;
    --dc-chat-bg: #e7edf4;

    position: fixed;
    z-index: 950;
    bottom: 24px;
    right: 24px;
    font-family: 'Inter', -apple-system, 'Segoe UI', Roboto, sans-serif;
    /* По умолчанию контейнер прозрачен для кликов; кнопка-bubble сама их собирает. */
    pointer-events: none;
}

#directChatWidget .dc-bubble,
#directChatWidget .dc-panel {
    pointer-events: auto;
}

/* ─── Bubble (closed state) ──────────────────────────────────────── */
.dc-bubble {
    width: 58px;
    height: 58px;
    border-radius: 50%;
    background: linear-gradient(135deg, #54b3ff, #3390ec);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(51, 144, 236, 0.45), 0 2px 6px rgba(0,0,0,0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 160ms ease, box-shadow 160ms ease;
    position: relative;
}
.dc-bubble:hover { transform: translateY(-2px) scale(1.04); box-shadow: 0 12px 32px rgba(51,144,236,0.55); }
.dc-bubble:active { transform: scale(0.96); }
.dc-bubble svg { width: 26px; height: 26px; }

.dc-bubble-badge {
    position: absolute;
    top: -3px; right: -3px;
    min-width: 21px; height: 21px;
    padding: 0 6px;
    border-radius: 11px;
    background: #e53935;
    color: #fff;
    font-size: 11.5px;
    font-weight: 700;
    line-height: 21px;
    text-align: center;
    box-shadow: 0 0 0 2.5px #fff;
    display: none;
}
#directChatWidget.dc-has-unread .dc-bubble-badge { display: block; }

/* ─── Panel (open states) ────────────────────────────────────────── */
.dc-panel {
    position: absolute;
    bottom: 74px;
    right: 0;
    width: 390px;
    height: 580px;
    max-height: calc(100vh - 100px);
    background: var(--dc-bg);
    border: 1px solid var(--dc-border);
    border-radius: 16px;
    box-shadow: 0 24px 64px -12px rgba(15, 23, 42, 0.28), 0 8px 24px rgba(15, 23, 42, 0.12);
    color: var(--dc-text);
    display: none;
    flex-direction: column;
    overflow: hidden;
    transform-origin: bottom right;
}

@keyframes dcPanelIn {
    from { opacity: 0; transform: translateY(10px) scale(0.97); }
    to   { opacity: 1; transform: none; }
}

/* Visibility per state */
#directChatWidget.dc-state-collapsed   .dc-panel { display: none; }
#directChatWidget.dc-state-open-list   .dc-panel--list { display: flex; animation: dcPanelIn 180ms cubic-bezier(0.2, 0.9, 0.3, 1); }
#directChatWidget.dc-state-open-chat   .dc-panel--chat { display: flex; animation: dcPanelIn 180ms cubic-bezier(0.2, 0.9, 0.3, 1); }

/* ─── Panel header ───────────────────────────────────────────────── */
.dc-panel-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: var(--dc-bg);
    border-bottom: 1px solid var(--dc-border);
    flex-shrink: 0;
    min-height: 56px;
    box-sizing: border-box;
}
.dc-panel-title {
    flex: 1;
    font-weight: 600;
    font-size: 1rem;
    color: var(--dc-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Заголовок открытого чата: аватар + имя + подпись роли */
.dc-head-peer {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}
.dc-head-info { flex: 1; min-width: 0; }
.dc-head-name {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--dc-text);
    line-height: 1.25;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.dc-head-sub {
    font-size: 0.75rem;
    color: var(--dc-text-muted);
    line-height: 1.2;
}

.dc-icon-btn {
    background: transparent;
    border: none;
    color: var(--dc-text-muted);
    cursor: pointer;
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
    font-size: 1.15rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 120ms ease, color 120ms ease;
}
.dc-icon-btn:hover { background: var(--dc-hover); color: var(--dc-text); }
.dc-icon-btn svg { width: 22px; height: 22px; }

/* ─── Avatars (Telegram gradient palette) ────────────────────────── */
.dc-avatar {
    border-radius: 50%;
    color: #fff;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    user-select: none;
    text-transform: uppercase;
}
.dc-avatar--list { width: 50px; height: 50px; font-size: 1.15rem; }
.dc-avatar--head { width: 40px; height: 40px; font-size: 0.95rem; }
.dc-avatar--c0 { background: linear-gradient(180deg, #ff885e, #ff516a); }
.dc-avatar--c1 { background: linear-gradient(180deg, #ffcd6a, #ffa85c); }
.dc-avatar--c2 { background: linear-gradient(180deg, #82b1ff, #665fff); }
.dc-avatar--c3 { background: linear-gradient(180deg, #a0de7e, #54cb68); }
.dc-avatar--c4 { background: linear-gradient(180deg, #53edd6, #28c9b7); }
.dc-avatar--c5 { background: linear-gradient(180deg, #72d5fd, #2a9ef1); }
.dc-avatar--c6 { background: linear-gradient(180deg, #e0a2f3, #d669ed); }

/* ─── Conversations list ─────────────────────────────────────────── */
.dc-conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px;
    background: var(--dc-bg);
}
.dc-conv-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 10px;
    border-radius: 12px;
    cursor: pointer;
    transition: background 120ms ease;
}
.dc-conv-item:hover { background: var(--dc-hover); }
.dc-conv-item:active { background: #eaeef4; }
.dc-conv-body { flex: 1; min-width: 0; }
.dc-conv-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: baseline;
}
.dc-conv-row + .dc-conv-row { margin-top: 3px; }
.dc-conv-name {
    font-weight: 600;
    font-size: 0.92rem;
    color: var(--dc-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.dc-conv-time {
    font-size: 0.72rem;
    color: var(--dc-text-dim);
    flex-shrink: 0;
}
.dc-conv-preview {
    font-size: 0.85rem;
    color: var(--dc-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}
.dc-conv-preview .dc-you { color: var(--dc-text); }
.dc-conv-badge {
    background: var(--dc-badge);
    color: #fff;
    border-radius: 11px;
    min-width: 21px;
    height: 21px;
    padding: 0 6px;
    font-size: 11.5px;
    font-weight: 600;
    line-height: 21px;
    text-align: center;
    flex-shrink: 0;
    box-sizing: border-box;
}

/* ─── Chat area (Telegram patterned background) ──────────────────── */
.dc-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px 8px;
    display: flex;
    flex-direction: column;
    background-color: var(--dc-chat-bg);
    background-image:
        radial-gradient(rgba(51, 144, 236, 0.10) 1.2px, transparent 1.2px),
        linear-gradient(160deg, #dbe7f3 0%, #e9eef6 55%, #dde9f4 100%);
    background-size: 20px 20px, 100% 100%;
}

/* Разделитель дат — плавающая пилюля по центру */
.dc-date-sep {
    align-self: center;
    margin: 10px 0 8px;
    padding: 3px 12px;
    border-radius: 12px;
    background: rgba(90, 110, 130, 0.25);
    color: #fff;
    font-size: 0.74rem;
    font-weight: 500;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    text-shadow: 0 1px 1px rgba(0,0,0,0.1);
    flex-shrink: 0;
}

/* ─── Message bubbles ────────────────────────────────────────────── */
.dc-msg {
    position: relative;
    max-width: 78%;
    padding: 6px 10px 7px;
    border-radius: 15px;
    word-wrap: break-word;
    word-break: break-word;
    margin-bottom: 2px;
    line-height: 1.3;
}
/* последний в группе — отступ до следующей группы больше */
.dc-msg--last { margin-bottom: 8px; }

.dc-msg--me {
    align-self: flex-end;
    background: var(--dc-out);
    color: var(--dc-out-text);
}
.dc-msg--them {
    align-self: flex-start;
    background: var(--dc-in);
    color: var(--dc-text);
    filter: drop-shadow(0 1px 1.5px rgba(16, 35, 47, 0.12));
}

/* Группировка: сглаживаем углы на стороне отправителя между соседями группы */
.dc-msg--me:not(.dc-msg--last)   { border-bottom-right-radius: 6px; }
.dc-msg--me:not(.dc-msg--first)  { border-top-right-radius: 6px; }
.dc-msg--them:not(.dc-msg--last)  { border-bottom-left-radius: 6px; }
.dc-msg--them:not(.dc-msg--first) { border-top-left-radius: 6px; }

/* Хвостик у последнего сообщения группы (SVG, повторяет цвет пузыря) */
.dc-msg--me.dc-msg--last { border-bottom-right-radius: 4px; }
.dc-msg--me.dc-msg--last::after {
    content: '';
    position: absolute;
    right: -8px;
    bottom: 0;
    width: 9px;
    height: 16px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='9' height='16'%3E%3Cpath d='M0,0 L0,16 L9,16 C4,15 1,8 0,0 Z' fill='%233390ec'/%3E%3C/svg%3E") no-repeat;
}
.dc-msg--them.dc-msg--last { border-bottom-left-radius: 4px; }
.dc-msg--them.dc-msg--last::after {
    content: '';
    position: absolute;
    left: -8px;
    bottom: 0;
    width: 9px;
    height: 16px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='9' height='16'%3E%3Cpath d='M9,0 L9,16 L0,16 C5,15 8,8 9,0 Z' fill='%23ffffff'/%3E%3C/svg%3E") no-repeat;
}

.dc-msg-text {
    font-size: 0.9rem;
    line-height: 1.35;
    white-space: pre-wrap;
    /* flow-root: floated .dc-msg-meta учитывается в высоте пузыря */
    display: flow-root;
}

/* Время + галочки: вплавлены в последнюю строку текста (как в Telegram) */
.dc-msg-meta {
    float: right;
    font-size: 0.68rem;
    line-height: 1;
    margin: 8px -2px -2px 8px;
    white-space: nowrap;
    user-select: none;
}
.dc-msg--me .dc-msg-meta { color: rgba(255, 255, 255, 0.75); }
.dc-msg--them .dc-msg-meta { color: var(--dc-text-dim); }
.dc-msg-ticks { letter-spacing: -2px; margin-left: 3px; font-size: 0.75rem; }
.dc-msg--me .dc-msg-ticks--read { color: #aef2b8; }
/* Блочная мета — для сообщений из файлов-ссылок без текста */
.dc-msg-meta--block {
    float: none;
    display: block;
    text-align: right;
    margin: 4px 0 -2px;
}

/* Медиа-сообщение (только картинка, без текста) — минимальные поля,
   время накладывается на фото тёмной пилюлей */
.dc-msg--media { padding: 3px; background: transparent; filter: none; }
.dc-msg--media.dc-msg--last::after { display: none; }
.dc-msg--media .dc-msg-meta {
    position: absolute;
    right: 9px;
    bottom: 9px;
    float: none;
    margin: 0;
    padding: 3px 8px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.45);
    color: #fff !important;
    backdrop-filter: blur(2px);
}

.dc-msg-files {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 4px;
}
.dc-msg--media .dc-msg-files { margin-top: 0; }
.dc-msg-file img {
    max-width: 100%;
    max-height: 240px;
    border-radius: 12px;
    display: block;
}
.dc-msg-file--link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: inherit;
    text-decoration: none;
    font-size: 0.82rem;
    padding: 5px 8px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.08);
}
.dc-msg--me .dc-msg-file--link { background: rgba(255, 255, 255, 0.16); }
.dc-msg-file--link:hover { text-decoration: underline; }

.dc-msg-edited {
    font-style: italic;
    opacity: 0.85;
    font-size: 0.66rem;
}

/* ─── Edit/Delete для своих сообщений ────────────────────────────── */
.dc-msg-actions {
    position: absolute;
    top: -14px;
    right: 4px;
    display: none;
    gap: 0;
    background: #fff;
    border: 1px solid var(--dc-border);
    border-radius: 10px;
    padding: 2px;
    box-shadow: 0 4px 16px rgba(15, 23, 42, 0.18);
}
.dc-msg--me:hover .dc-msg-actions { display: flex; }
.dc-msg-act {
    background: transparent;
    border: none;
    color: var(--dc-text-muted);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 4px 8px;
    border-radius: 8px;
    line-height: 1;
    transition: background 100ms ease;
}
.dc-msg-act:hover { background: var(--dc-hover); color: var(--dc-text); }
.dc-msg-act--danger { color: var(--dc-danger); }
.dc-msg-act--danger:hover { background: #fdecea; color: var(--dc-danger); }
.dc-msg-act--primary {
    background: var(--dc-accent);
    color: #fff;
    padding: 5px 12px;
    font-weight: 600;
    border-radius: 8px;
}
.dc-msg-act--primary:hover { background: var(--dc-accent-hover); color: #fff; }

.dc-msg-edit { display: flex; flex-direction: column; gap: 6px; min-width: 200px; }
.dc-msg-edit-input {
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    color: var(--dc-text);
    border: 1px solid var(--dc-border);
    border-radius: 10px;
    padding: 7px 9px;
    font-family: inherit;
    font-size: 0.88rem;
    resize: vertical;
    min-height: 60px;
    outline: none;
}
.dc-msg-edit-input:focus { border-color: var(--dc-accent); }
.dc-msg-edit-row { display: flex; gap: 6px; justify-content: flex-end; }
.dc-msg--me .dc-msg-edit .dc-msg-act { color: rgba(255,255,255,0.85); }
.dc-msg--me .dc-msg-edit .dc-msg-act:hover { background: rgba(255,255,255,0.15); color: #fff; }
.dc-msg--me .dc-msg-edit .dc-msg-act--primary { background: #fff; color: var(--dc-accent); }
.dc-msg--me .dc-msg-edit .dc-msg-act--primary:hover { background: #f0f6ff; }

/* Touch-устройства: hover не сработает, всегда показываем actions компактно */
@media (hover: none) {
    .dc-msg--me .dc-msg-actions { display: flex; opacity: 0.55; }
    .dc-msg--me .dc-msg-actions:active { opacity: 1; }
}

/* ─── Composer ───────────────────────────────────────────────────── */
.dc-composer {
    border-top: 1px solid var(--dc-border);
    padding: 8px 10px;
    flex-shrink: 0;
    background: var(--dc-bg);
}
.dc-file-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-bottom: 6px;
}
.dc-file-chips:empty { display: none; }
.dc-file-chip {
    background: #eaf3fd;
    border: 1px solid #c6e0f8;
    border-radius: 12px;
    padding: 3px 9px;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--dc-text);
}
.dc-file-chip__x {
    cursor: pointer;
    color: var(--dc-danger);
    font-weight: 700;
}
.dc-composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}
.dc-composer textarea {
    flex: 1;
    resize: none;
    background: var(--dc-hover);
    color: var(--dc-text);
    border: 1px solid transparent;
    border-radius: 19px;
    padding: 9px 14px;
    font-family: inherit;
    font-size: 0.9rem;
    line-height: 1.3;
    min-height: 38px;
    max-height: 120px;
    box-sizing: border-box;
    outline: none;
    transition: border-color 120ms ease, background 120ms ease;
}
.dc-composer textarea::placeholder { color: var(--dc-text-dim); }
.dc-composer textarea:focus { border-color: var(--dc-accent); background: #fff; }
.dc-send-btn {
    width: 40px;
    height: 40px;
    background: var(--dc-accent);
    color: #fff;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
    transition: background 120ms ease, transform 120ms ease;
    box-shadow: 0 2px 8px rgba(51, 144, 236, 0.35);
}
.dc-send-btn:hover { background: var(--dc-accent-hover); transform: scale(1.06); }
.dc-send-btn:active { transform: scale(0.94); }
.dc-send-btn svg { width: 19px; height: 19px; margin-left: 2px; }

/* ─── Scrollbars (тонкие, как в Telegram) ────────────────────────── */
.dc-conv-list::-webkit-scrollbar,
.dc-messages::-webkit-scrollbar { width: 5px; }
.dc-conv-list::-webkit-scrollbar-thumb,
.dc-messages::-webkit-scrollbar-thumb {
    background: rgba(90, 110, 130, 0.35);
    border-radius: 3px;
}
.dc-conv-list::-webkit-scrollbar-thumb:hover,
.dc-messages::-webkit-scrollbar-thumb:hover { background: rgba(90, 110, 130, 0.55); }

/* ─── Drag-over hint ─────────────────────────────────────────────── */
.dc-dropzone-hint {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(51, 144, 236, 0.12);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 5;
    pointer-events: none;
}
.dc-panel.dc-dragover .dc-dropzone-hint { display: flex; }
.dc-panel.dc-dragover { outline: 2px dashed var(--dc-accent); outline-offset: -8px; }
.dc-dropzone-hint__card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 22px 28px;
    border-radius: 16px;
    background: #fff;
    color: var(--dc-accent);
    font-size: 0.9rem;
    font-weight: 600;
    box-shadow: 0 12px 36px rgba(15, 23, 42, 0.22);
    animation: dcPanelIn 160ms ease;
}
.dc-dropzone-hint__card svg { width: 34px; height: 34px; }

/* ─── Empty states ───────────────────────────────────────────────── */
.dc-empty {
    text-align: center;
    color: var(--dc-text-muted);
    padding: 32px 16px;
    font-size: 0.85rem;
}
.dc-messages .dc-empty {
    align-self: center;
    margin: auto;
    padding: 6px 14px;
    border-radius: 14px;
    background: rgba(90, 110, 130, 0.25);
    color: #fff;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
.dc-empty--small { padding: 6px 14px; }

/* ─── Mobile overlay (fullscreen) ────────────────────────────────── */
@media (max-width: 767px) {
    #directChatWidget {
        bottom: 16px;
        right: 16px;
    }
    .dc-bubble { width: 54px; height: 54px; }

    #directChatWidget.dc-state-open-list .dc-panel--list,
    #directChatWidget.dc-state-open-chat .dc-panel--chat {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        border: none;
        bottom: 0;
        right: 0;
        animation: none;
        padding-bottom: env(safe-area-inset-bottom);
    }
    .dc-panel-head { padding-top: calc(10px + env(safe-area-inset-top)); }
    .dc-msg { max-width: 85%; }
    /* На мобильнике bubble прячем когда панель открыта, чтобы не перекрывала composer */
    #directChatWidget.dc-state-open-list .dc-bubble,
    #directChatWidget.dc-state-open-chat .dc-bubble {
        display: none;
    }
}
