/* =====================================================================
   Responsive rules
   Desktop:  sidebar fixed, content offset by 240px
   Tablet:   sidebar narrows to icon rail
   Mobile:   sidebar becomes an off-canvas drawer toggled from the topbar
   ===================================================================== */

/* ---- Tablet (<= 991px): icon-only rail --------------------------------- */
@media (max-width: 991.98px) {
    .sidebar { width: 76px; }
    .sidebar-brand span,
    .sidebar-nav .nav-link span { display: none; }
    .sidebar-brand { justify-content: center; padding: 1.35rem 0.5rem; }
    .sidebar-nav .nav-link { justify-content: center; padding: 0.7rem; }
    .sidebar-nav .nav-link i { width: auto; font-size: 1.2rem; }
    .main-content { margin-left: 76px; width: calc(100% - 76px); }
    .topbar { margin-left: 76px; }
}

/* ---- Mobile (<= 767px): off-canvas drawer ------------------------------ */
@media (max-width: 767.98px) {
    .sidebar {
        width: 240px;
        transform: translateX(-100%);
        box-shadow: var(--shadow-md);
    }
    .sidebar.show { transform: translateX(0); }
    .sidebar-brand span,
    .sidebar-nav .nav-link span { display: inline; }
    .sidebar-brand { justify-content: flex-start; padding: 1.35rem 1.25rem; }
    .sidebar-nav .nav-link { justify-content: flex-start; padding: 0.62rem 0.85rem; }
    .sidebar-nav .nav-link i { width: 20px; }

    .main-content { margin-left: 0; width: 100%; }
    .topbar { margin-left: 0; }

    .global-search { max-width: none; }
    .global-search input::placeholder { font-size: 0.78rem; }

    .page-content { padding: 1rem; }

    .sidebar-backdrop {
        display: none;
        position: fixed; inset: 0;
        background: rgba(0,0,0,0.4);
        z-index: 1025;
    }
    .sidebar-backdrop.show { display: block; }
}

@media (max-width: 575.98px) {
    .stat-card { flex-direction: row; }
    .auth-card { padding: 1.75rem 1.4rem; }
}

/* =====================================================================
   Mobile bottom navigation bar — hidden above the mobile breakpoint,
   where the sidebar is the primary navigation instead.
   ===================================================================== */
.bottom-nav { display: none; }

