/* Mobile entity-list card system.
   The list views (quotes/jobs/invoices/clients) render each row as a card on
   mobile. This file owns: the .mobile-card-* header/body layout, the desktop
   .entity-id-pill mirror, the <=767px table->card transformation, and the
   per-row actions dropdown. Loaded after Bootstrap (see base.html). */

/* Mobile card styles */
.mobile-card-view {
    padding: 0 15px;
}

/* Mobile card header — [ID] [client rows]. The ID column width and the
   column gap are declared here once; the below-address description reaches
   back under the ID column by referencing the same two values. */
.mobile-card-header-main {
    --mobile-card-id-width: 50px;
    --mobile-card-gap: 0.75rem;
    display: flex;
    gap: var(--mobile-card-gap);
    align-items: flex-start;
}

.mobile-card-amount {
    font-size: 0.8rem;
    white-space: nowrap;
}

/* The eye (View) and the client quick actions (Call/Email) share one
   right-aligned flow stack, so they share dimensions — a tidy 44px column. */
.mobile-card-eye,
.mobile-card-quick {
    margin-left: auto;
    width: 44px;
    height: 44px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.mobile-card-eye .bi,
.mobile-card-quick .bi {
    font-size: 1.2rem;
}

/* Tap feedback for the eye. The skeleton swap destroys the card (and the eye) the
   instant a visit starts, so an element-bound :active never gets a frame to paint.
   Instead instant-nav.js spawns a free-floating pulse into #tap-pulse-layer at the
   eye's position; the layer is data-turbo-permanent, so the pulse survives both the
   skeleton swap and the body swap, animating in place through to the next page.
   Fixed + click-through so it never blocks anything. */
#tap-pulse-layer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 2000;
}
.tap-pulse {
    position: fixed;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #6c757d;
    color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    /* Animation is driven by the Web Animations API in instant-nav.js, not a CSS
       animation: Turbo reparents this node onto the next page, which would restart
       a CSS animation (a visible second flash). WAA plays through the move once. */
}
.tap-pulse .bi {
    font-size: 1.2rem;
}

.mobile-card-id {
    display: flex;
    flex-direction: column;
    min-width: var(--mobile-card-id-width);
    flex-shrink: 0;
}

.mobile-card-id .entity-type {
    font-size: 0.7rem;
    color: #6c757d;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    line-height: 1;
}

.mobile-card-id .entity-num {
    font-size: 0.95rem;
    font-weight: 600;
    color: #2c3e50;
    line-height: 1.2;
}

/* Desktop entity ID pill — mirrors the mobile .mobile-card-id layout
   and tint so the "#" column on entity list tables carries the same
   themed highlight users see on mobile cards. */
.entity-id-pill {
    display: inline-flex;
    flex-direction: column;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    line-height: 1;
}

.entity-id-pill .entity-type {
    font-size: 0.7rem;
    color: #6c757d;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    line-height: 1;
}

.entity-id-pill .entity-num {
    font-size: 0.95rem;
    font-weight: 600;
    color: #2c3e50;
    line-height: 1.2;
    margin-top: 2px;
}

.entity-id-quotes {
    background: color-mix(in srgb, var(--entity-color-quotes) 15%, transparent);
}

.entity-id-jobs {
    background: color-mix(in srgb, var(--entity-color-jobs) 15%, transparent);
}

.entity-id-invoices {
    background: color-mix(in srgb, var(--entity-color-invoices) 15%, transparent);
}

.entity-id-clients {
    background: color-mix(in srgb, var(--entity-color-clients) 15%, transparent);
}

