/* ============================================
   BPC Buddy — Adaptive Mobile UI
   Completely separate mobile experience for < 992px
   Bottom tab bar + slim top bar + action sheet
   ============================================ */

/* === Safe Area Insets (iPhone Dynamic Island / notch) === */
:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --mobile-tabbar-h: 56px;
    /* Height of the desktop status-bar footer; mobile-tabbar lifts by this
       amount so it isn't clipped if the status bar happens to be visible. */
    --mobile-statusbar-h: 24px;
}


/* ═══════════════════════════════════════════════
   MOBILE TOP BAR  (< 992px)
   ═══════════════════════════════════════════════ */
.mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(0.4rem + var(--safe-top)) 0.75rem 0.4rem;
    position: sticky;
    top: 0;
    z-index: 1030;
}
.mobile-topbar-brand {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-weight: 600;
    font-size: 1.05rem;
    text-decoration: none;
    color: inherit;
}
.mobile-topbar-brand i {
    font-size: 1.15rem;
}
.mobile-topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}
.mobile-topbar-btn {
    background: none;
    border: none;
    font-size: 1.25rem;
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.15s;
    position: relative;
}


/* ═══════════════════════════════════════════════
   MOBILE BOTTOM TAB BAR  (iOS / Android style)
   ═══════════════════════════════════════════════ */
.mobile-tabbar {
    position: fixed;
    bottom: var(--mobile-statusbar-h);
    left: 0;
    right: 0;
    z-index: 1040;
    display: flex;
    align-items: stretch;
    justify-content: space-around;
    height: var(--mobile-tabbar-h);
    padding-bottom: var(--safe-bottom);
}
.mobile-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: none;
    border: none;
    font-size: 0.65rem;
    font-weight: 500;
    cursor: pointer;
    transition: color 0.15s;
    padding: 4px 0;
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
}
.mobile-tab i {
    font-size: 1.25rem;
    line-height: 1;
}
.mobile-tab:active {
    transform: scale(0.92);
}


/* ═══════════════════════════════════════════════
   MOBILE ACTION SHEET  (slide-up "More" menu)
   ═══════════════════════════════════════════════ */
.mobile-sheet-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1050;
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    transition: opacity 0.25s ease;
}
.mobile-sheet-backdrop.show {
    display: block;
    opacity: 1;
}

.mobile-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1051;
    border-radius: 16px 16px 0 0;
    max-height: 80vh;
    max-height: 80dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    padding-bottom: calc(0.5rem + var(--safe-bottom));
}
.mobile-sheet.show {
    transform: translateY(0);
}
.mobile-sheet-handle {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    margin: 10px auto 6px;
}
.mobile-sheet-body {
    padding: 0 1rem 0.5rem;
}
.mobile-sheet-section {
    margin-bottom: 0.75rem;
}
.mobile-sheet-section-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.5rem 0.75rem 0.25rem;
}
.mobile-sheet-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.75rem;
    border: none;
    background: none;
    font-size: 0.92rem;
    cursor: pointer;
    border-radius: 10px;
    transition: background 0.15s;
    min-height: 48px;
    -webkit-tap-highlight-color: transparent;
    text-align: left;
}
.mobile-sheet-item i {
    font-size: 1.15rem;
    width: 1.5rem;
    text-align: center;
    flex-shrink: 0;
}
.mobile-sheet-badge {
    font-size: .6rem;
    font-weight: 700;
    margin-left: auto;
}


/* ═══════════════════════════════════════════════
   MOBILE LAYOUT ADJUSTMENTS  (< 992px)
   ═══════════════════════════════════════════════ */
@media (max-width: 991.98px) {

    /* Main content: room for top bar + bottom tab bar + status bar */
    main {
        padding-bottom: calc(var(--mobile-tabbar-h) + var(--mobile-statusbar-h) + var(--safe-bottom) + 8px) !important;
    }

    /* Admin sidebar: slide-in drawer */
    .admin-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 1050;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
        width: 280px !important;
        min-width: 280px !important;
        height: 100vh;
        height: 100dvh;
    }
    .admin-sidebar.mobile-open {
        transform: translateX(0);
    }
    .admin-sidebar.sidebar-collapsed {
        width: 280px !important;
        min-width: 280px !important;
    }
    .admin-sidebar.sidebar-collapsed .sidebar-header span,
    .admin-sidebar.sidebar-collapsed .tree-nav,
    .admin-sidebar.sidebar-collapsed #sidebar-pdf-btn {
        display: unset;
    }

    /* Sidebar backdrop */
    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 1049;
        background: rgba(0, 0, 0, 0.5);
    }
    .sidebar-backdrop.show {
        display: block;
    }

    /* Admin content: full width */
    .admin-content {
        width: 100% !important;
        padding: 1rem 0.75rem calc(var(--mobile-tabbar-h) + var(--mobile-statusbar-h) + var(--safe-bottom) + 16px) 0.75rem;
    }
    .admin-layout {
        flex-direction: column;
        height: auto;
    }

    /* Dashboard filters: wrap vertically */
    .content-section .d-flex.align-items-center.justify-content-between.mb-3 {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.75rem;
    }
    .content-section .d-flex.align-items-end.gap-3 {
        flex-wrap: wrap;
        gap: 0.5rem !important;
        width: 100%;
    }
    .content-section .d-flex.align-items-end.gap-3 > div {
        flex: 1 1 auto;
        min-width: 120px;
    }
}