@media (max-width: 767.98px) {
    .bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0; left: 0; right: 0;
        height: 60px;
        padding-bottom: env(safe-area-inset-bottom, 0);
        background: var(--card-bg);
        border-top: 1px solid var(--border);
        box-shadow: 0 -2px 12px rgba(0,0,0,0.07);
        z-index: 1035;
        align-items: center;
        justify-content: space-around;
    }
    .bottom-nav-item {
        display: flex; flex-direction: column; align-items: center; justify-content: center;
        gap: 2px; flex: 1; height: 100%;
        color: var(--text-muted);
        text-decoration: none;
        font-size: 0.64rem;
        font-weight: 500;
        border: none; background: none;
        -webkit-tap-highlight-color: transparent;
        transition: color 0.15s ease;
    }
    .bottom-nav-item i { font-size: 1.3rem; line-height: 1; }
    .bottom-nav-item.active { color: var(--gold-dark); }

    /* Leave room so the fixed bar never covers the last row of content
       or a page's action buttons. */
    .main-content { padding-bottom: 66px; }

    /* Bigger tap targets everywhere on mobile — 48px is the spec'd
       comfortable minimum (was 44px). */
    .btn, .btn-icon, .form-control, .form-select { min-height: 48px; }
    .btn-icon { display: inline-flex; align-items: center; justify-content: center; width: 44px; }
    .mini-table .btn-icon { min-height: 40px; }

    /* Slightly larger body text on mobile than the desktop default —
       desktop itself stays untouched, this is mobile-only. */
    body { font-size: 15.5px; }

    /* Every add/edit form's Save/Cancel row (the same "mt-4 d-flex
       gap-2" wrapper used consistently across all 12 form pages) goes
       full-width and stacks vertically — the primary action reads as
       one clear, thumb-friendly button instead of two buttons
       competing for a narrow row. */
    .mt-4.d-flex.gap-2 {
        flex-direction: column;
    }
    .mt-4.d-flex.gap-2 > .btn {
        width: 100%;
    }
    /* KPI cards (Dashboard + Reports use this exact combination only)
       go full-width, one per row, on mobile — reads as a clear stack
       of stats instead of a cramped 2-up grid. */
    .col-6.col-md-4.col-lg-2 {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* =====================================================================
   Collapsible filter panels — desktop/tablet always show filters open
   (add class="filter-toggle-btn" to a button and it just won't
   render there). On mobile, filters start collapsed behind a
   "Filters" button (see app.js for the toggle behavior) so the actual
   list content is visible without scrolling past every filter field
   first.
   ===================================================================== */
.filter-toggle-btn { display: none; }
@media (max-width: 767.98px) {
    .filter-toggle-btn { display: flex; }
    .filter-panel { display: none; }
    .filter-panel.show-filters { display: block; }
}

/* =====================================================================
   Responsive card tables — add class="responsive-cards" to any <table>
   and data-label="Column Name" to each <td> (see pages/leads/list.php
   for the reference implementation). On mobile, each row becomes a
   stackedcard instead of a table row you have to scroll sideways to
   read. The actions cell can additionally take class="card-actions"
   to render as a right-aligned button row at the bottom of the card
   instead of a labeled field.
   ===================================================================== */
@media (max-width: 767.98px) {
    .responsive-cards thead { display: none; }
    .responsive-cards, .responsive-cards tbody, .responsive-cards tr, .responsive-cards td {
        display: block;
        width: 100%;
    }
    .responsive-cards tr {
        margin-bottom: 0.75rem;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        padding: 0.75rem 0.85rem;
        background: var(--card-bg);
        position: relative;
    }
    /* The select checkbox sits in the top-right corner of the card
       instead of taking a labeled row like every other field. */
    .select-cell {
        position: absolute;
        top: 0.6rem;
        right: 0.7rem;
        width: auto !important;
        padding: 0 !important;
        min-height: 0 !important;
    }
    .select-cell::before { content: none; }
    .select-cell input[type="checkbox"] {
        width: 20px;
        height: 20px;
    }
    .responsive-cards tr:last-child { margin-bottom: 0; }
    .responsive-cards td {
        border: none !important;
        padding: 0.3rem 0 !important;
        padding-left: 48% !important;
        position: relative;
        text-align: left !important;
        white-space: normal !important;
        max-width: none !important;
        overflow: visible !important;
        text-overflow: clip !important;
        min-height: 1.4rem;
    }
    .responsive-cards td::before {
        content: attr(data-label);
        position: absolute;
        left: 0; top: 0.3rem;
        width: 44%;
        font-size: 0.66rem;
        font-weight: 600;
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.02em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: 1.3;
    }
    .responsive-cards td:empty { display: none; }
    .responsive-cards td.card-actions {
        padding-left: 0 !important;
        text-align: right !important;
        border-top: 1px solid var(--border) !important;
        margin-top: 0.5rem;
        padding-top: 0.6rem !important;
    }
    .responsive-cards td.card-actions::before { content: none; }
}

/* =====================================================================
   Mobile text readability tuning (mobile only — desktop untouched).
   Rationale for each value:
   - Body 16px: below 16px, iOS Safari auto-zooms when a field is
     focused, which feels janky; 16px is also the widely-recommended
     minimum comfortable reading size on phones.
   - Card table values ~0.95rem with looser line-height: the value
     text sits in the right ~52% of a card row, so it needs to be big
     enough to read at a glance without feeling cramped.
   - Card labels 0.7rem: nudged up from 0.66rem so the field name is
     legible, while staying clearly smaller than the value it labels.
   - Form controls 16px: same iOS anti-zoom reason as body, and makes
     data entry on a phone much easier.
   - Headings/panel titles: a small bump so screen hierarchy still
     reads clearly at mobile sizes.
   ===================================================================== */
@media (max-width: 767.98px) {
    body { font-size: 16px; line-height: 1.5; }

    .form-control, .form-select, .form-control-sm, .form-select-sm {
        font-size: 16px;
    }

    .responsive-cards td {
        font-size: 0.95rem;
        line-height: 1.45;
        padding-top: 0.4rem !important;
        padding-bottom: 0.4rem !important;
    }
    .responsive-cards td::before {
        font-size: 0.7rem;
        top: 0.42rem;
    }

    .page-heading { font-size: 1.3rem; }
    .panel-title { font-size: 1.05rem; }
    .mini-table { font-size: 0.95rem; }

    /* Inline dropdowns inside a card row need a bit of breathing room
       so the label and control don't crowd each other. */
    .responsive-cards td form .form-select-sm { margin-top: 0.15rem; }
    .badge { font-size: 0.8rem; }
}

/* =====================================================================
   Batch: compact dashboard KPI cards on phones + roomier leads cards.
   ===================================================================== */

/* --- Dashboard KPI cards: smaller and tidier on phones -------------- */
@media (max-width: 767.98px) {
    /* Tighter gap so two cards per row don't waste space. */
    .stat-card {
        padding: 0.7rem 0.75rem;
        gap: 0.6rem;
        border-radius: var(--radius-sm);
    }
    .stat-icon {
        width: 34px; height: 34px;
        font-size: 0.95rem;
        border-radius: 9px;
    }
    .stat-value { font-size: 1.15rem; }
    .stat-label { font-size: 0.68rem; margin-top: 0; line-height: 1.2; }
    /* The KPI row is col-6 (two per row) — shrink its gutters a touch
       so each card gets a bit more usable width. */
    .row.g-3 > [class*="col-6"] { padding-left: 0.4rem; padding-right: 0.4rem; }
}

/* --- Leads list card view: give values (and inline dropdowns) room -- */
@media (max-width: 767.98px) {
    /* The default card layout reserves 48% for the label, which leaves
       the inline Project/Source/Status/Assigned dropdowns too narrow and
       makes their text truncate ("Golden Grand c..."). Narrow the label
       column so the value side gets noticeably more width. */
    .responsive-cards td {
        padding-left: 38% !important;
    }
    .responsive-cards td::before {
        width: 35%;
    }
    /* Inline dropdowns fill their value column so the full project /
       source / staff name is readable at a glance. */
    .responsive-cards td form { width: 100%; }
    .responsive-cards td form .form-select-sm { width: 100%; }
    /* Customer name and phone read better a little larger and bolder. */
    .responsive-cards td[data-label="Customer"] .fw-semibold { font-size: 1rem; }
    .responsive-cards td[data-label="Phone"] a { font-size: 0.95rem; }
}

/* =====================================================================
   Bulk-friendly mobile leads view — bigger text and easy tap targets
   so selecting and acting on leads on a phone is comfortable. No
   gestures, just larger controls (per the "keep it simple" choice).
   ===================================================================== */
@media (max-width: 767.98px) {
    /* Bigger, easier-to-hit select checkbox in the card corner. */
    .select-cell input[type="checkbox"] {
        width: 26px;
        height: 26px;
    }

    /* Action icons (call / WhatsApp / view / edit / delete) become
       proper finger-sized buttons with clear spacing, not tiny icons. */
    .responsive-cards td.card-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem;
        justify-content: flex-start !important;
        padding-top: 0.7rem !important;
    }
    .responsive-cards td.card-actions .btn-icon,
    .responsive-cards td.card-actions form {
        margin: 0;
    }
    .responsive-cards td.card-actions .btn-icon {
        width: 44px;
        height: 44px;
        font-size: 1.25rem;
        border: 1px solid var(--border);
        border-radius: 10px;
    }
    .responsive-cards td.card-actions .btn-icon:hover { background: var(--bg); }

    /* Slightly larger field values throughout the card for readability. */
    .responsive-cards td { font-size: 0.95rem; }

    /* Bulk action bar: stack controls full-width so each is easy to tap. */
    .bulk-action-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }
    .bulk-action-bar .form-select,
    .bulk-action-bar .btn {
        width: 100%;
        font-size: 16px;      /* prevents iOS zoom-on-focus */
        padding: 0.55rem 0.75rem;
    }
    .bulk-action-bar .fw-semibold { text-align: center; }
}

