/* Application chrome only: mail/document paper keeps its original light rendering. */
@media screen {
    html[data-theme="light"] {
        color-scheme: light;
    }

    /* FullCalendar may set its own scheme on a nested root. Manual choice wins. */
    html[data-theme="light"],
    html[data-theme="light"] [data-color-scheme] {
        --fc-monarch-primary: #415f91;
        --fc-monarch-primary-foreground: #fff;
        --fc-monarch-primary-over: #526e9c;
        --fc-monarch-primary-down: #647ea7;
        --fc-monarch-secondary: #d6e3ff;
        --fc-monarch-secondary-foreground: #284777;
        --fc-monarch-secondary-over: #c9d6f2;
        --fc-monarch-secondary-down: #bdc9e5;
        --fc-monarch-tertiary: #855d8c;
        --fc-monarch-tertiary-foreground: #fff;
        --fc-monarch-tertiary-over: #916c97;
        --fc-monarch-tertiary-down: #9d7ca2;
        --fc-monarch-event: var(--fc-monarch-primary);
        --fc-monarch-event-contrast: var(--fc-monarch-primary-foreground);
        --fc-monarch-highlight: #d6e3ff4d;
        --fc-monarch-now: #ba1a1a;
        --fc-monarch-selected: #565f71;
        --fc-monarch-selected-foreground: #fff;
        --fc-monarch-selected-over: #656e7e;
        --fc-monarch-selected-down: #757d8c;
        --fc-monarch-outline: #6687ff;
        --fc-monarch-popover: #fff;
        --fc-monarch-popover-foreground: #020617;
        --fc-monarch-background: #fff;
        --fc-monarch-faint: #64748b1a;
        --fc-monarch-muted: #64748b26;
        --fc-monarch-strong: #64748b4d;
        --fc-monarch-stronger: #64748b59;
        --fc-monarch-strongest: #64748b66;
        --fc-monarch-foreground: #020617;
        --fc-monarch-faint-foreground: #bebebe;
        --fc-monarch-muted-foreground: #8d8d8d;
        --fc-monarch-strong-foreground: #020617;
        --fc-monarch-border: #dddee1;
        --fc-monarch-strong-border: #c2c3c7;
    }

    html[data-theme="dark"] {
        color-scheme: dark;
        color: #e0e9f3;
        background: #111b29;
        --surface: #1a283a;
        --surface-raised: #223349;
        --surface-soft: #182536;
        --control-bg: #142234;
        --text: #e0e9f3;
        --text-muted: #abbdd1;
        --text-accent: #a9d1f6;
        --line: #34485f;
        --border: #34485f;
        --muted: #abbdd1;
        --blue: #80b7e8;
        --navy: #142235;
        --table-base-bg: #1a283a;
        --table-heading-bg: #253d54;
        --table-heading-ink: #c9e2f7;
        --table-heading-line: #42607a;
        --table-row-line: #314358;
        --table-stripe-bg: #1e3043;
        --table-hover-bg: #24413f;
        --table-focus-bg: #283d58;
        --table-selected-bg: #2b4765;
        accent-color: #78b4ea;
        scrollbar-color: #61758e #172333;
    }

    html[data-theme="dark"] :is(body, .startup) {
        background: #111b29;
        color: var(--text);
    }

    html[data-theme="dark"] :is(.topbar, .data-card, dialog, .login-card, .calendar-panel,
        .security-card, .security-metric, .communications-settings-card, .lookup-results) {
        color: var(--text);
        background: var(--surface);
        border-color: var(--line);
    }

    html[data-theme="dark"] dialog {
        box-shadow: 0 20px 80px #0008;
    }

    html[data-theme="dark"] dialog::backdrop {
        background: #050b16aa;
    }

    html[data-theme="dark"] :is(.dialog-header, .dialog-footer) {
        color: var(--text);
        background: var(--surface-raised);
        border-color: var(--line);
    }

    html[data-theme="dark"] :is(input, select, textarea) {
        color: var(--text);
        background-color: var(--control-bg);
        border-color: #4b627c;
    }

    html[data-theme="dark"] :is(input, textarea)::placeholder {
        color: #97abc2;
        opacity: 1;
    }

    html[data-theme="dark"] :is(input, textarea):focus {
        border-color: #9ac6eb;
    }

    html[data-theme="dark"] :is(input[readonly], textarea[readonly], input:disabled, select:disabled),
    html[data-theme="dark"] .billing-dialog input:read-only,
    html[data-theme="dark"] .billing-adjustment input:disabled {
        color: var(--text-muted);
        background: #243244;
    }

    html[data-theme="dark"] :is(button, a, input, select, textarea, summary):focus-visible {
        outline-color: #efc17c;
    }

    html[data-theme="dark"] :is(.secondary, .icon-button, .action-icon, .row-button,
        .appearance-button, .font-size-button) {
        color: #c0d6ee;
        border-color: #4a627b;
        background: var(--surface-raised);
    }

    html[data-theme="dark"] :is(.secondary, .icon-button, .action-icon, .row-button,
        .appearance-button, .font-size-button):hover:not(:disabled) {
        color: #eef6ff;
        border-color: #799abd;
        background: #304962;
    }

    html[data-theme="dark"] :is(.font-size-control, .appearance-controls) {
        border-color: var(--line);
    }

    html[data-theme="dark"] .font-size-control {
        background: var(--surface-soft);
    }

    html[data-theme="dark"] .login-logo {
        background: #fff;
        border-radius: 6px;
    }

    html[data-theme="dark"] :is(.font-size-button, .appearance-button)[aria-pressed="true"] {
        color: #d4eaff;
        background: #345879;
        border-color: #79a3cb;
    }

    html[data-theme="dark"] .primary {
        color: #fff;
        background: #365f87;
        border-color: #668bb1;
    }

    html[data-theme="dark"] .primary:hover:not(:disabled) {
        background: #426f9b;
    }

    html[data-theme="dark"] .login-card .primary {
        color: #203248;
        background: #e9a346;
        border-color: #e9a346;
    }

    html[data-theme="dark"] :is(.danger, .list-error) {
        color: #ffb8b8;
        background: #432c38;
        border-color: #a1616c;
    }

    html[data-theme="dark"] :is(.danger:hover:not(:disabled), .action-icon.action-delete:hover) {
        color: #ffcece;
        background: #59313e;
    }

    html[data-theme="dark"] :is(.error, .action-icon.action-delete, .billing-delete) {
        color: #ffb1b1;
    }

    html[data-theme="dark"] :is(.notice, .demo-pill, .billing-note) {
        color: #ecd09e;
        background: #382f23;
        border-color: #756344;
    }

    html[data-theme="dark"] :is(.eyebrow, .page-heading .eyebrow, .page-heading p:last-child,
        .breadcrumb, .account, .font-size-control, .pagination, .list-summary, .workspace-footer,
        .form-field label, .form-field small, .login-card > p, .login-card label, .login-card > small,
        .group-help, .documents-status, .documents-pager, .upload-drop > small,
        .upload-item-label small, .upload-item-status, .document-details small,
        .document-preview-body small, .filter-content > small, .filter-join, .export-help,
        .calendar-toolbar .calendar-status, .calendar-legend,
        .security-status, .security-metric > span, .security-metric small, .security-help,
        .security-cell small, .security-reference, .security-pager, .security-empty,
        .security-field, .security-policy-body p, .security-note,
        .communications-settings-help, .communications-settings-switch small,
        .communications-settings-field, .communications-settings-note p, .mail-preview-position) {
        color: var(--text-muted);
    }

    html[data-theme="dark"] :is(.breadcrumb span:last-child, .empty-state h2,
        .schedule-description, .schedule-total, .billing-state, .communications-settings-card h2,
        .communications-settings-note strong, .export-menu > label, .filter-panel > summary) {
        color: var(--text-accent);
    }

    html[data-theme="dark"] :is(.empty-state, .list-summary #resultCount, .list-summary #searchHint) {
        color: var(--text-muted);
    }

    html[data-theme="dark"] .toolbar .quick-search {
        background: var(--control-bg);
        border-color: #4b627c;
    }

    html[data-theme="dark"] .quick-search input {
        background: transparent;
    }

    html[data-theme="dark"] .quick-search > span {
        color: var(--text-muted);
    }

    html[data-theme="dark"] .quick-search:focus-within {
        border-color: #90bce2;
        box-shadow: 0 0 0 2px #7db8eb33;
    }

    html[data-theme="dark"] :is(kbd, #columnOptions, .toolbar .export-menu) {
        color: var(--text-muted);
        background: var(--surface-raised);
        border-color: var(--line);
    }

    html[data-theme="dark"] .export-menu button {
        color: var(--text-accent);
    }

    html[data-theme="dark"] :is(.export-menu button:hover, .lookup-results button:hover,
        .lookup-results button:focus, .toolbar details[open] > summary) {
        background: #304962;
        color: #d4eaff;
        border-color: #799abd;
    }

    html[data-theme="dark"] .filter-panel {
        background: var(--surface-soft);
        border-color: var(--line);
    }

    html[data-theme="dark"] :is(.applied-chip, .filter-chip, .lookup-chip,
        .toolbar .filter-toggle.has-filters, .schedule-preview:not(:empty),
        .billing-summary, .security-count) {
        color: #bedcf7;
        background: #263e57;
        border-color: #476682;
    }

    html[data-theme="dark"] :is(.applied-chip button, .clear-filters) {
        color: #abd4fa;
    }

    html[data-theme="dark"] :is(.table-area, .filter-footer, .filter-group, .weekly-schedule,
        .assessment-scroll, .upload-item, .documents-toolbar, .documents-list, .document-row,
        .billing-lines-scroll, .billing-line-editor, .billing-calculated-row,
        .calendar-readonly-report, .export-menu #exportSoft) {
        border-color: var(--line);
    }

    /* Data tables retain stripe/hover/focus and opaque frozen-cell backgrounds. */
    html[data-theme="dark"] :is(.data-card .table-area > table, .assessment-scroll > table,
        .billing-lines-scroll > table, .security-table-scroll > table,
        .document-preview-body > table, table.calendar-attendance) {
        color: var(--text);
    }

    html[data-theme="dark"] :is(.data-card .table-area > table, .assessment-scroll > table,
        .billing-lines-scroll > table, .security-table-scroll > table,
        .document-preview-body > table, table.calendar-attendance) td:first-child {
        color: var(--text-muted);
    }

    html[data-theme="dark"] :is(.data-card .table-area > table, .assessment-scroll > table,
        .billing-lines-scroll > table, .security-table-scroll > table,
        .document-preview-body > table, table.calendar-attendance) td:nth-child(2) {
        color: var(--text);
    }

    html[data-theme="dark"] .upload-drop {
        background: #1a3044;
        border-color: #6686a7;
    }

    html[data-theme="dark"] .upload-drop.dragging {
        background: #294d69;
        border-color: #9acbef;
    }

    html[data-theme="dark"] :is(.document-unavailable, .billing-instructions) {
        color: #e7c38c;
    }

    html[data-theme="dark"] .billing-tabs button[aria-pressed="true"] {
        background: #294b69;
        color: #cae5fc;
        border-color: #85b5dd;
    }

    html[data-theme="dark"] .security-metric.good strong {
        color: #8dd8b8;
    }

    html[data-theme="dark"] .security-metric.warning strong {
        color: #efcb89;
    }

    html[data-theme="dark"] .communications-settings-switch input {
        background: #45556a;
        border-color: #71849c;
    }

    html[data-theme="dark"] .communications-settings-switch input:checked {
        background: #438782;
        border-color: #8ac1b9;
    }

    html[data-theme="dark"] .communications-settings-status {
        color: #bcdcf8;
        background: #263e57;
    }

    html[data-theme="dark"] :is(.communications-settings-status, .communications-settings-mode).is-demo {
        color: #a8ded1;
        background: #25413d;
    }

    html[data-theme="dark"] :is(.communications-settings-status, .communications-settings-mode).is-real {
        color: #efcd93;
        background: #463622;
    }

    /* Composer chrome is dark; its editable email remains white paper. */
    html[data-theme="dark"] .mail-dialog .dialog-header,
    html[data-theme="dark"] .mail-preview-window > .dialog-header {
        color: #c1def7;
        background: #243b53;
        border-color: #42617d;
    }

    html[data-theme="dark"] :is(.mail-dialog .mail-editor, .mail-preview-body) {
        background: #152232;
    }

    html[data-theme="dark"] .mail-dialog .mail-section {
        --mail-accent: #70a9d4;
        --mail-tint: #243b53;
        background: var(--surface);
        border-color: var(--line);
        border-top-color: var(--mail-accent);
    }

    html[data-theme="dark"] .mail-dialog .mail-section-content {
        --mail-accent: #79bdb0;
        --mail-tint: #23413e;
    }

    html[data-theme="dark"] .mail-dialog .mail-section-recipients {
        --mail-accent: #b19ad6;
        --mail-tint: #352f48;
    }

    html[data-theme="dark"] .mail-dialog .mail-section-actions {
        --mail-accent: #d5ae6e;
        --mail-tint: #423626;
    }

    html[data-theme="dark"] .mail-dialog .mail-section-status {
        --mail-accent: #98b1ca;
        --mail-tint: #27384a;
    }

    html[data-theme="dark"] .mail-dialog .mail-section > h3 {
        background: var(--mail-tint);
        color: var(--text);
    }

    html[data-theme="dark"] .mail-send-dialog > form > .mail-editor > .mail-section > h3 {
        background: transparent;
    }

    html[data-theme="dark"] .mail-dialog .weekly-schedule {
        background: #272638;
        border-color: #635a7f;
    }

    html[data-theme="dark"] .mail-dialog .weekly-schedule legend {
        color: #d2bfe9;
    }

    html[data-theme="dark"] .mail-dialog .mail-token-tools {
        background: #213b38;
        border-color: #507b71;
    }

    html[data-theme="dark"] .mail-send-dialog > form > .mail-editor .mail-token-title {
        color: #b0ded4;
    }

    html[data-theme="dark"] .mail-field-pill {
        color: #cae5ff;
        background: #294760;
        border-color: #6d9cbd;
    }

    html[data-theme="dark"] .mail-dialog :is(.mail-progress span, .mail-config-status > .mail-status),
    html[data-theme="dark"] .mail-recipient-pills > span {
        color: #c1dcf4;
        background: #273f57;
        border-color: #4c6b87;
    }

    html[data-theme="dark"] .mail-dialog :is(.mail-section-status .mail-status,
        .mail-section-config > .group-field:has(input[type=checkbox])) {
        color: #c1dcf4;
    }

    html[data-theme="dark"] .mail-dialog .mail-preview:not(:empty) {
        background: #203b35;
        border-color: #4e8d7e;
    }

    html[data-theme="dark"] .mail-dialog .mail-preview > h3 {
        color: #b0ded4;
    }

    html[data-theme="dark"] :is(.mail-preview-envelope, .mail-json-view) {
        color: var(--text);
        background: var(--surface);
        border-color: var(--line);
    }

    html[data-theme="dark"] :is(.mail-rich-editor, .mail-rich-toolbar, .mail-preview-paper, .mail-preview-window) {
        border-color: #536b84;
    }

    html[data-theme="dark"] .mail-preview-paper.assessment-scroll {
        background: var(--table-base-bg);
        color: var(--text);
    }

    /* Do not recolor saved message HTML, rendered previews, or document sheets. */
    html[data-theme="dark"] :is(.mail-rich-content, .mail-preview-paper:not(.assessment-scroll), .tripartita-contents) {
        color-scheme: light;
        background-color: #fff;
        color: #25344b;
        --border: #d9e2ee;
        scrollbar-color: #a1a9b2 #f1f3f5;
    }

    /* TinyMCE toolbar, popovers and dialogs (the editable surface stays light above). */
    html[data-theme="dark"] .tox {
        color: var(--text);
    }

    html[data-theme="dark"] .tox :is(.tox-editor-header, .tox-toolbar-overlord,
        .tox-toolbar, .tox-toolbar__primary, .tox-toolbar__overflow, .tox-menubar,
        .tox-menu, .tox-dialog, .tox-dialog__header, .tox-dialog__body,
        .tox-dialog__footer, .tox-statusbar, .tox-pop, .tox-pop__dialog, .tox-sidebar-wrap,
        .tox-collection--list, .tox-listboxfield .tox-listbox--select, .tox-insert-table-picker) {
        color: var(--text);
        background: var(--surface-raised);
        border-color: #4b627c;
    }

    html[data-theme="dark"] .tox :is(.tox-tbtn, .tox-mbtn, .tox-collection__item,
        .tox-label, .tox-dialog__title, .tox-dialog__body-nav-item, .tox-toolbar-label,
        .tox-checkbox__label, .tox-listbox__select-label, .tox-statusbar a,
        .tox-statusbar__path-item, .tox-statusbar__wordcount, .tox-help__more-link,
        .tox-dialog__table, .tox-dialog__body-content, .tox-button--naked,
        .tox-insert-table-picker__label, .tox-collection__item-accessory) {
        color: var(--text);
    }

    html[data-theme="dark"] .tox .tox-collection__group-heading {
        color: var(--text-muted);
        background: var(--surface-soft);
    }

    html[data-theme="dark"] .tox .tox-dialog__body-content .tox-form__group :is(h1, h2) {
        color: var(--text);
    }

    html[data-theme="dark"] .tox :is(.tox-tbtn, .tox-mbtn, .tox-collection__item,
        .tox-button--naked) svg {
        fill: currentColor;
    }

    html[data-theme="dark"] .tox :is(.tox-textfield, .tox-textarea, .tox-selectfield select,
        .tox-listboxfield .tox-listbox--select, .tox-custom-editor, .tox-toolbar-textfield,
        .tox-number-input input, .tox-number-input input:hover, .tox-number-input input:focus,
        .tox-number-input button) {
        color: var(--text);
        background: var(--control-bg);
        border-color: #718aa5;
    }

    html[data-theme="dark"] .tox :is(.tox-tbtn:hover, .tox-tbtn:focus,
        .tox-tbtn--enabled, .tox-tbtn--enabled:hover, .tox-mbtn:hover,
        .tox-mbtn--active, .tox-collection__item--active, .tox-collection__item--enabled,
        .tox-collection__item--active:not(.tox-collection__item--state-disabled),
        .tox-collection__item--enabled:not(.tox-collection__item--state-disabled)) {
        color: #e4f1ff;
        background: #3a5674;
    }

    html[data-theme="dark"] .tox :is(.tox-tbtn--disabled, .tox-tbtn--disabled:hover,
        .tox-collection__item--state-disabled) {
        color: #8c9caf;
        background: transparent;
    }

    html[data-theme="dark"] .tox :is(.tox-toolbar__group, .tox-dialog__footer,
        .tox-collection__group, .tox-statusbar, .tox-dialog__body-nav) {
        border-color: #4b627c;
    }

    html[data-theme="dark"] .tox :is(.tox-button--secondary,
        .tox-button--secondary:hover:not(:disabled), .tox-button--secondary:focus:not(:disabled),
        .tox-button--secondary:active:not(:disabled), .tox-button--naked:hover:not(:disabled),
        .tox-button--naked:focus:not(:disabled), .tox-button--naked:active:not(:disabled),
        .tox-button--naked.tox-button--icon:hover:not(:disabled)) {
        color: #c0d6ee;
        background: #304962;
        border-color: #799abd;
    }

    /* FullCalendar's own palette is inherited by grid, menus and popovers. */
    html[data-theme="dark"],
    html[data-theme="dark"] [data-color-scheme] {
        --fc-monarch-primary: #b0d2fa;
        --fc-monarch-primary-foreground: #143250;
        --fc-monarch-primary-over: #c2dffc;
        --fc-monarch-primary-down: #9cc3ef;
        --fc-monarch-secondary: #2e4c6b;
        --fc-monarch-secondary-foreground: #d9e9ff;
        --fc-monarch-secondary-over: #385d80;
        --fc-monarch-secondary-down: #436c91;
        --fc-monarch-tertiary: #d4bfe5;
        --fc-monarch-tertiary-foreground: #392446;
        --fc-monarch-tertiary-over: #dfcfed;
        --fc-monarch-tertiary-down: #c5afd8;
        --fc-monarch-event: var(--fc-monarch-primary);
        --fc-monarch-event-contrast: var(--fc-monarch-primary-foreground);
        --fc-monarch-highlight: #4777a14d;
        --fc-monarch-now: #ffa99f;
        --fc-monarch-selected: #adc7e1;
        --fc-monarch-selected-foreground: #172c43;
        --fc-monarch-selected-over: #c2d9ee;
        --fc-monarch-selected-down: #9ebbd9;
        --fc-monarch-outline: #9bc9f2;
        --fc-monarch-popover: #24364b;
        --fc-monarch-popover-foreground: #e0e9f3;
        --fc-monarch-background: #1a283a;
        --fc-monarch-faint: #9bb4d01a;
        --fc-monarch-muted: #9bb4d030;
        --fc-monarch-strong: #9bb4d045;
        --fc-monarch-stronger: #9bb4d058;
        --fc-monarch-strongest: #9bb4d06b;
        --fc-monarch-foreground: #e0e9f3;
        --fc-monarch-faint-foreground: #93a6bc;
        --fc-monarch-muted-foreground: #b0bfd2;
        --fc-monarch-strong-foreground: #fff;
        --fc-monarch-border: #425870;
        --fc-monarch-strong-border: #6a839c;
    }

    html[data-theme="dark"] .calendar-host {
        color: var(--text);
    }
}
