/* Dark mode for pages that keep their own colours in page-level <style> blocks.
   Every rule is limited to on-screen dark mode, so light mode and printing are untouched.
   Colours come from the theme tokens, so they follow the colours a user picks. */

@media screen {

    /* Shared -------------------------------------------------------------- */

    html.cemr-theme-dark .text-info:not([class*="bg-"]) {
        color: var(--cemr-ui-info) !important;
    }

    html.cemr-theme-dark .text-danger:not([class*="bg-"]) {
        color: var(--cemr-ui-danger) !important;
    }

    html.cemr-theme-dark .invalid-feedback {
        color: var(--cemr-ui-danger);
    }

    html.cemr-theme-dark :is(.ba-range-chip, .pra-range-chip) {
        background: var(--md-sys-color-surface-container);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark :is(.ba-validation, .pra-validation) {
        background: var(--cemr-ui-danger-soft);
        color: var(--cemr-ui-danger);
    }

    /* The error banner keeps its light yellow, so its text has to stay dark. */
    html.cemr-theme-dark #blazor-error-ui,
    html.cemr-theme-dark #blazor-error-ui a {
        color: #1c1d1f;
    }

    /* Clinical row components: unset, these fall back to light colours. */
    html.cemr-theme-dark {
        --e-bg-base: var(--md-sys-color-surface);
        --e-input-bg: var(--md-sys-color-surface-container);
        --e-hover-bg: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, var(--md-sys-color-surface));
        --e-content-border-color: var(--md-sys-color-outline-variant);
        --e-content-font-color: var(--md-sys-color-on-surface);
        --e-content-secondary-color: var(--md-sys-color-on-surface-variant);
        --e-content-placeholder-color: var(--md-sys-color-on-surface-variant);
    }

    /* Billing: /Bill and the patient invoice list ------------------------- */

    html.cemr-theme-dark .billing-video-ui {
        --bill-accent-soft: var(--md-sys-color-surface-container);
        --bill-border: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .billing-video-ui :is(.billing-info-card, .patient-select-box, .content-wrapper, #BillingGrid, .billing-bottom-summary) {
        background: var(--md-sys-color-surface);
        border-color: var(--md-sys-color-outline-variant);
    }

    html.cemr-theme-dark .billing-video-ui .billing-card-title {
        background: var(--md-sys-color-surface-container);
        border-bottom-color: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .billing-video-ui .label_header,
    html.cemr-theme-dark .billing-video-ui .bill-summary-item {
        color: var(--md-sys-color-on-surface-variant);
    }

    html.cemr-theme-dark .billing-video-ui .bill-summary-item.total b {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .billing-video-ui .required-star {
        color: var(--cemr-ui-danger);
    }

    html.cemr-theme-dark .billing-video-ui :is(.patient-select-box, .patient-name) {
        color: var(--md-sys-color-on-surface);
    }

    /* The success token is a dark green, too dim on a dark surface. */
    html.cemr-theme-dark .text-success {
        color: #1abe17 !important;
    }

    html.cemr-theme-dark .billing-video-ui :is(.patient-dot, .patient-edit-icon:hover) {
        color: var(--cemr-on-primary);
    }

    html.cemr-theme-dark .billing-video-ui .summary-divider {
        background: var(--md-sys-color-outline-variant);
    }

    /* The page sets a pale row hover, which would sit behind light cell text. */

    /* Billing analysis and query builder: both read local colour variables. */

    html.cemr-theme-dark :is(.ba-page, .bqb-page) {
        --border: var(--md-sys-color-outline-variant);
        --border-md: var(--md-sys-color-outline);
        --text: var(--md-sys-color-on-surface);
        --muted: var(--md-sys-color-on-surface-variant);
        --hint: var(--md-sys-color-on-surface-variant);
        --surf: var(--md-sys-color-surface);
        --surf2: var(--md-sys-color-surface-container);
        --green: #1abe17;
        --green-lt: color-mix(in srgb, #1abe17 16%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .bqb-page {
        background: transparent;
    }

    html.cemr-theme-dark .bqb-page .bqb-saved-name {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .ba-qbtn:not(.ba-qbtn-active):hover {
        border-color: var(--cemr-focus-ring);
        color: var(--md-sys-color-on-surface);
    }

    /* Pharmacy report analysis -------------------------------------------- */

    html.cemr-theme-dark .pra-page,
    html.cemr-theme-dark .pra-title {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark :is(.pra-subtitle, .pra-label, .pra-section-sub-label) {
        color: var(--md-sys-color-on-surface-variant);
    }

    html.cemr-theme-dark .pra-card {
        background: var(--md-sys-color-surface);
        border-color: var(--md-sys-color-outline-variant);
    }

    html.cemr-theme-dark .pra-section-divider {
        background: var(--md-sys-color-outline-variant);
    }

    html.cemr-theme-dark .pra-qbtn:not(.pra-qbtn-active) {
        background: var(--md-sys-color-surface-container);
        border-color: var(--md-sys-color-outline);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .pra-qbtn:not(.pra-qbtn-active):hover {
        background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, var(--md-sys-color-surface-container));
        border-color: var(--cemr-focus-ring);
    }

    /* Advance report ------------------------------------------------------ */

    html.cemr-theme-dark .e-splitter .bg-white {
        background: var(--md-sys-color-surface) !important;
    }

    html.cemr-theme-dark :is(.refresh-container, .search-container) {
        background: var(--md-sys-color-surface);
    }

    html.cemr-theme-dark .image-count {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .tab-links li a {
        background: var(--md-sys-color-surface-container);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .tab-links li:is(.active, :hover) a {
        background: var(--md-sys-color-primary);
        color: var(--md-sys-color-on-primary);
    }

    html.cemr-theme-dark .e-splitter .btn-secondary {
        background: var(--md-sys-color-surface-container) !important;
        border-color: var(--md-sys-color-outline) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-splitter .btn-secondary:is(:hover, :active) {
        background: color-mix(in srgb, var(--md-sys-color-on-surface) 12%, var(--md-sys-color-surface-container)) !important;
    }

    /* Report viewers: the toolbars turn dark, the report page stays white. */

    html.cemr-theme-dark .rv-toolbar {
        background: var(--md-sys-color-surface);
        border-bottom-color: var(--md-sys-color-outline-variant);
    }

    html.cemr-theme-dark .rv-divider {
        background: var(--md-sys-color-outline-variant);
    }

    /* Bold viewer: its own stylesheet fixes these areas to light colours. The report page itself is left alone. */
    html.cemr-theme-dark .e-reportviewer :is(.e-reportviewer-viewerblock, .e-reportviewer-viewercontainer, .e-reportviewer-pageviewcontainer) {
        background: var(--md-sys-color-surface-container) !important;
        border-color: var(--md-sys-color-outline-variant) !important;
    }

    html.cemr-theme-dark .e-reportviewer .e-toolbar .e-current-page-box {
        background: var(--md-sys-color-surface) !important;
        border-color: var(--md-sys-color-outline) !important;
    }

    html.cemr-theme-dark .e-reportviewer [id$="_toolbar_labelpageno"] {
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-reportviewer :is(.e-rv-scroller, .e-rv-native-rightToolbar, .e-acrdn-header, .e-acrdn-panel) {
        background: var(--md-sys-color-surface) !important;
    }

    html.cemr-theme-dark .e-reportviewer :is(.e-reportviewer-nativepropertiesheader, .e-acrdn-header-content) {
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-reportviewer .e-rv-native-rightToolbar > [id$="_toolbar_settings_div"] {
        background: var(--md-sys-color-surface-container) !important;
    }

    html.cemr-theme-dark .e-reportviewer :is(.e-rv-native-rightToolbar, .e-rv-native-PropertiesContainer, .e-reportviewer-nativepropertiesheader, .e-acrdn-item, .e-acrdn-header) {
        border-color: var(--md-sys-color-outline-variant) !important;
    }

    html.cemr-theme-dark .e-reportviewer .e-rv-native-rightToolbar .e-viewer-icons {
        color: var(--md-sys-color-on-surface) !important;
    }

    /* Pharmacy icon tiles: pale tints set per card, some inline. The icon keeps its colour. */
    html.cemr-theme-dark :is(.summary-icon, .audit-metric-icon) {
        background: color-mix(in srgb, currentColor 16%, var(--md-sys-color-surface)) !important;
    }

    html.cemr-theme-dark .summary-icon {
        color: color-mix(in srgb, var(--card-color) 60%, #ffffff);
    }

    /* MST choice buttons: a chosen option has to stand out clearly on dark. */
    html.cemr-theme-dark .mst-pd-segmented:not(.mst-pd-priority) button.selected {
        color: var(--cemr-on-primary);
        background: var(--md-sys-color-primary);
        border-color: var(--cemr-focus-ring);
    }

    html.cemr-theme-dark .pharmacy-title-icon {
        background: var(--md-sys-color-surface-container);
        color: var(--cemr-focus-ring);
    }

    html.cemr-theme-dark .bg-light.border-bottom:has(> .btn-outline-secondary) {
        background: var(--md-sys-color-surface-container) !important;
        border-bottom-color: var(--md-sys-color-outline-variant) !important;
    }

    html.cemr-theme-dark .bg-light.border-bottom > .btn-outline-secondary {
        border-color: var(--md-sys-color-outline) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .bg-light.border-bottom > .btn-outline-secondary:is(:hover, :active, :focus-visible) {
        background: color-mix(in srgb, var(--md-sys-color-on-surface) 12%, var(--md-sys-color-surface-container)) !important;
    }

    html.cemr-theme-dark .bg-light.border-bottom:has(> .btn-outline-secondary) .text-muted {
        color: var(--md-sys-color-on-surface-variant) !important;
    }

    /* RTF report: the page sets fixed white panels in its own style block. */

    html.cemr-theme-dark:has(.thumbnail-sidebar) .body-layout {
        background: var(--md-sys-color-background);
    }

    html.cemr-theme-dark:has(.thumbnail-sidebar) :is(.patient-header, .form-container, .thumbnail-sidebar, .editor, .gallery, .gallery .section, .captured, .dialog-box) {
        background: var(--md-sys-color-surface);
        border-color: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    /* IPD workstation ----------------------------------------------------- */

    html.cemr-theme-dark main:has(.ipd-page-container) {
        background: var(--md-sys-color-background);
    }

    html.cemr-theme-dark .ipd-page-container .alert-info {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .ipd-card .h4 {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark a.ipd-stay-chip:hover {
        color: var(--md-sys-color-on-surface);
    }

    /* Bootstrap's light helpers on IPD pages and their dialogs. The text already follows
       the theme, so a fill that stays light hides it. A bg-* class marks a selected button. */
    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-white {
        background-color: var(--md-sys-color-surface) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-light {
        background-color: var(--md-sys-color-surface-container-high) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) :is(.badge-light, .btn-light:not([class*="bg-"])) {
        background-color: var(--md-sys-color-surface-container-high) !important;
        border-color: var(--md-sys-color-outline-variant) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .btn-light:not([class*="bg-"]):is(:hover, :focus-visible, :active) {
        background-color: color-mix(in srgb, var(--md-sys-color-on-surface) 12%, var(--md-sys-color-surface-container-high)) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) :is(.bg-white, .bg-light).text-dark {
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .border:not([class*="border-"]) {
        border-color: var(--md-sys-color-outline-variant) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table {
        border-color: var(--md-sys-color-outline-variant);
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) :is(.table-light, .table-secondary, .table-primary, .table-success, .table-warning, .table-danger, .table-info) {
        --bs-table-bg: var(--md-sys-color-surface-container-high);
        border-color: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table-primary {
        --bs-table-bg: color-mix(in srgb, var(--md-sys-color-primary) 18%, var(--md-sys-color-surface));
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table-success {
        --bs-table-bg: color-mix(in srgb, #8ed8a4 14%, var(--md-sys-color-surface));
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table-warning {
        --bs-table-bg: color-mix(in srgb, #ffc078 14%, var(--md-sys-color-surface));
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table-danger {
        --bs-table-bg: color-mix(in srgb, #ffb4ab 14%, var(--md-sys-color-surface));
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .table-info {
        --bs-table-bg: color-mix(in srgb, #a3d6ff 14%, var(--md-sys-color-surface));
    }

    /* The yellow fill stays in dark mode, so its text must stay dark. */
    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .btn-warning.text-dark {
        color: #1c1d1f !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .badge.bg-secondary {
        background-color: var(--md-sys-color-surface-container-high) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    /* These pages use Bootstrap 5.3 subtle classes, which 5.1 does not define, so the app's
       text helpers skip them as "has its own bg-* colour" and leave a light-mode text colour. */
    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-primary-subtle.text-primary {
        color: color-mix(in srgb, var(--md-sys-color-primary) 45%, #ffffff) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-info-subtle.text-info {
        color: var(--cemr-ui-info) !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-danger-subtle.text-danger {
        color: #ffb4ab !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-success-subtle.text-success {
        color: #8ed8a4 !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-warning-subtle.text-warning {
        color: #ffc078 !important;
    }

    html.cemr-theme-dark body:has(:is(.ipd-page-container, .ipd-master-page)) .bg-secondary-subtle.text-secondary {
        color: var(--md-sys-color-on-surface-variant) !important;
    }

    /* IPD colours set for light mode in page styles and ipd.css. */
    html.cemr-theme-dark .btn-text-clear {
        color: #ffb4ab;
    }

    html.cemr-theme-dark .rv-field .validation-message {
        color: #ffb4ab;
    }

    html.cemr-theme-dark .attachment-thumb-wrap {
        background: var(--md-sys-color-surface-container-high);
    }

    html.cemr-theme-dark .meta-badge.allergy-alert {
        background: var(--ipd-warning-bg);
        border-color: var(--ipd-warning-border);
        color: var(--ipd-warning);
    }

    html.cemr-theme-dark .range-summary .max-tag {
        background: color-mix(in srgb, #ffd8e4 14%, var(--md-sys-color-surface));
        color: #ffd8e4;
    }

    /* Report viewer title, used by IPD and other pages. */
    html.cemr-theme-dark .rv-title strong {
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .rv-title small {
        color: var(--md-sys-color-on-surface-variant);
    }

    /* Pharmacy sale and return pages: their colour variables are set for light mode. */
    html.cemr-theme-dark {
        --pos-primary-soft: color-mix(in srgb, var(--md-sys-color-primary) 14%, var(--md-sys-color-surface));
        --pos-primary-container: var(--md-sys-color-primary-container);
        --pos-surface: var(--md-sys-color-surface);
        --pos-surface-container: var(--md-sys-color-surface-container);
        --pos-surface-container-high: var(--md-sys-color-surface-container-high);
        --pos-border: var(--md-sys-color-outline-variant);
        --pos-border-subtle: var(--md-sys-color-outline-variant);
        --pos-text: var(--md-sys-color-on-surface);
        --pos-text-muted: var(--md-sys-color-on-surface-variant);
        --pos-danger: #ffb4ab;
        --pos-danger-soft: color-mix(in srgb, #ffb4ab 14%, var(--md-sys-color-surface));
        --pos-success: #8ed8a4;
        --pos-success-soft: color-mix(in srgb, #8ed8a4 14%, var(--md-sys-color-surface));
        --pos-warning: #ffc078;
        --pos-warning-soft: color-mix(in srgb, #ffc078 14%, var(--md-sys-color-surface));
    }

    /* MST reporting: the same, for its own variables. */
    html.cemr-theme-dark {
        --mst-primary: var(--cemr-focus-ring);
        --mst-primary-dark: var(--cemr-focus-ring);
        --mst-primary-light: var(--cemr-focus-ring);
        --mst-primary-soft: color-mix(in srgb, var(--md-sys-color-primary) 14%, var(--md-sys-color-surface));
        --mst-primary-soft-strong: color-mix(in srgb, var(--md-sys-color-primary) 24%, var(--md-sys-color-surface));
        --mst-bg: var(--md-sys-color-surface);
        --mst-card: var(--md-sys-color-surface-container);
        --mst-border: var(--md-sys-color-outline-variant);
        --mst-border-soft: var(--md-sys-color-outline-variant);
        --mst-border-dark: var(--md-sys-color-outline-variant);
        --mst-gray-border: var(--md-sys-color-outline-variant);
        --mst-gray-divider: var(--md-sys-color-outline-variant);
        --mst-bg-light: var(--md-sys-color-surface-container);
        --mst-text: var(--md-sys-color-on-surface);
        --mst-text-soft: var(--md-sys-color-on-surface-variant);
        --mst-muted: var(--md-sys-color-on-surface-variant);
        --mst-danger: #ffb4ab;
        --mst-danger-soft: color-mix(in srgb, #ffb4ab 14%, var(--md-sys-color-surface));
        --mst-warn: #ffc078;
        --mst-success-bg: color-mix(in srgb, #8ed8a4 14%, var(--md-sys-color-surface));
        --mst-success-text: #8ed8a4;
        --mst-success-border: color-mix(in srgb, #8ed8a4 45%, var(--md-sys-color-surface));
        --mst-approved-bg: color-mix(in srgb, #8ed8a4 14%, var(--md-sys-color-surface));
        --mst-approved-text: #8ed8a4;
        --mst-approved-border: color-mix(in srgb, #8ed8a4 45%, var(--md-sys-color-surface));
        --mst-draft-bg: color-mix(in srgb, #ffc078 14%, var(--md-sys-color-surface));
        --mst-draft-text: #ffc078;
        --mst-draft-border: color-mix(in srgb, #ffc078 45%, var(--md-sys-color-surface));
    }

    /* Alerts on any page. */
    html.cemr-theme-dark .alert-info {
        background-color: color-mix(in srgb, #a3d6ff 14%, var(--md-sys-color-surface));
        border-color: color-mix(in srgb, #a3d6ff 45%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .alert-warning {
        background-color: color-mix(in srgb, #ffc078 14%, var(--md-sys-color-surface));
        border-color: color-mix(in srgb, #ffc078 45%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .alert-danger {
        background-color: color-mix(in srgb, #ffb4ab 14%, var(--md-sys-color-surface));
        border-color: color-mix(in srgb, #ffb4ab 45%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .alert-success {
        background-color: color-mix(in srgb, #8ed8a4 14%, var(--md-sys-color-surface));
        border-color: color-mix(in srgb, #8ed8a4 45%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .alert-primary {
        background-color: color-mix(in srgb, var(--md-sys-color-primary) 14%, var(--md-sys-color-surface));
        border-color: color-mix(in srgb, var(--md-sys-color-primary) 45%, var(--md-sys-color-surface));
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .alert-secondary {
        background-color: var(--md-sys-color-surface-container-high);
        border-color: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    /* Light info headers carry white text in light mode. */
    html.cemr-theme-dark .bg-info.text-white,
    html.cemr-theme-dark .bg-info.text-white * {
        color: #10202b !important;
    }

    html.cemr-theme-dark .btn-secondary {
        background-color: var(--md-sys-color-surface-container-high) !important;
        border-color: var(--md-sys-color-outline) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .btn-secondary:hover {
        background-color: color-mix(in srgb, var(--md-sys-color-on-surface) 14%, var(--md-sys-color-surface)) !important;
    }

    html.cemr-theme-dark .bg-light.border-bottom:has(.btn-outline-secondary) {
        background-color: var(--md-sys-color-surface-container) !important;
    }

    html.cemr-theme-dark .bg-light.border-bottom .btn-outline-secondary {
        border-color: var(--md-sys-color-outline) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .text-muted.bg-white {
        background-color: var(--md-sys-color-surface-container) !important;
    }

    /* Consultation layout toggles. */
    html.cemr-theme-dark .section-toggle-card span {
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark main:has(.section-toggle-card) .badge {
        color: #ffffff !important;
    }

    /* Pharmacy sales return: the area around the page. */
    html.cemr-theme-dark main:has(.pos-return-root) {
        background-color: var(--md-sys-color-surface) !important;
    }

    /* Small notes inside filled card headers follow the header's text colour. */
    html.cemr-theme-dark .card-header[class*="bg-"].text-white small {
        color: inherit;
        opacity: 1 !important;
    }

    html.cemr-theme-dark .ipd-patient-drop-list {
        color: var(--md-sys-color-on-surface);
    }

    /* Consultation: section titles and the expanded patient panel. */
    html.cemr-theme-dark:has(.topics-section) main :is(.card-header, .topic-header) :is(h1, h2, h3, h4, h5, h6, span, label) {
        color: var(--md-sys-color-on-surface);
    }

    /* Grid totals rows on any page. */
    html.cemr-theme-dark .e-grid .e-gridfooter,
    html.cemr-theme-dark .e-grid .e-summaryrow,
    html.cemr-theme-dark .e-grid .e-summaryrow .e-summarycell {
        background: var(--md-sys-color-surface-container) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    /* Stock ledger tab bar. */
    html.cemr-theme-dark .nav-pills.bg-light {
        background: var(--md-sys-color-surface-container) !important;
    }

    /* Consultation: the topic strip and quick-add labels. */
    html.cemr-theme-dark .topics-chips.bg-light {
        background: var(--md-sys-color-surface-container) !important;
    }

    html.cemr-theme-dark :is(.cc-chip-lbl, .dx-chip-lbl) {
        color: var(--md-sys-color-on-surface-variant) !important;
    }

    /* Dialogs: Bootstrap light helpers, which would otherwise stay white on a dark dialog. */
    html.cemr-theme-dark .e-dialog :is(.form-control.bg-light, .card-header.bg-light, div.bg-light, span.bg-light:not(.badge)) {
        background: var(--md-sys-color-surface-container) !important;
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .e-dialog .card-header.bg-white {
        background: var(--md-sys-color-surface-container) !important;
    }

    html.cemr-theme-dark .e-dialog :is(.text-dark:not(.badge), .text-black) {
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-dialog .text-muted {
        color: var(--md-sys-color-on-surface-variant) !important;
    }

    html.cemr-theme-dark .e-dialog .alert-secondary {
        background: var(--md-sys-color-surface-container) !important;
        border-color: var(--md-sys-color-outline-variant) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-dialog .badge.bg-secondary {
        background: var(--md-sys-color-surface-container-high) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    html.cemr-theme-dark .e-dialog .btn-outline-secondary:not(:hover):not(:active) {
        border-color: var(--md-sys-color-outline) !important;
        color: var(--md-sys-color-on-surface) !important;
    }

    /* Required-field asterisks written as an inline red. */
    html.cemr-theme-dark :is([style*="color: red"], [style*="color:red"]):not([style*="background"]) {
        color: #ffb4ab !important;
    }

    /* Consultation footer action labels. */
    html.cemr-theme-dark .rx-footer-actions .fantasy-btn,
    html.cemr-theme-dark .rx-footer-actions .fantasy-btn span {
        color: var(--md-sys-color-on-surface) !important;
    }

    /* MST biopsy dialog sets its own text and line colours on the dialog itself. */
    html.cemr-theme-dark .mst-biopsy-intervention-dialog {
        --mst-border: var(--md-sys-color-outline-variant);
        --mst-text: var(--md-sys-color-on-surface);
        --mst-muted: var(--md-sys-color-on-surface-variant);
    }

    /* POS "Clear this bill?" confirmation. */
    html.cemr-theme-dark .pos-overlay .btn-danger {
        background: #c5162f !important;
        border-color: #c5162f !important;
    }

    /* MST image dialog secondary text. */
    html.cemr-theme-dark .mst-image-selection-dialog {
        --image-muted: var(--md-sys-color-on-surface-variant);
    }

    /* MST biopsy dialog: the footer's main action matches the other filled buttons. */
    html.cemr-theme-dark .mst-dialog-footer-btn.mst-dlg-primary:not(:disabled, .e-disabled) {
        background: var(--md-sys-color-primary) !important;
        border-color: var(--md-sys-color-primary) !important;
    }

    html.cemr-theme-dark .mst-dialog-footer-btn.mst-dlg-primary:not(:disabled, .e-disabled),
    html.cemr-theme-dark .mst-dialog-footer-btn.mst-dlg-primary:not(:disabled, .e-disabled) .e-btn-icon {
        color: var(--md-sys-color-on-primary) !important;
    }

    html.cemr-theme-dark .mst-dialog-footer-btn.mst-dlg-primary:not(:disabled, .e-disabled):hover {
        background: color-mix(in srgb, var(--md-sys-color-primary) 92%, #fff) !important;
    }

    html.cemr-theme-dark .pos-overlay .btn-danger:hover {
        background: #a81227 !important;
        border-color: #a81227 !important;
    }

    /* MST image dialog sets its own text, line and panel colours on the dialog itself. */
    html.cemr-theme-dark .mst-image-selection-dialog {
        --image-primary-soft: color-mix(in srgb, var(--md-sys-color-primary) 16%, var(--md-sys-color-surface));
        --image-text: var(--md-sys-color-on-surface);
        --image-border: var(--md-sys-color-outline-variant);
        --image-border-soft: var(--md-sys-color-outline-variant);
        --image-bg: var(--md-sys-color-surface-container);
    }

    /* MST add buttons fill on hover, so their plus sign changes with them. */
    html.cemr-theme-dark .mst-accordion-add-button:is(:hover, :active) .e-icons {
        color: var(--md-sys-color-on-primary) !important;
    }

    /* IPD loading placeholders. */
    html.cemr-theme-dark .ipd-skel {
        background-image: linear-gradient(90deg, var(--md-sys-color-surface-container) 25%, var(--md-sys-color-surface-container-high) 50%, var(--md-sys-color-surface-container) 75%);
    }

    /* Green status badges keep white text, which reads better than the dark-mode text colour. */
    html.cemr-theme-dark .badge.bg-success {
        color: #fff !important;
    }

    /* Text set directly in the theme colour stays readable on dark surfaces. */
    html.cemr-theme-dark :is([style*="color:var(--ipd-primary"], [style*="color:var(--md-sys-color-primary)"]):not([style*="background"]) {
        color: var(--cemr-focus-ring) !important;
    }

    /* Billing workbench settled chip. */
    html.cemr-theme-dark .ipd-state-chip--settled {
        background: color-mix(in srgb, #2e7d32 24%, var(--md-sys-color-surface));
        color: #8ed8a4;
    }

    /* Consultation layout section names. */
    html.cemr-theme-dark .section-toggle-card .small {
        color: var(--md-sys-color-on-surface-variant) !important;
    }

    /* About page banner sits on the theme colour. */
    html.cemr-theme-dark :is(.hero-main, .hero-sub) {
        color: var(--md-sys-color-on-primary) !important;
    }

    /* Default blue link buttons. */
    html.cemr-theme-dark .btn-link:not(.text-danger, .text-success, .text-warning, .text-primary) {
        color: var(--cemr-focus-ring);
    }

    /* White Bootstrap field parts. */
    html.cemr-theme-dark .input-group-text.bg-white {
        background: var(--md-sys-color-surface-container) !important;
        border-color: var(--md-sys-color-outline-variant);
        color: var(--md-sys-color-on-surface);
    }

    html.cemr-theme-dark .form-check-input:not(:checked) {
        background-color: var(--md-sys-color-surface-container);
        border-color: var(--md-sys-color-outline);
    }
}
