/* ====================================================================
   ALZAR LOGIC — MOBILE / PHONE MODE
   Activated via body.is-mobile (set by js/core/device-mode.js).

   Approach: CSS-only show/hide. JS only sets the flag and body class.
   No JS re-mounting — heavy modules that aren't reached stay dormant.

   Breakpoint mirrors device-mode.js: max-width 768px or pointer:coarse
   ==================================================================== */

/* ── Visibility Utilities ───────────────────────────────────────────── */
/* Default: mobile-only content is hidden, desktop content is visible  */
.mobile-only { display: none !important; }

body.is-mobile .mobile-only  { display: revert !important; }
body.is-mobile .desktop-only { display: none !important; }

/* ── Shell Override: full-width single-column layout ────────────────── */
body.is-mobile #app-sidebar  { display: none !important; }
body.is-mobile #title-bar    { display: none !important; }

body.is-mobile #app-shell {
    grid-template-columns: 1fr;
    height: 100dvh;           /* dynamic viewport height respects browser chrome */
}

/* Content area: 4px side margin, leave room for bottom nav */
body.is-mobile #app-content {
    padding: 0 4px 64px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Compact topbar — full width, safe-area aware */
body.is-mobile #app-topbar {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    padding: max(6px, env(safe-area-inset-top, 0px)) max(10px, env(safe-area-inset-right, 0px)) 8px max(10px, env(safe-area-inset-left, 0px));
    min-height: calc(52px + env(safe-area-inset-top, 0px));
    height: auto;
    position: sticky;
    top: 0;
    z-index: 200;
    background: linear-gradient(
        180deg,
        var(--color-bg-surface, #1a1a2e) 0%,
        var(--color-bg-base, #12121f) 100%
    );
    border-bottom: 1px solid var(--color-border, rgba(255, 255, 255, 0.08));
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
}

body.is-mobile #app-topbar.search-expanded .topbar-brand,
body.is-mobile #app-topbar.search-expanded .topbar-actions {
    opacity: 0;
    pointer-events: none;
    width: 0;
    min-width: 0;
    max-width: 0;
    overflow: hidden;
    padding: 0;
    margin: 0;
}

body.is-mobile #app-topbar.search-expanded .topbar-search-wrap {
    flex: 1 1 100%;
}

