/* =========================================
   MOBILE BOTTOM NAVIGATION BAR
   Mobile-only (≤991.98px). Hidden for customers via the PHP gate
   in templates/layout/bottom-nav.php.
========================================== */

/* ─── Layout variables ───────────
   --dmu-bottom-nav-height is read by other stylesheets (KB / memos
   floating toolbars) to lift their bottom-anchored chrome above the
   bar. Defaults to 0 on desktop where the bar is not rendered, and
   is overridden inside the mobile media query below.

   --dmu-bottom-nav-gap is the floating margin between the pill bar
   and the viewport edges (left, right, bottom). Same desktop/mobile
   semantics as --dmu-bottom-nav-height — 0 on desktop, real value on
   mobile. The bar is display:none on desktop so the gap value is
   harmless there; the consistent-with-height pattern keeps the
   --dmu-bottom-nav-clearance derived value (below) clean.

   --dmu-bottom-nav-clearance is the total vertical space the floating
   bar reserves at the viewport bottom on mobile (bar height + gap +
   safe-area inset). Read by:
     - body padding-bottom (so document content can scroll past the bar)
     - mail.css's full-height mobile wrap and columns (which can't rely
       on body padding-bottom because they're position-relative with
       explicit height — the padding doesn't shrink them)
     - any future fixed-height descendant of <body> that must clear
       the bar
   Resolves to env(safe-area-inset-bottom) only on desktop (height/gap
   both 0); to ~84px+ on mobile. */

:root {
    --dmu-bottom-nav-height: 0px;
    --dmu-bottom-nav-gap: 0px;
    --dmu-bottom-nav-clearance: calc(var(--dmu-bottom-nav-height) + var(--dmu-bottom-nav-gap) + env(safe-area-inset-bottom));
}

/* ─── Bar shell ───────────
   Hidden on desktop. The mobile media query below promotes display
   to flex. Width / position rules stay outside the media query so
   they apply when JS or a future caller toggles the bar at any size. */

.dmu-bottom-nav {
    display: none;
    position: fixed;
    /* Centering trick: left/right both 0 + width set + margin auto
       horizontally centers a fixed element. Using this instead of
       left/right gap values so width is the ONLY positioning
       property that changes when the bar collapses to a 134px
       handle — width is smoothly animatable from calc(100% - 24px)
       to 134px, while a transition between left/right gap values
       and `left: 50%; right: auto` would jerk (right:auto isn't
       animatable). See Feature B collapse animation below. */
    left: 0;
    right: 0;
    margin: 0 auto;
    width: calc(100% - 2 * var(--dmu-bottom-nav-gap));
    /* Bottom offset = floating gap + device safe-area. Safe-area moved
       OUT of the bar's padding (was: padding-bottom inside a taller
       bar) so the visual pill is always exactly 64px tall regardless
       of device chrome. */
    bottom: calc(var(--dmu-bottom-nav-gap) + env(safe-area-inset-bottom));
    z-index: 1045;

    height: 64px;

    /* Sidebar palette — bar reads as a horizontal extension of #nav.
       --background-navcolor-dark and --dmu-topbar-border are what
       css/layout/sidebar.css:14-15 uses for #nav.sidebar. The sidebar's
       "always dark" rule applies here too — bar is dark in both light
       and dark theme. */
    background: var(--background-navcolor-dark);
    border: 1px solid var(--dmu-topbar-border);
    border-radius: 9999px;
    /* Light-mode shadow. Dark-mode override below increases opacity
       so the (always-dark) pill stays distinct from a dark page bg. */
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    /* Horizontal inset keeps hover circles and active backgrounds
       clear of the pill's curved ends (32px half-circle radius at
       each end). Replaces the previous clip-path approach, which
       trapped the Account dropup menu. */
    padding: 0 16px;

    align-items: stretch;
    justify-content: space-around;

    /* ─── Feature B: scroll-aware collapse animation ───────────
       width / height / border-color / opacity all animate together
       on the .is-collapsed and .is-hidden state transitions below.
       iOS-style ease-out cubic-bezier (0.32, 0.72, 0, 1). */
    transition:
        width 280ms cubic-bezier(0.32, 0.72, 0, 1),
        height 280ms cubic-bezier(0.32, 0.72, 0, 1),
        border-color 280ms cubic-bezier(0.32, 0.72, 0, 1),
        opacity 280ms cubic-bezier(0.32, 0.72, 0, 1);
}

