/*
 * ESC-PWA-2 (2026-05-22) — Mobile + PWA enhancements layered on top of
 * Escrow's PMS design system.
 *
 * Loaded automatically on every page (via partials/pwa_head.php).
 *
 * Brand: navy #1F3A6F · accent gold #E5B04D
 */

: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);
    --escrow-brand: #1F3A6F;
    --escrow-brand-dark: #142A55;
    --escrow-accent: #E5B04D;
}

/* iOS safe-area inset support */
@supports (padding: env(safe-area-inset-top)) {
    body.app-shell .topbar,
    body.app-shell header,
    body.app-shell .pms-sidebar {
        padding-top: max(var(--safe-top), 8px);
    }
    body.app-shell .main-content,
    body.app-shell main {
        padding-left:  max(var(--safe-left), 16px);
        padding-right: max(var(--safe-right), 16px);
    }
}

/* Prevent iOS Safari zoom-on-focus by enforcing 16px+ input font */
@media (max-width: 767.98px) {
    body input[type="text"],
    body input[type="email"],
    body input[type="number"],
    body input[type="search"],
    body input[type="password"],
    body input[type="tel"],
    body input[type="url"],
    body input[type="date"],
    body input[type="datetime-local"],
    body input[type="time"],
    body textarea,
    body select {
        font-size: 16px !important;
    }
}

/* Touch targets ≥44px on small screens */
@media (max-width: 991.98px) {
    body .btn,
    body a.btn,
    body button:not(.btn-close):not(.pwa-install-close),
    body .nav-link,
    body .dropdown-item {
        min-height: 44px;
    }
    body .form-check {
        min-height: 32px;
        padding-top: 6px;
        padding-bottom: 6px;
    }
}

/* Mobile sidebar: collapse into a hamburger drawer.
   Escrow's main layout uses .sidebar.pms-sidebar inside .app-shell.
   On tablets+phones we hide it and provide a top toggle. */
@media (max-width: 991.98px) {
    body.app-shell .pms-sidebar {
        position: fixed;
        inset-inline-start: 0;
        inset-block-start: 0;
        height: 100vh;
        width: min(86vw, 320px);
        z-index: 1050;
        transform: translateX(-100%);
        transition: transform .22s ease;
        box-shadow: 0 18px 40px rgba(15,23,42,.18);
    }
    body.app-shell.is-rtl .pms-sidebar {
        inset-inline-start: auto;
        inset-inline-end: 0;
        transform: translateX(100%);
    }
    body.app-shell.pwa-nav-open .pms-sidebar {
        transform: translateX(0);
    }
    body.app-shell .pwa-nav-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        position: fixed;
        inset-block-start: max(8px, var(--safe-top));
        inset-inline-start: 8px;
        z-index: 1060;
        background: var(--escrow-brand);
        color: #fff;
        border: 0;
        border-radius: 10px;
        font-size: 22px;
        box-shadow: 0 8px 18px rgba(15,23,42,.16);
        cursor: pointer;
    }
    body.app-shell.is-rtl .pwa-nav-toggle {
        inset-inline-start: auto;
        inset-inline-end: 8px;
    }
    body.app-shell main,
    body.app-shell .main-content {
        margin-inline-start: 0 !important;
    }
}
body.app-shell .pwa-nav-toggle { display: none; }

body.app-shell.pwa-nav-open::after {
    content: '';
    position: fixed; inset: 0; z-index: 1040;
    background: rgba(15,23,42,.4);
}