/* Brand — compact icon on phone; wordmark on wider mobile */
body.is-mobile .topbar-brand {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 0 0 auto;
    min-width: 36px;
    max-width: 36px;
    overflow: hidden;
    text-decoration: none;
    border-radius: var(--radius-ui);
    padding: 4px;
    margin: 0;
    transition: max-width 0.2s ease, opacity 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

body.is-mobile .topbar-brand:active {
    background: var(--color-active-dim, rgba(14, 165, 233, 0.12));
}

body.is-mobile .topbar-brand-icon {
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    display: block;
}

body.is-mobile .topbar-brand-text {
    display: none;
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--color-text, #fff);
    white-space: nowrap;
}

body.is-mobile .topbar-brand-accent { color: var(--color-active, #0ea5e9); }
body.is-mobile .topbar-brand-mark {
    font-size: 0.38em;
    vertical-align: super;
    color: var(--color-active, #0ea5e9);
    font-weight: 400;
    line-height: 0;
}

@media (min-width: 400px) {
    body.is-mobile .topbar-brand {
        max-width: 120px;
    }
    body.is-mobile .topbar-brand-text {
        display: inline;
    }
}

body.is-mobile .topbar-spacer { display: none; }

body.is-mobile .topbar-search-wrap {
    flex: 1 1 0;
    min-width: 0;
}

body.is-mobile #tb-global-search {
    width: 100%;
    max-width: none;
    min-width: 0;
}

body.is-mobile .tb-global-search-input {
    height: 40px;
    min-height: 40px;
    padding: 0 12px 0 36px;
    font-size: 15px;
    border-radius: var(--radius-ui);
    border: 1px solid var(--color-border, rgba(255, 255, 255, 0.12));
    background: var(--color-bg-base, rgba(0, 0, 0, 0.25));
    color: var(--color-text, #fff);
    -webkit-appearance: none;
    appearance: none;
}

body.is-mobile .tb-global-search-input:focus {
    border-color: var(--color-active, #0ea5e9);
    box-shadow: 0 0 0 3px var(--color-active-dim, rgba(14, 165, 233, 0.2));
    background: var(--color-bg-surface, rgba(255, 255, 255, 0.06));
}

body.is-mobile .tb-global-search-icon {
    left: 12px;
    font-size: 16px;
    color: var(--color-text-muted, rgba(255, 255, 255, 0.45));
}

body.is-mobile .tb-global-search-results {
    left: 0;
    right: 0;
    width: auto;
    max-height: min(60vh, 420px);
    overflow-y: auto;
    border-radius: var(--radius-ui);
    margin-top: 6px;
    box-shadow: var(--shadow-lg, 0 12px 40px rgba(0, 0, 0, 0.45));
}

/* Action cluster — bell, AI, avatar */
body.is-mobile .topbar-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 0 0 auto;
    flex-shrink: 0;
}

body.is-mobile .topbar-action-slot {
    position: relative;
    flex-shrink: 0;
}

body.is-mobile .topbar-icon-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-ui);
    border: 1px solid var(--color-border, rgba(255, 255, 255, 0.1));
    background: var(--color-bg-base, rgba(255, 255, 255, 0.04));
    color: var(--color-text-muted, #aaa);
    font-size: 18px;
    -webkit-tap-highlight-color: transparent;
}

body.is-mobile .topbar-icon-btn:active {
    background: var(--color-active-dim, rgba(14, 165, 233, 0.15));
    color: var(--color-active, #0ea5e9);
}

body.is-mobile .topbar-action-badge {
    position: absolute;
    top: 4px;
    right: 4px;
    min-width: 16px;
    height: 16px;
    font-size: 9px;
    line-height: 16px;
    padding: 0 4px;
    border-radius: var(--radius-ui);
}

body.is-mobile .topbar-ai-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    padding: 0;
    justify-content: center;
    border-radius: var(--radius-ui);
    font-size: 18px;
    box-shadow: 0 2px 10px rgba(59, 130, 246, 0.35);
}

body.is-mobile .topbar-ai-btn .tbr-ai-btn-label {
    display: none;
}

body.is-mobile .topbar-ai-btn i {
    font-size: 20px;
    margin: 0;
}

@media (min-width: 480px) {
    body.is-mobile .topbar-ai-btn {
        width: auto;
        min-width: 40px;
        padding: 0 12px;
        gap: 6px;
    }
    body.is-mobile .topbar-ai-btn .tbr-ai-btn-label {
        display: inline;
        font-size: 0.78rem;
    }
}

body.is-mobile .topbar-user {
    flex-shrink: 0;
    padding: 2px;
}

body.is-mobile .topbar-user .user-avatar {
    width: 36px;
    height: 36px;
    font-size: 14px;
    border: 2px solid var(--color-border, rgba(255, 255, 255, 0.12));
}

body.is-mobile .topbar-user .user-avatar img {
    width: 36px;
    height: 36px;
}

/* Desktop brand typography (shared with topbar refactor) */
.topbar-brand {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    text-decoration: none;
    cursor: pointer;
    color: inherit;
}

.topbar-brand-icon {
    height: 24px;
    width: auto;
    display: block;
}

.topbar-brand-text {
    font-weight: bold;
    font-size: 1.1rem;
    color: white;
}

.topbar-brand-accent { color: #0EA5E9; }

.topbar-brand-mark {
    font-size: 0.38em;
    vertical-align: super;
    color: #0EA5E9;
    font-weight: 400;
    line-height: 0;
    letter-spacing: 0;
}

.topbar-search-wrap {
    position: relative;
    flex-shrink: 1;
    min-width: 0;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* Stack view header on narrow screens */
body.is-mobile .view-header {
    flex-wrap: wrap;
    row-gap: 6px;
    padding: 8px 12px 6px;
    min-height: unset;
}
body.is-mobile .view-header .header-actions {
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
body.is-mobile .view-header .header-actions::-webkit-scrollbar { display: none; }
body.is-mobile .view-header .header-actions > * { flex-shrink: 0; }


/* ═══════════════════════════════════════════════════════════════════════
   BOTTOM NAVIGATION BAR
   ═══════════════════════════════════════════════════════════════════════ */
#mobile-bottom-nav {
    display: none;                        /* hidden until body.is-mobile */
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 500;
    height: 60px;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--color-bg-surface, #1a1a2e);
    border-top: 1px solid var(--color-border, rgba(255,255,255,0.08));
    align-items: stretch;
    justify-content: space-around;
}

body.is-mobile #mobile-bottom-nav { display: flex; }

.mobile-bnav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    flex: 1;
    min-width: 0;
    background: none;
    border: none;
    color: var(--color-text-muted, #888);
    font-family: var(--font-base, inherit);
    font-size: 10px;
    cursor: pointer;
    padding: 4px 2px;
    border-radius: 0;
    transition: color 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.mobile-bnav-item i {
    font-size: 22px;
    line-height: 1;
    flex-shrink: 0;
}
.mobile-bnav-item span {
    font-size: 9px;
    line-height: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60px;
}
.mobile-bnav-item.active { color: var(--color-primary, #0ea5e9); }
.mobile-bnav-item:active  {
    background: var(--color-bg-hover, rgba(255,255,255,0.06));
}


/* ── Page body title (shared across all mobile pages) ──────────────── */
.mob-page-title {
    padding: 10px 14px 9px;
    font-size: 17px;
    font-weight: 700;
    color: var(--color-text-main);
    letter-spacing: -0.01em;
    flex-shrink: 0;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg-base);
}

/* ── Workspace-Aware Bottom Nav Visibility ──────────────────────────── */
/* Default: hide ALL workspace-keyed items until workspace type is known */
.mobile-bnav-item[data-workspace] { display: none; }

/* "all" items always show (Dashboard, Settings) */
body.is-mobile .mobile-bnav-item[data-workspace~="all"] { display: flex; }

/* Service workspace */
body.is-mobile[data-workspace-type="service"]
    .mobile-bnav-item[data-workspace~="service"]          { display: flex; }

/* Manufacturing workspace */
body.is-mobile[data-workspace-type="manufacturing"]
    .mobile-bnav-item[data-workspace~="manufacturing"]    { display: flex; }

/* Agency */
body.is-mobile[data-workspace-type="agency"]
    .mobile-bnav-item[data-workspace~="agency"]           { display: flex; }

/* Agency HQ — same 5 fixed tabs as everyone else */
body.is-mobile[data-workspace-type="agency"]
    .mobile-bnav-item[data-workspace~="all"]              { display: flex; }

/* Standard / demo — show only "all" items */
body.is-mobile[data-workspace-type="standard"]
    .mobile-bnav-item[data-workspace~="standard"]         { display: flex; }
body.is-mobile[data-workspace-type="demo"]
    .mobile-bnav-item[data-workspace~="standard"]         { display: flex; }

/* Fallback: workspace type not yet set — show only "all" items */
body.is-mobile:not([data-workspace-type])
    .mobile-bnav-item[data-workspace~="all"]              { display: flex; }


/* ═══════════════════════════════════════════════════════════════════════
   MOBILE QUICK-ACTION BARS
   Horizontal scrollable chip row inside each page section.
   Only rendered on mobile via body.is-mobile (CSS-controlled).
   ═══════════════════════════════════════════════════════════════════════ */
.mobile-quickbar {
    display: none;
    gap: 8px;
    padding: 10px 12px;
    background: var(--color-bg-card, rgba(255,255,255,0.03));
    border-bottom: 1px solid var(--color-border, rgba(255,255,255,0.06));
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
    align-items: center;
}
.mobile-quickbar::-webkit-scrollbar { display: none; }

body.is-mobile .mobile-quickbar { display: flex; }

.mobile-qbtn {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    padding: 10px 14px;
    min-width: 68px;
    background: var(--color-bg-hover, rgba(255,255,255,0.06));
    border: 1px solid var(--color-border, rgba(255,255,255,0.08));
    border-radius: var(--radius-ui);
    color: var(--color-text-main, #e0e0e0);
    font-size: 11px;
    font-family: var(--font-base, inherit);
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.mobile-qbtn i {
    font-size: 20px;
    color: var(--color-primary, #0ea5e9);
}
.mobile-qbtn:active {
    background: rgba(14, 165, 233, 0.15);
    border-color: var(--color-primary, #0ea5e9);
}


/* ═══════════════════════════════════════════════════════════════════════
   DESKTOP-ONLY NOTICE
   Shown inside heavy modules when accessed from a mobile device.
   ═══════════════════════════════════════════════════════════════════════ */
.mobile-desktop-notice {
    display: none;
}
body.is-mobile .mobile-desktop-notice {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 40px 24px;
    text-align: center;
    color: var(--color-text-muted, #888);
}
.mobile-desktop-notice i {
    font-size: 48px;
    opacity: 0.5;
}
.mobile-desktop-notice p {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
    max-width: 280px;
}
.mobile-desktop-notice strong {
    color: var(--color-text-main, #ccc);
}


/* ═══════════════════════════════════════════════════════════════════════
   PER-WORKSPACE CONTENT RULES
   ═══════════════════════════════════════════════════════════════════════ */

/* CFO: hide the ledger table and predictor on mobile (keep dashboard summary) */
body.is-mobile #cfo-tab-ledger,
body.is-mobile #cfo-tab-predictor { display: none !important; }

/* Hide the CFO sub-nav tabs that lead to hidden panels */
body.is-mobile .nav-sub-item[data-view="ledger"],
body.is-mobile .nav-sub-item[data-view="predictor"] { display: none; }

/* Inventory table: hide the bulk-action header bar on mobile */
body.is-mobile #inventoryPage .alzar-controls-bar {
    flex-direction: column;
    align-items: stretch;
}

/* Production header: hide board-vs-detail toggle on very small screens */
@media (max-width: 420px) {
    #prod-view-toggle { display: none !important; }
}

/* Agency / SOG: mark certain sections as desktop-only via class */
body.is-mobile[data-workspace-type="agency"] #estimatorPage .view-header .header-actions > *:not(:first-child),
body.is-mobile[data-workspace-type="agency"] #inventoryPage { opacity: 0.4; pointer-events: none; }


/* ═══════════════════════════════════════════════════════════════════════
   GENERAL MOBILE POLISH
   ═══════════════════════════════════════════════════════════════════════ */

/* Slightly larger tap targets on mobile */
body.is-mobile .btn { min-height: 38px; }

/* Scrollable tables rather than overflow-hidden */
body.is-mobile .alzar-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Ensure modals don't overflow screen */
body.is-mobile .alzar-modal,
body.is-mobile .modal-container {
    max-width: 100vw !important;
    border-radius: var(--radius-ui) var(--radius-ui) 0 0 !important;
    position: fixed !important;
    bottom: 0 !important;
    top: auto !important;
    left: 0 !important;
    right: 0 !important;
    max-height: 85dvh !important;
    overflow-y: auto !important;
    margin: 0 !important;
}

/* ── Bottom nav badge overlay (used by MobileShell._updateBadges) ───── */
.msh-bnav-icon {
    position: relative;
    display: inline-block;
    font-size: 22px;
    line-height: 1;
}

.msh-bnav-badge {
    position: absolute;
    top: -3px;
    right: -5px;
    min-width: 14px;
    height: 14px;
    background: #ef4444;
    color: #fff;
    border-radius: 7px;
    font-size: 8px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 2px;
    pointer-events: none;
    line-height: 1;
}

.msh-bnav-badge:empty { display: none; }

/* ── Shared tap-to-call button (pipeline, schedule, reminders) ─── */
.mob-call-btn {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    min-width: 44px;
    border-radius: var(--radius-ui);
    border: 1px solid var(--color-border);
    background: var(--color-active-dim);
    color: var(--color-active);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    font-family: inherit;
}

.mob-call-btn:active {
    background: rgba(14, 165, 233, 0.25);
}

/* ── Shared post-call log sheet (MobileCallLog) ─────────────────── */
.mcl-sheet-bd {
    position: fixed;
    inset: 0;
    z-index: 9000;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: flex-end;
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.mcl-sheet {
    width: 100%;
    background: var(--color-bg-surface);
    border-radius: var(--radius-ui) var(--radius-ui) 0 0;
    padding: 20px 16px 24px;
    box-shadow: var(--shadow-lg);
}

.mcl-sheet-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-main);
    margin-bottom: 4px;
}

.mcl-sheet-sub {
    font-size: 13px;
    color: var(--color-text-muted);
    margin: 0 0 12px;
}

.mcl-sheet-input {
    width: 100%;
    box-sizing: border-box;
    min-height: 80px;
    padding: 10px 12px;
    border-radius: var(--radius-ui);
    border: 1px solid var(--color-border);
    background: var(--color-bg-base);
    color: var(--color-text-main);
    font-family: inherit;
    font-size: 15px;
    resize: vertical;
}

.mcl-sheet-actions {
    display: flex;
    gap: 10px;
    margin-top: 14px;
}

.mcl-sheet-cancel,
.mcl-sheet-save {
    flex: 1;
    min-height: 44px;
    border-radius: var(--radius-ui);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.mcl-sheet-cancel {
    border: 1px solid var(--color-border);
    background: transparent;
    color: var(--color-text-muted);
}

.mcl-sheet-save {
    border: none;
    background: var(--color-active);
    color: #fff;
}
