/* ═══════════════════════════════════════════════════════════════════
   Navigation Rail — Unified Global Navigation Shell
   Persistent left sidebar + header bar for authenticated users.
   ALWAYS-DARK by design (like VS Code sidebar / Slack sidebar).
   Uses --dark-* raw palette constants and --rail-* accent aliases
   from :root so the rail stays visually dark regardless of the
   active data-theme. Theme-switchable tokens are NOT used here.
   ═══════════════════════════════════════════════════════════════════ */

/* All CSS variables (--rail-width-collapsed, --rail-width-expanded,
   --header-height, --rail-accent-*, z-index layers, --dark-* palette)
   are defined in main.css :root and remain constant across themes */

/* ─── Global Header Bar ─────────────────────────────────────────── */
#global-header-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--dark-surface-bg);
    backdrop-filter: var(--blur-md);
    -webkit-backdrop-filter: var(--blur-md);
    border-bottom: 1px solid var(--dark-border-color);
    box-shadow: var(--dark-shadow-menu);
    z-index: var(--z-header);
    display: none;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
}

body.rail-loading #global-header-bar,
body.has-nav-rail #global-header-bar {
    display: flex;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.header-logo {
    height: var(--logo-rail-header);
    width: auto;
}

.header-org-name {
    font-size: var(--data-size);
    font-weight: 600;
    color: var(--dark-text-light);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
}

.header-center {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
}

.header-breadcrumb {
    font-size: 12px;
    color: var(--dark-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.header-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: var(--border-radius-sm);
    background: transparent;
    border: 1px solid transparent;
    color: var(--dark-text-secondary);
    cursor: pointer;
    transition: all var(--transition-base);
    font-size: 14px;
    position: relative;
}

.header-btn:hover {
    background: var(--dark-surface-hover);
    color: var(--dark-text-light);
    border-color: var(--dark-border-faint);
}

.header-btn svg {
    width: 18px;
    height: 18px;
}

.header-theme-toggle {
    font-size: 16px;
}

.header-user-menu {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: all var(--transition-base);
    background: transparent;
    border: 1px solid transparent;
    color: var(--dark-text-secondary);
    font-size: 12px;
    font-weight: 500;
}

.header-user-menu:hover {
    background: var(--dark-surface-hover);
    border-color: var(--dark-border-faint);
    color: var(--dark-text-light);
}

.header-user-name {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-user-avatar {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: var(--rail-accent-bg-medium);
    color: var(--rail-accent-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    flex-shrink: 0;
}

.header-credits-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--rail-accent-bg-subtle);
    color: var(--rail-accent-muted);
    border: 1px solid var(--rail-accent-border);
    white-space: nowrap;
    display: none;
}

.header-mobile-hamburger {
    display: none;
    width: 34px;
    height: 34px;
    border-radius: var(--border-radius-sm);
    background: transparent;
    border: 1px solid transparent;
    color: var(--dark-text-secondary);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

/* ─── Navigation Rail ────────────────────────────────────────────── */
#nav-rail {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    width: var(--rail-width-collapsed);
    background: var(--dark-surface-deep);
    border-right: 1px solid var(--dark-border-faint);
    z-index: var(--z-rail);
    display: none;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    transition: width var(--transition-slow);
    scrollbar-width: thin;
    scrollbar-color: var(--dark-surface-highlight) transparent;
}

body.rail-loading #nav-rail,
body.has-nav-rail #nav-rail {
    display: flex;
}

body.rail-loading .rail-nav-group {
    display: none;
}

body.has-nav-rail .rail-nav-group {
    display: block;
}

#nav-rail.expanded,
#nav-rail.pinned {
    width: var(--rail-width-expanded);
}

#nav-rail::-webkit-scrollbar {
    width: 4px;
}

#nav-rail::-webkit-scrollbar-thumb {
    background: var(--dark-surface-highlight);
    border-radius: 2px;
}

/* ─── Rail Pin Button ────────────────────────────────────────────── */
.rail-pin-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    background: transparent;
    border: none;
    color: var(--dark-text-muted);
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all var(--transition-base);
    z-index: 2;
}

#nav-rail.expanded .rail-pin-btn,
#nav-rail.pinned .rail-pin-btn {
    display: flex;
}

.rail-pin-btn:hover {
    background: var(--dark-surface-hover);
    color: var(--dark-text-light);
}

#nav-rail.pinned .rail-pin-btn {
    color: var(--accent-purple-muted);
}

/* ─── Rail Navigation Groups ─────────────────────────────────────── */
.rail-nav-groups {
    padding: 8px 0;
    flex: 1;
}

.rail-nav-group {
    margin-bottom: 4px;
}

