/* =========================================================================
   User Management page — mobile makeover.
   Scoped to #user-management, fires under 768px. Matches the Bookings
   pattern: fixed search+funnel on top, filters live in a right-side drawer,
   users table converted to cards, purple FAB for Add User, modals turned
   into full-viewport frames with sticky action bars.
   ========================================================================= */

/* Mobile chrome hidden by default so it can't leak into desktop. */
#umMobileHeader { display: none; }

/* Drawer styles are outside the media query so the sliding animation still
   plays correctly even at the boundary. */
.um-drawer-wrap {
    position: fixed; inset: 0; z-index: 10000;
}
.um-drawer-backdrop {
    position: absolute; inset: 0;
    background: rgba(0, 0, 0, 0);
    transition: background 0.2s;
}
.um-drawer-wrap.is-open .um-drawer-backdrop { background: rgba(0, 0, 0, 0.35); }
.um-drawer {
    position: absolute; right: 0; top: 0; bottom: 0;
    width: 88%; max-width: 380px;
    background: #fff;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.15);
    transform: translateX(100%);
    transition: transform 0.22s ease-out;
    display: flex; flex-direction: column;
}
.um-drawer-wrap.is-open .um-drawer { transform: translateX(0); }
.um-drawer-head {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1rem 1.15rem;
    border-bottom: 1px solid #e5e7eb;
    font-weight: 700; font-size: 1.05rem;
    color: #111827;
}
.um-drawer-close {
    border: none; background: transparent; font-size: 1.5rem;
    color: #6b7280; cursor: pointer; line-height: 1;
}
.um-drawer-body {
    padding: 1rem 1.15rem;
    overflow-y: auto; flex: 1;
    display: flex; flex-direction: column;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.um-drawer-body > .form-group {
    display: block !important;
    width: 100% !important;
    margin: 0 0 12px 0 !important;
}
.um-drawer-body > .form-group > label {
    font-size: 0.85rem !important;
    font-weight: 700 !important;
    color: #111827 !important;
    margin-bottom: 6px !important;
    display: block !important;
}
.um-drawer-body select {
    width: 100% !important;
    min-height: 42px !important;
    padding: 8px 12px !important;
    border: 1px solid #d1d5db !important;
    border-radius: 8px !important;
    background: #fff !important;
    font-size: 0.95rem !important;
}
/* Pills row for role / status */
.um-pill-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 8px;
    margin-top: 4px;
}
.um-pill {
    padding: 8px 10px;
    border: 1px solid #e5e7eb;
    border-radius: 999px;
    background: #fff;
    color: #374151;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.um-pill.selected {
    background: #eef2ff;
    color: #4338ca;
    border-color: #6366f1;
    font-weight: 600;
}
/* Clear filters button — pinned bottom, full width */
.um-drawer-body > .form-group:last-child {
    margin-top: auto !important;
    padding-top: 12px !important;
    border-top: 1px solid #f3f4f6;
}
.um-drawer-body > .form-group:last-child > label {
    display: none !important;
}
.um-drawer-body > .form-group:last-child > .btn {
    width: 100% !important;
    height: 42px !important;
    border-radius: 10px !important;
    background: #f3f4f6 !important;
    color: #374151 !important;
    border: 1px solid #e5e7eb !important;
    font-weight: 600 !important;
    justify-content: center !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 4px;
}

