:root {
    color-scheme: light;
}

.analysis-kpi-summary {
    background: linear-gradient(135deg, #f0f7ff, #e6f2ff);
}

.theme-selector {
    display: inline-flex;
    flex-shrink: 0;
    padding: 2px;
    border: 1px solid var(--bs-border-color);
    border-radius: 999px;
    background: var(--bs-body-bg);
}

.theme-option {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--bs-body-color);
}

.theme-option:hover {
    background: var(--theme-raised, #e9ecef);
}

.theme-option[aria-pressed="true"] {
    background: #006b3b;
    color: #fff;
}

.theme-option:focus-visible {
    outline: 2px solid var(--theme-accent, #006b3b);
    outline-offset: 2px;
}

@media (pointer: coarse) {
    .theme-option {
        width: 44px;
        height: 44px;
    }
}

.theme-selector-auth {
    padding: .75rem 1rem;
    position: absolute;
    top: 0;
    right: 0;
    z-index: 2;
}

.theme-selector-auth + main,
.theme-selector-auth + #layoutAuthentication {
    padding-top: 4rem;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --theme-surface: #1e2936;
    --theme-raised: #293747;
    --theme-muted: #b3bfce;
    --theme-accent: #6cdaa2;
    --bs-body-color: #e5eaf0;
    --bs-body-color-rgb: 229, 234, 240;
    --bs-body-bg: #141c26;
    --bs-body-bg-rgb: 20, 28, 38;
    --bs-border-color: #465365;
    --bs-border-color-translucent: rgba(200, 215, 235, .2);
    --bs-link-color: #8bb9ff;
    --bs-link-hover-color: #b4d2ff;
    --bs-code-color: #f49cc9;
}

html[data-theme="dark"] body.login-page {
    background: linear-gradient(135deg, #10392c, #141c26) !important;
}

html[data-theme="dark"] .card {
    --bs-card-color: var(--bs-body-color);
    --bs-card-bg: var(--theme-surface);
    --bs-card-cap-bg: var(--theme-raised);
}

html[data-theme="dark"] .list-group {
    --bs-list-group-color: var(--bs-body-color);
    --bs-list-group-bg: var(--theme-surface);
    --bs-list-group-border-color: var(--bs-border-color);
    --bs-list-group-action-color: var(--bs-body-color);
    --bs-list-group-action-hover-color: var(--bs-body-color);
    --bs-list-group-action-hover-bg: var(--theme-raised);
    --bs-list-group-action-active-color: var(--bs-body-color);
    --bs-list-group-action-active-bg: var(--theme-raised);
}

html[data-theme="dark"] .modal-content {
    --bs-modal-color: var(--bs-body-color);
    --bs-modal-bg: var(--theme-surface);
}

/* Explicit surfaces also cover the existing inline and per-view styles. */
html[data-theme="dark"] :is(.card, .card-header, .card-footer, .bg-white:not(.btn-close), .bg-light,
    .dashboard-tool, .page-empty-state, .empty-state, .ratio-card, .section-pills,
    .balance-workbar, .balance-actions, .table-scroll-hint, .loading-modal, #hcaptchaContainer,
    #captchaAttemptMessage) {
    background: var(--theme-surface) !important;
    color: var(--bs-body-color);
}

html[data-theme="dark"] :is(.text-dark, .text-black,
    .page-empty-state h2, .form-label, .ratio-name,
    .ratio-target, .loading-modal #spinnerMessage, #balanceAudit .audit-company) {
    color: var(--bs-body-color) !important;
}

html[data-theme="dark"] :is(.text-muted, .text-secondary, .table-scroll-hint,
    .ratio-formula, .loading-modal__hint, .audit-change-values dt, #balanceAudit .audit-date,
    #balanceAudit .audit-user, #balanceAudit .dt-length, #balanceAudit .dt-search,
    #balanceAudit .dt-info) {
    color: var(--theme-muted) !important;
}

html[data-theme="dark"] .financial-analysis {
    --ink: var(--bs-body-color);
    --muted: var(--theme-muted);
    --line: var(--bs-border-color);
}

html[data-theme="dark"] .section-pills a:not(.active) {
    color: var(--bs-body-color);
}

html[data-theme="dark"] :is(.dashboard-tool-icon, .chart-badge, .loading-modal__indicator) {
    background: #173f32;
    color: var(--theme-accent);
}

html[data-theme="dark"] :is(.loading-modal__eyebrow, .analysis-table strong,
    .ratio-change.up, .text-success, .audit-change-values > :last-child dd) {
    color: var(--theme-accent) !important;
}

html[data-theme="dark"] .text-primary {
    color: var(--bs-link-color) !important;
}

html[data-theme="dark"] :is(.text-danger, .ratio-change.down) {
    color: #ff8892 !important;
}

html[data-theme="dark"] :is(.form-control, .form-select, .input-group-text,
    .dt-container input, .dt-container select,
    .select2-container--bootstrap-5 .select2-selection,
    .select2-container--bootstrap-5 .select2-dropdown,
    .select2-container--bootstrap-5 .select2-search__field) {
    background-color: var(--theme-input-bg, var(--theme-surface));
    color: var(--bs-body-color);
    border-color: var(--bs-border-color);
}

html[data-theme="dark"] :is(.form-control, .form-select):focus {
    border-color: var(--theme-accent);
    box-shadow: 0 0 0 .25rem rgba(108, 218, 162, .25);
}

html[data-theme="dark"] .form-control::placeholder {
    color: var(--theme-muted);
}

html[data-theme="dark"] .form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='none' stroke='%23e5eaf0' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3E%3C/svg%3E");
}

html[data-theme="dark"] :is(.form-control:disabled, .form-select:disabled, .form-control[readonly]) {
    --theme-input-bg: var(--theme-raised);
    background-color: var(--theme-input-bg);
}

html[data-theme="dark"] .total-input {
    background: var(--theme-raised) !important;
}

html[data-theme="dark"] .select2-container--bootstrap-5 .select2-selection__rendered {
    color: var(--bs-body-color);
}

html[data-theme="dark"] .select2-container--bootstrap-5 .select2-selection__placeholder {
    color: var(--theme-muted);
}

html[data-theme="dark"] .select2-container--bootstrap-5 .select2-results__option--selected {
    background: var(--theme-raised);
    color: var(--bs-body-color);
}

html[data-theme="dark"] .dropdown-menu {
    --bs-dropdown-color: var(--bs-body-color);
    --bs-dropdown-bg: var(--theme-surface);
    --bs-dropdown-link-color: var(--bs-body-color);
    --bs-dropdown-link-hover-color: var(--bs-body-color);
    --bs-dropdown-link-hover-bg: var(--theme-raised);
    --bs-dropdown-link-active-bg: #006b3b;
}

html[data-theme="dark"] .pagination {
    --bs-pagination-color: var(--bs-link-color);
    --bs-pagination-bg: var(--theme-surface);
    --bs-pagination-border-color: var(--bs-border-color);
    --bs-pagination-hover-color: var(--bs-link-hover-color);
    --bs-pagination-hover-bg: var(--theme-raised);
    --bs-pagination-hover-border-color: var(--bs-border-color);
    --bs-pagination-disabled-color: var(--theme-muted);
    --bs-pagination-disabled-bg: var(--theme-raised);
    --bs-pagination-disabled-border-color: var(--bs-border-color);
}

html[data-theme="dark"] .btn-light {
    --bs-btn-color: var(--bs-body-color);
    --bs-btn-bg: var(--theme-raised);
    --bs-btn-border-color: var(--bs-border-color);
    --bs-btn-hover-color: #fff;
    --bs-btn-hover-bg: #3b4d62;
    --bs-btn-hover-border-color: #61758e;
    --bs-btn-active-color: #fff;
    --bs-btn-active-bg: #3b4d62;
}

html[data-theme="dark"] .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%);
}

