:root {
    --ap-bg: #f4f6f9;
    --ap-surface: #ffffff;
    --ap-surface-2: #f8f9fa;
    --ap-text: #222222;
    --ap-muted: #6c757d;
    --ap-border: #d9dde3;
}

@media (prefers-color-scheme: dark) {
    :root {
        --ap-bg: #12161d;
        --ap-surface: #1b222d;
        --ap-surface-2: #232c3a;
        --ap-text: #e6edf7;
        --ap-muted: #a6b1c2;
        --ap-border: #2f3b4e;
    }

    html,
    body {
        background: var(--ap-bg) !important;
        color: var(--ap-text) !important;
    }

    .container,
    .container-fluid,
    .panel,
    .dash-card,
    .month-selector-bar,
    .inv-summary-bar,
    .holdings-panel,
    .holdings-toggle-bar,
    .inv-account-bar,
    .dataTables_wrapper,
    .dt-buttons,
    .dt-search-info-row,
    .modal-content,
    .well,
    .alert,
    .list-group-item,
    .table,
    .table-responsive {
        background-color: var(--ap-surface) !important;
        color: var(--ap-text) !important;
        border-color: var(--ap-border) !important;
    }

    .table > thead > tr > th,
    .table > tbody > tr > td,
    .table > tbody > tr > th,
    .table > tfoot > tr > td,
    .table > tfoot > tr > th,
    .table > thead > tr > td,
    .table > thead > tr > th {
        border-color: var(--ap-border) !important;
        color: var(--ap-text) !important;
    }

    .table-striped > tbody > tr:nth-of-type(odd),
    .table-hover > tbody > tr:hover {
        background-color: var(--ap-surface-2) !important;
    }

    input,
    select,
    textarea,
    .form-control,
    .select2-selection,
    .select2-dropdown {
        background-color: var(--ap-surface-2) !important;
        color: var(--ap-text) !important;
        border-color: var(--ap-border) !important;
    }

    .form-control::placeholder,
    input::placeholder,
    textarea::placeholder {
        color: var(--ap-muted) !important;
    }

    .text-muted,
    .help-block,
    .dataTables_info {
        color: var(--ap-muted) !important;
    }

    a,
    .btn-link {
        color: #8fb7ff;
    }

    .modal-header,
    .modal-footer,
    .panel-heading,
    .navbar-default,
    .navbar-default .navbar-collapse,
    .navbar-default .navbar-form {
        border-color: var(--ap-border) !important;
    }

    .dropdown-menu {
        background-color: var(--ap-surface-2) !important;
        border-color: var(--ap-border) !important;
    }

    .dropdown-menu > li > a {
        color: var(--ap-text) !important;
    }

    .dropdown-menu > li > a:hover,
    .dropdown-menu > li > a:focus {
        background-color: #2b3748 !important;
    }

    .select2-container--default .select2-selection--single .select2-selection__rendered,
    .select2-container--default .select2-selection--multiple .select2-selection__choice,
    .select2-results__option {
        color: var(--ap-text) !important;
    }

    .select2-container--default .select2-selection--single .select2-selection__placeholder {
        color: var(--ap-muted) !important;
    }

    .site-nav,
    .site-nav .navbar-collapse,
    .site-nav .dropdown-menu {
        background-color: #0f151f !important;
        border-color: #263246 !important;
    }

    /* Keep account/view bars readable but not bright in dark mode */
    .view-label-bar,
    .account-selector-bar,
    .inv-account-bar,
    .month-selector-bar {
        background: #17202c !important;
        color: var(--ap-text) !important;
        border-color: var(--ap-border) !important;
    }

    .view-label-bar.withdrawal {
        background: #2a1d21 !important;
        color: #ffb4af !important;
        border-left-color: #d96a5f !important;
    }

    .view-label-bar.deposit {
        background: #182724 !important;
        color: #9fdabf !important;
        border-left-color: #3aa56f !important;
    }

    .view-label-bar.credit {
        background: #2b2618 !important;
        color: #f2d483 !important;
        border-left-color: #d0a72e !important;
    }

    /* Projected panel and holdings panel should not glow against dark tables */
    .proj-toggle-bar,
    .holdings-toggle-bar {
        background: #1b2633 !important;
        color: #c6d3e6 !important;
        border-color: var(--ap-border) !important;
    }

    .proj-toggle-bar:hover,
    .holdings-toggle-bar:hover {
        background: #202f40 !important;
    }

    .proj-panel,
    .holdings-panel,
    #holdingsPanel {
        background: #111a24 !important;
        border-color: var(--ap-border) !important;
    }

    #proj-table,
    #holdingsPanel table,
    #holdingsPanel .table {
        background: #111a24 !important;
        color: var(--ap-text) !important;
    }

    #proj-table thead th,
    #holdingsPanel thead th {
        background: #182433 !important;
        color: #c9d7ea !important;
        border-bottom-color: var(--ap-border) !important;
    }

    #proj-table tbody td,
    #holdingsPanel tbody td {
        background: #111a24 !important;
        border-bottom-color: #263246 !important;
    }

    #proj-table tbody tr:hover td,
    #holdingsPanel tbody tr:hover td {
        background: #1b2939 !important;
    }

    /* Unify DataTables odd/even row tones to prevent bright striping artifacts */
    table.dataTable tbody tr,
    table.dataTable.display tbody tr,
    table.dataTable.stripe tbody tr,
    table.dataTable.order-column.stripe tbody tr {
        background-color: #111a24 !important;
        color: var(--ap-text) !important;
    }

    table.dataTable.stripe tbody tr.odd,
    table.dataTable.display tbody tr.odd,
    table.dataTable.order-column.stripe tbody tr.odd,
    table.dataTable.order-column tbody tr > .sorting_1 {
        background-color: #172231 !important;
    }

    table.dataTable tbody tr.even {
        background-color: #111a24 !important;
    }

    table.dataTable tbody tr:hover,
    table.dataTable.display tbody tr:hover,
    .table-hover > tbody > tr:hover {
        background-color: #203042 !important;
    }

    /* Investment summary and freshness bars */
    .inv-stat {
        background: linear-gradient(135deg, #1b3f61 0%, #1c5c8d 100%) !important;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35) !important;
    }

    .freshness-banner {
        background: #17212d !important;
        color: #c2cfdf !important;
        border-color: var(--ap-border) !important;
    }

    .freshness-banner.warn {
        background: #332a1b !important;
        color: #e7c887 !important;
    }

    .freshness-banner.ok {
        background: #1d3026 !important;
        color: #9ed6b2 !important;
    }

    /* Journal inline cards and hero tones */
    .dash-card,
    .stmt-card {
        background: #18222f !important;
        color: var(--ap-text) !important;
    }

    .balance-hero,
    #journal-hero {
        background: linear-gradient(135deg, #1a3d63 0%, #1d567f 100%) !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35) !important;
    }

    #journal-content .dash-card .row .col-sm-3 > div {
        background: #1f2a38 !important;
        border-color: #314255 !important;
    }

    #journal-content .stat-label,
    #journal-content .section-title,
    #journal-content .month-selector-bar label,
    #journal-content .notes-status,
    #journal-content [id^='hero-'][id$='-flag'] {
        color: var(--ap-muted) !important;
    }

    /* Consistent button palette across pages and DataTables controls */
    body .btn {
        border-color: var(--ap-border) !important;
    }

    body .btn-default,
    body .dt-button.btn-default,
    body .btn-default:active,
    body .dt-button.btn-default:active {
        background: #202a38 !important;
        border-color: #35465c !important;
        color: #d7e1ef !important;
    }

    body .btn-default:hover,
    body .dt-button.btn-default:hover {
        background: #263548 !important;
        border-color: #43607e !important;
        color: #e7eef8 !important;
    }

    body .btn-primary,
    body .dt-button.btn-primary,
    body .btn-primary:active,
    body .dt-button.btn-primary:active {
        background: #2c5f88 !important;
        border-color: #3a7cae !important;
        color: #f4f9ff !important;
    }

    body .btn-primary:hover,
    body .dt-button.btn-primary:hover {
        background: #3472a3 !important;
        border-color: #4f8fbe !important;
    }

    body .btn-success,
    body .dt-button.btn-success,
    body .btn-success:active,
    body .dt-button.btn-success:active {
        background: #2f6b4f !important;
        border-color: #3f8b68 !important;
        color: #f2fff8 !important;
    }

    body .btn-success:hover,
    body .dt-button.btn-success:hover {
        background: #397d5e !important;
        border-color: #4e9a78 !important;
    }

    body .btn-info,
    body .dt-button.btn-info,
    body .btn-info:active,
    body .dt-button.btn-info:active {
        background: #2e677b !important;
        border-color: #3d889f !important;
        color: #f1fbff !important;
    }

    body .btn-info:hover,
    body .dt-button.btn-info:hover {
        background: #387a90 !important;
        border-color: #4a99b0 !important;
    }

    body .btn-warning,
    body .dt-button.btn-warning,
    body .btn-warning:active,
    body .dt-button.btn-warning:active {
        background: #775a2f !important;
        border-color: #9a7440 !important;
        color: #fff7e8 !important;
    }

    body .btn-warning:hover,
    body .dt-button.btn-warning:hover {
        background: #8a6a39 !important;
        border-color: #ad8248 !important;
    }

    body .btn-danger,
    body .dt-button.btn-danger,
    body .btn-danger:active,
    body .dt-button.btn-danger:active {
        background: #7a3a40 !important;
        border-color: #9f4a53 !important;
        color: #fff2f4 !important;
    }

    body .btn-danger:hover,
    body .dt-button.btn-danger:hover {
        background: #8f4650 !important;
        border-color: #b55a66 !important;
    }

    /* Toast consistency */
    body .toast-base {
        background-color: #243145 !important;
        color: #eaf1fb !important;
        border: 1px solid #3a4b64 !important;
        box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35) !important;
    }

    body .toast-base.success {
        background-color: #1f4436 !important;
        border-color: #2f6651 !important;
    }

    body .toast-base.error {
        background-color: #4b2a30 !important;
        border-color: #71404a !important;
    }

    body .toast-base.info {
        background-color: #24435f !important;
        border-color: #356382 !important;
    }

    /* Badge and label consistency */
    body .label,
    body .badge {
        color: #e9eff9 !important;
        border: 1px solid transparent;
    }

    body .label-default,
    body .badge-default {
        background: #2c3544 !important;
        border-color: #3d4b60 !important;
    }

    body .label-primary,
    body .badge-primary {
        background: #2c5f88 !important;
        border-color: #3f7fb2 !important;
    }

    body .label-success,
    body .badge-success {
        background: #2f6b4f !important;
        border-color: #438467 !important;
    }

    body .label-info,
    body .badge-info {
        background: #2e677b !important;
        border-color: #41859a !important;
    }

    body .label-warning,
    body .badge-warning {
        background: #775a2f !important;
        border-color: #9b7440 !important;
    }

    body .label-danger,
    body .badge-danger {
        background: #7a3a40 !important;
        border-color: #9f4a53 !important;
    }

    body .badge-checking,
    body .badge-savings,
    body .badge-credit_card,
    body .badge-investment,
    body .badge-cash,
    body .badge-other,
    body .badge-buy,
    body .badge-sell,
    body .badge-deposit,
    body .badge-withdrawal,
    body .badge-dividend,
    body .badge-fee,
    body .badge-reconciled,
    body .badge-overdue,
    body .badge-planned,
    body .tx-transfer-badge,
    body .tx-recurring-badge,
    body .inv-transfer-badge {
        filter: saturate(0.85) brightness(0.9);
    }

    /* Normalize custom modal header themes used by imports/transfers */
    body .modal-header[style*='background:#2c3e50'] {
        background: #1b2837 !important;
        color: #dce7f6 !important;
        border-bottom-color: #33465f !important;
    }

    body .modal-header[style*='background:#8e44ad'] {
        background: #3d2f57 !important;
        color: #e8ddf7 !important;
        border-bottom-color: #5a437e !important;
    }

    body .modal-header[style*='background:#1f6fb2'] {
        background: #274b6d !important;
        color: #deecfb !important;
        border-bottom-color: #3a658f !important;
    }

    body .modal-header[style*='background:#fff3cd'] {
        background: #3a311f !important;
        color: #ecd29a !important;
        border-bottom-color: #6e5a2b !important;
    }

    body .alert-success {
        background: #1d3026 !important;
        color: #a6d9bb !important;
        border-color: #2f4e3d !important;
    }

    body .alert-danger {
        background: #3a2328 !important;
        color: #e5b7be !important;
        border-color: #5a343d !important;
    }

    body .alert-warning {
        background: #3a311f !important;
        color: #ecd29a !important;
        border-color: #6e5a2b !important;
    }

    body .alert-info {
        background: #223546 !important;
        color: #b8d3ea !important;
        border-color: #334f66 !important;
    }

    /* DataTables search controls and selected row visibility */
    .dataTables_filter,
    .dataTables_wrapper .dataTables_filter,
    .dataTables_length,
    .dataTables_wrapper .dataTables_length {
        background: #182330 !important;
        color: var(--ap-text) !important;
    }

    table.dataTable tbody tr.selected,
    table.dataTable tbody th.selected,
    table.dataTable tbody td.selected,
    table.dataTable.display tbody tr.odd.selected,
    table.dataTable.display tbody tr.even.selected {
        background-color: #2e4f6f !important;
        color: #f4f9ff !important;
    }

    table.dataTable tbody tr.selected td,
    table.dataTable tbody tr.selected th {
        border-color: #55799e !important;
    }

    table.dataTable tbody tr.selected:hover,
    table.dataTable.display tbody tr.selected:hover {
        background-color: #396286 !important;
    }

    /* Balance validation add-point section in transactions modal */
    #vpAddForm {
        background: #182330 !important;
        border-top-color: #2f445a !important;
        color: var(--ap-text) !important;
    }

    #vpNoAccountMsg {
        background: #182330 !important;
        border-top-color: #2f445a !important;
        color: var(--ap-muted) !important;
    }

    #recurringSetupGroup {
        background: #182330 !important;
        border-color: #2f445a !important;
        color: var(--ap-text) !important;
    }

    /* Home page top balance cards */
    #dash-balance-hero,
    .balance-hero {
        background: linear-gradient(135deg, #1d4266 0%, #1f5b86 100%) !important;
    }

    #dash-balance-hero.stale,
    .balance-hero.stale {
        background: linear-gradient(135deg, #5a2f36 0%, #7a3d45 100%) !important;
    }

    .dash-card,
    .stat-card,
    .acct-mini-card,
    #account-cards .acct-mini-card {
        background: #18222f !important;
        color: var(--ap-text) !important;
        border-color: #2f4054 !important;
    }

    .stat-label,
    .acct-mini-name,
    .section-title,
    .recent-table th {
        color: var(--ap-muted) !important;
    }

    .stat-value,
    .acct-mini-bal {
        color: var(--ap-text) !important;
    }

    /* Accounts page cards */
    .account-card,
    .page-card,
    .net-bar {
        background: #18222f !important;
        color: var(--ap-text) !important;
        border-color: #2f4054 !important;
    }

    .account-card:hover,
    .acct-mini-card:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35) !important;
    }

    .acct-name,
    .net-label,
    .acct-type {
        color: #c7d4e6 !important;
    }

    /* Settings categories */
    .settings-panel,
    .category-item {
        background: #18222f !important;
        color: var(--ap-text) !important;
        border-color: #2f4054 !important;
    }

    .category-empty,
    .help-block {
        color: var(--ap-muted) !important;
    }

    /* Recurring sections */
    .section-panel,
    .process-panel {
        background: #18222f !important;
        color: var(--ap-text) !important;
        border-color: #2f4054 !important;
    }

    table.recurring-table th {
        background: #1f2c3b !important;
        color: #c5d4e6 !important;
        border-color: #33485f !important;
    }

    table.recurring-table td,
    table.recurring-table tr {
        background: #18222f !important;
        color: var(--ap-text) !important;
        border-color: #2f4054 !important;
    }

    table.recurring-table tbody tr:hover td {
        background: #223145 !important;
    }

    .dataTables_filter input,
    .dataTables_length select {
        background-color: var(--ap-surface-2) !important;
        color: var(--ap-text) !important;
        border: 1px solid var(--ap-border) !important;
    }

    .close {
        color: var(--ap-text) !important;
        opacity: 0.8;
        text-shadow: none !important;
    }
}