/* Sidebar Navigation Styles */
/* assets/sidebar.css */

/* ============================================
   CSS Custom Properties for Sidebar
   ============================================ */
:root {
    --sidebar-width: 240px;
    --sidebar-collapsed-width: 64px;
    /* Use theme color variables for theming support */
    --sidebar-bg: rgb(var(--color-background));
    --sidebar-border: rgb(var(--color-surface-2));
    --sidebar-text: rgb(var(--color-text-secondary));
    --sidebar-text-muted: rgb(var(--color-text-secondary) / 0.7);
    --sidebar-hover-bg: rgb(var(--color-surface-1));
    --sidebar-active-bg: rgb(var(--color-primary) / 0.15);
    --sidebar-active-border: rgb(var(--color-primary));
    --sidebar-icon-size: 20px;
    --sidebar-transition: 200ms ease;
}

/* ============================================
   Body Layout Adjustments
   ============================================ */
body.has-sidebar {
    display: flex;
    min-height: 100vh;
}

body.has-sidebar .dash-container,
body.has-sidebar .page-container {
    flex: 1;
    /* Instead of margin-left, use padding to offset for sidebar */
    padding-left: var(--sidebar-width);
    transition: padding-left var(--sidebar-transition);
    box-sizing: border-box;
}

body.has-sidebar.sidebar-collapsed .dash-container,
body.has-sidebar.sidebar-collapsed .page-container {
    padding-left: var(--sidebar-collapsed-width);
}

/* ============================================
   Sidebar Container
   ============================================ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    /* Safari fix: use 100dvh for dynamic viewport height on mobile */
    height: 100dvh;
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    z-index: 40;
    transition: width var(--sidebar-transition), transform var(--sidebar-transition);
    overflow: hidden;
}

.sidebar--collapsed {
    width: var(--sidebar-collapsed-width);
}

.sidebar--collapsed .sidebar-menu-label,
.sidebar--collapsed .sidebar-logo-text {
    opacity: 0;
    visibility: hidden;
    width: 0;
}

.sidebar--collapsed .sidebar-section-header {
    opacity: 0;
    visibility: hidden;
    width: 0;
    height: 0;
    padding: 0;
    margin: 0;
}

/* ============================================
   Sidebar Header
   ============================================ */
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 12px;
    border-bottom: 1px solid var(--sidebar-border);
    flex-shrink: 0;
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    overflow: hidden;
    text-decoration: none;
    cursor: pointer;
}

.sidebar-logo:hover {
    opacity: 0.9;
}

.sidebar-logo-icon {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.sidebar-logo-text {
    font-weight: 600;
    font-size: 14px;
    color: white;
    white-space: nowrap;
    transition: opacity var(--sidebar-transition), width var(--sidebar-transition);
}

.sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--sidebar-text-muted);
    cursor: pointer;
    border-radius: 6px;
    transition: color 150ms, background 150ms;
    flex-shrink: 0;
}

.sidebar-toggle:hover {
    color: white;
    background: var(--sidebar-hover-bg);
}

.sidebar-toggle svg {
    width: 18px;
    height: 18px;
}

.sidebar--collapsed .sidebar-toggle {
    margin: 0 auto;
}

/* ============================================
   Sidebar Navigation
   ============================================ */
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 0;
    /* Safari iOS momentum scrolling */
    -webkit-overflow-scrolling: touch;
    /* Ensure touch events work on Safari */
    overscroll-behavior: contain;
    /* Allow vertical pan/scroll gestures */
    touch-action: pan-y;
}

.sidebar-section {
    padding: 0 8px;
    margin-bottom: 8px;
}

.sidebar--collapsed .sidebar-section {
    margin-bottom: 0;
}

.sidebar-section-header {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--sidebar-text-muted);
    padding: 8px 12px 6px;
    white-space: nowrap;
    transition: opacity var(--sidebar-transition), width var(--sidebar-transition);
}

/* Separator between sections - visible only when collapsed */
.sidebar-separator {
    height: 0;
    margin: 0;
    border: none;
    transition: height var(--sidebar-transition), margin var(--sidebar-transition);
}