.rail-group-label {
    padding: 8px 16px 4px;
    font-size: var(--label-size);
    font-weight: var(--label-weight);
    text-transform: uppercase;
    letter-spacing: var(--label-spacing);
    color: var(--dark-text-muted);
    white-space: nowrap;
    overflow: hidden;
    opacity: 0;
    transition: opacity var(--transition-slow);
}

#nav-rail.expanded .rail-group-label,
#nav-rail.pinned .rail-group-label {
    opacity: 1;
}

/* ─── Rail Navigation Items ──────────────────────────────────────── */
.rail-nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    height: 40px;
    margin: 2px 8px;
    padding: 0 8px;
    border-radius: var(--border-radius-sm);
    color: var(--dark-text-mid);
    text-decoration: none;
    font-size: var(--data-size);
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-base);
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    border: none;
    background: transparent;
    width: calc(100% - 16px);
    box-sizing: border-box;
}

.rail-nav-item:hover {
    background: var(--dark-surface-hover);
    color: var(--dark-text-light);
    transform: var(--lift-sm);
}

.rail-nav-item.active {
    background: var(--rail-accent-bg-subtle);
    color: var(--rail-accent-muted);
}

.rail-nav-item.active::before {
    content: '';
    position: absolute;
    left: -8px;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 24px;
    background: var(--rail-accent);
    border-radius: 0 3px 3px 0;
    transition: top var(--transition-slow);
}

.rail-nav-icon {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 16px;
}

.rail-nav-icon svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.rail-nav-label {
    opacity: 0;
    transition: opacity var(--transition-slow);
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
}

#nav-rail.expanded .rail-nav-label,
#nav-rail.pinned .rail-nav-label {
    opacity: 1;
}

/* ─── Rail Sub-items (expandable groups) ─────────────────────────── */
.rail-subnav {
    display: none;
    padding: 0;
}

#nav-rail.expanded .rail-subnav.open,
#nav-rail.pinned .rail-subnav.open {
    display: block;
}

.rail-subnav .rail-nav-item {
    height: 34px;
    padding-left: 42px;
    font-size: 12px;
}

.rail-nav-item .rail-expand-arrow {
    margin-left: auto;
    font-size: 10px;
    opacity: 0;
    transition: all var(--transition-base);
    flex-shrink: 0;
}

#nav-rail.expanded .rail-nav-item .rail-expand-arrow,
#nav-rail.pinned .rail-nav-item .rail-expand-arrow {
    opacity: 0.6;
}

.rail-nav-item.group-open .rail-expand-arrow {
    transform: rotate(90deg);
}

/* ─── Tooltip (collapsed mode) ───────────────────────────────────── */
.rail-tooltip {
    position: fixed;
    left: calc(var(--rail-width-collapsed) + 8px);
    background: var(--dark-surface-modal);
    color: var(--dark-text-light);
    padding: 6px 12px;
    border-radius: var(--border-radius-sm);
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    z-index: var(--z-tooltip);
    pointer-events: none;
    opacity: 0;
    transform: translateX(-4px);
    transition: opacity var(--transition-fast), transform var(--transition-fast);
    box-shadow: var(--dark-shadow-menu);
    border: 1px solid var(--dark-border-color);
}

.rail-tooltip.visible {
    opacity: 1;
    transform: translateX(0);
}

/* ─── Body Layout Adjustments ────────────────────────────────────── */
/* NOTE: padding-left/padding-top are set via JavaScript in initNavRail()
   because CSS class-based padding doesn't reliably work on fullscreen-page
   bodies due to specificity/cascade issues in the existing codebase.
   The CSS below handles non-padding layout adjustments. */

/* has-nav-rail hide rules for #nav-bar, .hero-banner-section,
   #learner-nav-bar, .mobile-menu-overlay are in main.css
   under the Canonical Shell Contract section. */

body.has-nav-rail.fullscreen-page {
    padding-left: var(--rail-width-collapsed) !important;
}

body.has-nav-rail.rail-pinned.fullscreen-page {
    padding-left: var(--rail-width-expanded) !important;
}

body.has-nav-rail.fullscreen-page .sa-shell {
    min-height: calc(100vh - var(--header-height));
}

body.has-nav-rail.fullscreen-page .sa-sidebar {
    top: var(--header-height);
    height: calc(100vh - var(--header-height));
}

body.has-nav-rail.fullscreen-page.nav-hidden .docqa-container,
body.has-nav-rail.fullscreen-page.nav-hidden .workbench-container {
    height: calc(100vh - var(--header-height)) !important;
}

body.has-nav-rail.fullscreen-page.scrollable-page {
    min-height: calc(100vh - var(--header-height));
}

