.dm-theme-toggle { white-space: nowrap; }

/* Screen-only: printed documents keep the existing light palette. */
@media screen {
    html[data-theme="dark"] {
        color-scheme: dark;
        --dm-canvas: #111827;
        --dm-surface: #1b2535;
        --dm-ink: #e5eaf2;
        --dm-muted: #a6b4c9;
        --dm-border: #39465a;
        --dm-primary: #9bbfff;
        --dm-primary-hover: #c3d9ff;
        --dm-soft-blue: #253c60;
        --dm-shadow: 0 3px 16px rgba(0, 0, 0, .16);
        --dm-raised: #243145;
        --dm-input: #141e2e;
    }

    html[data-theme="dark"] .dm-theme :is(.card, .modern-toolbar, .modern-article-card, .modern-card-header,
        .modal-content, .dropdown-menu, .list-group-item, .dm-home-link, .main-footer,
        .login-card-body, .register-card-body, .popover, .page-link, .dm-table) {
        background-color: var(--dm-surface);
        color: var(--dm-ink);
        border-color: var(--dm-border);
    }
    html[data-theme="dark"] .dm-theme .card:not(.card-primary):not(.card-success):not(.card-danger):not(.card-warning):not(.card-info):not(.card-secondary):not(.card-dark) > .card-header,
    html[data-theme="dark"] .dm-theme .card.card-outline > .card-header,
    html[data-theme="dark"] .dm-theme .dm-auth-card .card-header,
    html[data-theme="dark"] .dm-theme .content .container > h1 + form:not(.form-inline) {
        background: var(--dm-surface);
        color: var(--dm-ink);
    }
    html[data-theme="dark"] .dm-theme :is(.card-footer, .modal-footer, .modern-form-section, .modern-price-box,
        .input-group-text, .popover-header, .breadcrumb, .modern-progress, .progress, .dm-admin-icon,
        .dm-admin-close, .dm-home-icon, .modern-icon-box, .empty-icon, .ticket-move-avatar) {
        background: var(--dm-raised);
        color: var(--dm-ink);
        border-color: var(--dm-border);
    }
    html[data-theme="dark"] .dm-theme :is(.main-header .navbar-nav .nav-link, .nav-sidebar .nav-link,
        .nav-sidebar .nav-link > i:first-child, .dm-admin-link, .dm-admin-group h3, .dropdown-item,
        .nav-tabs .nav-link, .list-group-item-action, label:not(.form-check-label):not(.custom-control-label)) { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme :is(.nav-sidebar .nav-header, .dm-admin-eyebrow, .dm-admin-intro,
        .dm-admin-link-description, .dm-auth-caption, .nav-sidebar .nav-link > i:first-child) { color: var(--dm-muted); }
    html[data-theme="dark"] .dm-theme :is(.main-header .nav-link:hover, .nav-sidebar .nav-link:hover,
        .dropdown-item:hover, .dropdown-item:focus, .list-group-item-action:hover, .list-group-item-action:focus,
        .dm-home-link:hover, .dm-admin-link:hover, .dm-admin-link.is-active, .page-link:hover) {
        background: var(--dm-raised);
        color: var(--dm-ink);
    }
    html[data-theme="dark"] .dm-theme :is(.nav-sidebar .nav-link.active, .list-group-item.active, .nav-pills .nav-link.active) {
        background: var(--dm-soft-blue);
        color: var(--dm-primary);
        border-color: #5275aa;
    }
    html[data-theme="dark"] .dm-theme .nav-tabs .nav-link.active {
        background: var(--dm-surface);
        color: var(--dm-primary);
        border-color: var(--dm-border) var(--dm-border) var(--dm-surface);
    }
    html[data-theme="dark"] .dm-theme :is(.nav-tabs .nav-link:hover, .modal-header, .dropdown-divider, hr, .timeline) { border-color: var(--dm-border); }
    html[data-theme="dark"] .dm-theme .close { color: var(--dm-ink); text-shadow: none; }
    html[data-theme="dark"] .dm-theme .popover-body { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .navbar-nav > .user-menu > .dropdown-menu > .user-footer { background: var(--dm-raised); }

    html[data-theme="dark"] .dm-theme :is(.form-control, .custom-select, .form-control-navbar, .swal2-input, .swal2-textarea, .swal2-select) {
        background-color: var(--dm-input);
        color: var(--dm-ink);
    }
    html[data-theme="dark"] .dm-theme :is(.form-control, .custom-select):not(.is-invalid):not(.is-valid),
    html[data-theme="dark"] .dm-theme .dm-auth-card .input-group > .form-control:not(.is-invalid):not(.is-valid),
    html[data-theme="dark"] .dm-theme .dm-auth-card .input-group-text { border-color: var(--dm-border); }
    html[data-theme="dark"] .dm-theme :is(.form-control:disabled, .form-control[readonly], .custom-select:disabled) {
        background: var(--dm-raised);
        color: var(--dm-muted);
    }
    html[data-theme="dark"] .dm-theme .form-control::placeholder { color: #95a5bd; }
    html[data-theme="dark"] .dm-theme :is(.btn-primary, .btn-outline-primary:hover, .btn-outline-primary:not(:disabled).active, .page-item.active .page-link) {
        background: #315fba;
        border-color: #567ece;
        color: #fff;
    }
    html[data-theme="dark"] .dm-theme .btn-primary:hover,
    html[data-theme="dark"] .dm-theme .btn-primary:not(:disabled):active { background: #294f9c; color: #fff; border-color: #567ece; }
    html[data-theme="dark"] .dm-theme :is(.btn-outline-secondary, .btn-default, .btn-light) { color: var(--dm-ink); border-color: #60718b; }
    html[data-theme="dark"] .dm-theme :is(.btn-default, .btn-light) { background: var(--dm-raised); }
    html[data-theme="dark"] .dm-theme .btn-outline-primary { color: var(--dm-primary); border-color: #607da9; }
    html[data-theme="dark"] .dm-theme .btn-outline-danger { color: #ffa3b1; border-color: #b45266; }
    html[data-theme="dark"] .dm-theme .btn-outline-success { color: #83dab2; border-color: #428968; }
    html[data-theme="dark"] .dm-theme :is(.btn-outline-danger:hover, .btn-outline-success:hover) { color: #fff; }
    html[data-theme="dark"] .dm-theme .page-item.disabled .page-link { background: var(--dm-canvas); color: var(--dm-muted); border-color: var(--dm-border); }

    html[data-theme="dark"] .dm-theme .table:not(.table-dark) thead th { background: var(--dm-raised); color: var(--dm-muted); }
    html[data-theme="dark"] .dm-theme .modern-meta-value { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme :is(.table td, .table th, .table-bordered, .dm-table td) { border-color: var(--dm-border); }
    html[data-theme="dark"] .dm-theme .dm-table tbody tr:nth-child(even) { background: var(--dm-surface); }
    html[data-theme="dark"] .dm-theme .table:not(.table-dark).table-striped tbody tr:nth-of-type(odd),
    html[data-theme="dark"] .dm-theme .dm-table tbody tr:nth-child(odd) { background: #202c3e; }
    html[data-theme="dark"] .dm-theme .table:not(.table-dark).table-hover tbody tr:hover,
    html[data-theme="dark"] .dm-theme .modern-table tbody tr:hover { background: var(--dm-raised); color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme :is(.badge-primary, .alert-info, .badge-info, .metric-total) { background: #203d58; color: #a9d6ff; border-color: #416887; }
    html[data-theme="dark"] .dm-theme :is(.badge-success, .alert-success, .metric-done, .table-success, .table-success > td) { background: #1e4037; color: #a1e2c4; border-color: #3c7360; }
    html[data-theme="dark"] .dm-theme :is(.badge-warning, .alert-warning, .metric-open, .table-warning, .table-warning > td) { background: #473a20; color: #f5d68e; border-color: #7f693d; }
    html[data-theme="dark"] .dm-theme :is(.badge-danger, .alert-danger, .metric-danger, .table-danger, .table-danger > td) { background: #492b38; color: #ffb8c4; border-color: #875266; }
    html[data-theme="dark"] .dm-theme :is(.badge-secondary, .alert-secondary, .metric-muted, .alert-light, .badge-light) { background: var(--dm-raised); color: var(--dm-ink); border-color: var(--dm-border); }
    html[data-theme="dark"] .dm-theme :is(.bg-white, .bg-light) { background-color: var(--dm-surface) !important; color: var(--dm-ink) !important; }
    html[data-theme="dark"] .dm-theme :is(.text-dark, .text-body, .text-black) { color: var(--dm-ink) !important; }
    html[data-theme="dark"] .dm-theme .text-secondary { color: var(--dm-muted) !important; }
    html[data-theme="dark"] .dm-theme .text-primary { color: var(--dm-primary) !important; }
    html[data-theme="dark"] .dm-theme .text-danger { color: #ff9cad !important; }
    html[data-theme="dark"] .dm-theme .text-success { color: #83dab2 !important; }
    html[data-theme="dark"] .dm-theme .text-info { color: #88d6eb !important; }
    html[data-theme="dark"] .dm-theme :is(.border, .border-top, .border-bottom, .border-left, .border-right) { border-color: var(--dm-border) !important; }
    html[data-theme="dark"] .dm-theme #ticket-suggest-overlay { background: var(--dm-surface) !important; }
    html[data-theme="dark"] .dm-theme.dm-auth-page { background: radial-gradient(ellipse at 50% 0, #233650 0, var(--dm-canvas) 60%); }

    /* Third-party editors and popups are outside the usual card styles. */
    html[data-theme="dark"] .dm-theme :is(.swal2-popup, .swal2-html-container, .swal2-title, .swal2-footer) { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .swal2-popup { background: var(--dm-surface); }
    html[data-theme="dark"] .dm-theme :is(.swal2-validation-message, .swal2-input) { background: var(--dm-input); color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme :is(.ql-toolbar.ql-snow, .ql-container.ql-snow, .ql-snow .ql-picker-options, .ql-snow .ql-tooltip) {
        background: var(--dm-surface); color: var(--dm-ink); border-color: var(--dm-border);
    }
    html[data-theme="dark"] .dm-theme .ql-editor { background: var(--dm-surface); color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme #moveTicketModal .ticket-move-avatar { background: var(--dm-raised); color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .ql-editor.ql-blank::before { color: var(--dm-muted); }
    html[data-theme="dark"] .dm-theme .ql-snow .ql-stroke { stroke: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .ql-snow .ql-fill { fill: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .ql-snow .ql-picker { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .tagify {
        --tags-border-color: var(--dm-border); --tag-bg: var(--dm-raised); --tag-hover: #354862;
        --tag-text-color: var(--dm-ink); --input-color: var(--dm-ink); --placeholder-color: var(--dm-muted);
        --placeholder-color-focus: var(--dm-muted); --tag-remove-btn-color: var(--dm-ink);
    }
    html[data-theme="dark"] .dm-theme :is(.tagify__dropdown__wrapper, .select2-dropdown, .select2-selection, .select2-search__field) {
        background: var(--dm-input); color: var(--dm-ink); border-color: var(--dm-border);
    }
    html[data-theme="dark"] .dm-theme .select2-selection__rendered { color: var(--dm-ink); }
    html[data-theme="dark"] .dm-theme .filepond--panel-root { background: var(--dm-raised); }
    html[data-theme="dark"] .dm-theme .filepond--drop-label { color: var(--dm-ink); }
    /* Keep original HTML emails readable regardless of their own fixed colours. */
    html[data-theme="dark"] .dm-theme #emailContentIframe { background: #fff; color-scheme: light; }

    /* Compose and preview mail on the same light surface as the received HTML. */
    html[data-theme="dark"] .dm-theme .dm-mail-content-light,
    html[data-theme="dark"] .dm-theme .dm-mail-content-light :is(.form-control, .ql-toolbar.ql-snow,
        .ql-container.ql-snow, .ql-editor, .ql-picker-options, .ql-tooltip,
        .note-editor, .note-toolbar, .note-editable, .note-statusbar, .note-btn, .dropdown-menu, .dropdown-item),
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .note-editor label {
        background-color: #fff !important;
        color: #222 !important;
        color-scheme: light;
    }
    html[data-theme="dark"] .dm-theme .dm-mail-content-light :is(.ql-toolbar.ql-snow,
        .ql-container.ql-snow, .note-editor, .note-toolbar, .note-btn, .dropdown-menu) {
        border-color: #d7dfe9;
    }
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .form-control::placeholder,
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .ql-editor.ql-blank::before { color: #6c757d; }
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .ql-snow .ql-stroke { stroke: #444; }
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .ql-snow .ql-fill { fill: #444; }
    html[data-theme="dark"] .dm-theme .dm-mail-content-light .ql-snow .ql-picker { color: #444; }
}

@media print {
    .dm-theme-toggle { display: none !important; }
}