.sidebar--collapsed .sidebar-separator {
    height: 1px;
    margin: 8px 12px;
    background: var(--sidebar-border);
}

/* ============================================
   Menu Items
   ============================================ */
.sidebar-menu-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 10px 12px;
    border: none;
    background: transparent;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
    transition: background 150ms, color 150ms;
    text-align: left;
    overflow: hidden;
}

.sidebar-menu-item:hover {
    background: var(--sidebar-hover-bg);
    color: white;
}

.sidebar-menu-item--active {
    background: var(--sidebar-active-bg);
    color: white;
    border-left: 3px solid var(--sidebar-active-border);
    padding-left: 9px;
}

.sidebar-menu-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--sidebar-icon-size);
    height: var(--sidebar-icon-size);
    flex-shrink: 0;
}

.sidebar-menu-icon svg {
    width: 100%;
    height: 100%;
}

.sidebar-menu-label {
    white-space: nowrap;
    transition: opacity var(--sidebar-transition), width var(--sidebar-transition);
}

/* Collapsed state: center icons */
.sidebar--collapsed .sidebar-menu-item {
    justify-content: center;
    padding: 10px;
}

.sidebar--collapsed .sidebar-menu-item--active {
    padding-left: 10px;
    border-left: none;
    border-bottom: 3px solid var(--sidebar-active-border);
}

/* ============================================
   Sidebar Footer
   ============================================ */
.sidebar-footer {
    padding: 8px;
    border-top: 1px solid var(--sidebar-border);
    flex-shrink: 0;
}

.sidebar-shortcut-hint {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 12px;
    margin-bottom: 4px;
    font-size: 11px;
    color: var(--sidebar-text-muted);
    opacity: 0.8;
    transition: opacity 0.2s ease;
    cursor: pointer;
}

.sidebar-shortcut-hint:hover {
    opacity: 1;
}

.sidebar-shortcut-text {
    margin-right: 4px;
}

.sidebar-kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 18px;
    padding: 0 5px;
    font-family: system-ui, -apple-system, sans-serif;
    font-size: 10px;
    font-weight: 500;
    color: var(--sidebar-text-muted);
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

/* Hide shortcut hint when sidebar is collapsed */
.sidebar--collapsed .sidebar-shortcut-hint {
    display: none;
}

/* ============================================
   Profile Button & Dropdown
   ============================================ */
.sidebar-profile-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    border: none;
    background: transparent;
    color: var(--sidebar-text);
    cursor: pointer;
    border-radius: 8px;
    transition: background 150ms;
    text-align: left;
}

.sidebar-profile-btn:hover {
    background: var(--sidebar-hover-bg);
}

.sidebar-profile-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    background: var(--sidebar-border);
}

