/* =====================================================================
   Sistema PSE — modo aplicativo (tema, gaveta, barra de abas, FAB)
   Carregado depois de style.css
   ===================================================================== */

/* Toque: sem realce azul, sem zoom por toque duplo em controles */
a, button, .btn, .nav-item, .tab-item, .cell-content, .tema-item, .calendar-day, .stat-card, .module-card {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

html { scroll-padding-top: calc(var(--topbar-h) + 12px); }

/* ---------- Alternância de tema ---------- */
.theme-icon-light { display: none; }
:root[data-theme="dark"] .theme-icon-dark { display: none; }
:root[data-theme="dark"] .theme-icon-light { display: inline-block; }

button.nav-item {
    width: 100%;
    border: none;
    background: none;
    font: inherit;
    cursor: pointer;
    text-align: left;
}

/* ---------- Sidebar: cabeçalho e créditos ---------- */
.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 12px;
}

.sidebar-close { display: none; }

.sidebar-credits {
    font-size: 11px;
    color: var(--text-3);
    text-align: center;
    margin-top: 10px;
}

/* ---------- Topbar ---------- */
.topbar-avatar {
    display: none;
    border: none;
    cursor: pointer;
    width: 36px;
    height: 36px;
}

/* ---------- Barra de abas (somente celular) ---------- */
.tabbar { display: none; }

/* ---------- Botão flutuante (FAB) ---------- */
.fab {
    display: none;
    position: fixed;
    right: 18px;
    bottom: calc(var(--tabbar-h) + 18px + env(safe-area-inset-bottom));
    z-index: 820;
    width: 58px;
    height: 58px;
    border-radius: 18px;
    border: none;
    background: var(--gradient);
    color: #fff;
    box-shadow: 0 12px 28px -8px rgba(79, 70, 229, .65);
    cursor: pointer;
    place-items: center;
    transition: transform .2s var(--ease), box-shadow .2s;
}

.fab .i { width: 26px; height: 26px; }
.fab:active { transform: scale(.94); }

/* ---------- Tema escuro: ajustes pontuais ---------- */
:root[data-theme="dark"] .hero { box-shadow: 0 18px 40px -18px rgba(0, 0, 0, .7); }
:root[data-theme="dark"] .btn-primary { box-shadow: none; }
:root[data-theme="dark"] .modal { background: rgba(0, 0, 0, .6); }
:root[data-theme="dark"] .toast { border: 1px solid var(--border-strong); }
:root[data-theme="dark"] select.form-control {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%236f7a96' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E");
}
:root[data-theme="dark"] .calendar-day.other-month { opacity: .4; }
:root[data-theme="dark"] .auth-main,
:root[data-theme="dark"] body.auth { background: var(--bg); }
:root[data-theme="dark"] .activation-box { background: var(--surface); }

/* =====================================================================
   Celular: experiência de aplicativo
   ===================================================================== */
