/*
 * 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; }
/* Conta Walden4: AI credit meter (the org's balance as the site last said it) */
.credits-meter {
    display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px;
    border-radius: var(--md-sys-shape-corner-full); text-decoration: none;
    font-size: .8rem; font-weight: 600; letter-spacing: .01em;
    background: var(--md-sys-color-secondary-container); color: var(--md-sys-color-on-secondary-container);
}
.credits-meter:hover { filter: brightness(.96); }
.credits-meter.is-empty { background: color-mix(in srgb, var(--md-sys-color-error) 16%, var(--md-sys-color-surface)); color: var(--md-sys-color-error); }
.credits-meter.is-blocked { outline: 2px solid var(--md-sys-color-error); outline-offset: 1px; }
/* Minha assinatura */
.credits-balance { display: flex; align-items: baseline; flex-wrap: wrap; gap: 6px 12px; margin: 0 0 8px; }
.credits-balance b { font-size: 2.2rem; font-weight: 800; line-height: 1; }
.credits-balance b.is-debt { color: var(--md-sys-color-error); }
.credits-balance span { color: var(--md-sys-color-on-surface-variant); font-size: .9rem; }
.credits-bar { height: 8px; border-radius: 4px; background: var(--md-sys-color-surface-variant); overflow: hidden; }
.credits-bar > span { display: block; height: 100%; background: var(--md-sys-color-primary); }
.credits-bar > span.is-high { background: var(--md-sys-color-error); }
.credits-limit-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.credits-limit-form .form-control { width: 130px; }
.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); }

.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);
}
.navbar-icon-btn:hover, .mobile-menu-toggle:hover {
    background: var(--md-sys-color-surface-container-highest);
    box-shadow: var(--md-sys-elevation-1);
}
.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;
}

/* ---- 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; }
/* Icon accent slots (--app-nav-accent-1..6, defaults in tokens.css = currentColor;
   each theme sets its own six in themes.css). MenuRenderer emits .nav-accent-N from
   the route's 'accent' key. In the sidebar the slot always applies (base look =
   currentColor = unchanged); in the account dropdown it applies only under a theme,
   so the base look keeps its primary-tinted icons. Specificity (0,3,0)/(0,4,0) sits
   below the themed `.active .bi` overrides (t4) on purpose. */
.sidebar-nav-link .bi.nav-accent-1, html[data-app-theme] .dropdown-item .bi.nav-accent-1 { color: var(--app-nav-accent-1); }
.sidebar-nav-link .bi.nav-accent-2, html[data-app-theme] .dropdown-item .bi.nav-accent-2 { color: var(--app-nav-accent-2); }
.sidebar-nav-link .bi.nav-accent-3, html[data-app-theme] .dropdown-item .bi.nav-accent-3 { color: var(--app-nav-accent-3); }
.sidebar-nav-link .bi.nav-accent-4, html[data-app-theme] .dropdown-item .bi.nav-accent-4 { color: var(--app-nav-accent-4); }
.sidebar-nav-link .bi.nav-accent-5, html[data-app-theme] .dropdown-item .bi.nav-accent-5 { color: var(--app-nav-accent-5); }
.sidebar-nav-link .bi.nav-accent-6, html[data-app-theme] .dropdown-item .bi.nav-accent-6 { color: var(--app-nav-accent-6); }
.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;
}
/* "Voltar" link at the top of a drill-in submenu (e.g. Banco de Dados). */
.sidebar-nav-back { color: var(--md-sys-color-on-surface-variant); }
.sidebar-nav-back:hover { color: var(--md-sys-color-on-surface); }

/* ---- Drill-in sidebar panels (main menu sections) ----------------------- */
.sidebar-nav { position: relative; overflow-x: hidden; }
.sidebar-panel { display: none; }
.sidebar-panel.active { display: block; animation: sidebar-panel-in 0.2s ease; }
@keyframes sidebar-panel-in {
    from { opacity: 0; transform: translateX(12px); }
    to   { opacity: 1; transform: none; }
}
/* Section buttons + back button are <button>s styled like the nav links. */
button.sidebar-nav-link {
    width: 100%; font: inherit; text-align: left; cursor: pointer;
    background: none; border: none; border-left: 3px solid transparent;
}
.sidebar-nav-chevron {
    margin-left: auto; width: auto !important; height: auto !important;
    font-size: 12px !important; opacity: 0.5;
}