.sidebar-profile-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.sidebar-profile-name {
    font-size: 13px;
    font-weight: 500;
    color: white;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-profile-email {
    font-size: 11px;
    color: var(--sidebar-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-profile-chevron {
    width: 16px;
    height: 16px;
    color: var(--sidebar-text-muted);
    flex-shrink: 0;
    transition: transform 150ms;
}

.sidebar-profile-btn[aria-expanded="true"] .sidebar-profile-chevron {
    transform: rotate(180deg);
}

/* Collapsed sidebar profile */
.sidebar--collapsed .sidebar-profile-info,
.sidebar--collapsed .sidebar-profile-chevron {
    display: none;
}

.sidebar--collapsed .sidebar-profile-btn {
    justify-content: center;
    padding: 10px;
}

/* Collapsed sidebar dropdown - needs to extend beyond sidebar */
.sidebar--collapsed .sidebar-footer {
    overflow: visible;
}

.sidebar--collapsed .sidebar-profile-dropdown {
    /* Use fixed positioning so dropdown floats over dashboard content */
    position: fixed;
    /* Position above the profile button at bottom of viewport */
    bottom: 70px;
    left: var(--sidebar-collapsed-width);
    right: auto;
    min-width: 220px;
    margin-bottom: 0;
}

/* Profile Dropdown */
.sidebar-profile-dropdown {
    position: absolute;
    bottom: 100%;
    left: 8px;
    right: 8px;
    margin-bottom: 8px;
    background: rgb(var(--color-surface-1));
    border: 1px solid var(--sidebar-border);
    border-radius: 8px;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: opacity 150ms, visibility 150ms, transform 150ms;
    z-index: 50;
}

.sidebar-profile-dropdown--open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.sidebar-profile-dropdown-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-profile-dropdown-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.sidebar-profile-dropdown-info {
    flex: 1;
    min-width: 0;
}

.sidebar-profile-dropdown-name {
    font-size: 14px;
    font-weight: 600;
    color: white;
    margin-bottom: 2px;
}

.sidebar-profile-dropdown-email {
    font-size: 12px;
    color: var(--sidebar-text-muted);
}

.sidebar-profile-dropdown-providers {
    display: flex;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-provider-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    font-size: 11px;
    color: var(--sidebar-text-muted);
}

.sidebar-provider-badge svg {
    width: 14px;
    height: 14px;
}

.sidebar-profile-dropdown-menu {
    padding: 6px;
}

.sidebar-profile-dropdown-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px 10px;
    border: none;
    background: transparent;
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 13px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 150ms, color 150ms;
}

.sidebar-profile-dropdown-item:hover {
    background: var(--sidebar-hover-bg);
    color: white;
}

.sidebar-profile-dropdown-item svg {
    width: 16px;
    height: 16px;
}

.sidebar-profile-dropdown-item--danger {
    color: rgb(248 113 113);
}

.sidebar-profile-dropdown-item--danger:hover {
    background: rgb(127 29 29 / 0.3);
    color: rgb(248 113 113);
}

/* Footer needs relative for dropdown positioning */
.sidebar-footer {
    position: relative;
}

/* ============================================
   Mobile Styles
   ============================================ */
.sidebar-mobile-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--sidebar-hover-bg);
    color: white;
    cursor: pointer;
    border-radius: 8px;
    padding: 8px;
}

/* Sky theme: darker toggle for light backgrounds */
[data-theme='sky'] .sidebar-mobile-toggle {
    background: rgb(var(--color-primary));
    color: rgb(var(--color-text-on-accent));
}

.sidebar-mobile-toggle svg {
    width: 24px;
    height: 24px;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 35;
    opacity: 0;
    transition: opacity var(--sidebar-transition);
}

.sidebar-overlay--visible {
    display: block;
    opacity: 1;
}