@media (max-width: 768px) {
    :root { --topbar-h: 56px; }

    body.has-shell {
        padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
        overscroll-behavior-y: none;
    }

    /* Campos com 16px: evita o zoom automático do iPhone ao focar */
    .form-control, select.form-control, textarea.form-control { font-size: 16px; }

    /* Barra superior compacta, sólida, com área segura (entalhe) */
    .topbar {
        padding: env(safe-area-inset-top) 16px 0;
        height: calc(var(--topbar-h) + env(safe-area-inset-top));
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        gap: 10px;
    }

    .topbar.scrolled { background: var(--surface); }
    .topbar .menu-toggle,
    .topbar-date { display: none !important; }
    .topbar-title h1 { font-size: 18px; }
    .topbar-title p { display: none; }
    .topbar .theme-toggle { width: 38px; height: 38px; border: none; box-shadow: none; background: transparent; }
    .topbar-avatar { display: grid; }

    /* Gaveta "Mais": surge por baixo das abas, como uma folha */
    .sidebar {
        width: min(86vw, 340px);
        padding-top: env(safe-area-inset-top);
    }
    .sidebar-close { display: inline-flex; }
    .sidebar-footer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
    .nav-item { padding: 12px; font-size: 15px; }

    /* Barra de abas inferior */
    .tabbar {
        display: flex;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 840;
        height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
        padding: 6px 6px env(safe-area-inset-bottom);
        background: var(--topbar-bg-scrolled);
        backdrop-filter: saturate(180%) blur(16px);
        -webkit-backdrop-filter: saturate(180%) blur(16px);
        border-top: 1px solid var(--border);
    }

    .tab-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        border: none;
        background: none;
        font: inherit;
        font-size: 11px;
        font-weight: 600;
        color: var(--text-3);
        text-decoration: none;
        border-radius: 12px;
        cursor: pointer;
        transition: color .15s;
    }

    .tab-item .i {
        width: 24px;
        height: 24px;
        padding: 2px 14px;
        box-sizing: content-box;
        border-radius: 999px;
        transition: background .2s, color .2s;
    }

    .tab-item.active { color: var(--primary); }
    .tab-item.active .i { background: var(--primary-50); }
    .tab-item:active .i { transform: scale(.92); }

    /* Botão flutuante substitui o botão principal do cabeçalho */
    .fab { display: grid; }
    .has-fab [data-fab] { display: none !important; }

    /* Rodapé e créditos ficam na gaveta */
    .footer { display: none; }

    /* Conteúdo com respiro para o FAB */
    .container { padding-bottom: 96px; }

    /* Toasts acima da barra de abas */
    .toast-stack { bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom)); }

    /* Modais como folha inferior, respeitando a área segura */
    .modal-footer { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

    /* Cartões e listas mais "nativos" */
    .card { box-shadow: none; }
    .hero { border-radius: 20px; }
    .module-card, .stat-card { box-shadow: none; }
    .module-card:hover, a.stat-card:hover { transform: none; }
    .module-card:active, a.stat-card:active, .tema-card:active { transform: scale(.98); }

    /* Abas (segmented) ocupam a largura toda */
    .tabs { display: flex; width: 100%; }
    .tab { flex: 1; justify-content: center; padding: 9px 10px; }
}

/* App instalado (tela cheia): status bar na cor do cabeçalho */
html.standalone .topbar { border-bottom-color: var(--border); }

/* Botão "marcar como realizada" nas tabelas */
.action-buttons .btn-success-ghost:hover { background: var(--success-50); color: var(--success); }
.action-buttons .btn-success-ghost { color: var(--success); }

/* Lista de próximas ações (Início) */
.agenda-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }

.agenda-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    text-decoration: none;
    color: inherit;
    transition: border-color .15s, background .15s;
}

.agenda-item:hover { border-color: var(--primary-100); background: var(--row-hover); }

.agenda-date {
    width: 48px;
    flex-shrink: 0;
    text-align: center;
    border-radius: 12px;
    background: var(--primary-50);
    color: var(--primary);
    padding: 6px 0;
    line-height: 1.1;
}

.agenda-date strong { display: block; font-size: 18px; font-weight: 800; }
.agenda-date span { font-size: 11px; font-weight: 700; text-transform: uppercase; }
.agenda-item.atrasada .agenda-date { background: var(--danger-50); color: var(--danger); }

.agenda-info { flex: 1; min-width: 0; }
.agenda-info strong { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agenda-info span { font-size: 13px; color: var(--text-3); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Filtros recolhíveis no celular */
.btn-filtros { display: none; }

@media (max-width: 768px) {
    .toolbar:has(.btn-filtros) .search-field { flex: 1 1 0; }
    .btn-filtros { display: inline-flex; flex: 0 0 auto; height: 42px; }
    .toolbar .filtro-extra { display: none; }
    .toolbar.aberta .filtro-extra { display: block; flex: 1 1 100%; }
    .toolbar.aberta .btn-filtros { background: var(--primary-50); color: var(--primary); border-color: var(--primary-100); }
}

/* ===================== Anexos ===================== */
.anexo-btn { position: relative; }

.anexo-count {
    position: absolute;
    top: 1px;
    right: 1px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
}

.upload-box {
    border: 1.5px dashed var(--border-strong);
    border-radius: 16px;
    padding: 14px;
    margin-bottom: 18px;
    background: var(--surface-2);
}

.upload-box .tabs { width: 100%; display: flex; }
.upload-box .tab { flex: 1; justify-content: center; }

.upload-actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 6px;
}

.upload-actions .btn { cursor: pointer; }

/* "Tirar foto" só aparece em aparelhos de toque */
@media (hover: hover) and (pointer: fine) {
    .camera-only { display: none; }
}

.upload-progress {
    height: 6px;
    border-radius: 999px;
    background: var(--track);
    overflow: hidden;
    margin-top: 10px;
    display: none;
}

.upload-progress.ativo { display: block; }

.upload-progress span {
    display: block;
    height: 100%;
    width: 0;
    background: var(--gradient);
    transition: width .2s;
}

.anexos-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
}

