/* Camada comum de cores. Componentes com cores de status mantêm seu significado. */
:root {
    color-scheme: light;
    --theme-page: #f3f5f8;
    --theme-surface: #ffffff;
    --theme-surface-muted: #f8fafc;
    --theme-text: #263238;
    --theme-muted: #64748b;
    --theme-border: #dce2e8;
    --theme-shadow: 0 4px 16px rgba(15, 23, 42, .08);
    --theme-input: #ffffff;
    --theme-table-head: #f1f5f9;
    --theme-link: #4659a8;
    --theme-focus: rgba(70, 89, 168, .25);

}

:root[data-bs-theme="dark"] {
    color-scheme: dark;
    --brand: #c98bd0;
    --brand-hover: #e0b5e5;
    --brand-soft: #38263d;
    --brand-green: #85c970;
    --brand-green-light: #9bd782;
    --brand-green-strong: #85c970;
    --theme-page: #111827;
    --theme-surface: #1f2937;
    --theme-surface-muted: #273449;
    --theme-text: #e5e7eb;
    --theme-muted: #a7b2c2;
    --theme-border: #3b4758;
    --theme-shadow: 0 4px 18px rgba(0, 0, 0, .24);
    --theme-input: #172131;
    --theme-table-head: #273449;
    --theme-link: #c6a0e8;
    --theme-focus: rgba(201, 139, 208, .4);
    --bg-color: #111827;
    --panel-color: #1f2937;
    --surface-green: #1d3326;
    --surface-purple: #38263d;
    --border-color: #3b4758;
    --text-primary: #e5e7eb;
    --text-secondary: #c0cad7;
    --text-tertiary: #a7b2c2;
    --header-grad-from: #38263d;
    --header-grad-to: #51355a;
    --separator: rgba(203, 213, 225, .18);
    --fill-hover: #273449;
    --fill-pressed: #344256;
    --fill-segment: #273449;
    --focus-ring: 0 0 0 3px rgba(201, 139, 208, .4);
    --header-action-bg: #51355a;
    --header-action-hover: #604066;
    --header-primary-hover: #e0b5e5;
    --bs-body-bg: var(--theme-page);
    --bs-body-color: var(--theme-text);
    --bs-secondary-color: var(--theme-muted);
    --bs-tertiary-color: var(--theme-muted);
    --bs-secondary-bg: var(--theme-surface-muted);
    --bs-tertiary-bg: var(--theme-surface-muted);
    --bs-border-color: var(--theme-border);
    --bs-border-color-translucent: rgba(203, 213, 225, .18);
    --bs-link-color: var(--theme-link);
    --bs-link-hover-color: #e0b5e5;
    --bs-emphasis-color: var(--theme-text);
}

/* Alguns portais BI redeclaram tokens no body para adequar a identidade local. */
html[data-bs-theme="dark"] :is(body.portal-bi, body:has(.portal-bi-nav)) {
    --brand: #c98bd0;
    --brand-hover: #e0b5e5;
    --brand-soft: #38263d;
    --bg-color: #111827;
    --panel-color: #1f2937;
    --text-primary: #e5e7eb;
    --text-secondary: #c0cad7;
    --border-color: #3b4758;
    --separator: rgba(203, 213, 225, .18);
    --fill-hover: #273449;
    --fill-pressed: #344256;
    --fill-segment: #273449;
    --focus-ring: 0 0 0 3px rgba(201, 139, 208, .4);
    --header-action-bg: #51355a;
    --header-action-hover: #604066;
    --header-primary-hover: #e0b5e5;
}

html[data-bs-theme="dark"] body {
    background-color: var(--theme-page) !important;
    color: var(--theme-text);
}

html[data-bs-theme="dark"] :is(.card, .modal-content, .dropdown-menu, .list-group-item, .offcanvas, .popover, .toast) {
    background-color: var(--theme-surface);
    color: var(--theme-text);
    border-color: var(--theme-border);
}

html[data-bs-theme="dark"] :is(.card-header, .card-footer, .modal-header, .modal-footer, .dropdown-item:hover, .list-group-item-action:hover) {
    background-color: var(--theme-surface-muted);
    color: var(--theme-text);
    border-color: var(--theme-border);
}

html[data-bs-theme="dark"] :is(.navbar, .navbar-custom, .navbar-estoque, .navbar-qa, .navbar-forms, .inno-navbar) {
    background-color: var(--theme-surface) !important;
    color: var(--theme-text);
    border-color: var(--theme-border);
}

html[data-bs-theme="dark"] :is(.navbar, .navbar-custom, .navbar-estoque, .navbar-qa, .navbar-forms) :is(.nav-link, .navbar-brand, .dropdown-toggle) {
    color: var(--theme-text);
}

html[data-bs-theme="dark"] :is(.form-control, .form-select, .input-group-text) {
    background-color: var(--theme-input);
    color: var(--theme-text);
    border-color: var(--theme-border);
}

