/* ==========================================================================
   Variables adaptables según el sitio (Cómputos / Fiscal)
   ========================================================================== */
.custom-chat-computos, .custom-chat-fiscal {
    --chat-primary: #0d6efd;
    --chat-bg: #ffffff;
    --chat-bubble-me: #e3f2fd;
    --chat-bubble-other: #ffffff;
}

/* Estilo uniforme para todos los contadores del chat */
.dy-badge {
    font-size: 11px !important;
    padding: 4px 7px !important;
    line-height: 1 !important;
    font-weight: 600 !important;
    min-width: 18px;
    text-align: center;
}

/* Evita que los íconos internos bloqueen la captura del clic */
#chat-root-container i,
#chat-trigger-toggle i {
    pointer-events: none;
}

/* ==========================================================================
   Contenedor Flotante Adaptativo (Escritorio / Base)
   ========================================================================== */
#chat-root-container {
    position: fixed;
    bottom: 2px;
    right: 0px;
    width: 320px;
    height: 420px;
    background: var(--chat-bg);
    box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.15);
    border-radius: 12px;
    font-family: system-ui, -apple-system, sans-serif;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    overscroll-behavior: contain !important;
    z-index: 10000;
    
    transition: width 0.3s cubic-bezier(0.25, 1, 0.5, 1), 
                height 0.3s cubic-bezier(0.25, 1, 0.5, 1), 
                border-radius 0.25s ease,
                right 0.3s ease,
                bottom 0.3s ease,
                top 0.3s ease;
}

/* ESTADO COLAPSADO EN ESCRITORIO */
#chat-root-container.collapsed {
    width: 46px !important; 
    height: 46px !important;
    left: auto !important;
    right: 20px !important;  
    bottom: 10px !important; 
    top: auto !important;
    border-radius: 50% !important;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
    overflow: visible !important;
    cursor: pointer;
    background: transparent !important;
}

#chat-root-container.collapsed .chat-window-header {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50% !important;
}

/* Cabecera / Header */
.chat-window-header {
    background: var(--chat-primary);
    color: #ffffff;
    padding: 12px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    box-sizing: border-box;
    user-select: none;
    flex-shrink: 0 !important;
}

#chat-root-container:not(.collapsed) .chat-window-header {
    height: 52px;
    padding: 8px 12px;
}

#chat-root-container.collapsed #chat-header-title {
    font-size: 18px;
    width: 100%;
    text-align: center;
    margin: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Subtítulos de la cabecera */
.chat-header-hint {
    font-size: 10px;
    font-weight: 400;
    opacity: 0.8;
    margin-top: 1px;
    display: block;
}

.chat-header-substatus {
    font-size: 11px;
    font-weight: 400;
    opacity: 0.9;
    color: #e8f5e9;
    line-height: 1;
    margin-top: 2px;
}

#chat-root-container.collapsed .chat-header-hint,
#chat-root-container.collapsed .chat-header-substatus {
    display: none !important;
}

/* Botón de volver */
.chat-btn-volver { 
    background: transparent; 
    color: white; 
    padding: 4px 8px; 
    font-size: 18px; 
    cursor: pointer; 
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    margin-right: 0px;
    transition: background-color 0.2s ease;
}

.chat-btn-volver:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

/* Cuerpo y Pantallas */
.chat-window-body {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background-color: #f8f9fa;
    min-height: 0;
}

#chat-root-container.collapsed .chat-window-body {
    display: none !important;
}

/* Asegura que las pantallas del chat tengan un flujo de flexbox estricto */
.chat-screen {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    min-height: 0; /* AGREGAR: permite que los elementos flex descendientes se reduzcan correctamente */
}


.lista-items { 
    flex: 1 1 auto; 
    overflow-y: auto; 
    padding: 0; 
    min-height: 0; /* AGREGAR: indispensable para habilitar el scroll interno en flexbox */
    display: flex; /* AGREGAR */
    flex-direction: column; /* AGREGAR */
    overscroll-behavior: contain !important;
    overscroll-behavior-y: contain !important;
    -webkit-overflow-scrolling: touch;
}
/* AGREGAR: Específicamente para el listado interno de usuarios en privado */
#chat-usuarios-listado {
    flex: 1 1 auto;
    overflow-y: auto;
    min-height: 0;
}
.hidden-screen { display: none !important; }

/* Listados */
.lista-items { 
    flex: 1 1 auto; 
    overflow-y: auto; 
    padding: 0; 
    overscroll-behavior: contain !important;
    overscroll-behavior-y: contain !important;
    -webkit-overflow-scrolling: touch;
}

.item-canal {
    padding: 12px 16px;
    border-bottom: 1px solid #dee2e6;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s ease, transform 0.2s ease, opacity 0.2s ease;
}

.item-canal:hover { 
    background-color: rgba(25, 135, 84, 0.05); 
}

.item-canal.active-chan { 
    background-color: #e8f5e9; 
    font-weight: 600; 
}

@keyframes subirChatAnim {
    0% {
        background-color: rgba(25, 135, 84, 0.18);
        transform: scale(1.01);
    }
    100% {
        background-color: transparent;
        transform: scale(1);
    }
}

