/*
 * EnigmAPI Layout System - MODERN CSS GRID APPROACH
 *
 * Based on 2025 best practices:
 * - CSS Grid for layout structure (NOT fixed positioning + margins)
 * - Position sticky for sidebar (NOT fixed)
 * - Responsive-first design
 * - Performance optimized
 *
 * Layer Structure:
 * 1. RootLayout - Global app wrapper
 * 2. AppLayout - Authenticated app shell (navbar + sidebar) - USES CSS GRID
 * 3. PageLayout - Page-specific layouts
 * 4. SectionLayout - Reusable content sections
 *
 * References:
 * - https://akashhamirwasia.com/blog/how-to-and-not-to-build-sidebar-layouts/
 * - Modern dashboard layout patterns 2025
 */

/* ================================
   1. ROOT LAYOUT
   Global app wrapper with theme support
   ================================ */
.root-layout {
    min-height: 100vh;
    position: relative;
    isolation: isolate;
}

/* Dark mode support */
.root-layout[data-theme="dark"] {
    --bg-primary: #0a0e27;
    --bg-secondary: rgba(15, 22, 41, 0.4);
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
}

.root-layout[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
}

/* Skip link for accessibility */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--accent);
    color: white;
    padding: 8px;
    text-decoration: none;
    z-index: 100;
}

.skip-link:focus {
    top: 0;
}

/* ================================
   2. APP LAYOUT - MODERN CSS GRID APPROACH
   Main authenticated app shell
   ================================ */

.app-layout {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Fixed navbar at top */
.app-layout__header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    height: 70px;
    background: rgba(10, 14, 39, 0.98);
    backdrop-filter: blur(30px) saturate(180%);
    border-bottom: 1px solid rgba(99, 102, 241, 0.1);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

/*
 * MODERN APPROACH: CSS Grid Container
 * This is the KEY to proper layout - Grid handles positioning, NOT margins
 */
.app-layout__body {
    display: grid;
    grid-template-columns: 260px 1fr;  /* Sidebar: 260px, Main content: remaining space */
    padding-top: 70px;  /* Account for fixed navbar */
    min-height: calc(100vh - 70px);
    position: relative;
}

/*
 * MODERN APPROACH: Position Sticky Sidebar
 * NOT position: fixed with margins (that's the old, brittle way)
 */
.app-layout__sidebar {
    position: sticky;  /* ✅ MODERN: Sticky within grid, NOT fixed */
    top: 70px;  /* Stick below navbar */
    height: calc(100vh - 70px);  /* Full viewport height minus navbar */
    overflow-y: auto;  /* Scroll if content exceeds height */
    overflow-x: hidden;

    /* Visual styling */
    background: rgba(10, 14, 39, 0.95);
    backdrop-filter: blur(30px) saturate(180%);
    border-right: 1px solid rgba(99, 102, 241, 0.08);
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.1);

    /* Spacing */
    padding: 2rem 0;

    /* Z-index */
    z-index: 999;

    /* Grid positioning - let grid handle it */
    grid-column: 1;  /* First column */
}

/*
 * MODERN APPROACH: Main Content
 * NO margin-left needed! Grid handles positioning automatically
 */
.app-layout__main {
    /* Grid positioning - let grid handle it */
    grid-column: 2;  /* Second column */

    /* Content sizing - full width of grid column */
    width: 100%;
    min-width: 0;  /* Prevent grid blowout */

    /* Visual */
    background: var(--bg-primary);

    /* No margin-left needed - grid handles it! */
    padding: 0;

    /* Make sure content flows naturally */
    overflow-x: hidden;
}

/* Optional footer */
.app-layout__footer {
    grid-column: 2;  /* Align with main content */
    padding: 2rem;
    border-top: 1px solid var(--border);
    background: var(--bg-primary);
}

/* ================================
   RESPONSIVE BEHAVIOR
   Mobile-first approach
   ================================ */