/* Bottom action sheet (Edit / Reset / Delete) — matches bookings' style */
.um-sheet-wrap {
    position: fixed; inset: 0; z-index: 10000;
    display: flex; flex-direction: column; justify-content: flex-end;
    pointer-events: none;
}
.um-sheet-backdrop {
    position: absolute; inset: 0;
    background: rgba(0, 0, 0, 0);
    transition: background 0.2s;
    pointer-events: auto;
}
.um-sheet-wrap.is-open .um-sheet-backdrop { background: rgba(0, 0, 0, 0.4); }
.um-sheet {
    position: relative;
    background: #fff;
    border-radius: 16px 16px 0 0;
    padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
    transform: translateY(100%);
    transition: transform 0.22s ease-out;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.15);
    pointer-events: auto;
    max-height: 80vh;
    overflow-y: auto;
}
.um-sheet-wrap.is-open .um-sheet { transform: translateY(0); }
.um-sheet-handle {
    width: 40px; height: 4px;
    border-radius: 2px;
    background: #e5e7eb;
    margin: 4px auto 12px;
}
.um-sheet-title {
    font-weight: 700; font-size: 0.95rem;
    color: #111827; text-align: center;
    margin-bottom: 8px;
    padding: 0 8px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.um-sheet-list {
    display: flex; flex-direction: column;
    gap: 2px;
    margin-bottom: 8px;
}
.um-sheet-item {
    display: flex; align-items: center; justify-content: center; gap: 12px;
    padding: 12px 8px;
    border: none;
    background: transparent;
    color: #111827;
    font-size: 0.95rem;
    font-weight: 500;
    text-align: left;
    cursor: pointer;
    border-radius: 10px;
}
.um-sheet-item:active { background: #f3f4f6; }
.um-sheet-item.is-danger { color: #991b1b; }
.um-sheet-icon {
    width: 36px; height: 36px;
    border-radius: 10px;
    display: inline-flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.um-sheet-label { flex: 0 0 auto; }
.um-sheet-divider {
    height: 1px;
    background: #f3f4f6;
    margin: 6px 8px;
}
.um-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;
}

@media (max-width: 767px) {

    /* -----------------------------------------------------------------
       Page shell — leave room for the fixed top search bar. Matches the
       bookings pattern: mobile-optimized.css already gives .main-content
       a 56px top margin for the app header, we add another 56px here for
       the fixed search bar.
       ----------------------------------------------------------------- */
    #user-management {
        padding-top: 56px !important;
        padding-bottom: 90px !important;
        padding-left: 8px !important;
        padding-right: 8px !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }

    /* Hide the entire desktop filter card — its contents are relocated
       into the drawer on demand by user-management-mobile.js. */
    #user-management > .modern-content-card {
        display: none !important;
    }

    /* -----------------------------------------------------------------
       Fixed mobile search + funnel bar, pinned below the 56px app header.
       ----------------------------------------------------------------- */
    #umMobileHeader {
        display: flex !important;
        gap: 8px;
        position: fixed !important;
        top: 56px; left: 0; right: 0;
        z-index: 990;
        padding: 8px 12px;
        background: #fff;
        border-bottom: 1px solid #e5e7eb;
    }
    #umMobileHeader #umMobileSearch {
        flex: 1;
        height: 40px;
        padding: 8px 12px;
        border: 1px solid #d1d5db;
        border-radius: 8px;
        font-size: 0.9rem;
        background: #fff;
        color: #111827;
        outline: none;
    }
    #umMobileHeader #umMobileSearch:focus {
        border-color: #6366f1;
    }
    #umMobileHeader .um-filter-btn {
        flex-shrink: 0;
        width: 40px; height: 40px;
        border-radius: 8px;
        border: 1px solid #d1d5db;
        background: #fff;
        color: #4b5563;
        display: inline-flex;
        align-items: center; justify-content: center;
        cursor: pointer;
        position: relative;
    }
    #umMobileHeader .um-filter-btn[data-has-filters="1"]::after {
        content: "";
        position: absolute; top: 6px; right: 6px;
        width: 8px; height: 8px;
        border-radius: 50%;
        background: #6366f1;
    }

    /* -----------------------------------------------------------------
       Users list — no wrapper card, no "Utilizadores" title. Cards
       float free directly on the page background, matching Bookings.
       ----------------------------------------------------------------- */
    #user-management .content-section {
        background: transparent !important;
        border: none !important;
        border-radius: 0 !important;
        padding: 0 !important;
        margin: 0 !important;
        box-shadow: none !important;
        overflow: visible !important;
    }
    /* Collapse the "Utilizadores" section header to zero height.
       We can't use `display: none` because the Add-User button lives
       inside it and needs to remain in the DOM (even though it's
       positioned as a fixed FAB).
       Note: `#user-management .content-section > div:first-of-type`
       in mobile-optimized.css:3084 forces white background + padding.
       We match that selector shape (plus .section-header) to guarantee
       higher specificity. */
    #user-management .content-section > .section-header,
    #user-management .content-section > div:first-of-type.section-header,
    #user-management .content-section > div:first-of-type {
        display: block !important;
        height: 0 !important;
        min-height: 0 !important;
        padding: 0 !important;
        margin: 0 !important;
        border: none !important;
        background: transparent !important;
        border-radius: 0 !important;
        overflow: visible !important;   /* let the fixed button still paint */
    }
    #user-management .content-section > .section-header > .section-title {
        display: none !important;
    }
    /* Kill the empty messages div too */
    #user-management > #userMessages:empty {
        display: none !important;
    }
    /* Floating action button — Add New User. Rounded square (16px) to
       match how Bookings' FAB renders visually. */
    #user-management .content-section > .section-header > .btn.btn-success {
        position: fixed !important;
        bottom: 90px !important;
        right: 16px !important;
        z-index: 900 !important;
        width: 56px !important;
        height: 56px !important;
        border-radius: 12px !important;
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: 2px solid rgba(255, 255, 255, 0.2) !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4) !important;
        cursor: pointer !important;
        padding: 0 !important;
        margin: 0 !important;
        font-size: 0 !important;  /* hide the text label */
        overflow: hidden !important;
    }
    /* Hide the original "Add New User" SVG (person + silhouette) — it
       renders visually tighter than Bookings' plain "+" and makes the
       FAB look "more circular". We replace it with a bold "+" glyph via
       ::before so both pages read the same. */
    #user-management .content-section > .section-header > .btn.btn-success svg,
    #user-management .content-section > .section-header > .btn.btn-success span {
        display: none !important;
    }
    #user-management .content-section > .section-header > .btn.btn-success::before {
        content: "+";
        color: #fff !important;
        font-size: 28px !important;
        font-weight: 400 !important;
        line-height: 1 !important;
        display: block !important;
        margin-top: -2px !important;  /* optical vertical centering */
    }
    #user-management .content-section > .section-header > .btn.btn-success:active {
        transform: scale(0.95);
    }

    /* Users table → cards */
    #user-management .content-section .table-container {
        overflow-x: visible !important;
        padding: 0 !important;
        background: transparent !important;
        border: none !important;
        margin: 0 !important;
    }
    #user-management .content-section .data-table {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        background: transparent !important;
        border: none !important;
    }
    #user-management .content-section .data-table thead {
        display: none !important;
    }
    #user-management .content-section .data-table tbody {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
    }
    #user-management .content-section .data-table tbody tr {
        display: grid !important;
        grid-template-columns: 1fr auto !important;
        gap: 6px 12px !important;
        background: #f9fafb !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 12px !important;
        padding: 12px !important;
    }
    #user-management .content-section .data-table tbody tr:has(td[colspan]) {
        display: block !important;
        text-align: center !important;
        padding: 20px 12px !important;
        color: #9ca3af !important;
    }
    #user-management .content-section .data-table tbody tr:has(td[colspan]) td {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        text-align: center !important;
    }
    #user-management .content-section .data-table tbody td {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        font-size: 0.85rem !important;
        color: #4b5563 !important;
    }
    /* Name (col 1): bold title, sits in row 1 col 1 next to the 3-dot */
    #user-management .content-section .data-table tbody td:nth-child(1) {
        grid-row: 1 !important;
        grid-column: 1 !important;
        font-weight: 700 !important;
        color: #111827 !important;
        font-size: 0.95rem !important;
        text-align: left !important;
    }
    /* Email (col 2): full width, muted */
    #user-management .content-section .data-table tbody td:nth-child(2) {
        grid-column: 1 / -1 !important;
        color: #6b7280 !important;
        font-size: 0.85rem !important;
        overflow-wrap: anywhere !important;
        margin-bottom: 4px !important;
    }
    /* Role + Status badges: inline pills */
    #user-management .content-section .data-table tbody td:nth-child(3),
    #user-management .content-section .data-table tbody td:nth-child(4) {
        display: inline-flex !important;
        align-items: center !important;
    }
    /* Last Login + Created: label:value style, full width */
    #user-management .content-section .data-table tbody td:nth-child(5),
    #user-management .content-section .data-table tbody td:nth-child(6) {
        grid-column: 1 / -1 !important;
        font-size: 0.75rem !important;
        color: #9ca3af !important;
    }
    #user-management .content-section .data-table tbody td:nth-child(5)::before {
        content: "Último login: ";
        font-weight: 600 !important;
        color: #6b7280 !important;
    }
    #user-management .content-section .data-table tbody td:nth-child(6)::before {
        content: "Criado: ";
        font-weight: 600 !important;
        color: #6b7280 !important;
    }
    /* Actions cell (col 7): floated as a 3-dot button in the top-right
       corner of the card. JS injects a <button.um-more-btn>; the rest of
       this cell renders empty. */
    #user-management .content-section .data-table tbody td:nth-child(7) {
        grid-row: 1 !important;
        grid-column: 2 !important;
        justify-self: end !important;
        align-self: start !important;
        display: block !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
    }
    #user-management .um-more-btn {
        width: 36px; height: 36px;
        border: none;
        background: #f3f4f6;
        color: #4b5563;
        border-radius: 8px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        padding: 0;
    }
    #user-management .um-more-btn:active {
        background: #e5e7eb;
    }

    /* Role/status badges — compact */
    #user-management .role-badge,
    #user-management .status-badge {
        display: inline-block !important;
        padding: 3px 8px !important;
        border-radius: 999px !important;
        font-size: 0.7rem !important;
        font-weight: 600 !important;
        text-transform: uppercase !important;
        letter-spacing: 0.02em !important;
    }

    /* -----------------------------------------------------------------
       User modal (userModal, .settings-modal) — full-viewport frame.
       ----------------------------------------------------------------- */
    #userModal.settings-modal,
    #deleteUserModal.settings-modal {
        padding: 0 !important;
        align-items: stretch !important;
        justify-content: stretch !important;
    }
    #userModal .modal-content,
    #deleteUserModal .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;
        background: #fff !important;
        border: none !important;
    }
    #userModal .modal-header,
    #deleteUserModal .modal-header {
        background: #fff !important;
        color: #111827 !important;
        border-bottom: 1px solid #f3f4f6 !important;
        padding: 14px 20px !important;
        flex-shrink: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
    }
    #userModal .modal-header .modal-title,
    #userModal .modal-header h3,
    #deleteUserModal .modal-header .modal-title,
    #deleteUserModal .modal-header h3 {
        color: #111827 !important;
        font-size: 1.05rem !important;
        font-weight: 700 !important;
        margin: 0 !important;
    }
    #userModal .modal-header .close,
    #deleteUserModal .modal-header .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 !important;
        padding: 0 !important;
    }
    #userModal .modal-body,
    #deleteUserModal .modal-body {
        padding: 14px !important;
        background: #f9fafb !important;
        overflow-y: auto !important;
        flex: 1 !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior: contain !important;
        transform: translateZ(0);
        contain: paint;
    }

    /* Tabs (Profile / Security / Notifications) — only style when the
       modal actually shows them (edit own account). New-user create
       keeps them hidden via inline `style="display: none"`, so we don't
       force a display value here. */
    #userModal .tour-tabs {
        gap: 4px !important;
        overflow-x: auto !important;
        margin: 0 -14px 12px -14px !important;
        padding: 0 14px !important;
        border-bottom: 1px solid #f3f4f6 !important;
        background: #fff !important;
    }
    #userModal .tour-tab {
        flex-shrink: 0 !important;
        padding: 10px 12px !important;
        font-size: 0.85rem !important;
        white-space: nowrap !important;
    }
    #userModal .tour-tab svg {
        width: 14px !important;
        height: 14px !important;
    }

    /* Form fills the body so `margin-top: auto` on the buttons row
       actually pins them to the viewport bottom (sticky only pins if
       content is taller than the container). */
    #userModal #userForm {
        display: flex !important;
        flex-direction: column !important;
        min-height: 100% !important;
    }
    /* Only stretch the ACTIVE tab pane (whichever one isn't inline-
       hidden) — otherwise `display: flex !important` overrides the
       modal's own `style="display: none"` on inactive tabs and every
       tab content stacks at once. */
    #userModal .account-tab-content:not([style*="display: none"]):not([style*="display:none"]) {
        display: flex !important;
        flex-direction: column !important;
        flex: 1 !important;
        min-height: 100% !important;
    }
    /* Form grids in the modal — single column */
    #userModal .form-grid {
        display: block !important;
    }
    #userModal .form-group {
        margin-bottom: 12px !important;
    }
    #userModal .form-group > label {
        font-size: 0.85rem !important;
        font-weight: 600 !important;
        color: #374151 !important;
        display: block !important;
        margin-bottom: 4px !important;
    }
    #userModal .form-group input:not([type="checkbox"]):not([type="radio"]),
    #userModal .form-group select,
    #userModal .form-group textarea {
        width: 100% !important;
        min-height: 42px !important;
        padding: 8px 12px !important;
        border: 1px solid #d1d5db !important;
        border-radius: 8px !important;
        font-size: 0.95rem !important;
        background: #fff !important;
        box-sizing: border-box !important;
    }
    #userModal .form-group input:focus,
    #userModal .form-group select:focus,
    #userModal .form-group textarea:focus {
        outline: none !important;
        border-color: #6366f1 !important;
        box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
    }
    #userModal .form-group small {
        font-size: 0.75rem !important;
        color: #6b7280 !important;
        margin-top: 4px !important;
        display: block !important;
    }

    /* Bottom actions (Cancel + Save) — pinned to viewport bottom via
       margin-top: auto (form fills the body). Also sticky as a fallback
       when content is taller than viewport. */
    #userModal #profileSaveButtons,
    #userModal .account-tab-content > div[style*="text-align: right"]:last-child,
    #userModal .account-tab-content > div[style*="justify-content: flex-end"]:last-child {
        margin: auto -14px 0 -14px !important;
        padding: 12px 14px calc(12px + env(safe-area-inset-bottom)) !important;
        position: sticky !important;
        bottom: 0 !important;
        background: #fff !important;
        border-top: 1px solid #f3f4f6 !important;
        display: flex !important;
        gap: 8px !important;
        justify-content: stretch !important;
        text-align: left !important;
        z-index: 5 !important;
    }
    #userModal #profileSaveButtons > .btn,
    #userModal .account-tab-content > div[style*="text-align: right"]:last-child > .btn,
    #userModal .account-tab-content > div[style*="justify-content: flex-end"]: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;
        padding: 0 12px !important;
    }
    #userModal .btn-secondary {
        background: #f3f4f6 !important;
        color: #374151 !important;
        border: 1px solid #e5e7eb !important;
    }
    #userModal .btn-success {
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
    }

    /* -----------------------------------------------------------------
       Delete User modal — soft warning styling, sticky action row.
       ----------------------------------------------------------------- */
    #deleteUserModal .modal-body .u-text-center {
        text-align: center !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 100% !important;
    }
    #deleteUserModal .modal-body h3 {
        color: #dc2626 !important;
        font-size: 1.05rem !important;
        margin: 12px 0 8px 0 !important;
    }
    #deleteUserModal .modal-body p {
        font-size: 0.9rem !important;
        color: #6b7280 !important;
    }
    #deleteUserModal #deleteUserDetails {
        background: #fff !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 10px !important;
        padding: 12px 14px !important;
        margin: 14px 0 !important;
        text-align: left !important;
        font-size: 0.85rem !important;
    }
    #deleteUserModal .modal-body > div > div[style*="justify-content: center"]:last-child {
        margin-top: auto !important;
        margin-left: -14px !important;
        margin-right: -14px !important;
        padding: 12px 14px calc(12px + env(safe-area-inset-bottom)) !important;
        position: sticky !important;
        bottom: 0 !important;
        background: #fff !important;
        border-top: 1px solid #f3f4f6 !important;
        display: flex !important;
        gap: 8px !important;
        justify-content: stretch !important;
    }
    #deleteUserModal .modal-body > div > div[style*="justify-content: center"]: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;
    }
    #deleteUserModal .btn-secondary {
        background: #f3f4f6 !important;
        color: #374151 !important;
        border: 1px solid #e5e7eb !important;
    }
    #deleteUserModal .btn-danger {
        background: #dc2626 !important;
        color: #fff !important;
        border: none !important;
    }
}