.chat-recien-actualizado {
    animation: subirChatAnim 0.75s ease-out;
}

/* Área de Mensajes */
.box-mensajes { 
    flex: 1 1 auto; 
    min-height: 0; 
    overflow-y: auto; 
    padding: 10px 12px; 
    display: flex; 
    flex-direction: column;
    background-color: #efeae2;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain !important;
    overscroll-behavior-y: contain !important;
    touch-action: pan-y;
}

/* Burbujas de Mensaje */
.bubble-msg {
    max-width: 82%;
    padding: 6px 10px 8px 10px;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 18px;
    word-break: break-word;
    box-shadow: 0 1px 1px rgba(0,0,0,0.13);
    position: relative;
    display: flex;
    flex-direction: column;
    transition: margin-bottom 0.15s ease;
}

.bubble-msg .msg-content {
    display: block;
    width: 100%;
}

.bubble-msg:has(.msg-reactions-wrapper:not(:empty)) {
    margin-bottom: 16px;
}

.bubble-msg.me { 
    background: var(--chat-bubble-me); 
    align-self: flex-end; 
    border-top-right-radius: 0px;
}

.bubble-msg.other { 
    background: var(--chat-bubble-other); 
    align-self: flex-start; 
    border-top-left-radius: 0px;
}

.bubble-msg small.msg-header-title { 
    display: block; 
    font-size: 11px; 
    color: #0d6efd; 
    font-weight: bold;
    margin-bottom: 2px; 
}

.bubble-msg .msg-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 3px;
    font-size: 10px;
    color: #8696a0;
    margin-top: 2px;
    line-height: 1;
    user-select: none;
}

.bubble-msg code {
    background-color: rgba(0, 0, 0, 0.06);
    padding: 2px 4px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 12px;
}

/* Cápsula de Reacciones */
.msg-reactions-wrapper {
    position: absolute;
    bottom: -13px;
    right: 12px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background-color: #ffffff1a;
    padding: 2px 7px;
    border-radius: 12px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    z-index: 10;
    cursor: pointer;
    user-select: none;
    transition: transform 0.15s ease;
}

.msg-reactions-wrapper:empty {
    display: none !important;
}

.msg-reactions-wrapper:hover {
    transform: scale(1.05);
}

.bubble-msg.me .msg-reactions-wrapper {
    left: auto;
    right: 10px;
}

.reaction-pill {
    background: transparent;
    border: none;
    padding: 0;
    font-size: 15px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 3px;
    line-height: 1;
    color: #212529;
}

.reaction-pill small {
    font-size: 13px;
    font-weight: 600;
    color: #333333;
}

.reaction-pill.active-my-react {
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 6px;
}

/* Popover contextual rápido de reacciones */
.chat-reactions-popover {
    position: absolute;
    z-index: 1080;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(4px);
    border-radius: 28px;
    padding: 5px 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    display: flex;
    gap: 4px;
    animation: popIn 0.15s ease-out;
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.chat-reactions-popover .react-opt {
    font-size: 20px;
    cursor: pointer;
    padding: 2px 4px;
    transition: transform 0.15s ease;
}

.chat-reactions-popover .react-opt:hover {
    transform: scale(1.3);
}

/* Herramientas y Entradas de Mensaje */
.chat-toolbar-format {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 0;
    background: transparent;
    border: none;
    flex-shrink: 0;
    height: 38px;
}

.btn-format-tool {
    background: transparent;
    border: none;
    color: #495057;
    padding: 4px 6px;
    border-radius: 4px;
    font-size: 13px;
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.btn-format-tool:hover {
    background: #e9ecef;
    color: #212529;
}

.btn-format-tool:active {
    background: #ced4da;
}

.box-input-chat { 
    display: flex; 
    align-items: flex-end; 
    gap: 4px;
    padding: 8px; 
    background: #ffffff;
    border-top: 1px solid #dee2e6;
    flex-shrink: 0 !important; 
}

.box-input-chat textarea { 
    flex: 1; 
    border: 1px solid #ced4da; 
    padding: 8px 10px; 
    border-radius: 6px; 
    outline: none; 
    font-size: 15px; 
    line-height: 1.35;
    height: 38px; 
    max-height: 96px; 
    resize: none; 
    overflow-y: auto;
    transition: border-color 0.2s ease;
    font-family: inherit;
}

.box-input-chat textarea:focus { border-color: var(--chat-primary); }

.box-input-chat button#chat-btn-enviar { 
    background: #128C7E; 
    color: white; 
    border: none; 
    height: 38px;
    padding: 0 12px; 
    margin-left: 0; 
    border-radius: 6px; 
    cursor: pointer; 
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease;
    flex-shrink: 0;
}

.box-input-chat button:hover {
    background: #0e6c61;
}

/* ==========================================================================
   REGLAS GLOBALES DE BADGE Y OCULTAMIENTO INTELIGENTE
   ========================================================================== */
#chat-root-container.collapsed #global-chat-badge {
    position: absolute;
    top: -3px;       
    right: -3px;     
    border: none !important;
    background-color: #dc3545 !important;
    color: #ffffff !important;
    padding: 3px 6px;
    font-size: 10px;
    font-weight: bold;
    border-radius: 50%;
    z-index: 10001;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* Ocultar badge globalmente cuando sea 0, "0" o esté vacío */
#global-chat-badge:empty,
#global-chat-badge[data-count="0"],
.dy-badge:empty,
.dy-badge[data-count="0"] {
    display: none !important;
}

/* Integración Emoji Picker */
.btn-emoji-tool {
    font-size: 14px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

#emoji-picker-container {
    position: absolute;
    bottom: 52px;
    right: 10px;
    z-index: 1070;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    overflow: hidden;
    background: #ffffff;
    display: none;
}

#emoji-picker-container.active {
    display: block;
    animation: fadeInPicker 0.15s ease-out;
}

