/* =============================================================================
   WhatsApp Web integrado no CRM Comercial
   Hierarquia: app-frame > content-area > #tab-conv > layout > sidebar + main
   ============================================================================= */

/* --- Espaço para bottom-nav fixa (mobile/tablet) --- */
@media (max-width: 1023px) {
    body.wa-tab-conv-activo .content-area {
        padding-bottom: calc(64px + env(safe-area-inset-bottom));
        box-sizing: border-box;
    }
}

/* --- Tab conversas ocupa a área útil do CRM (sem scroll externo) --- */
#tab-conv.wa-web.tab-panel.active,
#tab-conv.wa-web.active {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    max-height: 100% !important;
    overflow: hidden !important;
    background: #fff !important;
    padding: 0 !important;
}

@media (min-width: 1024px) {
    #tab-conv.wa-web.tab-panel.active {
        overflow: hidden !important;
    }
}

/* --- Layout principal (lista + chat) --- */
.wa-web-layout {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

/* --- Sidebar lista de conversas --- */
.wa-web-sidebar {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    width: 100%;
    max-width: 380px;
    min-width: 280px;
    min-height: 0;
    background: #fff;
    border-right: 1px solid #d1d7db;
}

.wa-web-sidebar-head {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
    min-height: 56px;
    padding: 10px 16px;
    background: #f0f2f5;
}

.wa-web-sidebar-head .wa-title {
    flex: 1;
    font-size: 15px;
    font-weight: 600;
    color: #111b21;
}

.wa-icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: #54656f;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.wa-icon-btn:hover { background: rgba(0,0,0,0.06); }

.wa-web-search {
    flex-shrink: 0;
    padding: 8px 12px;
    background: #fff;
    border-bottom: 1px solid #e9edef;
}

.wa-web-search-inner {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    background: #f0f2f5;
    border-radius: 8px;
}

.wa-web-search-inner i { color: #54656f; font-size: 14px; }

.wa-web-search-inner input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 14px;
    color: #111b21;
}

.wa-web-filters {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    padding: 8px 12px 10px;
    overflow-x: auto;
    background: #fff;
    border-bottom: 1px solid #e9edef;
}

.wa-web-filters .filter-tab {
    padding: 6px 12px;
    border: none;
    border-radius: 16px;
    background: #f0f2f5;
    font-size: 13px;
    font-weight: 500;
    color: #54656f;
    white-space: nowrap;
    cursor: pointer;
}

.wa-web-filters .filter-tab.active {
    background: #d9fdd3;
    color: #008069;
}

#lista-whats.wa-web-chatlist {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    background: #fff;
}

.wa-chat-item {
    display: flex;
    gap: 13px;
    align-items: center;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid #f0f2f5;
}

