/* ========================================
   ConnectAI — Layout: Shell, Sidebar Nav, Content
   ======================================== */

.app-shell {
    display: flex;
    height: calc(100vh - var(--header-h));
    position: relative;
    z-index: 1;
    overflow: hidden;
}

/* ── Sidebar Nav ── */
.app-nav {
    width: var(--nav-width);
    flex-shrink: 0;
    background: var(--bg-white);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 12px 8px;
    overflow-y: auto;
    gap: 2px;
}


.nav-group-label {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-4);
    padding: 16px 12px 6px;
}
.nav-group-label:first-child { padding-top: 4px; }

.nav-divider {
    height: 1px;
    background: var(--border);
    margin: 8px 8px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-2);
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
    user-select: none;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    position: relative;
}

.nav-item:hover {
    color: var(--text-1);
    background: var(--bg-hover);
}

.nav-item.active {
    background: var(--primary);
    color: #fff;
    font-weight: 600;
}
.nav-item.active .nav-icon { color: #fff; }

.nav-icon {
    font-size: 17px;
    line-height: 1;
    flex-shrink: 0;
    width: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-3);
    transition: transform 0.15s;
}
.nav-item:hover .nav-icon { transform: scale(1.05); }

.nav-label { flex: 1; letter-spacing: 0.15px; }

/* Nav badge */
.nav-badge {
    font-size: 10px;
    font-weight: 700;
    min-width: 20px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    border-radius: 9px;
    padding: 0 5px;
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
}
.nav-badge--danger { background: var(--red-dim); color: var(--red); border: 1px solid rgba(239,68,68,0.15); }
.nav-badge--warn { background: var(--yellow-dim); color: var(--yellow); border: 1px solid rgba(234,179,8,0.15); }
.nav-badge--ok { background: var(--green-dim); color: var(--green); border: 1px solid rgba(34,197,94,0.15); }
.nav-badge--muted { background: var(--bg); color: var(--text-4); border: 1px solid var(--border); }

/* Active badge override */
.nav-item.active .nav-badge { background: rgba(255,255,255,0.2); color: #fff; border-color: rgba(255,255,255,0.15); }

/* Nav arrow for active item */
.nav-item__arrow {
    display: none;
    margin-left: auto;
    font-size: 11px;
    opacity: 0.7;
}
.nav-item.active .nav-item__arrow { display: block; }

/* Bottom section */
.nav-bottom {
    margin-top: auto;
    padding-top: 8px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.nav-item--danger { color: var(--red); }
.nav-item--danger:hover { background: var(--red-dim); }

/* ── Content Area ── */
.app-content {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

/* Page container */
.page {
    display: none;
    opacity: 0;
}

.page.active {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    animation: pageIn 0.2s ease forwards;
}

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

.page-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px 32px;
}

.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    padding: 24px 32px 0;
    flex-shrink: 0;
    gap: 16px;
}

.page-title {
    font-size: 22px;
    font-weight: 800;
    letter-spacing: -0.5px;
    color: var(--text-1);
}

.page-subtitle {
    font-size: 14px;
    color: var(--text-3);
    margin-top: 4px;
}

/* ── Split Layout (used by agents, security, webhooks, specs) ── */
.conversa-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.contacts-panel {
    width: 320px;
    flex-shrink: 0;
    border-right: 1px solid var(--border);
    background: var(--bg-white);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.contacts-panel-header {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.contacts-panel-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-1);
}

.contact-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px 8px;
}

.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
}

.chat-empty-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--text-3);
}

/* Chat header inside detail */
.chat-header {
    padding: 12px 20px;
    background: var(--bg-white);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
}
.chat-name { font-size: 14px; font-weight: 700; }
.chat-sub { font-size: 12px; color: var(--text-3); }
.chat-actions { display: flex; gap: 6px; margin-left: auto; }