.content-area {
    margin-left: 280px;
    /* min-width: 0 — a flex item never shrinks below its content by default, so one
       wide child (the /tables grid) widened the whole page; now the child's own
       overflow-x wrapper scrolls instead of the page. */
    flex: 1; min-width: 0; 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; }
img.list-avatar-img { object-fit: cover; background: var(--md-sys-color-surface-container); border: 1px solid var(--md-sys-color-outline-variant); }

/* Organization form: current logo + "remove" switch above the file input. */
.org-logo-preview { display: flex; align-items: center; gap: 16px; margin-bottom: 8px; }
.org-logo-preview img {
    width: 64px; height: 64px; object-fit: contain; border-radius: var(--md-sys-shape-corner-medium);
    background: var(--md-sys-color-surface-container); border: 1px solid var(--md-sys-color-outline-variant);
}
.org-login-link { word-break: break-all; }
.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); }
/* A TEST organization (organizations.is_test) — the only kind "Entrar como" reaches. */
/* Same amber as the impersonation bar: "this is make-believe". */
.chip-test { background: #f5c04a; color: #3d2a00; border-color: #b98300; }
.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; }
}

/* ---- Card grid (.app-grid) — a list as a grid of HORIZONTAL cards -------- */
/* Each record is a card wider than tall: the icon on the left, name and subtitle
   beside it, the state chips just below and the actions in a strip on the right.
   The class goes on the list CONTAINER (the element with role="list"); the card
   markup and its JS twin (the live-search renderItem) do not change by a byte —
   everything here is CSS over .list-item / .card-footer. Needs core/app-grid.js
   for the tooltips and the click-the-card-to-open behaviour. A new list = copy the
   Users module and put class="app-grid" on the container.
   Only for lists whose footer holds buttons: a footer with a <select> or a text
   field (the e-mail / WhatsApp instance lists) stays a stack. */
.app-grid {
    display: grid; gap: 14px;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
    align-items: stretch;
}
/* The empty state and any notice take the whole row. */
.app-grid > :not([role="listitem"]) { grid-column: 1 / -1; }
.app-grid > .card[role="listitem"] {
    position: relative; margin: 0; padding: 16px 16px 0;
    display: flex; flex-direction: column; text-align: left; min-width: 0;
    border-radius: var(--md-sys-shape-corner-large);
    transition: box-shadow .15s ease, transform .15s ease, border-color .15s ease;
}
.app-grid > .card[role="listitem"]:hover,
.app-grid > .card[role="listitem"]:focus-within {
    box-shadow: var(--md-sys-elevation-2);
    border-color: var(--md-sys-color-outline);
}
@media (hover: hover) and (prefers-reduced-motion: no-preference) {
    .app-grid > .card[role="listitem"]:hover { transform: translateY(-1px); }
}
.app-grid > .card[role="listitem"].is-openable { cursor: pointer; }
/* Icon on the left spanning both rows; name on top, chips below. */
.app-grid .list-item {
    flex: 1 1 auto; min-width: 0;
    display: grid; grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas: "avatar body" "avatar meta";
    align-items: start; align-content: start; column-gap: 14px; row-gap: 8px;
}
/* ⚠️ Flat primary under the text, NOT a gradient toward white: lightening the
   primary behind --md-sys-color-on-primary drops the pair under 4.5:1 in the
   light themes. The top highlight is an inset shadow, which never moves the
   colour the contrast is measured on. */
