// -----------------------------------------------------------------------------
//  Dark mode corrections
//
//  Bootstrap's `.bg-white`, `.bg-light`, `.text-dark` and `.text-black` are
//  absolute, not theme-aware: they stay white-on-white and black-on-black when
//  `data-bs-theme="dark"` is set. This codebase uses them in around forty
//  views, which is why several pages read badly in dark mode.
//
//  Remapping the four utilities here fixes every one of those pages at once.
//  The alternative — editing forty files — would be a large diff that the next
//  view written from the same habit would immediately undo.
//
//  These are intentionally scoped to the dark theme, so light mode is
//  untouched.
// -----------------------------------------------------------------------------

[data-bs-theme='dark'] {

    // --- Surfaces -------------------------------------------------------------
    // `bg-white` almost always means "this is a card"; in dark that is the body
    // surface, not literal white.
    .bg-white {
        background-color: var(--bs-body-bg) !important;
    }

    // `bg-light` means "a shade off the card" — a filter bar, a well, a muted
    // row. The tertiary surface is its dark equivalent.
    .bg-light,
    .bg-body-secondary {
        background-color: var(--bs-tertiary-bg) !important;
    }

    // --- Text -----------------------------------------------------------------
    // These read as "strong body text", never as literal black.
    .text-dark,
    .text-black {
        color: var(--bs-body-color) !important;
    }

    // A muted grey chosen against white is usually too dark to read on a dark
    // card. Bootstrap's own secondary colour already adapts.
    .text-muted {
        color: var(--bs-secondary-color) !important;
    }

    // --- Combinations that break ----------------------------------------------
    // `.badge.bg-light.text-*` is a common pill in this codebase. With bg-light
    // remapped the pill works, but it needs a border to stay visible against a
    // card of nearly the same shade.
    .badge.bg-light {
        border: 1px solid var(--bs-border-color);
    }

    // Input groups pin an addon to `bg-white` to blend with the field beside
    // it; the field itself is themed, so the addon must follow.
    .input-group-text.bg-white {
        background-color: var(--bs-body-bg) !important;
        border-color: var(--bs-border-color);
        color: var(--bs-secondary-color) !important;
    }

    // A modal explicitly painted white-on-black text would otherwise invert
    // into black text on a dark panel.
    .modal-content.bg-white {
        background-color: var(--bs-body-bg) !important;
    }

    // --- Falcon subtle badges -------------------------------------------------
    // The theme mixes these from light-mode values, so they land as pale tints
    // that vanish. Rebuild them from the semantic colour at low alpha, which
    // reads on either background.
    @each $tone, $rgb in (
        'primary': var(--bs-primary-rgb),
        'success': var(--bs-success-rgb),
        'danger': var(--bs-danger-rgb),
        'warning': var(--bs-warning-rgb),
        'info': var(--bs-info-rgb),
        'secondary': var(--bs-secondary-rgb)
    ) {
        .badge-subtle-#{$tone} {
            background-color: rgba(#{$rgb}, 0.18) !important;
            color: rgb(#{$rgb}) !important;
            border: 1px solid rgba(#{$rgb}, 0.28);
        }
    }

    // --- Tables ---------------------------------------------------------------
    // A striped table built for light mode darkens rows that are already dark.
    .table-striped > tbody > tr:nth-of-type(odd) > * {
        --bs-table-accent-bg: rgba(255, 255, 255, 0.025);
        color: var(--bs-body-color);
    }

    // Borders drawn in near-black disappear entirely.
    .table,
    .table > :not(caption) > * > * {
        border-color: var(--bs-border-color);
    }

    // --- Table variants -------------------------------------------------------
    // `.table-info`, `.table-primary` and friends hard-code a pale fill with
    // black text (`--bs-table-bg: #cff4fc`, `--bs-table-color: #000`), which is
    // absolute — so a totals row keeps its light-blue block on a dark page and
    // the figures inside become unreadable.
    //
    // The theme does ship dark `--cp-table-*` values for these classes, which
    // makes this look redundant. It is not: the tables that use these variants
    // are Bootstrap-classed DataTables reading `--bs-table-*`, so the theme's
    // `--cp-` rule never reaches them. Removing this block puts the light-blue
    // Page Totals / Grand Totals rows straight back — verified by doing exactly
    // that.
    @each $tone in (primary, secondary, success, info, warning, danger) {
        .table-#{$tone} {
            --bs-table-color: var(--bs-body-color);
            --bs-table-bg: rgba(var(--bs-#{$tone}-rgb), 0.14);
            --bs-table-border-color: var(--bs-border-color);
            --bs-table-striped-color: var(--bs-body-color);
            --bs-table-striped-bg: rgba(var(--bs-#{$tone}-rgb), 0.18);
            --bs-table-hover-color: var(--bs-body-color);
            --bs-table-hover-bg: rgba(var(--bs-#{$tone}-rgb), 0.22);
            color: var(--bs-body-color);
        }
    }

    .table-light {
        --bs-table-color: var(--bs-body-color);
        --bs-table-bg: var(--bs-tertiary-bg);
        --bs-table-border-color: var(--bs-border-color);
        color: var(--bs-body-color);
    }

    .table > thead.table-light th {
        background-color: var(--bs-tertiary-bg);
        color: var(--bs-body-color);
    }
}
