:root {
    /* Tema Claro - Identidade Visual Oficial */
    --bg:             #E5EBEA;
    --surface:        #ffffff;
    --surface-2:      #DDD0C6;
    --primary:        #20515F;
    --primary-hover:  #2d6b7c;
    --text:           #737271;
    --text-strong:    #20515F;
    --border:         #DDD0C6;
    
    --brand-success:  #27ae60;
    --brand-danger:   #e74c3c;
    
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    
    --font-heading: 'League Spartan', sans-serif;
    --font-body: 'Merriweather', serif;
    
    --shadow-md: 0 4px 20px rgba(0,0,0,0.06);
    --transition: all 0.2s ease-in-out;
    --bottom-nav-h: 64px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: var(--font-body);
    background-color: var(--bg);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-heading);
    color: var(--text-strong);
    font-weight: 600;
}

.hidden { display: none !important; }
.text-primary { color: var(--primary); }

.screen { width: 100vw; height: 100vh; }
#login-screen {
    display: flex; align-items: center; justify-content: center;
    background: var(--bg);
}

.login-card {
    background-color: var(--surface);
    padding: 48px 40px; border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md); width: 100%; max-width: 420px;
    border: 1px solid var(--border);
}

.logo { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.logo i { color: var(--primary); width: 32px; height: 32px; }
.logo h2 { font-size: 28px; font-weight: 700; letter-spacing: -0.5px; }
.subtitle { color: var(--text); margin-bottom: 32px; font-size: 15px; font-family: var(--font-heading); font-weight: 400;}

.input-group { display: flex; flex-direction: column; gap: 8px; margin-bottom: 24px; flex: 1; }
.form-row { display: flex; gap: 16px; flex-wrap: wrap; }
label { 
    font-family: var(--font-heading); 
    font-size: 14px; 
    color: var(--primary); 
    font-weight: 600; 
    display: block; 
}

input, select, textarea {
    background-color: var(--surface); 
    border: 1.5px solid var(--border);
    color: var(--text-strong); 
    padding: 12px 16px; 
    border-radius: var(--radius-sm);
    font-family: var(--font-body); 
    font-size: 16px; 
    transition: var(--transition);
    width: 100%;
}
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--primary);
}
.help-text { font-size: 13px; color: var(--text); margin-top: 4px; }

/* Botões */
button {
    cursor: pointer; position: relative;
    border-radius: var(--radius-sm); transition: var(--transition);
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    font-family: var(--font-heading); font-weight: 600; border: none;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: white; padding: 12px 24px; font-size: 16px; width: 100%; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-secondary { background: var(--surface-2); color: var(--text-strong); padding: 10px 20px; font-size: 15px; }
.btn-secondary:hover:not(:disabled) { background: var(--border); }

.btn-success { background-color: rgba(39, 174, 96, 0.1); color: var(--brand-success); border: 1.5px solid rgba(39, 174, 96, 0.2); padding: 8px 16px; font-size: 14px; }
.btn-success:hover:not(:disabled) { background-color: var(--brand-success); color: white; }

.btn-outline { background: transparent; color: var(--primary); border: 1.5px solid var(--primary); padding: 8px 16px; font-size: 14px; }
.btn-outline:hover:not(:disabled) { background: var(--primary); color: white; }

.btn-text { background: transparent; color: var(--text); padding: 8px 12px; font-size: 14px; }
.btn-text:hover:not(:disabled) { color: var(--primary); }

.btn-danger-outline { background: transparent; color: var(--brand-danger); border: 1.5px solid rgba(231,76,60,0.3); padding: 8px 16px; font-size: 14px; }
.btn-danger-outline:hover:not(:disabled) { background: var(--brand-danger); color: white; }

/* Layout base — mobile first */
#app-screen { display: flex; flex-direction: column; min-height: 100vh; }

.sidebar { display: none; }

.main-content {
    flex: 1;
    padding: 20px 16px calc(var(--bottom-nav-h) + 16px);
    min-height: 100vh;
}

/* Bottom Nav — visível por padrão (mobile) */
.bottom-nav {
    display: flex;
    position: fixed; bottom: 0; left: 0; right: 0;
    height: var(--bottom-nav-h);
    background: var(--surface);
    border-top: 1.5px solid var(--border);
    align-items: stretch;
    justify-content: space-around;
    z-index: 200;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.07);
}
.bottom-nav-btn {
    background: transparent; border: none;
    color: var(--text);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 4px; padding: 6px 0;
    font-size: 11px; font-weight: 600;
    font-family: var(--font-heading);
    flex: 1; border-radius: 0; text-align: center;
}
.bottom-nav-btn i { width: 22px; height: 22px; }
.bottom-nav-btn.active { color: var(--primary); }
.bottom-nav-btn:active { transform: scale(0.93); }