/* Chat messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 24px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background-color: var(--bg);
    background-image:
        radial-gradient(ellipse at 15% 80%, rgba(108,71,255,0.025) 0%, transparent 45%),
        radial-gradient(ellipse at 85% 15%, rgba(108,71,255,0.02) 0%, transparent 45%),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.015'/%3E%3C/svg%3E");
}
[data-theme="dark"] .chat-messages {
    background-image:
        radial-gradient(ellipse at 15% 80%, rgba(108,71,255,0.04) 0%, transparent 45%),
        radial-gradient(ellipse at 85% 15%, rgba(108,71,255,0.03) 0%, transparent 45%),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.035'/%3E%3C/svg%3E");
}

/* Status bar */
.status-bar {
    height: 28px;
    background: var(--bg-white);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    font-size: 11px;
    color: var(--text-4);
    gap: 16px;
    flex-shrink: 0;
}

/* ── Mobile back button (hidden on desktop) ── */
.btn-back-mobile {
    display: none;
    align-items: center;
    gap: 4px;
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    padding: 4px;
    font-weight: 600;
    flex-shrink: 0;
}

/* ── Responsive ── */
@media (max-width: 1024px) {
    .app-nav { width: 60px; padding: 12px 6px; }
    .nav-label, .nav-group-label, .nav-badge, .nav-item__arrow { display: none; }
    .nav-item { padding: 10px; justify-content: center; }
    .contacts-panel { width: 260px; }
}

@media (max-width: 768px) {
    .app-nav {
        position: fixed;
        bottom: 0; left: 0; right: 0;
        width: 100%;
        height: auto;
        flex-direction: row;
        border-right: none;
        border-top: 1px solid var(--border);
        padding: 4px 8px;
        z-index: 99;
        overflow-x: auto;
        gap: 0;
    }
    .nav-label, .nav-group-label, .nav-divider, .nav-badge, .nav-item__arrow, .nav-bottom { display: none; }
    .nav-item { padding: 8px; flex-direction: column; gap: 2px; font-size: 10px; justify-content: center; }
    .app-shell { flex-direction: column; height: calc(100vh - var(--header-h) - 52px); }

    /* ── Split layout: mobile stacked ── */
    .conversa-layout {
        position: relative;
        overflow: hidden;
    }
    .conversa-layout > * {
        width: 100% !important;
        max-width: none !important;
        flex-shrink: 0;
        border-right: none !important;
        border-left: none !important;
    }
    /* Sidebar: visible by default */
    .conversa-layout > .contacts-panel {
        position: absolute;
        inset: 0;
        z-index: 1;
    }
    /* Detail areas: overlay on top of sidebar, hidden by default */
    .conversa-layout > .chat-area {
        position: absolute;
        inset: 0;
        z-index: 2;
        display: none;
    }
    .conversa-layout > .chat-area.mobile-open {
        display: flex;
    }
    /* Chat right panel */
    #chatPanel {
        position: absolute !important;
        inset: 0 !important;
        z-index: 3 !important;
        width: 100% !important;
    }
    /* Show back button */
    .btn-back-mobile { display: flex !important; }
    /* Page header smaller */
    .page-header { padding: 16px !important; }
    .page-title { font-size: 18px !important; }
    .page-scroll { padding: 12px !important; }
    /* Stats responsive */
    .stats-row { grid-template-columns: repeat(2, 1fr) !important; }
    /* Modal body scroll fix */
    .modal-body { max-height: 60vh; overflow-y: auto; }
    /* Form rows stack */
    .form-row { flex-direction: column; }
    /* Table horizontal scroll */
    .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .table-wrap table { min-width: 600px; }
    /* Chat header compact */
    .chat-header { padding: 8px 12px !important; }
    .chat-actions { gap: 4px !important; }
    .chat-actions .btn { font-size: 10px !important; padding: 4px 8px !important; }
    /* Workspace picker */
    .ws-picker { max-width: 120px; }
}