.wa-chat-item:hover { background: #f5f6f6; }
.wa-chat-item.conv-ativa { background: #f0f2f5; }

.wa-chat-avatar {
    width: 49px;
    height: 49px;
    border-radius: 50%;
    flex-shrink: 0;
    background: #dfe5e7;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 18px;
    overflow: hidden;
}

.wa-chat-body { flex: 1; min-width: 0; }

.wa-chat-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.wa-chat-name {
    flex: 1;
    min-width: 0;
    font-size: 15px;
    font-weight: 700;
    color: #111b21;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.25;
}

.wa-chat-phone {
    font-size: 12px;
    font-weight: 500;
    color: #667781;
    margin-top: 1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

.wa-chat-time { font-size: 12px; color: #667781; flex-shrink: 0; }

.wa-chat-preview {
    margin-top: 3px;
    font-size: 14px;
    color: #667781;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.wa-chat-badge {
    min-width: 20px;
    height: 20px;
    margin-top: 3px;
    background: #25d366;
    color: #fff;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* Traços estilo WhatsApp Web */
.wa-ticks {
    display: inline-flex;
    align-items: center;
    margin-left: 3px;
    vertical-align: middle;
    letter-spacing: -2px;
    font-size: 11px;
    line-height: 1;
    position: relative;
    top: 1px;
}
.wa-ticks .t { font-family: inherit; font-weight: 700; }
.wa-ticks.pending .t,
.wa-ticks.server .t { color: #667781; }
.wa-ticks.device .t { color: #667781; }
.wa-ticks.read .t,
.wa-ticks.played .t { color: #53bdeb; }
.wa-ticks.error .t { color: #ea4335; }

/* --- Painel direito (vazio ou chat) --- */
.wa-web-main {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    background: #f0f2f5;
}

.wa-web-empty {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 32px 24px;
    border-bottom: 6px solid #25d366;
}

.wa-web-empty-icon {
    font-size: 100px;
    color: #25d366;
    opacity: 0.3;
    margin-bottom: 16px;
}

.wa-web-empty h2 {
    margin: 0 0 12px;
    font-size: 28px;
    font-weight: 300;
    color: #41525d;
}

.wa-web-empty p {
    margin: 0;
    max-width: 420px;
    font-size: 14px;
    line-height: 1.6;
    color: #667781;
}

.wa-web-empty .wa-lock {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 20px;
    font-size: 13px;
    color: #667781;
}

/* --- Painel de chat aberto --- */
#tab-conv #chat-view {
    display: none;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    overflow: hidden;
    background: #efeae2;
}

body.chat-cliente-aberto #tab-conv #chat-view {
    display: flex !important;
}

body.chat-cliente-aberto #tab-conv #conv-detail-placeholder {
    display: none !important;
}

/* Cabeçalho do chat */
#tab-conv .wa-chat-head {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
    min-height: 56px;
    padding: 10px 16px;
    background: #f0f2f5;
    border-bottom: 1px solid #e9edef;
}

#tab-conv .wa-chat-head .wa-back {
    display: none;
    color: #54656f;
    font-size: 20px;
    cursor: pointer;
    padding: 8px;
    margin: -8px 0 -8px -8px;
}

#tab-conv .wa-chat-head .list-avatar {
    width: 40px;
    height: 40px;
    font-size: 15px;
}

#tab-conv .wa-chat-head-info { flex: 1; min-width: 0; }

#tab-conv .wa-chat-head-name {
    font-size: 17px;
    font-weight: 700;
    color: #111b21;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

#tab-conv .wa-chat-head-status {
    font-size: 12px;
    font-weight: 500;
    color: #667781;
    margin-top: 1px;
}

#tab-conv #chat-view {
    position: relative;
}

#tab-conv .wa-contact-panel {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(380px, 100%);
    background: #f0f2f5;
    border-left: 1px solid #d1d7db;
    z-index: 40;
    overflow-y: auto;
    box-shadow: -4px 0 24px rgba(11, 20, 26, 0.08);
}
#tab-conv #chat-view.perfil-aberto .wa-contact-panel {
    display: block;
}
.wa-contact-panel-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 56px;
    padding: 10px 16px;
    background: #008069;
    color: #fff;
    font-size: 16px;
    font-weight: 500;
}
.wa-contact-panel-bar .wa-icon-btn { color: #fff; }
.wa-contact-hero {
    background: #fff;
    padding: 28px 20px 22px;
    text-align: center;
    margin-bottom: 10px;
}
.wa-contact-hero-avatar {
    width: 200px;
    height: 200px;
    border-radius: 50%;
    margin: 0 auto 18px;
    background: #dfe5e7;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 72px;
    color: #fff;
    font-weight: 600;
}
.wa-contact-hero-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.wa-contact-hero-name {
    font-size: 22px;
    font-weight: 500;
    color: #111b21;
    line-height: 1.3;
    word-break: break-word;
}
.wa-contact-hero-sub {
    margin-top: 6px;
    font-size: 15px;
    color: #667781;
}
.wa-contact-block {
    background: #fff;
    padding: 14px 20px 16px;
    margin-bottom: 10px;
}
.wa-contact-label {
    font-size: 13px;
    color: #008069;
    font-weight: 500;
    margin-bottom: 6px;
}
.wa-contact-value {
    font-size: 16px;
    color: #111b21;
    line-height: 1.4;
    word-break: break-word;
}
.wa-contact-hint {
    margin-top: 4px;
    font-size: 13px;
    color: #667781;
}
.wa-contact-actions {
    padding: 12px 20px 28px;
}

/* Barra atendente */
#tab-conv .chat-atendente-bar {
    display: none;
    flex-shrink: 0;
    padding: 7px 14px;
    background: #fff;
    color: #111b21;
    font-size: 12px;
    text-align: center;
    border-bottom: 1px solid #e9edef;
}

#tab-conv .chat-atendente-bar.visivel {
    display: block !important;
}

/* Corpo: mensagens + zona composer */
#tab-conv .wa-chat-body {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

#tab-conv #msgs-whats,
#tab-conv .chat-area {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 16px 48px;
    background-color: #efeae2;
    background-image: url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png');
    background-size: 380px;
}

/* Bolhas */
#tab-conv .bubble {
    max-width: 65%;
    padding: 6px 8px;
    border-radius: 7.5px;
    font-size: 14px;
    line-height: 1.35;
    box-shadow: 0 1px 0.5px rgba(11,20,26,0.13);
    word-wrap: break-word;
    margin-bottom: 2px;
}

#tab-conv .bubble.in {
    align-self: flex-start;
    background: #fff;
    border-top-left-radius: 0;
}

#tab-conv .bubble.out {
    align-self: flex-end;
    background: #d9fdd3;
    border-top-right-radius: 0;
}

#tab-conv .bubble-time {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    color: #667781;
    text-align: right;
    white-space: nowrap;
}

#tab-conv .bubble-time i {
    margin-left: 3px;
    font-size: 10px;
}

