/* ============================================================
   packages/ui — COLLAPSIBLE LEFT-PANEL NAV
   One nav for every internal app. App-switcher dropdown at the
   top, page links below, user/sign-out footer. Collapses to a
   56px icon rail (persisted). No top bar. Rendered by nav.js.
   Requires: tokens.css + components.css. Pair with body class
   "app-bg" and a <main class="app-main"> sibling.
   ============================================================ */

.app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-w);
    height: 100vh;
    background: var(--bg-elev);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: width 200ms var(--ease);
}

/* ------------------------------------------------------------
   APP-SWITCHER (top)
   ------------------------------------------------------------ */
.sidebar-top {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 12px;
    border-bottom: 1px solid var(--border);
}
.app-switcher {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 160ms var(--ease), border-color 160ms var(--ease);
}
.app-switcher:hover { background: var(--bg-elev-2); border-color: var(--border); }
.app-switcher img { height: 22px; width: 22px; object-fit: contain; flex-shrink: 0; }
.app-switcher .app-name {
    flex: 1;
    min-width: 0;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: left;
}
.app-switcher .chev {
    font-size: 10px;
    color: var(--text-dim);
    flex-shrink: 0;
    transition: transform 180ms var(--ease);
}
.app-switcher[aria-expanded="true"] .chev { transform: rotate(180deg); }

/* collapse toggle button (the « / » control) */
.sidebar-collapse {
    flex-shrink: 0;
    width: 28px; height: 28px;
    display: flex; align-items: center; justify-content: center;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-dim);
    cursor: pointer;
    font-size: 14px; line-height: 1;
    transition: background 160ms var(--ease), color 160ms var(--ease), border-color 160ms var(--ease);
}
.sidebar-collapse:hover { background: var(--bg-elev-2); color: var(--text); border-color: var(--border); }

/* the dropdown panel */
.app-switcher-menu {
    position: absolute;
    top: calc(100% - 4px);
    left: 12px;
    right: 12px;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    box-shadow: 0 16px 40px -16px rgba(0,0,0,0.25);
    padding: 6px;
    z-index: 200;
    display: none;
    animation: rise 180ms var(--ease) both;
}
.app-switcher-menu.open { display: block; }
.app-switcher-menu a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    transition: background 140ms var(--ease), color 140ms var(--ease);
}
.app-switcher-menu a:hover { background: var(--bg-elev); color: var(--text); }
.app-switcher-menu a.current { color: var(--coral); font-weight: 600; }
.app-switcher-menu a .icon { width: 18px; text-align: center; flex-shrink: 0; }
.app-switcher-menu a .ext { margin-left: auto; font-size: 11px; color: var(--text-dim); }

/* ------------------------------------------------------------
   NAV LINKS
   ------------------------------------------------------------ */
.sidebar-nav { flex: 1; padding: 12px 10px; overflow-y: auto; }
.sidebar-section { margin-bottom: 8px; }
.sidebar-section-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-dim);
    padding: 10px 12px 6px;
    white-space: nowrap;
}
.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    transition: background 160ms var(--ease), color 160ms var(--ease);
    cursor: pointer;
    white-space: nowrap;
}
.sidebar-link:hover { background: var(--bg-elev-2); color: var(--text); }
.sidebar-link.active { background: var(--coral-soft); color: var(--coral); font-weight: 600; }
.sidebar-link .icon { width: 18px; text-align: center; font-size: 14px; flex-shrink: 0; }
.sidebar-link .link-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.sidebar-link .tag {
    margin-left: auto;
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-dim);
}
.sidebar-link .badge-count {
    margin-left: auto;
    background: var(--coral);
    color: #fff;
    padding: 1px 7px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    min-width: 18px;
    text-align: center;
}

/* ------------------------------------------------------------
   FOOTER (user + sign out)
   ------------------------------------------------------------ */
.sidebar-footer {
    padding: 14px 16px;
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-dim);
}
.sidebar-user { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sidebar-user .email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; }

/* ============================================================
   COLLAPSED RAIL — body.nav-collapsed shrinks --sidebar-w, so
   .app-sidebar + .app-main both follow automatically.
   ============================================================ */
body.nav-collapsed { --sidebar-w: var(--sidebar-rail); }
body.nav-collapsed .app-switcher { justify-content: center; padding: 8px; }
body.nav-collapsed .app-switcher .app-name,
body.nav-collapsed .app-switcher .chev,
body.nav-collapsed .sidebar-section-label,
body.nav-collapsed .sidebar-link .link-label,
body.nav-collapsed .sidebar-link .tag,
body.nav-collapsed .sidebar-user .email { display: none; }
body.nav-collapsed .sidebar-top { flex-direction: column; gap: 6px; padding: 12px 6px; }
body.nav-collapsed .sidebar-link { justify-content: center; padding: 10px 0; }
body.nav-collapsed .sidebar-link .badge-count { margin-left: 0; position: absolute; }
body.nav-collapsed .sidebar-nav { padding: 12px 6px; }
body.nav-collapsed .sidebar-user { justify-content: center; }
/* in the rail, the switcher dropdown widens past the rail so labels are usable */
body.nav-collapsed .app-switcher-menu { left: 8px; right: auto; width: 200px; }

/* ------------------------------------------------------------
   RESPONSIVE — sidebar becomes a top strip under ~860px
   ------------------------------------------------------------ */
@media (max-width: 860px) {
    .app-sidebar {
        width: 100%;
        height: auto;
        position: relative;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .app-main { margin-left: 0 !important; padding: 24px 20px 48px; }
    .sidebar-nav { display: flex; overflow-x: auto; gap: 4px; padding: 8px 10px; }
    .sidebar-section { margin-bottom: 0; display: flex; gap: 4px; }
    .sidebar-section-label { display: none; }
    .sidebar-collapse { display: none; }
    .sidebar-footer { display: none; }
    /* the rail concept doesn't apply on mobile */
    body.nav-collapsed { --sidebar-w: 100%; }
    body.nav-collapsed .sidebar-link .link-label { display: inline; }
}
