/* ====================================================================
   ALZAR LOGIC - SHELL LAYOUT (Strict)
   ==================================================================== */

:root {
    /* --- STRICT LAYOUT VARIABLES --- */
    --sidebar-w-exp: 260px;
    --sidebar-w-col: 72px;
    --header-h: 52px;
}

/* --- RESET --- */
body {
    margin: 0;
    padding: 0;
    background-color: var(--color-bg-base);
    color: var(--color-text-main);
    overflow: hidden; 
    font-family: var(--font-base);
}

/* --- APP SHELL --- */
#app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w-exp) 1fr;
    height: 100vh;
    width: 100vw;
    transition: grid-template-columns 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

#app-shell.shell-collapsed {
    grid-template-columns: var(--sidebar-w-col) 1fr;
}

/* --- SIDEBAR --- */
#app-sidebar {
    background-color: var(--color-bg-surface);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    z-index: 100;
}

/* ============================================================
   🔒 LOCKED: ALZAR LOGO & SIDEBAR TOGGLE - DO NOT MODIFY
   Working implementation copied from Alzar Logic project
   ============================================================ */

/* Alzar Sidebar Header */
.alzar-sidebar-header {
    height: var(--header-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    border-bottom: 1px solid var(--color-border);
    white-space: nowrap;
}

.alzar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    user-select: none;
}

.alzar-brand-icon {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.alzar-brand-icon:hover {
    transform: scale(1.05);
}

.alzar-brand-icon i {
    font-size: 22px;
    color: var(--color-text-muted);
    transition: color 0.2s ease;
}

.alzar-brand-icon:hover i {
    color: var(--color-text-main);
}

/* Icon swap — show only the relevant icon per state */
.alzar-brand-icon .sidebar-icon-collapsed { display: none; }
.alzar-brand-icon .sidebar-icon-expanded  { display: block; }

#app-shell.shell-collapsed .alzar-brand-icon .sidebar-icon-collapsed { display: block; }
#app-shell.shell-collapsed .alzar-brand-icon .sidebar-icon-expanded  { display: none; }

/* "Navigation" label next to icon */
.alzar-brand-menu-label {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--color-text-muted);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    white-space: nowrap;
}

/* Sidebar Toggle Button */
.action-icon {
    background: none;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.action-icon:hover {
    color: var(--color-text-main);
    background-color: var(--overlay-hover);
}
.action-icon i { font-size: 1.2rem; }

/* Hide text labels when collapsed */
#app-shell.shell-collapsed .alzar-brand-menu-label,
#app-shell.shell-collapsed .nav-label,
#app-shell.shell-collapsed .user-info,
#app-shell.shell-collapsed .user-chevron {
    opacity: 0;
    pointer-events: none;
    display: none;
}

/* Navigation */
.sidebar-menu {
    flex: 1;
    min-height: 0;
    padding: 16px 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
    overflow-y: auto;
    overflow-x: hidden;
}

.nav-item {
    background: none;
    border: none;
    width: 100%;
    display: flex;
    align-items: center;
    padding: 12px 16px;
    color: var(--color-text-muted);
    cursor: pointer;
    transition: all 0.2s;
    text-align: left;
    font-size: 0.95rem;
    white-space: nowrap;
}

.nav-item:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-main);
}

.nav-item.active {
    background-color: var(--color-active-dim);
    color: var(--color-active);
    border-right: 3px solid var(--color-active);
    filter: var(--glow-active);
}

.nav-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    margin-right: 12px;
}
.nav-icon i { font-size: 1.3rem; }

.nav-spacer { flex: 1; }

/* Quick Backup Button (Browser Mode Only) */
.nav-backup {
    border-top: 1px solid var(--color-border);
    border-bottom: 1px solid var(--color-border);
    margin: 4px 0;
    background: linear-gradient(135deg, rgba(251, 146, 60, 0.1), rgba(251, 146, 60, 0.05));
    position: relative;
}

.nav-backup:hover {
    background: linear-gradient(135deg, rgba(251, 146, 60, 0.15), rgba(251, 146, 60, 0.08));
    color: #fb923c !important;
}