/* ─── Dark-mode shadow ───────────
   The bar's background is "always dark" (sidebar palette). In dark
   theme the page background is also dark, so the light-mode shadow
   doesn't register against it. Stronger shadow restores separation.
   Selector matches the codebase convention used throughout
   css/layout/header.css (e.g. line 42). */

html[data-color^="dark-"] .dmu-bottom-nav {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
}

/* ─── Lock body scroll while mail folder drawer is open ───────────
   .dmu-mail-col-inboxes is the mail folder drawer; gets
   .is-mobile-active when the user opens it on mobile (toggled by
   js/modules/mail/init.js's openMobileSidebar / closeMobileSidebar).
   Without this lock, scrolling on the drawer's backdrop scrolls the
   main page content behind it. Standard mobile-drawer pattern,
   missing from the existing drawer code. The drawer is mobile-only
   (MailModule.isMobile() guard in those handlers) so this rule
   only fires on mobile in practice; placed at top level so it
   applies regardless of viewport if the class ever gets set in a
   different context. */

body:has(.dmu-mail-col-inboxes.is-mobile-active) {
    overflow: hidden;
}

/* ─── Zero clearance while drawer hides the bar ───────────
   --dmu-bottom-nav-clearance means "space reserved for the floating
   bar." When the drawer-open hide rule (in the mobile @media block
   below) hides the bar, nothing needs reserving. Zeroing the variable
   here propagates to all 3 consumers:
     - body { padding-bottom: var(--dmu-bottom-nav-clearance) }
     - .dmu-mail-wrap { height: calc(100vh - var(--dmu-bottom-nav-clearance)) }
     - .dmu-mail-col-* { height: calc(100vh - var(--dmu-bottom-nav-clearance)) }
   Without this override, wrap/columns are ~84-104px shorter than
   viewport while drawer is open, producing a phantom empty band at
   the bottom (and the drawer column itself is cut off at the same
   boundary).

   TODO: same logic applies when the bar is in is-collapsed or
   is-hidden state from Feature B's scroll-aware behavior. Not
   handled here because the collapsed state's content-vs-handle
   overlap dynamics need separate consideration. Tracked separately. */

body:has(.dmu-mail-col-inboxes.is-mobile-active) {
    --dmu-bottom-nav-clearance: 0px;
}

/* ─── Tabs (flex equal-share) ─────────── */

.dmu-bottom-nav-tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    align-items: stretch;
    justify-content: stretch;
    /* No overflow rule here. The Account tab is .dmu-bottom-nav-tab.dropup;
       Bootstrap positions its .dropdown-menu via `position: absolute;
       bottom: 100%` — i.e. OUTSIDE the cell's content box. Putting
       overflow:hidden on the cell would clip the dropdown (regression
       from 14f8f593, fixed by moving the clip down to .dmu-bottom-nav-link
       — see below). The avatar-overflow problem this used to solve is
       handled at the link level instead. */
}

/* The inner link / button is the actual tap target. min-height 44px
   meets the iOS HIG tap-target minimum independently of the icon /
   label sizes. */

.dmu-bottom-nav-link {
    flex: 1 1 auto;
    /* min-height meets the iOS HIG 44px tap-target minimum. The bar
       itself is 64px tall, so ~10px of vertical breathing room
       above/below the tap target. Do not reduce. */
    min-height: 44px;
    padding: 0;
    background: transparent;
    border: 0;
    /* color propagates via currentColor to Lucide SVGs (which Lucide
       renders with stroke="currentColor"). Sidebar items use the same
       --dmu-sidebar-link token. */
    color: var(--dmu-sidebar-link);
    text-decoration: none;
    cursor: pointer;
    /* Clip link contents during the Feature B collapse animation.
       Link width shrinks from ~58px to ~22px as the bar collapses
       to 134px; without this, the fixed-size icon-wrap (40px) and
       its 28px Account avatar visually escape the link during the
       chaos window between ~50ms and ~150ms of the transition,
       before the opacity fade has fully hidden them. The dropdown
       menu (Account tab) is a SIBLING of this link inside the
       .dmu-bottom-nav-tab.dropup cell, not a descendant — so the
       clip stays scoped to the avatar/icon area without trapping
       the dropup menu. */
    overflow: hidden;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    transition: color 150ms ease-out;
    -webkit-tap-highlight-color: transparent;
}

