/* CEMR Syncfusion component states.
   Load after every other application stylesheet. Colours come from the
   --cemr-ui-* aliases, so each state follows the current mode and custom colours.
   Check changes on the Theme Lab page at /theme-lab. */

/* Grid: a selected row must differ from the header and from a hovered row. */
.e-grid.e-control {
    --cemr-grid-selected: color-mix(in srgb, var(--cemr-ui-accent) 32%, var(--cemr-ui-surface));
}

/* Buttons: Syncfusion paints a surface-coloured border image over the border, so
   a default button looked like plain text. */
:root .e-btn {
    border-image: none !important;
}

/* A mouse click should not draw the heavy keyboard focus ring. */
:root :is(.e-accordion .e-acrdn-item .e-acrdn-header, .e-toolbar .e-toolbar-item .e-tbar-btn):focus:not(:focus-visible) {
    box-shadow: none !important;
}

/* Syncfusion uses the chosen colour directly for text in these states, which is
   unreadable when the colour is pale. Only components the app uses are listed. */
:root .e-accordion .e-acrdn-item.e-selected > .e-acrdn-header > :is(.e-acrdn-header-content, .e-toggle-icon),
:root .e-accordion .e-acrdn-item.e-select > .e-acrdn-header:hover .e-icons,
:root .e-tab .e-tab-header .e-toolbar-item.e-active .e-close-icon,
:root :is(.e-grid, .e-excelfilter) .e-filtered::before,
:root .e-pager .e-pager-default:hover,
:root .e-schedule .e-schedule-toolbar .e-hor-nav:is(:hover, :active, :focus),
:root .e-schedule :is(.e-header-cells, .e-all-day-cells, .e-day-date-header, .e-date-header-wrap td).e-current-day,
:root .e-schedule .e-current-time,
:root .e-more-popup-wrapper .e-more-event-date-header .e-current-date {
    color: var(--cemr-primary-text) !important;
}

/* Outline primary button: stop .e-primary filling it with its own text colour. */
:root .e-btn.e-outline.e-primary {
    background: transparent !important;
    color: var(--cemr-primary-text) !important;
    border-color: var(--cemr-primary-text) !important;
}

    :root .e-btn.e-outline.e-primary:is(:hover, :focus, :active, .e-active):not(:disabled):not(.e-disabled) {
        background: var(--cemr-ui-hover) !important;
    }

/* Flat primary button: stop .e-primary giving it an outline. */
:root .e-btn.e-flat.e-primary {
    color: var(--cemr-primary-text) !important;
    border-color: transparent !important;
}

/* Primary-coloured text: --cemr-primary-text stays readable with any chosen
   colour, while filled areas keep the chosen colour itself. */
:root .e-tab .e-tab-header .e-toolbar-item.e-active :is(.e-tab-text, .e-tab-icon) {
    color: var(--cemr-primary-text) !important;
}

:root .e-popup .e-list-item.e-hover:not(.e-active) {
    color: var(--cemr-ui-text) !important;
}

/* 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-dark .e-dropdownbase .e-list-item:is(.e-item-focus, .e-hover, :hover):not(.e-active) {
    color: var(--cemr-ui-text) !important;
    background-color: var(--cemr-ui-selected) !important;
}

:is(#addPatientForm, .cemr-form-page) .e-input-focus .e-float-text {
    color: var(--cemr-primary-text) !important;
}

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;
}

/* Input validation: the same ring as focus, in the colour of the state.
   Syncfusion also adds e-success with "valid" to every correct field in a form,
   so only a success state set on purpose shows the ring. */
:root :is(.e-input-group, .e-float-input).e-control-wrapper.e-error {
    --cemr-input-state: var(--cemr-ui-danger);
}

:root :is(.e-input-group, .e-float-input).e-control-wrapper.e-warning {
    --cemr-input-state: var(--cemr-ui-warning);
}

:root :is(.e-input-group, .e-float-input).e-control-wrapper.e-success {
    --cemr-input-state: var(--cemr-ui-success);
}

:root :is(.e-input-group, .e-float-input).e-control-wrapper:is(.e-error, .e-warning, .e-success:not(.valid)) {
    border-color: var(--cemr-input-state) !important;
    box-shadow: 0 0 0 1px var(--cemr-input-state) !important;
}

/* Disabled input: faded, but still readable because these fields often show
   saved values such as an MRN. */
