/* =========================================================================
   Support Tickets page — mobile makeover.
     - Fixed top search bar + funnel drawer (Bookings pattern)
     - "New Ticket" → purple FAB
     - Table → cards with 3-dot ⋯ sheet (View / Delete)
     - New Ticket + Ticket Detail modals full-viewport with sticky bottom
   ========================================================================= */

#stMobileHeader { display: none; }

/* Filter drawer */
.st-drawer-wrap { position: fixed; inset: 0; z-index: 10000; }
.st-drawer-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0); transition: background 0.2s; }
.st-drawer-wrap.is-open .st-drawer-backdrop { background: rgba(0,0,0,0.35); }
.st-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;
}
.st-drawer-wrap.is-open .st-drawer { transform: translateX(0); }
.st-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;
}
.st-drawer-close {
    border: none; background: transparent; font-size: 1.5rem;
    color: #6b7280; cursor: pointer; line-height: 1;
}
.st-drawer-body {
    padding: 1rem 1.15rem;
    overflow-y: auto; flex: 1;
    display: flex; flex-direction: column;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.st-drawer-body > .form-group {
    display: block !important;
    width: 100% !important;
    margin: 0 0 14px 0 !important;
}
.st-drawer-body > .form-group > label {
    font-size: 0.85rem !important;
    font-weight: 700 !important;
    color: #111827 !important;
    margin-bottom: 6px !important;
    display: block !important;
}
.st-drawer-body select,
.st-drawer-body input {
    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;
    box-sizing: border-box !important;
}
/* Buttons row (Reset + New Ticket) at bottom */
.st-drawer-body > .form-group:last-child {
    margin-top: auto !important;
    padding-top: 12px !important;
    border-top: 1px solid #f3f4f6;
    margin-bottom: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
}
.st-drawer-body > .form-group:last-child button {
    width: 100% !important;
    height: 42px !important;
    border-radius: 10px !important;
    background: #f3f4f6 !important;
    color: #374151 !important;
    border: 1px solid #e5e7eb !important;
    font-size: 0.85rem !important;
    font-weight: 600 !important;
    padding: 0 8px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px !important;
    white-space: nowrap !important;
}
.st-drawer-body > .form-group:last-child button[onclick*="openNewTicketModal"] {
    background: linear-gradient(135deg, #667eea, #764ba2) !important;
    color: #fff !important;
    border: none !important;
}

/* Bottom action sheet */
.st-sheet-wrap { position: fixed; inset: 0; z-index: 10000; display: flex; flex-direction: column; justify-content: flex-end; pointer-events: none; }
.st-sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0); transition: background 0.2s; pointer-events: auto; }
.st-sheet-wrap.is-open .st-sheet-backdrop { background: rgba(0,0,0,0.4); }
.st-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: 85vh; overflow-y: auto; }
.st-sheet-wrap.is-open .st-sheet { transform: translateY(0); }
.st-sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: #e5e7eb; margin: 4px auto 12px; }
.st-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; }
.st-sheet-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.st-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; }
.st-sheet-item:active { background: #f3f4f6; }
.st-sheet-item.is-danger { color: #991b1b; }
.st-sheet-icon { width: 36px; height: 36px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
.st-sheet-label { flex: 0 0 auto; }
.st-sheet-divider { height: 1px; background: #f3f4f6; margin: 6px 8px; }
.st-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; }

/* FAB — hidden by default so it can't leak into desktop; revealed inside
   the mobile media query below. Same visual pattern as bookings/gallery. */
.st-fab { display: none; }

@media (max-width: 767px) {

    /* Mobile-only FAB — sits above the bottom tab bar (~76px) in the
       bottom-right corner. Purple gradient matches the rest of the app. */
    .st-fab {
        display: inline-flex !important;
        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;
        align-items: center !important;
        justify-content: center !important;
        box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4) !important;
        padding: 0 !important;
        cursor: pointer !important;
    }
    .st-fab:active { transform: scale(0.95) !important; }
    .st-fab svg { width: 22px; height: 22px; color: #fff; }

    #support-tickets {
        padding-top: 56px !important;
        padding-bottom: 90px !important;
        padding-left: 8px !important;
        padding-right: 8px !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }
    #support-tickets .page-content {
        padding: 0 !important;
    }
    /* Hide desktop filter card entirely */
    #support-tickets .modern-content-card:first-of-type {
        display: none !important;
    }

    /* Fixed mobile search + funnel */
    #stMobileHeader {
        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;
    }
    #stMobileHeader #stMobileSearch {
        flex: 1;
        height: 40px;
        padding: 8px 12px;
        border: 1px solid #d1d5db;
        border-radius: 8px;
        font-size: 0.9rem;
        background: #fff;
        color: #111827;
        outline: none;
    }
    #stMobileHeader #stMobileSearch:focus { border-color: #6366f1; }
    #stMobileHeader .st-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;
    }
    #stMobileHeader .st-filter-btn[data-has-filters="1"]::after {
        content: "";
        position: absolute; top: 6px; right: 6px;
        width: 8px; height: 8px;
        border-radius: 50%;
        background: #6366f1;
    }

    /* Tickets section — strip wrapper */
    #support-tickets .modern-content-card:last-of-type {
        background: transparent !important;
        border: none !important;
        border-radius: 0 !important;
        padding: 0 !important;
        margin: 0 !important;
        box-shadow: none !important;
    }
    #support-tickets .modern-section-header {
        padding: 4px 4px 8px !important;
        margin: 0 !important;
        background: transparent !important;
        border: none !important;
    }
    #support-tickets .modern-section-title {
        font-size: 1rem !important;
        font-weight: 700 !important;
        color: #111827 !important;
        margin: 0 !important;
    }

    /* Table → cards. Columns:
       Ticket# | Subject | Category | Priority | Status | Created | Responses | Actions */
    #support-tickets .table-container { overflow-x: visible !important; padding: 0 !important; border: none !important; }
    #support-tickets .data-table { display: block !important; width: 100% !important; min-width: 0 !important; background: transparent !important; border: none !important; }
    #support-tickets .data-table thead { display: none !important; }
    #support-tickets .data-table tbody { display: flex !important; flex-direction: column !important; gap: 8px !important; }
    #support-tickets .data-table tbody tr {
        display: grid !important;
        grid-template-columns: 1fr auto !important;
        gap: 6px 12px !important;
        background: #fff !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 10px !important;
        padding: 12px !important;
    }
    #support-tickets .data-table tbody tr:has(td[colspan]) {
        display: block !important;
        text-align: center !important;
        padding: 24px 12px !important;
        color: #9ca3af !important;
    }
    #support-tickets .data-table tbody tr:has(td[colspan]) td { display: block !important; padding: 0 !important; border: none !important; text-align: center !important; }
    #support-tickets .data-table tbody td {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        background: transparent !important;
        font-size: 0.85rem !important;
        color: #4b5563 !important;
    }
    /* Subject (col 2) — bold title, top-left */
    #support-tickets .data-table tbody td:nth-child(2) {
        grid-row: 1 !important;
        grid-column: 1 !important;
        font-weight: 700 !important;
        color: #111827 !important;
        font-size: 0.95rem !important;
        text-align: left !important;
        overflow-wrap: anywhere !important;
    }
    /* Actions (col 8) — 3-dot in row 1 col 2 */
    #support-tickets .data-table tbody td:nth-child(8) {
        grid-row: 1 !important;
        grid-column: 2 !important;
        justify-self: end !important;
        align-self: start !important;
        display: block !important;
    }
    /* Ticket # (col 1) — small muted line */
    #support-tickets .data-table tbody td:nth-child(1) {
        grid-column: 1 / -1 !important;
        font-size: 0.75rem !important;
        color: #9ca3af !important;
    }
    #support-tickets .data-table tbody td:nth-child(1)::before {
        content: "Nº: ";
        font-weight: 600 !important;
        color: #6b7280 !important;
    }
    /* Category (col 3) + Priority (col 4) — inline pills row */
    #support-tickets .data-table tbody td:nth-child(3),
    #support-tickets .data-table tbody td:nth-child(4),
    #support-tickets .data-table tbody td:nth-child(5) {
        grid-column: auto !important;
        display: inline-flex !important;
        align-items: center !important;
    }
    #support-tickets .data-table tbody td:nth-child(3) {
        grid-column: 1 / -1 !important;
    }
    /* Created (col 6) — small left */
    #support-tickets .data-table tbody td:nth-child(6) {
        grid-column: 1 !important;
        font-size: 0.75rem !important;
        color: #9ca3af !important;
    }
    /* Responses (col 7) — small right */
    #support-tickets .data-table tbody td:nth-child(7) {
        grid-column: 2 !important;
        justify-self: end !important;
        font-size: 0.75rem !important;
        color: #9ca3af !important;
    }
    #support-tickets .data-table tbody td:nth-child(7)::before {
        content: "Respostas: ";
        font-weight: 600 !important;
        color: #6b7280 !important;
    }

    #support-tickets .st-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;
    }
    #support-tickets .st-more-btn:active { background: #e5e7eb; }

    /* -----------------------------------------------------------------
       New Ticket modal + Ticket Detail modal — full-viewport
       ----------------------------------------------------------------- */
    #newTicketModal.modal,
    #ticketDetailModal.modal {
        padding: 0 !important;
        align-items: stretch !important;
        justify-content: stretch !important;
    }
    #newTicketModal .modal-content,
    #ticketDetailModal .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;
    }
    #newTicketModal .modal-header,
    #ticketDetailModal .modal-header {
        padding: 14px 20px !important;
        background: #fff !important;
        border-bottom: 1px solid #f3f4f6 !important;
        flex-shrink: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        margin-bottom: 0 !important;
    }
    #newTicketModal .modal-header h2,
    #ticketDetailModal .modal-header h2 {
        color: #111827 !important;
        font-size: 1.05rem !important;
        font-weight: 700 !important;
        margin: 0 !important;
    }
    #newTicketModal .modal-close,
    #ticketDetailModal .modal-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;
    }

    /* New Ticket form flex-fills so sticky footer pins to bottom */
    #newTicketModal #newTicketForm {
        display: flex !important;
        flex-direction: column !important;
        flex: 1 !important;
        min-height: 0 !important;
        padding: 14px !important;
        background: #f9fafb !important;
        overflow-y: auto !important;
    }
    /* Stack Category+Priority 2-col grid */
    #newTicketModal #newTicketForm > div[style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    /* Ticket Detail body */
    #ticketDetailModal .modal-body,
    #ticketDetailModal #ticketDetailContent {
        padding: 14px !important;
        background: #f9fafb !important;
        overflow-y: auto !important;
        flex: 1 !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior: contain !important;
    }

    /* -----------------------------------------------------------------
       Dynamically-injected content inside #ticketDetailContent.
       Targets the inline-styled divs the JS renders — no way to add
       classes without editing support-tickets.js, so we use attribute
       selectors on the inline styles.
       ----------------------------------------------------------------- */

    /* Header meta card (status/priority/category + date) — allow the
       date to wrap onto its own line and tighten spacing */
    #ticketDetailContent > div > div[style*="background: #f8fafc"][style*="border-radius: 10px"]:first-child {
        gap: 8px 10px !important;
        padding: 10px 12px !important;
        border-radius: 10px !important;
    }
    #ticketDetailContent > div > div[style*="background: #f8fafc"] > div[style*="margin-left: auto"] {
        margin-left: 0 !important;
        width: 100% !important;
        margin-top: 2px !important;
    }
    #ticketDetailContent > div > div[style*="background: #f8fafc"] span {
        font-size: 0.7rem !important;
    }

    /* Two-column grid (Subject / Details) → single column, stacked */
    #ticketDetailContent > div > div[style*="grid-template-columns: 1fr 1fr"] {
        display: block !important;
    }
    #ticketDetailContent > div > div[style*="grid-template-columns: 1fr 1fr"] > div {
        margin-bottom: 10px !important;
        padding: 12px !important;
        border-radius: 10px !important;
    }

    /* Details rows (Reserva / Atribuído / Anexos) — wrap allowed */
    #ticketDetailContent > div > div[style*="grid-template-columns"] > div > div[style*="align-items: center"][style*="gap: 0.5rem"] {
        flex-wrap: wrap !important;
        gap: 4px 6px !important;
    }
    /* + Adicionar Anexo button — tidier */
    #ticketDetailContent button[onclick*="addAttachment"] {
        padding: 4px 10px !important;
        font-size: 0.7rem !important;
    }

    /* Atribuir Ticket row (dropdown + Salvar Atribuição) — stack */
    #ticketDetailContent > div > div[style*="grid-template-columns"] > div > div[style*="border-top"] > div[style*="align-items: center"] {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
    }
    #ticketDetailContent select[id^="assignSelect"] {
        width: 100% !important;
        min-height: 42px !important;
        padding: 8px 12px !important;
        border-radius: 8px !important;
        font-size: 0.9rem !important;
    }
    #ticketDetailContent button[onclick*="assignTicket"] {
        width: 100% !important;
        height: 42px !important;
        border-radius: 10px !important;
        justify-content: center !important;
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
        font-weight: 600 !important;
    }

    /* Conversation container — tighter */
    #ticketDetailContent > div > div:last-child {
        padding: 12px !important;
        border-radius: 10px !important;
    }
    /* Response messages — tighter cards */
    #ticketDetailContent #responsesContainer > div {
        padding: 10px !important;
    }
    #ticketDetailContent #responsesContainer > div > div:last-child {
        padding-left: 0 !important;
    }

    /* Response form (textarea + buttons) — full-width textarea, sticky-ish
       button row with 2-col grid so Fechar + Enviar sit side-by-side */
    #ticketDetailContent #addResponseForm textarea {
        width: 100% !important;
        min-height: 80px !important;
        padding: 10px !important;
        border-radius: 8px !important;
        font-size: 0.95rem !important;
    }
    #ticketDetailContent #addResponseForm > div[style*="justify-content: flex-end"] {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        justify-content: stretch !important;
    }
    #ticketDetailContent #addResponseForm > div > .btn {
        width: 100% !important;
        min-width: 0 !important;
        margin: 0 !important;
        height: 44px !important;
        border-radius: 10px !important;
        font-weight: 600 !important;
        font-size: 0.9rem !important;
    }
    #ticketDetailContent #addResponseForm > div > .btn.btn-secondary {
        background: #f3f4f6 !important;
        color: #374151 !important;
        border: 1px solid #e5e7eb !important;
    }
    #ticketDetailContent #addResponseForm > div > .btn.btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
    }
    /* Closed-ticket footer — stack */
    #ticketDetailContent > div > div:last-child > div[style*="justify-content: space-between"]:last-child {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
    }

    /* Form fields — normalize */
    #newTicketModal .form-group {
        margin-bottom: 12px !important;
    }
    #newTicketModal .form-group > label {
        font-size: 0.85rem !important;
        font-weight: 600 !important;
        color: #374151 !important;
        display: block !important;
        margin-bottom: 4px !important;
    }
    #newTicketModal .form-group input:not([type="file"]):not([type="checkbox"]):not([type="radio"]),
    #newTicketModal .form-group select,
    #newTicketModal .form-group textarea,
    #newTicketModal #bookingRefRow input {
        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;
    }
    #newTicketModal .form-group textarea {
        min-height: 100px !important;
        resize: vertical !important;
    }
    #newTicketModal .form-group input:focus,
    #newTicketModal .form-group select:focus,
    #newTicketModal .form-group textarea:focus,
    #newTicketModal #bookingRefRow input:focus {
        outline: none !important;
        border-color: #6366f1 !important;
        box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
    }
    /* Drop zone tighter */
    #newTicketModal #dropZone {
        padding: 20px 12px !important;
    }
    #newTicketModal #dropZone svg {
        width: 32px !important;
        height: 32px !important;
        margin-bottom: 6px !important;
    }
    #newTicketModal #dropZone > div:first-of-type {
        font-size: 0.85rem !important;
    }
    #newTicketModal #dropZone > div:last-of-type {
        font-size: 0.75rem !important;
    }

    /* Sticky footer with actions — pin at bottom */
    #newTicketModal .form-actions {
        margin: auto -14px 0 !important;
        padding: 10px 14px calc(10px + env(safe-area-inset-bottom)) !important;
        position: sticky !important;
        bottom: 0 !important;
        background: #fff !important;
        border-top: 1px solid #f3f4f6 !important;
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        gap: 8px !important;
        z-index: 5 !important;
    }
    #newTicketModal .form-actions .btn {
        flex: 1 1 0 !important;
        min-width: 0 !important;
        width: 50% !important;
        margin: 0 !important;
        height: 44px !important;
        border-radius: 10px !important;
        font-weight: 600 !important;
        font-size: 0.9rem !important;
        padding: 0 8px !important;
    }
    #newTicketModal .btn-secondary {
        background: #f3f4f6 !important;
        color: #374151 !important;
        border: 1px solid #e5e7eb !important;
    }
    #newTicketModal .btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2) !important;
        color: #fff !important;
        border: none !important;
    }
}
