/* =====================================================================
   Mobile & tablet optimisation
   Loaded after style.css / whatsapp.css. Desktop (>1024px) is untouched
   apart from the hidden tab bar.
   ===================================================================== */

.mobile-tabbar { display: none; }
.email-back { display: none; }

/* Grid children must be allowed to shrink, otherwise long text pushes cards off-screen. */
[style*="grid-template-columns"] > *,
.leads-grid > *, #follow-up-list > *, .dash-grid > *, .report-grid > *, .campaign-grid > *, .admin-grid > *, .meta-grid > * { min-width: 0; }

/* Sidebar shadow should only show while the drawer is open. */
@media (max-width: 1024px) {
    #sidebar { box-shadow: none; width: min(300px, 86vw); padding-bottom: env(safe-area-inset-bottom); }
    #sidebar.open { box-shadow: var(--shadow-float); }
    #sidebar ul li a.nav-link { padding: 11px 12px; font-size: 14.5px; }

    /* ---------- topbar ---------- */
    .topbar { position: sticky; top: 0; z-index: 900; padding-top: env(safe-area-inset-top); height: calc(var(--topbar-h) + env(safe-area-inset-top)); }

    /* ---------- bottom tab bar ---------- */
    .mobile-tabbar {
        display: grid; grid-template-columns: repeat(5, 1fr);
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 950;
        height: calc(60px + env(safe-area-inset-bottom));
        padding-bottom: env(safe-area-inset-bottom);
        background: rgba(255, 255, 255, 0.96);
        -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
        border-top: 1px solid var(--line);
        box-shadow: 0 -4px 18px rgba(42, 39, 83, 0.06);
    }
    .mobile-tabbar button {
        position: relative; border: none; background: none; cursor: pointer;
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
        color: var(--ink-3); font-size: 11px; font-weight: 700; font-family: inherit;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-tabbar button .ic { width: 22px; height: 22px; }
    .mobile-tabbar button.active { color: var(--brand-blue); }
    .mobile-tabbar button.active::before {
        content: ''; position: absolute; top: 0; left: 30%; right: 30%; height: 3px; border-radius: 0 0 3px 3px; background: var(--brand-blue);
    }
    .mobile-tabbar .tab-pip { display: none; position: absolute; top: 9px; left: calc(50% + 7px); width: 8px; height: 8px; border-radius: 50%; background: #25d366; border: 2px solid #fff; }
    body.wa-chat-open .mobile-tabbar { display: none; }

    .page { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }

    /* ---------- page heads & actions ---------- */
    .page-head { flex-direction: column; align-items: stretch; gap: 14px; }
    .page-actions { flex-wrap: wrap; gap: 8px; }
    .page-actions > select, .page-actions > input { flex: 1 1 100%; width: 100% !important; }

    /* ---------- kanban: one column at a time, swipe to the next ---------- */
    .kanban-board {
        grid-template-columns: repeat(5, 84vw);
        scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch;
        margin: 0 -16px; padding: 0 16px 12px; scroll-padding: 0 16px;
    }
    .kanban-col { scroll-snap-align: start; max-height: calc(100dvh - 260px); }

    /* ---------- email: list, then reader ---------- */
    .email-inbox-layout { display: flex !important; flex-direction: column; gap: 12px !important; height: auto !important; min-height: 0 !important; }
    .email-inbox-layout > .card:first-child { flex-direction: row !important; padding: 6px !important; gap: 6px !important; }
    .email-inbox-layout > .card:first-child .email-folder-btn { justify-content: center; padding: 10px 12px !important; }
    .email-inbox-layout #email-list-container { max-height: none; }
    .email-inbox-layout > #email-reader-card { display: none !important; }
    .email-inbox-layout.reading > .card:not(#email-reader-card) { display: none !important; }
    .email-inbox-layout.reading > #email-reader-card { display: flex !important; padding: 18px !important; }
    .email-back { display: inline-flex; align-self: flex-start; }
    .email-read-head { flex-direction: column; gap: 12px; }
    .email-read-head > div:last-child { align-items: flex-start !important; flex-direction: row-reverse !important; justify-content: space-between; width: 100%; }
    #read-body { overflow-wrap: anywhere; }

    /* ---------- WhatsApp: account for the tab bar ---------- */
    .wa-app { height: calc(var(--app-vh, 1vh) * 100 - var(--topbar-h) - 36px - 60px - env(safe-area-inset-bottom) - env(safe-area-inset-top)); min-height: 420px; }
    body.wa-chat-open .wa-app { height: calc(var(--app-vh, 1vh) * 100 - var(--topbar-h) - 36px - env(safe-area-inset-top)); }
    body[data-section="whatsapp-chat"] .page { padding-bottom: 16px; }
    body[data-section="whatsapp-chat"]:not(.wa-chat-open) .page { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
}

@media (max-width: 760px) {
    /* iOS zooms into inputs smaller than 16px — keep them at 16px on phones. */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea { font-size: 16px !important; }
    .wa-app textarea, .wa-app input { font-size: 16px !important; }

    /* Comfortable touch targets */
    .btn { min-height: 40px; }
    .btn-sm { min-height: 36px; }
    .close-btn { width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center; top: 12px; right: 12px; }

    /* Any inline multi-column grid stacks on phones */
    .page [style*="grid-template-columns"]:not(.keep-grid):not(.cal-grid),
    .modal [style*="grid-template-columns"]:not(.keep-grid) { grid-template-columns: minmax(0, 1fr) !important; }
    .report-grid { grid-template-columns: minmax(0, 1fr) 1fr !important; }

    /* Toolbars & tabs scroll sideways instead of squashing */
    #source-tabs { overflow-x: auto; flex-wrap: nowrap !important; scrollbar-width: none; margin-right: -16px; padding-right: 16px; }
    #source-tabs > * { flex-shrink: 0; }

    /* ---------- tables → labelled cards ---------- */
    table.m-cards thead { display: none; }
    table.m-cards, table.m-cards tbody { display: block; width: 100%; }
    table.m-cards tr { display: block; padding: 12px 14px; border-bottom: 1px solid var(--line); }
    table.m-cards tbody tr:last-child { border-bottom: none; }
    table.m-cards tbody tr:hover td { background: none; }
    table.m-cards td {
        display: flex; align-items: flex-start; justify-content: space-between; gap: 14px;
        padding: 5px 0 !important; border: none; font-size: 13.5px; text-align: right; min-width: 0;
        overflow-wrap: anywhere;
    }
    table.m-cards td::before {
        content: attr(data-label); flex: 0 0 auto; max-width: 42%;
        color: var(--ink-3); font-size: 12px; font-weight: 700; text-align: left; padding-top: 1px;
    }
    table.m-cards td[data-label=""]::before, table.m-cards td[colspan]::before { display: none; }
    table.m-cards td[colspan] { justify-content: center; text-align: center; padding: 18px 0 !important; }
    table.m-cards td:first-child { font-weight: 700; font-size: 14.5px; }
    table.m-cards td > div, table.m-cards td > span { min-width: 0; }
    table.m-cards td:last-child:has(button) { justify-content: flex-end; flex-wrap: wrap; padding-top: 10px !important; }
    table.m-cards td:last-child:has(button)::before { display: none; }
    .table-responsive:has(table.m-cards), .card:has(> table.m-cards) { overflow-x: visible; }

    /* ---------- follow-ups ---------- */
    .fu-foot { flex-wrap: wrap; }
    .fu-foot .btn { flex: 1 1 calc(50% - 6px); }
    .fu-foot .btn-icon, .fu-foot .btn:last-child:not(:has(span)) { flex: 0 0 44px; }
    .follow-up-contact-bar .btn { flex: 1; }

    /* ---------- content calendar ---------- */
    .cal-card { overflow-x: visible !important; padding: 14px !important; }
    .cal-nav { flex-wrap: wrap; gap: 10px; }
    .cal-nav-week { flex: 1 1 100%; justify-content: space-between; }
    .cal-nav-week h3 { flex: 1; text-align: center; font-size: 14.5px !important; }
    .cal-nav > .btn { flex: 1 1 100%; }
    .cal-grid { grid-template-columns: minmax(0, 1fr) !important; }

    /* ---------- modals: full-screen sheets with sticky header ---------- */
    .modal { padding: 0 !important; }
    .modal-content {
        max-width: none !important; width: 100% !important; min-height: 100%; margin: 0 !important;
        border-radius: 0 !important; padding: calc(18px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom)) !important;
    }
    .modal-content .form-actions, .modal-content .modal-actions { position: sticky; bottom: 0; background: var(--surface); padding-top: 12px; margin-top: 16px; }
    .modal-content .form-actions .btn, .modal-content .modal-actions .btn { flex: 1; }

    /* ---------- misc ---------- */
    .hero-band h1, .hero-band h2 { font-size: 22px; }
    .card { border-radius: 14px; }
    .lead-card .btn, .leads-grid .btn { min-height: 40px; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
}

@media (max-width: 380px) {
    .mobile-tabbar button span { font-size: 10px; }
    .report-grid { grid-template-columns: minmax(0, 1fr) !important; }
}

/* ---------- phone overflow fixes (Android app audit, Nov 2026) ----------
   Meetings: the two header buttons sat in a fixed 2-column grid wider than the screen.
   Instagram: the status + Disconnect/Refresh row did not wrap.
   Configurations: campaign-template cards had a 500px no-wrap preview line that made the
   whole page scroll sideways (and dragged the bottom tab bar off-screen). */
@media (max-width: 760px) {
    .page-head > * { min-width: 0; max-width: 100%; }
    .page-actions > .btn { flex: 1 1 auto; justify-content: center; min-width: 0; }
    .page-actions { grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr)) !important; }
    #instagram-chat .ig-page-actions { flex-wrap: wrap !important; gap: 8px; min-width: 0; }
    #instagram-chat .ig-page-actions .ig-status { flex: 1 1 100%; }
    .panel-title { flex-wrap: wrap; }
    .panel-title > div { flex: 1 1 0; min-width: 0; }
    .panel-title > .btn { flex: 1 1 100%; justify-content: center; }
    #campaign-templates-admin-list > .card { flex-wrap: wrap; gap: 10px; }
    #campaign-templates-admin-list > .card > div:first-child { flex: 1 1 100%; min-width: 0; }
    #campaign-templates-admin-list > .card > div:first-child > div { max-width: 100% !important; }
}
