/* CEMR application styles: dark-mode and state fixes for non-Syncfusion markup (Pharmacy, outline
   buttons, links). Load after ipd.css.
   Moved from cemr-syncfusion-states.css.
   Bootstrap 5.3 theme switch: Syncfusion controls (.e-*) are styled only by the
   Syncfusion theme and theme-manager.js. Do not add .e-* rules here. */

/* Dark mode: a focused or hovered dropdown item was pale with primary-coloured text,
   or barely different from its neighbours. The chosen item keeps its filled colour. */

html.cemr-theme-light .text-primary:not([class*="bg-"]) {
    color: var(--cemr-primary-text) !important;
}

:root .page-header-breadcrumb .breadcrumb-item :is(a, i) {
    color: var(--cemr-primary-text) !important;
}

.cemr-theme .btn-outline-primary:not(:hover):not(:active),
:is(#addPatientForm, .cemr-form-page) .btn.btn-outline-primary:not(:hover):not(:active) {
    color: var(--cemr-primary-text) !important;
    border-color: var(--cemr-primary-text) !important;
}

/* Audiograms draw fixed dark lines, so each chart stays on a light card in dark mode. */

html.cemr-theme-dark .specialty-card .chart-container {
    background: #ffffff;
    border-radius: 8px;
    --cemr-ui-text: #1c1d1f;
    --cemr-ui-border: #cbcdd0;
    --cemr-text: #1c1d1f;
    --cemr-surface: #ffffff;
    --cemr-surface-soft: #f7f7f8;
    --md-sys-color-surface: #ffffff;
    --md-sys-color-on-surface: #1c1d1f;
    --md-sys-color-on-surface-variant: #48494e;
    --cemr-ui-muted: #48494e;
    --cemr-primary-text: color-mix(in srgb, var(--md-sys-color-primary) 45%, #000000);
    --bs-table-color: #1c1d1f;
    color: #1c1d1f;
}

/* The card sets its own fixed purple, which clashes with the text of a pale colour. */

:root .specialty-card .btn-primary {
    background: var(--md-sys-color-primary);
    border-color: var(--md-sys-color-primary);
    color: var(--cemr-on-primary);
}

/* Bootstrap tables keep dark text unless told otherwise. */

html.cemr-theme-dark .table {
    --bs-table-color: var(--cemr-ui-text);
    color: var(--cemr-ui-text);
}

/* Card headers and table headings use this soft surface, which had no dark value. */

html.cemr-theme-dark {
    --cemr-surface-soft: var(--md-sys-color-surface-container);
}

/* IPD vitals: reading tiles and min/max rows have a fixed light surface. */

html.cemr-theme-dark :is(.quick-vitals-grid .vcard, .range-summary .srow) {
    background: var(--md-sys-color-surface-container);
    border-color: var(--cemr-ui-border);
}

:root .range-summary .min-tag {
    color: var(--cemr-ui-text);
    background: color-mix(in srgb, var(--md-sys-color-primary) 22%, var(--cemr-ui-surface));
}

/* Pharmacy dashboard: status text sits on pale pills in both modes, and the other
   text colours have no dark values. */

:is(.pharmacy-page, .pharmacy-bill-dialog, .pharmacy-suspend-dialog) {
    --pharmacy-success: #12703f;
    --pharmacy-warning: #8a5003;
    --pharmacy-danger: #b3261e;
    --pharmacy-muted: #5b6575;
}

:root .pharmacy-btn-primary:not(:disabled) {
    color: var(--cemr-on-primary) !important;
}

:root .pharmacy-page .panel-link-button {
    color: var(--cemr-primary-text) !important;
}

:root .pharmacy-page .patient-avatar {
    color: var(--cemr-ui-text);
    background: color-mix(in srgb, var(--md-sys-color-primary) 22%, var(--cemr-ui-surface));
}

html.cemr-theme-dark :is(.pharmacy-page, .pharmacy-bill-dialog, .pharmacy-suspend-dialog) {
    --pharmacy-text: var(--cemr-on-background);
    --pharmacy-muted: var(--md-sys-color-on-surface-variant);
}

html.cemr-theme-dark .pharmacy-page :is(.grid-action-button, .bill-report-button) {
    background: var(--md-sys-color-surface-container) !important;
    color: var(--cemr-on-background) !important;
}

/* The chart takes fixed colours from its markup, so the chosen colour is applied here in both modes. */

:root .pharmacy-revenue-chart svg [stroke="#2a6b82" i] {
    stroke: var(--cemr-primary-text) !important;
}

:root .pharmacy-revenue-chart svg [fill="#2a6b82" i] {
    fill: var(--cemr-primary-text) !important;
}

/* Keyboard focus: page fields that remove the browser outline or draw a faint border. */

html.cemr-theme :is(.pos-meta-select, .pos-meta-date-inp, .pos-disc-type-sel, .pos-disc-val-inp, .discharge-input, .discharge-textarea, .form-check-input, .ipd-patient-drop-list):focus-visible {
    outline: 2px solid var(--cemr-focus-ring) !important;
    outline-offset: 1px !important;
}

/* In dark mode the primary colour is too dark to mark a focused field. */

html.cemr-theme-dark :is(.pos-pt-search-box, .pos-entry-search-wrap):focus-within,
html.cemr-theme-dark :is(.pos-pay-sel, .pos-pay-inp, .pos-entry-inp, .pos-field-select, .pos-field-input):focus {
    border-color: var(--cemr-focus-ring) !important;
    box-shadow: 0 0 0 1px var(--cemr-focus-ring) !important;
}

/* The ring would vanish against a filled header in the same colour. */

html.cemr-theme [class*="bg-"].text-white :is(a, button):focus-visible {
    outline-color: #ffffff !important;
}

/* Plain text links in dark mode: the primary colour is too dark to read on hover. */

html.cemr-theme-dark :is(main a:not([class]), .breadcrumb a):hover {
    color: var(--cemr-focus-ring) !important;
}

/* Bootstrap outline buttons: app.css uses a green and a red that miss 4.5:1 against white. */

html.cemr-theme .btn-outline-success:not(:hover, :focus, :active, .active) {
    border-color: #146c43 !important;
    color: #146c43 !important;
}

html.cemr-theme .btn-outline-success:is(:hover, :focus, :active, .active) {
    background-color: #146c43 !important;
    border-color: #146c43 !important;
    box-shadow: none;
    color: #ffffff !important;
}

html.cemr-theme .btn-outline-danger:not(:hover, :focus, :active, .active) {
    border-color: #c5162f !important;
    color: #c5162f !important;
}

html.cemr-theme .btn-outline-danger:is(:hover, :focus, :active, .active) {
    background-color: #c5162f !important;
    border-color: #c5162f !important;
    box-shadow: none;
    color: #ffffff !important;
}

html.cemr-theme-dark .btn-outline-success:not(:hover, :focus, :active, .active) {
    border-color: #8ed8a4 !important;
    color: #8ed8a4 !important;
}

html.cemr-theme-dark .btn-outline-success:is(:hover, :focus, :active, .active) {
    background-color: #8ed8a4 !important;
    border-color: #8ed8a4 !important;
    color: #10201a !important;
}

html.cemr-theme-dark .btn-outline-danger:not(:hover, :focus, :active, .active) {
    border-color: #ffb4ab !important;
    color: #ffb4ab !important;
}

html.cemr-theme-dark .btn-outline-danger:is(:hover, :focus, :active, .active) {
    background-color: #ffb4ab !important;
    border-color: #ffb4ab !important;
    color: #2b0f0c !important;
}

/* Hover: fields darken their border. Focus, errors and disabled fields keep their own look. */

html.cemr-theme :is(select, textarea, .form-control, .form-select, .form-check-input, .discharge-input, .discharge-textarea, .pos-field-select, .pos-field-input, .pos-pay-sel, .pos-pay-inp):not(:disabled, [readonly], :focus):hover,
html.cemr-theme :is(.global-search, .pos-pt-search-box, .pos-entry-search-wrap, :has(> .ipd-patient-switcher-inp)):not(:focus-within):hover {
    border-color: var(--md-sys-color-on-surface-variant) !important;
}

/* Hover: plain links inside pages. */

html.cemr-theme :is(main a:not([class]), .breadcrumb a):hover {
    text-decoration: underline;
}

/* Stock ledger tabs: the unselected label was too dim in dark mode. */

html.cemr-theme-dark .nav-link.rounded-pill:not(.active) {
    color: var(--md-sys-color-on-surface);
}

/* Role permissions: filter tabs answer the pointer, and the clone-role list shows keyboard focus. */

html.cemr-theme .filter-tab:not(.active):hover {
    background: var(--cemr-ui-hover) !important;
}

html.cemr-theme .filter-tab.active:hover {
    filter: brightness(1.12);
}

html.cemr-theme .clone-select:focus-visible {
    outline: 2px solid var(--cemr-focus-ring);
    outline-offset: 2px;
}

/* =========================================================================
   Restored in Phase 5. The Phase 2 extraction dropped these with the
   Syncfusion overrides they shared a rule with. They style plain elements,
   or chart SVG (charts take colours from code, not from the theme CSS).
   ========================================================================= */

/* Charts: transparent plot border, readable axis and legend text in light and dark. */
:root .e-chart svg rect[id$="_ChartBorder"] {
    fill: transparent !important;
}

:root .e-chart svg :is(text[id*="_AxisLabel_"], text[id*="_AxisTitle"], text[id*="_ChartTitle"], text[id*="_legend_text_"], text[id*="_stripline_"]) {
    fill: var(--cemr-ui-text) !important;
}

:root .e-chart svg :is(path[id*="MajorGridLine"], path[id*="MinorGridLine"]) {
    stroke: var(--cemr-ui-border) !important;
}

html.cemr-theme-dark .e-chart svg [fill="#404041" i] {
    fill: var(--cemr-ui-text) !important;
}

html.cemr-theme-dark .e-chart svg [stroke="#404041" i] {
    stroke: var(--cemr-ui-text) !important;
}

/* Press feedback on plain links and buttons (Syncfusion buttons use the theme's own). */
:root :is(a[href], button, .btn, [role="button"], summary):not(.e-btn, .e-link, :disabled, .e-disabled, [aria-disabled="true"]):active {
    filter: brightness(0.92);
}

html.cemr-theme-dark :is(a[href], button, .btn, [role="button"], summary):not(.e-btn, .e-link, :disabled, .e-disabled, [aria-disabled="true"]):active {
    filter: brightness(1.2);
}

/* Hover feedback on CEMR tabs, rows and tiles. */
html.cemr-theme :is(.pos-mode-tab, .discharge-tab-btn, .designer-tab, .template-row, .lab-choice, .nav-link.rounded-pill):not(.active, .is-active, [aria-selected="true"], :disabled):hover {
    background-color: var(--cemr-ui-hover);
}

html.cemr-theme :is(.btn-review-print, .btn-outline-light, .permission-btn, .ward-item, .section-toggle-card):hover {
    filter: brightness(0.96);
}

html.cemr-theme-dark :is(.btn-review-print, .btn-outline-light, .permission-btn, .ward-item, .section-toggle-card):hover {
    filter: brightness(1.12);
}

/* Consultation sidebar: Bootstrap cards inside the accordion follow the CEMR surface and text colours. */
html.cemr-theme .e-acrdn-panel:has(> .e-acrdn-content > .sidebar-content) {
    background: var(--md-sys-color-surface-container) !important;
    color: var(--md-sys-color-on-surface);
}

html.cemr-theme .e-acrdn-content > .sidebar-content .card {
    background: var(--md-sys-color-surface) !important;
    color: var(--md-sys-color-on-surface);
}

html.cemr-theme .e-acrdn-content > .sidebar-content :is(h1, h2, h3, h4, h5, h6, span, label, small, p):not(.badge, .btn, .btn *, .text-danger) {
    color: var(--md-sys-color-on-surface) !important;
}

html.cemr-theme .e-acrdn-content > .sidebar-content .badge-soft-primary {
    color: var(--cemr-primary-text) !important;
}

/* Radio buttons show keyboard focus on their label. */
html.cemr-theme input.e-radio:focus-visible + label {
    outline: 2px solid var(--cemr-focus-ring);
    outline-offset: 2px;
    border-radius: 4px;
}
