/* ============================================================
   message-chats-ui.css - Interfaz estilo WhatsApp Web
   ============================================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --wa-green: #00a884;
    --wa-green-dark: #008069;
    --wa-teal: #075e54;
    --wa-chat-bg: #efeae2;
    --wa-panel: #f0f2f5;
    --wa-border: #d1d7db;
    --wa-text: #111b21;
    --wa-text-secondary: #667781;
    --wa-bubble-out: #d9fdd3;
    --wa-bubble-in: #ffffff;
}

html, body {
    height: 100%;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #0b141a 0%, #1f2c34 100%);
    color: var(--wa-text);
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ---------- Barra superior de sesión ---------- */
.topbar {
    background: var(--wa-teal);
    color: #fff;
    padding: 10px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    flex-wrap: wrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
    z-index: 20;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 14px;
}

.back-home {
    color: #fff;
    text-decoration: none;
    font-size: 1.2rem;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.back-home:hover {
    background: rgba(255, 255, 255, 0.15);
}

.topbar-title {
    font-size: 1.2rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 10px;
}

.topbar-title i {
    font-size: 1.5rem;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.session-control {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.15);
    padding: 6px 12px;
    border-radius: 24px;
}

.session-control label {
    font-size: 0.85rem;
    opacity: 0.9;
}

.session-control input {
    background: transparent;
    border: none;
    border-bottom: 2px solid rgba(255, 255, 255, 0.5);
    color: #fff;
    padding: 4px 6px;
    font-size: 0.95rem;
    width: 120px;
    outline: none;
    font-family: inherit;
}

.session-control input:focus {
    border-bottom-color: #fff;
}

.status-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #f5f6f6;
    flex-shrink: 0;
    transition: background 0.3s;
}

.status-indicator.connected {
    background: #25d366;
    box-shadow: 0 0 6px #25d366;
}

.status-indicator.disconnected {
    background: #f15c6d;
}

.status-text {
    font-size: 0.9rem;
    opacity: 0.95;
    min-width: 90px;
}

.btn-refresh {
    background: none;
    border: none;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    transition: transform 0.3s;
}

.btn-refresh:hover {
    transform: rotate(180deg);
}

.btn-refresh.spinning i {
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ---------- Contenedor principal tipo WhatsApp ---------- */
.wa-app {
    flex: 1;
    display: flex;
    min-height: 0;
    background: var(--wa-chat-bg);
}

/* ---------- Panel izquierdo (chats) ---------- */
.wa-sidebar {
    width: 32%;
    min-width: 300px;
    max-width: 460px;
    background: #fff;
    border-right: 1px solid var(--wa-border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    z-index: 10;
}

.sidebar-tools {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--wa-panel);
    border-bottom: 1px solid var(--wa-border);
}

.search-box {
    flex: 1;
    display: flex;
    align-items: center;
    background: #fff;
    border-radius: 24px;
    padding: 7px 14px;
    gap: 8px;
    border: 1px solid var(--wa-border);
}

.search-box i {
    color: var(--wa-text-secondary);
    font-size: 0.85rem;
}

.search-box input {
    flex: 1;
    border: none;
    outline: none;
    font-size: 0.9rem;
    background: transparent;
    font-family: inherit;
}

.btn-close-sidebar {
    display: none;
    background: none;
    border: none;
    color: var(--wa-text-secondary);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 6px;
}

.chats-list {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.chats-list::-webkit-scrollbar {
    width: 6px;
}

.chats-list::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 4px;
}

.chats-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--wa-text-secondary);
}

.chats-empty.hidden, .chat-view.hidden, .chat-empty-state.hidden,
.modal-overlay.hidden, .emoji-picker.hidden, .chat-menu.hidden,
.typing-hint.hidden, #chatAvatarImg.hidden {
    display: none !important;
}

.chats-empty-icon {
    font-size: 3.5rem;
    color: var(--wa-green);
    opacity: 0.5;
    margin-bottom: 15px;
}

/* Ítem de chat en la lista */
.chat-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    cursor: pointer;
    border-bottom: 1px solid #f0f2f5;
    transition: background 0.15s;
}

.chat-item:hover {
    background: #f5f6f6;
}

.chat-item.active {
    background: #f0f2f5;
}

.chat-item-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 600;
    font-size: 1.1rem;
    text-transform: uppercase;
    flex-shrink: 0;
    position: relative;
}

.chat-item-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-item-avatar i {
    font-size: 1.4rem;
    opacity: 0.9;
}

