/* --- CSS VARIABLES --- */
:root {
    /* Light Theme Defaults */
    --bg-body: #f3f2f1;
    --bg-surface: #ffffff;
    --bg-nav: #ffffff;
    --text-primary: #201f1e;
    --text-secondary: #605e5c;
    --brand-color: #0078d4; 
    --brand-hover: #106ebe;
    --border-color: #e1dfdd;
    /* Shadows for Depth */
    --shadow-header: 0 2px 4px rgba(0,0,0,0.1);
    --shadow-nav: 2px 0 6px rgba(0,0,0,0.1);
    --shadow-card: 0 1.6px 3.6px 0 rgba(0,0,0,0.132), 0 0.3px 0.9px 0 rgba(0,0,0,0.108);
    
    --nav-width: 250px;
    --nav-collapsed-width: 50px; 
    --sidebar-actual-width: 0px;
    --header-height: 50px;
    --safe-top: env(safe-area-inset-top, 0px);
    --transition-speed: 0.3s;
}

.app-container:has(.sidebar) {
    --sidebar-actual-width: 250px;
}

.sidebar.collapsed ~ .main-content,
.app-container:has(.sidebar.collapsed) {
    --sidebar-actual-width: 50px;
}

@media (max-width: 768px) {
    :root, .app-container, .app-container:has(.sidebar) {
        --sidebar-actual-width: 0px;
    }
}

/* Dark Theme Overrides */
body.dark-mode {
    --bg-body: #111111;
    --bg-surface: #202020;
    --bg-nav: #2b2b2b;
    --text-primary: #ffffff;
    --text-secondary: #d0d0d0;
    --brand-color: #4f9ef3;
    --border-color: #444444;
    --shadow-header: 0 2px 8px rgba(0,0,0,0.5);
    --shadow-nav: 2px 0 8px rgba(0,0,0,0.5);
    --shadow-card: 0 4px 8px rgba(0,0,0,0.4);
}

/* --- GLOBAL RESET --- */
* { box-sizing: border-box; }
html, body {
    overscroll-behavior-y: contain; /* prevent page bounce/refresh */
}

body {
    font-family: 'Segoe UI', sans-serif;
    background-color: var(--bg-body);
    color: var(--text-primary);
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;    
}

/* --- HEADER (TOP BAR) --- */
.top-bar {
    height: var(--header-height);
    background-color: var(--bg-nav);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: flex-end; /* push content to bottom of the bar, above safe area */
    justify-content: space-between;
    padding: 0 16px;
    padding-top: env(safe-area-inset-top, 0px);
    width: 100%;
    position: fixed;
    top: 0px;
    left: 0px;
    z-index: 201;
    box-shadow: var(--shadow-header);
    transition: background 0.3s, border 0.3s, box-shadow 0.3s;
    /* Expand height to include the safe area (Dynamic Island / notch) */
    min-height: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    height: auto;
}

/* Align inner items with proper bottom padding so they don't hug the edge */
.top-bar > * {
    padding-bottom: 7px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 20px;
    min-width: 200px;
}

.site-title {
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--brand-color);
    letter-spacing: -0.5px;
}

/* Smart Page Title (Hidden by default) */
.header-page-title {
    font-weight: 600;
    font-size: 1rem;
    color: var(--text-primary);
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    margin-left: 20px;
    border-left: 1px solid var(--border-color);
    padding-left: 20px;
}

.header-page-title.visible {
    opacity: 1;
    transform: translateY(0);
}

/* --- LAYOUT GRID --- */
.app-container {
    display: flex;
    min-height: 100vh;
    /* Accounts for fixed top-bar + iOS safe area (Dynamic Island / notch) */
    padding-top: calc(var(--header-height) + env(safe-area-inset-top, 0px));
    width: 100%;
    box-sizing: border-box;
}