html[data-theme="dark"] .btn-close.bg-white {
    background-color: transparent !important;
}

html[data-theme="dark"] .table {
    --bs-table-color: var(--bs-body-color);
    --bs-table-bg: var(--theme-surface);
    --bs-table-border-color: var(--bs-border-color);
    --bs-table-striped-bg: rgba(200, 215, 235, .04);
    --bs-table-hover-bg: rgba(200, 215, 235, .08);
    --bs-table-active-bg: rgba(200, 215, 235, .12);
    color: var(--bs-body-color);
}

html[data-theme="dark"] :is(.table-light, #balanceAudit #auditTable thead) {
    --bs-table-color: var(--bs-body-color);
    --bs-table-bg: var(--theme-raised);
}

html[data-theme="dark"] .table :is(tbody, tfoot) tr {
    background-color: var(--theme-surface) !important;
}

html[data-theme="dark"] .table :is(td, th) {
    border-color: var(--bs-border-color);
}

html[data-theme="dark"] :is(.balance-table, .analysis-table, .ebitda-table) thead th {
    background-color: var(--theme-raised);
}

html[data-theme="dark"] :is(.balance-table, .analysis-table) .form-control {
    background-color: var(--theme-input-bg, var(--theme-surface)) !important;
}

html[data-theme="dark"] .balance-table .editable input[name^="saldo_deudor"] {
    --theme-input-bg: #163e46;
}

html[data-theme="dark"] .balance-table .editable input[name^="saldo_acreedor"] {
    --theme-input-bg: #173f32;
}

html[data-theme="dark"] #balanceAudit #auditTable :is(thead, tbody, th),
html[data-theme="dark"] #balanceAudit .dt-search input,
html[data-theme="dark"] #balanceAudit .dt-length select,
html[data-theme="dark"] #balanceAudit .audit-count-button,
html[data-theme="dark"] #balanceAudit .dt-paging .dt-paging-button:not(.current) {
    background: var(--theme-surface) !important;
    color: var(--bs-body-color) !important;
    border-color: var(--bs-border-color);
}

html[data-theme="dark"] #balanceAudit #auditTable td[data-label]::before {
    color: var(--theme-muted);
}

html[data-theme="dark"] .login-page :is(p, label, [style*="color:#777"]) {
    color: var(--bs-body-color) !important;
}

html[data-theme="dark"] .login-page a {
    color: var(--theme-accent);
}

html[data-theme="dark"] .analysis-kpi-summary {
    background: var(--theme-raised) !important;
}

@media print {
    .theme-selector, .theme-selector-auth {
        display: none !important;
    }
}