.chat-item-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.chat-item-name-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.chat-item-name {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-time {
    font-size: 0.7rem;
    color: var(--wa-text-secondary);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 4px;
}

.chat-item-time .fa-check-double {
    color: #53bdeb;
}

.chat-item-preview-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.chat-item-preview {
    font-size: 0.82rem;
    color: var(--wa-text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-preview.mine {
    color: var(--wa-text-secondary);
}

.chat-item-preview .fa-check-double {
    color: #53bdeb;
    font-size: 0.7rem;
    margin-right: 4px;
}

.chat-item-preview .fa-check {
    font-size: 0.7rem;
    margin-right: 4px;
}

.chat-item-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    flex-shrink: 0;
}

.unread-badge {
    background: var(--wa-green);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 19px;
    height: 19px;
    border-radius: 10px;
    padding: 0 5px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.pin-icon {
    color: var(--wa-text-secondary);
    font-size: 0.7rem;
}

/* Pie del sidebar: botón nuevo chat */
.sidebar-footer {
    padding: 10px;
    border-top: 1px solid var(--wa-border);
    background: var(--wa-panel);
}

.btn-new-chat {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: var(--wa-green);
    color: #fff;
    border: none;
    padding: 11px;
    border-radius: 24px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.2s;
}

.btn-new-chat:hover {
    background: var(--wa-green-dark);
}

/* ---------- Panel derecho (conversación) ---------- */
.wa-main {
    flex: 1;
    min-width: 0;
    position: relative;
    display: flex;
    flex-direction: column;
    background-color: var(--wa-chat-bg);
    background-image:
        radial-gradient(circle at 15px 15px, rgba(0, 0, 0, 0.03) 2px, transparent 2px),
        radial-gradient(circle at 55px 45px, rgba(0, 0, 0, 0.02) 1px, transparent 1px),
        radial-gradient(circle at 85px 70px, rgba(0, 0, 0, 0.025) 2px, transparent 2px);
    background-size: 100px 100px;
    background-position: 0 0, 40px 60px, 10px 10px;
}

/* Estado vacío */
.chat-empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 30px;
}

.wa-logo {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    background: rgba(0, 168, 132, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 25px;
}

.wa-logo i {
    font-size: 3.2rem;
    color: var(--wa-green);
}

.chat-empty-state h2 {
    color: var(--wa-text);
    font-size: 1.9rem;
    margin-bottom: 12px;
}

.chat-empty-state p {
    color: var(--wa-text-secondary);
    font-size: 0.95rem;
    max-width: 380px;
    line-height: 1.5;
}

/* Cabecera de la conversación */
.chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-header {
    background: var(--wa-panel);
    border-bottom: 1px solid var(--wa-border);
    padding: 9px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
    z-index: 5;
}

.btn-toggle-sidebar {
    display: none;
    background: none;
    border: none;
    color: var(--wa-text);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 6px;
}

.chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 600;
    font-size: 1rem;
    text-transform: uppercase;
    flex-shrink: 0;
    overflow: hidden;
}

.chat-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-contact {
    flex: 1;
    min-width: 0;
}

.chat-contact h3 {
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-contact p {
    font-size: 0.78rem;
    color: var(--wa-text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-actions {
    display: flex;
    gap: 4px;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--wa-text);
    font-size: 1.05rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.15s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.06);
}

/* Menú desplegable del chat */
.chat-menu {
    position: absolute;
    top: 58px;
    right: 12px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    z-index: 100;
    min-width: 220px;
    padding: 6px;
}

.chat-menu button {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    background: none;
    border: none;
    padding: 12px 14px;
    font-size: 0.9rem;
    cursor: pointer;
    color: var(--wa-text);
    text-align: left;
    font-family: inherit;
    border-radius: 8px;
    transition: background 0.1s;
}

.chat-menu button:hover {
    background: #f0f2f5;
}

.chat-menu button i {
    width: 18px;
    text-align: center;
    color: var(--wa-text-secondary);
}

/* Lista de mensajes */
.messages-container {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding: 20px 8%;
}

.messages-container::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-thumb {
    background: #b8bcc0;
    border-radius: 4px;
}

.messages-list {
    display: flex;
    flex-direction: column;
}

/* Separador de fecha */
.date-divider {
    align-self: center;
    background: rgba(255, 255, 255, 0.85);
    border-radius: 8px;
    padding: 5px 14px;
    font-size: 0.75rem;
    color: var(--wa-text-secondary);
    font-weight: 500;
    margin: 12px 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(2px);
}

/* Burbujas */
.message-row {
    display: flex;
    margin-bottom: 4px;
}

.message-row.incoming {
    justify-content: flex-start;
}

.message-row.outgoing {
    justify-content: flex-end;
}

.bubble {
    position: relative;
    max-width: 65%;
    padding: 7px 9px 8px 11px;
    border-radius: 10px;
    font-size: 0.92rem;
    line-height: 1.4;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.12);
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}

.message-row.incoming .bubble {
    background: var(--wa-bubble-in);
    border-top-left-radius: 2px;
    margin-left: 8px;
}

.message-row.outgoing .bubble {
    background: var(--wa-bubble-out);
    border-top-right-radius: 2px;
    margin-right: 8px;
}

.bubble .msg-author {
    font-size: 0.78rem;
    font-weight: 600;
    display: block;
    margin-bottom: 2px;
}

.bubble .msg-media-placeholder {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--wa-text);
    font-style: italic;
    opacity: 0.85;
}

.bubble .msg-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 5px;
    float: right;
    margin: 6px -2px -4px 10px;
    font-size: 0.68rem;
    color: var(--wa-text-secondary);
    user-select: none;
}

