/* ============================================================
   APP SHELL — left sidebar (accordion menu) + profile menu.
   Replaces the old top bar. Tokens only (app.css :root / light theme).
   Loaded after the role stylesheets so it wins ties.
   ============================================================ */

:root { --sidebar-w: 240px; --mobile-bar-h: 56px; }

.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    height: 100vh;
    overflow: hidden;
}
.app-shell > .main { grid-column: 2; grid-row: 1; }

/* ---------- sidebar ---------- */
.app-sidebar {
    grid-column: 1; grid-row: 1;
    display: flex; flex-direction: column;
    min-height: 0;
    background: var(--color-bg-surface);
    border-right: 1px solid var(--color-border);
    position: relative;
    /* Above page content: page toolbars use z-index 20, and the profile menu,
       notifications panel and their click-catchers live in this stacking context. */
    z-index: 40;
}
.app-sidebar > * { flex-shrink: 0; }

.sidebar__brand {
    display: flex; align-items: center; justify-content: space-between;
    gap: var(--space-2);
    padding: var(--space-4) var(--space-4) var(--space-3);
}
.sidebar__brand-link {
    display: inline-flex; align-items: center; gap: var(--space-3);
    color: var(--color-text-primary); text-decoration: none;
    border-radius: var(--radius-md);
}
.sidebar__logo { width: 30px; height: 30px; display: block; }
.sidebar__title {
    font-family: var(--font-display); font-weight: 700;
    font-size: var(--font-md); letter-spacing: .3em;
}
.sidebar__close { display: none; }

.sidebar__nav {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: var(--space-1) var(--space-3) var(--space-3);
    display: flex; flex-direction: column; gap: 2px;
}
.sidebar__nav > * { flex-shrink: 0; }

/* ---------- groups (admin) ---------- */
.side-group { display: flex; flex-direction: column; }
.side-group__trigger {
    display: flex; align-items: center; gap: var(--space-3);
    width: 100%; height: 36px; padding: 0 var(--space-3);
    border: 0; border-radius: var(--radius-md);
    background: transparent; color: var(--color-text-primary);
    font: 600 var(--font-base) var(--font-family);
    cursor: pointer; text-align: left;
}
.side-group__trigger:hover { background: var(--color-bg-hover); }
.side-group__icon { width: 16px; text-align: center; color: var(--color-text-muted); font-size: 14px; }
.side-group.is-active .side-group__icon { color: var(--color-accent); }
.side-group__label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-group__chev { font-size: 11px; color: var(--color-text-muted); transition: transform .15s ease; }
.side-group.is-open .side-group__chev { transform: rotate(180deg); }
.side-group__dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--color-warning);
}
.side-group__items {
    display: flex; flex-direction: column; gap: 1px;
    margin: 2px 0 var(--space-2) 19px;
    padding-left: var(--space-2);
    border-left: 1px solid var(--color-border);
}
.side-group__items[hidden] { display: none; }

/* ---------- items (group children and flat menus) ---------- */
.side-item {
    display: flex; align-items: center; gap: var(--space-3);
    min-height: 32px; padding: 0 var(--space-3);
    border-radius: var(--radius-md);
    color: var(--color-text-secondary);
    font: 500 var(--font-base) var(--font-family);
    text-decoration: none;
}
.side-item:hover { background: var(--color-bg-hover); color: var(--color-text-primary); }
.side-item.active { background: var(--color-accent-dim); color: var(--color-text-primary); font-weight: 600; }
.side-item > i { width: 16px; text-align: center; font-size: 13px; color: var(--color-text-muted); }
.side-item.active > i { color: var(--color-accent); }
.side-item > span:not(.side-item__badge) { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-group__items .side-item > i { display: none; } /* the group icon already says it */

.side-item__badge {
    margin-left: auto;
    min-width: 20px; height: 18px; padding: 0 6px;
    display: inline-grid; place-items: center;
    border-radius: var(--radius-full);
    font: 600 var(--font-xs) var(--font-family);
    font-variant-numeric: tabular-nums;
}
.side-item__badge--warn   { background: var(--color-warning-dim); color: var(--color-warning); }
.side-item__badge--danger { background: var(--color-danger-dim);  color: var(--color-danger); }
.side-item__badge--info   { background: var(--color-info-dim);    color: var(--color-info); }
.side-item__badge--muted  { background: var(--color-bg-muted);    color: var(--color-text-muted); }

/* ---------- trader geo selector ---------- */
.sidebar-geo { position: relative; padding: 0 var(--space-3) var(--space-3); }
.sidebar-geo__btn {
    display: flex; align-items: center; gap: var(--space-2);
    width: 100%; height: 34px; padding: 0 var(--space-3);
    border: 1px solid var(--color-border); border-radius: var(--radius-md);
    background: var(--color-bg-elevated); color: var(--color-text-primary);
    font: 500 var(--font-base) var(--font-family); cursor: pointer;
}
.sidebar-geo__btn:hover { background: var(--color-bg-hover); }
.sidebar-geo__flag { width: 20px; height: 14px; border-radius: var(--radius-chip); object-fit: cover; }
.sidebar-geo__label { flex: 1; text-align: left; }
.sidebar-geo__chev { font-size: 11px; color: var(--color-text-muted); }
.app-sidebar .sidebar-geo__menu { left: var(--space-3); right: var(--space-3); width: auto; top: calc(100% - var(--space-2)); }

/* ---------- profile ---------- */
.sidebar__profile { position: relative; padding: var(--space-2) var(--space-3) var(--space-3); border-top: 1px solid var(--color-border); }

.side-avatar {
    position: relative; flex: none;
    width: 32px; height: 32px; border-radius: 50%;
    display: grid; place-items: center;
    background: var(--tone, var(--color-accent));
    color: var(--color-switch-thumb);
    font: 600 var(--font-base) var(--font-family);
}
.side-avatar--lg { width: 40px; height: 40px; }
.side-avatar__letter, .side-avatar__img { grid-area: 1 / 1; }
.side-avatar__img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.side-avatar__dot {
    position: absolute; top: -1px; right: -1px;
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--color-danger);
    box-shadow: 0 0 0 2px var(--color-bg-surface);
}