.anexo-thumb,
.anexo-file {
    position: relative;
    display: block;
    border-radius: 14px;
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--surface-2);
    text-decoration: none;
    color: inherit;
    aspect-ratio: 1 / 1;
    transition: transform .15s var(--ease), box-shadow .15s;
}

.anexo-thumb:hover,
.anexo-file:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

.anexo-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.anexo-file {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px;
    text-align: center;
    color: var(--primary);
}

.anexo-nome {
    font-size: 12px;
    font-weight: 600;
    color: var(--text);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.anexo-del {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 30px;
    height: 30px;
    border-radius: 10px;
    border: none;
    background: rgba(15, 23, 42, .65);
    color: #fff;
    display: grid;
    place-items: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s;
}

.anexo-thumb:hover .anexo-del,
.anexo-file:hover .anexo-del { opacity: 1; }

@media (hover: none) {
    .anexo-del { opacity: 1; }
}

/* Campo de arquivo em forma de botão (registro rápido) */
.file-pick {
    display: flex !important;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 46px;
    padding: 0 12px;
    margin: 0 !important;
    border: 1.5px dashed var(--border-strong);
    border-radius: 12px;
    color: var(--text-2) !important;
    cursor: pointer;
    font-weight: 600;
    text-align: center;
    transition: border-color .15s, background .15s;
}

.file-pick input { display: none; }
.file-pick:hover { border-color: var(--primary); }
.file-pick.selecionado { border-style: solid; border-color: var(--success); background: var(--success-50); color: var(--success) !important; }

/* ===================== Gestão (perfis, ciclos, unidades) ===================== */
.ciclo-picker {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 40px;
    padding: 0 6px 0 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text-2);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.ciclo-picker select {
    border: none;
    background: transparent;
    color: var(--text);
    font: inherit;
    cursor: pointer;
    outline: none;
    max-width: 170px;
}

.tab-badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--warning);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: inline-grid;
    place-items: center;
}

.tabs .tab-badge { background: var(--primary); }
#badgePendentes { background: var(--warning); }

.perfil-card { cursor: pointer; }

.perm-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.perm-chip {
    font-size: 11px;
    font-weight: 600;
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--neutral-bg);
    color: var(--text-2);
}

.perm-chip.perm-total { background: var(--success-50); color: var(--success); }
.perm-chip.perm-edita { background: var(--primary-50); color: var(--primary); }
.perm-chip.perm-ve { background: var(--info-50); color: var(--info); }
.perm-chip.perm-erro { background: var(--danger-50); color: var(--danger); }

.perm-table { border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }

.perm-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) repeat(4, 64px);
    align-items: center;
    padding: 10px 14px;
    border-top: 1px solid var(--border);
}

.perm-row:first-child { border-top: none; }
.perm-row > label { display: grid; place-items: center; cursor: pointer; height: 32px; }
.perm-row > span:first-child small { display: block; font-size: 12px; color: var(--text-3); }

.perm-head {
    background: var(--surface-2);
    font-size: 12px;
    font-weight: 700;
    color: var(--text-3);
    text-transform: uppercase;
    letter-spacing: .04em;
}

.perm-head span:not(:first-child) { text-align: center; }