/* --- SIDEBAR NAVIGATION --- */
.sidebar {
    position: sticky;
    top: var(--header-height);
    height: calc(100vh - var(--header-height));
    width: var(--nav-width);
    flex-shrink: 0;
    background-color: var(--bg-nav);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-nav); 
    z-index: 150;
    overflow-x: hidden;
    overflow-y: auto;
    
    /* Hover Tunnel Logic: Linger open for 0.5s before closing */
    transition: width var(--transition-speed) ease 0.5s; 
}

/* Temporarily disables hover delays for instant click reactions */
.sidebar.no-delay {
    transition-delay: 0s !important;
}

@media (min-width: 769px) {
    .sidebar {
        position: sticky;
        top: 50px;
        left: 0px;
        height: calc(100dvh - 50px);
    } 
}
/* DESKTOP COLLAPSED LOGIC */
.sidebar.collapsed {
    width: var(--nav-collapsed-width);
}
/* Hover Tunnel Logic: Wait 1s before opening (ignore hover if over toggler button) */
.sidebar.collapsed:hover:not(:has(.sidebar-toggler-wrapper:hover)) {
    width: var(--nav-width);
    transition: width var(--transition-speed) ease 1s;
}
.sidebar.collapsed.hover-expanded {
    width: var(--nav-width);
}

.sidebar.collapsed:not(.hover-expanded) .nav-text, 
.sidebar.collapsed:not(.hover-expanded) .dropdown-arrow {
    
    pointer-events: none;
    
}

/* Navigation List */
.nav-list {
    list-style: none;
    padding: 16px 0;
    margin: 0;
    flex: 1;
}

.nav-link {
    display: flex;
    align-items: center;
    height: 44px; 
    padding: 0 14px;
    color: var(--text-primary);
    text-decoration: none;
    cursor: pointer;
    transition: background 0.2s;
    overflow: hidden;
    white-space: nowrap;
}

.nav-link:hover {
    background-color: rgba(128, 128, 128, 0.1);
}

/* --- NAV GROUP (Accordion) --- */
.nav-group-toggle {
    width: 100%;
    border: none;
    background: none;
    outline: none;
    text-align: left;
    font-family: inherit;
    font-size: inherit;
}

.nav-group-arrow {
    margin-left: auto;
    font-size: 10px;
    transition: transform 0.2s;
    opacity: 0.6;
}

.nav-group-open .nav-group-arrow {
    transform: rotate(90deg);
}

.nav-sub-list {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out;
    background: rgba(0, 0, 0, 0.03);
}

.nav-group-open .nav-sub-list {
    max-height: 600px; /* Expands when open - accommodates all navigation links */
    border-bottom: 1px solid var(--border-color);
}

.nav-sub-link {
    display: flex;
    align-items: center;
    height: 38px;
    padding: 0 14px 0 32px; /* Indent sub-links */
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.9rem;
    transition: background 0.2s, color 0.2s;
}

.nav-sub-link:hover {
    background: rgba(128, 128, 128, 0.1);
    color: var(--text-primary);
}

.nav-sub-link.active {
    color: var(--brand-color);
    font-weight: 600;
    background: rgba(0, 120, 212, 0.05);
}

.nav-sub-icon {
    font-size: 14px;
    width: 16px;
    margin-right: 12px;
    text-align: center;
}

.nav-icon {
    width: 20px;
    text-align: center;
    font-size: 18px;
    margin-right: 14px;
    flex-shrink: 0;
}

/* Collapse Button Wrapper */
.sidebar-toggler-wrapper {
    padding: 12px;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
}

.sidebar.collapsed:not(.hover-expanded) .sidebar-toggler-wrapper {
    justify-content:flex-start;
}

/* --- MAIN CONTENT --- */
.main-content {
    flex: 1;
    min-width: 0; /* Prevents overflow pushing */
    background-color: var(--bg-body);
    /* padding-top removed because app-container natively handles it */
}

