/*
 * Regras só do celular (até 575,98 px). Telefone de referência: 360 x 640.
 *   Formulários: o modal ocupa a tela toda. Título e rodapé ficam parados, só o corpo rola.
 *   Campos: 48 px de altura e letra de 16 px (abaixo disso o iPhone aumenta a tela).
 *   Toque: botões, ícones e marcadores "?" com área de pelo menos 44 px.
 *
 * Carregar por último, depois de screen-polish.css, typography.css, tutorial.css e
 * reproducao-event-modal.css. Para voltar ao modal pequeno, apague o primeiro bloco.
 */
@media (max-width: 575.98px) {
  /* Formulário em tela cheia */
  .modal .modal-dialog {
    margin: 0;
    width: 100%;
    max-width: 100%;
    height: 100%;
    min-height: 100%;
  }

  .modal .modal-dialog .modal-content {
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border-width: 0;
  }

  /* Alguns modais guardam corpo e rodapé dentro de um <form> */
  .modal .modal-content > form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
  }

  .modal .modal-header,
  .modal .modal-footer {
    flex: none;
  }

  .modal .modal-content .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }

  .modal .modal-footer {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }

  /* Colaboradores guarda o rodapé dentro do corpo: ele cola na base em vez de rolar junto */
  .modal .modal-body .modal-footer {
    position: sticky;
    bottom: 0;
    background-color: var(--ct-modal-bg);
  }

  .modal .modal-footer .btn {
    min-height: 44px;
  }

  .modal .btn-close {
    box-sizing: border-box;
    min-width: 44px;
    min-height: 44px;
  }

  /* Campos */
  .form-control,
  .form-select,
  .select2-container .select2-selection--single {
    min-height: 48px;
    font-size: 16px;
  }

  .select2-container.select2-container--default .select2-selection--single {
    padding-top: 0;
    padding-bottom: 0;
  }

  /* O select2 carrega depois deste arquivo: a classe dupla vence pela especificidade */
  .select2-container.select2-container--default .select2-selection--single .select2-selection__rendered {
    font-size: 16px;
    line-height: 46px;
  }

  .select2-container.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 46px;
  }

  .select2-container .select2-search__field {
    font-size: 16px;
  }

  .form-check:not(.form-switch) {
    padding-left: 2rem;
    min-height: 24px;
  }

  .form-check:not(.form-switch) .form-check-input {
    width: 24px;
    height: 24px;
    margin-left: -2rem;
    margin-top: 0;
  }

  /* Barra do topo: toque de 44 px sem mudar o desenho */
  .navbar-custom .button-toggle-menu,
  .navbar-custom .logo-topbar a,
  .navbar-custom a.nav-link,
  .navbar-custom .topbar-menu a.nav-link {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Marcador "?": a área de toque cresce, o círculo desenhado continua igual */
  .kpi-card [data-bs-toggle="tooltip"],
  .tip-q {
    position: relative;
  }

  .kpi-card [data-bs-toggle="tooltip"]::before {
    content: "";
    position: absolute;
    top: 50%;
    right: -14px;
    width: 44px;
    height: 44px;
    margin-top: -22px;
  }

  .tip-q::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 44px;
    height: 44px;
    margin: -22px 0 0 -22px;
  }

  /* Botão de ajuda: fica na própria faixa do topo, à esquerda do sino, e libera a lista e o fim dos formulários.
     194 px = 24 de margem + 3 ícones do topo (44 + 44 + 58) + 2 vãos de 8 + 8 de folga. */
  .navbar-custom .topbar-menu {
    gap: 8px !important;
  }

  /* O botão de ajuda fala baixo no celular: sem fundo rosa e sem sombra, igual ao sino e à busca */
  .tutorial-fab {
    top: 13px;
    right: 194px;
    bottom: auto;
    width: 44px;
    height: 44px;
    background: transparent;
    box-shadow: none;
    color: var(--ct-topbar-item-color);
  }

  .tutorial-fab [class*="ri-"] {
    font-size: 22px;
  }

  .tutorial-fab:hover {
    background: transparent;
    box-shadow: none;
    transform: none;
    color: var(--ct-topbar-item-color);
  }

  .tutorial-fab:focus-visible {
    outline: 2px solid var(--ct-primary);
    outline-offset: -2px;
  }
}

/*
 * Listas em cartões (celular).
 * A tabela com a classe table-cards vira uma lista: cada linha é um cartão de 100% da largura.
 *   Título (brinco, item, nome) em cima, os outros campos com o nome do campo ao lado ou em cima.
 *   Ações da linha no canto de cima, à direita (gatilho de 44 x 44 px). Caixa de seleção no canto de cima, à esquerda.
 *   "Ver detalhes" abre o detalhe do DataTables embaixo do cartão, na largura toda.
 * mobile-lists.js põe os data-label e as classes cell-*; a coluna do título leva data-card-title no <th>.
 * table-cards--pairs mostra os campos em duas colunas (rótulo em cima do valor).
 * Para voltar à tabela comum no celular, apague este bloco.
 */
