/*
 * App shell: top navbar + fixed sidebar + content area, plus a few MD3-flavoured
 * components. Bootstrap 5 provides the base; these rules theme it and add the
 * shell. (De-iframed adaptation of the health home shell.)
 */
* { box-sizing: border-box; }

body {
    font-family: 'Roboto', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--md-sys-color-background);
    color: var(--md-sys-color-on-background);
    margin: 0;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--md-sys-color-primary); }

/* Bootstrap primary -> brand colour */
.btn-primary {
    --bs-btn-bg: var(--md-sys-color-primary);
    --bs-btn-border-color: var(--md-sys-color-primary);
    --bs-btn-hover-bg: var(--app-primary-hover);
    --bs-btn-hover-border-color: var(--app-primary-hover);
    --bs-btn-active-bg: var(--app-primary-hover);
    --bs-btn-disabled-bg: var(--md-sys-color-primary);
    --bs-btn-disabled-border-color: var(--md-sys-color-primary);
}

/* ---- Navbar ------------------------------------------------------------- */
.app-navbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 64px;
    background: var(--md-sys-color-surface);
    border-bottom: 1px solid var(--md-sys-color-outline-variant);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 1050;
    box-shadow: var(--md-sys-elevation-1);
}
.navbar-left, .navbar-right { display: flex; align-items: center; gap: 12px; }
.logo-container { display: flex; align-items: center; gap: 12px; text-decoration: none; }
.logo-avatar {
    width: 40px; height: 40px;
    background: linear-gradient(135deg, var(--app-grad-a) 0%, var(--app-grad-b) 100%);
    color: #fff; border-radius: var(--md-sys-shape-corner-full);
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; font-weight: 600; letter-spacing: 0.5px;
}
img.logo-avatar { object-fit: cover; background: var(--md-sys-color-surface-container); } /* org logo */
.logo-name { font-size: 1.25rem; font-weight: 600; color: var(--md-sys-color-primary); }

.theme-toggle-btn, .navbar-icon-btn, .mobile-menu-toggle {
    width: 40px; height: 40px;
    border-radius: var(--md-sys-shape-corner-full);
    background: var(--md-sys-color-surface-container);
    border: 1px solid var(--md-sys-color-outline-variant);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; text-decoration: none;
    transition: all 0.2s ease;
    color: var(--md-sys-color-on-surface);
}
.theme-toggle-btn:hover, .navbar-icon-btn:hover, .mobile-menu-toggle:hover {
    background: var(--md-sys-color-surface-container-highest);
    box-shadow: var(--md-sys-elevation-1);
}
.theme-toggle-btn i { color: var(--app-navbar-toggle-icon); font-size: 18px; }
.navbar-icon-btn { color: var(--app-navbar-icon); font-size: 18px; }
.navbar-icon-btn--danger { color: var(--md-sys-color-error); }
.mobile-menu-toggle { display: none; }
.mobile-menu-toggle i { font-size: 20px; }

.user-avatar {
    width: 40px; height: 40px;
    background: linear-gradient(135deg, var(--app-grad-a) 0%, var(--app-grad-b) 100%);
    color: #fff; border: none; padding: 0; border-radius: var(--md-sys-shape-corner-full);
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; font-weight: 600; cursor: pointer;
    transition: box-shadow 0.2s ease;
}
.user-avatar:hover, .user-avatar:focus-visible, .user-avatar[aria-expanded="true"] {
    box-shadow: 0 0 0 2px var(--md-sys-color-surface), 0 0 0 4px var(--md-sys-color-primary);
    outline: none;
}