.mobile-card-client {
    flex: 1;
    min-width: 0; /* Allow text truncation */
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.mobile-card-line-name {
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
}

.mobile-card-name {
    flex: 1;
    min-width: 0;
}

.mobile-card-meta-top {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.1rem;
    flex-shrink: 0;
    max-width: 8rem;
}

/* Price sits in the top-right column directly under the status badge (moved out
   of the floated action stack so it no longer drops to the address line on
   two-line names). The margin gives it a little air below the badge. */
.mobile-card-meta-top .mobile-card-amount {
    margin-top: 0.2rem;
}

.mobile-card-status-badge {
    flex: 0 0 auto;
    max-width: 8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.mobile-card-status-badge .custom-status {
    display: block;
    font-size: 0.85em;
    margin-top: 2px;
    pointer-events: none;
    white-space: normal;
}

.mobile-card-client-details > br:first-child {
    display: none;
}

.mobile-card-line-address {
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
    min-width: 0;
}

.mobile-card-action-stack {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.25rem;
    flex-shrink: 0;
    margin-left: auto;
}

.mobile-card-client-details {
    flex: 1;
    min-width: 0;
    line-height: 1.3;
    overflow-wrap: anywhere;
}

/* Address sits in the same dark text as the client name, not muted. The address
   is the only <small class="text-muted"> inside the details (phone/email are
   spans, the amount lives in the action stack), so it is the one element this
   darkens. !important overrides Bootstrap's .text-muted utility. */
.mobile-card-client-details small.text-muted {
    color: var(--text-primary) !important;
}

/* Client name: wrap normally (multi-word names break across lines), break an
   over-long unspaced token (e.g. HappyPath1777389274126) so it can't side-scroll
   the viewport, then clamp to two lines with an ellipsis. overflow-wrap:anywhere
   (not break-word) is required so the long token also shrinks the min-content
   width — otherwise the table stays wide and scrolls. */
.mobile-card-name strong {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    overflow-wrap: anywhere;
    line-height: 1.15;
}

.mobile-card-description {
    color: var(--text-muted);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    line-height: 1.4;
    max-height: calc(1.4em * 3);
    overflow-wrap: anywhere;
}

.mobile-card-description--below-address {
    margin-left: calc(-1 * (var(--mobile-card-id-width) + var(--mobile-card-gap)));
    width: calc(100% + var(--mobile-card-id-width) + var(--mobile-card-gap));
}

/* Address + description card: the action stack (amount + eye) floats right
   inside a block-flow container so both the address and the description wrap
   AROUND the eye. Lines level with the eye stop short of it; once the text
   clears the eye's bottom it runs on to the card edge. flow-root keeps the
   float contained so the eye/amount keep their top-right position and nothing
   escapes into neighbouring rows. */
.mobile-card-flow {
    display: flow-root;
}

.mobile-card-flow .mobile-card-action-stack {
    float: right;
    margin-left: var(--mobile-card-gap);
}

/* Clamp the description to 3 wrapped lines with a clean trailing ellipsis.
   The clamp (-webkit-box + -webkit-line-clamp:3 + overflow:hidden) is inherited
   from the base .mobile-card-description rule; here we only keep the top gap.
   overflow:hidden makes this a block formatting context, so the description
   clears BELOW the floated action stack rather than wrapping around the eye —
   the accepted trade-off for line-based truncation (tr.expanded unclamps it). */
.mobile-card-flow .mobile-card-description {
    margin-top: 0.6rem;
}

.mobile-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    transition: transform 0.2s ease;
}

.mobile-card:active {
    transform: scale(0.98);
}

.mobile-card-header .card-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: #2c3e50;
}

.detail-row {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    font-size: 0.9rem;
    color: #6c757d;
}

.detail-row i {
    width: 16px;
    text-align: center;
    color: var(--primary-color);
}

.mobile-card-stats .stat-number {
    font-size: 1.2rem;
    font-weight: 600;
    color: #2c3e50;
}

.mobile-card-stats .stat-label {
    font-size: 0.75rem;
    color: #6c757d;
    text-transform: uppercase;
}

.mobile-card-actions .btn {
    min-height: 44px; /* Touch target compliance */
    font-size: 0.9rem;
}

/* Ensure proper spacing for touch */
.mobile-card-actions .btn-group .btn {
    border-radius: 0;
}

.mobile-card-actions .btn-group .btn:first-child {
    border-radius: 6px 0 0 6px;
}

.mobile-card-actions .btn-group .btn:last-child {
    border-radius: 0 6px 6px 0;
}

/* Kill Bootstrap's row hover tint on touch input.
   On a touch screen `:hover` latches onto the last-tapped row and never
   releases, so an expanded card stays washed in `--bs-table-hover-bg`
   (rgba(0,0,0,.075)) until something else is tapped.

   Bootstrap paints via a variable cascade, not a colour: `.table-hover >
   tbody > tr:hover > *` only sets `--bs-table-{color,bg}-state`, which
   `.table > :not(caption) > * > *` reads through
   `box-shadow: inset 0 0 0 9999px var(--bs-table-bg-state, ...)`. Restoring
   both vars to `initial` — the guaranteed-invalid value `.table` itself
   declares at rest — makes every `var()` fall through to its resting
   fallback. Same selector as Bootstrap's, so same (0,2,2) specificity; this
   file loads after it (base.html:493 then :514) and wins on source order.

   Keyed on input capability, not viewport width: a 900px touch tablet has the
   same latched hover a 390px phone does. */