.app-grid .list-avatar {
    grid-area: avatar;
    width: 52px; height: 52px; font-size: 1.15rem; letter-spacing: 0;
    border-radius: 28%;
    color: var(--md-sys-color-on-primary);
    background: var(--md-sys-color-primary);
    box-shadow: 0 2px 6px rgba(0, 0, 0, .16), inset 0 1px 0 rgba(255, 255, 255, .28);
}
.app-grid .list-avatar .bi { font-size: 1.45rem; }
/* An uploaded logo (organizations) keeps its own backdrop. */
.app-grid img.list-avatar-img { background: var(--md-sys-color-surface-container); }
.app-grid .list-body { grid-area: body; min-width: 0; }
.app-grid .list-title,
.app-grid .list-subtitle {
    white-space: normal; overflow: hidden; overflow-wrap: anywhere;
    display: -webkit-box; -webkit-box-orient: vertical;
}
.app-grid .list-title { -webkit-line-clamp: 2; line-clamp: 2; font-size: .975rem; }
.app-grid .list-subtitle { -webkit-line-clamp: 2; line-clamp: 2; font-size: .8125rem; margin-top: 2px; }
.app-grid .list-meta {
    grid-area: meta; min-width: 0; max-width: none; padding-left: 0;
    justify-content: flex-start; gap: 4px;
}
.app-grid .list-meta .chip,
.app-grid .list-meta .status-badge { font-size: .6875rem; padding: 2px 8px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
/* Actions: round icon-only buttons, right-aligned. The text stays in the DOM
   (font-size: 0) as the accessible name; core/app-grid.js adds the tooltip. */
.app-grid .card-footer {
    justify-content: flex-end; gap: 6px;
    margin: 12px -16px 0; padding: 8px 12px;
}
.app-grid .card-footer .btn {
    font-size: 0; width: 34px; height: 34px; padding: 0; flex: 0 0 auto;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 50%;
}
.app-grid .card-footer .btn .bi { font-size: 1rem; margin: 0; }
/* A button with no icon must not vanish: it goes back to a text pill. */
.app-grid .card-footer .btn:not(:has(.bi)) { font-size: .8125rem; width: auto; padding: 0 12px; border-radius: var(--md-sys-shape-corner-full); }
@media (max-width: 600px) {
    .app-grid { grid-template-columns: minmax(0, 1fr); gap: 10px; }
    .app-grid > .card[role="listitem"] { padding: 14px 14px 0; }
    .app-grid .list-avatar { width: 44px; height: 44px; font-size: 1rem; }
    .app-grid .list-avatar .bi { font-size: 1.25rem; }
    .app-grid .card-footer { margin: 10px -14px 0; }
}

.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; }
.auth-divider {
    display: flex; align-items: center; gap: 12px; margin: 16px 0;
    font-size: 13px; color: var(--md-sys-color-on-surface-variant);
}
.auth-divider::before, .auth-divider::after { content: ''; flex: 1; border-top: 1px solid var(--md-sys-color-outline-variant); }

/* Error pages (templates/layout/error.view.php) reuse the auth card. */
.error-card { text-align: center; }
.error-code {
    font-size: 3.5rem; font-weight: 700; line-height: 1; margin-bottom: 12px;
    color: var(--md-sys-color-primary); font-variant-numeric: tabular-nums;
}
.error-ref { font-size: .8125rem; color: var(--md-sys-color-on-surface-variant); }
.error-detail {
    margin: 20px 0 0; padding: 12px; max-height: 50vh; overflow: auto; text-align: left;
    font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere;
    background: var(--md-sys-color-surface-container); color: var(--md-sys-color-on-surface);
    border: 1px solid var(--md-sys-color-outline-variant); border-radius: var(--md-sys-shape-corner-small);
}
.auth-wrap:has(.error-detail) { max-width: 880px; }

/* Keyboard users: jump past the navbar + sidebar straight to the page. */
.skip-link {
    position: absolute; left: 12px; top: -48px; z-index: 1100;
    padding: 8px 16px; border-radius: var(--md-sys-shape-corner-small);
    background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary);
    text-decoration: none; font-weight: 500; transition: top .15s ease;
}
.skip-link:focus { top: 12px; color: var(--md-sys-color-on-primary); }
.content-area:focus { outline: none; }