/* --- USER MENU & MOBILE BTN --- */
.user-btn {
    background: none;
    border: none;
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 4px;
    color: var(--text-primary);
}
.user-btn:hover { background-color: rgba(128,128,128,0.1); }
.user-avatar { width: 32px; height: 32px; border-radius: 50%; }

.hamburger-btn {
    display: none;
    background: none; 
    border: none; 
    font-size: 20px; 
    color: var(--text-primary);
}

.dropdown-menu {
    position: absolute;
    top: 100%; right: 16px;
    width: 200px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-card);
    border-radius: 4px;
    display: none;
    flex-direction: column;
    padding: 8px 0;
    z-index: 3000;
}
.dropdown-menu.active { display: flex; }
.dropdown-item {
    padding: 10px 16px;
    color: var(--text-primary);
    text-decoration: none;
    display: flex; align-items: center; gap: 10px;
    cursor: pointer;
}
.dropdown-item:hover { background: rgba(128,128,128,0.1); }

.dropdown-divider {
    height: 1px;
    background: var(--border-color);
    margin: 4px 0;
}

.dropdown-item-user-name{
    display: none;
}
/* --- RESPONSIVE / MOBILE OVERRIDES --- */
@media (max-width: 768px) {
    .header-page-title {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        opacity: 1;
        border-left: 0px;
        margin-left: 0px;
        padding-left: 0px;
    }
    #pageTitleH1 {
        display: none;
    }
    .dropdown-item-user-name{
        text-align: center;
        padding: 5px;
        font-weight: 700;
        display: block;
    }
    .grid-container { 
        display: block; 
        /* padding-bottom: 140px; Provides enough ghost space to clear the iOS address bar and home indicator */

    }
    .user-name{
        display: none;
    }
    .sidebar {
        position: absolute;
        height: 100%;
        left: -100%; 
        width: 250px !important; 
    }
    .sidebar.mobile-open { 
        left: 0;
        position: fixed;
        top: calc(var(--header-height) + env(safe-area-inset-top, 0px));
        z-index: 200;
    }
    .sidebar.collapsed .nav-text, 
    .sidebar.collapsed .dropdown-arrow {
        display: block !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }
    .sidebar-toggler-wrapper { display: none !important; }
    .hamburger-btn { display: block; }
    .site-title { 
        padding-right: 5px;
        border-left: 1px solid var(--border-color);
        margin-left: -10px;
        padding-left: 20px;
    }
}


.app-container {
    flex: 1;
    min-height: 0;   /* IMPORTANT */
}

.main-content {
    flex: 1;
    min-height: 0;   /* VERY IMPORTANT */
}


.top-bar, .app-header{
    will-change: transform;
    backface-visibility: hidden;
}

/* --- MESSAGING UNREAD DIVIDER --- */
.unread-divider {
    display: flex;
    align-items: center;
    text-align: center;
    color: var(--brand-color);
    margin: 15px 0;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
}
.unread-divider-line {
    flex: 1;
    border-bottom: 1px solid var(--brand-color);
    margin: 0 10px;
    opacity: 0.5;
}

/* --- SHARED PAGE HEADER & STATS SYSTEM --- */
.page-header-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 24px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 16px;
    flex-wrap: wrap;
    gap: 16px;
}

.page-title {
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 6px 0;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 12px;
}

.page-subtitle {
    margin: 0;
    color: var(--text-secondary);
    font-size: 14px;
}

.top-stats-pill {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 6px 16px;
    font-size: 13px;
    color: var(--text-secondary);
    box-shadow: var(--shadow-card);
}

.stat-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.stat-divider {
    color: var(--border-color);
}

.text-brand {
    color: var(--brand-color) !important;
}

.info-callout {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    background: rgba(0, 120, 212, 0.08);
    border: 1px solid rgba(0, 120, 212, 0.25);
    border-radius: 8px;
    padding: 14px 18px;
    margin-bottom: 24px;
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
}

.info-callout-icon {
    font-size: 20px;
    color: var(--brand-color);
    margin-top: 2px;
}