/*
 * Native-app feel on phones and in the installed app (PWA).
 * Classes on <html> come from layouts/partials/mobile-app-head.blade.php:
 *   .app-touch       touch device
 *   .app-standalone  opened from the home-screen / desktop icon
 * Phone-only rules use max-width: 767.98px. Desktop is untouched.
 */

/* ---------- App shell ---------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

html.app-touch {
    /* No 300ms tap delay, no double-tap zoom on controls; pinch zoom still works. */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* Our own pull-to-refresh replaces the browser's, and the page doesn't "bounce" like a website. */
    overscroll-behavior-y: none;
}
html.app-touch body { overscroll-behavior-y: none; }

/* Buttons and menus behave like app controls: no text selection or long-press callout.
   Table cells, inputs and page text stay selectable so numbers can still be copied. */
html.app-touch :is(.btn, button, .dash-topnav, .top-nav, .dropdown-menu, .nav-link, .badge, .app-back-btn, .app-table-toggle) {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
html.app-touch :is(.btn, button, a.dash-topnav__link, a.dash-topnav__sublink, .dropdown-item):active {
    opacity: .7;
    transition: opacity 0s;
}
html.app-touch :is(img, a img) { -webkit-touch-callout: none; }

/* iPhone notch / home indicator (needs viewport-fit=cover). */
html.app-touch body { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
html.app-touch .top-nav { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
html.app-touch .dashboard-workspace { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

/* Installed app: the install button has nothing left to do. */
html.app-standalone [data-pwa-install],
html.app-standalone .pwa-install-btn { display: none !important; }

/* ---------- Back button (installed app has no browser back button) ---------- */
.app-back-btn { display: none; }
html.app-standalone .app-back-btn.is-visible {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 48px;
    width: 48px;
    height: 55px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--ui-text, #172033);
}
.app-back-btn svg { width: 22px; height: 22px; }

/* ---------- Page-loading bar (no browser progress bar in the installed app) ---------- */
.app-progress {
    position: fixed;
    z-index: 2000;
    top: 0;
    inset-inline: 0;
    height: 3px;
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s;
}
.app-progress::before {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline-start: 0;
    width: 100%;
    background: var(--ui-accent-strong, #0f766e);
    transform-origin: right center;
    transform: scaleX(0);
}
[dir="ltr"] .app-progress::before { transform-origin: left center; }
.app-progress.is-active { opacity: 1; }
.app-progress.is-active::before { animation: app-progress 8s cubic-bezier(.1, .7, .3, 1) forwards; }
@keyframes app-progress {
    0% { transform: scaleX(0); }
    10% { transform: scaleX(.35); }
    100% { transform: scaleX(.92); }
}

/* ---------- Pull to refresh ---------- */
.app-ptr {
    position: fixed;
    z-index: 1050;
    top: var(--dash-header-h, 55px);
    left: 50%;
    width: 40px;
    height: 40px;
    margin-left: -20px;
    margin-top: -48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--ui-surface, #fff);
    color: var(--ui-accent-strong, #0f766e);
    box-shadow: 0 2px 10px rgb(15 23 42 / 22%);
    opacity: 0;
    pointer-events: none;
    will-change: transform, opacity;
}
.app-ptr svg { width: 22px; height: 22px; transition: transform .15s; }
.app-ptr.is-ready svg { transform: rotate(180deg); }
.app-ptr.is-releasing { transition: transform .25s ease, opacity .25s ease; }
.app-ptr.is-loading svg { animation: app-spin .8s linear infinite; }
@keyframes app-spin { to { transform: rotate(360deg); } }

/* ---------- Phones ---------- */
@media screen and (max-width: 767.98px) {
    /* A table wider than the screen and not inside a scroll box scrolls sideways (class set by mobileApp.js). */
    .dashboard-workspace table.app-table-scroll:not(.app-cards) {
        display: block;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .dashboard-workspace .table-responsive { -webkit-overflow-scrolling: touch; }

    /* Table / card switch above each table. */
    .app-table-toggle-bar { display: flex; justify-content: flex-end; margin: 0 0 8px; }
    .app-table-toggle {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        min-height: 34px;
        padding: 4px 12px;
        border: 1px solid var(--ui-border, #cbd5e1);
        border-radius: 999px;
        background: var(--ui-surface, #fff);
        color: var(--ui-text, #172033);
        font-size: 13px;
    }
    .app-table-toggle svg { width: 16px; height: 16px; }

    /* Each row becomes a card: "header: value" lines. */
    table.app-cards,
    table.app-cards > tbody,
    table.app-cards > tfoot,
    table.app-cards > tbody > tr,
    table.app-cards > tfoot > tr { display: block; width: 100%; height: auto !important; }
    table.app-cards { border: 0 !important; margin-bottom: 0; background: transparent !important; }
    table.app-cards > thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
        white-space: nowrap;
    }
    table.app-cards > tbody > tr,
    table.app-cards > tfoot > tr {
        margin: 0 0 10px;
        padding: 4px 14px;
        border: 1px solid var(--ui-border, #cbd5e1) !important;
        border-radius: 12px;
        background: var(--ui-surface, #fff) !important;
        box-shadow: 0 1px 3px rgb(15 23 42 / 7%);
    }
    table.app-cards > tfoot > tr { background: var(--ui-surface-2, #eef2f6) !important; }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th) {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 4px 12px;
        width: 100%;
        min-height: 40px;
        padding: 8px 0 !important;
        border: 0 !important;
        border-bottom: 1px dashed var(--ui-border-soft, #e2e8f0) !important;
        background: transparent !important;
        box-shadow: none !important;
        text-align: start !important;
        white-space: normal !important;
        overflow-wrap: anywhere;
        position: static !important;
    }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th):last-child { border-bottom: 0 !important; }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th)[data-app-label]::before {
        content: attr(data-app-label);
        flex: 0 1 42%;
        color: var(--ui-muted, #64748b);
        font-weight: 600;
        font-size: .9em;
    }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th)[data-app-label=""]::before { display: none; }
    /* Row number column: a small "#3" at the top of the card instead of its own line. */
    table.app-cards > tbody > tr > :is(td, th)[data-app-label="#"] {
        justify-content: flex-start;
        gap: 0;
        min-height: 0;
        padding: 6px 0 2px !important;
        border-bottom: 0 !important;
        color: var(--ui-muted, #64748b);
        font-size: .85em;
        font-weight: 700;
    }
    table.app-cards > tbody > tr > :is(td, th)[data-app-label="#"]::before { flex: none; font-size: 1em; }
    /* Value side keeps its content together and on the far side. */
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th) > * { max-width: 100%; }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th):empty { display: none; }
    /* "No data" rows / totals rows spanning all columns read as one centred line. */
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th).app-cell-full {
        justify-content: center;
        text-align: center !important;
    }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th).app-cell-actions { justify-content: flex-end; }
    table.app-cards > :is(tbody, tfoot) > tr > :is(td, th).app-cell-actions::before { margin-inline-end: auto; }
    table.app-cards .btn { min-height: 36px; }
    /* A card list needs no sideways scroll box or fixed height. */
    .app-cards-host { overflow: visible !important; max-height: none !important; }

    /* Dialogs open as bottom sheets. */
    .modal-dialog:not(.modal-fullscreen) {
        display: flex;
        align-items: flex-end;
        min-height: 100%;
        max-width: none;
        margin: 0;
    }
    .modal.fade .modal-dialog:not(.modal-fullscreen) { transform: translateY(40px); }
    .modal.show .modal-dialog:not(.modal-fullscreen) { transform: none; }
    .modal-dialog:not(.modal-fullscreen) > .modal-content {
        width: 100%;
        max-height: 92dvh;
        overflow-y: auto;
        border: 0;
        border-radius: 16px 16px 0 0;
        padding-bottom: env(safe-area-inset-bottom);
    }

    /* Comfortable touch targets. */
    .dashboard-workspace :is(.form-control, .form-select) { min-height: 42px; }
    .dropdown-item { padding-block: .6rem; }
}

@media print {
    .app-ptr, .app-progress, .app-back-btn, .app-table-toggle-bar { display: none !important; }
}

/* Keep flex/grid children inside the phone viewport; tables own their scrolling. */
@media (max-width: 767.98px) {
    html, body { max-width: 100%; overflow-x: clip; }
    .main-content, .dashboard-workspace, .dashboard-workspace .card, .dashboard-workspace .card-body,
    .dashboard-workspace [class*="col-"] { min-width: 0; max-width: 100%; }
    .top-nav { padding-top: env(safe-area-inset-top); height: calc(55px + env(safe-area-inset-top)) !important; box-sizing: border-box; }
    .top-nav > .col-12 { width: auto !important; min-width: 0; flex: 1; height: 55px !important; }
    .top-nav :is(.btn, .nc-bell-toggle, .app-back-btn) { min-width: 44px; width: 44px !important; flex-shrink: 0; }
    .top-nav .app-back-btn { flex-basis: 44px; }
    .dash-content-offset { padding-top: env(safe-area-inset-top); }
    .dashboard-workspace :is(.table-responsive, .table-scroll-y) { max-width: 100%; overflow-x: auto; }
    .dashboard-workspace :is(.btn, button, .form-control, .form-select), .app-table-toggle { min-height: 44px; }
    .dashboard-workspace :is(input, select, textarea) { font-size: max(16px, 1em); max-width: 100%; }
    .top-nav .dropdown-menu, .nc-bell-menu { width: min(350px, calc(100vw - 24px)) !important; max-width: calc(100vw - 24px); }
    .modal-content { overflow-wrap: anywhere; }
    .modal-body { min-height: 0; overflow: auto; }
    .aside { max-width: calc(100vw - 44px); padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
    .dash-topnav { max-height: calc(100dvh - 55px - env(safe-area-inset-top)); overflow-y: auto; overscroll-behavior: contain; }
    .top-nav [data-push-toggle][aria-pressed="true"] { color: var(--ui-accent-strong, #0f766e); }
}
.app-progress { z-index: 11000; }
.app-progress.is-complete::before { animation: none; transform: scaleX(1); }
@media (prefers-reduced-motion: reduce) {
    .app-progress, .app-loader-logo, .app-ptr, .modal.fade .modal-dialog { transition: none !important; animation: none !important; }
    .app-progress.is-active::before { animation: none; transform: scaleX(.65); }
}
@media (max-width: 767.98px) {
    body.dash-topnav-layout { --dash-topbar-h: calc(55px + env(safe-area-inset-top)); }
    body.dash-topnav-layout .dash-content-offset { padding-top: 0; }
    body.dash-topnav-layout.dash-topnav-collapsed .dash-topnav-menu-btn { flex-basis: 44px; width: 44px; }
    .top-nav .font-control { flex: 0 0 44px; min-width: 0; }
    .top-nav .font-control > summary { width: 44px; min-height: 44px; padding-inline: 0; text-align: center; }
    .top-nav .font-control > summary span { display: none; }
    .top-nav .font-control > summary::after { content: 'خط'; }
    .top-nav > .col-12 > .dropdown, .top-nav > .col-12 > .dropdown > [data-bs-toggle] { width: 44px !important; flex-shrink: 0; }
}
.push-feedback {
    position: fixed; z-index: 12000; top: calc(64px + env(safe-area-inset-top)); inset-inline: 12px;
    width: fit-content; max-width: calc(100vw - 24px); margin-inline: auto; padding: 12px 16px;
    background: var(--ui-surface, #fff); color: var(--ui-text, #172033);
    border: 1px solid var(--ui-border, #cbd5e1); border-radius: 12px; box-shadow: 0 4px 20px #0003;
}
.top-nav [data-push-toggle] { color: var(--ui-text, #172033); }