.dmu-bottom-nav-link:focus-visible {
    outline: 2px solid var(--primary, #6f42c1);
    outline-offset: -2px;
}

/* ─── Icon wrap ───────────
   40x40 circle, host of:
   - the centered 22x22 icon (or 28x28 avatar on Account)
   - hover/press background fill (sidebar-style rgba(255,255,255,0.08))
   - active-state background fill (same color, persistent)
   - absolute-positioned badge dots (existing behavior)

   Wrap is the click target's visual hit area. Badges remain anchored
   to the wrap's top-right corner via their own position:absolute. */

.dmu-bottom-nav-icon-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    transition: background-color 150ms ease-out;
}

/* Lucide SVGs: lock to 22px and apply sidebar's icon opacity. The
   width/height !important defeats Lucide createIcons' baked attrs
   (18x18) and the global [data-lucide] rule in header.php (21x21).
   opacity matches sidebar.css:257 (.sidebar-link .icon { opacity: 0.8 }). */

.dmu-bottom-nav-icon-wrap [data-lucide],
.dmu-bottom-nav-icon-wrap svg {
    width: 22px !important;
    height: 22px !important;
    stroke-width: 1.75 !important;
    opacity: 0.8;
    transition: opacity 150ms ease-out;
}

/* ─── Account avatar ───────────
   Sized inline by getUserAvatar(); these rules are documentation +
   defensive override. Avatar lives inside the same 40x40 icon-wrap
   as Lucide icons; flex centering on the wrap centers it. */

.dmu-bottom-nav-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
}

/* ─── Active state ───────────
   Mirrors sidebar.css:278-294. Color flips to #f9fafb, icon opacity
   to 1, background pill on the icon-wrap (here circular at 50%
   instead of the sidebar's 0.8rem rectangle — bottom-nav cells are
   icon-only square-ish; circles fit the shape).

   Static fill, not a JS-animated indicator div. Sidebar uses an
   animator (sidebar-active-indicator.js) because its 20+ items
   reorder/collapse; bottom nav has 4-6 fixed tabs that never move,
   so the same visual outcome works without the machinery. */

.dmu-bottom-nav-tab.is-active .dmu-bottom-nav-link {
    color: #f9fafb;
}

.dmu-bottom-nav-tab.is-active .dmu-bottom-nav-icon-wrap {
    background: rgba(255, 255, 255, 0.08);
}

.dmu-bottom-nav-tab.is-active .dmu-bottom-nav-icon-wrap [data-lucide],
.dmu-bottom-nav-tab.is-active .dmu-bottom-nav-icon-wrap svg {
    opacity: 1;
}

/* ─── Hover / press states ───────────
   Same circle treatment, same color (#f9fafb matches sidebar
   .sidebar-link:hover at sidebar.css:275).

   Account tab opts out via [data-no-hover]: the avatar is its own
   indicator, a hover circle around it would be visual noise.

   Desktop:  @media (hover: hover) → :hover (skip on touch devices
             where :hover sticks after tap and would look broken).
   Mobile:   :active → transient press feedback. */

@media (hover: hover) {
    .dmu-bottom-nav-link:hover:not([data-no-hover]) .dmu-bottom-nav-icon-wrap {
        background: rgba(255, 255, 255, 0.08);
    }

    .dmu-bottom-nav-link:hover:not([data-no-hover]) {
        color: #f9fafb;
    }
}

.dmu-bottom-nav-link:active:not([data-no-hover]) .dmu-bottom-nav-icon-wrap {
    background: rgba(255, 255, 255, 0.08);
}

.dmu-bottom-nav-link:active:not([data-no-hover]) {
    color: #f9fafb;
}

/* ─── Account dropup positioning ───────────
   Bootstrap's .dropup .dropdown-menu defaults to bottom:100% on the
   parent .dropup. The parent here is the flex .dmu-bottom-nav-tab,
   which gives the menu a sensible anchor by default. Just nudge it
   a few pixels above the bar so it doesn't touch the icon. */

.dmu-bottom-nav-tab.dropup .dropdown-menu {
    margin-bottom: 8px;
}

