:root {
    /* Base Variables (Logic moved to theme selectors for clarity) */
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --secondary: #10b981;
    --accent-gradient: linear-gradient(135deg, #4f46e5 0%, #8b5cf6 100%);
}

/* Dark Mode (Default or Explicit) */
:root, [data-theme="dark"] {
    --bg-body: #0f172a;
    --bg-sidebar: #1e293b;
    --bg-card: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border: #334155;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --hover-bg: rgba(79, 70, 229, 0.1);
    --card-border-hover: #6366f1;
}

[data-theme="light"] {
    --bg-body: #f1f5f9;
    --bg-sidebar: #ffffff;
    --bg-card: #ffffff;
    --text-main: #0f172a;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --hover-bg: #f8fafc;
    --card-border-hover: #cbd5e1;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Outfit', sans-serif;
}

body {
    background-color: var(--bg-body);
    color: var(--text-main);
    display: flex;
    min-height: 100vh;
    overflow-x: hidden;
    transition: background-color 0.3s, color 0.3s;
}

/* Sidebar */
.sidebar {
    width: 260px;
    background-color: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    position: fixed;
    height: 100vh;
    z-index: 10;
    transition: background-color 0.3s, border-color 0.3s;
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-main);
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.logo i {
    color: #8b5cf6;
    font-size: 1.8rem;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.85rem 1rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 0.75rem;
    transition: all 0.3s ease;
    margin-bottom: 0;
    font-weight: 500;
}

.nav-item:hover {
    background: var(--hover-bg);
    color: var(--primary);
}

.nav-item.active {
    background: var(--accent-gradient);
    color: white;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.3);
}

.sidebar-footer {
    margin-top: auto;
    border-top: 1px solid var(--border);
    padding-top: 1.5rem;
}

.user-mini {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--accent-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1.1rem;
    color: white;
}

/* Main Content */
.main-content {
    margin-left: 260px;
    flex: 1;
    padding: 2rem;
}

.main-content.no-sidebar {
    margin-left: 0 !important;
    padding: 0 !important;
    width: 100% !important;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2.5rem;
}

.welcome-text h1 {
    font-size: 1.8rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.welcome-text p {
    color: var(--text-muted);
}

.header-actions {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.btn {
    padding: 0.75rem 1.5rem;
    border-radius: 0.75rem;
    border: none;
    font-weight: 500;
    cursor: pointer;
    transition: transform 0.2s, background-color 0.3s, color 0.3s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn:hover {
    transform: translateY(-2px);
}

.btn-primary {
    background: var(--accent-gradient);
    color: white;
    box-shadow: 0 4px 15px rgba(79, 70, 229, 0.4);
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-main);
}

.btn-outline:hover {
    background: var(--hover-bg);
}

/* Stats Grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2.5rem;
}

.stat-card {
    background: var(--bg-card);
    padding: 1.5rem;
    border-radius: 1rem;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: border-color 0.3s, background-color 0.3s;
    box-shadow: var(--shadow-sm);
}

.stat-card:hover {
    border-color: var(--card-border-hover);
    box-shadow: var(--shadow-md);
}

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
}

.stat-label {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Dashboard Grid */
.dashboard-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 1.5rem;
}

.card {
    background: var(--bg-card);
    border-radius: 1rem;
    border: 1px solid var(--border);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: background-color 0.3s, border-color 0.3s;
}

.card-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-title {
    font-size: 1.1rem;
    font-weight: 600;
}

.card-body {
    padding: 1.5rem;
}

/* Table */
.table-container {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    text-align: left;
    padding: 1rem;
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border);
}

td {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

tr:last-child td {
    border-bottom: none;
}

.patient-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.patient-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 2rem;
    font-size: 0.75rem;
    font-weight: 500;
}

.status-complete {
    background: rgba(16, 185, 129, 0.15);
    color: #10b981;
}

.status-pending {
    background: rgba(245, 158, 11, 0.15);
    color: #f59e0b;
}

/* Promo Card */
.promo-card {
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border: 1px solid #6366f1;
    position: relative;
    overflow: hidden;
}

