/*
 * Final theme compatibility layer.
 *
 * Loaded after feature styles so vendor widgets and legacy light-only states
 * resolve back to the shared MACC tokens. Keep all fixes theme-scoped and move
 * durable component values into core/01-tokens.css when a feature is refactored.
 */

html[data-bs-theme="dark"] {
    color-scheme: dark;
    scrollbar-color: var(--macc-scrollbar-thumb) var(--macc-scrollbar-track);
}

html[data-bs-theme="dark"] body {
    background: var(--macc-page-bg, var(--macc-bg));
    color: var(--macc-page-text, var(--macc-text));
}

html[data-bs-theme="dark"] :where(input, select, textarea, option, optgroup) {
    color-scheme: dark;
}

/* Native controls and browser-managed states can otherwise flash light. */
html[data-bs-theme="dark"] :where(input, textarea, select):-webkit-autofill,
html[data-bs-theme="dark"] :where(input, textarea, select):-webkit-autofill:hover,
html[data-bs-theme="dark"] :where(input, textarea, select):-webkit-autofill:focus {
    -webkit-text-fill-color: var(--macc-input-text) !important;
    caret-color: var(--macc-input-text);
    border-color: var(--macc-input-border) !important;
    box-shadow: 0 0 0 1000px var(--macc-input-bg) inset !important;
    transition: background-color 9999s ease-out 0s;
}

html[data-bs-theme="dark"] input::file-selector-button {
    background: var(--macc-surface-2);
    border-color: var(--macc-border-subtle);
    color: var(--macc-text-primary);
}

html[data-bs-theme="dark"] :where(input, select, textarea):disabled,
html[data-bs-theme="dark"] :where(.form-control, .form-select).disabled {
    background-color: var(--macc-input-disabled-bg) !important;
    border-color: var(--macc-input-disabled-border) !important;
    color: var(--macc-input-disabled-text) !important;
    opacity: 1;
}

/* Bootstrap/Halfmoon utility classes otherwise keep their light literals. */
html[data-bs-theme="dark"] :where(.bg-white, .bg-light) {
    background-color: var(--macc-surface) !important;
    color: var(--macc-text-primary) !important;
}

html[data-bs-theme="dark"] :where(.text-dark, .text-black) {
    color: var(--macc-text-primary) !important;
}

html[data-bs-theme="dark"] :where(.table-light) {
    --bs-table-bg: var(--macc-grid-header-start);
    --bs-table-color: var(--macc-table-head-text);
    --bs-table-border-color: var(--macc-table-head-border);
}

/* Body-appended overlays are not descendants of .macc-shell. */
html[data-bs-theme="dark"] :where(
    body > .dropdown-menu,
    .swal2-container .swal2-popup,
    .ui-autocomplete,
    .ui-datepicker
) {
    background: var(--macc-modal-bg, var(--macc-surface)) !important;
    border-color: var(--macc-modal-border, var(--macc-border)) !important;
    color: var(--macc-text-primary) !important;
}

html[data-bs-theme="dark"] :where(
    .dropdown-menu .dropdown-item
) {
    color: var(--macc-text-primary) !important;
}

/* Bootstrap owns the tooltip wrapper and arrow geometry. Keep the wrapper
   transparent; only the inner bubble and arrow receive theme colors. */
html[data-bs-theme="dark"] .tooltip.macc-bootstrap-tooltip {
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
    color: var(--macc-tooltip-text, var(--macc-sidebar-tooltip-text)) !important;
}

html[data-bs-theme="dark"] .tooltip.macc-bootstrap-tooltip .tooltip-inner {
    background: var(--macc-tooltip-bg, var(--macc-sidebar-tooltip-bg)) !important;
    color: var(--macc-tooltip-text, var(--macc-sidebar-tooltip-text)) !important;
}

html[data-bs-theme="dark"] :where(.dropdown-menu .dropdown-item:hover, .dropdown-menu .dropdown-item:focus) {
    background: var(--macc-surface-3) !important;
}

html[data-bs-theme="dark"] .dropdown-menu .dropdown-divider {
    border-color: var(--macc-border-subtle) !important;
}

html[data-bs-theme="dark"] dialog {
    background: var(--macc-modal-bg, var(--macc-surface));
    border-color: var(--macc-modal-border, var(--macc-border));
    color: var(--macc-text-primary);
}

html[data-bs-theme="dark"] dialog::backdrop {
    background: var(--macc-overlay-soft, rgba(0, 0, 0, 0.55));
}

html[data-bs-theme="dark"] .swal2-container {
    background: var(--macc-overlay-soft, rgba(0, 0, 0, 0.55));
}

html[data-bs-theme="dark"] :where(
    .swal2-title,
    .swal2-html-container,
    .swal2-content,
    .swal2-close,
    .swal2-validation-message
) {
    color: var(--macc-text-primary) !important;
}

html[data-bs-theme="dark"] :where(.swal2-input, .swal2-textarea, .swal2-select) {
    background: var(--macc-input-bg) !important;
    border-color: var(--macc-input-border) !important;
    color: var(--macc-input-text) !important;
}

html[data-bs-theme="dark"] .swal2-validation-message {
    background: var(--macc-error-bg) !important;
    border: 1px solid var(--macc-error-border);
}

html[data-bs-theme="dark"] .swal2-loader {
    border-color: var(--macc-accent-foreground) transparent var(--macc-accent-foreground) transparent !important;
}

html[data-bs-theme="dark"] :where(.ui-menu-item-wrapper, .ui-datepicker td a, .ui-datepicker th) {
    color: var(--macc-text-secondary) !important;
}