@media (hover: none) {
    .table-hover > tbody > tr:hover > * {
        --bs-table-color-state: initial;
        --bs-table-bg-state: initial;
    }
}

/* Mobile table adjustments */
@media (max-width: 767px) {
    /* Transform tables into card list on mobile */
    .table-responsive {
        overflow: visible;
        border: none;
    }

    /* Admin tables: card view on mobile — hide non-essential cells */
    .container-fluid .table-responsive td.d-none.d-md-table-cell {
        display: none !important;
    }

    .table-responsive table {
        border: 0;
    }

    .table-responsive thead {
        display: none; /* Hide table headers on mobile */
    }

    .table-responsive tbody {
        display: block;
    }

    .table-responsive tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid var(--border-color);
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        background: var(--card-bg);
        padding: 1rem;
        cursor: pointer;
        /* Height is animated imperatively (mobile-cards.js measures both ends and
           runs a WAA `height` animation) because a row's `auto` height has no
           interpolable start value. min-height must stay OUT of this transition:
           the script pins it as the expanded resting height, and a transition on
           it would re-grow the row after the height animation already landed. */
        transition: box-shadow 0.28s ease;
        position: relative;
        min-height: 80px;
    }

    .table-responsive tr.expanded {
        box-shadow: 0 8px 24px rgba(0,0,0,0.25);
    }

    .table-responsive td {
        display: block;
        text-align: left;
        padding: 0.5rem 0;
        border: none;
        position: relative;
    }

    /* First cell (ID) has no top padding */
    .table-responsive td:first-child {
        padding-top: 0;
    }

    /* Last visible cell before actions has reduced bottom padding */
    .table-responsive td:not(.table-actions):last-of-type {
        padding-bottom: 0.25rem;
    }

    /* Expanded: unclamp mobile descriptions and clear the action overlay. */
    tr.expanded .mobile-card-description {
        -webkit-line-clamp: unset;
        max-height: none;
        overflow: visible;
        padding-right: 145px;
    }

    /* Content extends full width - buttons overlay on top */
    .table-responsive tr td:not(.table-actions) {
        padding-right: 0;
    }

    /* Hide old mobile dropdown toggle */
    .table-responsive tr .mobile-actions-toggle,
    .table-responsive tr .mobile-actions-dropdown {
        display: none !important;
    }

    /* Actions container — absolute overlay top-right. When collapsed it shows
       nothing (the eye, and the client Call/Email quick actions, live in the
       header flow stack); when expanded it holds the full action stack.
       Pinned to the top: the card grows downward to fit. */
    td.table-actions {
        position: absolute !important;
        top: 0.5rem;
        right: 0.5rem;
        width: auto;
        height: auto;
        padding: 0 !important;
        margin: 0 !important;
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        gap: 0.5rem;
        z-index: 10;
        pointer-events: auto; /* Buttons are clickable */

        /* Bootstrap fills every <td> via `.table > :not(caption) > * > *`. This cell
           is lifted out of flow and sits over the card, so that fill paints as one
           opaque block across the whole button stack -- the gaps between buttons
           included -- hiding the status badge behind it. Only the buttons
           themselves should paint here.

           Both declarations are load-bearing, for different reasons:
             background-color -- `var(--bs-table-bg)`, always paints.
             box-shadow -- `inset 0 0 0 9999px var(--bs-table-bg-state, ...)`. The
               default resolves to `--bs-table-accent-bg: transparent`, so it looks
               dead. It is not: the lists are `.table-hover`, whose
               `tr:hover > *` sets `--bs-table-bg-state`, and on touch :hover sticks
               to the last-tapped element -- i.e. the expanded card. Remove this and
               the block returns on device while every desktop check stays green.

           The `td` qualifier is load-bearing. Bootstrap's `.table>:not(caption)>*>*`
           is (0,1,1) -- `:not()` contributes the specificity of its argument, and
           `caption` is a type selector. A bare `.table-actions` is (0,1,0) and loses
           on specificity, which is resolved before source order is ever consulted.
           `td.table-actions` is (0,1,1), ties, and wins on source order (bootstrap
           at base.html:493, this at :514). Drop the `td` and the fill returns the
           next time anything re-adds `.table-hover` to the table. */
        background-color: transparent;
        box-shadow: none;
    }

    /* Outline buttons declare `--bs-btn-bg: transparent`; they only looked solid
       because the cell painted behind them. With the cell transparent they need a
       fill of their own, or the badge shows straight through them. Solid variants
       (Start / Hold / Cancel) already carry their own background and must keep it,
       so this may not widen to `.table-actions .btn`.

       Matched by substring, not enumerated: macros/_row_actions.html owns the map
       of action kind -> Bootstrap class, and it emits outline-secondary,
       outline-warning, outline-danger, outline-primary and outline-dark (Archive,
       on terminal statuses). Listing them here would be a second owner of that map,
       silently wrong the next time a kind is added. */
    .table-actions [class*="btn-outline-"] {
        --bs-btn-bg: var(--card-bg);
    }

    /* Collapsed: the header aside owns the View "eye", so hide the actions-cell
       copy. Expanded: it rejoins the action stack (top, above Edit) and picks up
       the pill styling from `tr.expanded .table-actions .btn` below. */
    .table-actions .btn-view {
        display: none !important;
    }

    tr.expanded .table-actions .btn-view {
        display: flex !important;
    }

    /* ...and drop the header's eye once the stack owns it, or the card shows two
       View buttons. Previously masked: the actions cell painted an opaque block
       over this corner of the header. */
    tr.expanded .mobile-card-eye {
        display: none;
    }

    /* Hide desktop inline buttons by default */
    .table-actions .d-none.d-md-inline {
        display: none;
    }

    /* Show desktop actions on mobile when expanded */
    tr.expanded .table-actions .d-none.d-md-inline {
        display: flex !important;
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
        align-items: stretch;
    }

    /* Stack action buttons vertically when expanded - show text */
    tr.expanded .table-actions .d-none.d-md-inline .btn,
    tr.expanded .table-actions .d-none.d-md-inline form {
        display: block;
        width: auto;
        min-width: 120px;
        opacity: 0;
        transform: translateX(20px);
        animation: slideIn 0.3s ease forwards;
    }

    /* Stagger animation for each button */
    tr.expanded .table-actions .d-none.d-md-inline > *:nth-child(1) {
        animation-delay: 0s;
    }

    tr.expanded .table-actions .d-none.d-md-inline > *:nth-child(2) {
        animation-delay: 0.05s;
    }

    tr.expanded .table-actions .d-none.d-md-inline > *:nth-child(3) {
        animation-delay: 0.1s;
    }

    tr.expanded .table-actions .d-none.d-md-inline > *:nth-child(4) {
        animation-delay: 0.15s;
    }

    tr.expanded .table-actions .d-none.d-md-inline > *:nth-child(5) {
        animation-delay: 0.2s;
    }

    @keyframes slideIn {
        from {
            opacity: 0;
            transform: translateX(20px);
        }
        to {
            opacity: 1;
            transform: translateX(0);
        }
    }

    tr.expanded .table-actions .btn {
        min-height: 44px;
        width: auto;
        min-width: 120px;
        text-align: left;
        font-size: 0.875rem; /* Show text */
        padding: 0.5rem 1rem;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        display: flex;
        align-items: center;
        justify-content: flex-start;
        white-space: nowrap;
    }

    /* Show icons with text */
    tr.expanded .table-actions .btn .bi {
        margin-right: 0.5rem;
        font-size: 1rem;
    }

    /* Status badge adjustments for card view */
    .status-badge {
        max-width: 100%;
        word-wrap: break-word;
        line-height: 1.2;
        padding: 0.5rem 0.75rem;
        text-align: center;
        display: inline-block;
    }

    .status-badge[data-short="true"] {
        white-space: nowrap;
    }

    .status-badge:not([data-short]) {
        white-space: normal;
    }

    /* Client info styling in card */
    .table td small {
        font-size: 0.85rem;
        line-height: 1.3;
        color: #6c757d;
    }


    /* The eye, price, and status badge are rendered in the mobile card header
       (see _mobile_card_header.html) and laid out by flow (.mobile-card above).
       The old :has()-driven top:Nrem lookup table that positioned an absolutely
       placed eye/price per (has-price x has-custom-status) combo is gone — flow
       layout computes those positions now. */
}

