:root {
    --brand-primary: #0f766e;
    --brand-secondary: #0f172a;
    --brand-accent: #22c55e;
}

:root {
    --ui-primary: var(--brand-primary);
    --ui-primary-strong: var(--brand-secondary);
    --ui-accent: var(--brand-accent);
    --primary: var(--brand-primary);
    --primary-dark: var(--brand-secondary);
    --accent: var(--brand-accent);
}

.brand-settings-grid {
    display: grid;
    gap: 16px;
}

@media (min-width: 992px) {
    .brand-settings-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .brand-settings-grid .content-card {
        margin: 0;
    }
}

.logo-preview {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 92px;
    height: 92px;
    border-radius: 16px;
    border: 1px dashed var(--ui-border);
    background: #ffffff;
}

.logo-preview img {
    max-width: 82px;
    max-height: 82px;
    object-fit: contain;
}

.theme-preview {
    margin-top: 12px;
    border: 1px solid var(--ui-border);
    border-radius: 14px;
    overflow: hidden;
}

.theme-preview-head {
    padding: 12px 14px;
    background: var(--brand-secondary);
    color: #ffffff;
    font-weight: 600;
}

.theme-preview-body {
    --preview-primary: var(--brand-primary);
    --preview-secondary: var(--brand-secondary);
    --preview-accent: var(--brand-accent);
    padding: 14px;
    background: #ffffff;
    display: grid;
    gap: 10px;
}

.theme-preview-primary {
    background: var(--preview-primary);
    border-color: var(--preview-primary);
    color: #ffffff;
}

.theme-preview-secondary {
    color: var(--preview-primary);
    border-color: var(--preview-primary);
}

.theme-preview-accent {
    display: inline-flex;
    width: 36px;
    height: 12px;
    border-radius: 999px;
    background: var(--preview-accent);
}

.bulk-toolbar {
    display: none;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 12px;
    padding: 12px 14px;
    border: 1px solid var(--ui-border);
    border-radius: 12px;
    background: #ffffff;
    position: sticky;
    top: 126px;
    z-index: 8;
}

.bulk-toolbar.is-visible {
    display: flex;
}

.bulk-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.member-tag-list {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.member-tag-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    line-height: 1.2;
    border-radius: 999px;
    padding: 4px 9px;
    color: #ffffff;
}

.table-checkbox-cell {
    width: 38px;
}

.brand-mark {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.14);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.brand-mark img {
    width: 34px;
    height: 34px;
    object-fit: contain;
}

.receipt-watermark {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    opacity: 0.08;
    font-size: 48px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

/* Theme Tokens */
:root {
    color-scheme: light;
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-soft: #f1f5f9;
    --text: #0f172a;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --primary: var(--brand-primary);
    --accent: #22c55e;
    --shadow-soft: 0 10px 24px rgba(2, 8, 23, 0.08);
}

html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0b1120;
    --surface: #111827;
    --surface-soft: #1f2937;
    --text: #f8fafc;
    --text-muted: #cbd5e1;
    --border: #334155;
    --primary: #2dd4bf;
    --accent: #22c55e;
    --shadow-soft: 0 16px 30px rgba(0, 0, 0, 0.35);
    /* Bridge legacy design token systems to dark-safe values */
    --ui-bg: var(--bg);
    --ui-surface: var(--surface);
    --ui-surface-alt: var(--surface-soft);
    --ui-text: var(--text);
    --ui-muted: var(--text-muted);
    --ui-border: var(--border);
    --sa-bg: var(--bg);
    --sa-surface: var(--surface);
    --sa-border: var(--border);
    --sa-text: var(--text);
    --sa-muted: var(--text-muted);
    --surface-alt: var(--surface-soft);
    --secondary: var(--text);
    --muted: var(--text-muted);
    --error-text: #fda4af;
    --success-text: #4ade80;
}

