* { box-sizing: border-box; margin: 0; padding: 0; }

/* FIX: Use dvh (dynamic viewport height) to prevent iOS Safari collapse */
body, html { height: 100dvh; overflow: hidden; }

/* Scrollbar RC2 Style */
.custom-scrollbar::-webkit-scrollbar { width: 4px; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #1e293b; border-radius: 10px; }

.app-container {
    display: grid;
    grid-template-columns: 260px 1fr 300px;
    height: 100dvh; 
    position: relative;
}

/* Panel Layouts */
.left-panel, .right-panel {
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
    z-index: 20;
}

/* Middle Panel */
.middle-panel {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%; 
    padding: 16px;
    overflow: hidden; 
}

/* RC2 Micro-Typography Headers */
.panel-header-rc2 {
    font-size: 1.25rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: #fff;
    margin-bottom: 24px;
}

h2, h3 {
    font-size: 0.65rem;
    font-weight: 900;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.15em;
    margin-bottom: 12px;
}

/* Left Panel Components */
.nav-buttons { display: flex; flex-direction: column; gap: 8px; margin-bottom: 30px; }
.nav-buttons button {
    display: flex;
    align-items: center;
    gap: 12px;
    background: transparent;
    border: 1px solid transparent;
}

.nav-buttons button:hover {
    background: rgba(139, 92, 246, 0.1);
    border-color: var(--glass-border);
    color: #fff;
    box-shadow: inset 4px 0 0 var(--accent-purple); 
}

/* Terminal Container */
.terminal-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    border-radius: 16px;
    overflow: hidden; 
}