/* Tables responsive */
@media (max-width: 767.98px) {
    body .table-responsive {
        position: relative;
    }
    body .table-responsive::after {
        content: "";
        position: absolute;
        inset-block: 0;
        inset-inline-end: 0;
        width: 30px;
        background: linear-gradient(to left, rgba(255,255,255,.95), transparent);
        pointer-events: none;
        opacity: .55;
    }
}
@media (max-width: 575.98px) {
    body .table-cards-on-mobile {
        display: block;
        width: 100%;
    }
    body .table-cards-on-mobile thead { display: none; }
    body .table-cards-on-mobile tbody,
    body .table-cards-on-mobile tr { display: block; width: 100%; }
    body .table-cards-on-mobile tr {
        border: 1px solid var(--color-border,#E4E7EC);
        border-radius: 14px;
        padding: 10px 12px;
        margin-bottom: 10px;
        background: #fff;
        box-shadow: 0 6px 18px rgba(15,23,42,.06);
    }
    body .table-cards-on-mobile td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 12px;
        padding: 8px 0;
        border: 0;
        border-bottom: 1px dashed rgba(228,231,236,.6);
        text-align: end;
    }
    body .table-cards-on-mobile td:last-child { border-bottom: 0; }
    body .table-cards-on-mobile td[data-label]::before {
        content: attr(data-label);
        font-weight: 700;
        color: var(--color-text-muted,#677078);
        font-size: .85rem;
        text-align: start;
        flex: 0 0 auto;
        max-width: 45%;
    }
}
@media (max-width: 991.98px) {
    body [data-priority="low"] { display: none !important; }
}
@media (max-width: 575.98px) {
    body [data-priority="medium"] { display: none !important; }
}

/* PWA standalone mode */
@media (display-mode: standalone) {
    body.app-shell {
        padding-bottom: var(--safe-bottom);
    }
    body .pwa-only-installed { display: inline-flex !important; }
    body .pwa-hide-when-installed { display: none !important; }
}
.pwa-only-installed { display: none; }

/* Install banner */
.pwa-install-banner {
    position: fixed;
    inset-block-end: calc(14px + var(--safe-bottom));
    inset-inline-end: 14px;
    z-index: 1080;
    display: none;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-radius: 14px;
    background: linear-gradient(135deg, var(--escrow-brand-dark), var(--escrow-brand));
    color: #fff;
    box-shadow: 0 14px 30px rgba(31,58,111,.32);
    font-weight: 700;
    font-size: .95rem;
    cursor: pointer;
    user-select: none;
    border: 0;
    max-width: calc(100vw - 28px);
}
html.pwa-installable .pwa-install-banner {
    display: inline-flex;
}
.pwa-install-banner:hover {
    transform: translateY(-1px);
}
.pwa-install-banner .pwa-install-icon { font-size: 1.2rem; line-height: 1; }
.pwa-install-banner .pwa-install-close {
    background: transparent; border: 0;
    color: rgba(255,255,255,.85);
    font-size: 1.1rem; padding: 0 4px; cursor: pointer;
}

/* Offline chip */
.pwa-offline-chip {
    position: fixed;
    inset-block-start: calc(8px + var(--safe-top));
    inset-inline-start: 50%;
    transform: translateX(-50%);
    z-index: 1090;
    display: none;
    padding: 6px 14px;
    border-radius: 999px;
    background: #f97316;
    color: #fff;
    font-weight: 700;
    font-size: .85rem;
    box-shadow: 0 8px 20px rgba(249,115,22,.35);
    pointer-events: none;
}
html.pwa-offline .pwa-offline-chip {
    display: inline-block;
}

/* Modals on phones */
@media (max-width: 575.98px) {
    .modal-dialog { margin: 8px; max-width: calc(100vw - 16px); }
    .modal-content { border-radius: 14px; }
    .modal-fullscreen-mobile .modal-dialog,
    .modal-fullscreen-mobile .modal-content {
        margin: 0; max-width: 100%; height: 100vh; border-radius: 0;
    }
}

/* Sticky bottom action bar pattern */
.pwa-fab-bar {
    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
}
@media (max-width: 767.98px) {
    .pwa-fab-bar {
        position: sticky;
        inset-block-end: 0;
        margin-inline: -14px;
        margin-block-start: 18px;
        padding: 12px 14px calc(12px + var(--safe-bottom));
        background: rgba(255,255,255,.97);
        backdrop-filter: blur(8px);
        border-top: 1px solid var(--color-border,#E4E7EC);
        box-shadow: 0 -8px 24px rgba(15,23,42,.08);
        z-index: 30;
    }
    .pwa-fab-bar .btn { flex: 1 1 auto; }
}

/* Print: hide PWA UI chrome */
@media print {
    .pwa-install-banner,
    .pwa-offline-chip,
    .pwa-fab-bar,
    .pwa-nav-toggle {
        display: none !important;
    }
}

/* Unread badge mirror */
[data-unread-count] {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: #dc2626;
    color: #fff;
    font-size: 11px;
    font-weight: 800;
    line-height: 1;
}
[data-unread-count]:not(.has-unread) { display: none; }

/* ===================================================================
 * ESC-PWA Mobile Shell (top bar + bottom nav)
 * Visible ONLY on phones / narrow tablets. Hidden on desktop.
 * =================================================================== */

/* Always-hidden on desktop */
.pwa-mtop, .pwa-mbottom { display: none; }

@media (max-width: 991.98px) {
    /* === Mobile top bar === */
    .pwa-mtop {
        position: fixed;
        inset-block-start: 0;
        inset-inline: 0;
        z-index: 1055;
        display: flex;
        align-items: center;
        gap: 10px;
        height: 56px;
        padding: 0 12px;
        padding-top: max(0px, var(--safe-top));
        padding-inline-start: max(12px, var(--safe-left));
        padding-inline-end:   max(12px, var(--safe-right));
        background: linear-gradient(135deg, #142A55, #1F3A6F);
        color: #fff;
        box-shadow: 0 4px 14px rgba(15,23,42,.18);
    }
    .pwa-mtop-btn {
        appearance: none;
        background: rgba(255,255,255,.12);
        border: 0;
        color: #fff;
        width: 40px;
        height: 40px;
        border-radius: 10px;
        font-size: 20px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        text-decoration: none;
        position: relative;
    }
    .pwa-mtop-btn:hover { background: rgba(255,255,255,.2); }
    .pwa-mtop-title {
        flex: 1 1 auto;
        font-weight: 700;
        font-size: 1.05rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .pwa-mtop-bell [data-unread-count] {
        position: absolute;
        top: 2px;
        inset-inline-end: 2px;
        min-width: 16px;
        height: 16px;
        font-size: 10px;
        background: #f59e0b;
        color: #1f2937;
    }

    /* === Mobile bottom navigation === */
    .pwa-mbottom {
        position: fixed;
        inset-block-end: 0;
        inset-inline: 0;
        z-index: 1055;
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        background: #fff;
        border-top: 1px solid #E4E7EC;
        box-shadow: 0 -6px 20px rgba(15,23,42,.10);
        padding-bottom: var(--safe-bottom);
    }
    .pwa-mb-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        padding: 8px 4px;
        text-decoration: none;
        color: #677078;
        font-size: 11px;
        font-weight: 700;
        position: relative;
        min-height: 56px;
    }
    .pwa-mb-icon { font-size: 22px; line-height: 1; }
    .pwa-mb-label {
        font-size: 11px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;
    }
    .pwa-mb-item.is-active {
        color: #1F3A6F;
    }
    .pwa-mb-item.is-active::before {
        content: '';
        position: absolute;
        inset-block-start: 0;
        inset-inline-start: 25%;
        inset-inline-end: 25%;
        height: 3px;
        background: #1F3A6F;
        border-radius: 0 0 3px 3px;
    }
    .pwa-mb-dot {
        position: absolute;
        top: 4px;
        inset-inline-end: calc(50% - 18px);
        min-width: 16px;
        height: 16px;
        font-size: 10px;
        background: #dc2626;
        color: #fff;
        border: 2px solid #fff;
    }

    /* === Push the original desktop topbar out of the way === */
    body.app-shell .pms-topbar,
    body.app-shell .topbar.pms-topbar {
        display: none !important;
    }

    /* === Pad the main content for the mobile top + bottom shells === */
    body.app-shell .main-area {
        margin-inline-start: 0 !important;
        padding-top: calc(56px + var(--safe-top));
        padding-bottom: calc(56px + var(--safe-bottom));
    }
    body.app-shell .content-wrap,
    body.app-shell main#main-content {
        padding-inline: 12px;
        padding-block: 12px;
    }

    /* Sidebar becomes a drawer (already handled earlier, reaffirmed here) */
    body.app-shell .pms-sidebar {
        position: fixed;
        inset-block-start: 0;
        inset-inline-start: 0;
        height: 100vh;
        width: min(86vw, 320px);
        z-index: 1060;
        transform: translateX(-100%);
        transition: transform .22s ease;
        background: #fff;
        box-shadow: 0 18px 40px rgba(15,23,42,.18);
        padding-top: calc(56px + var(--safe-top));
        overflow-y: auto;
    }
    body.app-shell.is-rtl .pms-sidebar {
        inset-inline-start: auto;
        inset-inline-end: 0;
        transform: translateX(100%);
    }
    body.app-shell.pwa-nav-open .pms-sidebar {
        transform: translateX(0);
    }
    body.app-shell.pwa-nav-open::after {
        content: '';
        position: fixed; inset: 0; z-index: 1058;
        background: rgba(15,23,42,.5);
    }

    /* Hide the floating circular toggle from pwa-touch.js — we use the
       header hamburger instead. */
    body.app-shell .pwa-nav-toggle {
        display: none !important;
    }

    /* Hide language toggle + user chip on tiny screens (still accessible
       via the sidebar drawer if they're in there). */
    @media (max-width: 575.98px) {
        body.app-shell .topbar-note { display: none; }
    }
}

/* On phones we want the install banner to sit ABOVE the bottom nav. */
@media (max-width: 991.98px) {
    .pwa-install-banner {
        inset-block-end: calc(70px + var(--safe-bottom)) !important;
    }
    #pwa-queue-badge {
        inset-block-end: calc(70px + var(--safe-bottom)) !important;
    }
    .pwa-offline-chip {
        inset-block-start: calc(60px + var(--safe-top)) !important;
    }
}