@media (max-width: 575.98px) {
  .table-cards-wrap {
    overflow: visible;
  }

  .table-card.table-card--cards {
    padding: 0;
    border-width: 0;
    background: transparent;
  }

  .table-cards,
  .table-cards tbody {
    display: block;
    width: 100% !important;
    border: 0;
  }

  .table-cards thead {
    display: none;
  }

  .table-cards tbody tr {
    position: relative;
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr);
    margin: 0 0 12px;
    padding: 8px 12px 0;
    border: 1px solid var(--ui-line);
    border-radius: 12px;
    background: var(--ui-surface);
  }

  .table-cards tbody td {
    display: block;
    grid-column: 1 / -1;
    width: auto !important;
    padding: 0;
    border: 0;
    text-align: left;
  }

  table.table-cards.nowrap tbody td {
    white-space: normal;
    overflow-wrap: anywhere;
  }

  /* Título: o brinco primeiro (texto principal, 16 px, 600), o nome embaixo apagado */
  .table-cards .cell-title {
    grid-column: 3;
    grid-row: 1;
    min-height: 44px;
    padding: 0 48px 8px 0;
    font-size: 16px;
    line-height: 24px;
    color: var(--ui-text);
  }

  .table-cards .cell-title strong {
    font-size: 16px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    line-height: 24px;
    color: var(--ui-text);
  }

  .table-cards .cell-title small,
  .table-cards .cell-title .text-muted {
    display: block;
    font-size: 14px;
    font-weight: 400;
    line-height: 20px;
    color: var(--ui-muted);
  }

  .table-cards .cell-photo {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    padding: 0 12px 8px 0;
  }

  .table-cards .cell-photo + .cell-title {
    grid-column: 3;
  }

  /* Sem foto, o título ocupa o lugar dela */
  .table-cards .cell-title:first-child,
  .table-cards .cell-check + .cell-title {
    grid-column: 2 / -1;
  }

  /* Campos: nome do campo à esquerda, valor à direita */
  .table-cards td[data-label] {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 36px;
    padding: 6px 0;
    border-top: 1px solid var(--ui-line);
    font-size: 15px;
    line-height: 22px;
    text-align: right;
  }

  .table-cards td[data-label]::before {
    content: attr(data-label);
    flex: none;
    font-size: 14px;
    line-height: 20px;
    text-align: left;
    color: var(--ui-muted);
  }

  .table-cards td[data-label] .text-muted {
    text-align: right;
  }

  /* Campos em duas colunas */
  .table-cards--pairs tbody tr {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    column-gap: 12px;
  }

  .table-cards--pairs .cell-title,
  .table-cards--pairs .cell-title:first-child,
  .table-cards--pairs .cell-check + .cell-title {
    grid-column: 1 / -1;
  }

  .table-cards--pairs td[data-label] {
    grid-column: auto;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 0;
    min-height: 0;
    text-align: left;
  }

  .table-cards--pairs td[data-label] .text-muted {
    text-align: left;
  }

  /* Caixa de seleção: 24 px desenhada, 44 x 44 px de toque (o clique na célula marca a caixa) */
  .table-cards .cell-check {
    position: absolute;
    top: 8px;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
  }

  .table-cards .cell-check input[type="checkbox"] {
    width: 24px;
    height: 24px;
    margin: 0;
  }

  /* O título (ou a foto, quando existe) deixa espaço para a caixa de seleção */
  .table-cards--check .cell-photo,
  .table-cards--check .cell-title {
    padding-left: 40px;
  }

  .table-cards--check .cell-photo + .cell-title {
    padding-left: 0;
  }

  /* Ações da linha: gatilho no canto de cima, à direita */
  .table-cards .cell-actions {
    position: absolute;
    top: 8px;
    right: 4px;
    width: 44px;
    height: 44px;
    padding: 0;
    text-align: center;
  }

  .table-cards .cell-actions .dropdown {
    width: 44px;
    height: 44px;
  }

  .table-cards .cell-actions .action-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    font-size: 24px;
  }

  .table-cards .cell-actions .action-icon [class*="ri-"] {
    font-size: 24px;
    line-height: 1;
  }

  /* Ações sem menu (editar e excluir, ou um botão): linha própria no pé do cartão */
  .table-cards .cell-actions--row {
    position: static;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    width: auto;
    height: auto;
    padding: 6px 0;
    border-top: 1px solid var(--ui-line);
  }

  .table-cards .cell-actions--row .btn {
    min-height: 44px;
  }

  .table-cards .dropdown-menu {
    min-width: 220px;
  }

  .table-cards .dropdown-menu .dropdown-item {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    white-space: nowrap;
  }

  /* Detalhe: a seta vira uma linha de 44 px no pé do cartão */
  .table-cards .dt-control {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    order: 99;
    min-height: 44px;
    margin-top: 4px;
    border-top: 1px solid var(--ui-line);
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    cursor: pointer;
    color: var(--ui-muted);
  }

  .table-cards .dt-control .dt-control-label::before {
    content: "Ver detalhes";
  }

  .table-cards tbody tr.shown > .dt-control .dt-control-label::before {
    content: "Ocultar detalhes";
  }

  .table-cards tbody tr.shown {
    margin-bottom: 0;
    border-bottom-width: 0;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }

  .table-cards tbody tr.shown + tr {
    display: block;
    margin: 0 0 12px;
    padding: 0 12px 12px;
    border: 1px solid var(--ui-line);
    border-top-width: 0;
    border-radius: 0 0 12px 12px;
    background: var(--ui-surface);
  }

  .table-cards tbody tr.shown + tr > td {
    display: block;
    width: 100% !important;
    padding: 0;
  }

  /* Topo da lista mais curto: sem trilha de navegação, sem "Exibir N registros" */
  .page-title-box .breadcrumb {
    display: none;
  }

  .page-title-box .btn {
    min-height: 44px;
  }

  .mobile-actions-bar {
    width: 100%;
  }

  .mobile-actions-bar > .btn,
  .mobile-actions-bar > .mobile-more-actions {
    flex: 1 1 0;
    min-width: 0;
  }

  .mobile-actions-bar > .mobile-more-actions > .btn {
    width: 100%;
  }

  .mobile-more-actions .dropdown-menu .dropdown-item {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
  }

  /* Totais e abas mais baixos, para a primeira linha da lista aparecer na primeira tela e meia */
  .ui-kpis .kpi-card .card-body {
    padding: 8px 12px;
  }

  .ui-kpis .kpi-card h3 {
    line-height: 24px;
  }

  .ui-kpis .kpi-card-main h3 {
    line-height: 36px;
  }

  /* Título menor e espaços de 8 px: o botão principal cabe na linha do título e a lista sobe */
  .ui-screen .page-title-box .page-title {
    font-size: 20px;
    line-height: 28px;
  }

  .ui-screen .section-title {
    margin-bottom: 8px;
  }

  .ui-kpis.mb-3,
  .row.g-2.mb-3 {
    margin-bottom: 8px !important;
  }

  /* Abas: todas cabem na largura. Sem ícone, folga de 8 px, o rótulo quebra em duas linhas e, se
     ainda faltar lugar (Sanidade tem cinco), a fila desce para uma segunda linha. */
  .nav-tabs {
    flex-wrap: wrap;
    margin-bottom: 8px !important;
    overflow: visible;
  }

  .nav-tabs .nav-item {
    flex: 1 1 auto;
    min-width: 0;
  }

  .nav-tabs .nav-link {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 8px;
    line-height: 20px;
    text-align: center;
    white-space: normal;
  }

  .nav-tabs .nav-link > i {
    display: none;
  }

  .dataTables_length {
    display: none;
  }

  .dataTables_filter {
    margin-bottom: 8px;
    text-align: left;
  }

  /* Busca: o campo ocupa a linha toda. O texto "Buscar:" some da vista (font-size 0) e fica no
     campo como aria-label e placeholder (mobile-lists.js). */
  .dataTables_filter label {
    display: flex;
    align-items: center;
    gap: 0;
    width: 100%;
    margin: 0;
    font-size: 0;
  }

  .dataTables_filter label .form-control {
    flex: 1 1 auto;
    width: 100%;
    min-height: 48px;
    margin: 0 !important;
    font-size: 16px;
  }

  /* A coluna da busca perde a folga lateral: o campo alinha com o resto da tela */
  .dataTables_wrapper > .row > [class*="col-"] {
    padding-right: 0;
    padding-left: 0;
  }

  .dataTables_info {
    padding-top: 0;
    text-align: center;
  }

  .dataTables_paginate .pagination {
    flex-wrap: wrap;
    justify-content: center;
    margin-bottom: 0;
  }
}