.profile-btn {
    display: flex; align-items: center; gap: var(--space-3);
    width: 100%; padding: var(--space-2);
    border: 1px solid var(--color-border); border-radius: var(--radius-lg);
    background: var(--color-bg-elevated); color: var(--color-text-primary);
    cursor: pointer; text-align: left;
}
.profile-btn:hover { background: var(--color-bg-hover); }
.profile-btn__text { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.25; }
.profile-btn__name { font: 600 var(--font-base) var(--font-family); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-btn__role { font-size: var(--font-xs); color: var(--color-text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-btn__chev { font-size: 11px; color: var(--color-text-muted); }

.profile-menu {
    position: absolute; z-index: 110;
    left: var(--space-3); right: var(--space-3);
    bottom: calc(100% - var(--space-1));
    display: flex; flex-direction: column;
    padding: var(--space-2);
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-dropdown);
}
.profile-menu:focus, .app-sidebar .side-notif:focus { outline: none; }
.profile-menu__head { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2); }
.profile-menu__hr { border: 0; border-top: 1px solid var(--color-border); margin: var(--space-1) var(--space-2); }
.profile-menu__row {
    display: flex; align-items: center; gap: var(--space-3);
    min-height: 36px; padding: 0 var(--space-2);
    border: 0; border-radius: var(--radius-md);
    background: transparent; color: var(--color-text-primary);
    font: 500 var(--font-base) var(--font-family);
    text-decoration: none; text-align: left; cursor: pointer;
}
.profile-menu__row:hover { background: var(--color-bg-hover); }
.profile-menu__row > i:first-child { width: 16px; text-align: center; color: var(--color-text-muted); }
.profile-menu__row > span:not(.side-item__badge):not(.mini-seg) { flex: 1; }
.profile-menu__row--static { cursor: default; }
.profile-menu__row--static:hover { background: transparent; }
.profile-menu__row--danger, .profile-menu__row--danger > i:first-child { color: var(--color-danger); }
.profile-menu__ext { font-size: 11px; color: var(--color-text-muted); }
.profile-menu__key { display: flex; flex-direction: column; gap: var(--space-1); padding: var(--space-1) var(--space-2) var(--space-2); }
.profile-menu__label { font-size: var(--font-xs); text-transform: uppercase; letter-spacing: .06em; color: var(--color-text-muted); }

.mini-seg { display: inline-flex; gap: 2px; padding: 2px; border-radius: var(--radius-sm); background: var(--color-bg-muted); }
.mini-seg__btn {
    border: 0; border-radius: var(--radius-chip); padding: 4px 9px;
    background: transparent; color: var(--color-text-muted);
    font: 600 var(--font-xs) var(--font-family); cursor: pointer;
}
.mini-seg__btn[aria-pressed="true"] { background: var(--color-bg-surface); color: var(--color-text-primary); box-shadow: var(--shadow-segment); }

/* Notifications panel: opens beside the sidebar, anchored to its bottom. */
.app-sidebar .side-notif {
    position: fixed;
    top: auto; right: auto;
    left: calc(var(--sidebar-w) + var(--space-2));
    bottom: var(--space-3);
    width: 340px; max-height: min(560px, calc(100vh - 2 * var(--space-3)));
    display: flex; flex-direction: column;
}
.app-sidebar .side-notif .notif-pop-body { overflow-y: auto; min-height: 0; }

/* Keyboard focus on every interactive shell element. */
.side-group__trigger:focus-visible, .side-item:focus-visible, .profile-btn:focus-visible,
.profile-menu__row:focus-visible, .mini-seg__btn:focus-visible, .sidebar-geo__btn:focus-visible,
.sidebar__brand-link:focus-visible, .sidebar__close:focus-visible,
.mobile-bar__burger:focus-visible, .mobile-bar__avatar:focus-visible {
    outline: 2px solid var(--color-accent); outline-offset: 2px;
}

/* Toasts: no top bar any more. */
.notif-toast-stack { top: var(--space-4); }

/* ---------- phone / tablet: off-canvas sidebar + top strip ---------- */
.mobile-bar { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 960px) {
    .app-shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: var(--mobile-bar-h) minmax(0, 1fr); }
    .app-shell > .main { grid-column: 1; grid-row: 2; }

    .mobile-bar {
        grid-column: 1; grid-row: 1;
        display: flex; align-items: center; gap: var(--space-3);
        padding: 0 var(--space-3);
        background: var(--color-bg-surface);
        border-bottom: 1px solid var(--color-border);
    }
    .mobile-bar .sidebar__brand-link { flex: 1; }
    .mobile-bar__burger, .mobile-bar__avatar, .sidebar__close {
        display: inline-grid; place-items: center;
        width: 36px; height: 36px; padding: 0;
        border: 0; border-radius: var(--radius-md);
        background: transparent; color: var(--color-text-primary);
        font-size: 16px; cursor: pointer;
    }
    .mobile-bar__burger:hover, .sidebar__close:hover { background: var(--color-bg-hover); }

    .app-sidebar {
        position: fixed; top: 0; bottom: 0; left: 0;
        width: min(300px, 86vw);
        transform: translateX(-100%);
        /* Closed = hidden from Tab order and screen readers too, after the slide-out. */
        visibility: hidden;
        transition: transform .2s ease, visibility 0s linear .2s;
        z-index: 121;
    }
    .app-sidebar.is-open {
        transform: translateX(0);
        visibility: visible;
        transition: transform .2s ease;
        box-shadow: var(--shadow-dropdown); /* only when open: a closed drawer's blur would show at the edge */
    }
    .sidebar-backdrop {
        display: block; position: fixed; inset: 0; z-index: 120;
        background: rgba(var(--color-ink-rgb), .5);
    }

    /* In the drawer the notifications panel takes the drawer's width. */
    .app-sidebar .side-notif {
        left: var(--space-2) !important; right: var(--space-2) !important;
        top: auto !important; bottom: var(--space-3);
        width: auto !important;
    }
    .notif-toast-stack { top: calc(var(--mobile-bar-h) + var(--space-2)); }
    /* app.css pins every .pop under the old top bar on phones; keep the geo menu under its button. */
    .app-sidebar .sidebar-geo__menu {
        top: calc(100% - var(--space-2)) !important;
        left: var(--space-3) !important; right: var(--space-3) !important;
    }
}