:root :is(.e-input-group, .e-float-input).e-control-wrapper.e-disabled {
    background: color-mix(in srgb, var(--cemr-ui-text) 5%, var(--cemr-ui-surface)) !important;
    border-color: color-mix(in srgb, var(--cemr-ui-text) 38%, transparent) !important;
    color: color-mix(in srgb, var(--cemr-ui-text) 60%, transparent) !important;
    opacity: 1 !important;
}

    :root :is(.e-input-group, .e-float-input).e-control-wrapper.e-disabled :is(input, textarea, .e-delim-values) {
        color: color-mix(in srgb, var(--cemr-ui-text) 60%, transparent) !important;
        -webkit-text-fill-color: color-mix(in srgb, var(--cemr-ui-text) 60%, transparent) !important;
    }

/* Ticked and half-ticked boxes, including those in grids: the mark must stand out
   from the chosen colour in both modes. */
:root .e-frame.e-icons:is(.e-check, .e-stop) {
    background: var(--md-sys-color-primary) !important;
    border-color: var(--md-sys-color-primary) !important;
    color: var(--cemr-on-primary) !important;
}

/* Disabled checkbox. */
:root .e-checkbox-wrapper.e-checkbox-disabled .e-frame {
    background: transparent !important;
    border-color: color-mix(in srgb, var(--cemr-ui-text) 38%, transparent) !important;
}

    :root .e-checkbox-wrapper.e-checkbox-disabled .e-frame:is(.e-check, .e-stop) {
        background: color-mix(in srgb, var(--cemr-ui-text) 38%, transparent) !important;
        border-color: transparent !important;
        color: var(--cemr-ui-surface) !important;
    }

/* Disabled radio button. */
:root .e-radio-wrapper .e-radio:disabled + label::before {
    border-color: color-mix(in srgb, var(--cemr-ui-text) 38%, transparent) !important;
}

:root .e-radio-wrapper .e-radio:disabled:checked + label::after {
    background: color-mix(in srgb, var(--cemr-ui-text) 38%, transparent) !important;
}

/* Charts draw their colours as SVG attributes, so the mode only reaches them
   through these rules. Series colours stay as each chart sets them, except the
   default dark grey, which cannot be seen on a dark surface. */
: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;
}

/* Schedule: appointments are filled with the chosen colour. */
:root .e-schedule .e-appointment :is(.e-subject, .e-time, .e-location, .e-icons) {
    color: var(--cemr-on-primary) !important;
}

:root .e-schedule .e-schedule-toolbar .e-active-view :is(.e-tbar-btn-text, .e-icons) {
    color: var(--cemr-primary-text) !important;
}

/* Rich text editor: disabled text may be saved notes, so it stays readable. */
:root .e-richtexteditor .e-rte-content a {
    color: var(--cemr-primary-text);
}

:root .e-richtexteditor.e-disabled {
    opacity: .6;
}

/* 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;
}

/* Splitter: show that the bar can be dragged. */
:root .e-splitter .e-split-bar:is(:hover, .e-split-bar-hover, .e-split-bar-active) {
    background: var(--cemr-primary-text) !important;
}

/* Pressed: every action responds while the pointer is down. */
:root :is(a[href], a.e-link, button, .btn, .e-btn, [role="button"], summary):not(:disabled, .e-disabled, [aria-disabled="true"]):active {
    filter: brightness(0.92);
}

html.cemr-theme-dark :is(a[href], a.e-link, button, .btn, .e-btn, [role="button"], summary):not(:disabled, .e-disabled, [aria-disabled="true"]):active {
    filter: brightness(1.2);
}

/* Keyboard focus: grid headers and checkboxes. A checkbox's input is hidden, so its box shows the ring. */
html.cemr-theme .e-grid .e-headercell:focus-visible {
    outline: 2px solid var(--cemr-focus-ring) !important;
    outline-offset: -2px !important;
}

html.cemr-theme .e-checkbox-wrapper input:focus-visible ~ .e-frame {
    outline: 2px solid var(--cemr-focus-ring) !important;
    outline-offset: 2px !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,
html.cemr-theme .ipd-page-container :is(select, textarea, input:not([class*="e-"])):focus-visible {
    outline: 2px solid var(--cemr-focus-ring) !important;
    outline-offset: 1px !important;
}

html.cemr-theme .ipd-page-container .e-input-group.e-input-focus {
    border-color: var(--cemr-focus-ring) !important;
    box-shadow: 0 0 0 1px var(--cemr-focus-ring) !important;
}

/* In dark mode the primary colour is too dark to mark a focused field. */
html.cemr-theme-dark :is(.e-input-group, .e-float-input):is(.e-input-focus, :focus-within):not(.e-disabled) {
    border-color: var(--cemr-focus-ring) !important;
    box-shadow: 0 0 0 1px var(--cemr-focus-ring) !important;
}

html.cemr-theme-dark :is(.e-input-group, .e-float-input):is(.e-input-focus, :focus-within)::after {
    background-color: var(--cemr-focus-ring) !important;
}

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;
}