/* Submit buttons locked by core/feedback.js while the form is on its way. */
.btn.is-submitting { pointer-events: none; }
.btn.is-submitting .spinner-border { width: 1em; height: 1em; border-width: .15em; margin-right: 6px; vertical-align: -.125em; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---- 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 .chat-seen { color: var(--md-sys-color-primary); font-weight: 600; }
.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-bubble .chat-video { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; }
.chat-bubble .chat-audio { max-width: 100%; min-width: 220px; 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, .chat-mic-btn, .chat-rec-btn {
    width: 42px; height: 42px; flex-shrink: 0; display: inline-flex;
    align-items: center; justify-content: center; padding: 0;
}
.chat-recording { display: flex; align-items: center; gap: 10px; padding: 4px 2px; }
.chat-rec-time { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--md-sys-color-on-surface); }
.chat-rec-dot {
    width: 12px; height: 12px; border-radius: 50%; background: var(--md-sys-color-error);
    animation: chat-rec-pulse 1s ease-in-out infinite;
}
@keyframes chat-rec-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }
.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; }
}

/* ── Tabelas (dev DB row browser: templates/tables/) ────────────────────────
   Toolbar (table picker + column filter) over a real <table> data grid — a
   justified <table> case (raw DB rows). All colors flow through --md-sys-*
   tokens, so light + dark are automatic; the grid scrolls inside its own
   overflow-x wrapper so the page body never scrolls sideways. */
.tables-toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 20px 24px; margin-bottom: 16px; }
.tables-toolbar .form-label { font-size: .8125rem; font-weight: 500; margin-bottom: 4px; color: var(--md-sys-color-on-surface-variant); }

.tables-picker { flex: 0 0 auto; }
.tables-picker-row { display: flex; gap: 8px; }
.tables-picker .form-select { min-width: 220px; border-radius: var(--md-sys-shape-corner-full); }

.tables-filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; flex: 1 1 520px; }
.tables-filters .list-search { flex: 1 1 200px; min-width: 180px; margin: 0; }
.tables-filter-search { flex: 1 1 220px; min-width: 180px; }
.tables-filter-col { flex: 0 1 200px; min-width: 150px; }
.tables-filter-op { flex: 0 1 170px; min-width: 140px; }
.tables-filter-val { flex: 1 1 180px; min-width: 150px; }
.tables-filters .form-select { border-radius: var(--md-sys-shape-corner-full); }
.tables-filters .form-control { border-radius: var(--md-sys-shape-corner-full); }
.tables-filter-actions { display: flex; align-items: center; gap: 6px; }