html[data-bs-theme="dark"] :where(.ui-menu-item-wrapper.ui-state-active, .ui-datepicker td a.ui-state-active) {
    background: var(--macc-surface-3) !important;
    border-color: var(--macc-focus-border) !important;
    color: var(--macc-text-primary) !important;
}

html[data-bs-theme="dark"] :where(.ui-datepicker-header, .ui-widget-header) {
    background: var(--macc-surface-2) !important;
    border-color: var(--macc-border-subtle) !important;
    color: var(--macc-text-primary) !important;
}

/* Customer Support is intentionally outside the heavy visual normalizer. */
html[data-bs-theme="dark"] body[data-macc-path*="CustomerSupport"] .macc-shell :where(
    .card,
    .modal-content,
    .dropdown-menu,
    .form-control,
    .form-select,
    .input-group-text
) {
    background-color: var(--macc-surface) !important;
    border-color: var(--macc-border-subtle) !important;
    color: var(--macc-text-primary) !important;
}

/* Public/legal pages should be dark surfaces, not white paper islands. */
html[data-bs-theme="dark"] .macc-legal-meta,
html[data-bs-theme="dark"] .macc-legal-section :where(p, li),
html[data-bs-theme="dark"] .macc-legal-check {
    color: var(--macc-text-secondary);
}

html[data-bs-theme="dark"] .macc-legal-notice span {
    color: var(--macc-text-secondary);
}

html[data-bs-theme="dark"] .macc-legal-notice i,
html[data-bs-theme="dark"] .macc-legal-kicker,
html[data-bs-theme="dark"] .macc-legal-pdf-link {
    color: var(--macc-accent-foreground);
}

html[data-bs-theme="dark"] .macc-legal-section {
    border-color: var(--macc-border-subtle);
}

html[data-bs-theme="dark"] .macc-legal-pill-list span {
    background: var(--macc-badge-soft-bg);
    border-color: var(--macc-badge-soft-border);
    color: var(--macc-accent-foreground);
}

html[data-bs-theme="dark"] .macc-legal-actions {
    background: var(--macc-surface-2);
    border-color: var(--macc-border-subtle);
}

html[data-bs-theme="dark"] .macc-legal-acknowledged {
    background: var(--macc-status-warning-bg, var(--macc-token-warning-bg));
    border-color: var(--macc-status-warning-border, var(--macc-token-warning-border));
    color: var(--macc-status-warning-text, var(--macc-token-warning-text));
}

/* Feature states that were previously pinned to a light-only palette. */
html[data-bs-theme="dark"] .notification-center-item.is-read {
    background: var(--macc-surface-2);
    border-color: var(--macc-border-subtle);
}

html[data-bs-theme="dark"] .notification-center-item.is-read::before {
    background: var(--macc-text-tertiary);
}

html[data-bs-theme="dark"] .notification-center-item.is-read :where(
    .notification-center-title,
    .notification-center-message,
    .notification-center-meta,
    .notification-center-icon
) {
    color: var(--macc-text-secondary);
}

html[data-bs-theme="dark"] .notification-center-item.is-read :where(
    .notification-center-link-kind,
    .notification-center-detail
) {
    border-color: var(--macc-border-subtle);
}

html[data-bs-theme="dark"] .macc-alert-away-toggle.is-active .macc-alert-away-icon,
html[data-bs-theme="dark"] .macc-alert-away-status.is-active {
    color: var(--macc-status-success-text, var(--macc-token-success-text));
}

html[data-bs-theme="dark"] .macc-alert-away-toggle.is-scheduled .macc-alert-away-icon,
html[data-bs-theme="dark"] .macc-alert-away-status.is-scheduled {
    color: var(--macc-status-info-text, var(--macc-token-info-text));
}

html[data-bs-theme="dark"] .macc-alert-away-toggle.is-incomplete .macc-alert-away-icon,
html[data-bs-theme="dark"] .macc-alert-away-status.is-warning {
    color: var(--macc-status-warning-text, var(--macc-token-warning-text));
}

html[data-bs-theme="dark"] .macc-alert-away-toggle.is-expired .macc-alert-away-icon,
html[data-bs-theme="dark"] .macc-alert-away-status.is-expired {
    color: var(--macc-status-neutral-text, var(--macc-text-muted));
}

html[data-bs-theme="dark"] .camera-masking-page .masking-pill-pending {
    background: var(--macc-token-warning-bg);
    border-color: var(--macc-token-warning-border);
    box-shadow: none;
    color: var(--macc-token-warning-text);
}

html[data-bs-theme="dark"] .camera-masking-page .mask-risk-grid .form-check-input:checked + .form-check-label::after {
    background: var(--macc-token-danger-bg);
    border-color: var(--macc-token-danger-border);
    color: var(--macc-token-danger-text);
}

/* Login validation and registration messages inherit the dark login card. */
html[data-bs-theme="dark"] .macc-login-form-shell .alert-danger,
html[data-bs-theme="dark"] .macc-login-notice.is-danger {
    background: var(--macc-error-bg);
    border-color: var(--macc-error-border);
    color: var(--macc-error-text-strong);
}

html[data-bs-theme="dark"] .macc-register-note,
html[data-bs-theme="dark"] .macc-login-notice.is-success {
    background: var(--macc-token-success-bg);
    border-color: var(--macc-token-success-border);
}

html[data-bs-theme="dark"] .macc-register-note-title,
html[data-bs-theme="dark"] .macc-login-notice.is-success .macc-login-notice-icon i {
    color: var(--macc-token-success-text);
}

html[data-bs-theme="dark"] .macc-login-notice.is-warning {
    background: var(--macc-token-warning-bg);
    border-color: var(--macc-token-warning-border);
}

html[data-bs-theme="dark"] .macc-login-notice.is-warning .macc-login-notice-icon i {
    color: var(--macc-token-warning-text);
}