[data-theme="light"] .promo-card {
    background: linear-gradient(135deg, #ffffff 0%, #f1f5f9 100%);
}

.promo-content {
    position: relative;
    z-index: 1;
}

.promo-badge {
    background: #f43f5e;
    color: white;
    padding: 0.25rem 0.75rem;
    border-radius: 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    display: inline-block;
    margin-bottom: 1rem;
}

.glow {
    position: absolute;
    width: 150px;
    height: 150px;
    background: #6366f1;
    filter: blur(80px);
    opacity: 0.2;
    border-radius: 50%;
    top: -20px;
    right: -20px;
}

/* Theme Toggle */
.theme-toggle {
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    transition: all 0.3s;
    border: 1px solid var(--border);
    background: transparent;
}

.theme-toggle:hover {
    background: var(--hover-bg);
    color: var(--primary);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-in {
    animation: fadeIn 0.5s ease-out forwards;
}

.delay-1 {
    animation-delay: 0.1s;
}

.delay-2 {
    animation-delay: 0.2s;
}

.delay-3 {
    animation-delay: 0.3s;
}

/* ============================================================================
   Dark-mode hardening
   ----------------------------------------------------------------------------
   Muitas telas são construídas com Bootstrap e utilitários de cor "fixos"
   (bg-white, table-light, text-dark) ou hex claros. Em tema escuro elas ficavam
   claras sobre o app escuro (telas "misturadas") ou com texto ilegível.

   Pré-requisito: data-bs-theme é sincronizado com data-theme no base.html, o
   que já escurece os componentes nativos do Bootstrap (card, table, modal,
   dropdown, form-control, borda padrão, .text-muted, etc.).

   Este bloco cobre o que o data-bs-theme NÃO trata: alinhar a paleta do
   Bootstrap à do portal e neutralizar utilitários/hex de cor clara fixos.
   Os seletores [data-theme="dark"] .classe têm especificidade maior que regras
   de classe simples, então vencem até <style> inline das próprias páginas.
   ========================================================================== */

/* Alinha as variáveis do Bootstrap às superfícies/cores do portal */
[data-theme="dark"] {
    --bs-body-bg: var(--bg-body);
    --bs-body-color: var(--text-main);
    --bs-emphasis-color: var(--text-main);
    --bs-secondary-color: var(--text-muted);
    --bs-border-color: var(--border);
    --bs-card-bg: var(--bg-card);
    --bs-card-cap-bg: var(--bg-card);
    --bs-tertiary-bg: #0f172a;
    --bs-secondary-bg: #0f172a;
}

/* Fundos claros fixos (classe) → superfície escura do card */
[data-theme="dark"] .bg-white,
[data-theme="dark"] .bg-light {
    background-color: var(--bg-card) !important;
    color: var(--text-main);
}

/* Cabeçalho/linhas de tabela "light" */
[data-theme="dark"] .table-light,
[data-theme="dark"] .table-light > th,
[data-theme="dark"] .table-light > td,
[data-theme="dark"] thead.table-light th,
[data-theme="dark"] .table-light tr {
    --bs-table-bg: #0f172a;
    --bs-table-color: var(--text-muted);
    background-color: #0f172a !important;
    color: var(--text-muted) !important;
    border-color: var(--border) !important;
}

/* Texto preto/escuro fixo → legível no escuro */
[data-theme="dark"] .text-dark,
[data-theme="dark"] .text-black {
    color: var(--text-main) !important;
}

/* .text-muted do Bootstrap continua discreto, porém legível no escuro */
[data-theme="dark"] .text-muted {
    color: var(--text-muted) !important;
}

/* Bordas claras padrão do Bootstrap */
[data-theme="dark"] .border,
[data-theme="dark"] .border-top,
[data-theme="dark"] .border-bottom,
[data-theme="dark"] .border-start,
[data-theme="dark"] .border-end {
    border-color: var(--border) !important;
}

/* Tabelas simples do portal (sem .table): garante texto legível em células
   que não definem cor própria (ex.: datas no dashboard) */
[data-theme="dark"] td {
    color: var(--text-main);
}

/* Garante que o conteúdo principal herde a cor clara do tema, mesmo se algum
   reset do Bootstrap tentar impor a cor de corpo clara */
[data-theme="dark"] body,
[data-theme="dark"] .main-content {
    color: var(--text-main);
}

/* ----------------------------------------------------------------------------
   Painéis/caixas reutilizáveis adaptáveis ao tema
   ----------------------------------------------------------------------------
   Para caixas dentro de modais/telas interativas que antes usavam fundo claro
   fixo via style inline (não sobrescritível por CSS). Use estas classes no
   lugar do hex inline. Claro por padrão; escuro via [data-theme="dark"].
   NÃO usar em telas de relatório/impressão (devem permanecer claras).
   ========================================================================== */
.tw-soft   { background: #f8fafc; border: 1px solid #e2e8f0; }
.tw-info   { background: #eef6ff; border: 1px solid #bfdbfe; }
.tw-warn   { background: #fffbeb; border: 1px solid #f59e0b; }
.tw-danger { background: #fef2f2; border: 1px solid #fca5a5; color: #991b1b; }
.tw-ec     { background: #eef6ff; border: 1px solid #2980b9; }
.tw-eo     { background: #fff8ee; border: 1px solid #e67e22; }
.tw-soft-bg { background: #f8fafc; }
.tw-muted-text { color: #555; }

[data-theme="dark"] .tw-soft   { background: #0f172a; border-color: var(--border); color: var(--text-main); }
[data-theme="dark"] .tw-info   { background: #0e2233; border-color: #1e3a5f; }
[data-theme="dark"] .tw-warn   { background: #2a2206; border-color: #a16207; }
[data-theme="dark"] .tw-danger { background: #2a0e0e; border-color: #7f1d1d; color: #fca5a5; }
[data-theme="dark"] .tw-ec     { background: #0e2535; border-color: #2563eb; }
[data-theme="dark"] .tw-eo     { background: #2a1d0a; border-color: #d97706; }
[data-theme="dark"] .tw-soft-bg { background: #0f172a; }
[data-theme="dark"] .tw-muted-text { color: #cbd5e1; }