@keyframes fadeInPicker {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

emoji-picker {
    width: 290px;
    height: 280px;
    --num-columns: 7;
    --emoji-size: 1.25rem;
    --background: #ffffff;
    --border-color: #e9ecef;
}

/* ==========================================================================
   Ajustes Responsive para Móviles
   ========================================================================== */
@media (max-width: 576px) {
    /* Selector de Emojis */
    #emoji-picker-container {
        right: 0;
        left: 0;
        margin: auto;
        width: 100%;
        max-width: 320px;
        bottom: 58px;
    }

    emoji-picker {
        width: 100%;
        height: 260px;
    }

    .box-input-chat:focus-within + .chat-toolbar-format,
    .chat-toolbar-format:focus-within {
        margin-top: auto;
    }

    /* BARRA DE CABECERA FLOTANTE EN MÓVIL */
    #chat-root-container:not(.collapsed) .chat-window-header {
        position: sticky !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        z-index: 10020 !important;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        flex-shrink: 0 !important;
    }

    /* CONTENEDOR BASE MÓVIL */
    #chat-root-container:not(.collapsed) {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: 100% !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        margin: 0 !important;
        padding: 0 !important;
        z-index: 10000;
        box-sizing: border-box !important;
        border-radius: 0 !important;
        pointer-events: auto !important;
        background: var(--chat-bg) !important;
        display: flex !important;
        flex-direction: column !important;
        overscroll-behavior: contain !important;
    }

    #chat-root-container.collapsed {
        top: auto !important;
        bottom: 0 !important;
        left: auto !important;
        right: 0 !important;
        width: 70px !important;
        height: 70px !important;
        position: fixed !important;
        z-index: 10000;
        background: transparent !important;
        box-shadow: none !important;
        overflow: visible !important;
        clip-path: none !important;
        border-radius: 0 !important;
        pointer-events: auto !important;
    }

    #chat-root-container.collapsed .chat-window-header {
        position: absolute !important;
        bottom: 16px !important;
        right: 16px !important;
        left: auto !important;
        top: auto !important;
        width: 46px !important;
        height: 46px !important;
        border-radius: 50% !important;
        padding: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        overflow: visible !important;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25) !important;
    }

    /* BADGE ROJO EN MÓVIL (Muestra solo si TIENE un valor distinto de 0 o vacío) */
    #chat-root-container.collapsed #global-chat-badge {
        position: absolute !important;
        top: -3px !important;
        right: -3px !important;
        transform: none !important;
        border: none !important;
        background-color: #dc3545 !important;
        color: #ffffff !important;
        padding: 3px 6px !important;
        font-size: 10px !important;
        font-weight: bold !important;
        border-radius: 10px !important;
        z-index: 10002 !important;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
    }

    /* REGLA DEFINITIVA PARA OCULTAR BADGE EN MOBILE */
    #chat-root-container.collapsed #global-chat-badge:empty,
    #chat-root-container.collapsed #global-chat-badge[data-count="0"] {
        display: none !important;
    }

    /* Adaptación de pantallas e inputs */
    #chat-root-container:not(.collapsed) .chat-window-body {
        flex: 1 1 auto !important;
        height: 100% !important;
        margin: 0 !important;
        padding: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
        position: relative !important;
        overflow: hidden !important;
    }

    #chat-root-container:not(.collapsed) .chat-screen {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        height: 100% !important;
        width: 100% !important;
        flex: 1 1 auto !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
    }

    #chat-root-container:not(.collapsed) .chat-screen.hidden-screen {
        display: none !important;
    }

    #chat-root-container:not(.collapsed) .box-mensajes {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        overflow-y: auto !important;
    }

    /* TEXTAREA EN MOVIL */
    #chat-root-container:not(.collapsed) .box-input-chat {
        position: sticky !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        flex-shrink: 0 !important;
        background: #ffffff !important;
        z-index: 10010 !important;
    }

    .box-input-chat textarea {
        font-size: 16px !important;
    }

    /* Parche para Firefox Mobile */
    @supports (-moz-appearance: none) {
        #chat-root-container:not(.collapsed) {
            height: 100dvh !important;
            position: fixed !important;
        }
    }
}