/* Tablet and below: Hide sidebar by default, show with toggle */
@media (max-width: 1024px) {
    .app-layout__body {
        /* Single column on mobile - main content takes full width */
        grid-template-columns: 1fr;
    }

    .app-layout__sidebar {
        /* Sidebar becomes overlay */
        position: fixed;
        top: 70px;
        left: 0;
        bottom: 0;
        width: 260px;
        transform: translateX(-100%);  /* Hidden by default */
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1001;  /* Above everything on mobile */
    }

    .app-layout__sidebar.is-open {
        transform: translateX(0);  /* Slide in when open */
    }

    .app-layout__main,
    .app-layout__footer {
        /* No grid column needed - full width */
        grid-column: 1;
    }
}

/* Mobile: Adjust sidebar for smaller screens */
@media (max-width: 768px) {
    .app-layout__sidebar {
        width: 240px;  /* Slightly narrower on mobile */
    }
}

/* ================================
   NAVBAR COMPONENTS
   ================================ */

.navbar {
    height: 100%;
    display: flex;
    align-items: center;
}

.nav-container {
    width: 100%;
    max-width: 100%;
    padding: 0 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
}

.nav-left,
.nav-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.nav-brand {
    font-size: 26px;
    font-weight: 900;
    background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -0.5px;
    text-decoration: none;
}

/* ================================
   SIDEBAR COMPONENTS
   ================================ */

.sidebar-section {
    margin-bottom: 2.5rem;
    padding: 0 1.25rem;
}

.sidebar-title {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: rgba(148, 163, 184, 0.5);
    margin-bottom: 1rem;
    padding: 0 0.75rem;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.875rem;
    padding: 0.875rem 0.75rem;
    margin-bottom: 0.375rem;
    border-radius: 12px;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 15px;
    font-weight: 500;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
}

.sidebar-link:hover {
    background: rgba(99, 102, 241, 0.08);
    color: var(--text-primary);
    transform: translateX(4px);
}

.sidebar-link.active {
    background: rgba(99, 102, 241, 0.15);
    color: var(--accent-light);
    font-weight: 600;
}

.sidebar-link.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 20px;
    background: linear-gradient(180deg, #6366f1 0%, #a855f7 100%);
    border-radius: 0 3px 3px 0;
}

.sidebar-icon {
    font-size: 20px;
    width: 24px;
    text-align: center;
}

/* ================================
   3. PAGE LAYOUT
   Page-specific layout patterns
   ================================ */

.page-layout {
    padding: 2.5rem;
    width: 100%;
    max-width: 100%;  /* Use full width available in grid */
    animation: fadeIn 0.5s ease-out;
}

.page-layout--centered {
    max-width: 1400px;
    margin: 0 auto;
}

.page-layout--wide {
    max-width: none;
    padding: 2rem;
}

.page-layout--narrow {
    max-width: 960px;
    margin: 0 auto;
}

/* Page header */
.page-layout__header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2.5rem;
    animation: fadeInUp 0.6s ease-out;
    flex-wrap: wrap;
    gap: 1.5rem;
}

.page-layout__title-section {
    flex: 1;
    min-width: 300px;
}

.page-title {
    font-size: 36px;
    font-weight: 900;
    color: var(--text-primary);
    margin-bottom: var(--space-3);
    letter-spacing: var(--tracking-tighter);
    line-height: var(--line-height-tight);
}

.page-subtitle {
    font-size: 16px;
    color: var(--text-secondary);
    font-weight: 400;
    line-height: var(--line-height-relaxed);
    letter-spacing: var(--tracking-normal);
}

.page-layout__actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
}

/* Page body */
.page-layout__body {
    animation: fadeInUp 0.8s ease-out;
}

/* Responsive page layout */
@media (max-width: 768px) {
    .page-layout {
        padding: 1.5rem;
    }

    .page-layout__header {
        flex-direction: column;
        gap: 1.5rem;
    }

    .page-layout__title-section {
        min-width: 100%;
    }

    .page-layout__actions {
        width: 100%;
    }

    .page-title {
        font-size: 28px;
    }
}

/* ================================
   4. SECTION LAYOUT
   Reusable content sections
   ================================ */