/* A dropdown showing a validation error still shows focus. */
html.cemr-theme .e-ddl.e-error:focus-visible {
    outline: 2px solid var(--cemr-focus-ring) !important;
    outline-offset: 1px !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;
}

/* Filled primary buttons in dark mode: hover stayed too pale for white text. */
html.cemr-theme-dark .e-btn.e-primary:not(.e-outline, .e-flat, :disabled, .e-disabled):is(:hover, :focus, :active, .e-active) {
    background: color-mix(in srgb, var(--md-sys-color-primary) 92%, #ffffff) !important;
    border-color: color-mix(in srgb, var(--md-sys-color-primary) 92%, #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;
}

/* Grid buttons darken on hover, so pressing has to go further. */
.e-grid.e-control .e-btn:not(:disabled):not(.e-disabled):active {
    filter: brightness(.86);
}

html.cemr-theme-dark .e-grid.e-control .e-btn:not(:disabled):not(.e-disabled):active {
    filter: brightness(1.25);
}

/* Grid filter boxes in dark mode: the primary colour is too dark to mark focus. */
html.cemr-theme-dark .e-grid.e-control .e-filterbarcell .e-input-group.e-input-focus {
    border-bottom-color: var(--cemr-focus-ring) !important;
}

/* Pharmacy grids: a hovered row turned pale under light text in dark mode. */

/* Hover: fields darken their border. Focus, errors and disabled fields keep their own look. */
html.cemr-theme :is(.e-input-group, .e-float-input, .e-ddl):not(.e-disabled, .e-error, .e-input-focus, :focus-within):hover,
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: sortable grid headers and small icons inside fields and headers. */
html.cemr-theme .e-grid.e-control .e-gridheader .e-headercell.e-mousepointer:hover,
html.cemr-theme .e-grid.e-control .e-columnmenu:hover,
html.cemr-theme .e-input-group .e-input-group-icon:not(.e-disabled):hover {
    background-color: var(--cemr-ui-hover) !important;
}

/* Hover: plain links inside pages. */
html.cemr-theme :is(main a:not([class]), .breadcrumb a):hover {
    text-decoration: underline;
}

/* Hover: page tabs and list rows that had none. */
html.cemr-theme :is(.pos-mode-tab, .discharge-tab-btn, .designer-tab, .template-row, .lab-choice, .nav-link.rounded-pill):not(.active, .is-active, .e-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, .e-appointment):hover {
    filter: brightness(0.96);
}

html.cemr-theme-dark :is(.btn-review-print, .btn-outline-light, .permission-btn, .ward-item, .section-toggle-card, .e-appointment):hover {
    filter: brightness(1.12);
}

/* 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);
}

/* Hover: grid filter boxes have almost no border, so they tint instead. */
html.cemr-theme .e-grid.e-control .e-filterbarcell .e-input-group:not(.e-disabled, .e-input-focus, :focus-within):hover {
    background-color: var(--cemr-ui-hover) !important;
}

/* Consultation: the expanded patient panel sits on the theme-coloured header, so it needs its own surface. */
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;
}

/* 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;
}

/* 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;
}

/* Dialog header: title on the left, close button on the right, both centred
   on one line. Syncfusion floats the close button with a 38px min-height,
   which sat the X a few pixels below the title. Dialogs with their own
   !important header rules (e.g. .mst-selection-list-dialog) keep them. */
.e-dialog .e-dlg-header-content {
    display: flex;
    align-items: center;
    gap: 8px;
}

.e-dialog .e-dlg-header-content > .e-dlg-header {
    order: 1;
    flex: 1 1 auto;
    min-width: 0;
}

.e-dialog .e-dlg-header-content > .e-btn.e-dlg-closeicon-btn {
    order: 2;
    float: none;
    flex: 0 0 32px;
    width: 32px;
    height: 32px;
    min-width: 0;
    min-height: 0;
    margin: 0 -6px 0 auto;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

.e-dialog .e-dlg-header-content > .e-btn.e-dlg-closeicon-btn .e-icon-dlg-close {
    margin: 0;
    line-height: 1;
}
