/* ══════════════════════════════════════════════════════════════
   CashFlow — Mobile & Tablet UX Layer
   Loaded after main.css. Progressive enhancement for touch devices:
   phones (< 768px), tablets (768–1024px) and notched/standalone PWA.
   ══════════════════════════════════════════════════════════════ */

/* ── Tokens ────────────────────────────────────────────────── */
:root {
    --cf-safe-top: env(safe-area-inset-top, 0px);
    --cf-safe-bottom: env(safe-area-inset-bottom, 0px);
    --cf-safe-left: env(safe-area-inset-left, 0px);
    --cf-safe-right: env(safe-area-inset-right, 0px);

    /* Minimum comfortable touch target (WCAG 2.5.5 / platform HIG) */
    --cf-tap: 44px;
    --cf-tap-sm: 38px;

    --cf-mobile-nav-h: 56px;
}

/* ── Global touch ergonomics ───────────────────────────────── */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* Drawers and scroll panes should not chain their scroll to the page. */
.sidebar,
.offcanvas-body,
.modal-body,
.table-responsive {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

/* Locked while a drawer is open (set from mobile.js). */
body.cf-scroll-lock {
    overflow: hidden;
    touch-action: none;
}

/* Long account names / descriptions must never force a sideways scroll.
   `clip` rather than `hidden`: `hidden` would make <body> a scroll container
   and break position:sticky on the top bar. */
body {
    overflow-x: clip;
}

@media (hover: none) and (pointer: coarse) {
    a, button, .btn, [role="button"], .nav-link,
    .list-group-item-action, .page-link, label, summary {
        -webkit-tap-highlight-color: rgba(37, 99, 235, 0.12);
        touch-action: manipulation;
    }

    /* Grow every interactive control to a thumb-sized target. */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-height: var(--cf-tap);
    }
    .btn-sm { min-height: var(--cf-tap-sm); }
    .btn-lg { min-height: 52px; }
    .btn-link { min-height: var(--cf-tap-sm); }

    .dropdown-item {
        display: flex;
        align-items: center;
        min-height: var(--cf-tap);
    }
    .page-link {
        display: flex;
        align-items: center;
        justify-content: center;
        min-width: var(--cf-tap);
        min-height: var(--cf-tap);
    }
    .list-group-item-action { min-height: var(--cf-tap); }
    /* Icon-only controls in dense rows are otherwise ~22px wide. */
    .list-group-item .btn,
    .table td .btn,
    .card-header .btn { min-width: var(--cf-tap-sm); }
    .form-check-input,
    input[type="checkbox"],
    input[type="radio"] {
        width: 1.15rem;
        height: 1.15rem;
    }
    .form-check-input { margin-top: 0.15rem; }
    .btn-close {
        width: 1rem;
        height: 1rem;
        padding: 0.75rem;
    }
}

/* ── Forms: never trigger the iOS focus-zoom ───────────────── */
/* Safari zooms the viewport when a focused field is under 16px. */
@media (max-width: 1024px) {
    .form-control,
    .form-select,
    .form-control-sm,
    .form-select-sm,
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    input[type="date"],
    input[type="datetime-local"],
    input[type="month"],
    input[type="time"],
    textarea,
    select {
        font-size: 16px;
    }
    .form-control,
    .form-select {
        min-height: var(--cf-tap);
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }
    /* main.css scopes these as `.filter-bar .form-control`, so match it. */
    .filter-bar .form-control,
    .filter-bar .form-select,
    .input-group .form-control,
    .input-group .form-select {
        font-size: 16px;
        min-height: var(--cf-tap);
    }
    /* Date inputs render short on iOS without an explicit height. */
    input[type="date"],
    input[type="datetime-local"] {
        min-height: var(--cf-tap);
    }
}

