/* SisPat - ajustes para celular e tablet. Carregado por último; tudo dentro de @media, salvo regras neutras. */

/* Barra superior móvel: oculta no desktop */
.mobile-topbar { display: none; }

@media (max-width: 992px) {
    /* ---------- Barra superior fixa ---------- */
    .mobile-topbar {
        position: fixed;
        top: 0; left: 0; right: 0;
        z-index: 90;
        display: flex;
        align-items: center;
        gap: 6px;
        height: calc(52px + env(safe-area-inset-top, 0px));
        padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(6px, env(safe-area-inset-left, 0px));
        background: var(--sidebar-color, #000c27);
        color: #fff;
        box-shadow: 0 2px 8px rgba(2, 6, 23, .25);
    }
    .mobile-topbar .hamburger-menu {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 44px;
        width: 44px;
        height: 44px;
        padding: 0;
        border-radius: 10px;
        background: transparent;
        color: #fff;
        font-size: 1.25rem;
        line-height: 1;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-topbar .hamburger-menu:active,
    .mobile-topbar .hamburger-menu[aria-expanded="true"] { background: rgba(255, 255, 255, .16); }
    .mobile-topbar .hamburger-menu i { width: 1.25em; text-align: center; }
    .mobile-topbar-title {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 1.05rem;
        font-weight: 600;
    }
    body.has-mobile-topbar { padding-top: calc(52px + env(safe-area-inset-top, 0px)); }
    html:has(body.menu-open) { overflow: hidden; }
    body.menu-open { overflow: hidden; overscroll-behavior: contain; }

    /* ---------- Gaveta lateral ---------- */
    .sidebar {
        left: 0 !important;
        width: min(85vw, 320px) !important;
        max-width: 320px;
        height: 100vh;
        height: 100dvh;
        padding: calc(14px + env(safe-area-inset-top, 0px)) 14px calc(12px + env(safe-area-inset-bottom, 0px)) calc(14px + env(safe-area-inset-left, 0px)) !important;
        transform: translateX(-105%);
        visibility: hidden;
        transition: transform .28s ease, visibility 0s linear .28s;
        z-index: 1000;
        overscroll-behavior: contain;
    }
    .sidebar.sidebar-visible {
        transform: translateX(0);
        visibility: visible;
        transition: transform .28s ease, visibility 0s;
    }
    .sidebar-overlay { z-index: 999; -webkit-tap-highlight-color: transparent; }
    .sidebar-nav { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
    .sidebar-nav li a {
        min-height: 44px;
        padding: 10px 12px !important;
        gap: 12px !important;
        font-size: .95rem !important;
        border-radius: 10px;
        align-items: center;
    }
    .sidebar-nav .icon,
    .sidebar-nav li a > i:first-child {
        flex: 0 0 24px !important;
        width: 24px !important;
        font-size: 1rem !important;
        text-align: center;
    }
    .sidebar-nav li a > span:not(.menu-badge) { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; }
    .sidebar-nav li a.active {
        background: rgba(255, 255, 255, .2) !important;
        box-shadow: inset 4px 0 0 #fff;
        font-weight: 700;
    }
    .sidebar-nav .menu-title { padding-top: 14px; }
    .sidebar-logout .btn-logout { min-height: 44px; }
    .sidebar-warning { min-height: 44px; }

    /* ---------- Conteúdo ---------- */
    .main-layout { display: block; min-height: 0; }
    .main-layout > #menu-container { width: 0; flex: 0 0 0; }
    .content {
        width: 100% !important;
        max-width: 100%;
        padding: 14px max(12px, env(safe-area-inset-right, 0px)) calc(20px + env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px)) !important;
        overflow-x: clip;
    }
    .page-header {
        display: flex !important;
        flex-direction: column;
        align-items: stretch !important;
        gap: 10px;
        margin-bottom: 14px;
    }
    .page-header > * { grid-column: auto !important; grid-row: auto !important; min-width: 0; }
    .page-header h1 { font-size: 1.35rem; }
    .page-header-actions,
    .header-actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
    .page-header-actions > .btn,
    .header-actions > .btn { flex: 1 1 160px; }
    .card { max-width: 100%; }

    /* ---------- Tabelas ---------- */
    .table-responsive,
    .table-wrapper,
    .card:has(> table) {
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    /* Tabelas sem data-label (não viram cartões): voltam a ser tabela com rolagem lateral */
    .card table:not(:has(td[data-label])) { display: table; min-width: 640px; overflow: visible; }
    .card table:not(:has(td[data-label])) thead { display: table-header-group; }
    .card table:not(:has(td[data-label])) tbody { display: table-row-group; }
    .card table:not(:has(td[data-label])) tr { display: table-row; border-bottom: 0; padding: 0; margin: 0; }
    .card table:not(:has(td[data-label])) th,
    .card table:not(:has(td[data-label])) td { display: table-cell; padding: 8px 10px; border-bottom: 1px solid #e5e7eb; }
    .card table:not(:has(td[data-label])) td::before { content: none; }
    /* Cartões: rótulo e valor com quebra segura */
    .card table td[data-label] { gap: 12px; text-align: right; }
    .card table td[data-label]::before { flex: 0 0 auto; max-width: 45%; text-align: left; }
    .card table td[data-label] > * { min-width: 0; }
    td .btn, td button, .row-actions .btn, .action-menu .btn { min-height: 40px; }
    .row-actions, .action-menu { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }

    /* ---------- Grids e filtros ---------- */
    .form-grid, .form-grid2, .form-grid-modal, .item-details-grid, .common-details-grid,
    .patrimonio-filter-grid, .painel-filter-grid, .conta-grid, .nf-modal-grid {
        grid-template-columns: 1fr !important;
    }
    .form-grid .full-width, .form-grid2 .full-width { grid-column: auto; }
    .pesquisa-itens, .pending-signatures-toolbar, .schedule-catalog-toolbar, .patrimonio-search-toolbar,
    .usuarios-filtros, .responsaveis-filtros, .projetos-filtros, .historico-filtro {
        display: flex; flex-direction: column; align-items: stretch; gap: 10px;
    }
    .pesquisa-itens > *, .pending-signatures-toolbar > *, .schedule-catalog-toolbar > *,
    .patrimonio-search-toolbar > *, .usuarios-filtros > *, .responsaveis-filtros > *, .projetos-filtros > *,
    .historico-filtro > * { width: 100%; max-width: 100%; }
    .mixed-list-toolbar { display: grid !important; grid-template-columns: 1fr !important; gap: 8px; }
    .mixed-list-toolbar > *, .mixed-list-toolbar select, .mixed-list-toolbar .btn { width: 100% !important; min-width: 0 !important; }
    .patrimonio-filter-actions { display: flex; flex-wrap: wrap; gap: 8px; }
    .patrimonio-filter-actions .btn { flex: 1 1 140px; }
    .kpi-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }

    /* ---------- Abas roláveis ---------- */
    .tabs, .mixed-tabs {
        display: flex;
        flex-wrap: nowrap;
        justify-content: flex-start !important;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .tabs::-webkit-scrollbar, .mixed-tabs::-webkit-scrollbar { display: none; }
    .tab-link, .mixed-tab {
        flex: 0 0 auto;
        min-height: 44px;
        white-space: nowrap;
        display: inline-flex;
        align-items: center;
        gap: 6px;
    }

    /* ---------- Ícones e botões ---------- */
    .btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        min-height: 44px;
        line-height: 1.2;
    }
    .btn-small, .btn.btn-sm { min-height: 40px; }
    .btn > i, .btn > .fa-solid, .btn > .fa-regular,
    .tab-link > i, .mixed-tab > i {
        flex: 0 0 auto;
        width: 1.1em;
        margin: 0;
        text-align: center;
        line-height: 1;
    }
    .modal-close-control, .modal-close-itens, .modal-close { min-width: 44px; min-height: 44px; }
}

/* ---------- Telefone e tablet pequeno ---------- */
@media (max-width: 768px) {
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]),
    select, textarea,
    .ts-control, .ts-control input, .ts-dropdown {
        font-size: 16px !important;
    }
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="hidden"]),
    select { min-height: 44px; }
    input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
}