/* Theme Toggle UI */
.theme-toggle-control {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.theme-toggle-label {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 600;
}

.theme-toggle-control select {
    min-height: 36px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    padding: 0 10px;
    font-size: 13px;
    font-weight: 600;
}

.theme-toggle-compact .theme-toggle-label {
    display: none;
}

.header-right,
.site-admin-header-right {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

/* Global Surface and Text */
body,
body.admin-shell,
body.public-site-body,
body.site-admin-body,
body.portal-body {
    background: var(--bg);
    color: var(--text);
}

h1, h2, h3, h4, h5, h6,
label,
p,
span,
small,
th,
td {
    color: inherit;
}

.muted,
.text-muted {
    color: var(--text-muted) !important;
}

a {
    color: var(--primary);
}

html[data-theme="dark"] .app-main,
html[data-theme="dark"] .app-content,
html[data-theme="dark"] .page-inner,
html[data-theme="dark"] .page-shell,
html[data-theme="dark"] .site-admin-main,
html[data-theme="dark"] .public-main,
html[data-theme="dark"] .portal-page {
    color: var(--text);
}

/* Cards, tables, forms, dropdowns */
.content-card,
.metric-card,
.hero-card,
.card,
.sa-card,
.portal-card,
.portal-content-card,
.portal-giving-card,
.portal-event-card,
.portal-action-card,
.portal-custom-card,
.comm-card,
.table-wrap,
.table-wrapper,
.sa-table-wrap,
.public-form-card,
.form-card {
    background: var(--surface);
    border-color: var(--border);
}

input,
select,
textarea,
.sa-field input,
.sa-field select,
.sa-field textarea,
.public-field input,
.public-field select,
.public-field textarea,
.portal-form input,
.portal-form select,
.portal-form textarea {
    background: var(--surface-soft);
    color: var(--text);
    border-color: var(--border);
}

input::placeholder,
textarea::placeholder {
    color: #94a3b8;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(45, 212, 191, 0.15);
}

table {
    background: var(--surface);
    color: var(--text);
}

th {
    color: var(--text-muted);
    border-bottom-color: var(--border);
}

td {
    border-bottom-color: var(--border);
}

/* Admin + Site Admin + Public shell dark refinements */
html[data-theme="dark"] body.admin-shell {
    background: radial-gradient(circle at 15% -10%, rgba(45, 212, 191, 0.1) 0, transparent 45%), var(--bg);
}

html[data-theme="dark"] .app-header,
html[data-theme="dark"] .quick-actions,
html[data-theme="dark"] .site-admin-header,
html[data-theme="dark"] .public-header,
html[data-theme="dark"] .site-header {
    background: rgba(15, 23, 42, 0.88);
    border-color: var(--border);
}

html[data-theme="dark"] .app-sidebar {
    background: #020617;
    border-color: #1e293b;
}

html[data-theme="dark"] .sidebar-link {
    color: #cbd5e1;
}

html[data-theme="dark"] .sidebar-link.is-active {
    background: rgba(45, 212, 191, 0.12);
    color: #5eead4;
}

html[data-theme="dark"] .header-title,
html[data-theme="dark"] .header-user,
html[data-theme="dark"] .site-admin-header h1,
html[data-theme="dark"] .site-admin-header-right span,
html[data-theme="dark"] .public-brand,
html[data-theme="dark"] .public-brand-text {
    color: var(--text);
}

html[data-theme="dark"] .header-subtitle,
html[data-theme="dark"] .site-admin-header p,
html[data-theme="dark"] .public-footer-copy,
html[data-theme="dark"] .public-footer-links a,
html[data-theme="dark"] .section-copy,
html[data-theme="dark"] .subject-desc,
html[data-theme="dark"] .person-meta,
html[data-theme="dark"] .card h4,
html[data-theme="dark"] .metric-label {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .button.button-secondary,
html[data-theme="dark"] .sa-button-secondary,
html[data-theme="dark"] .sa-button-muted {
    background: var(--surface-soft);
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] .quick-more-menu,
html[data-theme="dark"] .public-nav,
html[data-theme="dark"] .sa-menu-toggle,
html[data-theme="dark"] .dropdown-menu {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

/* Public site dark mode */
html[data-theme="dark"] body.public-site-body {
    background:
        radial-gradient(circle at 90% -5%, rgba(34, 197, 94, 0.12) 0%, transparent 36%),
        radial-gradient(circle at -8% 8%, rgba(45, 212, 191, 0.1) 0%, transparent 34%),
        var(--bg);
}

html[data-theme="dark"] .public-nav-links a {
    color: var(--text);
}

html[data-theme="dark"] .public-nav-links a:hover,
html[data-theme="dark"] .public-nav-links a.is-active {
    background: rgba(45, 212, 191, 0.15);
    color: #5eead4;
}

html[data-theme="dark"] .feature-card,
html[data-theme="dark"] .feature-item,
html[data-theme="dark"] .trust-strip,
html[data-theme="dark"] .trust-item,
html[data-theme="dark"] .pricing-item,
html[data-theme="dark"] .split-intro,
html[data-theme="dark"] .finance-aside,
html[data-theme="dark"] .focus-item,
html[data-theme="dark"] .portal-item,
html[data-theme="dark"] .value-item,
html[data-theme="dark"] .mock-window,
html[data-theme="dark"] .mock-window-head,
html[data-theme="dark"] .mock-window-body,
html[data-theme="dark"] .mock-list-item,
html[data-theme="dark"] .metric-card,
html[data-theme="dark"] .public-alert,
html[data-theme="dark"] .public-chip,
html[data-theme="dark"] .public-card,
html[data-theme="dark"] .site-admin-card,
html[data-theme="dark"] .sa-table-wrap,
html[data-theme="dark"] .worship-card,
html[data-theme="dark"] .worship-stat,
html[data-theme="dark"] .worship-panel,
html[data-theme="dark"] .portal-builder-shell,
html[data-theme="dark"] .portal-builder-card,
html[data-theme="dark"] .builder-card {
    background: var(--surface);
    border-color: var(--border);
}

html[data-theme="dark"] .feature-card h3,
html[data-theme="dark"] .feature-card p,
html[data-theme="dark"] .section-headline,
html[data-theme="dark"] .mockup-card,
html[data-theme="dark"] .mockup-card p,
html[data-theme="dark"] .mockup-card span,
html[data-theme="dark"] .worship-card h1,
html[data-theme="dark"] .worship-card h2,
html[data-theme="dark"] .worship-card h3,
html[data-theme="dark"] .worship-card p,
html[data-theme="dark"] .worship-card span {
    color: var(--text) !important;
}

/* Communication module dark mode */
html[data-theme="dark"] .comm-shell {
    background: var(--bg);
    border-color: var(--border);
}

html[data-theme="dark"] .comm-tab,
html[data-theme="dark"] .comm-channel-card,
html[data-theme="dark"] .comm-token-btn,
html[data-theme="dark"] .comm-conversation-item,
html[data-theme="dark"] .comm-thread-panel,
html[data-theme="dark"] .comm-inbox-sidebar,
html[data-theme="dark"] .comm-template-card,
html[data-theme="dark"] .comm-segment-card,
html[data-theme="dark"] .comm-thread-head,
html[data-theme="dark"] .comm-reply-box,
html[data-theme="dark"] .comm-preview-card {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}

html[data-theme="dark"] .comm-thread-body {
    background: var(--surface-soft);
}

html[data-theme="dark"] .comm-message-bubble {
    background: var(--surface);
    border-color: var(--border);
}

html[data-theme="dark"] .comm-message-bubble.is-outgoing {
    background: rgba(45, 212, 191, 0.14);
    border-color: rgba(45, 212, 191, 0.35);
}

/* Member portal dark mode */
html[data-theme="dark"] .portal-app {
    background: #0b1120;
}

html[data-theme="dark"] .portal-topbar,
html[data-theme="dark"] .portal-bottom-nav,
html[data-theme="dark"] .portal-chat-composer {
    background: #111827;
    border-color: #334155;
}

html[data-theme="dark"] .portal-brand-logo,
html[data-theme="dark"] .portal-topbar-icon,
html[data-theme="dark"] .portal-chat-back,
html[data-theme="dark"] .portal-chat-new,
html[data-theme="dark"] .portal-chat-attach,
html[data-theme="dark"] .portal-form-block,
html[data-theme="dark"] .portal-checkbox,
html[data-theme="dark"] .portal-list-item,
html[data-theme="dark"] .portal-menu-list a,
html[data-theme="dark"] .portal-thread-list a,
html[data-theme="dark"] .portal-chat-main,
html[data-theme="dark"] .portal-thread-messages,
html[data-theme="dark"] .portal-chat-bubble,
html[data-theme="dark"] .portal-directory-item .avatar,
html[data-theme="dark"] .portal-member-list article,
html[data-theme="dark"] .portal-profile-grid > div,
html[data-theme="dark"] .portal-public-meta > div,
html[data-theme="dark"] .portal-giving-impact article,
html[data-theme="dark"] .portal-kpi-card,
html[data-theme="dark"] .portal-empty {
    background: #111827;
    border-color: #334155;
    color: #f8fafc;
}

html[data-theme="dark"] .portal-chat-bubble.is-mine {
    background: linear-gradient(135deg, #0f766e, #1d4ed8);
}

html[data-theme="dark"] .portal-chat-bubble header small,
html[data-theme="dark"] .portal-chat-bubble p,
html[data-theme="dark"] .portal-section-head p,
html[data-theme="dark"] .portal-content-card p,
html[data-theme="dark"] .portal-giving-card p,
html[data-theme="dark"] .portal-menu-copy small,
html[data-theme="dark"] .portal-muted-line,
html[data-theme="dark"] .portal-list li span,
html[data-theme="dark"] .portal-notify-card p,
html[data-theme="dark"] .portal-install-card p,
html[data-theme="dark"] .portal-rich-copy,
html[data-theme="dark"] .portal-copy-block,
html[data-theme="dark"] .portal-bullet-list,
html[data-theme="dark"] .event-info p,
html[data-theme="dark"] .event-info small,
html[data-theme="dark"] .portal-form label,
html[data-theme="dark"] .welcome-kicker {
    color: #cbd5e1 !important;
}

html[data-theme="dark"] .portal-bottom-nav a {
    color: #94a3b8;
}

html[data-theme="dark"] .portal-bottom-nav a.is-active,
html[data-theme="dark"] .portal-bottom-nav a.active {
    color: #5eead4;
}

html[data-theme="dark"] .portal-install-card,
html[data-theme="dark"] .portal-notify-card,
html[data-theme="dark"] .portal-settings-card,
html[data-theme="dark"] .portal-profile-card,
html[data-theme="dark"] .portal-table-card,
html[data-theme="dark"] .portal-data-card {
    background: #111827;
    border-color: #334155;
    color: #f8fafc;
}

/* Reports and finance dark safe */
html[data-theme="dark"] .report-table .totals-row td {
    background: rgba(51, 65, 85, 0.4);
    border-top-color: #475569;
}

html[data-theme="dark"] .negative-value {
    color: #f87171;
}

html[data-theme="dark"] .positive-value {
    color: #4ade80;
}

/* Modals */
html[data-theme="dark"] .modal-overlay {
    background: rgba(0, 0, 0, 0.65);
}

html[data-theme="dark"] .modal-box,
html[data-theme="dark"] .confirm-dialog {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

html[data-theme="dark"] .modal-header,
html[data-theme="dark"] .modal-footer,
html[data-theme="dark"] .comm-thread-head,
html[data-theme="dark"] .comm-reply-box {
    border-color: var(--border);
}

html[data-theme="dark"] .modal-close {
    color: #cbd5e1;
}

html[data-theme="dark"] .modal-overlay .modal-btn-neutral {
    background: var(--surface-soft);
    color: var(--text);
    border-color: var(--border);
}

/* Dark-safe badges */
html[data-theme="dark"] .badge-success {
    background: rgba(34, 197, 94, 0.15);
    color: #4ade80;
}

html[data-theme="dark"] .badge-warning {
    background: rgba(245, 158, 11, 0.15);
    color: #fbbf24;
}

html[data-theme="dark"] .badge-danger {
    background: rgba(239, 68, 68, 0.15);
    color: #f87171;
}

html[data-theme="dark"] .badge-info {
    background: rgba(59, 130, 246, 0.15);
    color: #93c5fd;
}

/* Site-admin specific surfaces */
html[data-theme="dark"] .site-admin-sidebar {
    background: #020617;
}

html[data-theme="dark"] .site-admin-nav a {
    color: #cbd5e1;
}

html[data-theme="dark"] .site-admin-nav a:hover,
html[data-theme="dark"] .site-admin-nav a.is-active {
    background: rgba(45, 212, 191, 0.12);
    color: #5eead4;
}

html[data-theme="dark"] .sa-table th {
    background: #0f172a;
    color: #cbd5e1;
}

html[data-theme="dark"] .sa-alert-success {
    background: rgba(34, 197, 94, 0.14);
    border-color: rgba(34, 197, 94, 0.28);
    color: #4ade80;
}

html[data-theme="dark"] .sa-alert-error {
    background: rgba(239, 68, 68, 0.16);
    border-color: rgba(239, 68, 68, 0.3);
    color: #fda4af;
}

@media print {
    body {
        background: #ffffff !important;
        color: #000000 !important;
    }
}

/* Adaptive dark text safety layer */
html[data-theme="dark"] :where(
    .content-card,
    .metric-card,
    .hero-card,
    .sa-card,
    .portal-card,
    .portal-content-card,
    .portal-giving-card,
    .portal-event-card,
    .portal-action-card,
    .portal-custom-card,
    .comm-card,
    .comm-shell,
    .table-wrap,
    .table-wrapper,
    .sa-table-wrap,
    .public-form-card,
    .form-card,
    .modal-box,
    .report-section-card
) {
    color: var(--text);
}

html[data-theme="dark"] :where(
    .content-card,
    .metric-card,
    .hero-card,
    .sa-card,
    .portal-card,
    .portal-content-card,
    .portal-giving-card,
    .portal-event-card,
    .portal-action-card,
    .portal-custom-card,
    .comm-card,
    .comm-shell,
    .table-wrap,
    .table-wrapper,
    .sa-table-wrap,
    .public-form-card,
    .form-card,
    .modal-box,
    .report-section-card
) :where(h1, h2, h3, h4, h5, h6, p, span, strong, small, label, li, td, th) {
    color: inherit;
}

html[data-theme="dark"] :where(
    .muted,
    .text-muted,
    .helper-text,
    .card-desc,
    .subject-desc,
    .person-meta,
    .header-subtitle,
    .section-copy,
    .sa-stat span,
    .sa-field label,
    .sa-list,
    .portal-muted,
    .portal-muted-line,
    .welcome-kicker,
    .portal-menu-copy small,
    .portal-content-meta small,
    .portal-content-meta span,
    .event-info p,
    .event-info small,
    .comm-header p,
    .comm-empty,
    .comm-live-note,
    .comm-conversation-item p,
    .comm-thread-head p
) {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] :where(
    .content-card,
    .metric-card,
    .hero-card,
    .sa-card,
    .portal-card,
    .portal-content-card,
    .portal-giving-card,
    .portal-event-card,
    .portal-action-card,
    .portal-custom-card,
    .comm-card,
    .comm-shell,
    .table-wrap,
    .table-wrapper,
    .sa-table-wrap,
    .public-form-card,
    .form-card,
    .modal-box,
    .report-section-card
) a:not(.button):not(.portal-btn):not(.sa-button):not(.comm-tab):not(.sidebar-link):not(.social-link):not(.portal-topbar-icon):not(.portal-chat-back):not(.portal-chat-new) {
    color: var(--primary);
}

html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    color: var(--text);
}

html[data-theme="dark"] option {
    background: var(--surface);
    color: var(--text);
}

/* Dark mode form control normalization (overrides legacy white field rules) */
html[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
}

html[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"])::placeholder,
html[data-theme="dark"] textarea::placeholder {
    color: #94a3b8 !important;
    opacity: 1;
}

html[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]):focus,
html[data-theme="dark"] select:focus,
html[data-theme="dark"] textarea:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 4px rgba(45, 212, 191, 0.15) !important;
    outline: none !important;
}

html[data-theme="dark"] input[type="checkbox"],
html[data-theme="dark"] input[type="radio"] {
    accent-color: var(--primary);
}

html[data-theme="dark"] select option {
    background: var(--surface);
    color: var(--text);
}

html[data-theme="dark"] .field input,
html[data-theme="dark"] .field select,
html[data-theme="dark"] .field textarea,
html[data-theme="dark"] .search-form input,
html[data-theme="dark"] .search-form select,
html[data-theme="dark"] .member-check-card select,
html[data-theme="dark"] .member-entry-form input,
html[data-theme="dark"] .member-entry-form select,
html[data-theme="dark"] .member-entry-form textarea,
html[data-theme="dark"] .public-field input,
html[data-theme="dark"] .public-field select,
html[data-theme="dark"] .public-field textarea,
html[data-theme="dark"] .sa-field input,
html[data-theme="dark"] .sa-field select,
html[data-theme="dark"] .sa-field textarea,
html[data-theme="dark"] .modal-body input,
html[data-theme="dark"] .modal-body select,
html[data-theme="dark"] .modal-body textarea,
html[data-theme="dark"] .portal-form input,
html[data-theme="dark"] .portal-form select,
html[data-theme="dark"] .portal-form textarea,
html[data-theme="dark"] .comm-shell input,
html[data-theme="dark"] .comm-shell select,
html[data-theme="dark"] .comm-shell textarea,
html[data-theme="dark"] .budget-line-table input,
html[data-theme="dark"] .budget-line-table select,
html[data-theme="dark"] .budget-line-table textarea {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] input:disabled,
html[data-theme="dark"] select:disabled,
html[data-theme="dark"] textarea:disabled {
    background: rgba(148, 163, 184, 0.14) !important;
    color: #94a3b8 !important;
    border-color: rgba(148, 163, 184, 0.35) !important;
}

/* Browser autofill can force light backgrounds/text in dark mode */
html[data-theme="dark"] input:-webkit-autofill,
html[data-theme="dark"] input:-webkit-autofill:hover,
html[data-theme="dark"] input:-webkit-autofill:focus,
html[data-theme="dark"] textarea:-webkit-autofill,
html[data-theme="dark"] select:-webkit-autofill {
    -webkit-text-fill-color: var(--text) !important;
    caret-color: var(--text) !important;
    -webkit-box-shadow: 0 0 0px 1000px var(--surface-soft) inset !important;
    box-shadow: 0 0 0px 1000px var(--surface-soft) inset !important;
    transition: background-color 9999s ease-out 0s !important;
}

html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder {
    color: #94a3b8;
    opacity: 1;
}

html[data-theme="dark"] .portal-chat-bubble.is-mine,
html[data-theme="dark"] .portal-chat-bubble.is-mine :where(h1, h2, h3, h4, h5, h6, p, span, strong, small) {
    color: #ffffff !important;
}

html[data-theme="dark"] .comm-message-bubble.is-outgoing,
html[data-theme="dark"] .comm-message-bubble.is-outgoing :where(h1, h2, h3, h4, h5, h6, p, span, strong, small) {
    color: #e6fffb !important;
}

/* FINAL DARK WINNER - keep this as the last dark block */
html[data-theme="dark"] body :where(
    .content-card,
    .metric-card,
    .hero-card,
    .card,
    .form-card,
    .table-wrap,
    .table-wrapper,
    .settings-grid .setting-tile,
    .dashboard-event-card,
    .dashboard-latest-activity-item,
    .dashboard-quickstart-card .card,
    .builder-shell-card,
    .builder-settings-panel,
    .builder-settings-panel .dashboard-card,
    .portal-builder-preview-grid .dashboard-card,
    .portal-app-live-card,
    .portal-builder-frame,
    .portal-sortable-item,
    .portal-nav-sort-group,
    .portal-nav-sort-item,
    .comm-shell,
    .comm-card,
    .comm-channel-card,
    .comm-template-card,
    .comm-segment-card,
    .comm-thread-panel,
    .comm-thread-head,
    .comm-reply-box,
    .comm-inbox-sidebar,
    .comm-conversation-item,
    .comm-preview-card
) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    .muted,
    .text-muted,
    .muted-text,
    .helper-text,
    .field-help,
    .dashboard-event-type,
    .dashboard-event-time,
    .dashboard-grid .metric-label,
    .dashboard-grid .metric-card p,
    .dashboard-latest-activity-item p,
    .dashboard-latest-activity-item .metric-label,
    .portal-sortable-copy small,
    .portal-nav-sort-copy small,
    .portal-nav-sort-group p,
    .portal-nav-sort-status,
    .comm-card p,
    .comm-card small,
    .comm-thread-head p
) {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(
    .section-heading h1,
    .settings-grid .setting-tile h2,
    .dashboard-event-head h3,
    .dashboard-grid .metric-value,
    .dashboard-latest-activity-item .metric-value,
    .builder-shell-card h1,
    .builder-shell-card h2,
    .builder-shell-card h3,
    .builder-shell-card h4,
    .builder-shell-card label,
    .builder-meta-chips strong,
    .portal-sortable-copy strong,
    .portal-nav-sort-copy strong,
    .comm-card h1,
    .comm-card h2,
    .comm-card h3,
    .comm-card h4
) {
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    .input-field,
    input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
    select,
    textarea
) {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] body :where(
    input::placeholder,
    textarea::placeholder,
    .input-field::placeholder
) {
    color: #94a3b8 !important;
    opacity: 1 !important;
}

html[data-theme="dark"] body :where(.user-badge, .header-user) {
    background: rgba(45, 212, 191, 0.16) !important;
    color: #ccfbf1 !important;
    border: 1px solid rgba(45, 212, 191, 0.38) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs {
    background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important;
    border-bottom-color: var(--border) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

/* =======================================================================
   DARK MODE ULTIMATE WINNER (MUST STAY LAST)
   ======================================================================= */
html[data-theme="dark"] body :where(
    .content-card,
    .metric-card,
    .hero-card,
    .card,
    .form-card,
    .table-wrap,
    .table-wrapper,
    .dashboard-event-card,
    .dashboard-latest-activity-item,
    .dashboard-quickstart-card .card,
    .settings-grid .setting-tile,
    .builder-shell-card,
    .builder-settings-panel,
    .builder-settings-panel .dashboard-card,
    .portal-builder-preview-grid .dashboard-card,
    .portal-app-live-card,
    .portal-builder-frame,
    .portal-sortable-item,
    .portal-nav-sort-group,
    .portal-nav-sort-item,
    .comm-shell,
    .comm-card,
    .comm-channel-card,
    .comm-template-card,
    .comm-segment-card,
    .comm-thread-panel,
    .comm-thread-head,
    .comm-reply-box,
    .comm-inbox-sidebar,
    .comm-conversation-item,
    .comm-preview-card
) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    .muted,
    .text-muted,
    .muted-text,
    .helper-text,
    .field-help,
    .dashboard-event-type,
    .dashboard-event-time,
    .dashboard-grid .metric-label,
    .dashboard-grid .metric-card p,
    .dashboard-latest-activity-item p,
    .dashboard-latest-activity-item .metric-label,
    .portal-sortable-copy small,
    .portal-nav-sort-copy small,
    .portal-nav-sort-group p,
    .portal-nav-sort-status,
    .comm-card p,
    .comm-card small,
    .comm-thread-head p
) {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(
    .section-heading h1,
    .settings-grid .setting-tile h2,
    .dashboard-event-head h3,
    .dashboard-grid .metric-value,
    .dashboard-latest-activity-item .metric-value,
    .builder-shell-card h1,
    .builder-shell-card h2,
    .builder-shell-card h3,
    .builder-shell-card h4,
    .builder-shell-card label,
    .builder-meta-chips strong,
    .portal-sortable-copy strong,
    .portal-nav-sort-copy strong,
    .comm-card h1,
    .comm-card h2,
    .comm-card h3,
    .comm-card h4
) {
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
    select,
    textarea,
    .input-field
) {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] body :where(
    input::placeholder,
    textarea::placeholder,
    .input-field::placeholder
) {
    color: #94a3b8 !important;
    opacity: 1 !important;
}

html[data-theme="dark"] body :where(
    .user-badge,
    .header-user
) {
    background: rgba(45, 212, 191, 0.16) !important;
    color: #ccfbf1 !important;
    border: 1px solid rgba(45, 212, 191, 0.38) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs {
    background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important;
    border-bottom-color: var(--border) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(.builder-panel-tab, .comm-tab, .portal-nav-sort-btn, .portal-nav-sort-handle) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(.builder-panel-tab.is-active, .comm-tab.is-active) {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
    color: #052e2b !important;
}

html[data-theme="dark"] body :where(option, optgroup, select option) {
    background: #111827 !important;
    color: #f8fafc !important;
}

/* =======================================================================
   DARK MODE FORCE-WIN BLOCK (FINAL)
   Keep this block LAST in file so dark theme always wins page/module CSS.
   ======================================================================= */
html[data-theme="dark"] body {
    background: var(--bg) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    .content-card,
    .metric-card,
    .hero-card,
    .card,
    .form-card,
    .table-wrap,
    .table-wrapper,
    .dropdown-menu,
    .modal-box,
    .settings-grid .setting-tile,
    .dashboard-event-card,
    .dashboard-latest-activity-item,
    .dashboard-quickstart-card .card,
    .builder-shell-card,
    .builder-settings-panel,
    .builder-settings-panel .dashboard-card,
    .portal-builder-preview-grid .dashboard-card,
    .portal-app-live-card,
    .portal-builder-frame,
    .portal-sortable-item,
    .portal-nav-sort-group,
    .portal-nav-sort-item,
    .comm-shell,
    .comm-card,
    .comm-channel-card,
    .comm-template-card,
    .comm-segment-card,
    .comm-thread-panel,
    .comm-thread-head,
    .comm-reply-box,
    .comm-inbox-sidebar,
    .comm-conversation-item,
    .comm-preview-card
) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    .muted,
    .text-muted,
    .muted-text,
    .helper-text,
    .field-help,
    .field-help code,
    .description,
    .subtitle,
    .meta,
    .empty-state p,
    .dashboard-event-type,
    .dashboard-event-time,
    .dashboard-grid .metric-label,
    .dashboard-grid .metric-card p,
    .dashboard-latest-activity-item p,
    .dashboard-latest-activity-item .metric-label,
    .portal-sortable-copy small,
    .portal-nav-sort-copy small,
    .portal-nav-sort-group p,
    .portal-nav-sort-status,
    .comm-card p,
    .comm-card small,
    .comm-empty,
    .comm-thread-head p,
    .comm-message-bubble p
) {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(
    .settings-grid .setting-tile h2,
    .section-heading h1,
    .dashboard-event-head h3,
    .dashboard-grid .metric-value,
    .dashboard-latest-activity-item .metric-value,
    .builder-shell-card h1,
    .builder-shell-card h2,
    .builder-shell-card h3,
    .builder-shell-card h4,
    .builder-shell-card label,
    .builder-meta-chips strong,
    .portal-sortable-copy strong,
    .portal-nav-sort-copy strong,
    .comm-card h1,
    .comm-card h2,
    .comm-card h3,
    .comm-card h4,
    .comm-conversation-item strong
) {
    color: var(--text) !important;
}

html[data-theme="dark"] body :where(
    input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
    select,
    textarea,
    .input-field,
    .builder-settings-panel .input-field,
    #portalBlockBuilderForm .input-field
) {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] body :where(
    input::placeholder,
    textarea::placeholder,
    .input-field::placeholder
) {
    color: #94a3b8 !important;
    opacity: 1 !important;
}

html[data-theme="dark"] body :where(
    input:focus,
    select:focus,
    textarea:focus,
    .input-field:focus
) {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 3px rgba(45, 212, 191, 0.18) !important;
    outline: none !important;
}

html[data-theme="dark"] body :where(
    .user-badge,
    .header-user
) {
    background: rgba(45, 212, 191, 0.16) !important;
    color: #ccfbf1 !important;
    border: 1px solid rgba(45, 212, 191, 0.38) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs {
    background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important;
    border-bottom-color: var(--border) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(
    .portal-nav-sort-handle,
    .portal-nav-sort-btn,
    .builder-panel-tab,
    .comm-tab
) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body :where(
    .builder-panel-tabs,
    .comm-inbox-filters
) {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] body :where(
    .builder-panel-tab.is-active,
    .comm-tab.is-active
) {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
    color: #052e2b !important;
}

html[data-theme="dark"] body :where(
    select option,
    option,
    optgroup
) {
    background: #111827 !important;
    color: #f8fafc !important;
}

/* Final override block: Admin dashboard dark contrast */
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs {
    background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important;
    border-bottom-color: var(--border) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-card,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-card {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.28) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-head h3,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item .metric-value,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-value,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card .value {
    color: var(--text) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-type,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-time,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item p,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-card p,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-label,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item .metric-label,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card h4 {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-actions {
    color: var(--primary) !important;
}

/* Admin dashboard (mobile stylesheet parity) dark-mode hardening */
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs {
    background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important;
    border-bottom-color: var(--border) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-card,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-card {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.28) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-head h3,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item .metric-value,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-value,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card .value {
    color: var(--text) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-type,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-time,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item p,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-card p,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-grid .metric-label,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-item .metric-label,
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-quickstart-card .card h4 {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-event-actions {
    color: var(--primary) !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-grid::-webkit-scrollbar-track {
    background: #0f172a !important;
}

html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-latest-activity-grid::-webkit-scrollbar-thumb {
    background: #334155 !important;
}

/* Dark Theme Final Contrast Fixes (Builder / Portal Nav / Settings / Worship / Communication) */
html[data-theme="dark"] .user-badge,
html[data-theme="dark"] .header-user {
    background: rgba(45, 212, 191, 0.16) !important;
    color: #ccfbf1 !important;
    border: 1px solid rgba(45, 212, 191, 0.38) !important;
}

html[data-theme="dark"] .settings-grid .setting-tile {
    background: linear-gradient(180deg, #111827 0%, #0f172a 100%) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .settings-grid .setting-tile h2 {
    color: #e2e8f0 !important;
}

html[data-theme="dark"] .settings-grid .setting-tile p {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .settings-grid .setting-link {
    color: #5eead4 !important;
}

html[data-theme="dark"] .theme-preview,
html[data-theme="dark"] .theme-preview-body {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .theme-preview-head {
    background: #0f172a !important;
    color: #f8fafc !important;
}

html[data-theme="dark"] .theme-preview-primary {
    color: #ffffff !important;
}

html[data-theme="dark"] .theme-preview-secondary {
    color: #99f6e4 !important;
    border-color: rgba(45, 212, 191, 0.45) !important;
}

html[data-theme="dark"] .builder-shell-card,
html[data-theme="dark"] .builder-settings-panel,
html[data-theme="dark"] .builder-settings-panel .dashboard-card,
html[data-theme="dark"] .portal-builder-preview-grid .dashboard-card,
html[data-theme="dark"] .portal-app-live-card,
html[data-theme="dark"] .portal-builder-frame,
html[data-theme="dark"] .portal-rich-editor,
html[data-theme="dark"] .portal-editor-toolbar button,
html[data-theme="dark"] .portal-sortable-item,
html[data-theme="dark"] .builder-meta-chips span,
html[data-theme="dark"] .builder-panel-tabs,
html[data-theme="dark"] .builder-panel-tab,
html[data-theme="dark"] .table-wrapper .data-table,
html[data-theme="dark"] .table-wrapper .data-table td,
html[data-theme="dark"] .table-wrapper .data-table th {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .builder-panel-tabs,
html[data-theme="dark"] .portal-nav-sort-group,
html[data-theme="dark"] .comm-inbox-filters {
    background: var(--surface-soft) !important;
}

html[data-theme="dark"] .builder-panel-tab,
html[data-theme="dark"] .portal-nav-sort-btn,
html[data-theme="dark"] .portal-nav-sort-handle {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .builder-panel-tab.is-active {
    color: #f8fafc !important;
    border-color: rgba(45, 212, 191, 0.42) !important;
    box-shadow: 0 0 0 2px rgba(45, 212, 191, 0.12) inset !important;
}

html[data-theme="dark"] .builder-shell-card :where(h1, h2, h3, h4, label, strong),
html[data-theme="dark"] .portal-nav-sort-copy strong,
html[data-theme="dark"] .portal-app-live-card h4 {
    color: var(--text) !important;
}

html[data-theme="dark"] .builder-shell-card :where(p, small, .field-help, .muted-text),
html[data-theme="dark"] .portal-nav-sort-copy small,
html[data-theme="dark"] .portal-sortable-copy small,
html[data-theme="dark"] .portal-app-live-rich {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .builder-settings-panel .input-field,
html[data-theme="dark"] .builder-settings-panel textarea.input-field,
html[data-theme="dark"] .builder-settings-panel select.input-field,
html[data-theme="dark"] #portalBlockBuilderForm .input-field,
html[data-theme="dark"] #portalBlockBuilderForm textarea.input-field,
html[data-theme="dark"] #portalBlockBuilderForm select.input-field,
html[data-theme="dark"] .portal-nav-sort-group input,
html[data-theme="dark"] .portal-nav-sort-group select,
html[data-theme="dark"] .portal-nav-sort-group textarea {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .builder-settings-panel .input-field::placeholder,
html[data-theme="dark"] #portalBlockBuilderForm .input-field::placeholder,
html[data-theme="dark"] .portal-nav-sort-group input::placeholder,
html[data-theme="dark"] .portal-nav-sort-group textarea::placeholder {
    color: #94a3b8 !important;
}

html[data-theme="dark"] .portal-nav-sort-group,
html[data-theme="dark"] .portal-nav-sort-item {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .portal-nav-sort-group h4,
html[data-theme="dark"] .portal-nav-sort-copy strong {
    color: var(--text) !important;
}

html[data-theme="dark"] .portal-nav-sort-group p,
html[data-theme="dark"] .portal-nav-sort-status,
html[data-theme="dark"] .portal-nav-sort-copy small {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .checkbox-line {
    color: var(--text) !important;
}

html[data-theme="dark"] .checkbox-line input[type="checkbox"] {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .checkbox-line input[type="checkbox"]:checked {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
}

html[data-theme="dark"] .comm-shell,
html[data-theme="dark"] .comm-card,
html[data-theme="dark"] .comm-channel-card,
html[data-theme="dark"] .comm-template-card,
html[data-theme="dark"] .comm-segment-card,
html[data-theme="dark"] .comm-thread-panel,
html[data-theme="dark"] .comm-thread-head,
html[data-theme="dark"] .comm-reply-box,
html[data-theme="dark"] .comm-inbox-sidebar,
html[data-theme="dark"] .comm-conversation-item,
html[data-theme="dark"] .comm-preview-card {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .comm-tab {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .comm-tab.is-active {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
    color: #052e2b !important;
}

html[data-theme="dark"] .comm-card :where(h1, h2, h3, h4, strong),
html[data-theme="dark"] .comm-conversation-item {
    color: var(--text) !important;
}

html[data-theme="dark"] .comm-card :where(p, small, .kpi-label, .comm-empty),
html[data-theme="dark"] .comm-message-bubble p,
html[data-theme="dark"] .comm-thread-head p {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .comm-inbox-filters a {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .comm-inbox-filters a.is-active {
    background: var(--surface) !important;
    color: #5eead4 !important;
}

html[data-theme="dark"] .report-list .report-row,
html[data-theme="dark"] .worship-metric-list .report-row {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .report-list .report-row span,
html[data-theme="dark"] .worship-metric-list .report-row span {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .report-list .report-row strong,
html[data-theme="dark"] .worship-metric-list .report-row strong {
    color: var(--text) !important;
}

html[data-theme="dark"] select option,
html[data-theme="dark"] optgroup {
    background: #111827 !important;
    color: #f8fafc !important;
}

/* Strong adaptive text resolver for legacy hardcoded colors */
html[data-theme="dark"] :where(
    .app-content,
    .page-inner,
    .page-shell,
    .site-admin-main,
    .portal-app,
    .portal-page,
    .public-page,
    .report-container
) :where(
    h1, h2, h3, h4, h5, h6,
    p, span, strong, em, b, i,
    label, li, dt, dd, td, th, summary
):not(.badge):not([class*="badge"]):not([class*="status"]):not([class*="btn"]):not([class*="button"]) {
    color: var(--text);
}

html[data-theme="dark"] :where(
    .app-content,
    .page-inner,
    .page-shell,
    .site-admin-main,
    .portal-app,
    .portal-page,
    .public-main,
    .public-site-body,
    .report-container
) :where(
    .card,
    .content-card,
    .metric-card,
    .hero-card,
    .sa-card,
    .form-card,
    .table-wrap,
    .table-wrapper,
    .dropdown-menu,
    .modal-box,
    .public-alert,
    .public-card,
    .worship-card,
    .worship-panel,
    .portal-builder-card
) {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] :where(
    .app-content,
    .page-inner,
    .page-shell,
    .site-admin-main,
    .portal-app,
    .portal-page,
    .public-page,
    .report-container
) :where(
    .muted,
    .text-muted,
    .helper-text,
    .hint,
    .subtitle,
    .description,
    .meta,
    .empty-copy,
    .form-hint,
    .field-help
) {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] :where(
    .app-content,
    .page-inner,
    .page-shell,
    .site-admin-main,
    .portal-app,
    .portal-page,
    .public-page,
    .report-container
) a:not(.button):not(.btn):not([class*="btn-"]):not([class*="button-"]):not(.portal-btn):not(.sa-button) {
    color: var(--primary);
}

/* Inline color overrides (legacy templates/content blocks) */
html[data-theme="dark"] [style*="color:#0f172a" i],
html[data-theme="dark"] [style*="color: #0f172a" i],
html[data-theme="dark"] [style*="color:#111827" i],
html[data-theme="dark"] [style*="color: #111827" i],
html[data-theme="dark"] [style*="color:#1f2937" i],
html[data-theme="dark"] [style*="color: #1f2937" i],
html[data-theme="dark"] [style*="color:#334155" i],
html[data-theme="dark"] [style*="color: #334155" i],
html[data-theme="dark"] [style*="color:#475569" i],
html[data-theme="dark"] [style*="color: #475569" i],
html[data-theme="dark"] [style*="color:#64748b" i],
html[data-theme="dark"] [style*="color: #64748b" i] {
    color: var(--text) !important;
}

html[data-theme="dark"] [style*="color:#94a3b8" i],
html[data-theme="dark"] [style*="color: #94a3b8" i],
html[data-theme="dark"] [style*="color:#cbd5e1" i],
html[data-theme="dark"] [style*="color: #cbd5e1" i] {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] [style*="background:#fff" i],
html[data-theme="dark"] [style*="background: #fff" i],
html[data-theme="dark"] [style*="background:#ffffff" i],
html[data-theme="dark"] [style*="background: #ffffff" i] {
    background: var(--surface) !important;
}

html[data-theme="dark"] [style*="background:#f8fafc" i],
html[data-theme="dark"] [style*="background: #f8fafc" i],
html[data-theme="dark"] [style*="background:#f1f5f9" i],
html[data-theme="dark"] [style*="background: #f1f5f9" i],
html[data-theme="dark"] [style*="background:#f3f4f6" i],
html[data-theme="dark"] [style*="background: #f3f4f6" i] {
    background: var(--surface-soft) !important;
}

/* Native and utility elements that often remain dark-on-dark */
html[data-theme="dark"] code,
html[data-theme="dark"] kbd,
html[data-theme="dark"] pre {
    color: #e2e8f0;
    background: rgba(148, 163, 184, 0.12);
    border-color: rgba(148, 163, 184, 0.22);
}

html[data-theme="dark"] details,
html[data-theme="dark"] summary {
    color: var(--text);
}

html[data-theme="dark"] hr {
    border-color: var(--border);
}

/* Admin members page: explicit dark safety */
html[data-theme="dark"] .section-heading > div > p,
html[data-theme="dark"] .cell-note,
html[data-theme="dark"] .pagination-summary,
html[data-theme="dark"] .empty-state p,
html[data-theme="dark"] .checkbox-line,
html[data-theme="dark"] .person-meta {
    color: var(--text-muted);
}

html[data-theme="dark"] .table-actions a,
html[data-theme="dark"] .table-actions .link-button {
    color: var(--primary);
}

html[data-theme="dark"] .section-heading h1,
html[data-theme="dark"] .empty-state h2,
html[data-theme="dark"] .person-name,
html[data-theme="dark"] .subject-title,
html[data-theme="dark"] .person-cell strong,
html[data-theme="dark"] .table-row td,
html[data-theme="dark"] .table-row th,
html[data-theme="dark"] .pagination-link,
html[data-theme="dark"] .search-form select,
html[data-theme="dark"] .search-form input,
html[data-theme="dark"] .content-card-embedded summary {
    color: var(--text);
}

html[data-theme="dark"] .search-form select,
html[data-theme="dark"] .search-form input,
html[data-theme="dark"] .content-card-embedded,
html[data-theme="dark"] .table-wrap,
html[data-theme="dark"] .table-wrapper,
html[data-theme="dark"] .app-table {
    background: var(--surface);
    border-color: var(--border);
}

html[data-theme="dark"] .app-table thead th {
    background: var(--surface-soft);
    color: var(--text-muted);
}

html[data-theme="dark"] .status-converted {
    background: rgba(45, 212, 191, 0.18);
    color: #99f6e4;
}

html[data-theme="dark"] .actions a,
html[data-theme="dark"] .actions .link-button,
html[data-theme="dark"] .table-actions a,
html[data-theme="dark"] .table-actions .link-button {
    color: var(--primary);
}

/* Global empty/placeholder state readability */
html[data-theme="dark"] .empty-state {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .empty-state h1,
html[data-theme="dark"] .empty-state h2,
html[data-theme="dark"] .empty-state h3,
html[data-theme="dark"] .empty-state strong {
    color: var(--text) !important;
}

html[data-theme="dark"] .empty-state p,
html[data-theme="dark"] .empty-state small,
html[data-theme="dark"] .helper-text {
    color: var(--text-muted) !important;
}

/* Worship module specific dark adjustments */
html[data-theme="dark"] .portal-subnav a {
    background: var(--surface) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .portal-subnav a:hover {
    background: var(--surface-soft) !important;
    border-color: rgba(45, 212, 191, 0.42) !important;
    color: #5eead4 !important;
}

html[data-theme="dark"] .portal-subnav a.is-active {
    background: #0f5fa6 !important;
    border-color: #0f5fa6 !important;
    color: #ffffff !important;
}

html[data-theme="dark"] .report-row {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .report-row span {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .report-row strong {
    color: var(--text) !important;
}

/* Portal App Builder dark-mode hardening */
html[data-theme="dark"] .builder-shell-card,
html[data-theme="dark"] .builder-settings-panel,
html[data-theme="dark"] .builder-settings-panel .dashboard-card,
html[data-theme="dark"] .portal-app-live-card,
html[data-theme="dark"] .portal-builder-frame,
html[data-theme="dark"] .portal-sortable-item,
html[data-theme="dark"] .portal-rich-editor,
html[data-theme="dark"] .portal-editor-toolbar button,
html[data-theme="dark"] .builder-meta-chips span,
html[data-theme="dark"] .builder-panel-tabs,
html[data-theme="dark"] .builder-panel-tab,
html[data-theme="dark"] .field-help code {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .builder-shell-card {
    background: linear-gradient(180deg, #0f172a 0%, #111827 100%) !important;
}

html[data-theme="dark"] .builder-panel-tabs {
    background: var(--surface-soft) !important;
}

html[data-theme="dark"] .builder-panel-tab.is-active {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .builder-meta-chips strong,
html[data-theme="dark"] .builder-settings-panel h3,
html[data-theme="dark"] .builder-settings-panel label,
html[data-theme="dark"] .portal-sortable-copy strong,
html[data-theme="dark"] .portal-app-live-card h4 {
    color: var(--text) !important;
}

html[data-theme="dark"] .builder-meta-chips span,
html[data-theme="dark"] .portal-app-live-rich,
html[data-theme="dark"] .portal-sortable-copy small,
html[data-theme="dark"] .portal-drag-handle,
html[data-theme="dark"] .muted-text,
html[data-theme="dark"] .builder-feedback {
    color: var(--text-muted) !important;
}

html[data-theme="dark"] .builder-settings-panel .input-field,
html[data-theme="dark"] .builder-settings-panel textarea.input-field,
html[data-theme="dark"] #portalBlockBuilderForm .input-field,
html[data-theme="dark"] #portalBlockBuilderForm textarea.input-field,
html[data-theme="dark"] #portalBlockBuilderForm select.input-field {
    background: var(--surface-soft) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .builder-settings-panel .input-field::placeholder,
html[data-theme="dark"] #portalBlockBuilderForm .input-field::placeholder {
    color: #94a3b8 !important;
}

html[data-theme="dark"] .builder-settings-panel .input-field:focus,
html[data-theme="dark"] #portalBlockBuilderForm .input-field:focus,
html[data-theme="dark"] .portal-rich-editor:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 3px rgba(45, 212, 191, 0.2) !important;
}

html[data-theme="dark"] .checkbox-line {
    color: var(--text) !important;
}

html[data-theme="dark"] .checkbox-line input[type="checkbox"] {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
}

html[data-theme="dark"] .checkbox-line input[type="checkbox"]:checked {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
}

/* Preserve contrast for chat/send surfaces after global text adaptation */
html[data-theme="dark"] .portal-chat-bubble.is-mine,
html[data-theme="dark"] .portal-chat-bubble.is-mine :where(h1, h2, h3, h4, h5, h6, p, span, strong, small) {
    color: #ffffff !important;
}

html[data-theme="dark"] .comm-message-bubble.is-outgoing,
html[data-theme="dark"] .comm-message-bubble.is-outgoing :where(h1, h2, h3, h4, h5, h6, p, span, strong, small) {
    color: #e6fffb !important;
}

/* FINAL DARK WINNER (EOF) */
html[data-theme="dark"] body :where(
    .content-card, .metric-card, .hero-card, .card, .form-card, .table-wrap, .table-wrapper,
    .settings-grid .setting-tile, .dashboard-event-card, .dashboard-latest-activity-item, .dashboard-quickstart-card .card,
    .builder-shell-card, .builder-settings-panel, .builder-settings-panel .dashboard-card, .portal-builder-preview-grid .dashboard-card,
    .portal-app-live-card, .portal-builder-frame, .portal-sortable-item, .portal-nav-sort-group, .portal-nav-sort-item,
    .comm-shell, .comm-card, .comm-channel-card, .comm-template-card, .comm-segment-card, .comm-thread-panel, .comm-thread-head,
    .comm-reply-box, .comm-inbox-sidebar, .comm-conversation-item, .comm-preview-card
) { background: var(--surface) !important; border-color: var(--border) !important; color: var(--text) !important; }

html[data-theme="dark"] body :where(
    .muted, .text-muted, .muted-text, .helper-text, .field-help, .dashboard-event-type, .dashboard-event-time,
    .dashboard-grid .metric-label, .dashboard-grid .metric-card p, .dashboard-latest-activity-item p, .dashboard-latest-activity-item .metric-label,
    .portal-sortable-copy small, .portal-nav-sort-copy small, .portal-nav-sort-group p, .portal-nav-sort-status,
    .comm-card p, .comm-card small, .comm-thread-head p
) { color: var(--text-muted) !important; }

html[data-theme="dark"] body :where(
    .section-heading h1, .settings-grid .setting-tile h2, .dashboard-event-head h3, .dashboard-grid .metric-value, .dashboard-latest-activity-item .metric-value,
    .builder-shell-card h1, .builder-shell-card h2, .builder-shell-card h3, .builder-shell-card h4, .builder-shell-card label, .builder-meta-chips strong,
    .portal-sortable-copy strong, .portal-nav-sort-copy strong, .comm-card h1, .comm-card h2, .comm-card h3, .comm-card h4
) { color: var(--text) !important; }

html[data-theme="dark"] body :where(.input-field, input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="file"]):not([type="submit"]):not([type="button"]):not([type="reset"]), select, textarea) {
    background: var(--surface-soft) !important; color: var(--text) !important; border-color: var(--border) !important;
}

html[data-theme="dark"] body :where(input::placeholder, textarea::placeholder, .input-field::placeholder) { color: #94a3b8 !important; opacity: 1 !important; }
html[data-theme="dark"] body :where(.user-badge, .header-user) { background: rgba(45, 212, 191, 0.16) !important; color: #ccfbf1 !important; border: 1px solid rgba(45, 212, 191, 0.38) !important; }
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs { background: linear-gradient(180deg, rgba(11, 17, 32, 0.98), rgba(11, 17, 32, 0.92)) !important; border-bottom-color: var(--border) !important; }
html[data-theme="dark"] body.admin-dashboard-page-shell .dashboard-mobile-tabs a { background: var(--surface) !important; border-color: var(--border) !important; color: var(--text-muted) !important; }

/* PWA directory dark-mode stubborn white guards */
html[data-theme="dark"] .portal-directory-item,
html[data-theme="dark"] .portal-member-list article,
html[data-theme="dark"] .portal-list li,
html[data-theme="dark"] .portal-menu-list a,
html[data-theme="dark"] .portal-giving-impact article {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .portal-directory-item .avatar,
html[data-theme="dark"] .portal-member-list .avatar {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .portal-searchbar button,
html[data-theme="dark"] .portal-searchbar .portal-btn,
html[data-theme="dark"] .portal-inline-action .portal-btn {
    background: var(--surface-soft) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
}

html[data-theme="dark"] .portal-searchbar input:-webkit-autofill,
html[data-theme="dark"] .portal-searchbar input:-webkit-autofill:hover,
html[data-theme="dark"] .portal-searchbar input:-webkit-autofill:focus {
    -webkit-text-fill-color: var(--text) !important;
    box-shadow: 0 0 0 1000px var(--surface-soft) inset !important;
}