/* Grid Section */
.section-grid {
    display: grid;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.section-grid--2col {
    grid-template-columns: repeat(2, 1fr);
}

.section-grid--3col {
    grid-template-columns: repeat(3, 1fr);
}

.section-grid--4col {
    grid-template-columns: repeat(4, 1fr);
}

.section-grid--auto {
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.section-grid--wide {
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

/* Stack Section */
.section-stack {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.section-stack--tight {
    gap: 1rem;
}

.section-stack--loose {
    gap: 2.5rem;
}

/* Card Container Section */
.section-card {
    background: var(--bg-card, rgba(15, 22, 41, 0.6));
    backdrop-filter: blur(20px) saturate(180%);
    border: 1px solid var(--border, rgba(148, 163, 184, 0.08));
    border-radius: var(--radius-lg, 16px);
    padding: 2rem;
    margin-bottom: 2rem;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.section-card--compact {
    padding: 1.5rem;
}

.section-card--spacious {
    padding: 3rem;
}

.section-card--no-padding {
    padding: 0;
}

/* Section with header */
.section-with-header {
    margin-bottom: 2rem;
}

.section-with-header__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 1.5rem;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid var(--border, rgba(148, 163, 184, 0.08));
}

.section-with-header__title {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
}

.section-with-header__actions {
    display: flex;
    gap: 0.75rem;
}

.section-with-header__body {
    /* Body content */
}

/* Split Section (Sidebar + Content) */
.section-split {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.section-split--narrow {
    grid-template-columns: 200px 1fr;
}

.section-split--wide {
    grid-template-columns: 360px 1fr;
}

.section-split__sidebar {
    /* Sidebar content */
}

.section-split__content {
    /* Main content */
}

/* Responsive sections */
@media (max-width: 1024px) {
    .section-grid--4col {
        grid-template-columns: repeat(2, 1fr);
    }

    .section-split {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .section-grid--2col,
    .section-grid--3col,
    .section-grid--4col {
        grid-template-columns: 1fr;
    }

    .section-card {
        padding: 1.5rem;
    }
}

/* ================================
   UTILITY LAYOUT CLASSES
   ================================ */

/* Flexbox utilities */
.flex {
    display: flex;
}

.flex-col {
    flex-direction: column;
}

.flex-row {
    flex-direction: row;
}

.flex-wrap {
    flex-wrap: wrap;
}

.flex-center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.flex-between {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.flex-start {
    display: flex;
    align-items: flex-start;
}

.flex-end {
    display: flex;
    align-items: flex-end;
}

/* Gap utilities */
.gap-0 { gap: 0; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }
.gap-3 { gap: 1.5rem; }
.gap-4 { gap: 2rem; }
.gap-5 { gap: 2.5rem; }

/* Container utilities */
.container {
    width: 100%;
    max-width: 1600px;
    margin: 0 auto;
    padding: 0 2rem;
}

.container--narrow {
    max-width: 960px;
}

.container--wide {
    max-width: 1920px;
}

/* Spacing utilities */
.p-0 { padding: 0; }
.p-1 { padding: 0.5rem; }
.p-2 { padding: 1rem; }
.p-3 { padding: 1.5rem; }
.p-4 { padding: 2rem; }
.p-5 { padding: 2.5rem; }

.m-0 { margin: 0; }
.m-1 { margin: 0.5rem; }
.m-2 { margin: 1rem; }
.m-3 { margin: 1.5rem; }
.m-4 { margin: 2rem; }
.m-5 { margin: 2.5rem; }

/* ================================
   ANIMATIONS
   ================================ */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ================================
   SCROLLBAR STYLING
   ================================ */
.app-layout__sidebar::-webkit-scrollbar {
    width: 6px;
}

.app-layout__sidebar::-webkit-scrollbar-track {
    background: rgba(10, 14, 39, 0.4);
}

.app-layout__sidebar::-webkit-scrollbar-thumb {
    background: rgba(99, 102, 241, 0.3);
    border-radius: 3px;
}

.app-layout__sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(99, 102, 241, 0.5);
}

/* ================================
   PERFORMANCE OPTIMIZATIONS
   ================================ */

/* GPU acceleration for animations */
.sidebar-link,
.app-layout__sidebar {
    will-change: transform;
}

/* Contain layout reflows */
.app-layout__main,
.app-layout__sidebar {
    contain: layout style paint;
}