/* Mobile breakpoint */
@media (max-width: 768px) {
    .sidebar-mobile-toggle {
        display: flex;
    }

    .sidebar {
        transform: translateX(-100%);
        width: var(--sidebar-width);
    }

    .sidebar--collapsed {
        width: var(--sidebar-width);
        /* Override collapsed width on mobile */
    }

    /* Force show labels on mobile even when collapsed */
    .sidebar--collapsed .sidebar-menu-label,
    .sidebar--collapsed .sidebar-logo-text {
        opacity: 1;
        visibility: visible;
        width: auto;
    }

    .sidebar--collapsed .sidebar-section-header {
        opacity: 1;
        visibility: visible;
        width: auto;
        height: auto;
        padding: 8px 12px 6px;
        margin: 0;
    }

    .sidebar--collapsed .sidebar-menu-item {
        justify-content: flex-start;
        padding: 8px 12px;
    }

    .sidebar--collapsed .sidebar-shortcut-hint {
        display: flex;
    }

    .sidebar--mobile-open {
        transform: translateX(0);
    }

    .sidebar-toggle {
        display: none;
        /* Hide collapse toggle on mobile */
    }

    body.has-sidebar .dash-container,
    body.has-sidebar .page-container {
        padding-left: 0;
        width: 100%;
    }

    body.has-sidebar.sidebar-collapsed .dash-container,
    body.has-sidebar.sidebar-collapsed .page-container {
        padding-left: 0;
        width: 100%;
    }

    /* Prevent body scroll when mobile menu is open */
    body.sidebar-mobile-open {
        overflow: hidden;
    }

    /* Floating mobile toggle for pages without header slot */
    .sidebar-mobile-toggle--floating {
        position: fixed;
        top: 16px;
        left: 16px;
        z-index: 50;
    }

    /* When sidebar is open, move floating toggle to sidebar header's right side */
    /* This prevents the X button from overlapping the sidebar logo */
    body.sidebar-mobile-open .sidebar-mobile-toggle--floating {
        /* Position at right side of sidebar header (240px - 12px padding - 40px button = 188px) */
        left: calc(var(--sidebar-width) - 52px);
        z-index: 45; /* Above sidebar (40) but visually part of it */
        background: transparent;
    }

    /* ============================================
       Mobile Header Clearance for Hamburger Menu
       The floating toggle is at top:16px, left:16px
       with 40px width, so headers need ~56px clearance
       ============================================ */

    /* Page headers - add left padding to clear hamburger */
    /* Pattern: container > first flex/header element */
    body.has-sidebar .page-container > .flex:first-child,
    body.has-sidebar .page-container > header:first-child,
    body.has-sidebar .page-container > div:first-child > .flex:first-child,
    body.has-sidebar .page-container > div:first-child > header,
    body.has-sidebar .page-container > div:first-child > a:first-child,
    body.has-sidebar .dash-container > .flex:first-child,
    body.has-sidebar .dash-container > header:first-child,
    body.has-sidebar .history-container > .history-header {
        padding-left: 56px;
    }

    /* Special handling for pages with specific header classes */
    body.has-sidebar .page-header,
    body.has-sidebar .dashboard-header {
        padding-left: 56px;
    }

    /* Utility class for manual application */
    .mobile-menu-clearance {
        padding-left: 56px !important;
    }

    /* Ensure page titles don't wrap oddly with the padding */
    body.has-sidebar .page-container h1,
    body.has-sidebar .dash-container h1,
    body.has-sidebar .history-container h1 {
        word-break: break-word;
    }

    /* Hide back links on mobile - users can use hamburger menu to navigate */
    /* Target navigation back links by:
       1. href + text-sm class (before JS runs)
       2. .nav-back-link class (after updateBackButtons() changes href)
       3. .back-link class (history.html uses this)
       Error recovery links use different styling (bg-blue-600, font-medium) so they're preserved */
    body.has-sidebar a[href="/dashboard"].text-sm,
    body.has-sidebar a[href="dashboard"].text-sm,
    body.has-sidebar a[href="index.html"].text-sm,
    body.has-sidebar .nav-back-link,
    body.has-sidebar .back-link {
        display: none !important;
    }

    /* Ensure consistent mobile padding for page content */
    /* This ensures left/right padding is even across all pages */
    /* Exclude pages with px-0 (like heatmap) that need full-width */
    body.has-sidebar .page-container:not(.px-0),
    body.has-sidebar .dash-container,
    body.has-sidebar .history-container {
        padding-left: 16px;
        padding-right: 16px;
    }
}

/* ============================================
   Scrollbar Styling
   ============================================ */
.sidebar-nav::-webkit-scrollbar {
    width: 6px;
}

.sidebar-nav::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar-nav::-webkit-scrollbar-thumb {
    background: var(--sidebar-border);
    border-radius: 3px;
}

.sidebar-nav::-webkit-scrollbar-thumb:hover {
    background: var(--sidebar-text-muted);
}

/* ============================================
   Content Area Spacing
   ============================================ */
body.has-sidebar .dashboard-filter-bar {
    margin-left: 1rem;
}

/* ============================================
   Hide old navigation elements
   ============================================ */
body.has-sidebar #analysis-dropdown,
body.has-sidebar #resources-dropdown,
body.has-sidebar #settings-dropdown,
body.has-sidebar>.dash-container button[data-action="logout"] {
    display: none !important;
}