/* =====================================================================
   Compact leads list on mobile (.leads-compact)
   - Card face shows only Customer, Phone, Project, Status.
   - Lead Code, Source, Assigned To, Created are collapsed behind a
     "More details" toggle to save space (4-6 leads per screen).
   - Fields use a flat flex layout (label + value on one line) with NO
     absolute positioning, so a wrapping value can never overlap the
     next field.
   ===================================================================== */

/* The More-details toggle is a mobile-only control — hidden on desktop */
.card-more-toggle { display: none; }

@media (max-width: 767.98px) {
    .leads-compact tr {
        padding: 0.55rem 0.7rem 0.5rem;
        margin-bottom: 0.5rem;
        position: relative;
    }

    /* Every field: one flat flex row, label then value, nothing absolute */
    .leads-compact td {
        display: flex !important;
        align-items: center;
        gap: 0.45rem;
        padding: 0.12rem 0 !important;
        padding-left: 0 !important;
        min-height: 0 !important;
        font-size: 0.8rem;
    }
    .leads-compact td::before {
        position: static !important;
        left: auto !important;
        top: auto !important;
        width: 60px !important;
        flex: 0 0 60px;
        font-size: 0.6rem !important;
        line-height: 1.2;
    }
    .leads-compact td[data-label="Customer"] .fw-semibold { font-size: 0.9rem; }

    /* Inline-edit selects (Project, Status, Source, Assigned) fill the row */
    .leads-compact td:not(.card-actions) form { flex: 1 1 auto; margin: 0; width: auto; }
    .leads-compact td:not(.card-actions) form .form-select-sm { width: 100%; font-size: 0.8rem; padding: 0.2rem 1.6rem 0.2rem 0.5rem; }

    /* Keep the action icons as a tidy left-aligned wrapping row */
    .leads-compact td.card-actions {
        display: flex !important;
        flex-wrap: wrap;
        justify-content: flex-start;
        align-items: center;
        gap: 0.35rem;
    }

    /* Collapsed extras — revealed when the row gets .show-extra */
    .leads-compact td.lead-extra { display: none !important; }
    .leads-compact tr.show-extra td.lead-extra { display: flex !important; }

    /* The More/Less toggle button */
    .leads-compact .card-more-toggle {
        display: block !important;
        padding: 0.2rem 0 0 !important;
    }
    .leads-compact .card-more-toggle::before { content: none !important; }
    .lead-more-btn {
        background: none;
        border: none;
        color: var(--gold);
        font-size: 0.72rem;
        font-weight: 600;
        padding: 0.15rem 0;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 0.25rem;
    }
    .lead-more-btn .bi { font-size: 0.7rem; transition: transform 0.15s ease; }
    .lead-more-btn .less-text { display: none; }
    .leads-compact tr.show-extra .lead-more-btn .more-text { display: none; }
    .leads-compact tr.show-extra .lead-more-btn .less-text { display: inline; }
    .leads-compact tr.show-extra .lead-more-btn .bi { transform: rotate(180deg); }

    /* Select checkbox: a clean, fixed-size tappable square in the card's
       top-right corner. Forced to display:block (NOT flex — the general
       .leads-compact td flex rule was stretching the box into a thin
       sliver on mobile) and to a locked 24px square with !important so
       the mobile browser can't squish or stretch it. Shown for all users. */
    .leads-compact .select-cell {
        position: absolute !important;
        display: block !important;
        top: 8px;
        right: 10px;
        width: 24px !important;
        height: 24px !important;
        padding: 0 !important;
        min-height: 0 !important;
        margin: 0 !important;
        z-index: 3;
    }
    .leads-compact .select-cell::before { content: none !important; }
    .leads-compact .select-cell input[type="checkbox"] {
        -webkit-appearance: checkbox;
        appearance: checkbox;
        width: 24px !important;
        height: 24px !important;
        min-width: 24px;
        min-height: 24px;
        margin: 0 !important;
        padding: 0 !important;
        display: block;
        flex: none;
        box-sizing: border-box;
        accent-color: var(--gold);
        cursor: pointer;
    }
    .leads-compact td[data-label="Customer"] { padding-right: 40px !important; }
}

/* =====================================================================
   Mobile icon-button sizing fixes (My Work action buttons)
   The global ".btn { min-height:48px }" mobile rule stretched the small
   call/WhatsApp icon buttons into tall, narrow boxes. Make them clean
   squares so they read as proper icon buttons.
   ===================================================================== */
@media (max-width: 767.98px) {
    .mywork-actions .btn {
        width: 42px;
        min-height: 42px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    .mywork-actions .btn i { font-size: 1.05rem; }
}