/* ---------- Modais em tela cheia ---------- */
@media (max-width: 640px) {
    .modal {
        padding: 0 !important;
        align-items: stretch !important;
        justify-content: stretch !important;
        overflow: hidden !important;
    }
    .modal > div {
        width: 100% !important;
        max-width: none !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-height: 100vh !important;
        max-height: 100dvh !important;
        margin: 0 !important;
        border-radius: 0 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    .modal .modal-header,
    .modal .modal-header-flex {
        position: sticky;
        top: 0;
        z-index: 6;
        gap: 8px;
        background: var(--card-bg-color, #fff);
        box-shadow: 0 1px 0 #e4e7ec;
        padding-top: max(12px, env(safe-area-inset-top, 0px));
    }
    .modal .modal-header { margin-bottom: 12px; }
    .modal .modal-header h3 { width: auto; flex: 1 1 auto; min-width: 0; max-width: none; font-size: 1.15rem; }
    .modal .modal-header .modal-close-control { flex: 0 0 44px; }
    .modal .modal-buttons {
        position: sticky;
        bottom: 0;
        z-index: 6;
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 8px;
        margin-top: 14px;
        padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px)) !important;
        background: var(--card-bg-color, #fff) !important;
        border-top: 1px solid #e4e7ec !important;
        box-shadow: 0 -4px 10px rgba(15, 23, 42, .06);
    }
    .modal .modal-buttons > .btn { flex: 1 1 130px; width: auto !important; }
    .modal-itens { grid-template-columns: 1fr; margin: 12px; }
    .modal .tabs { padding: 0 12px !important; }
    .modal table:not(:has(td[data-label])) { display: block; overflow-x: auto; max-width: 100%; }
    .transport-modal #transporte-assinatura-canvas { height: 200px; }
    .transport-items-list li:has(.transporte-item-destinatario) { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
    .transport-items-list .transporte-item-destinatario { order: 3; grid-column: 1 / -1; max-width: 100%; }
    .transport-modal #transporte-enviar { white-space: normal; line-height: 1.25; }
}

/* ---------- Páginas públicas (assinatura/agendamento) ---------- */
@media (max-width: 640px) {
    .public-sign-card, .public-form, .public-card { width: 100% !important; max-width: 100% !important; }
    .public-form-actions { display: flex; flex-direction: column; gap: 8px; }
    .public-form-actions > .btn { width: 100%; }
    canvas { max-width: 100%; touch-action: none; }
}

/* ---------- 2.12.16: correções de layout no celular ---------- */
@media (max-width: 992px) {
    /* O contêiner do menu não ocupa espaço no fluxo (sidebar e overlay são fixos) */
    #menu-container, #menu-container > .main-container { height: 0 !important; min-height: 0 !important; }
    /* hidden deve sempre esconder, mesmo com display definido por classe */
    .content [hidden], .modal [hidden] { display: none !important; }
    /* Em barras empilhadas em coluna, flex-basis em px viraria altura */
    .pesquisa-itens > *, .pending-signatures-toolbar > *, .schedule-catalog-toolbar > *,
    .patrimonio-search-toolbar > *, .usuarios-filtros > *, .responsaveis-filtros > *, .projetos-filtros > *,
    .historico-filtro > * { flex: 0 0 auto !important; }
    /* Título já aparece na barra superior */
    body.has-mobile-topbar .page-header > h1,
    body.has-mobile-topbar .page-header > h2,
    body.has-mobile-topbar .page-header > div > h1 { display: none; }
    body.has-mobile-topbar .content { padding-top: 10px !important; }
    .page-header { margin-bottom: 10px; gap: 8px; }
    .page-header p, .page-header .page-subtitle { margin: 0; font-size: .88rem; }
    /* Ações do cabeçalho em grade de 2 colunas */
    .page-header .page-header-actions { display: grid !important; grid-template-columns: 1fr 1fr; gap: 8px; width: 100%; }
    .page-header .page-header-actions > .btn { width: 100%; min-width: 0; padding-inline: 10px; font-size: .9rem; }
    .patrimonio-quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
    .patrimonio-quick-actions > .btn { width: 100%; min-width: 0; padding-inline: 10px; font-size: .9rem; }
    /* Cartão de lista mais compacto */
    .card { padding: 14px !important; border-radius: 12px; }
    .card > h3, .card > h2 { font-size: 1.05rem; margin-bottom: 10px; }
    /* Linhas-cartão de tabela */
    .card table td.patrimonio-selection-column {
        width: auto !important; max-width: none !important; min-width: 0 !important;
        display: flex !important; justify-content: space-between; align-items: center;
    }
    .card table td.patrimonio-selection-column::before { max-width: none !important; white-space: nowrap; }
    .card table td[data-label]::before { white-space: nowrap; }
    /* Paginação */
    .pagination, .cadastro-pagination, .paginacao, .pagination-controls {
        display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px;
    }
    .pagination > span, .cadastro-pagination > span, .paginacao > span, .pagination-controls > span { white-space: nowrap; }
    .patrimonio-filter-actions .btn { flex: 1 1 0 !important; min-width: 0; }
    .etiquetas-selecao-barra { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
}

/* ---------- 2.12.16: cartão compacto de patrimônio no celular ---------- */
@media (max-width: 760px) {
    #itens-tabela-corpo > tr {
        display: grid !important;
        grid-template-columns: 32px minmax(0, 1fr) auto 44px;
        grid-template-areas:
            "sel otss status menu"
            "nome nome nome nome"
            "cat cat loc loc"
            "resp resp resp resp"
            "data data valor valor";
        column-gap: 10px; row-gap: 8px;
        padding: 12px !important;
        margin: 0 0 10px !important;
        border: 1px solid #e4e7ec !important;
        border-radius: 12px;
        background: #fff;
        box-shadow: 0 1px 2px rgba(15, 23, 42, .05);
    }
    #itens-tabela-corpo > tr > td {
        display: block !important; padding: 0 !important; border: 0 !important;
        text-align: left !important; min-width: 0; width: auto !important;
        overflow-wrap: anywhere; font-size: .9rem;
    }
    #itens-tabela-corpo > tr > td[style*="display:none"],
    #itens-tabela-corpo > tr > td[style*="display: none"] { display: none !important; }
    #itens-tabela-corpo > tr > td::before { display: block; content: attr(data-label); margin: 0 0 2px; font-size: .72rem; font-weight: 600; letter-spacing: .02em; text-transform: uppercase; color: #64748b; max-width: none !important; white-space: nowrap; }
    #itens-tabela-corpo > tr > td:is([data-label="Selecionar"],[data-label="Patrimônio OTSS"],[data-label="Nome"],[data-label="Status"],[data-label="Ações"])::before { content: none !important; display: none !important; }
    #itens-tabela-corpo td[data-label="Selecionar"] { grid-area: sel; align-self: center; display: flex !important; }
    #itens-tabela-corpo td[data-label="Patrimônio OTSS"] { grid-area: otss; align-self: center; font-size: .95rem; }
    #itens-tabela-corpo td[data-label="Status"] { grid-area: status; align-self: center; justify-self: end; }
    #itens-tabela-corpo td[data-label="Status"] .tag { white-space: nowrap; }
    #itens-tabela-corpo td[data-label="Ações"] { grid-area: menu; align-self: center; justify-self: end; }
    #itens-tabela-corpo td[data-label="Ações"] .dots { width: 40px; height: 40px; }
    #itens-tabela-corpo td[data-label="Nome"] { grid-area: nome; font-weight: 600; font-size: 1rem; color: #0f172a; }
    #itens-tabela-corpo td[data-label="Categoria"] { grid-area: cat; }
    #itens-tabela-corpo td[data-label="Localização"] { grid-area: loc; }
    #itens-tabela-corpo td[data-label="Responsável"] { grid-area: resp; }
    #itens-tabela-corpo td[data-label="Data Compra"] { grid-area: data; }
    #itens-tabela-corpo td[data-label="Valor"] { grid-area: valor; }
    #itens-tabela-corpo td[colspan] { grid-column: 1 / -1; text-align: center !important; }
    #itens-tabela-corpo > tr:has(td[colspan]) { display: block !important; }
    .patrimonio-quick-actions > .btn, .page-header .page-header-actions > .btn { white-space: normal !important; line-height: 1.15; text-align: center; }
    #itens-tabela-corpo > tr { grid-template-columns: 32px minmax(0, 1fr) minmax(0, 1fr) 44px !important; }
    #itens-tabela-corpo td[data-label="Patrimônio OTSS"] { white-space: nowrap; }
    .pagination { display: flex !important; flex-wrap: nowrap !important; align-items: center; justify-content: space-between; gap: 6px; }
    .pagination > .btn { flex: 0 0 auto; min-width: 0; padding-inline: 10px; }
    .pagination > span { flex: 1 1 auto; text-align: center; font-size: .85rem; }
}
@media (max-width: 380px) {
    .patrimonio-quick-actions, .page-header .page-header-actions { grid-template-columns: 1fr !important; }
}

/* ---------- 2.12.16: toda tabela rotulada vira cartão no celular ---------- */
@media (max-width: 760px) {
    .content table:has(tbody td[data-label]), .modal table:has(tbody td[data-label]) { display: block !important; min-width: 0 !important; width: 100% !important; overflow: visible !important; border: 0; }
    .content table:has(tbody td[data-label]) thead, .modal table:has(tbody td[data-label]) thead { display: none !important; }
    .content table:has(tbody td[data-label]) tbody, .modal table:has(tbody td[data-label]) tbody { display: block !important; }
    .content table:has(tbody td[data-label]) tbody > tr, .modal table:has(tbody td[data-label]) tbody > tr {
        display: block !important; margin: 0 0 10px !important; padding: 4px 12px !important;
        border: 1px solid #e4e7ec !important; border-radius: 12px; background: #fff; box-shadow: 0 1px 2px rgba(15, 23, 42, .05);
    }
    .content table:has(tbody td[data-label]) tbody > tr > td, .modal table:has(tbody td[data-label]) tbody > tr > td {
        display: flex !important; justify-content: space-between; align-items: center; gap: 12px;
        padding: 8px 0 !important; border: 0 !important; border-bottom: 1px solid #f1f5f9 !important;
        text-align: right !important; min-width: 0; width: auto !important; white-space: normal !important; overflow-wrap: anywhere;
    }
    .content table:has(tbody td[data-label]) tbody > tr > td:last-child, .modal table:has(tbody td[data-label]) tbody > tr > td:last-child { border-bottom: 0 !important; }
    .content table:has(tbody td[data-label]) tbody > tr > td::before, .modal table:has(tbody td[data-label]) tbody > tr > td::before {
        content: attr(data-label); flex: 0 0 auto; max-width: 45%; text-align: left; white-space: normal;
        font-size: .78rem; font-weight: 600; color: #64748b; margin: 0;
    }
    .content table:has(tbody td[data-label]) tbody > tr > td[colspan], .modal table:has(tbody td[data-label]) tbody > tr > td[colspan] { display: block !important; text-align: center !important; }
    .content table:has(tbody td[data-label]) tbody > tr > td[colspan]::before { content: none; }
    .content table:has(tbody td[data-label]) td:has(.actions-menu) { justify-content: flex-end; }
    .content table:has(tbody td[data-label]) td:has(.actions-menu)::before { content: none; }
    /* Tabela ainda sem linhas: cabeçalho sem quebra letra a letra */
    .content table:not(:has(tbody td[data-label])) th { white-space: nowrap; }
    #itens-tabela-corpo td[data-label="Status"] .tag { white-space: normal; text-align: right; }
}
@media (max-width: 992px) {
    .page-header .page-header-actions > .btn:only-child,
    .page-header .page-header-actions > .btn:last-child:nth-child(odd) { grid-column: 1 / -1; }
}

/* ---------- 2.12.16: modais no celular ---------- */
@media (max-width: 640px) {
    /* Grades inline com colunas mínimas largas (ex.: minmax(400px)) viram uma coluna */
    .modal [style*="grid-template-columns"] { grid-template-columns: minmax(0, 1fr) !important; gap: 12px !important; }
    .modal form [style*="padding: 20px"] { padding: 14px !important; }
    .modal .modal-content [style*="overflow-y: auto"] { padding-left: 12px !important; padding-right: 12px !important; }
    /* O rodapé fixo precisa encostar na borda inferior do modal */
    .modal > div { padding-bottom: 0 !important; padding-left: 16px !important; padding-right: 16px !important; }
    .modal .modal-buttons { margin-left: -16px; margin-right: -16px; flex-wrap: nowrap !important; }
    .modal .modal-buttons > .btn { flex: 1 1 0 !important; min-width: 0; padding-inline: 10px; }
    .modal .modal-header { margin-left: -16px; margin-right: -16px; padding-left: 16px; padding-right: 8px; }
}