/* Mobile dropdown menu for actions */
.mobile-actions-dropdown {
    position: absolute;
    left: 0;
    top: 100%;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 9999;
    min-width: 180px;
    display: none;
    overflow: hidden;
    margin-top: 4px;
}

.mobile-actions-dropdown.show {
    display: block;
}

.mobile-actions-dropdown .dropdown-item {
    display: flex;
    align-items: center;
    /* Large touch target — easy to hit with big fingers. */
    padding: 15px 18px;
    color: var(--text-primary);
    text-decoration: none;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 1rem;
    transition: background-color 0.2s ease;
}

.mobile-actions-dropdown .dropdown-item:hover {
    background-color: var(--bg-secondary);
}

.mobile-actions-dropdown .dropdown-item i {
    margin-right: 8px;
    width: 16px;
}

/* Flush divider — a crisp line directly between two items, no floating gap. */
.mobile-actions-dropdown .dropdown-divider {
    height: 0;
    margin: 0;
    border-top: 1px solid var(--border-color);
}

/* ---------------------------------------------------------------------------
   Swipe gestures on the mobile cards (card_swipe_controller.js).

   Swipe right -> View (detail page); swipe left -> the status workflow's next
   forward action (Accept / Mark Paid / Start / ...). As the card slides, a pane
   is revealed under it on the edge being uncovered: right-swipe shows it on the
   LEFT, left-swipe on the RIGHT. The pane brightens (.is-armed) once the drag
   passes the fire threshold. Mobile-only — the controller never builds a pane on
   desktop, but the rules are scoped to the breakpoint to be certain.
   --------------------------------------------------------------------------- */