html[data-bs-theme="dark"] :is(.form-control, .form-select)::placeholder { color: #94a3b8; }
html[data-bs-theme="dark"] :is(input, textarea)::placeholder { color: #94a3b8; }
html[data-bs-theme="dark"] .table {
    --bs-table-color: var(--theme-text);
    --bs-table-bg: transparent;
    --bs-table-border-color: var(--theme-border);
    --bs-table-striped-color: var(--theme-text);
    --bs-table-striped-bg: rgba(255, 255, 255, .035);
    --bs-table-hover-color: var(--theme-text);
    --bs-table-hover-bg: rgba(255, 255, 255, .06);
}
html[data-bs-theme="dark"] .table thead th,
html[data-bs-theme="dark"] .table .table-light > *,
html[data-bs-theme="dark"] .table-light {
    --bs-table-color: var(--theme-text);
    --bs-table-bg: var(--theme-table-head);
    --bs-table-border-color: var(--theme-border);
}
html[data-bs-theme="dark"] .table-dark {
    --bs-table-color: #f8fafc;
    --bs-table-bg: #111827;
    --bs-table-border-color: var(--theme-border);
}
/* Explicit surface utilities only; status badges and buttons keep their colors. */
html[data-bs-theme="dark"] :is(.card, .modal-content, .offcanvas, .dropdown-menu) :is(.bg-white, .bg-light):not(.badge):not(.btn):not(.alert) {
    background-color: var(--theme-surface-muted) !important;
    color: var(--theme-text);
}
html[data-bs-theme="dark"] :is(.card, .modal-content, .offcanvas, .dropdown-menu) :is(.text-dark, .text-body):not(.badge):not(.btn-warning):not(.btn-info) {
    color: var(--theme-text) !important;
}
/* Rede de segurança: utilitários claros fora de cards/modais (abas, barras, painéis soltos). */
html[data-bs-theme="dark"] :is(.bg-white, .bg-light, .bg-body-tertiary):not(.badge):not(.btn):not(.alert):not(.theme-print-fixed) {
    background-color: var(--theme-surface) !important;
    color: var(--theme-text);
}
html[data-bs-theme="dark"] :is(.bg-white, .bg-light).text-dark:not(.badge):not(.btn) { color: var(--theme-text) !important; }
html[data-bs-theme="dark"] .nav-tabs .nav-link.active,
html[data-bs-theme="dark"] .nav-tabs .nav-item.show .nav-link {
    background-color: var(--theme-surface);
    color: var(--theme-text);
    border-color: var(--theme-border) var(--theme-border) var(--theme-surface);
}
/* Rede de segurança: superfícies claras em style="" inline. */
html[data-bs-theme="dark"] :is(
    [style*="background:#fff;" i], [style*="background: #fff;" i], [style$="background:#fff" i], [style$="background: #fff" i],
    [style*="background:#ffffff" i], [style*="background: #ffffff" i],
    [style*="background-color:#fff;" i], [style*="background-color: #fff;" i], [style*="background-color:#ffffff" i], [style*="background-color: #ffffff" i],
    [style*="background:white" i], [style*="background: white" i], [style*="background-color:white" i], [style*="background-color: white" i]
):not(.badge):not(.btn):not(.theme-print-fixed) {
    background-color: var(--theme-surface) !important;
    color: var(--theme-text);
}
html[data-bs-theme="dark"] :is(
    [style*="background:#f8fafc" i], [style*="background: #f8fafc" i], [style*="background-color:#f8fafc" i], [style*="background-color: #f8fafc" i],
    [style*="background:#f8f9fa" i], [style*="background: #f8f9fa" i], [style*="background-color:#f8f9fa" i], [style*="background-color: #f8f9fa" i],
    [style*="background:#f1f5f9" i], [style*="background: #f1f5f9" i], [style*="background-color:#f1f5f9" i], [style*="background-color: #f1f5f9" i],
    [style*="background:#f9fafb" i], [style*="background: #f9fafb" i], [style*="background:#fafafa" i], [style*="background:#f5f5f5" i],
    [style*="background-color: #e9ecef" i], [style*="background-color:#e9ecef" i], [style*="background:#e9ecef" i]
):not(.badge):not(.btn):not(.theme-print-fixed) {
    background-color: var(--theme-surface-muted) !important;
    color: var(--theme-text);
}
html[data-bs-theme="dark"] :is(
    [style*="color:#1e293b" i], [style*="color: #1e293b" i], [style*="color:#333" i], [style*="color: #333" i],
    [style*="color:#212529" i], [style*="color:#374151" i], [style*="color:#334155" i]
):not([style*="background" i]):not(.badge):not(.btn):not(.theme-print-fixed) {
    color: var(--theme-text) !important;
}
html[data-bs-theme="dark"] .text-muted { color: var(--theme-muted) !important; }
html[data-bs-theme="dark"] :is(hr, .border, .border-top, .border-bottom) { border-color: var(--theme-border) !important; }
/* Links de texto: só os sem classe. Links com classe costumam ser botões/abas estilizados
   (.btn-bi-primary, .btn-quick, .filter-tab…) e herdavam roxo claro sobre fundo roxo claro. */
html[data-bs-theme="dark"] { --bs-link-color-rgb: 198, 160, 232; --bs-link-hover-color-rgb: 224, 181, 229; }
html[data-bs-theme="dark"] a:not([class]) { color: var(--theme-link); }

/* Cores fixas do Bootstrap que não se adaptam ao escuro (texto escuro ou saturado sobre fundo escuro). */
html[data-bs-theme="dark"] .text-dark:not(.badge):not(.btn):not([class*="bg-"]) { color: var(--theme-text) !important; }
html[data-bs-theme="dark"] .text-success:not(.badge):not(.btn) { color: #4ade80 !important; }
html[data-bs-theme="dark"] .text-danger:not(.badge):not(.btn) { color: #f87171 !important; }
html[data-bs-theme="dark"] .text-primary:not(.badge):not(.btn) { color: #93c5fd !important; }
html[data-bs-theme="dark"] .text-secondary:not(.badge):not(.btn) { color: var(--theme-muted) !important; }
html[data-bs-theme="dark"] .btn-outline-secondary { --bs-btn-color: #cbd5e1; --bs-btn-border-color: #64748b; --bs-btn-hover-bg: #475569; --bs-btn-hover-border-color: #475569; --bs-btn-hover-color: #fff; }
html[data-bs-theme="dark"] .btn-outline-danger { --bs-btn-color: #f87171; --bs-btn-border-color: #f87171; }
html[data-bs-theme="dark"] .btn-outline-dark { --bs-btn-color: var(--theme-text); --bs-btn-border-color: var(--theme-border); --bs-btn-hover-bg: var(--fill-hover); --bs-btn-hover-color: var(--theme-text); --bs-btn-hover-border-color: var(--theme-border); }
html[data-bs-theme="dark"] .panel-nota { background: rgba(245, 158, 11, .14); color: #fcd34d; }
html[data-bs-theme="dark"] .panel-nota :is(strong, b) { color: #fde68a; }

/* Botões preenchidos com a cor da marca: no escuro --brand é roxo claro, então o rótulo
   vira escuro (mesmo tratamento de .btn-bi-primary). */
html[data-bs-theme="dark"] :is(.btn-brand, .crm-btn-cta, .fc .fc-button-primary, .btn-quick) { color: #251b28 !important; }

/* Controles segmentados: o "thumb" ativo era branco fixo (apple.css). */
html[data-bs-theme="dark"] :is(.modo-switch .modo-btn.active, .visao-cols button.active) {
    background: var(--theme-surface); color: var(--theme-text);
    box-shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 0 0 .5px rgba(255, 255, 255, .08);
}
html[data-bs-theme="dark"] :is(.form-control, .form-select):focus {
    border-color: #9b70a5;
    box-shadow: 0 0 0 .25rem var(--theme-focus);
}

.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .45rem;
    margin-inline-start: .5rem;
    border: 1px solid var(--theme-border);
    color: var(--theme-text);
    background: var(--theme-surface);
    white-space: nowrap;
}
.theme-toggle:hover, .theme-toggle:focus-visible { color: var(--theme-text); background: var(--theme-surface-muted); }
.theme-toggle-icon { display: block; width: 1.125rem; height: 1.125rem; flex: 0 0 1.125rem; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.theme-toggle-float { position: fixed; inset: 1rem 1rem auto auto; z-index: 1080; box-shadow: var(--theme-shadow); }
@media (max-width: 575.98px) { .theme-toggle-label { display: none; } }
/* Na navbar o botão é só ícone (o rótulo continua disponível para leitores de tela via aria-label). */
.theme-toggle-item { display: flex; align-items: center; }
.theme-toggle-nav { width: 36px; height: 36px; padding: 0; margin-inline-start: 0; border-radius: 999px; }
.theme-toggle-nav .theme-toggle-label {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
@media (max-width: 991.98px) { .theme-toggle-item { padding: .25rem 0; } }

html[data-bs-theme="dark"] .btn-bi-primary,
html[data-bs-theme="dark"] .btn-bi-primary:hover:not(:disabled) {
    color: #251b28;
}

@media print {
    html[data-bs-theme="dark"] {
        color-scheme: light !important;
        --bg-color: #fff;
        --panel-color: #fff;
        --text-primary: #222;
        --text-secondary: #444;
        --border-color: #bbb;
        --separator: #bbb;
        --theme-page: #fff;
        --theme-surface: #fff;
        --theme-text: #222;
        --theme-border: #bbb;
    }
    html[data-bs-theme="dark"] body,
    html[data-bs-theme="dark"] :is(.card, .modal-content, .table-responsive) {
        background: #fff !important;
        color: #222 !important;
    }
    .theme-toggle { display: none !important; }
}