/* ─── Mobile Responsive (below 768px) ────────────────────────────── */
@media (max-width: 768px) {
    #global-header-bar {
        padding: 0 8px;
        gap: 6px;
    }

    .header-left {
        gap: 8px;
    }

    .header-right {
        margin-left: auto;
        gap: 4px;
    }

    .header-btn {
        width: 30px;
        height: 30px;
    }

    .header-btn svg {
        width: 16px;
        height: 16px;
    }

    #header-cmd-palette {
        display: none;
    }

    .header-user-menu {
        padding: 2px 6px;
        gap: 4px;
    }

    .header-user-name {
        display: none;
    }

    .header-mobile-hamburger {
        display: flex;
    }

    .header-center {
        display: none;
    }

    .header-org-name {
        display: none;
    }

    .header-credits-badge {
        display: none !important;
    }

    #nav-rail {
        width: var(--rail-width-expanded) !important;
        left: calc(-1 * var(--rail-width-expanded));
        transition: left var(--transition-slow);
    }

    #nav-rail.mobile-open {
        left: 0;
    }

    #nav-rail .rail-nav-label {
        opacity: 1;
    }

    #nav-rail .rail-group-label {
        opacity: 1;
    }

    #nav-rail .rail-expand-arrow {
        opacity: 0.6 !important;
    }

    #nav-rail .rail-subnav.open {
        display: block;
    }

    #nav-rail-overlay {
        display: none;
        position: fixed;
        top: var(--header-height);
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--dark-overlay-medium);
        z-index: var(--z-rail-overlay);
    }

    #nav-rail-overlay.visible {
        display: block;
    }

    /* JS handles mobile reset of padding-left via initNavRail */

    .rail-pin-btn {
        display: none !important;
    }

    .rail-tooltip {
        display: none !important;
    }
}

/* ─── sa-sidebar-toggle adjustment with rail ─────────────────────── */
body.has-nav-rail .sa-sidebar-toggle {
    left: calc(var(--rail-width-collapsed) + 12px);
    top: calc(var(--header-height) + 12px);
}

@media (max-width: 768px) {
    body.has-nav-rail .sa-sidebar-toggle {
        left: 12px;
        top: calc(var(--header-height) + 12px);
    }
}

/* ─── Keyboard Focus ─────────────────────────────────────────────── */
.rail-nav-item:focus-visible {
    outline: 2px solid var(--rail-accent);
    outline-offset: -2px;
}

/* ─── Reduced Motion ─────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    #nav-rail {
        transition: none;
    }

    .rail-nav-item {
        transition: none;
    }

    .rail-nav-item::before {
        transition: none;
    }

    .rail-tooltip {
        transition: none;
    }

    .rail-nav-label,
    .rail-group-label {
        transition: none;
    }
}

/* ─── Header Notification Dropdown ──────────────────────────────── */
.header-notification-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    width: 320px;
    max-height: 400px;
    background: var(--dark-surface-modal);
    border: 1px solid var(--dark-border-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
    z-index: var(--z-tooltip);
    overflow: hidden;
}

.header-notif-title {
    padding: 12px 16px;
    font-weight: 600;
    font-size: 13px;
    color: var(--dark-text-primary);
    border-bottom: 1px solid var(--dark-border-faint);
}

.header-notif-list {
    max-height: 300px;
    overflow-y: auto;
}

.header-notif-empty {
    padding: 24px 16px;
    text-align: center;
    color: var(--dark-text-muted);
    font-size: 13px;
}

.header-notif-item {
    padding: 10px 16px;
    border-bottom: 1px solid var(--dark-border-faint);
    font-size: 13px;
    color: var(--dark-text-secondary);
    cursor: pointer;
}

.header-notif-item:hover {
    background: var(--dark-surface-hover);
}

.header-notif-footer {
    display: block;
    padding: 10px 16px;
    text-align: center;
    font-size: 12px;
    color: var(--rail-accent);
    text-decoration: none;
    border-top: 1px solid var(--dark-border-faint);
}

.header-notif-footer:hover {
    background: var(--dark-surface-hover);
}

/* ─── Rail Skeleton Loading ─────────────────────────────────────── */
.rail-skeleton-item {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    gap: 12px;
}

.rail-skeleton-icon {
    width: 20px;
    height: 20px;
    border-radius: var(--border-radius-sm);
    background: var(--dark-surface-tint);
    animation: rail-skeleton-pulse 1.5s ease-in-out infinite;
}

.rail-skeleton-label {
    height: 12px;
    width: 80px;
    border-radius: 4px;
    background: var(--dark-surface-tint);
    animation: rail-skeleton-pulse 1.5s ease-in-out infinite;
    animation-delay: 0.1s;
}

@keyframes rail-skeleton-pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 0.8; }
}

body.has-nav-rail .rail-skeleton-group {
    display: none;
}
