/* =====================================================================
   Customers page — mobile makeover. Matches the bookings-page pattern:
     - Hide the title + old filter card
     - Simple search bar at the top
     - Cards instead of table
     - Floating "+ Add" button (bottom-right)
   Everything scoped inside @media (max-width: 767px) so desktop is
   unaffected.
   ===================================================================== */

/* Desktop safety net — mobile-only elements stay hidden */
#customers .cm-mobile-header,
#customers .cm-cards,
#customers .cm-fab { display: none; }

/* =====================================================================
   Add Customer modal (#addCustomerModal) — mobile-only modernisation.
   Bottom-sheet style: cleaner header, form scrolls in the middle,
   Cancelar + Adicionar Cliente pinned side-by-side at the bottom.
   ===================================================================== */
@media (max-width: 767px) {
    /* Full-viewport bottom sheet */
    #addCustomerModal.modal-overlay,
    #addCustomerModal.modal,
    #editCustomerModal.modal-overlay,
    #editCustomerModal.modal {
        padding: 0 !important;
        align-items: flex-end !important;
    }
    #addCustomerModal .modal-content,
    #editCustomerModal .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        margin: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }
    /* Header — kill the purple bar, use white with dark title */
    #addCustomerModal .modal-header,
    #editCustomerModal .modal-header {
        background: #fff !important;
        color: #111827 !important;
        border-bottom: 1px solid #f3f4f6 !important;
        padding: 16px 20px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        flex-shrink: 0 !important;
    }
    #addCustomerModal .modal-header .modal-title,
    #editCustomerModal .modal-header .modal-title {
        color: #111827 !important;
        font-size: 1.05rem !important;
        font-weight: 700 !important;
        margin: 0 !important;
    }
    #addCustomerModal .close,
    #editCustomerModal .close {
        background: #f3f4f6 !important;
        color: #6b7280 !important;
        border: none !important;
        width: 32px !important;
        height: 32px !important;
        border-radius: 50% !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 1.15rem !important;
        cursor: pointer;
    }

    /* Body — scrolls; form fills. Zero bottom padding so the pinned
       button row can sit flush against the viewport bottom edge. */
    #addCustomerModal .modal-body,
    #editCustomerModal .modal-body {
        padding: 16px 20px 0 !important;
        overflow-y: auto !important;
        flex: 1 !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior: contain !important;
    }
    #addCustomerModal #addCustomerForm,
    #editCustomerModal #editCustomerForm {
        display: flex !important;
        flex-direction: column !important;
        min-height: 100% !important;
    }
    /* Form fields cleaner, tighter */
    #addCustomerModal .form-grid,
    #editCustomerModal .form-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 12px !important;
        flex: 1 !important;
    }
    #addCustomerModal .form-group label,
    #editCustomerModal .form-group label {
        font-size: 0.85rem !important;
        font-weight: 600 !important;
        color: #374151 !important;
        display: block !important;
        margin-bottom: 4px !important;
    }
    #addCustomerModal .form-group input,
    #addCustomerModal .form-group select,
    #editCustomerModal .form-group input,
    #editCustomerModal .form-group select {
        width: 100% !important;
        height: 42px !important;
        padding: 0 12px !important;
        border: 1px solid #d1d5db !important;
        border-radius: 8px !important;
        font-size: 0.95rem !important;
        background: #fff !important;
    }
    #addCustomerModal .form-group input:focus,
    #addCustomerModal .form-group select:focus,
    #editCustomerModal .form-group input:focus,
    #editCustomerModal .form-group select:focus {
        outline: none !important;
        border-color: #6366f1 !important;
    }

    /* Pinned bottom buttons — Cancelar (grey secondary) + Adicionar
       Cliente (purple gradient primary). Uses sticky positioning so it
       stays glued to the visible bottom edge as the form scrolls, and
       adds its own vertical padding (safe-area aware) so the buttons
       are flush with the viewport bottom. */
    #addCustomerModal #addCustomerForm > div:last-child,
    #editCustomerModal #editCustomerForm > div:last-child {
        margin-top: auto !important;
        position: sticky !important;
        bottom: 0 !important;
        background: #fff !important;
        padding: 12px 0 calc(12px + env(safe-area-inset-bottom)) !important;
        margin-left: -20px !important;
        margin-right: -20px !important;
        padding-left: 20px !important;
        padding-right: 20px !important;
        border-top: 1px solid #f3f4f6 !important;
        display: flex !important;
        flex-direction: row !important;
        gap: 8px !important;
        justify-content: stretch !important;
    }
    #addCustomerModal #addCustomerForm > div:last-child > .btn,
    #editCustomerModal #editCustomerForm > div:last-child > .btn {
        flex: 1 1 calc(50% - 4px) !important;
        max-width: calc(50% - 4px) !important;
        margin: 0 !important;
        height: 44px !important;
        border-radius: 10px !important;
        font-weight: 600 !important;
        font-size: 0.9rem !important;
        box-shadow: none !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
    #addCustomerModal #addCustomerForm > div:last-child > .btn-danger,
    #editCustomerModal #editCustomerForm > div:last-child > .btn-danger {
        background: #f3f4f6 !important;
        color: #374151 !important;
        border: 1px solid #e5e7eb !important;
    }
    #addCustomerModal #addCustomerForm > div:last-child > .btn-success,
    #editCustomerModal #editCustomerForm > div:last-child > .btn-success {
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
    }

    /* =====================================================================
       Customer details modal (#customerModal) — same modernisation as
       the booking details modal.
       ===================================================================== */
    #customerModal.modal-overlay,
    #customerModal.modal {
        padding: 0 !important;
        overflow: hidden !important;
        align-items: stretch !important;
    }
    #customerModal .modal-content {
        width: 100% !important;
        max-width: 100% !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        margin: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }
    /* Header — white bg, dark title, drop the purple bar */
    #customerModal .modal-header {
        background: #fff !important;
        color: #111827 !important;
        border-bottom: 1px solid #f3f4f6 !important;
        padding: 16px 20px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        flex-shrink: 0 !important;
    }
    #customerModal .modal-header .modal-title,
    #customerModal .modal-header h3 {
        color: #111827 !important;
        font-size: 1.05rem !important;
        font-weight: 700 !important;
        margin: 0 !important;
    }
    #customerModal .close {
        background: #f3f4f6 !important;
        color: #6b7280 !important;
        border: none !important;
        width: 32px !important;
        height: 32px !important;
        border-radius: 50% !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 1.15rem !important;
        cursor: pointer;
    }

    /* Body — single scroll container with hardware-accelerated momentum,
       soft grey background for cards to pop on. Flex column so the last
       child (buttons row) can `margin-top: auto` to the bottom when
       content is short. */
    #customerModal .modal-body {
        background: #f9fafb !important;
        padding: 12px !important;
        overflow-y: auto !important;
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior: contain !important;
        transform: translateZ(0);
        contain: paint;
    }

    /* Cards inside body — unified white with subtle border */
    #customerModal .modal-body > div[style*="border"] {
        background: #fff !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 12px !important;
        padding: 14px 16px !important;
        margin-bottom: 10px !important;
        box-shadow: none !important;
    }
    /* Contact + Activity: stack vertically on narrow screens */
    #customerModal .bm-two-col {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
    }

    /* Merge the NOME card with the CONTACTO card so they look like a
       single card with a subtle divider. Also tighten the bottom of
       NOME + top of CONTACTO so there's no dead space between them. */
    #customerModal .modal-body > div > div:first-child[style*="border"] {
        margin-bottom: 0 !important;
        border-bottom-left-radius: 0 !important;
        border-bottom-right-radius: 0 !important;
        border-bottom: none !important;
        padding-bottom: 12px !important;
    }
    #customerModal .bm-two-col > div:first-child[style*="border"] {
        border-top-left-radius: 0 !important;
        border-top-right-radius: 0 !important;
        border-top: 1px solid #f3f4f6 !important;
        margin-top: -10px !important;  /* pull up over the flex gap */
        padding-top: 12px !important;
    }
    /* Section headings (Recent Bookings / Recent Invoices) */
    #customerModal .modal-body h4 {
        font-size: 0.95rem !important;
        margin: 14px 0 8px !important;
        color: #111827 !important;
    }
    /* Collapsible section headings — chevron on the right, rotates
       180deg when expanded. */
    #customerModal .modal-body h4.cm-collapsible {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        padding: 10px 12px !important;
        background: #fff !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 10px !important;
        margin: 12px 0 8px !important;
    }
    #customerModal .modal-body h4.cm-collapsible .cm-chev {
        display: inline-flex !important;
        color: #6b7280 !important;
        transition: transform 0.2s ease !important;
    }
    #customerModal .modal-body h4.cm-collapsible:not(.is-collapsed) .cm-chev {
        transform: rotate(180deg) !important;
    }
    /* Convert the Reservas Recentes + Faturas Recentes tables into
       stacked cards — much more readable than a horizontally-scrolled
       table with truncated columns. Uses CSS `display: block` on tr/td
       so we don't need to touch the render JS. Each td prefixes itself
       with the column label via `data-label` (fallback: uses text-only
       so users can still read the value). */
    #customerModal .modal-body .table-container {
        max-height: none !important;
        border: none !important;
        overflow: visible !important;
        background: transparent !important;
    }
    #customerModal .modal-body .data-table {
        width: 100% !important;
        min-width: 0 !important;    /* base CSS has min-width: 800px */
        display: block !important;
        border: none !important;
        background: transparent !important;
        margin-bottom: 4px !important;
    }
    /* Cards: also constrain to parent width */
    #customerModal .modal-body .data-table tbody,
    #customerModal .modal-body .data-table tbody tr {
        width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
    }
    #customerModal .modal-body .data-table tbody td {
        max-width: 100% !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }
    /* Kill the table header row on mobile */
    #customerModal .modal-body .data-table thead {
        display: none !important;
    }
    /* Body/rows: each <tr> becomes a card */
    #customerModal .modal-body .data-table tbody {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
    }
    #customerModal .modal-body .data-table tbody tr {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: wrap !important;
        align-items: center !important;
        background: #fff !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 10px !important;
        padding: 10px 12px !important;
        gap: 4px 6px !important;
        box-shadow: none !important;
    }
    /* Non-badge tds span the full row (behave like block). Badge tds
       stay inline so they can pair up. */
    #customerModal .modal-body .data-table tbody td:not(:has(.status-badge)):not(:has(span[style*="9999px"])) {
        flex: 1 1 100% !important;
    }
    /* Each td becomes a row inside the card, with the first td styled
       as the "title" (reference / invoice number). */
    #customerModal .modal-body .data-table tbody td {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        font-size: 0.85rem !important;
        color: #4b5563 !important;
    }
    #customerModal .modal-body .data-table tbody td:first-child {
        font-weight: 700 !important;
        color: #111827 !important;
        font-size: 0.9rem !important;
    }
    /* Second td (service / date) — muted */
    #customerModal .modal-body .data-table tbody td:nth-child(2) {
        color: #374151 !important;
    }
    /* Status pill td: normalize the badge look */
    #customerModal .modal-body .data-table tbody td .status-badge {
        display: inline-block !important;
        padding: 3px 10px !important;
        border-radius: 999px !important;
        font-size: 0.7rem !important;
        font-weight: 700 !important;
        text-transform: uppercase !important;
        letter-spacing: 0.03em !important;
    }
    /* Any td that contains a status badge collapses inline so ENVIADA +
       NÃO PAGO sit side-by-side on invoice cards. Booking status pills
       use `.status-badge`; invoice pills use inline-styled spans with
       `border-radius: 9999px` — match both. */
    #customerModal .modal-body .data-table tbody td:has(.status-badge),
    #customerModal .modal-body .data-table tbody td:has(span[style*="9999px"]) {
        display: inline-block !important;
        width: auto !important;
        max-width: none !important;
        margin-right: 6px !important;
        padding: 0 !important;
        white-space: normal !important;
        overflow: visible !important;
    }

    /* Bottom action buttons row — pushed to the viewport bottom via
       `margin-top: auto` (parent modal-body is flex-column), and
       sticky on scroll so it stays glued even when the list is long. */
    #customerModal .modal-body > div:last-child[style*="display: flex"][style*="justify-content: flex-end"] {
        margin: auto -12px 0 !important;
        position: sticky !important;
        bottom: 0 !important;
        padding: 12px 12px calc(12px + env(safe-area-inset-bottom)) !important;
        background: #fff !important;
        border-top: 1px solid #e5e7eb !important;
        justify-content: stretch !important;
        gap: 8px !important;
        flex-shrink: 0 !important;
    }
    #customerModal .modal-body > div:last-child[style*="display: flex"][style*="justify-content: flex-end"] > .btn {
        flex: 1 !important;
        height: 44px !important;
        border-radius: 10px !important;
        box-shadow: none !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
    /* Colour scheme — primary=purple gradient, secondary=grey outlined,
       warning=amber. Consistent with drawers. */
    #customerModal .modal-body > div:last-child .btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
    }
    #customerModal .modal-body > div:last-child .btn-secondary {
        background: #fff !important;
        color: #4338ca !important;
        border: 1.5px solid #c7d2fe !important;
    }
    #customerModal .modal-body > div:last-child .btn-warning {
        background: #fef3c7 !important;
        color: #92400e !important;
        border: 1.5px solid #fcd34d !important;
    }
}

