/* ============================================================
   App CUPAV — folha de estilos MOBILE
   Aplica-se em ecrãs ≤ 768px (breakpoint md do Tailwind).
   Mesmo código que desktop, comportamento diferente.
   ============================================================ */

:root {
  --bottom-nav-height: 60px;
  --fab-size: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Estes elementos só existem para mobile; em desktop ficam escondidos */
#bottom-nav, #fab-quick { display: none; }

@media (max-width: 768px) {
  /* -----------------------------------------------------------
     Layout global
     ----------------------------------------------------------- */
  main {
    /* deixa espaço para a bottom-nav fixa não tapar o último item */
    padding-bottom: calc(var(--bottom-nav-height) + var(--safe-bottom) + 24px) !important;
    padding-left: 12px !important;
    padding-right: 12px !important;
  }

  /* Tipografia e títulos: encolher em mobile para não rebentar */
  h1.text-3xl, .text-3xl { font-size: 1.5rem !important; line-height: 1.2 !important; }
  h2.text-xl, .text-xl    { font-size: 1.15rem !important; }
  h3.text-base, .text-base{ font-size: 0.95rem !important; }

  .surface {
    padding: 0.85rem 1rem;
    border-radius: 12px;
  }

  /* -----------------------------------------------------------
     Header — versão compacta
     Esconde o block de botões de export/import (vai para "Mais")
     ----------------------------------------------------------- */
  header.app-header {
    /* o header continua sticky */
  }
  #header-actions { display: none !important; }
  /* O hamburger fica escondido em mobile: o acesso ao menu completo é
     feito pelo botão "Mais" da bottom-nav (que abre o mesmo drawer).
     Liberta espaço no header, que estava sobrecarregado. */
  .nav-toggle { display: none !important; }
  /* a brand vira pequeno: só logo e iniciais */
  .brand-home .brand-logo { width: 26px; height: 26px; }
  .brand-home > .text-left .text-xl { font-size: 1rem; }
  .brand-home > .text-left > div:last-child { display: none; } /* esconde "Gestão Interna" */

  /* O nav interno (drawer) mantém-se; é o "Mais" */
  /* O badge supa-badge fica visível mas pequeno */
  #supa-badge { font-size: 9px; }

  /* Header compacto: garante que tudo cabe sem rebentar a linha.
     Esconde o cargo no badge de utilizador e encolhe o "Sair". */
  .header-right { gap: 6px !important; }
  .user-badge-role { display: none !important; }
  .user-badge-name { max-width: 64px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #btn-logout { padding: 4px 8px; font-size: 11px; }
  .notif-bell { padding: 4px; }

  /* Caixa de ações automáticas no modal de peça: confortável ao toque */
  #m-actions-wrap .surface-soft label { padding: 8px 0; }
  #m-actions-wrap input[type="checkbox"] { width: 20px; height: 20px; }

  /* -----------------------------------------------------------
     Bottom-nav fixa
     ----------------------------------------------------------- */
  #bottom-nav {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--bottom-nav-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: #ffffff;
    border-top: 1px solid var(--line);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
    z-index: 90;
  }
  .bn-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 0;
    background: none;
    cursor: pointer;
    padding: 6px 4px;
    color: var(--ink-soft);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.02em;
  }
  .bn-tab .bn-icon { font-size: 22px; line-height: 1; }
  .bn-tab.active { color: var(--ink); }
  .bn-tab.active .bn-icon { transform: translateY(-1px); }
  .bn-tab:active { background: var(--paper-soft); }

  /* -----------------------------------------------------------
     FAB — "+ Tarefa rápida"
     ----------------------------------------------------------- */
  #fab-quick {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    right: 16px;
    bottom: calc(var(--bottom-nav-height) + var(--safe-bottom) + 16px);
    width: var(--fab-size);
    height: var(--fab-size);
    border-radius: 50%;
    background: var(--ink);
    color: #fff;
    border: 0;
    font-size: 28px;
    font-weight: 300;
    line-height: 1;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
    cursor: pointer;
    z-index: 95;
  }
  #fab-quick:active { transform: scale(0.95); }

  /* -----------------------------------------------------------
     Modais — fullscreen-sheet
     ----------------------------------------------------------- */
  #modal {
    padding: 0 !important;
    align-items: stretch !important;
  }
  #modal > .modal-content,
  #modal .modal-content,
  #modal [role="dialog"],
  #modal > div {
    /* o conteúdo do modal varia em cada componente; aplicamos o
       fullscreen ao primeiro filho directo do overlay */
    width: 100vw !important;
    max-width: 100vw !important;
    min-height: 100vh;
    border-radius: 0 !important;
    margin: 0 !important;
    padding: 16px !important;
    overflow-y: auto;
  }
  /* Modais que usam grid de 2 colunas no body → 1 coluna em mobile */
  #modal .grid.grid-cols-2 { grid-template-columns: 1fr !important; }
  #modal .grid.grid-cols-3 { grid-template-columns: 1fr !important; }
  #modal .col-span-2 { grid-column: 1 / -1 !important; }

  /* -----------------------------------------------------------
     Tabelas → Cards
     Aplica a todas as tabelas .table-cards-mobile.
     Pré-condição: cada <td> tem atributo data-label="…" para
     mostrar o cabeçalho inline (faltbacks: nada quebra se não
     tiver, simplesmente esconde o label).
     ----------------------------------------------------------- */
  table {
    /* tabelas normais ainda funcionam mas com font menor */
    font-size: 12px;
  }
  table.table-cards-mobile {
    display: block;
  }
  table.table-cards-mobile thead { display: none; }
  table.table-cards-mobile tbody { display: block; }
  table.table-cards-mobile tr {
    display: block;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  table.table-cards-mobile td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    border: 0;
    font-size: 13px;
  }
  table.table-cards-mobile td:first-child {
    font-weight: 600;
    font-size: 14px;
    padding-top: 0;
    border-bottom: 1px dashed var(--line);
    padding-bottom: 6px;
    margin-bottom: 4px;
  }
  table.table-cards-mobile td[data-label]::before {
    content: attr(data-label);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--ink-faint);
    font-weight: 600;
    flex-shrink: 0;
  }
  /* Cabeçalho de grupo (ano/mês no Programa) como barra de secção */
  table.table-cards-mobile tr.group-row {
    display: block;
    background: var(--paper-soft);
    border: 0;
    border-radius: 8px;
    padding: 8px 12px;
    margin: 10px 0 4px;
    font-weight: 700;
  }
  table.table-cards-mobile tr.group-row td {
    display: block;
    border: 0;
    padding: 0;
    font-size: 13px;
  }
  table.table-cards-mobile tr.group-row td::before { content: none; }

  /* Linha de atividade (histórico de tarefas) — bloco de largura total,
     sem moldura de card nem labels. */
  table.table-cards-mobile tr.task-activity-row {
    display: block;
    background: none;
    border: 0;
    padding: 0;
    margin: 0 0 6px;
  }
  table.table-cards-mobile tr.task-activity-row td {
    display: block;
    border: 0;
    padding: 0;
  }
  table.table-cards-mobile tr.task-activity-row td::before { content: none; }

  /* tr de "estado vazio" — não vira card */
  table.table-cards-mobile tr.empty-row {
    border: 0;
    background: none;
    text-align: center;
    padding: 24px 0;
  }
  table.table-cards-mobile tr.empty-row td { display: block; border: 0; }
  table.table-cards-mobile tr.empty-row td::before { content: none; }

  /* -----------------------------------------------------------
     Filtros — comprimidos
     Reduz padding e força que o painel ocupe menos vertical
     ----------------------------------------------------------- */
  .surface.mb-4 .grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .surface.mb-4 .grid > div { min-width: 0; }
  .surface.mb-4 input.input, .surface.mb-4 select.input {
    font-size: 13px;
    padding: 6px 8px;
  }
  .label { font-size: 11px; margin-bottom: 2px; }

  /* -----------------------------------------------------------
     Sininho — painel ocupa mais ecrã
     ----------------------------------------------------------- */
  .notif-panel {
    width: 92vw !important;
    right: -8px !important;
    max-height: 70vh !important;
  }

  /* -----------------------------------------------------------
     Calendário geral (views/calendario) — cells menores
     Quando o utilizador tiver de o usar mesmo em mobile,
     pelo menos as células ficam legíveis e tocáveis.
     A vista alternativa "agenda" é tratada em JS (task 24).
     ----------------------------------------------------------- */
  .cal-cell {
    min-height: 60px !important;
    padding: 4px 5px !important;
  }
  .cal-day { font-size: 11px !important; }
  .cal-evt {
    font-size: 9px !important;
    padding: 2px 4px !important;
    line-height: 1.2 !important;
  }
  .cal-grid { gap: 2px !important; }
  .cal-head { font-size: 9px !important; padding: 4px 0 !important; }

  /* Calendário de férias mobile: idem (a "agenda" vem em JS) */
  .fer-cal-cell { min-height: 60px !important; padding: 3px 4px !important; }
  .fer-cal-bar { font-size: 9px !important; padding: 1px 3px !important; }
  .fer-cal-day { font-size: 10px !important; }
  .fer-cal-head { font-size: 9px !important; padding: 3px 0 !important; }

  /* -----------------------------------------------------------
     Calendário — vista AGENDA (mobile)
     ----------------------------------------------------------- */
  #calendar-agenda { padding: 4px; }
  .agenda-day {
    padding: 10px 4px;
    border-bottom: 1px solid var(--line);
    cursor: pointer;
  }
  .agenda-day:last-child { border-bottom: 0; }
  .agenda-day-today {
    background: var(--sun-soft);
    border-radius: 8px;
    margin: 0 -4px;
    padding-left: 8px;
    padding-right: 8px;
    scroll-margin-top: 80px;
  }
  .agenda-day-header {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 4px;
  }
  .agenda-day-num {
    font-size: 16px;
    font-weight: 700;
    color: var(--ink);
    min-width: 26px;
  }
  .agenda-day-dow {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--ink-faint);
    font-weight: 600;
  }
  .agenda-day-items {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }
  .agenda-item {
    padding: 6px 8px;
    border-radius: 6px;
    font-size: 12px;
    background: var(--paper-soft);
    border: 1px solid var(--line);
  }
  .agenda-item.cal-evt-program { background: rgba(40,118,198,0.10); border-color: rgba(40,118,198,0.30); }
  .agenda-item.cal-evt-ferias  { background: rgba(232,156,28,0.10); border-color: rgba(232,156,28,0.30); }
  .agenda-item.cal-evt-emprestimo-atraso, .agenda-item.cal-evt-processo-atraso {
    background: rgba(220,38,38,0.10); border-color: rgba(220,38,38,0.30);
  }
  .agenda-item-title { font-weight: 600; color: var(--ink); }
  .agenda-item-sub   { font-size: 11px; color: var(--ink-soft); margin-top: 2px; }
  .agenda-empty {
    padding: 6px 4px;
    font-size: 11px;
    color: var(--ink-faint);
    font-style: italic;
    border-bottom: 1px dashed var(--line);
  }

  /* -----------------------------------------------------------
     Resumo (Hoje) — destacar visualmente o dia actual
     ----------------------------------------------------------- */
  .resumo-day-today {
    background: var(--sun-soft);
    border-radius: 10px;
    padding: 10px 12px !important;
    margin: 0 -4px;
    scroll-margin-top: 80px; /* não fica colado ao header sticky */
  }
  .resumo-day {
    padding: 8px 0 !important;
  }

  /* -----------------------------------------------------------
     Esconde elementos que não fazem sentido em mobile
     ----------------------------------------------------------- */
  .hide-on-mobile { display: none !important; }
}

/* Esconde "só em mobile" se o ecrã for grande */
@media (min-width: 769px) {
  .show-on-mobile { display: none !important; }
}