.nav-backup::before {
    content: "TEST";
    position: absolute;
    top: 4px;
    right: 8px;
    font-size: 0.6rem;
    background: rgba(251, 146, 60, 0.9);
    color: white;
    padding: 2px 4px;
    border-radius: 3px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

/* User Switcher */
.user-trigger {
    background: none;
    border: none;
    border-top: 1px solid var(--color-border);
    padding: 16px;
    display: flex;
    align-items: center;
    width: 100%;
    text-align: left;
    cursor: pointer;
    color: var(--color-text-main);
}
.user-trigger:hover {
    background-color: var(--color-bg-hover);
}

/* --- MAIN VIEWPORT --- */
#app-viewport {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background-color: var(--color-bg-base);
    min-height: 0;
}

/* --- APP TOPBAR (search, bell, AI, user) --- */
#app-topbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 var(--app-gap-strict);
    height: 52px;
    min-height: 52px;
    background-color: var(--color-bg-surface);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
    z-index: 50;
}

.topbar-spacer {
    flex: 1;
}

/* --- SCROLLABLE CONTENT AREA --- */
#app-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--app-gap-strict);
    min-height: 0;
}

.view-container {
    display: none;
    height: 100%;
    animation: fadeIn 0.3s ease-out;
}

.view-container.active-view {
    display: block;
}

/* Settings needs flex-column for scroll chain (min-height:0 cascade) */
#settingsPage.active-view {
    display: flex;
    flex-direction: column;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.spin {
    animation: spin 1s linear infinite;
    display: inline-block;
}

.view-header {
    margin-bottom: var(--app-gap-strict);
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 10px;
}

.view-title {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 12px;
}

/* --- SETTINGS LAYOUT --- */
/* 🔧 REMOVED: Old 2-column grid layout for settings.
   settings.css now owns .settings-layout as flex-column.
   Keeping this block as a comment to prevent re-introduction. */

.settings-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
    border-right: 1px solid var(--color-border);
    padding-right: var(--app-gap-strict);
}

.set-tab {
    background: none;
    border: none;
    text-align: left;
    padding: 10px 12px;
    color: var(--color-text-muted);
    border-radius: var(--radius-control);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
}
.set-tab:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-main);
}
.set-tab.active {
    background-color: var(--color-bg-hover);
    color: var(--color-text-main);
    font-weight: 600;
}

.set-panel { display: none; }
.set-panel.active-panel { display: block; }

/* --- PLACEHOLDERS --- */
.empty-state-panel {
    border: 1px dashed var(--color-border);
    background-color: rgba(30, 41, 59, 0.5);
    border-radius: var(--radius-ui);
    padding: 40px;
    text-align: center;
    color: var(--color-text-muted);
}
.empty-icon {
    font-size: 3rem;
    margin-bottom: 16px;
    color: var(--color-border);
}
.text-muted { color: var(--color-text-muted); }

/* ====================================================================
   MOBILE RESPONSIVE LAYOUT
   ==================================================================== */

@media (max-width: 768px) {
    /* Change shell to overlay mode */
    #app-shell {
        grid-template-columns: 1fr;
    }
    
    /* Sidebar becomes overlay */
    #app-sidebar {
        position: fixed;
        left: -280px; /* Hidden by default */
        top: 0;
        width: 260px;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    /* Show sidebar when expanded */
    #app-shell.shell-expanded #app-sidebar {
        left: 0;
    }
    
    /* Add overlay backdrop */
    body.menu-open::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
        animation: fadeIn 0.3s ease-out;
    }
    
    /* Viewport takes full width */
    #app-viewport {
        padding: 12px;
    }
    
    /* Adjust header for mobile */
    .view-header {
        margin-bottom: 12px;
    }
    
    .view-title {
        font-size: 1.25rem;
    }
}

/* ====================================================================
   SIDEBAR ACCORDION — sub-groups, sub-items, chevrons
   ==================================================================== */