/* ══════════════════════════════════════════════════════════════
   PHONES  (< 768px)
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 767.98px) {

    /* ── Shell ─────────────────────────────────────────────── */
    .main-content {
        padding-bottom: calc(var(--cf-mobile-nav-h) + var(--cf-safe-bottom) + 0.5rem) !important;
    }
    /* Backoffice and other shells render no bottom bar to clear. */
    .main-content.no-bottom-nav {
        padding-bottom: calc(var(--cf-safe-bottom) + 0.5rem) !important;
    }

    .top-navbar {
        padding: 0.6rem 0.9rem;
        padding-top: calc(0.6rem + var(--cf-safe-top));
        padding-left: calc(0.9rem + var(--cf-safe-left));
        padding-right: calc(0.9rem + var(--cf-safe-right));
        gap: 0.5rem;
    }
    /* The page title must shrink rather than shove the actions off-screen. */
    .top-navbar > div:first-child { min-width: 0; flex: 1 1 auto; }
    .top-navbar h5 {
        font-size: 1rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .top-navbar .breadcrumb { display: none; }

    .content-area {
        padding: 0.9rem;
        padding-left: calc(0.9rem + var(--cf-safe-left));
        padding-right: calc(0.9rem + var(--cf-safe-right));
    }

    /* ── Drawer sidebar ────────────────────────────────────── */
    .sidebar {
        z-index: 1056;
        width: min(84vw, 300px);
        padding-top: var(--cf-safe-top);
        padding-bottom: var(--cf-safe-bottom);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.25);
    }
    .sidebar .nav-link {
        padding: 0.85rem 1.25rem;
        font-size: 0.95rem;
    }
    .sidebar-close {
        color: rgba(255, 255, 255, 0.65);
        border: 0;
        background: none;
        padding: 0.25rem 0.5rem;
    }
    .sidebar-close:hover, .sidebar-close:focus { color: #fff; }
    /* The collapse control is desktop-only affordance. */
    .sidebar-toggle { display: none; }

    .sidebar-overlay { z-index: 1055; }

    /* ── Bottom navigation ─────────────────────────────────── */
    .mobile-nav {
        padding: 0.25rem 0 0;
        padding-bottom: var(--cf-safe-bottom);
        padding-left: var(--cf-safe-left);
        padding-right: var(--cf-safe-right);
        box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.06);
    }
    .mobile-nav a {
        min-height: var(--cf-mobile-nav-h);
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 0.1rem;
        padding: 0.3rem 0.15rem;
        border-radius: var(--cf-radius-sm);
    }
    .mobile-nav a i {
        margin-bottom: 0;
        font-size: 1.2rem;
    }
    .mobile-nav a:active { background: var(--cf-surface-hover); }
    /* Center "Add" as the primary action. */
    .mobile-nav a.mobile-nav-primary i {
        width: 34px;
        height: 34px;
        border-radius: 50%;
        background: var(--cf-primary);
        color: #fff !important;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.95rem;
        margin-bottom: 0;
        box-shadow: var(--cf-shadow-md);
    }

    /* ── Page headers stack instead of squeezing ───────────── */
    .page-header,
    .cf-page-actions {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem;
    }
    .cf-page-actions .btn,
    .page-header .btn { width: 100%; }

    /* ── Cards & density ───────────────────────────────────── */
    .card-header {
        padding: 0.75rem 0.9rem;
        gap: 0.5rem;
        flex-wrap: wrap;
    }
    .card-body { padding: 0.9rem; }
    .stat-card { padding: 0.9rem; }
    .stat-card .stat-value { font-size: 1.15rem; word-break: break-word; }
    .stat-card .stat-label { font-size: 0.68rem; letter-spacing: 0.03em; }
    .stat-card .stat-icon { width: 34px; height: 34px; font-size: 0.9rem; }

    .empty-state { padding: 2rem 1rem; }
    .empty-state-icon { width: 64px; height: 64px; }

    /* ── Quick actions ─────────────────────────────────────── */
    .cf-quick-actions { display: grid !important; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
    .cf-quick-actions .btn { width: 100%; }

    /* ── Sticky form action bar ────────────────────────────── */
    /* Keeps Save/Cancel reachable on long forms without scrolling. */
    .form-actions {
        position: sticky;
        /* Clear the fixed bottom navigation. */
        bottom: calc(var(--cf-mobile-nav-h) + var(--cf-safe-bottom));
        z-index: 5;
        display: flex;
        flex-direction: column-reverse;
        gap: 0.5rem;
        margin: 1rem -0.9rem -0.9rem;
        padding: 0.75rem 0.9rem;
        background: var(--cf-surface);
        border-top: 1px solid var(--cf-border);
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
    }
    .form-actions .btn { width: 100%; }

    /* ── Filters collapse behind a toggle ──────────────────── */
    .filter-bar { padding: 0.75rem 0.9rem; }
    .filter-bar .col-auto { flex: 1 1 100%; width: 100%; }
    .filter-bar .btn { width: 100%; }
    .filter-bar .form-label { margin-bottom: 0.25rem; }
    /* Toggle and panel only need separating once the panel is open. */
    .cf-filter-bar-collapsible .collapse.show,
    .cf-filter-bar-collapsible .collapsing { margin-top: 0.75rem; }

    /* ── Tables become stacked cards ─────────────────────────
       Each row turns into a labelled card so a six-column ledger
       stays readable at 360px instead of scrolling sideways. */
    .table-stack { border: 0; }
    .table-stack thead {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
    }
    .table-stack,
    .table-stack tbody,
    .table-stack tfoot,
    .table-stack td { display: block; width: 100%; }

    /* Rows read as separated records inside the card that already frames
       them, so they use dividers rather than a second nested border. */
    .table-stack tbody tr,
    .table-stack tfoot tr {
        display: flex;
        flex-direction: column;
        width: 100%;
        padding: 0.85rem 0.9rem;
        border-bottom: 1px solid var(--cf-border);
    }
    .table-stack tbody tr:last-child { border-bottom: 0; }
    /* Bootstrap paints row state as an inset shadow on each cell, which in
       card form tints the cells but not the card's own padding. */
    .table-stack > :not(caption) > * > * {
        background-color: transparent;
        box-shadow: none;
    }
    .table-stack.table-hover > tbody > tr:hover { background-color: var(--cf-surface-hover); }
    /* Totals row stays visually distinct from the records above it. */
    .table-stack tfoot tr {
        border-top: 2px solid var(--cf-border);
        border-bottom: 0;
        background: var(--cf-surface-hover);
    }

    .table-stack tbody td,
    .table-stack tfoot td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.75rem;
        padding: 0.3rem 0;
        border: 0;
        text-align: right;
        font-size: 0.875rem;
        min-height: 0;
    }
    .table-stack tbody td::before,
    .table-stack tfoot td::before {
        content: attr(data-label);
        flex: 0 0 auto;
        text-align: left;
        font-size: 0.7rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--cf-muted);
    }
    /* Cells with no header label (or an empty one) render full width. */
    .table-stack tbody td[data-label=""]::before,
    .table-stack tfoot td[data-label=""]::before { content: none; }
    .table-stack tbody td[data-label=""],
    .table-stack tfoot td[data-label=""] { justify-content: flex-end; }

    /* The title leads the card regardless of its desktop column position. */
    .table-stack tbody td[data-col-first],
    .table-stack tfoot td[data-col-first] {
        order: -1;
        justify-content: flex-start;
        text-align: left;
        font-size: 0.95rem;
        font-weight: 600;
        padding-bottom: 0.5rem;
        margin-bottom: 0.25rem;
        border-bottom: 1px solid var(--cf-border);
    }
    .table-stack tbody td[data-col-first]::before,
    .table-stack tfoot td[data-col-first]::before { content: none; }

    /* Action buttons close the card. */
    .table-stack tbody td[data-col="actions"] {
        order: 99;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 0.5rem;
        padding-top: 0.6rem;
        margin-top: 0.35rem;
        border-top: 1px solid var(--cf-border);
    }
    .table-stack tbody td[data-col="actions"]::before { content: none; }
    .table-stack tbody td[data-col="actions"] .btn {
        min-width: var(--cf-tap);
        min-height: var(--cf-tap);
    }

    /* A leading checkbox column is a selector, not a title. */
    .table-stack tbody td[data-cf-control] { justify-content: flex-start; }
    .table-stack tbody td[data-cf-control]::before { order: 1; margin-left: 0.5rem; }

    /* Empty-state / colspan rows keep their normal block flow. */
    .table-stack tbody td[colspan] {
        display: block;
        text-align: center;
        padding: 0;
    }
    .table-stack tbody td[colspan]::before { content: none; }
    .table-stack tbody tr.table-stack-plain {
        border: 0;
        padding: 0;
        margin: 0;
        background: none;
    }

    /* Tables that stay tabular still need comfortable scrolling. */
    .table-responsive { margin-bottom: 0; }
    .table:not(.table-stack) { font-size: 0.85rem; }

    /* ── Modals & offcanvas ────────────────────────────────── */
    /* Bottom sheets feel native and stay within thumb reach. */
    .modal-dialog:not(.modal-fullscreen) {
        margin: 0.5rem;
    }
    .offcanvas-bottom {
        border-radius: var(--cf-radius-lg) var(--cf-radius-lg) 0 0;
        padding-bottom: var(--cf-safe-bottom);
    }
    .offcanvas-bottom .offcanvas-body {
        padding-top: 0;
        padding-bottom: calc(var(--cf-mobile-nav-h) + var(--cf-safe-bottom) + 1rem);
    }
    /* Grab handle */
    .offcanvas-bottom .offcanvas-header::before {
        content: "";
        position: absolute;
        top: 0.5rem;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--cf-border);
    }
    .offcanvas-bottom .offcanvas-header {
        position: relative;
        padding-top: 1.25rem;
    }
    .offcanvas .list-group-item {
        padding-top: 0.85rem;
        padding-bottom: 0.85rem;
        font-size: 0.95rem;
    }

    /* ── Charts ────────────────────────────────────────────── */
    /* Shorter plots so a chart and the numbers under it share the fold. */
    .cf-chart { height: 200px; }
    .cf-chart-sm { height: 170px; }
    .cf-chart-lg { height: 230px; }
    .cf-chart-pie { max-width: 230px; }
    .cf-chart-pie-legend { max-width: 300px; }

    /* ── Misc ──────────────────────────────────────────────── */
    .pagination { flex-wrap: wrap; gap: 0.25rem; justify-content: center; }
    .alert { font-size: 0.9rem; }
    .badge { white-space: normal; }
}