#tab-conv .chat-day-sep {
    align-self: center;
    margin: 10px 0 6px;
    padding: 5px 12px;
    background: rgba(255,255,255,0.92);
    border-radius: 7.5px;
    font-size: 12px;
    font-weight: 600;
    color: #54656f;
    box-shadow: 0 1px 0.5px rgba(11,20,26,0.13);
    text-transform: capitalize;
}

#tab-conv .chat-agent-banner {
    align-self: center;
    max-width: 90%;
    margin: 6px auto;
    padding: 6px 12px;
    background: rgba(255,255,255,0.92);
    border-radius: 7.5px;
    font-size: 12px;
    color: #54656f;
    text-align: center;
    box-shadow: 0 1px 0.5px rgba(11,20,26,0.13);
}

/* --- Zona composer (sempre visível, dentro do CRM) --- */
#tab-conv .wa-composer-zone {
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
    flex-shrink: 0;
    background: #f0f2f5;
    border-top: 1px solid #e9edef;
    position: relative;
    z-index: 2;
}

/* Grelha de ações CRM (Catálogo, Proposta, etc.) */
#tab-conv .chat-action-grid {
    display: none;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 10px 12px;
    background: #f0f2f5;
    border-bottom: 1px solid #d1d7db;
}

#tab-conv .chat-action-grid.open {
    display: grid !important;
}

#tab-conv .chat-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 10px 6px;
    border: none;
    border-radius: 8px;
    color: #fff;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    text-align: center;
}

#tab-conv .chat-btn i { font-size: 18px; }

/* Sub-menus (anexos, etiquetas) */
#tab-conv .wa-composer-zone .sub-menu {
    display: none !important;
    position: static !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    width: 100% !important;
    max-height: min(38vh, 260px);
    overflow-y: auto;
    padding: 12px 16px;
    background: #fff !important;
    border-bottom: 1px solid #e9edef;
    box-shadow: none !important;
    z-index: auto !important;
}

#tab-conv .wa-composer-zone .sub-menu.active {
    display: block !important;
}

#tab-conv .sm-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid #f0f2f5;
    font-weight: 500;
    color: #111b21;
    cursor: pointer;
}

/* Composer principal */
#tab-conv .wa-composer {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px 10px;
    background: #f0f2f5;
}

#tab-conv .wa-composer-btn,
#tab-conv .wa-composer-send {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: #54656f;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    padding: 0;
}

#tab-conv .wa-composer-btn { font-size: 20px; }
#tab-conv .wa-composer-send { font-size: 22px; }
#tab-conv .wa-composer-send.modo-enviar { color: #008069; }
#tab-conv .wa-composer-send.gravando { color: #ea0038 !important; }

#tab-conv .wa-composer-input-wrap {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    min-height: 42px;
    padding: 8px 12px;
    background: #fff;
    border-radius: 8px;
}

#tab-conv .wa-composer-input-wrap input {
    width: 100%;
    border: none;
    outline: none;
    background: transparent;
    font-size: 15px;
    color: #111b21;
}

/* Painel QR pessoal */
#tab-conv #wa-pessoal-panel {
    flex-shrink: 0;
    padding: 10px 16px;
    background: #f0f2f5;
    border-bottom: 1px solid #e9edef;
    font-size: 12px;
}

#tab-conv .conv-main-header { display: none !important; }

/* Anular CSS legado que quebrava o layout */
body.chat-cliente-aberto #tab-conv .conv-detail-col,
body.chat-cliente-aberto #tab-conv .wa-web-main {
    position: relative !important;
    inset: auto !important;
    z-index: auto !important;
    height: auto !important;
    max-height: none !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow: hidden !important;
}

/* --- Mobile: lista OU chat, dentro do CRM --- */
@media (max-width: 767px) {
    .wa-web-layout {
        flex-direction: column;
        flex: 1 1 auto;
        min-height: 0;
    }

    .wa-web-sidebar {
        max-width: none;
        width: 100%;
        flex: 1 1 auto;
        min-height: 0;
        border-right: none;
    }

    .wa-web-main {
        display: none;
        flex: 1 1 auto;
        min-height: 0;
        width: 100%;
        overflow: hidden;
    }

    body.chat-cliente-aberto .wa-web-sidebar {
        display: none !important;
    }

    body.chat-cliente-aberto .wa-web-main {
        display: flex !important;
        flex: 1 1 auto;
        min-height: 0;
    }

    #tab-conv .wa-chat-head .wa-back {
        display: block !important;
    }

    #tab-conv #msgs-whats,
    #tab-conv .chat-area {
        padding: 12px 14px;
    }

    #tab-conv .bubble {
        max-width: 85%;
    }
}

/* --- Desktop: split view --- */
@media (min-width: 768px) {
    .wa-web-layout { flex-direction: row; }
    .wa-web-main { display: flex !important; }
    #tab-conv .wa-chat-head .wa-back { display: none !important; }
}