.check-list {
    display: grid;
    gap: 6px;
    max-height: 320px;
    overflow: auto;
    padding-right: 4px;
}

.check-list .check-item small { display: block; }

.metas-lista { display: grid; gap: 6px; max-height: 360px; overflow: auto; }

.meta-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 12px;
}

.meta-nome { flex: 1; font-size: 13px; font-weight: 500; }
.meta-input { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; color: var(--text-2); }
.meta-input .form-control { width: 84px; height: 36px; text-align: right; }

@media (max-width: 768px) {
    .topbar .ciclo-picker { height: 34px; padding: 0 4px 0 10px; font-size: 12px; }
    .topbar .ciclo-picker .i { display: none; }
    .topbar .ciclo-picker select { max-width: 110px; }
    .perm-row { grid-template-columns: minmax(0, 1fr) repeat(4, 44px); padding: 8px 10px; }
    .perm-head span:not(:first-child) { font-size: 10px; }
}

/* Seleção em lote (Ações → e-SUS) */
.col-sel { width: 36px; }
.col-sel input { width: 18px; height: 18px; }

.selecao-bar {
    display: none;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    padding: 10px 14px;
    margin-bottom: 14px;
    border-radius: 14px;
    background: var(--primary-50);
    border: 1px solid var(--primary-100);
    color: var(--primary);
    font-weight: 600;
}

.selecao-bar.ativa { display: flex; }
.selecao-bar span { flex: 1; }

@media (max-width: 640px) {
    .table td.col-sel { border-bottom: none; padding-bottom: 0; }
    .table td.col-sel:empty { display: none; }
    .table td.col-sel::before { content: 'Selecionar'; }
}

@media (max-width: 640px) {
    /* Quando a 1ª coluna é a de seleção, a 2ª vira o título do cartão */
    .table tbody tr td.col-sel + td { text-align: left; font-size: 15px; }
    .table td.col-sel + td::before { display: none; }
    .table td.col-sel { text-align: left; }
    .table td.col-sel::before { content: none; }
}

/* Metas do ciclo */
.metas-progresso { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px 28px; }
.meta-linha .progress-head span { min-width: 0; }
.meta-bar { position: relative; overflow: visible; height: 10px; }
.meta-bar > span { border-radius: 999px; }
.meta-marca {
    position: absolute;
    top: -4px;
    width: 2px;
    height: 18px;
    background: var(--text);
    border-radius: 2px;
    transform: translateX(-1px);
    opacity: .55;
}
@media (max-width: 640px) { .metas-progresso { grid-template-columns: 1fr; } }

/* Cartões de resumo que funcionam como filtro */
button.stat-card { font: inherit; text-align: left; cursor: pointer; width: 100%; }
button.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.filtro-card.ativo { border-color: var(--primary); box-shadow: var(--ring); }

/* Painel inicial */
.hero-meta:empty { display: none; }
.hero-progress { margin-top: 16px; max-width: 420px; position: relative; z-index: 1; }
.hero-progress .progress { background: rgba(255, 255, 255, .25); margin: 6px 0; }
.hero-progress small { opacity: .85; }

.pendencias-lista { display: grid; gap: 8px; margin-bottom: 20px; }

.pendencia {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: 14px;
    text-decoration: none;
    font-size: 14px;
    border: 1px solid transparent;
    transition: transform .15s var(--ease);
}

.pendencia span { flex: 1; color: var(--text); }
.pendencia:hover { transform: translateX(3px); }
.pendencia.tone-danger { border-color: var(--danger-border); }
.pendencia.tone-warning { border-color: var(--warning-border); }
.pendencia.tone-info { border-color: var(--info-border); }

.tab-badge[hidden] { display: none; }

/* Configurações */
.copy-field { display: flex; gap: 8px; }
.copy-field .form-control { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; }
details.form-group > summary { cursor: pointer; color: var(--primary); font-weight: 600; font-size: 14px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 6px; }
.config-grid > * { min-width: 0; }
.config-grid { align-items: start; }
@media (max-width: 768px) {
    .config-grid { grid-template-columns: minmax(0, 1fr); }
}
