/* ================================================================
   SIDEBAR NAV  --  desktop left-rail navigation (>= 1025px)
   Markup: #appSidebar in app/index.html. Behaviour: js/components/sidebar-nav.js.

   Collapsed (body.sidebar-collapsed): icon rail, hover tooltips, "More" opens
   as a flyout. Expanded (default): icon + label, "More" is an inline accordion.

   IMPORTANT: do NOT add backdrop-filter / transform / filter / contain to
   #appSidebar or its .nav-content -- any of them makes the collapsed "More"
   flyout (position: fixed) resolve against the sidebar instead of the viewport.

   Below 1025px the sidebar is hidden and the existing mobile bottom nav /
   "More" sheet keep working exactly as before.
   Revert point: git tag pre-sidebar-nav-20261001.
   ================================================================ */

#appSidebar {
    display: none;
}

.app-sidebar-tip {
    display: none;
}

@media (min-width: 1025px) {

    /* ── Layout vars ─────────────────────────────────────────────── */
    body:has(#appSidebar) {
        --sb-w-collapsed: 80px;
        --sb-w-expanded: 256px;
        --sb-w: var(--sb-w-expanded);
        /* Header is now a fixed-height bar (no more shrink-on-scroll) so the
           sidebar can sit flush beneath it. Every consumer of this var
           (sticky drawers, scroll-margins, page padding) follows automatically. */
        --dashboard-header-height: 72px;
    }

    body.sidebar-collapsed:has(#appSidebar) {
        --sb-w: var(--sb-w-collapsed);
    }

    /* ── Header: slimmer, fixed height, nav row removed ──────────── */
    body:has(#appSidebar) .header,
    body:has(#appSidebar) .header.scrolled {
        box-sizing: border-box;
        height: var(--dashboard-header-height);
        padding: 0;
        display: flex;
        align-items: center;
    }

    body:has(#appSidebar) .header .header-content {
        width: 100%;
        height: 100%;
    }

    body:has(#appSidebar) .header .logo,
    body:has(#appSidebar) .header.scrolled .logo {
        font-size: 30px;
        gap: 12px;
    }

    body:has(#appSidebar) .header .logo-image,
    body:has(#appSidebar) .header.scrolled .logo-image {
        width: 52px;
        height: 52px;
        padding: 4px;
    }

    /* ── Page content makes room for the rail ────────────────────── */
    body:has(#appSidebar) .dashboard.active {
        padding-left: var(--sb-w);
        transition: none;
    }

    /* Wider desktop sizes have enough room for the expanded rail. Keep page
       widths fixed and use only the measured offset needed to clear the rail. */
    @media (min-width: 1366px) {
        body:not(.sidebar-collapsed):has(#appSidebar) .dashboard.active {
            padding-left: var(--sb-w-collapsed);
        }

        body:not(.sidebar-collapsed):has(#appSidebar) .dashboard.active > .page-content {
            left: clamp(0px, calc(906px - 62.5vw), 52.25px);
        }
    }

    /* ── Sidebar shell ───────────────────────────────────────────── */
    #appSidebar {
        --sb-bg: linear-gradient(180deg, rgba(14, 15, 30, 0.97) 0%, rgba(8, 9, 19, 0.98) 100%);
        --sb-border: rgba(255, 255, 255, 0.07);
        --sb-text: rgba(212, 218, 244, 0.7);
        --sb-text-strong: #ffffff;
        --sb-muted: rgba(190, 198, 232, 0.45);
        --sb-hover-bg: rgba(255, 255, 255, 0.07);
        --sb-accent: var(--accent-primary, #6366f1);
        --sb-accent-soft: #a5b4fc;
        --sb-active-bg: rgba(99, 102, 241, 0.2);
        --sb-flyout-bg: rgba(15, 16, 32, 0.99);

        display: flex;
        flex-direction: column;
        position: fixed;
        z-index: calc(var(--z-header, 2000) - 10);
        top: calc(var(--dashboard-header-height) + var(--email-verify-banner-height, 0px));
        left: 0;
        bottom: 0;
        width: var(--sb-w);
        box-sizing: border-box;
        background: var(--sb-bg);
        border-right: 1px solid var(--sb-border);
        box-shadow: 10px 0 36px rgba(0, 0, 0, 0.26);
        font-family: 'Josefin Sans', sans-serif;
        transition: width 0.22s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @supports (background: color-mix(in srgb, red 50%, blue)) {
        #appSidebar {
            --sb-active-bg: color-mix(in srgb, var(--sb-accent) 24%, transparent);
            --sb-accent-soft: color-mix(in srgb, var(--sb-accent) 50%, #ffffff);
        }
    }

    /* ── Top row: eyebrow + collapse toggle ──────────────────────── */
    .app-sidebar-top {
        flex: 0 0 auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 60px;
        padding: 0 14px 0 24px;
        box-sizing: border-box;
    }

    .app-sidebar-eyebrow {
        font-size: 11px;
        font-weight: 700;
        letter-spacing: 0.16em;
        text-transform: uppercase;
        color: var(--sb-muted);
        white-space: nowrap;
        overflow: hidden;
    }

    .app-sidebar-toggle {
        flex: 0 0 auto;
        display: grid;
        place-items: center;
        width: 38px;
        height: 38px;
        padding: 0;
        border-radius: 11px;
        border: 1px solid var(--sb-border);
        background: rgba(255, 255, 255, 0.03);
        color: var(--sb-text);
        cursor: pointer;
        transition: background 0.16s ease, color 0.16s ease, border-color 0.16s ease;
    }

    .app-sidebar-toggle:hover {
        background: var(--sb-hover-bg);
        color: var(--sb-text-strong);
        border-color: rgba(255, 255, 255, 0.16);
    }

    .app-sidebar-toggle:focus-visible,
    #appSidebar .nav-item:focus-visible {
        outline: none;
        box-shadow: inset 0 0 0 2px var(--focus-ring, rgba(129, 140, 248, 0.75));
    }

    .app-sidebar-toggle svg {
        width: 20px;
        height: 20px;
        fill: none;
        stroke: currentColor;
        stroke-width: 1.7;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .app-sidebar-toggle-chev {
        transform-box: fill-box;
        transform-origin: center;
        transition: transform 0.22s ease;
    }

    body.sidebar-collapsed .app-sidebar-top {
        justify-content: center;
        padding: 0;
    }

    body.sidebar-collapsed .app-sidebar-eyebrow {
        display: none;
    }

    body.sidebar-collapsed .app-sidebar-toggle-chev {
        transform: rotate(180deg);
    }

    /* ── Nav list ────────────────────────────────────────────────── */
    #appSidebar .nav-content {
        flex: 1 1 auto;
        min-height: 0;
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        padding: 6px 12px 14px;
        overflow-x: hidden;
        overflow-y: auto;
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.16) transparent;
    }

    #appSidebar .nav-content::-webkit-scrollbar {
        width: 6px;
    }

    #appSidebar .nav-content::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.16);
        border-radius: 6px;
    }

    #appSidebar .workspace-switch,
    #appSidebar .workspace-switch.dual-mode {
        display: contents;
        border: 0;
        background: none;
        padding: 0;
    }

    /* ── Nav item (beats the per-theme `.nav-item` header rules) ─── */
    #appSidebar .nav-item,
    #appSidebar .nav-item.active,
    #appSidebar .workspace-switch.dual-mode .nav-item,
    #appSidebar .workspace-switch.dual-mode .nav-item.active {
        position: relative;
        display: flex;
        align-items: center;
        gap: 14px;
        width: 100%;
        height: 44px;
        box-sizing: border-box;
        padding: 0 16px;
        margin: 0;
        border: 0;
        border-bottom: 0;
        border-radius: 12px;
        background: transparent;
        color: var(--sb-text);
        font-family: inherit;
        font-size: 14.5px;
        font-weight: 600;
        letter-spacing: 0.2px;
        text-align: left;
        text-shadow: none;
        white-space: nowrap;
        cursor: pointer;
        transition: background 0.16s ease, color 0.16s ease;
    }

    #appSidebar .nav-item:hover {
        background: var(--sb-hover-bg);
        color: var(--sb-text-strong);
        text-shadow: none;
    }

    #appSidebar .nav-item.active,
    #appSidebar .nav-dropdown-toggle.more-active,
    #appSidebar .workspace-switch.dual-mode .nav-item.active {
        background: var(--sb-active-bg);
        color: var(--sb-text-strong);
    }

    /* Accent bar on the sidebar's inner edge for the current page */
    #appSidebar .nav-item.active::before,
    #appSidebar .nav-dropdown-toggle.more-active::before {
        content: "";
        position: absolute;
        left: -12px;
        top: 11px;
        bottom: 11px;
        width: 4px;
        border-radius: 0 4px 4px 0;
        background: var(--sb-accent-soft);
        box-shadow: 0 0 12px var(--sb-accent-soft);
    }

    #appSidebar .nav-ico {
        flex: 0 0 24px;
        width: 24px;
        height: 24px;
        display: grid;
        place-items: center;
        transition: transform 0.18s ease, color 0.16s ease;
    }

    #appSidebar .nav-ico svg {
        width: 22px;
        height: 22px;
        fill: none;
        stroke: currentColor;
        stroke-width: 1.7;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    #appSidebar .nav-item:hover .nav-ico {
        transform: scale(1.08);
    }

    #appSidebar .nav-item.active .nav-ico,
    #appSidebar .nav-dropdown-toggle.more-active .nav-ico {
        color: var(--sb-accent-soft);
    }

    #appSidebar .nav-label {
        flex: 1 1 auto;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        transition: opacity 0.16s ease 0.04s;
    }

    /* Collapsed rail: icons only (labels stay in the DOM for screen readers) */
    body.sidebar-collapsed #appSidebar .nav-label,
    body.sidebar-collapsed #appSidebar .nav-chevron {
        opacity: 0;
        pointer-events: none;
        transition-delay: 0s;
    }

    body.sidebar-collapsed #appSidebar .nav-dropdown-menu .nav-label {
        opacity: 1;
        pointer-events: auto;
    }

    /* ── "More" group ────────────────────────────────────────────── */
    #appSidebar .nav-dropdown {
        display: block;
        position: static;
        width: 100%;
        margin-top: 6px;
        padding-top: 8px;
        border-top: 1px solid var(--sb-border);
    }

    #appSidebar .nav-dropdown-toggle {
        justify-content: flex-start;
    }

    #appSidebar .nav-dropdown-toggle::after {
        display: none;
    }

    #appSidebar .nav-chevron {
        flex: 0 0 auto;
        width: 8px;
        height: 8px;
        margin-right: 3px;
        border-right: 1.8px solid currentColor;
        border-bottom: 1.8px solid currentColor;
        transform: translateY(-2px) rotate(45deg);
        transition: transform 0.2s ease, opacity 0.16s ease;
        opacity: 0.75;
    }

    #appSidebar .nav-dropdown.open .nav-chevron {
        transform: translateY(2px) rotate(-135deg);
    }

    /* Expanded: inline accordion */
    body:not(.sidebar-collapsed) #appSidebar .nav-dropdown-menu {
        position: static;
        display: none;
        flex-direction: column;
        gap: 2px;
        min-width: 0;
        margin: 4px 0 2px 23px;
        padding: 2px 0 2px 12px;
        border: 0;
        border-left: 1px solid var(--sb-border);
        border-radius: 0;
        background: transparent;
        box-shadow: none;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }

    body:not(.sidebar-collapsed) #appSidebar .nav-dropdown.open .nav-dropdown-menu,
    body:not(.sidebar-collapsed) #appSidebar .nav-dropdown:has(.nav-sub-item.active) .nav-dropdown-menu {
        display: flex;
        animation: sbMenuIn 0.18s ease-out;
    }

    /* Collapsed: flyout to the right of the rail */
    body.sidebar-collapsed #appSidebar .nav-dropdown-menu {
        position: fixed;
        left: calc(var(--sb-w) + 10px);
        top: var(--sb-flyout-top, 220px);
        right: auto;
        z-index: calc(var(--z-header, 2000) + 100);
        display: none;
        flex-direction: column;
        gap: 2px;
        min-width: 210px;
        padding: 8px;
        border-radius: 14px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: var(--sb-flyout-bg);
        box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5), 0 2px 8px rgba(0, 0, 0, 0.3);
    }

    body.sidebar-collapsed #appSidebar .nav-dropdown.open .nav-dropdown-menu {
        display: flex;
        animation: sbFlyIn 0.16s ease-out;
    }

    #appSidebar .nav-dropdown .nav-sub-item,
    #appSidebar .nav-dropdown .nav-sub-item.active {
        height: 40px;
        padding: 0 12px;
        gap: 12px;
        border-radius: 10px;
        font-size: 14px;
    }

    #appSidebar .nav-dropdown .nav-sub-item .nav-ico {
        flex-basis: 20px;
        width: 20px;
        height: 20px;
    }

    #appSidebar .nav-dropdown .nav-sub-item .nav-ico svg {
        width: 19px;
        height: 19px;
    }

    #appSidebar .nav-dropdown .nav-sub-item.active::before {
        left: -13px;
        top: 10px;
        bottom: 10px;
    }

    body.sidebar-collapsed #appSidebar .nav-dropdown .nav-sub-item.active::before {
        left: -8px;
    }

    /* ── Footer: reserved corner for the floating Messages bubble ── */
    .app-sidebar-foot {
        flex: 0 0 auto;
        height: 80px;
        border-top: 1px solid var(--sb-border);
    }

    /* The Messages bubble docks into that corner instead of overlapping nav */
    body:has(#appSidebar) .messaging-dock-toggle {
        left: 13px;
        bottom: 13px;
    }

    /* ── Tooltip (collapsed rail) ────────────────────────────────── */
    .app-sidebar-tip {
        display: block;
        position: fixed;
        z-index: calc(var(--z-header, 2000) + 200);
        left: 0;
        top: 0;
        padding: 7px 11px;
        border-radius: 9px;
        border: 1px solid rgba(255, 255, 255, 0.12);
        background: rgba(10, 11, 22, 0.98);
        color: #f1f5f9;
        font: 600 12.5px/1 'Josefin Sans', sans-serif;
        letter-spacing: 0.2px;
        white-space: nowrap;
        box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.12s ease;
    }

    .app-sidebar-tip.is-visible {
        opacity: 1;
    }

    /* ── Theme tuning ────────────────────────────────────────────── */
    body[data-theme="cloud"] #appSidebar {
        --sb-bg: linear-gradient(180deg, #2d1b69 0%, #231452 100%);
        --sb-border: rgba(255, 255, 255, 0.12);
        --sb-text: rgba(226, 220, 255, 0.78);
        --sb-muted: rgba(226, 220, 255, 0.5);
        --sb-accent: #a78bfa;
        --sb-active-bg: rgba(255, 255, 255, 0.16);
        --sb-hover-bg: rgba(255, 255, 255, 0.1);
        --sb-flyout-bg: #2a1963;
    }

    body[data-theme^="luxury"] #appSidebar {
        --sb-bg: linear-gradient(180deg, #2f2a28 0%, #252120 100%);
        --sb-text: rgba(240, 232, 226, 0.74);
        --sb-muted: rgba(240, 232, 226, 0.45);
        --sb-accent: #d4a97a;
        --sb-flyout-bg: #2c2725;
    }

    /* Dark / Dark Obsidian: neutral charcoal that matches the card surfaces
       (--bg-card #262626) instead of the default navy-indigo. Active state is a
       neutral lift rather than an accent tint, so nothing reads as blue. */
    body[data-theme="dark-obsidian"] #appSidebar,
    body[data-theme="dark"] #appSidebar {
        --sb-bg: linear-gradient(180deg, #292929 0%, #242424 100%);
        --sb-border: rgba(255, 255, 255, 0.08);
        --sb-text: rgba(228, 228, 231, 0.7);
        --sb-muted: rgba(212, 212, 216, 0.45);
        --sb-hover-bg: rgba(255, 255, 255, 0.06);
        --sb-active-bg: rgba(255, 255, 255, 0.11);
        --sb-accent-soft: #e4e4e7;
        --sb-flyout-bg: #2b2b2b;
    }

    body[data-theme="deep-blue"] #appSidebar {
        --sb-bg: linear-gradient(180deg, #08142a 0%, #050c1a 100%);
        --sb-accent: #2d8cff;
        --sb-flyout-bg: #0a1830;
    }
}

@keyframes sbMenuIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes sbFlyIn {
    from { opacity: 0; transform: translateX(-6px); }
    to   { opacity: 1; transform: translateX(0); }
}

@media (prefers-reduced-motion: reduce) {
    #appSidebar,
    #appSidebar *,
    body:has(#appSidebar) .dashboard.active,
    .app-sidebar-tip {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
}