@media (prefers-reduced-motion: reduce) {
    .app-sidebar, .side-group__chev { transition: none; }
}

/* ── PROD | TEST view (.omc/plans/test-circuit.md, §3) ─────────────────────
   The switch sits under the logo (staff only). In TEST the active half, the
   sidebar tint, the active item and primary buttons turn warning-orange, and a
   banner over the page says the money is not real. Test accounts' cabinets get
   the same tint and banner, without the switch. */
.mode-switch { padding: 0 var(--space-4) var(--space-3); }
.mode-switch .act-tabs-wrap { display: flex; width: 100%; box-sizing: border-box; }
.mode-switch .act-tab { flex: 1 1 0; letter-spacing: .08em; font-weight: 700; }
.mode-switch__hint { margin-top: 6px; padding: 0 2px; font-size: 11.5px; color: var(--color-text-muted); }
.acct-type { padding: 0 var(--space-4) var(--space-3); }
/* .act-tab-active is !important in app.css (both themes). */
.is-test .mode-switch .act-tab-active { background: var(--color-warning) !important; color: #1A1205 !important; }
.is-test .app-sidebar {
    background: color-mix(in srgb, var(--color-warning) 7%, var(--color-bg-surface));
    border-right-color: var(--color-warning-border);
}
.is-test .side-item.active { background: var(--color-warning-dim); }
.is-test .side-item.active > i { color: var(--color-warning); }
.is-test .ui-btn--primary, .is-test .bal-btn-primary {
    background: var(--color-warning); border-color: var(--color-warning); color: #1A1205;
}
.mode-banner {
    display: flex; align-items: center; gap: 10px;
    margin-bottom: var(--space-4); padding: 10px 14px;
    border-radius: var(--radius-md);
    background: var(--color-warning-dim); border: 1px solid var(--color-warning-border);
    color: var(--color-warning); font-weight: 600; font-size: 13px;
}
/* The page wrapper exists only to re-create the page on a view change. */
.view-scope { display: contents; }
.type-cell { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.type-note { margin: var(--space-2) 0 var(--space-4); padding: 10px 12px; border-radius: var(--radius-md); font-size: 13px; line-height: 1.5; }
.type-note--test { background: var(--color-warning-dim); border: 1px solid var(--color-warning-border); color: var(--color-warning); }
.type-note--prod { background: var(--color-accent-dim); border: 1px solid var(--color-accent-border); color: var(--color-accent-bright); }