/* ---- Navbar user (account) dropdown ------------------------------------- */
.user-menu-dropdown {
    min-width: 224px; margin-top: 8px; padding: 8px;
    background: var(--md-sys-color-surface);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--md-sys-shape-corner-medium);
    box-shadow: var(--md-sys-elevation-2);
}
.user-menu-header { padding: 6px 12px 10px; }
.user-menu-name { font-weight: 600; font-size: 0.95rem; color: var(--md-sys-color-on-surface); }
.user-menu-role { font-size: 0.8rem; color: var(--md-sys-color-on-surface-variant); }
.user-menu-dropdown .dropdown-item {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 12px; border-radius: var(--md-sys-shape-corner-small);
    color: var(--md-sys-color-on-surface);
}
.user-menu-dropdown .dropdown-item:hover,
.user-menu-dropdown .dropdown-item:focus {
    background: var(--md-sys-color-surface-container-highest);
    color: var(--md-sys-color-on-surface);
}
.user-menu-dropdown .dropdown-item.active {
    background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-on-primary-container);
}
.user-menu-dropdown .dropdown-item i { font-size: 16px; color: var(--md-sys-color-primary); }
.user-menu-dropdown .dropdown-item.active i { color: inherit; }
.user-menu-dropdown .dropdown-item--danger,
.user-menu-dropdown .dropdown-item--danger i { color: var(--md-sys-color-error); }
.user-menu-dropdown .dropdown-item--danger:hover,
.user-menu-dropdown .dropdown-item--danger:focus {
    background: var(--md-sys-color-error);
    color: var(--md-sys-color-on-error);
}
.user-menu-dropdown .dropdown-item--danger:hover i,
.user-menu-dropdown .dropdown-item--danger:focus i { color: var(--md-sys-color-on-error); }
.user-menu-dropdown .dropdown-divider {
    margin: 6px 4px; border-top-color: var(--md-sys-color-outline-variant); opacity: 1;
}

.navbar-role-switch {
    display: inline-flex; align-items: center; gap: 8px;
    height: 40px; padding: 0 14px;
    border-radius: var(--md-sys-shape-corner-full);
    background: var(--md-sys-color-surface-container);
    border: 1px solid var(--md-sys-color-outline-variant);
    color: var(--md-sys-color-on-surface); text-decoration: none;
    font-size: 13px; font-weight: 500;
    transition: all 0.2s ease;
}
.navbar-role-switch:hover {
    background: var(--md-sys-color-surface-container-highest);
    box-shadow: var(--md-sys-elevation-1);
}
.navbar-role-switch i { color: var(--md-sys-color-primary); font-size: 16px; }
@media (max-width: 768px) { .navbar-role-label { display: none; } }

/* ---- Layout ------------------------------------------------------------- */
.app-container { display: flex; padding-top: 64px; min-height: 100vh; }

.sidebar {
    position: fixed; left: 0; top: 64px;
    width: 280px; height: calc(100vh - 64px);
    background: var(--md-sys-color-surface);
    border-right: 1px solid var(--md-sys-color-outline-variant);
    overflow-y: auto;
    z-index: 1040;
    transition: transform 0.3s ease;
}
.sidebar-menu { padding: 12px 0 80px; }
.sidebar-nav-list { list-style: none; margin: 0; padding: 0; }
.sidebar-nav-link {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 16px;
    color: var(--md-sys-color-on-surface);
    text-decoration: none;
    border-left: 3px solid transparent;
    transition: all 0.2s ease;
}
.sidebar-nav-link:hover { background: var(--md-sys-color-surface-container); }
.sidebar-nav-link.active {
    background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-primary);
    border-left-color: var(--md-sys-color-primary);
    font-weight: 500;
}
.sidebar-nav-link i {
    font-size: 17px; width: 28px; height: 28px;
    display: flex; align-items: center; justify-content: center;
}
.sidebar-nav-link span { font-size: 13px; font-weight: 500; }
.sidebar-section-label {
    padding: 16px 16px 4px;
    font-size: 10px; font-weight: 700; letter-spacing: 1.2px;
    text-transform: uppercase;
    color: var(--md-sys-color-on-surface-variant);
    opacity: 0.6; list-style: none;
}

.content-area {
    margin-left: 280px;
    flex: 1; padding: 24px;
    min-height: calc(100vh - 64px);
    background: var(--md-sys-color-background);
}
.content-header { margin-bottom: 24px; }
.content-header h1 { font-size: 1.6rem; font-weight: 600; margin: 0 0 6px; color: var(--md-sys-color-on-surface); }
.content-header p { font-size: 14px; color: var(--md-sys-color-on-surface-variant); margin: 0; }

/* ---- Cards & components ------------------------------------------------- */
.card {
    background: var(--md-sys-color-surface);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--md-sys-shape-corner-medium);
    box-shadow: var(--md-sys-elevation-1);
    padding: 24px;
    margin-bottom: 24px;
}
.card-title { font-size: 1.15rem; font-weight: 600; margin: 0 0 16px; color: var(--md-sys-color-on-surface); }
.card-text { font-size: 14px; color: var(--md-sys-color-on-surface-variant); }
/* Footer strip for panel action buttons: spans the card padding, sits flush at
   the bottom with a divider, buttons right-aligned (use .me-auto to push info left). */