/* ─── Feature B: collapsed handle state ───────────
   Bar shrinks to a 134×4px iOS-style home-indicator pill at the same
   bottom anchor. Width is the only positioning property that changes
   (centering via margin:0 auto stays constant — see shell rule above).
   Border drops to transparent because a 1px border on a 4px element
   would be 50% of the visible pill height and read as wrong.

   Children (.dmu-bottom-nav-tab) are faded out and made
   non-interactive so the collapsed bar is just the visible handle —
   tap-to-expand is captured by the ::before tap zone below. */

.dmu-bottom-nav.is-collapsed {
    width: 134px;
    height: 4px;
    border-color: transparent;
}

.dmu-bottom-nav.is-collapsed .dmu-bottom-nav-tab {
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease-out;
}

/* 44px tall tap target around the visible 4px handle (20px above +
   4px handle + 20px below = 44px). Pseudo-element clicks register
   on the parent .dmu-bottom-nav, so the JS click delegation can
   detect them via e.target === bar (or contains). */

.dmu-bottom-nav.is-collapsed::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: -20px;
    bottom: -20px;
    cursor: pointer;
}

/* ─── Feature B: hidden state (soft keyboard open) ───────────
   Bar is invisible AND uninteractive. Different from .is-collapsed:
   no handle is shown — the keyboard reclaims the full bottom area.
   Restored via focusout in JS. */

.dmu-bottom-nav.is-hidden {
    opacity: 0;
    pointer-events: none;
}

/* ─── In-page mobile breadcrumb (desktop hide) ───────────
   The breadcrumb chain is rendered by menu/menu.php both inside the
   topbar (desktop) and as a body-level sibling between #topbar and
   <main> (mobile). Hide the body-level copy on desktop — the topbar
   already carries the full breadcrumb chain there. The mobile media
   query below promotes display back to block. */

.dmu-mobile-breadcrumbs {
    display: none;
}

/* ─── Mobile activation ─────────── */