@media (max-width: 767px) {
    /* Kill the title/filter card + the "Clientes" section title.
       The section header has an inline `display: flex !important` so we
       target the h3 directly (no specificity fight) and force-hide the
       header via multiple selectors. */
    #customers > .modern-content-card.u-mb-4,
    #customers > .modern-content-card .modern-section-header,
    #customers > .modern-content-card > .modern-section-header {
        display: none !important;
    }
    #customers .modern-section-title,
    #customers h3.modern-section-title {
        display: none !important;
    }

    /* Flatten the section wrapper — cards float directly on page */
    #customers > .modern-content-card {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin: 0 !important;
        border-radius: 0 !important;
    }

    /* Hide the desktop table + pagination */
    #customers .table-container,
    #customers table {
        display: none !important;
    }
    #customers .email-logs-pagination {
        padding: 12px 4px !important;
        border-top: none !important;
        font-size: 0.85rem !important;
    }

    /* Top search bar — pinned right below the 56px mobile header, same
       as the bookings page. */
    #customers .cm-mobile-header {
        display: flex !important;
        gap: 8px;
        padding: 0.55rem 0.75rem;
        position: fixed !important;
        top: 56px;
        left: 0;
        right: 0;
        z-index: 990;
        background: #ffffff;
        border-bottom: 1px solid #e5e7eb;
    }
    #customers .cm-top-search-input {
        flex: 1;
        min-width: 0;
        height: 40px;
        padding: 0.55rem 0.75rem;
        border: 1px solid #d1d5db;
        border-radius: 8px;
        font-size: 0.9rem;
        background: #fff;
        color: #111827;
        outline: none;
    }
    #customers .cm-top-search-input:focus {
        border-color: #6366f1;
    }

    /* Cards container — top padding accounts for the fixed search bar. */
    #customers .cm-cards {
        display: flex !important;
        flex-direction: column;
        gap: 10px;
        padding: 60px 4px 90px; /* fixed search + FAB + tab bar */
    }

    /* Individual card */
    .cm-card {
        background: #fff;
        border: 1px solid #e5e7eb;
        border-radius: 12px;
        padding: 12px 14px;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    }
    .cm-card .cm-card-head {
        display: flex;
        align-items: flex-start;
        gap: 12px;
    }
    /* Avatar hidden per user request — cleaner cards */
    .cm-card .cm-avatar {
        display: none !important;
    }
    .cm-card .cm-card-body {
        flex: 1;
        min-width: 0;
    }
    .cm-card .cm-name {
        font-size: 1rem;
        font-weight: 700;
        color: #111827;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .cm-card .cm-email {
        font-size: 0.85rem;
        color: #4b5563;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin-top: 2px;
    }
    .cm-card .cm-phone {
        font-size: 0.8rem;
        color: #6b7280;
        margin-top: 2px;
    }
    .cm-card .cm-more {
        width: 32px;
        height: 32px;
        border-radius: 8px;
        border: none;
        background: transparent;
        color: #6b7280;
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }
    .cm-card .cm-more:active { background: #f3f4f6; }
    .cm-card .cm-meta {
        display: flex;
        gap: 6px;
        flex-wrap: wrap;
        margin-top: 8px;
    }
    .cm-card .cm-chip {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 4px 10px;
        border-radius: 999px;
        background: #f3f4f6;
        color: #4b5563;
        font-size: 0.7rem;
        font-weight: 600;
    }
    .cm-card .cm-chip svg {
        color: #6b7280;
        flex-shrink: 0;
    }
    /* Money chip — pick out with the same amber tone the coin icon uses
       in the bookings-table payment badge (pending state, tan/gold). */
    .cm-card .cm-chip.cm-chip-money {
        background: #fef3c7;
        color: #78350f;
    }
    .cm-card .cm-chip.cm-chip-money svg {
        color: #78350f;
    }
    .cm-card .cm-phone {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        font-size: 0.8rem;
        color: #6b7280;
        margin-top: 2px;
    }
    .cm-card .cm-phone svg {
        color: #9ca3af;
        flex-shrink: 0;
    }

    /* Floating Action Button — purple gradient, matches the app FAB */
    #customers .cm-fab {
        display: inline-flex !important;
        position: fixed;
        bottom: 90px;
        right: 16px;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: #fff;
        border: 2px solid rgba(255, 255, 255, 0.2);
        box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
        align-items: center;
        justify-content: center;
        cursor: pointer;
        z-index: 900;
    }
    #customers .cm-fab:active { transform: scale(0.95); }

    /* Bottom action sheet (opens on 3-dot tap) */
    .cm-sheet-wrap {
        position: fixed;
        inset: 0;
        z-index: 1100;
        pointer-events: none;
    }
    .cm-sheet-wrap.is-open { pointer-events: auto; }
    .cm-sheet-backdrop {
        position: absolute;
        inset: 0;
        background: rgba(0, 0, 0, 0);
        transition: background 0.2s;
    }
    .cm-sheet-wrap.is-open .cm-sheet-backdrop { background: rgba(0, 0, 0, 0.35); }
    .cm-sheet {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fff;
        border-radius: 20px 20px 0 0;
        padding: 0.5rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
        transform: translateY(100%);
        transition: transform 0.22s cubic-bezier(0.32, 0.72, 0, 1);
        max-height: 82vh;
        overflow-y: auto;
    }
    .cm-sheet-wrap.is-open .cm-sheet { transform: translateY(0); }
    .cm-sheet-handle {
        width: 40px;
        height: 4px;
        background: #d1d5db;
        border-radius: 2px;
        margin: 8px auto 12px;
    }
    .cm-sheet-item {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 12px 8px;
        background: transparent;
        border: none;
        cursor: pointer;
        text-align: left;
        width: 100%;
    }
    .cm-sheet-item:active { background: #f3f4f6; }
    .cm-sheet-icon {
        width: 32px;
        height: 32px;
        border-radius: 8px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }
    .cm-sheet-label {
        font-size: 0.95rem;
        font-weight: 600;
        color: #111827;
    }
    .cm-sheet-divider {
        height: 1px;
        background: #f3f4f6;
        margin: 6px 8px;
    }
    .cm-sheet-cancel {
        width: 100%;
        margin-top: 8px;
        padding: 12px;
        background: #f3f4f6;
        border: none;
        border-radius: 10px;
        font-size: 0.95rem;
        font-weight: 600;
        color: #374151;
        cursor: pointer;
    }
}
