/* ============================================================
   NIMBLE ACCOUNTS — MOBILE LAYER (phones & small tablets)
   Everything here sits inside media queries, so desktop is untouched.
   Tables become labelled cards (labels added by /js/mobile.js).
   ============================================================ */

@media (max-width: 1023px) {
    /* the desktop "collapse sidebar" button must never show next to the mobile menu button */
    .topbar-collapse-btn { display: none !important; }
    /* never let one wide element make the whole page scroll sideways */
    body { overflow-x: hidden; }
    img, video, canvas, svg { max-width: 100%; }
    .content { max-width: 100vw; }
    .sidebar { max-width: 86vw; box-shadow: none !important; }
    .sidebar.mobile-open { box-shadow: 4px 0 24px rgba(0,0,0,.25) !important; }
}

@media (max-width: 767px) {

    /* ── Top bar ─────────────────────────────────────────── */
    .topbar { gap: 8px; }
    .topbar > div:first-child { min-width: 0; flex: 1 1 auto; }
    .topbar-right { gap: 6px; flex: 0 1 auto; min-width: 0; }
    .topbar-user-pill { padding: 3px !important; }
    .company-switcher-btn { padding: 4px 8px !important; gap: 6px; }
    .company-switcher-btn .company-name { display: none; }   /* the company name is shown under the page title instead */
    .company-dropdown { right: 0; left: auto; max-width: calc(100vw - 24px); }

    /* sign-in page: the card fills the screen width */
    .content.content-guest-main { padding: 0 !important; }
    .content-guest-main .min-h-screen { padding: 16px 12px !important; justify-content: center; }

    /* ── Content & cards ─────────────────────────────────── */
    .content { padding-left: 10px !important; padding-right: 10px !important; }
    .card, .li-page { padding: .8rem !important; }
    h1, .li-title, .fc-title { word-break: break-word; }

    /* page header: title above, buttons wrap underneath */
    .li-header, .fc-header { flex-wrap: wrap; }
    .li-actions, .fc-header-right { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
    .li-actions > *, .fc-header-right > * { flex: 1 1 calc(50% - 8px); min-width: 120px; justify-content: center; text-align: center; }
    .li-actions form { display: flex; }
    .li-actions form > button { flex: 1; }

    /* ── Forms ───────────────────────────────────────────── */
    .input, .select, .textarea, .fc-input, .fc-select, input[type=text], input[type=number], input[type=date],
    input[type=email], input[type=password], input[type=search], input[type=tel], input[type=url], select, textarea {
        max-width: 100%; box-sizing: border-box;
    }
    .fc-grid-2, .fc-grid-3, .fc-grid-4 { grid-template-columns: 1fr !important; }
    .fc-section { padding: .85rem !important; }
    .fc-action-bar { flex-wrap: wrap; gap: 8px; }
    .fc-action-bar > * { flex: 1 1 140px; justify-content: center; }
    .filter-grid-4 { grid-template-columns: 1fr !important; }
    form[style*="display:flex"], form[style*="display: flex"] { flex-wrap: wrap; }

    /* inline-styled grids (used all over the app) collapse to 1–2 columns */
    [style*="grid-template-columns"]:not([style*="auto-fit"]):not([style*="auto-fill"]) {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)) !important;
    }
    [style*="grid-column"], .fc-span-2, .fc-span-3 { grid-column: 1 / -1 !important; }
    [style*="min-width:3"], [style*="min-width: 3"], [style*="min-width:4"], [style*="min-width: 4"] { min-width: 0 !important; }
    [style*="width:2"], [style*="width: 2"] { max-width: 100%; }

    /* split layouts (e.g. form left / summary right) stack */
    .fc-layout, .two-col, .split { grid-template-columns: 1fr !important; flex-direction: column !important; }

    /* ── Modals ──────────────────────────────────────────── */
    .modal { padding: .5rem; align-items: flex-end; }
    .modal-content { width: 100% !important; max-width: 100% !important; max-height: 92vh; overflow-y: auto; border-radius: 16px 16px 0 0; padding: 1.1rem; }

    /* ── Tables: scroll-wrapper reset, then cards ────────── */
    .table-wrap, .fc-table-wrap, .table-responsive { overflow-x: visible; border: 0; background: transparent; box-shadow: none; }
    table { min-width: 0 !important; max-width: 100%; }
    table.m-scroll { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; min-width: 0 !important; }
    table.m-scroll th, table.m-scroll td { white-space: nowrap; }
    table:not(.m-cards):not(.m-scroll) th, table:not(.m-cards):not(.m-scroll) td { word-break: break-word; }

    table.m-cards, table.m-cards tbody, table.m-cards tfoot, table.m-cards tr, table.m-cards td { display: block; width: 100%; box-sizing: border-box; }
    table.m-cards thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

    table.m-cards tbody tr {
        display: flex; flex-wrap: wrap; align-items: flex-start;
        border: 1px solid #e2e8f0; border-radius: 12px; margin: 0 0 10px; background: #fff;
        overflow: hidden; box-shadow: 0 1px 2px rgba(15,23,42,.05);
    }
    table.m-cards tbody tr:nth-child(even) { background: #fff; }
    table.m-cards td {
        position: relative; flex: 0 0 50%; width: 50% !important; max-width: 50% !important; min-width: 0; box-sizing: border-box;
        padding: .4rem .8rem !important; text-align: left !important;
        border: 0 !important; border-top: 1px solid #f1f5f9 !important;
        white-space: normal !important; word-break: break-word; font-size: .86rem;
    }
    /* small caption above each value */
    table.m-cards td[data-label]::before {
        content: attr(data-label); display: block; margin-bottom: 1px;
        font-size: .64rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
        color: #64748b; line-height: 1.3;
    }
    /* title cell, long text, headings-less cells (buttons) and spanning cells use the full card width */
    table.m-cards td.m-first, table.m-cards td.m-wide, table.m-cards td.m-nolabel, table.m-cards td.m-span { flex: 0 0 100%; width: 100% !important; max-width: 100% !important; }
    table.m-cards td.m-first { padding: .6rem .8rem !important; font-weight: 600; background: #f8fafc; border-top: 0 !important; border-bottom: 1px solid #e2e8f0 !important; }
    table.m-cards td.m-nolabel { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: .5rem .8rem !important; background: #fcfcfd; }
    table.m-cards td.m-span { text-align: center !important; }
    table.m-cards td:empty { display: none; }
    /* money / number cells keep their emphasis */
    table.m-cards td[style*="font-weight:600"], table.m-cards td[style*="font-weight:700"], table.m-cards td[style*="font-weight: 600"] { font-weight: 700; }
    /* form controls fill their cell */
    table.m-cards td input:not([type=checkbox]):not([type=radio]), table.m-cards td select, table.m-cards td textarea, table.m-cards td .ts-wrapper { width: 100% !important; max-width: 100% !important; min-width: 0 !important; }
    table.m-cards td .btn, table.m-cards td .act-btn, table.m-cards td button { white-space: nowrap; }

    /* totals / footer rows */
    table.m-cards tfoot tr { display: flex; flex-wrap: wrap; justify-content: space-between; padding: .55rem .85rem; margin: 0 0 12px; border: 1px solid #e2e8f0; border-radius: 12px; background: #f8fafc; font-weight: 700; }
    table.m-cards tfoot td { display: block; width: auto !important; padding: .2rem .25rem !important; border: 0 !important; text-align: right !important; flex: 0 1 auto; }
    table.m-cards tfoot td:empty { display: none; }

    /* Chart of Accounts tree: each account is a compact flex row — name, balance, then its buttons underneath */
    table[data-mobile=off], table[data-mobile=off] > tbody, table[data-mobile=off] > tbody > tr { display: block; width: 100%; }
    table[data-mobile=off] > tbody > tr > td[colspan] { display: block; width: 100%; }
    .coa-row { display: flex !important; flex-wrap: wrap; align-items: center; }
    .coa-row > td { display: block; white-space: normal; }
    .coa-row > td.coa-indent { flex: 0 0 auto; padding: 8px 2px 8px calc(6px + var(--d, 0) * 12px) !important; width: auto !important; }
    .coa-row > td:nth-child(2) { flex: 1 1 0; min-width: 0; padding: 8px 4px 8px 0 !important; }
    .coa-row > td:nth-child(2) > div { flex-wrap: wrap; }
    .coa-row > td.coa-bal { flex: 0 0 auto; padding: 8px 10px 8px 4px !important; font-size: .8rem; }
    .coa-row .act-btn { min-height: 32px !important; padding: 4px 10px; }
    .coa-row > td:last-child { flex: 0 0 100%; width: 100% !important; text-align: left !important; padding: 0 10px 8px 30px !important; }

    /* Dashboard: KPI cards two per row, compact */
    .stats-grid-4, .stats-grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px !important; margin-bottom: 20px !important; }
    .stats-grid .stat-card { padding: 13px !important; }
    .stats-grid .stat-icon { width: 34px; height: 34px; border-radius: 10px; }
    .stats-grid .stat-icon svg { width: 18px; height: 18px; }
    .stats-grid .stat-arrow { display: none; }
    .stats-grid .stat-label { margin: 8px 0 2px !important; font-size: 10px !important; }
    .stats-grid .stat-value { font-size: 16px !important; word-break: normal; overflow-wrap: anywhere; }
    .stats-grid .stat-sub { font-size: 10.5px !important; }
    .highlight-card { padding: 16px !important; }
    .highlight-card .h-value { font-size: 22px !important; }

    /* filter / search forms that use fixed-width inputs: fields flow two per row, full width inside */
    form[style*="flex-wrap"] > div { flex: 1 1 140px; min-width: 0; }
    form[style*="flex-wrap"] input:not([type=checkbox]):not([type=radio]), form[style*="flex-wrap"] select { width: 100% !important; }
    form[style*="flex-wrap"] > .btn, form[style*="flex-wrap"] > a.btn, form[style*="flex-wrap"] > button { flex: 1 1 120px; }

    /* rows of buttons/headings built with utility classes: wrap instead of running off screen */
    .content .flex.items-center.gap-4, .content .flex.items-center.gap-3, .content .flex.items-center.gap-2,
    .content .flex.gap-2, .content .flex.gap-3, .content .flex.gap-4 { flex-wrap: wrap; }
    .content .flex.items-center.gap-4 > h1 { min-width: 0; flex: 1 1 100%; }

    /* ── Misc components ─────────────────────────────────── */
    .stat-card { min-width: 0; }
    .pagination nav > div:first-child { display: none; }
    .pagination nav > div:last-child { display: flex; justify-content: center; }
    .pagination svg { width: 18px; height: 18px; }
    .badge { white-space: nowrap; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
    .ts-dropdown { max-width: calc(100vw - 20px); }
}


/* collapsible filter block (rep list pages) — always open on desktop */
.m-filter-btn { display: none; }
@media (max-width: 767px) {
    .m-filter-btn { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 12px 14px; margin: 10px 0; border: 1px solid #e2e8f0; border-radius: 12px; background: #f8fafc; font-weight: 600; font-size: .85rem; color: #334155; }
    .m-filter-on { background: #e0e7ff; color: #4338ca; border-radius: 100px; padding: 1px 9px; font-size: .68rem; }
    .m-filter-body { display: none; }
    .m-filter-body.open { display: block; }
}

/* ============================================================
   APP SHELL — makes the browser version feel like a native app
   ============================================================ */
.topbar-company { display: none; }

@media (max-width: 1023px) {
    html { -webkit-text-size-adjust: 100%; }
    * { -webkit-tap-highlight-color: transparent; }
    a, button, input, select, textarea, label, .btn { touch-action: manipulation; }
    .btn, .act-btn, .nav-link, .nav-sub-link, .topbar, .mobile-bottom-nav * { -webkit-user-select: none; user-select: none; }
    .btn:active, .act-btn:active, .mobile-bottom-nav a:active, .mobile-bottom-nav button:active { transform: scale(.96); }
    .btn, .act-btn, .mobile-bottom-nav a, .mobile-bottom-nav button { transition: transform .08s ease, background .15s, color .15s; }

    /* Top app bar: menu button, page title with the company under it, company switcher */
    .topbar { height: calc(56px + env(safe-area-inset-top)) !important; padding-top: env(safe-area-inset-top); }
    .topbar-titlebox { min-width: 0; flex: 1 1 auto; display: flex; flex-direction: column; justify-content: center; line-height: 1.2; }
    .topbar .topbar-title { display: block !important; max-width: 100% !important; font-size: 15px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .topbar-company { display: block; font-size: 11px; font-weight: 600; color: #6366f1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .topbar > div:first-child { min-width: 0; flex: 1 1 auto; }

    /* Page area leaves room for the bar above and the tab bar below (incl. phone notch / home bar) */
    .content { padding-top: calc(68px + env(safe-area-inset-top)) !important; padding-bottom: calc(86px + env(safe-area-inset-bottom)) !important; animation: m-fade .14s ease-out; }
    @keyframes m-fade { from { opacity: .4; } to { opacity: 1; } }

    /* Bottom tab bar */
    .mobile-bottom-nav { height: calc(58px + env(safe-area-inset-bottom)) !important; padding-bottom: env(safe-area-inset-bottom) !important; background: rgba(255,255,255,.96) !important; -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); }
    .mobile-bottom-nav a, .mobile-bottom-nav button { position: relative; }
    .mobile-bottom-nav a.active::before { content: ''; position: absolute; top: 0; left: 50%; transform: translateX(-50%); width: 30px; height: 3px; border-radius: 0 0 4px 4px; background: #4f46e5; }
    .mobile-bottom-nav a.active { font-weight: 700; }
    .mobile-bottom-nav a.active svg { stroke-width: 2.2; }

    /* Side drawer */
    .sidebar { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
}

@media (max-width: 767px) {
    /* the Save / Cancel bar of long forms stays on screen just above the tab bar */
    .fc-action-bar {
        position: sticky; bottom: calc(58px + env(safe-area-inset-bottom)); z-index: 25;
        margin: 16px -10px 0 !important; padding: 10px 12px !important;
        background: rgba(255,255,255,.97); border-top: 1px solid #e2e8f0 !important;
        box-shadow: 0 -4px 14px rgba(15,23,42,.06);
    }
    /* flatter, app-style cards and headings */
    .card, .fc-section { box-shadow: 0 1px 2px rgba(15,23,42,.04); }
    .li-title, .fc-title { font-size: 1.2rem !important; }
    .li-subtitle, .fc-subtitle { font-size: .78rem !important; }
}

/* Touch devices: comfortable tap targets and no iOS auto-zoom on focus */
@media (hover: none) and (pointer: coarse) and (max-width: 1023px) {
    input:not([type=checkbox]):not([type=radio]), select, textarea, .ts-control, .ts-control input { font-size: 16px !important; }
    .btn, .btn-secondary, .act-btn, button.btn, a.btn { min-height: 42px; }
    input[type=checkbox], input[type=radio] { width: 20px; height: 20px; }
    .nav-link, .nav-sub-link { min-height: 44px; display: flex; align-items: center; }
}
