/* ============================================================================
 * BigBreakingWire Prime — Shared Application Shell (Part 2 foundation)
 * ----------------------------------------------------------------------------
 * Normalises the terminal chrome every shell page renders: top header +
 * branding, shell grid, sidebar column + drawer, page container + headings,
 * account cluster. Values are the dashboard canonicals expressed as
 * var(--bbw-*, <identical fallback>) so pages WITHOUT token support render
 * byte-identical to before.
 *
 * SCOPE CONTRACT:
 * - Every rule below is scoped under `.bbw-shell` (the `.app-shell` element
 *   on opted-in templates) EXCEPT the two Prime-specific overlay IDs noted
 *   in §6, which exist nowhere outside Prime templates (never wp-admin).
 * - No chart internals (.pc-*), no page widgets, no wp-admin selectors.
 * - Opt-in is one server-side class (`<div class="app-shell bbw-shell">`).
 *   Pages without the class keep their own stylesheets untouched.
 * - Existing hooks preserved: #mobileSidebar, #mobileOverlay, .top-header,
 *   .shell-body, .side, .nav-item(.active), .brand, toggleSidebar() classes
 *   (.open, body.sidebar-open, body.sidebar-collapsed).
 * ========================================================================== */

/* ---- §1 Shell root -------------------------------------------------------- */
.bbw-shell {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--bbw-bg-canvas, #eef1f5);
    color: var(--bbw-ink, #0b1526);
    font-family: var(--bbw-font, 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif);
    font-size: var(--bbw-text-body, 14px);
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

.bbw-shell button { font-family: inherit; }
.bbw-shell :focus-visible {
    outline: 2px solid var(--bbw-blue, #1e56e0);
    outline-offset: 2px;
    border-radius: 4px;
}

/* ---- §2 Top header ---------------------------------------------------------
 * Wrap-safe at every width: on roomy screens everything fits one 58px row;
 * on tight screens (tablet, desktop-mode-on-phone) the bar grows instead of
 * overflowing the page. Fixes page-level horizontal scroll ~960–1024px where
 * the unshrinkable account cluster + switcher exceeded the viewport. */
.bbw-shell .top-header {
    position: sticky;
    top: 0;
    z-index: var(--bbw-z-sticky, 60);
    height: auto;
    min-height: var(--bbw-topbar, 58px);
    flex-shrink: 0;
    background: var(--bbw-surface, #ffffff);
    border-bottom: 1px solid var(--bbw-border, #e2e7ee);
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px 16px;
    padding: 8px 20px;
}

/* Branding: BBW mark + wordmark + PRIME accent. Destinations unchanged. */
.bbw-shell .brand {
    font-size: 1.02rem;
    font-weight: 800;
    letter-spacing: .2px;
    color: var(--bbw-ink, #0b1526);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 7px;
    white-space: nowrap;
    flex-shrink: 0;
}
.bbw-shell .brand .brand-mark {
    width: 26px;
    height: 26px;
    border-radius: 7px;
    background: var(--bbw-blue, #1e56e0);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 800;
}
.bbw-shell .brand span { color: var(--bbw-down, #d90429); }
.bbw-shell .brand small {
    font-size: 9px;
    font-weight: 800;
    color: #fff;
    background: var(--bbw-blue, #1e56e0);
    border-radius: 4px;
    padding: 2px 5px;
    letter-spacing: .4px;
    margin-left: 4px;
}

/* Hamburger visibility belongs to each page's own breakpoint (dashboard ≤900,
 * facts/chatbot ≤980): this rule must not force-hide it, or drawer pages
 * lose their only way to open the menu. Every shell page hides it by default
 * in its own stylesheet. */
.bbw-shell .hamburger-btn {
    width: 38px;
    height: 38px;
    border: 1px solid var(--bbw-border, #e2e7ee);
    background: var(--bbw-surface, #ffffff);
    border-radius: 10px;
    font-size: 18px;
    color: var(--bbw-ink, #0b1526);
    cursor: pointer;
    align-items: center;
    justify-content: center;
}

/* The account cluster keeps its functional selectors; the shell only
 * guarantees spacing, alignment and focus treatment. It may shrink and
 * wrap — a cluster that cannot shrink is what overflowed the page. */
.bbw-shell .user-cluster,
.bbw-shell .user-status {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-left: auto;
    flex-shrink: 1;
    min-width: 0;
    row-gap: 8px;
}
/* Engine-owned market switcher lives inside the cluster: keep it fluid too. */
.bbw-shell .top-header .bbw-market { min-width: 0; flex-shrink: 1; }
.bbw-shell .avatar-pill {
    display: flex;
    align-items: center;
    gap: 9px;
    background: var(--bbw-surface-2, #f7f9fb);
    border: 1px solid var(--bbw-border, #e2e7ee);
    padding: 4px 12px 4px 5px;
    border-radius: var(--bbw-radius-pill, 999px);
    height: 38px;
    font: inherit;
    color: inherit;
    cursor: pointer;
    transition: border-color var(--bbw-transition-med, .18s ease), background var(--bbw-transition-med, .18s ease);
}
.bbw-shell .avatar-pill:hover { border-color: var(--bbw-blue, #1e56e0); background: var(--bbw-blue-soft, #eef3ff); }
.bbw-shell .avatar-circle {
    width: 28px;
    height: 28px;
    background: var(--bbw-blue, #1e56e0);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 12px;
    flex-shrink: 0;
}
.bbw-shell .logout-link {
    font-size: 12px;
    font-weight: 700;
    color: var(--bbw-ink-3-text, #5c6b80);
    text-decoration: none;
    padding: 6px 8px;
    border-radius: 8px;
    transition: color var(--bbw-transition-fast, .12s ease), background var(--bbw-transition-fast, .12s ease);
}
.bbw-shell .logout-link:hover { color: var(--bbw-down, #d90429); background: var(--bbw-down-soft, #fdeef1); }

/* ---- §3 Shell grid + sidebar column ----------------------------------------
 * Explicit placement: when a page stylesheet turns the sidebar into a fixed
 * drawer (facts/chatbot ≤980px), the fixed sidebar leaves grid flow and an
 * auto-placed content would land in the 222px first column — the squeezed
 * narrow-column bug. Pinning content to column 2 keeps it full-width. */
.bbw-shell .shell-body {
    display: grid;
    grid-template-columns: var(--bbw-sidebar, 222px) minmax(0, 1fr);
    flex: 1;
    min-height: 0;
    transition: grid-template-columns .22s ease;
}
.bbw-shell .shell-body > .side,
.bbw-shell .shell-body > sidebar.side { grid-column: 1; grid-row: 1; }
.bbw-shell .shell-body > main.content,
.bbw-shell .shell-body > .content { grid-column: 2; grid-row: 1; min-width: 0; }
body.sidebar-collapsed .bbw-shell { --bbw-sidebar: var(--bbw-sidebar-rail, 64px); }

.bbw-shell .side {
    background: var(--bbw-surface, #ffffff);
    border-right: 1px solid var(--bbw-border, #e2e7ee);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: calc(100vh - var(--bbw-topbar, 58px));
    /* In normal flow the sidebar is just the first grid column. It must not
     * out-stack the sticky header, or a scrolled page paints the nav column
     * over the brand. */
    z-index: 0;
}
/* Drawer pages fix the sidebar to the viewport at their own breakpoint; only
 * in that open state does it need to sit above the overlay and header. */
.bbw-shell .side.open,
body.sidebar-open .bbw-shell .side { z-index: var(--bbw-z-drawer, 120); }
.bbw-shell .side-scroll { flex: 1; overflow-y: auto; padding: 14px 0 10px; }

/* Nav leaves: identifiers (.nav-item / .active / ids / data-ms-*) are owned
 * by sidebar.php; the shell only unifies metrics + active treatment. */
.bbw-shell .nav-section-title {
    font-size: var(--bbw-text-micro, 10.5px);
    font-weight: 800;
    color: var(--bbw-ink-3-text, #5c6b80);
    padding: 10px 20px 7px;
    text-transform: uppercase;
    letter-spacing: .6px;
}
.bbw-shell .nav-item {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 20px;
    margin: 1px 0;
    color: var(--bbw-ink-2, #46556c);
    text-decoration: none;
    font-size: var(--bbw-text-label, 13px);
    font-weight: 700;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: background var(--bbw-transition-fast, .12s ease), color var(--bbw-transition-fast, .12s ease), border-color var(--bbw-transition-fast, .12s ease);
    white-space: nowrap;
    overflow: hidden;
}
.bbw-shell .nav-item:hover { background: var(--bbw-surface-2, #f7f9fb); color: var(--bbw-ink, #0b1526); }
.bbw-shell .nav-item.active,
.bbw-shell .nav-item[aria-current="page"] {
    background: var(--bbw-blue-soft, #eef3ff);
    color: var(--bbw-blue, #1e56e0);
    border-left-color: var(--bbw-blue, #1e56e0);
}
.bbw-shell .nav-item.nav-disabled { opacity: .65; cursor: default; }
body.sidebar-collapsed .bbw-shell .nav-item .nav-label,
body.sidebar-collapsed .bbw-shell .ms-group-label,
body.sidebar-collapsed .bbw-shell .side-account,
body.sidebar-collapsed .bbw-shell .side-telegram-label { display: none; }

/* ---- §4 Page container + headings + breadcrumbs ----------------------------- */
.bbw-shell .content {
    min-width: 0;
    max-width: var(--bbw-content-max, 1640px);
    width: 100%;
    box-sizing: border-box;
    margin: 0 auto;
    padding: 22px var(--bbw-content-pad, 26px) 40px;
}
.bbw-shell .page-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 2px 0 16px; }
.bbw-shell .page-title {
    margin: 0;
    font-size: var(--bbw-text-display, 24px);
    font-weight: 800;
    letter-spacing: -.2px;
    line-height: 1.25;
    color: var(--bbw-ink, #0b1526);
}
.bbw-shell .page-sub { font-size: var(--bbw-text-label, 13px); font-weight: 500; color: var(--bbw-ink-2, #46556c); margin-top: 2px; }

/* Breadcrumb slot (new, dormant): <nav class="bbw-crumbs" aria-label="Breadcrumb"> */
/* ---- §5 Persistent Ask-BBW floating action ---------------------------------- */
.bbw-ask-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: var(--bbw-z-toast, 300);
    display: flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, #f59e0b, #d97706);
    color: #fff;
    /* Part 5: white-on-amber is ~3.2:1 (below AA for small text). The gradient
       is a pre-existing brand element, so it stays; the shadow lifts glyph
       legibility until a rebrand lands. */
    text-shadow: 0 1px 2px rgba(0, 0, 0, .28);
    text-decoration: none;
    padding: 12px 20px;
    border-radius: var(--bbw-radius-pill, 999px);
    font-weight: 700;
    font-size: 13px;
    box-shadow: 0 4px 20px rgba(217, 119, 6, .4);
    transition: transform var(--bbw-transition-med, .18s ease), box-shadow var(--bbw-transition-med, .18s ease);
    cursor: pointer;
}
.bbw-ask-fab:hover { transform: translateY(-2px); box-shadow: 0 6px 28px rgba(217, 119, 6, .55); }
.bbw-ask-fab:focus-visible { outline: 3px solid var(--bbw-blue, #1e56e0); outline-offset: 3px; }
.bbw-ask-fab-icon { font-size: 18px; }
.bbw-ask-fab-label { white-space: nowrap; }

/* ---- §6 Mobile drawer (unified 900px) + overlay ------------------------------
 * #mobileOverlay / .sidebar-overlay are Prime-template-only IDs/classes; no
 * wp-admin or plugin-admin surface uses them. */
#mobileOverlay,
.sidebar-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--bbw-z-overlay, 98);
    background: rgba(11, 21, 38, .45);
    opacity: 0;
    visibility: hidden;
    transition: opacity .2s ease, visibility .2s ease;
}
#mobileOverlay.open,
.sidebar-overlay.open,
body.sidebar-open #mobileOverlay,
body.sidebar-open .sidebar-overlay { opacity: 1; visibility: visible; display: block; }

@media (max-width: 900px) {
    .bbw-shell .shell-body { display: block; }
    .bbw-shell .hamburger-btn { display: inline-flex; }
    /* STEP 6 regression fix: the base rule pins .top-header to a fixed 58px,
       which clipped multi-row mobile headers (search + account cluster
       overlapped the page title). Let the header wrap like the original. */
    .bbw-shell .top-header {
        height: auto;
        min-height: var(--bbw-topbar, 58px);
        flex-wrap: wrap;
        row-gap: 8px;
        padding-top: 8px;
        padding-bottom: 8px;
    }
    /* Six pages still render their own .app-shell with a copy of this header
       markup (screener, stock-analysis, technical-patterns, technical-levels,
       fii-dii, live-blog). They never matched the rule above, so at 375px the
       nowrap brand plus the account cluster measured 648px inside a shell with
       overflow-x:hidden and Logout was pushed off-screen entirely. */
    .app-shell .top-header {
        height: auto;
        min-height: var(--bbw-topbar, 58px);
        flex-wrap: wrap;
        row-gap: 8px;
        padding-top: 8px;
        padding-bottom: 8px;
    }
    .app-shell .user-status { margin-left: auto; }
    .bbw-shell .side {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        width: 284px;
        max-width: 86vw;
        z-index: var(--bbw-z-drawer, 96);
        transform: translateX(-102%);
        transition: transform .22s ease;
        min-height: 0;
        border-right: 1px solid var(--bbw-border, #e2e7ee);
        box-shadow: var(--bbw-shadow-lg, 0 12px 40px rgba(11, 21, 38, .18));
    }
    .bbw-shell .side.open,
    body.sidebar-open .bbw-shell .side { transform: translateX(0); }
    .bbw-shell .content { padding: 18px 16px 32px; }
    .bbw-shell .page-title { font-size: 21px; }
}

/* ---- §7 Motion + forced-colors ------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    .bbw-shell,
    .bbw-shell *,
    .bbw-shell *::before,
    .bbw-shell *::after,
    .bbw-ask-fab {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}
@media (forced-colors: active) {
    .bbw-shell .nav-item.active,
    .bbw-shell .nav-item[aria-current="page"] { border-left: 3px solid Highlight; }
    .bbw-shell .avatar-circle,
    .bbw-shell .brand .brand-mark { forced-color-adjust: none; }
}