/* Sub-group: collapsed by default, GPU-accelerated grid-row animation */
.nav-sub-group {
    display: grid;
    grid-template-rows: 0fr;
    overflow: hidden;
    transition: grid-template-rows var(--transition-slow), padding var(--transition-slow);
    padding: 0;
}
.nav-sub-group > .nav-sub-inner {
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

/* Expand when parent has nav-open — full-width sub-items (no inset pill) */
.nav-item.nav-open + .nav-sub-group {
    grid-template-rows: 1fr;
    padding: 0;
}

/* Sub-items — same active marker as .nav-item.active */
.nav-sub-item {
    background: none;
    border: none;
    border-left: none;
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px 10px 28px;
    color: var(--color-text-muted);
    cursor: pointer;
    border-radius: 0;
    font-size: 0.88rem;
    text-align: left;
    transition: background-color 0.2s, color 0.2s;
    white-space: nowrap;
}

.nav-sub-item:hover {
    background-color: var(--color-bg-hover);
    color: var(--color-text-main);
}

.nav-sub-item.nav-sub-active {
    background-color: var(--color-active-dim);
    color: var(--color-active);
    border-right: 3px solid var(--color-active);
    border-left: none;
    font-weight: 600;
    filter: var(--glow-active);
    border-radius: 0;
}

.nav-sub-item i {
    font-size: 1rem;
    min-width: 16px;
    flex-shrink: 0;
}

/* Chevron rotate on open */
.nav-chevron {
    margin-left: auto;
    font-size: 0.85rem;
    transition: transform 0.25s ease;
    flex-shrink: 0;
}

.nav-item.nav-open .nav-chevron {
    transform: rotate(-180deg);
}

/* Collapsed: sub-groups hidden by default */
#app-shell.shell-collapsed .nav-sub-group {
    grid-template-rows: 0fr !important;
    padding: 0 !important;
}

/* Collapsed + open: reveal icon-only sub-items */
#app-shell.shell-collapsed .nav-item.nav-open + .nav-sub-group {
    grid-template-rows: 1fr !important;
    padding: 4px 0 !important;
}

/* Collapsed sub-items: center the icon, strip text */
#app-shell.shell-collapsed .nav-sub-item {
    justify-content: center;
    padding: 10px 0;
    gap: 0;
}

#app-shell.shell-collapsed .nav-sub-item.nav-sub-active {
    border-right: 3px solid var(--color-active);
    border-left: none;
    border-radius: 0;
}

#app-shell.shell-collapsed .nav-sub-item span {
    display: none;
}

/* Hide chevron when collapsed — icon alone signals the group */
#app-shell.shell-collapsed .nav-chevron {
    display: none;
}

/* ====================================================================
   CONTEXTUAL SETTINGS SIDEBAR — swap mechanism
   ==================================================================== */

/* #sidebar-main fills the sidebar as a flex column */
#sidebar-main {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* #sidebar-settings hidden by default */
#sidebar-settings {
    display: none;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
}

/* When settings context is active: swap panels */
.settings-context-active #sidebar-main {
    display: none !important;
}

.settings-context-active #sidebar-settings {
    display: flex !important;
}

/* Settings sidebar header */
#sidebar-settings .sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    height: var(--header-h);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
    gap: 8px;
}

#sidebar-settings .sidebar-head h3 {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--color-text-main);
    white-space: nowrap;
    overflow: hidden;
    flex: 1;
}

/* Collapse toggle button */
.settings-collapse-btn {
    background: none;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius-control);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: color 0.2s, background 0.2s;
    font-size: 1.1rem;
}
.settings-collapse-btn:hover {
    color: var(--color-text-main);
    background: var(--color-bg-hover);
}

/* Collapsed state — settings sidebar */
#app-shell.shell-collapsed #sidebar-settings .sidebar-head {
    justify-content: center;
    padding: 0 8px;
}
#app-shell.shell-collapsed #sidebar-settings .settings-sidebar-title {
    display: none;
}
#app-shell.shell-collapsed #sidebar-settings .settings-collapse-btn {
    margin: 0 auto;
}
#app-shell.shell-collapsed #sidebar-settings .nav-item {
    justify-content: center;
    padding: 10px 0;
}
#app-shell.shell-collapsed #sidebar-settings .nav-item .nav-label,
#app-shell.shell-collapsed #sidebar-settings .nav-item .nav-chevron {
    display: none;
}
#app-shell.shell-collapsed #sidebar-settings .nav-item .nav-icon {
    margin: 0;
}
#app-shell.shell-collapsed #sidebar-settings .nav-item:not(.nav-open) + .nav-sub-group {
    max-height: 0 !important;
    overflow: hidden !important;
    padding: 0 !important;
}
/* Collapsed + open: reveal icon-only sub-items in settings sidebar */
#app-shell.shell-collapsed #sidebar-settings .nav-item.nav-open + .nav-sub-group {
    max-height: 600px !important;
    overflow: auto !important;
    padding: 4px 0 !important;
}
#app-shell.shell-collapsed #sidebar-settings .btn-back-to-app span {
    display: none;
}
#app-shell.shell-collapsed #sidebar-settings .btn-back-to-app {
    justify-content: center;
    padding: 14px 0;
}