/* Very narrow phones (iPhone SE and similar) */
@media (max-width: 379.98px) {
    .content-area { padding: 0.75rem; }
    .stat-card .stat-value { font-size: 1.05rem; }
    .cf-quick-actions { grid-template-columns: 1fr; }
    .mobile-nav a { font-size: 0.58rem; }
}

/* ══════════════════════════════════════════════════════════════
   TABLETS  (768px – 1024px)
   ══════════════════════════════════════════════════════════════ */
@media (min-width: 768px) and (max-width: 1024px) {
    .content-area {
        padding: 1.25rem;
        padding-left: calc(1.25rem + var(--cf-safe-left));
        padding-right: calc(1.25rem + var(--cf-safe-right));
    }
    .top-navbar {
        padding: 0.75rem 1.25rem;
        padding-top: calc(0.75rem + var(--cf-safe-top));
    }
    /* Reclaim horizontal space — a 250px rail is expensive at 768px. */
    .sidebar:not(.collapsed) { width: 220px; }
    .sidebar:not(.collapsed) + .main-content { margin-left: 220px; }

    .stat-card { padding: 1rem; }
    .stat-card .stat-value { font-size: 1.3rem; }
}

/* Tablet in landscape has room for the full desktop rail. */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
    .table { font-size: 0.875rem; }
}

/* ── Phone landscape: reclaim vertical space ───────────────── */
@media (max-width: 926px) and (orientation: landscape) and (pointer: coarse) {
    .top-navbar { padding-top: calc(0.4rem + var(--cf-safe-top)); padding-bottom: 0.4rem; }
    /* Landscape trades height for width; keep the plot within one screen. */
    .cf-chart { height: 175px; }
    .cf-chart-sm { height: 150px; }
    .cf-chart-lg { height: 200px; }
    .mobile-nav a { min-height: 46px; }
    .main-content {
        padding-bottom: calc(46px + var(--cf-safe-bottom)) !important;
    }
}

/* ── Standalone PWA: the status bar area is ours to paint ──── */
@media (display-mode: standalone) {
    .top-navbar {
        padding-top: calc(0.6rem + var(--cf-safe-top));
    }
}

/* ── Motion & contrast preferences ─────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}

/* Keyboard/AT users must keep a visible focus ring on touch builds too. */
:focus-visible {
    outline: 2px solid var(--cf-primary);
    outline-offset: 2px;
}
.mobile-nav a:focus-visible,
.sidebar .nav-link:focus-visible {
    outline-offset: -2px;
}
