/*
 * Phone bottom tab bar — Home · Fav1 · Ask · Fav2 · Menu.
 * Markup: components/phone_tab_bar.html; tab choice: _base/phone_tabs.py.
 *
 * Shown ONLY below 640px (same breakpoint as .table-stack). iPad and desktop
 * never see it. Hidden on pages with a fixed form action bar (that bar owns
 * the bottom edge) and while the on-screen keyboard is open (body.cs-kb-open,
 * set by the visualViewport handler in base.html). Never hidden on scroll.
 *
 * Body classes (set by the bar's own script, so nothing here needs :has):
 *   cs-has-tabbar    the bar is on this page
 *   cs-has-form-bar  the page has a .form-action-bar
 *   cs-kb-open       the keyboard is up
 *   cs-no-tabbar     page opt-out (body_class block in base.html)
 *
 * Stacking: z-index 1036 — above the chat panel (1035) so Ask can close it,
 * below the offcanvas backdrop (1040), modals (1055) and toasts (1090).
 */

.cs-tabbar { display: none; }

@media (max-width: 639.98px) {
    .cs-tabbar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1036;
        display: flex;
        align-items: stretch;
        height: calc(56px + env(safe-area-inset-bottom, 0px));
        padding: 0 4px env(safe-area-inset-bottom, 0px);
        background-color: var(--cs-surface, var(--bs-body-bg));
        border-top: 1px solid var(--bs-border-color);
    }

    /* Hide rules. :has() covers the form bar before any script runs; the
       class is the fallback for browsers without :has. Kept as a separate
       rule: an unsupported selector voids every selector in its list. */
    body:has(.form-action-bar) .cs-tabbar { display: none; }
    body.cs-has-form-bar .cs-tabbar,
    body.cs-kb-open .cs-tabbar,
    body.cs-no-tabbar .cs-tabbar { display: none; }

    /* Room for the bar at the true bottom of the page. Only while it shows,
       and never on a form page, where buttons.css reserves the action bar's
       height instead (the two never stack). !important because #app-main's
       .p-3 utility sets padding with !important. The keyboard state is left
       out on purpose: dropping the padding mid-typing would jump the page. */
    body.cs-has-tabbar:not(.cs-has-form-bar):not(.cs-no-tabbar) #app-main {
        padding-bottom: calc(56px + 1rem + env(safe-area-inset-bottom, 0px)) !important;
    }

    /* Toasts and the chat panel stop above the bar instead of under it. */
    body.cs-has-tabbar:not(.cs-has-form-bar):not(.cs-kb-open):not(.cs-no-tabbar) #toast-container {
        bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
    }
    body.cs-has-tabbar:not(.cs-has-form-bar):not(.cs-kb-open):not(.cs-no-tabbar) #chat-panel {
        bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
    }

    /* Menu is a tab now: the header logo stays as the brand (still opens the
       menu) but drops the caret that advertised it as one. */
    body.cs-has-tabbar:not(.cs-no-tabbar) .cs-menu-caret { display: none; }
}

/* ── Tabs ──────────────────────────────────────────────────────────────── */
.cs-tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 0;
    margin: 0;
    border: 0;
    background: none;
    color: var(--bs-secondary-color);
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.cs-tab:hover { color: var(--bs-body-color); }

.cs-tab-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    max-width: 100%;
    height: 28px;
    border-radius: 14px;
}
.cs-tab-pill .bi { font-size: 22px; line-height: 1; }

.cs-tab-label {
    max-width: 100%;
    font-size: 11px;
    font-weight: 500;
    line-height: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: clip;
}

/* Active: pill behind the icon in the pale accent, filled icon, label in the
   accent's text tone. The accent appears nowhere else on the bar. While the
   chat panel is open, Ask is the active tab and the page's tab steps back. */
.cs-tab.is-active { color: var(--bs-primary-text-emphasis); }
.cs-tab.is-active .cs-tab-pill { background-color: var(--bs-primary-bg-subtle); }
.cs-tabbar.chat-open .cs-tab.is-active:not(.cs-tab-ask) { color: var(--bs-secondary-color); }
.cs-tabbar.chat-open .cs-tab.is-active:not(.cs-tab-ask) .cs-tab-pill { background-color: transparent; }

.cs-tab:focus { outline: none; }
.cs-tab:focus-visible .cs-tab-pill {
    outline: 2px solid var(--cs-focus-ring, rgba(var(--bs-primary-rgb), 0.4));
    outline-offset: 2px;
}

@media (prefers-reduced-motion: no-preference) {
    .cs-tab-pill {
        transition: transform 120ms var(--cs-ease-out, ease-out), background-color 160ms ease-out;
    }
    .cs-tab:active .cs-tab-pill { transform: scale(0.9); }
    .cs-tab:active .cs-tab-label { opacity: 0.7; }
}

/* Dark: the bar is chrome, like the header and sidebar (Brand Slate). */
[data-bs-theme="dark"] .cs-tabbar {
    background-color: var(--cs-chrome, #16171c);
    border-top-color: var(--bs-border-color);
}

/* /dev/app-screens/ preview: render the bar in-flow at any width. */
.cs-tabbar.cs-tabbar--demo {
    position: static;
    display: flex;
    height: 56px;
    padding: 0 4px;
    background-color: var(--cs-surface, var(--bs-body-bg));
    border-top: 1px solid var(--bs-border-color);
}
[data-bs-theme="dark"] .cs-tabbar.cs-tabbar--demo { background-color: var(--cs-chrome, #16171c); }

@media print {
    .cs-tabbar { display: none !important; }
}