.card-footer {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px;
    margin: 24px -24px -24px; padding: 16px 24px;
    border-top: 1px solid var(--md-sys-color-outline-variant);
}
.card-footer form { margin: 0; }

/* Flash modal messages keep their line breaks (multi-field error lists,
   storage-value dumps from the Exemplos page). */
.flash-message { white-space: pre-line; }

/* ---- Realtime field validation states (core/forms.js) -------------------- */
/* Tokenized so all themes + dark mode work; Bootstrap's hardcoded red/green
   and background icons are overridden. Applied on blur, cleared on input. */
.form-control.is-invalid, .form-select.is-invalid {
    border-color: var(--md-sys-color-error);
    background-image: none;
}
.form-control.is-valid, .form-select.is-valid {
    border-color: var(--md-sys-color-primary);
    background-image: none;
}
.invalid-feedback { color: var(--md-sys-color-error); }

/* CEP auto-fill (ViaCEP): spinner pinned inside the field, hint below. */
.br-cep-wrap { position: relative; }
.br-cep-spinner {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--md-sys-color-primary);
}

/* ---- Password fields: live policy checklist ------------------------------ */
/* Show/hide comes from the shared .password-field / .password-toggle primitive
   (data-reveal, above). The checklist below mirrors App\Security\PasswordPolicy. */
.pw-checklist {
    list-style: none;
    margin: 8px 0 0;
    padding: 0;
    display: grid;
    gap: 4px;
    font-size: .8125rem;
    color: var(--md-sys-color-on-surface-variant);
}
.pw-checklist .bi { margin-right: 6px; }
.pw-checklist li.ok { color: var(--md-sys-color-primary); }
.pw-checklist li.bad { color: var(--md-sys-color-error); }

/* ---- Record-as-panel list primitive (copy across CRUD modules) ---------- */
/* Lists are a stack of .card panels — one per record — never a <table>. All
   colors flow through --md-sys-* tokens, so light + dark are automatic. Reuses
   the .card / .card-footer contract above. Canonical usage: templates/users/. */
.section-title { font-size: 1.15rem; font-weight: 600; margin: 24px 0 16px; color: var(--md-sys-color-on-surface); }

.list-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 16px; }
.list-search { position: relative; flex: 1 1 260px; min-width: 220px; }
.list-search .bi {
    position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
    color: var(--md-sys-color-on-surface-variant); pointer-events: none;
}
.list-search .form-control { padding-left: 40px; border-radius: var(--md-sys-shape-corner-full); }
.list-org-filter { flex: 0 1 240px; min-width: 180px; border-radius: var(--md-sys-shape-corner-full); }
.list-count { margin: 0 0 12px; }

/* Password reveal toggle (data-reveal on the input; button injected by forms.js). */
.password-field { position: relative; }
.password-field .form-control { padding-right: 44px; }
.password-toggle {
    position: absolute; top: 0; right: 0; height: 100%; width: 44px;
    display: flex; align-items: center; justify-content: center;
    background: transparent; border: 0; padding: 0; cursor: pointer;
    color: var(--md-sys-color-on-surface-variant);
    border-radius: var(--md-sys-shape-corner-full);
}
.password-toggle:hover { color: var(--md-sys-color-on-surface); }
.password-toggle:focus-visible { outline: 2px solid var(--md-sys-color-primary); outline-offset: -2px; }