/* Cabeçalhos de Aba */
.tab-content { animation: fadeIn 0.3s ease-out; }
.content-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 40px; padding-bottom: 24px; border-bottom: 1.5px solid var(--border); }
.content-header h1 { font-size: 32px; font-weight: 700; margin-bottom: 8px; }
.content-header p { color: var(--text); font-size: 16px; }

.header-actions { display: flex; align-items: center; gap: 16px; }
.select-filter { padding: 10px 16px; background-color: var(--surface); min-width: 180px; font-family: var(--font-heading); font-weight: 600;}

/* --- Lista de Internados --- */
.setor-group { margin-bottom: 48px; }
.setor-title { font-size: 18px; font-weight: 700; color: var(--primary); margin-bottom: 20px; display: flex; align-items: center; gap: 12px; text-transform: uppercase; letter-spacing: 0.5px; font-family: var(--font-heading);}
.setor-title::after { content: ''; flex: 1; height: 1.5px; background-color: var(--border); }

.paciente-card { background: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius-md); margin-bottom: 16px; overflow: hidden; transition: var(--transition); box-shadow: 0 2px 8px rgba(0,0,0,0.02); }
.paciente-card:hover { border-color: var(--primary); box-shadow: 0 4px 12px rgba(32, 81, 95, 0.08); }
.paciente-header { padding: 20px 24px; display: flex; align-items: center; justify-content: space-between; cursor: pointer; }
.paciente-info-main { display: flex; align-items: center; gap: 24px; }
.paciente-nome-block { display: flex; flex-direction: column; }
.paciente-nome { font-size: 18px; font-family: var(--font-heading); font-weight: 700; color: var(--text-strong); }
.paciente-meta { font-size: 14px; color: var(--text); margin-top: 4px; }

.badge { padding: 4px 10px; border-radius: 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; font-family: var(--font-heading);}
.badge.internado { background-color: rgba(32, 81, 95, 0.1); color: var(--primary); }
.badge.alta { background-color: rgba(39, 174, 96, 0.1); color: var(--brand-success); }

.paciente-actions { display: flex; gap: 12px; pointer-events: auto; }
.paciente-detalhe { display: none; padding: 0 24px 24px 24px; border-top: 1.5px solid var(--border); margin-top: 16px; padding-top: 20px; }
.paciente-card.expanded .paciente-detalhe { display: block; animation: fadeIn 0.2s ease-out; }
.detalhe-block { margin-bottom: 20px; }
.detalhe-block h4 { font-family: var(--font-heading); font-size: 14px; text-transform: uppercase; color: var(--primary); margin-bottom: 8px; font-weight: 700;}
.detalhe-block p { font-size: 15px; color: var(--text-strong); background-color: var(--bg); padding: 16px; border-radius: var(--radius-sm); border: 1.5px solid var(--border); }

/* --- Pessoas / Produção --- */
.grid-layout { display: grid; grid-template-columns: 320px 1fr; gap: 32px; align-items: start; }
.card { background-color: var(--surface); border: 1.5px solid var(--border); border-radius: var(--radius-md); padding: 32px; box-shadow: var(--shadow-md); }
.card h3 { font-size: 20px; margin-bottom: 24px; padding-bottom: 16px; border-bottom: 1.5px solid var(--border); color: var(--primary);}

.membro-item { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-radius: var(--radius-sm); cursor: pointer; transition: var(--transition); margin-bottom: 12px; border: 1.5px solid transparent; }
.membro-item:hover { background-color: var(--bg); border-color: var(--border); }
.membro-item.active { background-color: var(--bg); border-color: var(--primary); }

.membro-nome { font-weight: 600; font-size: 16px; font-family: var(--font-heading); color: var(--text-strong);}
.membro-count { background-color: var(--surface-2); padding: 4px 12px; border-radius: 12px; font-size: 14px; font-weight: 700; color: var(--primary); font-family: var(--font-heading);}

.detalhe-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 32px; padding-bottom: 20px; border-bottom: 1.5px solid var(--border); }
.detalhe-header h3 { margin-bottom: 0; border-bottom: none; padding-bottom: 0; }

.visita-table { width: 100%; border-collapse: collapse; }
.visita-table th, .visita-table td { padding: 16px; text-align: left; border-bottom: 1.5px solid var(--border); font-size: 15px; color: var(--text-strong);}
.visita-table th { font-weight: 700; color: var(--primary); text-transform: uppercase; font-size: 13px; font-family: var(--font-heading);}

/* --- Form Container --- */
.form-container { background: var(--surface); padding: 40px; border-radius: var(--radius-md); border: 1.5px solid var(--border); box-shadow: var(--shadow-md); max-width: 900px;}