/*
 * Toque em todas as telas (celular): botão, link de página e interruptor com área de pelo menos 44 px.
 *   Botão e paginação: 44 x 44 px, texto e ícone no meio (inline-flex).
 *   Interruptor: 44 x 24 px desenhado e linha de 44 px. O texto ao lado também marca e desmarca.
 *   Interruptor sem texto (switch-bare): o rótulo escondido cobre a linha toda e vira a área de toque.
 * Para voltar ao tamanho de computador, apague este bloco.
 */
@media (max-width: 575.98px) {
  .btn,
  .page-link {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
  }

  .action-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
  }

  /* Tabela de poucas colunas com interruptores: folga curta nas células para caber sem rolar */
  .table-tight > :not(caption) > * > * {
    padding-right: 8px;
    padding-left: 8px;
  }

  .form-switch {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding-left: 3.25rem;
  }

  .form-switch .form-check-input {
    float: none;
    flex: none;
    width: 44px;
    height: 24px;
    margin: 0 0 0 -3.25rem;
  }

  .form-switch .form-check-label {
    margin: 0;
  }

  .form-switch-lg {
    padding-left: 4.25rem;
  }

  .form-switch-lg .form-check-input {
    margin-left: -4.25rem;
  }

  .form-switch.switch-bare {
    position: relative;
    display: inline-flex !important;
    justify-content: center;
    min-width: 44px;
    padding-left: 0;
  }

  .form-switch.switch-bare .form-check-input {
    margin: 0;
  }

  .form-switch.switch-bare .form-check-label {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
}