/* Settings sidebar scrollable nav area */
#sidebar-settings .sidebar-menu {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Back to App button */
.btn-back-to-app {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 14px 16px;
    background: none;
    border: none;
    border-top: 1px solid var(--color-border);
    color: var(--color-text-muted);
    cursor: pointer;
    font-size: 0.88rem;
    text-decoration: none;
    transition: all 0.2s;
    box-sizing: border-box;
}

.btn-back-to-app:hover {
    color: var(--color-text-main);
    background-color: var(--color-bg-hover);
}

.btn-back-to-app i {
    font-size: 1rem;
    flex-shrink: 0;
}

/* ====================================================================
   SETTINGS SIDEBAR — TENANT SCOPE GATING  (CSS layer)
   These rules are always active regardless of JS timing.
   filterSettingsSidebar() in shell-logic.js adds inline styles on top
   for runtime tab-switching, but this block is the authoritative gate.

   Attributes stamped by context-manager._applyTheme():
     body[data-tenant]          — exact tenant ID    (e.g. "combid_metals")
     body[data-workspace-type]  — workspaceType      (e.g. "manufacturing")
   ==================================================================== */

/* ── Catalog sub-items: manufacturing type only ───────────────────── */
/* Trims & Profiles, Raw Materials, Roof Panels, Colors, Accessories, Suppliers */
body:not([data-workspace-type="manufacturing"]) #sidebar-settings .nav-sub-item[data-tenant-guard="manufacturing"] {
    display: none !important;
}

/* ── Catalog sub-items: service type only ────────────────────────── */
/* Materials, Services, Math Rules, Carport Rules */
body:not([data-workspace-type="service"]) #sidebar-settings .nav-sub-item[data-tenant-guard="service"] {
    display: none !important;
}

/* ── Agency HQ: hide platform-irrelevant items ───────────────────── */
/* Company Modules, Estimator Defaults, Pipeline Rules, Production Rules */
body[data-workspace-type="agency"] #sidebar-settings [data-sog-hidden="true"] {
    display: none !important;
}

/* ── Team & Permissions: hide for non-SOG users without manage_users ─ */
/* JS (filterSettingsSidebar) handles this — RBAC state isn't a CSS concern */

/* ====================================================================
   SIDEBAR TOOLTIP (collapsed mode)
   ==================================================================== */

#sidebar-nav-tooltip {
    position: fixed;
    background: var(--color-bg-elevated, #1e293b);
    color: var(--color-text-main, #f1f5f9);
    padding: 5px 10px;
    border-radius: var(--radius-control);
    font-size: 0.8rem;
    font-weight: 500;
    white-space: nowrap;
    z-index: 99999;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    border: 1px solid var(--color-border, rgba(255,255,255,0.08));
    transform: translateY(-50%);
    opacity: 0;
    transition: opacity 0.12s ease;
}

#sidebar-nav-tooltip.visible {
    opacity: 1;
}

/* ====================================================================
   PRINT STYLES
   ==================================================================== */

@media print {
    @page { margin: 0; }

    /* Hide navigation and controls */
    #app-sidebar,
    .view-header .btn,
    .view-header .actions,
    .sidebar-head,
    .sidebar-menu,
    .widget-controls,
    .drag-handle,
    .btn-icon,
    button {
        display: none !important;
    }
    
    /* Full width layout */
    #app-shell {
        grid-template-columns: 1fr !important;
        height: auto !important;
    }
    
    #app-viewport,
    .view-content {
        padding: 0 !important;
        overflow: visible !important;
        height: auto !important;
    }
    
    /* Ensure content flows properly */
    .page-wrapper {
        overflow: visible !important;
        height: auto !important;
    }
    
    /* Preserve colors for important elements */
    * {
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
    }
}