.bubble .msg-footer .fa-check-double {
    font-size: 0.75rem;
}

.bubble .msg-footer .fa-check-double.read {
    color: #53bdeb;
}

.bubble .msg-footer .fa-check {
    font-size: 0.75rem;
}

/* Aviso mensaje eliminado / sistema */
.message-row .bubble.revoked {
    font-style: italic;
    color: var(--wa-text-secondary);
    background: rgba(255, 255, 255, 0.6);
    box-shadow: none;
}

/* ---------- Composer ---------- */
.composer {
    background: var(--wa-panel);
    border-top: 1px solid var(--wa-border);
    padding: 8px 16px;
    position: relative;
}

.composer-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.composer-input-wrap {
    flex: 1;
    position: relative;
}

.composer-input-wrap input {
    width: 100%;
    border: none;
    border-radius: 24px;
    padding: 12px 18px;
    font-size: 0.95rem;
    outline: none;
    background: #fff;
    font-family: inherit;
}

.btn-emoji {
    font-size: 1.3rem;
    color: var(--wa-text);
}

.btn-send {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--wa-green);
    border: none;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-send:hover {
    background: var(--wa-green-dark);
}

.btn-send:active {
    transform: scale(0.92);
}

.btn-send:disabled {
    background: #c1c9cf;
    cursor: not-allowed;
}

/* Selector emoji simple */
.emoji-picker {
    position: absolute;
    bottom: 55px;
    left: 0;
    background: #fff;
    border: 1px solid var(--wa-border);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.18);
    padding: 10px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 4px;
    z-index: 90;
    max-height: 220px;
    overflow-y: auto;
}

.emoji-picker button {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    transition: background 0.1s;
}

.emoji-picker button:hover {
    background: #f0f2f5;
}

/* Hint "escribiendo" */
.typing-hint {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 8px;
    padding: 8px 4px 0;
    font-size: 0.8rem;
    color: var(--wa-text-secondary);
}

.typing-hint i {
    color: var(--wa-green);
}

.typing-dots {
    display: inline-flex;
    gap: 3px;
    align-items: center;
}

.typing-dots i {
    width: 7px;
    height: 7px;
    background: var(--wa-green);
    border-radius: 50%;
    animation: blink 1.4s infinite both;
}

.typing-dots i:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dots i:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes blink {
    0%, 80%, 100% { opacity: 0.2; }
    40% { opacity: 1; }
}

/* ---------- Modal ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.modal {
    background: #fff;
    border-radius: 14px;
    width: 92%;
    max-width: 400px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.modal-header {
    background: var(--wa-teal);
    color: #fff;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-close {
    background: none;
    border: none;
    color: #fff;
    font-size: 1.6rem;
    cursor: pointer;
    line-height: 1;
}

.modal-body {
    padding: 22px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.modal-hint {
    font-size: 0.85rem;
    color: var(--wa-text-secondary);
}

.modal-body input {
    width: 100%;
    border: 2px solid var(--wa-border);
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 1rem;
    outline: none;
    font-family: inherit;
    transition: border-color 0.2s;
}

.modal-body input:focus {
    border-color: var(--wa-green);
}

.btn-confirm {
    background: var(--wa-green);
    color: #fff;
    border: none;
    padding: 12px;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background 0.2s;
}

.btn-confirm:hover {
    background: var(--wa-green-dark);
}

/* ---------- Toasts ---------- */
#toastContainer {
    position: fixed;
    top: 70px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 300;
}

.toast {
    background: #323739;
    color: #fff;
    padding: 12px 18px;
    border-radius: 8px;
    font-size: 0.9rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    gap: 10px;
    animation: slideIn 0.25s ease;
    max-width: 320px;
}

.toast.success {
    border-left: 4px solid #25d366;
}

.toast.error {
    border-left: 4px solid #f15c6d;
}

.toast.info {
    border-left: 4px solid #53bdeb;
}

.toast.fade-out {
    opacity: 0;
    transition: opacity 0.4s;
}

@keyframes slideIn {
    from { transform: translateX(40px); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .wa-app {
        flex-direction: row;
    }

    .wa-sidebar {
        width: 85%;
        max-width: 380px;
        min-width: 0;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.2);
    }

    .wa-sidebar.closed {
        transform: translateX(-105%);
        transition: transform 0.25s ease;
    }

    .wa-sidebar {
        transition: transform 0.25s ease;
    }

    .btn-close-sidebar {
        display: inline-flex;
    }

    .btn-toggle-sidebar {
        display: inline-flex;
    }

    .bubble {
        max-width: 85%;
    }

    .messages-container {
        padding: 14px 3%;
    }

    .chat-empty-state {
        display: none;
    }

    .wa-main {
        width: 100%;
    }
}