.terminal-output {
    flex-grow: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

/* Overlays & Modals */
.terminal-overlay {
    position: absolute;
    top: 16px; left: 16px; right: 16px; bottom: 16px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    border-radius: 16px;
    padding: 20px;
}
.terminal-overlay.hidden { display: none !important; }

.overlay-header { 
    display: flex; 
    justify-content: space-between; 
    align-items: center;
    margin-bottom: 20px; 
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
}

.modal-close {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 8px;
}
.modal-close:hover { color: var(--accent-rose); background: transparent; border-color: transparent; }

#overlay-content { flex-grow: 1; overflow-y: auto; }

/* Right Panel Components */
.crud-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.crud-btn { width: 100%; padding: 10px; cursor: pointer; text-align: left; }

.input-area {
   display: flex;
   align-items: center;
   width: 100%; 
   padding: 10px 0; 
}

.terminal-input { 
    flex-grow: 1; 
    width: 100%;
    background: transparent;
    color: var(--terminal-text);
    border: none;
    font-family: inherit;
    outline: none;
}

/* =========================================
   MOBILE RESPONSIVE OVERRIDE (< 810px)
   ========================================= */
@media (max-width: 810px) {
    /* 1. Stack the grid vertically */
    .app-container {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto 1fr;
        overflow-y: auto;
        overflow-x: hidden;
    }

    /* 2. Hide terminal completely */
    .middle-panel {
        display: none !important; 
    }

    /* 3. Reflow the Left Panel */
    .left-panel {
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        padding-bottom: 10px;
        height: auto;
    }

	/* Turn vertical nav into a clean, non-obscured vertical stack on mobile */
    .nav-buttons {
        flex-direction: column !important;
        overflow-x: visible !important;
        margin-bottom: 15px;
        padding-bottom: 5px;
        width: 100%;
    }
    .nav-buttons button {
        width: 100% !important;
        white-space: normal !important;
        justify-content: flex-start !important;
    }
    .nav-buttons::-webkit-scrollbar { display: none; }
    .nav-buttons button { white-space: nowrap; flex-shrink: 0; }

    /* 4. Reflow the Right Panel (Telemetry) */
    .right-panel {
        border-left: none;
        padding-top: 10px;
        height: auto;
    }

    /* 5. Elevate Overlays to Fullscreen */
    .terminal-overlay, #sim-display-overlay, #telemetry-overlay {
        position: fixed;
        inset: 0;
        border-radius: 0;
        z-index: 9999;
        background-color: var(--bg-panel-solid); 
    }
    
    /* 6. Utility class to handle JS state-driven mobile wrapping */
    .mobile-mode-flex-wrap {
        flex-wrap: wrap !important;
    }
    .mobile-mode-full-width {
        width: 100% !important;
        min-width: 100% !important;
        margin-bottom: 8px !important;
    }
	
	/* --- BUG FIX PATCH --- */
	    
    /* Fix Header Overlap */
    .left-panel { padding: 15px !important; }
    .panel-header-rc2 { margin-bottom: 12px !important; }
    .nav-buttons { 
        align-items: center; 
        gap: 10px !important;
        margin-bottom: 5px !important; 
    }

    /* Override inline JS flex styles for the What-If & Charts mobile overlays */
    #overlay-content .wi-workspace div[style*="display: flex"] {
        flex-wrap: wrap !important;
    }
    
    #overlay-content input, 
    #overlay-content select {
        width: 100% !important;
        flex: 1 1 100% !important;
        margin-bottom: 10px !important;
    }

    /* Protect small functional buttons from stretching */
    #overlay-content button.del-holding,
    #overlay-content button.del-scenario,
    #overlay-content button.del-rule {
        width: auto !important;
        flex: 0 0 auto !important;
    }	
	
	/* =========================================
       MODULE GRIDS: Analytics, Adv Sim, Replication
       ========================================= */
       
    /* Force side-by-side split panels and form columns to stack vertically */
    #overlay-content .wi-workspace div[style*="display: flex"] > div[style*="flex: 1"],
    #overlay-content .wi-workspace div[style*="display: flex"] > div[style*="flex: 2"],
    #overlay-content .wi-workspace div[style*="display: flex"][style*="gap"] > div.wi-section,
    #overlay-content .wi-workspace form > div[style*="display: flex"][style*="gap"] > div,
    #overlay-content .wi-workspace form[style*="display: flex"] > div {
        flex: 1 1 100% !important;
        min-width: 100% !important;
        width: 100% !important;
    }

    /* Stack the Top Header cleanly (Title on top, Tabs stretching below) */
    #overlay-content .wi-workspace > div[style*="justify-content: space-between"] {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 15px !important;
        height: auto !important;
    }
    #overlay-content .wi-workspace > div[style*="justify-content: space-between"] > div {
        min-width: 100% !important;
        width: 100% !important;
        display: flex;
        flex-wrap: nowrap !important;
    }
    
    /* Make the tab buttons evenly fill the mobile width */
    #overlay-content .wi-workspace .wi-tab,
    #overlay-content .wi-workspace .rep-tab {
        flex: 1 !important; 
        text-align: center;
        padding: 10px 5px !important;
        font-size: 0.85em;
    }
    
    /* Stop small inline inputs (like the 'Weight %' box) from going full width */
    #overlay-content .wi-workspace input[type="number"] {
        flex: 0 1 auto !important;
        width: auto !important;
        min-width: 80px !important;
    }

    /* =========================================
       TELEMETRY CLEANUPS
       ========================================= */
       
    /* Hide the server farm graphic */
    #idle-cluster-canvas {
        display: none !important; 
    }
    
    /* Shrink the EKG vertically */
    #idle-ekg-canvas {
        height: 60px !important;  
        max-height: 60px !important;
        margin-top: 10px;
    }
	
	/* BUG 1: Asset Studio categories too wide */
    #ac-body .wi-section {
        overflow-x: auto !important;
    }
    #ac-body table {
        min-width: 650px !important; /* Forces scrolling instead of squishing text */
    }

    /* BUG 2: Analytics alignment and wrapping */
    #analytics-body form > div[style*="gap: 15px"] {
        flex-wrap: wrap !important;
    }
    #analytics-body form > div[style*="gap: 15px"] > div,
    #analytics-body form > div[style*="gap: 15px"] > button {
        flex: 1 1 100% !important; /* Forces inputs and "View Dimensions" to stack vertically */
    }
    /* Fix random indentation on data source checkboxes */
    #clf-datasets {
        grid-template-columns: 1fr !important;
        height: 200px !important; /* Give it more room on mobile */
    }

    /* BUG 3: Replication Radio Buttons vertical alignment */
    input[name="rep-goal"] {
        margin-top: 2px; /* Fixes radio circle vertical drift */
    }
    #rep-config-tab div[style*="gap: 20px"] {
        flex-direction: column !important;
        gap: 10px !important;
    }

    /* BUG 5: Hide Cluster Utilization Pane */
    #node-status-panel, .node-status-container, #cluster-telemetry-canvas-wrapper {
        display: none !important; /* Update this selector to match your specific pane's ID/class */
    }	
}