/* Data grid — a real <table> in a horizontal-scroll wrapper. */
.data-grid-card { padding: 0; overflow: hidden; }
.data-grid-wrap { overflow-x: auto; width: 100%; }
.data-grid {
    --bs-table-bg: transparent;
    --bs-table-color: var(--md-sys-color-on-surface);
    --bs-table-hover-bg: var(--md-sys-color-surface-variant);
    --bs-table-hover-color: var(--md-sys-color-on-surface);
    --bs-table-border-color: var(--md-sys-color-outline-variant);
    color: var(--md-sys-color-on-surface); font-size: 13px; margin: 0;
    font-variant-numeric: tabular-nums;
}
.data-grid thead th {
    background: var(--md-sys-color-surface-container-highest);
    border-bottom: 2px solid var(--md-sys-color-outline-variant);
    vertical-align: top; white-space: nowrap; padding: 8px 12px;
}
.data-grid tbody td {
    padding: 7px 12px; vertical-align: top;
    max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.data-grid thead th.is-sorted { background: var(--md-sys-color-secondary-container); }
.data-grid-sort {
    display: inline-flex; align-items: center; gap: 6px;
    color: var(--md-sys-color-on-surface); text-decoration: none; font-weight: 600;
}
.data-grid-sort:hover { color: var(--md-sys-color-primary); }
.data-grid-caret { font-size: .7rem; color: var(--md-sys-color-on-surface-variant); }
.data-grid-pk { font-size: .75rem; color: var(--md-sys-color-primary); }
.data-grid-type {
    display: block; font-weight: 400; font-size: .6875rem; margin-top: 2px;
    color: var(--md-sys-color-on-surface-variant); text-transform: none;
}
.cell-null { color: var(--md-sys-color-outline); font-style: italic; }
.cell-empty { color: var(--md-sys-color-outline); }
.cell-masked { color: var(--md-sys-color-on-surface-variant); white-space: nowrap; }
.cell-masked .bi { font-size: .75rem; }

@media (max-width: 720px) {
    .tables-picker, .tables-picker .form-select { width: 100%; }
    .tables-filters { flex: 1 1 100%; }
}

/* "Entrar como…" bar (templates/partials/impersonation.view.php): fixed at the foot
   of every page while a SuperAdmin sees the app as someone else. Fixed amber on
   purpose — the same warning in every theme, light or dark (11.6:1 text contrast).
   Below Bootstrap's modal (1055) so dialogs still cover it. */
.impersonation-bar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1045;
    display: flex; align-items: center; flex-wrap: wrap; gap: 8px 16px;
    padding: 10px 20px calc(10px + env(safe-area-inset-bottom, 0px));
    background: #f5c04a; color: #3d2a00; border-top: 2px solid #b98300;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, .18); font-size: .9rem; line-height: 1.35;
}
.impersonation-bar > .bi { font-size: 1.25rem; }
.impersonation-who { flex: 1 1 220px; margin: 0; }
.impersonation-who small { display: block; font-size: .78rem; opacity: .9; }
.impersonation-back,
.impersonation-back:hover,
.impersonation-back:focus-visible { min-height: 40px; padding: 0 18px; background: #3d2a00; color: #fff; border-color: #3d2a00; font-weight: 600; }
.impersonation-back:hover { background: #241800; }
.impersonation-back:focus-visible { outline: 3px solid #241800; outline-offset: 2px; }
body:has(.impersonation-bar) .content-area,
body:has(.impersonation-bar) .sidebar,
body:has(.impersonation-bar) .auth-wrap { padding-bottom: 84px; }
@media (max-width: 640px) {
    .impersonation-bar { padding-left: 14px; padding-right: 14px; }
    .impersonation-back { min-height: 44px; }
    body:has(.impersonation-bar) .content-area,
    body:has(.impersonation-bar) .sidebar,
    body:has(.impersonation-bar) .auth-wrap { padding-bottom: 128px; }
}

/* ---- Academia: follow-up records (templates/progress/course.view.php) ----
   One card per group (a test, a unit of lessons) that opens into its rows:
   the name on the left, the facts as chips, the date at the end. Token-driven,
   so every theme (light and dark) follows. */
.progress-thin { height: 6px; background: var(--md-sys-color-surface-container-highest, var(--md-sys-color-surface-variant)); }
.progress-thin .progress-bar { background: var(--md-sys-color-primary); }
.record-group > summary { cursor: pointer; list-style: none; }
.record-group > summary::-webkit-details-marker { display: none; }
.record-group > summary::after { content: "\F282"; font-family: "bootstrap-icons"; margin-left: 8px; color: var(--md-sys-color-on-surface-variant); transition: transform .15s ease; }
.record-group[open] > summary::after { transform: rotate(180deg); }
.record-group > summary:focus-visible { outline: 2px solid var(--md-sys-color-primary); outline-offset: -2px; }
.record-rows { list-style: none; margin: 0; padding: 0 16px 8px; }
.record-rows li {
    display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 10px 0;
    border-top: 1px solid var(--md-sys-color-outline-variant); font-size: .875rem;
}
.record-name { flex: 1 1 280px; min-width: 0; color: var(--md-sys-color-on-surface); }
.record-when { margin-left: auto; font-size: .75rem; color: var(--md-sys-color-on-surface-variant); white-space: nowrap; }
.app-grid .list-avatar { font-size: .8125rem; }