/* --- Toast --- */
#toast-container { position: fixed; bottom: 32px; right: 32px; display: flex; flex-direction: column; gap: 16px; z-index: 9999; }
.toast { background-color: var(--surface); border-left: 6px solid var(--primary); box-shadow: var(--shadow-md); padding: 20px 24px; border-radius: var(--radius-sm); display: flex; align-items: center; gap: 16px; animation: slideIn 0.3s ease-out; font-size: 15px; font-weight: 600; min-width: 320px; color: var(--text-strong); font-family: var(--font-heading); border-top: 1px solid var(--border); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);}
.toast.success { border-left-color: var(--brand-success); }
.toast.error { border-left-color: var(--brand-danger); }
.toast.success i { color: var(--brand-success); }
.toast.error i { color: var(--brand-danger); }

.empty-state { text-align: center; padding: 64px 48px; color: var(--text); background: var(--bg); border-radius: var(--radius-md); border: 1.5px dashed var(--border);}
.empty-state i { width: 56px; height: 56px; margin-bottom: 20px; opacity: 0.5; color: var(--primary); }
.empty-state p { font-family: var(--font-heading); font-size: 18px; font-weight: 500;}

@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideIn { from { opacity: 0; transform: translateX(100%); } to { opacity: 1; transform: translateX(0); } }

/* --- Modal --- */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.45);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000; backdrop-filter: blur(3px);
    animation: fadeIn 0.15s ease-out;
}
.modal-card {
    background: var(--surface); border-radius: var(--radius-lg);
    padding: 40px; max-width: 440px; width: 90%;
    border: 1.5px solid var(--border);
    box-shadow: 0 24px 64px rgba(0,0,0,0.18);
    animation: slideUp 0.2s ease-out;
}
.modal-card h3 { font-size: 22px; margin-bottom: 8px; }
.modal-card > p { margin-bottom: 24px; font-size: 15px; color: var(--text); }
.modal-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 28px; }
@keyframes slideUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }

/* --- Content header (mobile base) --- */
.content-header {
    display: flex; flex-direction: column; align-items: flex-start;
    gap: 12px; margin-bottom: 20px; padding-bottom: 16px;
    border-bottom: 1.5px solid var(--border);
}
.content-header h1 { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.content-header p { color: var(--text); font-size: 13px; }
.header-actions { width: 100%; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.search-box { flex: 1 1 100%; }
.search-box input { width: 100%; }
.select-filter { flex: 1; min-width: unset; }

/* --- Form mobile --- */
.form-container { padding: 20px 16px; }
.form-row { flex-direction: column; gap: 0; }

/* --- Login mobile --- */
.login-card { padding: 32px 20px; margin: 16px; }

/* --- Toast mobile (acima do bottom nav) --- */
#toast-container {
    position: fixed;
    bottom: calc(var(--bottom-nav-h) + 16px);
    right: 16px; left: 16px;
    display: flex; flex-direction: column; gap: 12px; z-index: 9999;
}
.toast { min-width: unset; width: 100%; }

/* --- Setor title mobile --- */
.setor-title { font-size: 15px; }

/* --- Linha de paciente (mobile) --- */
.paciente-row {
    background: var(--surface);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: 10px;
    overflow: hidden;
    transition: var(--transition);
}
.paciente-row:hover { border-color: var(--primary); }
.paciente-row.status-alta { border-left: 4px solid var(--brand-success); }

.paciente-row-header {
    display: flex; align-items: center;
    padding: 12px 14px; gap: 10px; cursor: pointer;
}
.paciente-row-info {
    flex: 1; min-width: 0;
    display: flex; flex-direction: column; gap: 2px;
}
.paciente-row-nome {
    font-family: var(--font-heading); font-weight: 700;
    font-size: 15px; color: var(--text-strong);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.paciente-row-meta { font-size: 12px; color: var(--text); }

.paciente-row-actions {
    display: flex; align-items: center; gap: 6px; flex-shrink: 0;
}
.paciente-row-actions button {
    background: transparent; border: none; padding: 6px;
    border-radius: var(--radius-sm); color: var(--text);
    display: flex; align-items: center; justify-content: center;
}
.paciente-row-actions button:hover { color: var(--primary); background: var(--bg); }
.paciente-row-actions button.btn-visita-ok { color: var(--brand-success); }
.paciente-row-actions button.btn-danger-icon:hover { color: var(--brand-danger); background: rgba(231,76,60,0.08); }
.paciente-row-actions i { width: 18px; height: 18px; }

.paciente-row-detalhe {
    display: none;
    padding: 12px 14px 14px;
    border-top: 1.5px solid var(--border);
}
.paciente-row.expanded .paciente-row-detalhe {
    display: block; animation: fadeIn 0.2s ease-out;
}

/* --- Membros mobile (tela 1 drill-down) --- */
.membros-list-mobile { display: flex; flex-direction: column; gap: 10px; }
.membro-card-mobile {
    background: var(--surface); border: 1.5px solid var(--border);
    border-radius: var(--radius-md); padding: 16px;
    display: flex; align-items: center; justify-content: space-between;
    cursor: pointer; transition: var(--transition);
}
.membro-card-mobile:hover { border-color: var(--primary); }
.membro-card-mobile:active { transform: scale(0.98); }
.membro-card-mobile-nome {
    font-family: var(--font-heading); font-weight: 700;
    font-size: 16px; color: var(--text-strong);
}
.membro-card-mobile-sub { font-size: 13px; color: var(--text); margin-top: 2px; }
.membro-card-mobile-badge {
    background: var(--surface-2); color: var(--primary);
    font-family: var(--font-heading); font-weight: 700;
    font-size: 20px; padding: 8px 16px;
    border-radius: var(--radius-sm); min-width: 56px; text-align: center;
}

/* --- Desktop (≥769px) — restaura sidebar e layout original --- */
@media (min-width: 769px) {
    /* Sidebar */
    .sidebar {
        display: flex;
        width: 280px; height: 100vh;
        background-color: var(--surface);
        border-right: 1.5px solid var(--border);
        flex-direction: column;
        padding: 32px 24px;
        position: fixed; left: 0; top: 0;
    }
    .sidebar-header { display: flex; align-items: center; gap: 12px; margin-bottom: 48px; }
    .sidebar-header h2 { font-size: 24px; }
    .sidebar-nav { display: flex; flex-direction: column; gap: 8px; flex: 1; }
    .nav-btn {
        background: transparent; color: var(--text);
        padding: 14px 16px; justify-content: flex-start;
        font-size: 16px; width: 100%; text-align: left;
        font-family: var(--font-heading); font-weight: 500;
    }
    .nav-btn:hover { background-color: var(--bg); color: var(--primary); }
    .nav-btn.active {
        background-color: var(--bg); color: var(--primary);
        font-weight: 700; border-left: 4px solid var(--primary);
        border-radius: 0 8px 8px 0;
    }
    .sidebar-footer {
        border-top: 1.5px solid var(--border); padding-top: 24px;
        display: flex; justify-content: space-between; align-items: center;
    }
    .user-info {
        display: flex; align-items: center; gap: 8px;
        font-size: 14px; color: var(--text-strong);
        font-family: var(--font-heading); font-weight: 600;
    }

    /* Esconde bottom nav */
    .bottom-nav { display: none; }

    /* Main content */
    .main-content { margin-left: 280px; padding: 48px 64px; }

    /* Header */
    .content-header { flex-direction: row; align-items: flex-end; margin-bottom: 40px; padding-bottom: 24px; }
    .content-header h1 { font-size: 32px; }
    .header-actions { flex-wrap: nowrap; width: auto; }
    .search-box { flex: unset; }
    .search-box input { width: 280px; }
    .select-filter { min-width: 180px; flex: unset; }

    /* Grid Pessoas — dois painéis */
    .grid-layout { grid-template-columns: 320px 1fr; }

    /* Toast — posição desktop */
    #toast-container { bottom: 32px; right: 32px; left: auto; }
    .toast { min-width: 320px; width: auto; }

    /* Cards de paciente — desktop usa card expandível */
    .paciente-row { display: none !important; }
    .paciente-card { display: block; }

    /* Pessoas: desktop mostra painel lateral, esconde drill-down mobile */
    #pessoas-tela-lista { display: none !important; }
    #pessoas-tela-detalhe { display: none !important; }
    #pessoas-desktop-layout { display: grid !important; }
}

/* --- Componentes Novos: Busca e Loader --- */
.search-box { position: relative; display: flex; align-items: center; }
.search-box i { position: absolute; left: 16px; color: var(--text); width: 18px; height: 18px; }
.search-box input { 
    height: 44px; padding-left: 44px; padding-right: 16px; width: 280px; 
    font-size: 14px; background-color: var(--surface); border: 1.5px solid var(--border); 
    color: var(--text-strong); border-radius: var(--radius-sm); font-family: var(--font-body); 
    transition: var(--transition); margin:0;
}
.search-box input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(32, 81, 95, 0.1); }

.btn-loading { color: transparent !important; pointer-events: none; position: relative; }
.btn-loading::after {
    content: ""; position: absolute; width: 18px; height: 18px;
    top: 50%; left: 50%; margin-top: -9px; margin-left: -9px;
    border-radius: 50%; border: 3px solid rgba(255,255,255,0.3); border-top-color: white;
    animation: spinnerRotate 0.8s linear infinite;
}
.btn-outline.btn-loading::after, .btn-secondary.btn-loading::after { border-color: rgba(32, 81, 95, 0.2); border-top-color: var(--primary); }
@keyframes spinnerRotate { 100% { transform: rotate(360deg); } }