/* MD3 horizontal list item inside a .card */
.list-item { display: flex; align-items: center; gap: 16px; }
.list-avatar {
    flex: 0 0 auto; width: 48px; height: 48px;
    border-radius: var(--md-sys-shape-corner-full);
    background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-on-primary-container);
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; letter-spacing: .5px;
}
.list-avatar .bi { font-size: 1.25rem; }
.list-body { flex: 1 1 auto; min-width: 0; }
.list-title {
    font-weight: 600; color: var(--md-sys-color-on-surface); line-height: 1.3;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.list-subtitle {
    font-size: .875rem; color: var(--md-sys-color-on-surface-variant);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.list-meta {
    flex: 0 0 auto; display: flex; flex-wrap: wrap; align-items: center;
    justify-content: flex-end; gap: 6px; max-width: 340px;
}
.list-time { font-size: .8125rem; color: var(--md-sys-color-on-surface-variant); white-space: nowrap; }

/* Role chips + status badge (token-driven, AA in light & dark; no green in palette) */
.chip {
    font-size: .75rem; font-weight: 500; padding: 3px 10px;
    border-radius: var(--md-sys-shape-corner-full);
    background: var(--md-sys-color-surface-variant); color: var(--md-sys-color-on-surface-variant);
    border: 1px solid var(--md-sys-color-outline-variant); white-space: nowrap;
}
.chip-org { background: var(--md-sys-color-secondary-container); color: var(--md-sys-color-on-secondary-container); }
.status-badge {
    font-size: .75rem; font-weight: 600; padding: 3px 10px;
    border-radius: var(--md-sys-shape-corner-full); white-space: nowrap;
}
.status-badge.is-active { background: var(--md-sys-color-primary-container); color: var(--md-sys-color-on-primary-container); }
.status-badge.is-inactive {
    background: var(--md-sys-color-surface-variant); color: var(--md-sys-color-on-surface-variant);
    border: 1px solid var(--md-sys-color-outline-variant);
}

/* Tokenize Bootstrap's outline button so the footer action themes with the app. */
.btn-outline-primary {
    --bs-btn-color: var(--md-sys-color-primary);
    --bs-btn-border-color: var(--md-sys-color-outline);
    --bs-btn-hover-color: var(--md-sys-color-primary);
    --bs-btn-hover-bg: var(--md-sys-color-surface-variant);
    --bs-btn-hover-border-color: var(--md-sys-color-primary);
    --bs-btn-active-color: var(--md-sys-color-primary);
    --bs-btn-active-bg: var(--md-sys-color-surface-variant);
    --bs-btn-active-border-color: var(--md-sys-color-primary);
}

.list-empty { text-align: center; padding: 32px 24px; color: var(--md-sys-color-on-surface-variant); }
.list-empty .bi { font-size: 2rem; color: var(--md-sys-color-outline); display: block; margin-bottom: 8px; }

.list-pagination { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 16px; }
.list-pagination-info { font-size: 0.875rem; color: var(--md-sys-color-on-surface-variant); }

/* Narrow screens: drop the trailing meta below the text, aligned past the avatar. */
@media (max-width: 560px) {
    .list-item { flex-wrap: wrap; }
    .list-meta { width: 100%; justify-content: flex-start; padding-left: 64px; }
}

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 24px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 24px; margin-bottom: 24px; }

.stat-card {
    background: var(--md-sys-color-surface);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--md-sys-shape-corner-medium);
    box-shadow: var(--md-sys-elevation-1);
    padding: 20px; display: flex; align-items: center; gap: 16px;
}
.stat-icon {
    width: 56px; height: 56px;
    border-radius: var(--md-sys-shape-corner-medium);
    background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-primary);
    display: flex; align-items: center; justify-content: center; font-size: 24px;
}
.stat-content h4 { font-size: 1.5rem; font-weight: 600; margin: 0 0 4px; color: var(--md-sys-color-on-surface); }
.stat-content p { font-size: 13px; color: var(--md-sys-color-on-surface-variant); margin: 0; }

/* ---- Sidebar overlay (mobile) ------------------------------------------ */
.sidebar-overlay {
    display: none; position: fixed; inset: 64px 0 0 0;
    background: rgba(0, 0, 0, 0.5); z-index: 1035; opacity: 0;
    transition: opacity 0.3s ease;
}
.sidebar-overlay.active { display: block; opacity: 1; }

@media (max-width: 1024px) {
    .sidebar { width: 240px; }
    .content-area { margin-left: 240px; }
}
@media (max-width: 768px) {
    .mobile-menu-toggle { display: flex; }
    .sidebar { transform: translateX(-100%); width: 280px; }
    .sidebar.mobile-open { transform: translateX(0); box-shadow: var(--md-sys-elevation-3); }
    .content-area { margin-left: 0; padding: 16px; }
    .logo-name { display: none; }
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

/* ---- Auth pages -------------------------------------------------------- */
.auth-body {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: var(--md-sys-color-background); padding: 20px;
}
.auth-wrap { width: 100%; max-width: 400px; }
.auth-card {
    background: var(--md-sys-color-surface);
    border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--md-sys-shape-corner-large);
    box-shadow: var(--md-sys-elevation-2);
    padding: 32px;
}
.auth-brand { display: flex; align-items: center; gap: 12px; justify-content: center; margin-bottom: 24px; }
.auth-title { font-size: 1.4rem; font-weight: 600; margin: 0 0 4px; color: var(--md-sys-color-on-surface); }
.auth-subtitle { font-size: 14px; color: var(--md-sys-color-on-surface-variant); margin: 0 0 20px; }
.auth-links { margin-top: 16px; text-align: center; font-size: 14px; }