/* ═══════════════════════════════════════════════
   PHONE SPECIFICS  (< 768px)
   ═══════════════════════════════════════════════ */
@media (max-width: 767.98px) {

    /* Touch targets */
    .btn-sm {
        min-height: 38px;
        padding: 0.4rem 0.75rem;
    }
    .form-select-sm, .form-control-sm {
        min-height: 38px;
        font-size: 1rem; /* >= 16px prevents iOS auto-zoom */
    }
    .form-control, .form-select {
        font-size: 1rem;
    }
    .dropdown-item {
        padding: 0.6rem 1rem;
        min-height: 44px;
        display: flex;
        align-items: center;
    }
    .tree-child {
        padding: 0.65rem 1rem 0.65rem 2rem;
        min-height: 44px;
    }
    .tree-toggle {
        padding: 0.65rem 1rem;
        min-height: 44px;
    }

    /* Dashboard cards: force single column */
    .row.g-3 > [class*="col-md"],
    .row.g-3 > [class*="col-lg"] {
        flex: 0 0 100%;
        max-width: 100%;
    }

    /* Content section: reduce padding */
    .content-section {
        padding: 1rem 0.75rem;
    }
    .card-body {
        padding: 0.75rem;
    }

    /* Auth: full width */
    .auth-card {
        max-width: 100%;
        padding: 1.5rem;
        margin: 0 0.5rem;
    }
    .auth-container {
        padding: 1rem 0.5rem;
    }

    /* Modals: near full-screen */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100vw - 1rem);
    }
    .modal-dialog.modal-lg {
        max-width: calc(100vw - 1rem);
    }
    .modal-body {
        max-height: calc(100vh - 180px);
        max-height: calc(100dvh - 180px);
        overflow-y: auto;
    }

    /* Charts */
    .card canvas {
        max-height: 220px !important;
    }

    /* Popover & Tutorial */
    .sidebar-popover {
        max-width: calc(100vw - 2rem);
    }
    #tutorial-tooltip {
        max-width: calc(100vw - 2rem);
        min-width: unset;
    }

    /* Heading sizing */
    .dashboard-welcome h4,
    .content-section h4 {
        font-size: 1.1rem;
    }
}


/* ═══════════════════════════════════════════════
   SMALL PHONE  (< 480px — iPhone SE etc.)
   ═══════════════════════════════════════════════ */
@media (max-width: 480px) {
    .mobile-topbar-brand span {
        font-size: 0.9rem;
    }
    .content-section {
        padding: 0.75rem 0.5rem;
        border-radius: 4px;
    }
    .dashboard-container {
        padding: 0.5rem;
    }
    .card-body h5 {
        font-size: 1rem;
    }

    /* Team chat launcher: lift above the mobile tab bar so it isn't hidden. */
    .chat-launcher {
        bottom: calc(var(--mobile-tabbar-h) + var(--mobile-statusbar-h) + var(--safe-bottom) + 12px) !important;
        left: 12px !important;
    }

    /* Mobile sidebar "Menu" pill: stack above the team chat launcher so the
       two no longer overlap in the lower-left corner. */
    .mobile-sidebar-toggle {
        bottom: calc(var(--mobile-tabbar-h) + var(--mobile-statusbar-h) + var(--safe-bottom) + 64px) !important;
    }
}


/* ═══════════════════════════════════════════════
   DESKTOP OVERRIDES  (>= 992px)
   Hide all mobile-only elements completely
   ═══════════════════════════════════════════════ */
@media (min-width: 992px) {
    .mobile-topbar,
    .mobile-tabbar,
    .mobile-sheet,
    .mobile-sheet-backdrop,
    .sidebar-backdrop,
    .mobile-sidebar-toggle {
        display: none !important;
    }
}
