/* ──────────────────────────────────────────────────────────────────────
   EMI Dashboard — Mobile design system
   Activates at ≤ 768px (Bootstrap `md` breakpoint). Desktop ≥ 769px is
   untouched. Lifts the patterns proved out in main_mobile.html into
   shared CSS.
   ────────────────────────────────────────────────────────────────────── */

:root {
    --m-topbar-h: 52px;
    --m-tabbar-h: 64px;
    --m-safe-top: env(safe-area-inset-top);
    --m-safe-bot: env(safe-area-inset-bottom);
    --m-bg-mobile: #1E2B33;
    --m-bg-mobile-2: #243640;
    --m-surface-mobile: #1a2a32;
    --m-surface-strong-mobile: #213540;
}

@media (max-width: 768px) {
    /* ── Background ──────────────────────────────────────────────── */
    html[data-theme="dark"],
    html[data-theme="dark"] body { background: var(--m-bg-mobile) !important; }
    html:not([data-theme="dark"]) body { background: var(--m-bg-mobile) !important; color: #e9fbff !important; }

    body {
        padding-top: calc(var(--m-topbar-h) + var(--m-safe-top));
        padding-bottom: calc(var(--m-tabbar-h) + var(--m-safe-bot) + 16px);
    }

    /* ── Compact topbar ─────────────────────────────────────────── */
    .topbar {
        position: fixed !important;
        top: 0 !important; left: 0 !important; right: 0 !important;
        z-index: 50 !important;
        height: calc(var(--m-topbar-h) + var(--m-safe-top)) !important;
        min-height: 0 !important;
        margin: 0 !important;
        padding: var(--m-safe-top) 14px 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        gap: 4px !important;
        width: 100% !important;
        background: rgba(30, 43, 51, 0.85) !important;
        backdrop-filter: saturate(160%) blur(20px) !important;
        -webkit-backdrop-filter: saturate(160%) blur(20px) !important;
        border-bottom: 1px solid rgba(123, 224, 200, 0.12) !important;
        border-radius: 0 !important;
        box-shadow: none !important;
    }
    html:not([data-theme="dark"]) .topbar {
        background: rgba(255, 255, 255, 0.92) !important;
    }

    .topbar-brand { font-size: 0 !important; line-height: 0 !important; gap: 0 !important; min-width: 0; }
    .topbar-brand::before {
        content: "" !important;
        display: block !important;
        width: 81px !important;
        height: 22px !important;
        background: url("/assets/EMI_LOGO_DARKBACKGROUND_WEB.png") left center / contain no-repeat !important;
        flex: 0 0 auto !important;
    }
    html:not([data-theme="dark"]) .topbar-brand::before {
        background-image: url("/assets/EMI_LOGO_COLOR_WEB.png") !important;
    }
    .topbar-logo,
    .topbar-brand-copy {
        display: none !important;
    }

    /* hide the desktop horizontal nav — bottom tab bar replaces it */
    .topbar-nav { display: none !important; }

    /* hide the tablet-band hamburger + its backdrop — bottom tab bar replaces them */
    .topbar-hamburger,
    .topbar-mobile-backdrop { display: none !important; }
    body.topbar-nav-open { overflow: auto !important; }

    /* Hide ALL topbar action children; re-show only the items we explicitly
       keep on mobile (V16.1: Brain admin shortcut + notifications bell).
       The data-m-keep attributes are applied by mobile.js so we don't need
       :has() (still missing on older iOS Safari). */
    .topbar-actions { gap: 4px !important; padding: 0 !important; }
    .topbar-actions > * { display: none !important; }
    .topbar-actions > .topbar-menu:has(#notificationMenuButton),
    .topbar-actions > .topbar-menu[data-m-keep="notifications"],
    .topbar-actions > [data-m-keep="brain"] {
        display: inline-flex !important;
    }
    /* Brain shortcut starts hidden via .hidden; only admins see it after
       applyEntitlementChrome() runs. The .hidden class still wins because
       it's higher specificity than [data-m-keep]. */
    .topbar-actions > [data-m-keep="brain"].hidden { display: none !important; }

    /* notification button styling */
    .topbar-actions .icon-button,
    .topbar-actions .button {
        width: 44px !important; height: 44px !important;
        min-width: 44px !important; min-height: 44px !important;
        padding: 0 !important;
        border-radius: 12px !important;
        background: transparent !important;
        border: 0 !important;
        box-shadow: none !important;
        color: #e9fbff !important;
    }
    html:not([data-theme="dark"]) .topbar-actions .icon-button,
    html:not([data-theme="dark"]) .topbar-actions .button {
        color: #1b2b34 !important;
    }
    .topbar-actions .topbar-icon { font-size: 22px !important; }

    /* dropdown menus — anchor to viewport edges so they don't overflow */
    .topbar-dropdown {
        right: 12px !important;
        top: calc(var(--m-topbar-h) + var(--m-safe-top) + 6px) !important;
        max-width: calc(100vw - 24px) !important;
    }

    /* ── Shell + panels ─────────────────────────────────────────── */
    .shell {
        width: 100% !important;
        max-width: 560px !important;
        margin: 0 auto 16px !important;
        padding: 14px 14px 0 !important;
        gap: 14px !important;
    }
    .shell > section.panel,
    .shell > .panel {
        border-radius: 18px !important;
        padding: 16px !important;
    }

    /* section heads — tighten + uppercase the muted style */
    .panel .section-head h2,
    .panel > h2 {
        font-size: 15px !important;
        letter-spacing: -0.01em !important;
    }
    .strip-head, .tiny.strip-head { font-size: 10px !important; letter-spacing: 0.16em !important; }

    /* ── Stacked-table mode ─────────────────────────────────────────
       Every data table on mobile collapses into one card per row.
       Header text moves into a left-side label on each cell via
       data-label (populated by mobile.js shim from <thead>).
       Opt out per-table with data-m-table="scroll" (keeps horizontal
       scroll for tables where row-as-card doesn't fit).
       ───────────────────────────────────────────────────────────── */
    .panel .table-wrap { margin-left: 0; margin-right: 0; padding: 0; overflow: visible; }

    .panel table:not([data-m-table="scroll"]) {
        display: block;
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
        background: transparent !important;
        box-shadow: none !important;
        font-size: 13px;
    }
    .panel table:not([data-m-table="scroll"]) thead,
    .panel table:not([data-m-table="scroll"]) colgroup { display: none !important; }
    .panel table:not([data-m-table="scroll"]) tbody,
    .panel table:not([data-m-table="scroll"]) tfoot { display: block; }
    .panel table:not([data-m-table="scroll"]) tr {
        display: block;
        width: 100%;
        padding: 12px 14px;
        margin: 0 0 10px;
        border: 1px solid rgba(123, 224, 200, 0.14);
        border-radius: 14px;
        background: var(--m-bg-mobile-2);
        box-shadow: none;
    }
    html:not([data-theme="dark"]) .panel table:not([data-m-table="scroll"]) tr {
        background: #ffffff;
        border-color: rgba(27, 43, 52, 0.10);
    }
    .panel table:not([data-m-table="scroll"]) tr:last-child { margin-bottom: 0; }
    .panel table:not([data-m-table="scroll"]) td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px;
        padding: 6px 0;
        border: 0 !important;
        min-height: 0;
        line-height: 1.35;
        text-align: right;
        color: #e9fbff;
        word-break: break-word;
        overflow-wrap: anywhere;
    }
    html:not([data-theme="dark"]) .panel table:not([data-m-table="scroll"]) td { color: #1b2b34; }
    .panel table:not([data-m-table="scroll"]) td:first-child {
        padding-top: 0;
        padding-bottom: 10px;
        margin-bottom: 8px;
        border-bottom: 1px solid rgba(123, 224, 200, 0.10) !important;
        font-weight: 600;
        font-size: 14px;
        text-align: left;
    }
    html:not([data-theme="dark"]) .panel table:not([data-m-table="scroll"]) td:first-child {
        border-bottom-color: rgba(27, 43, 52, 0.10) !important;
    }
    .panel table:not([data-m-table="scroll"]) td::before {
        content: attr(data-label);
        flex: 0 0 42%;
        font-size: 10.5px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.14em;
        color: #a8c4cf;
        text-align: left;
        align-self: center;
    }
    html:not([data-theme="dark"]) .panel table:not([data-m-table="scroll"]) td::before { color: #557480; }
    .panel table:not([data-m-table="scroll"]) td:first-child::before,
    .panel table:not([data-m-table="scroll"]) td[data-label=""]::before,
    .panel table:not([data-m-table="scroll"]) td:not([data-label])::before { content: none; display: none; }

    /* Scroll mode: horizontal scroll fallback for dense tables that opt in */
    .panel table[data-m-table="scroll"] { font-size: 12px; }
    .panel .table-wrap:has(table[data-m-table="scroll"]) {
        margin-left: -16px;
        margin-right: -16px;
        padding: 0 16px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ── Bottom tab bar (injected by mobile.js) ─────────────────── */
    .m-tabbar {
        position: fixed;
        left: 0; right: 0; bottom: 0;
        z-index: 50;
        height: calc(var(--m-tabbar-h) + var(--m-safe-bot));
        padding-bottom: var(--m-safe-bot);
        background: rgba(30, 43, 51, 0.88);
        backdrop-filter: saturate(160%) blur(20px);
        -webkit-backdrop-filter: saturate(160%) blur(20px);
        border-top: 1px solid rgba(123, 224, 200, 0.12);
        display: grid;
        grid-template-columns: repeat(5, 1fr);
    }
    html:not([data-theme="dark"]) .m-tabbar { background: rgba(255, 255, 255, 0.92); }

    .m-tab {
        display: flex; flex-direction: column; align-items: center; justify-content: center;
        gap: 2px;
        color: #a8c4cf;
        font-size: 10px; font-weight: 500;
        min-height: 56px;
        padding: 6px 4px 4px;
        text-decoration: none;
        font-family: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
        -webkit-tap-highlight-color: transparent;
    }
    html:not([data-theme="dark"]) .m-tab { color: #557480; }
    .m-tab .material-symbols-rounded { font-size: 22px; }
    .m-tab.active { color: #7be0c8; }
    .m-tab.active .material-symbols-rounded { font-variation-settings: 'FILL' 1, 'wght' 500; }

    /* ── Floating filter FAB (injected by mobile.js) ────────────── */
    .m-fab {
        position: fixed;
        right: 16px;
        bottom: calc(var(--m-tabbar-h) + var(--m-safe-bot) + 16px);
        z-index: 40;
        width: 52px; height: 52px;
        border-radius: 999px;
        background: #4bb7a3;
        color: #0b1d22;
        display: grid; place-items: center;
        box-shadow: 0 12px 28px rgba(0, 0, 0, 0.40);
        border: 0;
        cursor: pointer;
        transition: transform 220ms cubic-bezier(0.32, 0.72, 0, 1), opacity 220ms ease;
        -webkit-tap-highlight-color: transparent;
    }
    .m-fab.hidden {
        transform: translateY(120%) scale(0.9);
        opacity: 0;
        pointer-events: none;
    }
    .m-fab:active { transform: scale(0.94); }
    .m-fab .material-symbols-rounded { font-size: 24px; }
    .m-fab-badge {
        position: absolute; top: -2px; right: -2px;
        min-width: 18px; height: 18px; padding: 0 5px;
        border-radius: 999px;
        background: #ff7e89;
        color: #fff;
        font-size: 10px; font-weight: 700;
        display: grid; place-items: center;
        border: 2px solid #1E2B33;
    }
    html:not([data-theme="dark"]) .m-fab-badge { border-color: #f4fbfd; }

    /* ── Sheets + backdrop ──────────────────────────────────────── */
    .m-backdrop {
        position: fixed; inset: 0;
        background: rgba(0, 0, 0, 0.55);
        opacity: 0;
        pointer-events: none;
        transition: opacity 220ms ease;
        z-index: 70;
    }
    .m-backdrop.open { opacity: 1; pointer-events: auto; }

    .m-sheet {
        position: fixed;
        left: 0; right: 0; bottom: 0;
        z-index: 80;
        background: var(--m-surface-mobile);
        border-top-left-radius: 24px;
        border-top-right-radius: 24px;
        border-top: 1px solid rgba(123, 224, 200, 0.22);
        box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.40);
        transform: translateY(100%);
        transition: transform 280ms cubic-bezier(0.32, 0.72, 0, 1);
        max-height: 88vh;
        overflow-y: auto;
        padding-bottom: calc(var(--m-safe-bot) + 16px);
        color: #e9fbff;
        font-family: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
    }
    html:not([data-theme="dark"]) .m-sheet { background: #ffffff; color: #1b2b34; }
    .m-sheet.open { transform: translateY(0); }

    .m-sheet-handle {
        width: 36px; height: 4px;
        background: rgba(123, 224, 200, 0.22);
        border-radius: 999px;
        margin: 8px auto 4px;
    }
    .m-sheet-head {
        display: flex; justify-content: space-between; align-items: center;
        padding: 10px 18px 6px;
    }
    .m-sheet-head h3 { margin: 0; font-size: 16px; font-weight: 600; }
    .m-sheet-close {
        width: 36px; height: 36px;
        border-radius: 999px;
        background: var(--m-bg-mobile-2);
        display: grid; place-items: center;
        color: #a8c4cf;
        border: 0;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }
    .m-sheet-body { padding: 8px 18px 16px; }
    .m-sheet-empty {
        padding: 24px 18px;
        text-align: center;
        color: #a8c4cf;
        font-size: 13px;
        line-height: 1.5;
    }

    /* generic chip-style filter rows for sheet content */
    .m-filter-row {
        display: flex; flex-direction: column; gap: 4px;
        padding: 12px 0;
        border-bottom: 1px solid rgba(123, 224, 200, 0.12);
    }
    .m-filter-row:last-child { border-bottom: 0; }
    .m-filter-row .label {
        font-size: 11px; font-weight: 600;
        text-transform: uppercase; letter-spacing: 0.14em;
        color: #a8c4cf;
    }
    .m-filter-options { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
    .m-filter-option {
        padding: 8px 14px;
        border-radius: 999px;
        background: var(--m-bg-mobile-2);
        border: 1px solid rgba(123, 224, 200, 0.12);
        font-size: 13px; font-weight: 500;
        color: #c9edf5;
        min-height: 44px;
        display: inline-flex; align-items: center;
        cursor: pointer;
    }
    .m-filter-option.active {
        background: rgba(123, 224, 200, 0.18);
        border-color: #4bb7a3;
        color: #7be0c8;
    }
}

/* Hide injected mobile chrome on desktop, even if mobile.js ran */
@media (min-width: 769px) {
    .m-tabbar, .m-fab, .m-backdrop, .m-sheet { display: none !important; }
    /* The Brain shortcut is mobile-only — desktop already has a Brain link
       in the top nav, so don't double up after a viewport resize. */
    .topbar-actions > [data-m-brain] { display: none !important; }
}