@media (max-width: 991.98px) {

    /* Promote the layout variables so KB/memos floating toolbars and
       any future bottom-anchored page chrome can offset above the bar.
       --dmu-bottom-nav-clearance derives from these via the unconditional
       :root definition above; no explicit override needed here. */
    :root {
        --dmu-bottom-nav-height: 64px;
        --dmu-bottom-nav-gap: 20px;
    }

    .dmu-bottom-nav {
        display: flex;
    }

    /* ─── Bottom-nav clearance ───────────
       Interacts with: css/layout/header.css:91-94 (.container padding-bottom: 0 !important).
       See templates/layout/bottom-nav.php for context.

       The whole document scrolls as one (no element on the page declares
       overflow-y). The .container rule above zeroes its own bottom padding
       so <main>'s min-height: calc(100vh - 65px) sets the layout floor;
       any padding we'd add to .container would be overridden. We add the
       bar's clearance to <body> instead — body has no existing padding rule,
       so this is purely additive.

       Clearance is consolidated into --dmu-bottom-nav-clearance (defined
       on :root above) so this rule and mail.css's full-height mobile wrap
       both consume the same expression. */
    body {
        padding-bottom: var(--dmu-bottom-nav-clearance);
    }

    /* ─── Hide chrome that the bar replaces ─────────── */

    /* Topbar hamburger — superseded by the bar's Menu tab.
       It currently has d-block d-lg-none (visible on mobile);
       this rule wins via specificity + !important. */
    #sidebar-mobile-toggle {
        display: none !important;
    }

    /* Floating chat launcher — chat is now invoked from the bar.
       The launcher button is still in the DOM (so bottom-nav.js
       can programmatically click it), only its visibility is
       suppressed. */
    .chat-launcher {
        display: none !important;
    }

    /* Topbar right-side action buttons that the bar duplicates.
       - Notifications: only button targeting #notifications-modal
       - Time tracker:  only button with id #dmuTimeBtn
       - Mail link:     stable .topbar-mail-link class on the <a> in
                        menu/menu.php (was .topbar-right > a; switched
                        so adding any future <a> to .topbar-right
                        doesn't silently get hidden too)
       - Divider:       cosmetic separator before the profile
       - Profile:       avatar + dropdown */
    #topbar [data-bs-target="#notifications-modal"],
    #topbar #dmuTimeBtn,
    #topbar .topbar-mail-link,
    #topbar .topbar-divider,
    #topbar .topbar-profile {
        display: none !important;
    }

    /* Trim the now-empty gap on the right side of the topbar so
       page-presence avatars (the only remaining right-side item)
       don't sit alone with extra padding. */
    #topbar .topbar-right {
        gap: 0;
    }

    /* ─── Hide bar while mail folder drawer is open ───────────
       Drawer (.dmu-mail-col-inboxes.is-mobile-active) sits at z:1040,
       below the bar's z:1045 — without this hide, the bar floats above
       the drawer's bottom edge and cuts off the folder list. Detect
       drawer-open via :has() since the toggle class lives on the
       drawer element, not body. :has() precedent already in this file
       at the .dmu-mobile-breadcrumbs:not(:has(...)) rule below. */
    body:has(.dmu-mail-col-inboxes.is-mobile-active) .dmu-bottom-nav {
        display: none;
    }

    /* ─── Hide mobile topbar ───────────
       The bottom nav and the in-page mobile breadcrumb take over the
       topbar's responsibilities on mobile. The element stays in the
       DOM (display:none, not removal) so JS that depends on it still
       works:
       - js/modules/mail/init.js reparents #mailSearchContainer in/out
       - js/base/page-presence.js reads/writes #dmu-page-presence
       Other rules above this point in the same media query (notifications
       button, mail link, profile, etc.) become redundant once #topbar
       itself is hidden, but they're harmless and document intent. */
    #topbar {
        display: none !important;
    }

    /* ─── Hide page-presence avatars ───────────
       Container hidden via CSS only. js/base/page-presence.js keeps
       fetching so the body-level concurrent-edit warning banner
       (#dmu-presence-banner) still appears on edit URLs. The avatars
       are out-of-scope on mobile until further design work. */
    #dmu-page-presence {
        display: none !important;
    }

    /* ─── Promote #dmu-tabs to the very top ───────────
       On desktop: top:65px (clears the topbar). On mobile: topbar is
       gone, so tabs sit at top:0 and become the user's only fixed
       chrome above the content. position:fixed is already set on
       desktop (css/layout/tabs.css:13-26) and carries over since the
       mobile media query in tabs.css only redefines left and z-index. */
    #dmu-tabs {
        top: 0 !important;
    }

    /* ─── <main> top margin reset ───────────
       Desktop sets `main { margin-top: 65px }` unconditionally
       (css/layout/header.css:419-431) to clear the 65px topbar. On
       mobile the topbar is hidden, so <main> needs no top margin.
       When SPA tabs are visible, the in-page mobile breadcrumb
       (between the fixed tabs bar and <main>) provides the
       clearance — see the .dmu-mobile-breadcrumbs rules below. */
    main {
        margin-top: 0 !important;
    }

    /* ─── In-page mobile breadcrumb (mobile show) ───────────
       Wrapper rendered by menu/menu.php as a body-level sibling
       between #topbar and <main>. Sits in document flow (not
       sticky), scrolls with the page. */
    .dmu-mobile-breadcrumbs {
        display: block;
        padding: 12px 16px;
        background: var(--dmu-topbar-bg);
        border-bottom: 1px solid var(--dmu-topbar-border);
        font-size: 13px;
    }

    /* ─── Empty-wrapper collapse ───────────
       The wrapper always renders (so the SPA loader can swap its
       contents on navigation, mirroring #dmu-page-actions). Hide
       it when it has no breadcrumb chain inside — pages without
       $_PAGE['hero']['bread'] would otherwise leave a 12px-padding +
       1px-border empty bar visible above content. */
    .dmu-mobile-breadcrumbs:not(:has(.dmu-bc-root)) {
        display: none;
    }

    /* ─── Mobile breadcrumb clearance for fixed #dmu-tabs ───────────
       #dmu-mobile-breadcrumbs is a body-level sibling rendered before
       <main>. When tabs are visible they overlay the top of the
       viewport (#dmu-tabs is position:fixed top:0); push the
       breadcrumb down by the tabs height so it isn't covered.

       Tabs hidden (default): no clearance — breadcrumb sits at the
       actual top of the document. */
    body.dmu-tabs-visible .dmu-mobile-breadcrumbs {
        margin-top: var(--dmu-tabs-h);
    }
}