@media (max-width: 767px) {
    /* Positioning context for the absolutely-placed pane, and room for the card
       to slide past its own edges. */
    [data-controller~="card-swipe"] {
        position: relative;
    }

    /* Hand horizontal drags to the controller while leaving vertical panning to
       the browser (page scroll). Without this the global touch-action lets the
       browser claim the gesture for scrolling and fire pointercancel, which ends
       the swipe the instant it starts. */
    [data-controller~="card-swipe"] tbody tr {
        touch-action: pan-y;
    }

    .card-swipe-pane {
        position: absolute;
        z-index: 0;                 /* sits under the sliding card (row gets z-index 1) */
        display: flex;
        align-items: center;
        border-radius: 12px;        /* matches the card */
        padding: 0 1.25rem;
        color: #fff;
        overflow: hidden;
        pointer-events: none;
    }

    /* Which edge the revealed content hugs — the edge the card uncovers. */
    .card-swipe-pane.is-left { justify-content: flex-start; }
    .card-swipe-pane.is-right { justify-content: flex-end; }

    .card-swipe-pane-inner {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        font-weight: 600;
        font-size: 0.95rem;
        opacity: 0.85;
        transition: transform 0.12s ease, opacity 0.12s ease;
    }

    .card-swipe-pane .bi {
        font-size: 1.3rem;
    }

    /* View pane stacks the icon over the label (icon on top, "View" beneath) so the
       hook only needs a narrow reveal. The controller measures this stack's width to
       decide the hook distance. */
    .card-swipe-pane--view .card-swipe-pane-inner {
        flex-direction: column;
        gap: 0.15rem;
        font-size: 0.8rem;
        line-height: 1;
    }

    /* Past the fire threshold: full opacity + a small pop so the user knows a
       release will commit. */
    .card-swipe-pane.is-armed .card-swipe-pane-inner {
        opacity: 1;
        transform: scale(1.08);
    }

    /* Kind -> colour. Mirrors the action button semantics (primary/success/dark)
       and a neutral grey for View. 'dark' is the Archive fallback for terminal
       statuses with no forward action. */
    .card-swipe-pane--primary { background: var(--bs-primary, #0d6efd); }
    .card-swipe-pane--success { background: var(--bs-success, #198754); }
    .card-swipe-pane--dark { background: var(--bs-dark, #212529); }
    .card-swipe-pane--view { background: var(--bs-secondary, #6c757d); }

    /* Hint the gesture: a grab cursor for pointer/mouse testing. */
    [data-controller~="card-swipe"] tbody tr.is-swiping {
        cursor: grabbing;
        user-select: none;
    }
}

/* ── Swipe-to-view incoming detail layer ────────────────────────────────────
   The interactive left-swipe transition (card_swipe_controller) builds this layer
   at the hook and scrubs it in from the right with the finger. It is appended to
   <html> (NOT <body>) so the outgoing page's body transform doesn't move it, and so
   it survives the Turbo <body> swap to cover the navigation until turbo:load removes
   it. Opaque background hides the receding page behind it. */
.cs-view-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;              /* above navbar/content; no modal is open mid-swipe */
    background: var(--bs-body-bg, #fff);
    overflow: hidden;
    will-change: transform;
    transform: translateX(100%);
}

/* Inner page wrapper holds the cloned skeleton (then the real fetched content). The
   banner is edge-to-edge; the rest mirrors base.html's .container.mt-4. */
.cs-view-overlay__page {
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}