/* ---- Chat (Conversas / WhatsApp) --------------------------------------- */
.chat-layout { display: flex; gap: 16px; align-items: stretch; height: calc(100vh - 210px); min-height: 420px; }
.chat-sidebar { width: 320px; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; }
.chat-sidebar-toolbar { display: flex; flex-direction: column; gap: 8px; }
/* .list-search has flex: 1 1 260px for row toolbars; inside this COLUMN
   toolbar that basis would become height — pin it to content size. */
.chat-search { flex: 0 0 auto; width: 100%; min-width: 0; }
.chat-conversations { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding-right: 2px; }
.chat-conversation {
    display: flex; align-items: center; gap: 12px; padding: 10px 12px; cursor: pointer;
    background: var(--md-sys-color-surface); border: 1px solid var(--md-sys-color-outline-variant);
    border-radius: var(--md-sys-shape-corner-large); text-align: left; width: 100%;
}
.chat-conversation:hover { background: var(--md-sys-color-surface-container); }
.chat-conversation.is-active { background: var(--md-sys-color-secondary-container); border-color: transparent; }
.chat-conversation .list-title { font-size: 14px; }
.chat-conversation .list-subtitle {
    font-size: 12.5px; max-width: 180px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-unread {
    margin-left: auto; min-width: 22px; height: 22px; border-radius: 11px; padding: 0 6px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--md-sys-color-primary); color: #fff; font-size: 12px; font-weight: 600;
}
.chat-thread { flex: 1; display: flex; flex-direction: column; min-width: 0; padding: 0; overflow: hidden; }
.chat-thread-header {
    display: flex; align-items: center; gap: 12px; padding: 12px 16px;
    border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.chat-messages {
    flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px;
    background: var(--md-sys-color-surface-container);
}
.chat-empty { margin: auto; text-align: center; color: var(--md-sys-color-on-surface-variant); }
.chat-empty .bi { font-size: 2rem; color: var(--md-sys-color-outline); display: block; margin-bottom: 8px; }
.chat-bubble {
    max-width: min(75%, 560px); padding: 8px 12px; border-radius: 14px; font-size: 14px;
    white-space: pre-wrap; overflow-wrap: break-word;
}
.chat-bubble.is-in {
    align-self: flex-start; background: var(--md-sys-color-surface);
    border: 1px solid var(--md-sys-color-outline-variant); border-bottom-left-radius: 4px;
}
.chat-bubble.is-out {
    align-self: flex-end; background: var(--md-sys-color-primary-container);
    color: var(--md-sys-color-on-primary-container); border-bottom-right-radius: 4px;
}
.chat-bubble.is-failed { border: 1px solid var(--md-sys-color-error); }
.chat-bubble-sender { font-size: 12px; font-weight: 600; color: var(--md-sys-color-primary); margin-bottom: 2px; }
.chat-bubble-meta {
    display: flex; align-items: center; gap: 4px; justify-content: flex-end;
    font-size: 11px; color: var(--md-sys-color-on-surface-variant); margin-top: 2px;
}
.chat-bubble-meta .bi-check2-all.is-read { color: var(--md-sys-color-primary); }
.chat-bubble-meta .bi-exclamation-triangle-fill { color: var(--md-sys-color-error); }
.chat-bubble img.chat-image { max-width: 100%; max-height: 280px; border-radius: 8px; display: block; }
.chat-doc {
    display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 10px;
    background: var(--md-sys-color-surface); border: 1px solid var(--md-sys-color-outline-variant);
    color: inherit; text-decoration: none; font-size: 13.5px; max-width: 100%;
}
.chat-doc .bi { font-size: 1.3rem; }
.chat-composer { padding: 10px 12px; border-top: 1px solid var(--md-sys-color-outline-variant); }
.chat-composer-row { display: flex; align-items: flex-end; gap: 8px; }
.chat-composer textarea { resize: none; max-height: 120px; }
.chat-attach-btn, .chat-send-btn {
    width: 42px; height: 42px; flex-shrink: 0; display: inline-flex;
    align-items: center; justify-content: center; padding: 0;
}
.chat-attachment { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
@media (max-width: 820px) {
    .chat-layout { flex-direction: column; height: auto; }
    .chat-sidebar { width: 100%; }
    .chat-conversations { max-height: 260px; }
    .chat-thread { min-height: 420px; }
}
