/* Loaded after page styles so shared surfaces also cover legacy inline colours. */
[data-theme="dark"] :is(header, footer) {
    color: var(--text-dark);
    border-color: var(--border-color);
    box-shadow: none;
}

[data-theme="dark"] :is(h1, h2, h3, h4, h5, h6) {
    color: var(--text-dark);
}
[data-theme="dark"] h1 a,
[data-theme="dark"] [data-card-grid-id] :is(h1, h2, h3, h4, h5, h6) {
    color: var(--text-dark) !important;
}
[data-theme="dark"] a:not(.btn):not(.pure-button):not(.login-btn):not(.nav-back) {
    color: var(--link-color);
}
[data-theme="dark"] :is(section, .card, .login-card, .gallery-card, .chart-box,
    .weekday-card, .date-picker, .dropdown-menu, .no-data, .panel, .table-responsive) {
    background: var(--bg-white);
    color: var(--text-dark);
    border-color: var(--border-color);
}
[data-theme="dark"] :is(.gallery-thumbnail, .loading-overlay) {
    background: var(--bg-light);
}

[data-theme="dark"] [data-card-grid-id] > :is(a, .card) {
    background: linear-gradient(145deg, var(--surface-raised), var(--bg-white)) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: 0 6px 20px rgb(0 0 0 / 16%) !important;
}
[data-theme="dark"] [data-card-grid-id] > :is(a, .card):hover {
    background: var(--surface-hover) !important;
    border-color: #5681b4 !important;
}
[data-theme="dark"] [data-card-grid-id] :is(p, .desc) {
    color: var(--text-light) !important;
}

/* Restrict legacy overrides to neutral surfaces; retain warning/success colours. */
[data-theme="dark"] :is(
    [style*="background:white;" i], [style*="background: white;" i],
    [style*="background:#fff;" i], [style*="background: #fff;" i],
    [style*="background:#ffffff;" i], [style*="background: #ffffff;" i],
    [style*="background-color: #fff;" i], [style*="background-color: white;" i],
    [style*="background: #f8fafc;" i], [style*="background:#f8fafc;" i],
    [style*="background: #f9fafb;" i], [style*="background: #f3f4f6;" i],
    [style*="background:#f3f4f6;" i], [style*="background: #f8f9fa;" i],
    [style*="background: #e5e7eb;" i], [style*="background:#e5e7eb;" i],
    [style*="background: #e2e8f0;" i], [style*="background: #f9f9f9;" i]
) {
    background: var(--bg-white) !important;
    border-color: var(--border-color) !important;
}
[data-theme="dark"] #menu-toggle > div { background: var(--text-dark) !important; }
[data-theme="dark"] :is(
    [style*="background: #e8f4f8;" i], [style*="background: #eff6ff;" i],
    [style*="background:#eff6ff;" i], [style*="background: #e0f2fe;" i]
) {
    background: #172f4a !important;
    border-color: #345b83 !important;
    color: #bfdbfe;
}
[data-theme="dark"] :is(
    [style*="color: #333;" i], [style*="color:#333;" i],
    [style*="color: #1f2937;" i], [style*="color:#1f2937;" i],
    [style*="color: #334155;" i], [style*="color: #374151;" i],
    [style*="color:#374151;" i]
) {
    color: var(--text-dark) !important;
}
[data-theme="dark"] :is(
    [style*="color: #666;" i], [style*="color:#666;" i],
    [style*="color: #6b7280;" i], [style*="color:#6b7280;" i],
    [style*="color: #9ca3af;" i], [style*="color:#9ca3af;" i]
) {
    color: var(--text-light) !important;
}

[data-theme="dark"] :is(input:not([type="checkbox"]):not([type="radio"]):not([type="color"]):not([type="range"]):not([type="submit"]):not([type="button"]),
    select, textarea) {
    background: var(--bg-light) !important;
    color: var(--text-dark) !important;
    border-color: var(--border-color) !important;
}
[data-theme="dark"] :is(input, textarea)::placeholder { color: #94a3b8; opacity: 1; }
[data-theme="dark"] :is(input, select, textarea) { accent-color: #60a5fa; }
[data-theme="dark"] :is(a, button, input, select, textarea):focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 3px;
}
[data-theme="dark"] :is(input, select, textarea):disabled { opacity: 0.65; }

[data-theme="dark"] table { color: var(--text-dark); background: var(--bg-white); }
[data-theme="dark"] :is(th, td) { border-color: var(--border-color) !important; }
[data-theme="dark"] thead,
[data-theme="dark"] thead th {
    background: var(--surface-raised) !important;
    color: var(--text-dark) !important;
}
[data-theme="dark"] .pure-table-striped tr:nth-child(2n-1) td {
    background: var(--surface-raised);
}
[data-theme="dark"] :is(.preset-btn, .pure-button, .card-view-btn, .grid-size-btn) {
    background: var(--surface-raised);
    color: var(--text-dark);
    border-color: var(--border-color);
}
[data-theme="dark"] :is(.preset-btn.active, .card-view-btn.active, .grid-size-btn.active) {
    background: #2563eb !important;
    border-color: #60a5fa !important;
    color: #fff !important;
}
[data-theme="dark"] .pure-button-primary {
    background: #2563eb !important;
    color: #fff !important;
}
[data-theme="dark"] .tab-button { color: var(--text-light); }
[data-theme="dark"] .tab-button.active { color: var(--link-color); }
[data-theme="dark"] .tabs { border-color: var(--border-color); }
[data-theme="dark"] #weekly-avg-box {
    background: #172f4a !important;
    border-color: #345b83 !important;
    color: #bfdbfe !important;
}
[data-theme="dark"] .login-card {
    border: 1px solid var(--border-color);
    box-shadow: 0 24px 64px rgb(0 0 0 / 40%);
}
[data-theme="dark"] .login-overlay { background: rgb(3 7 18 / 78%); }
[data-theme="dark"] .login-btn {
    background: #2563eb;
    color: #fff;
}
[data-theme="dark"] .login-btn-secondary { background: #334155; }
[data-theme="dark"] :is(a, button).nav-back {
    background: rgb(28 42 62 / 85%);
    border-color: #3b4f6b;
    color: var(--text-dark);
    box-shadow: 0 6px 18px rgb(0 0 0 / 25%);
}
[data-theme="dark"] :is(a, button).nav-back:hover {
    background: var(--surface-hover);
    border-color: var(--primary-light);
    color: #fff;
}

[data-theme="dark"] :is(#slide-menu, #notifications-panel, .route-crumb-dropdown) {
    background: var(--bg-white) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: 0 18px 48px rgb(0 0 0 / 35%) !important;
}
[data-theme="dark"] #slide-menu :is(a, button) { color: var(--text-dark) !important; }
[data-theme="dark"] #nav-menu a:hover {
    background: var(--surface-hover) !important;
}
[data-theme="dark"] #route-breadcrumbs {
    background: var(--header-end);
    border-color: var(--border-color);
}
[data-theme="dark"] .route-crumb {
    background: var(--surface-raised);
    color: var(--text-dark);
    border-color: var(--border-color);
}
[data-theme="dark"] .route-crumb.current {
    background: #25466d;
    color: #dbeafe;
    border-color: #416996;
}
[data-theme="dark"] .route-crumb-option:hover { background: var(--surface-hover); }
[data-theme="dark"] .notification-item { border-color: var(--border-color); }
[data-theme="dark"] .notification-item.is-unread { background: #172f4a; }

/* Home navigation: muted tinted surfaces keep each section's colour without pastel glare. */
[data-theme="dark"] #main-nav .nav-card {
    background: linear-gradient(150deg,
        color-mix(in srgb, var(--nav-accent, #3b82f6) 18%, var(--surface-raised)),
        var(--bg-white) 75%) !important;
    border: 1px solid color-mix(in srgb, var(--nav-accent, #3b82f6) 40%, var(--border-color)) !important;
}
[data-theme="dark"] #main-nav .nav-card:hover {
    border-color: var(--nav-accent, #60a5fa) !important;
    box-shadow: 0 10px 28px rgb(0 0 0 / 35%) !important;
}
[data-theme="dark"] #main-nav .card-label { color: var(--text-dark) !important; }
[data-theme="dark"] .home-nav-section { color: var(--text-light) !important; border-color: #2f4a6e !important; }

[data-theme="dark"] .page-card-view-controls {
    background: var(--surface-raised);
    border-color: var(--border-color);
    box-shadow: none;
}
[data-theme="dark"] .page-card-view-label { color: var(--text-light); }
[data-theme="dark"] .page-card-view-controls .card-view-btn:not(.active) { color: var(--text-light); }

/* Category/type row tints: blend toward the dark surface so text stays readable. */
[data-theme="dark"] tbody tr.category-tinted {
    background-color: color-mix(in srgb, var(--category-pastel, transparent) 14%, var(--bg-white));
}
[data-theme="dark"] tbody tr[style*="background-color"] {
    background-image: linear-gradient(rgb(21 31 48 / 86%), rgb(21 31 48 / 86%));
}
[data-theme="dark"] tbody tr:hover { background-color: var(--surface-hover); }
[data-theme="dark"] tbody tr.selected { background-color: #2a2752; }
[data-theme="dark"] tbody tr.editing { background-color: #3a2f12; }
[data-theme="dark"] :is(.hp-type-1, .hp-type-asset) { background: #15263d; }
[data-theme="dark"] :is(.hp-type-2, .hp-type-item) { background: #2a2414; }
[data-theme="dark"] :is(.hp-type-3, .hp-type-bill) { background: #2d1a28; }

/* House payments / mortgage / mortgage tools. */
[data-theme="dark"] {
    --hmt-bg-a: var(--bg-light);
    --hmt-bg-b: var(--bg-light);
    --hmt-surface: var(--bg-white);
    --hmt-text: var(--text-dark);
    --hmt-muted: var(--text-light);
    --hmt-border: var(--border-color);
    --hmt-info: var(--link-color);
    --hmt-value-payment: #7dd3fc;
    --hmt-value-total: #c4b5fd;
    --hmt-value-interest: #fdba74;
    --hmt-value-principal: #86efac;
    --hmt-value-balance: #fca5a5;
}
[data-theme="dark"] :is(.hp-tab, .hm-tab, .hmt-tab) {
    background: var(--surface-raised);
    color: var(--text-light);
    border-color: var(--border-color);
}
[data-theme="dark"] :is(.hp-tab, .hm-tab, .hmt-tab):hover { background: var(--surface-hover); color: var(--text-dark); }
[data-theme="dark"] :is(.hp-tab, .hm-tab, .hmt-tab).active {
    background: #172f4a;
    color: #dbeafe;
    border-color: #3b82f6;
}
[data-theme="dark"] :is(.hp-filters, .hp-toolbar, .hp-table-wrap, .hp-graph-shell, #hp-graph,
    .hp-pagination, .hm-filterbar, .hm-toolbar, .hm-table-wrap, .hmt-proj-card, .hmt-plot) {
    background: var(--bg-white);
    border-color: var(--border-color);
    color: var(--text-dark);
}
[data-theme="dark"] :is(.hp-filters label, .hm-filterbar label, .hp-card .k, #hm-page-info,
    .hmt-graph-meta, .hmt-proj-card p) {
    color: var(--text-light);
}
[data-theme="dark"] :is(.hmt-proj-card h4, .hmt-proj-card .hmt-proj-value) { color: var(--text-dark); }
[data-theme="dark"] .hmt-proj-card .hmt-proj-value.hmt-money-neg { color: #fca5a5; }
[data-theme="dark"] :is(.hp-pagination button, .hm-actions button, .hmt-btn-slate, .hm-pagination button) {
    background: var(--surface-raised);
    color: var(--text-dark);
    border-color: var(--border-color);
}
[data-theme="dark"] .hp-card.kind-person { background: #15263d; border-color: #2f5585; }
[data-theme="dark"] .hp-card.kind-all { background: #2a2414; border-color: #6b5420; }
[data-theme="dark"] .hp-card.kind-gap { background: #2d1a1d; border-color: #7a3238; }
[data-theme="dark"] .hp-card .p { color: #fcd34d; }
[data-theme="dark"] :is(.hm-money-neg, .hp-money-neg, [style*="color: #cc0000" i]) { color: #f87171 !important; }
[data-theme="dark"] [style*="color: #0066cc" i] { color: #60a5fa !important; }

/* Leaflet controls sit on light chrome by default. */
[data-theme="dark"] .leaflet-bar a {
    background: var(--surface-raised);
    color: var(--text-dark);
    border-color: var(--border-color);
}
[data-theme="dark"] .leaflet-bar a:hover { background: var(--surface-hover); }
[data-theme="dark"] .leaflet-control-attribution { background: rgb(12 18 32 / 80%) !important; color: var(--text-light); }

.notification-item {
    display: flex; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border-color);
}
.notification-item.is-unread { background: #eff6ff; }
.notification-content { min-width: 0; }
.notification-summary { font-size: 0.875em; color: var(--text-dark); }
.notification-time { font-size: 0.75em; color: var(--text-light); margin-top: 2px; }
.notification-empty { padding: 20px; text-align: center; color: var(--text-light); font-size: 0.9em; }

.login-card { min-width: 0; max-width: min(400px, calc(100vw - 24px)); }
#slide-menu { width: min(85vw, 300px) !important; }
#notifications-panel { width: min(320px, calc(100vw - 32px)) !important; }
#dark-mode-toggle, #notifications-toggle { min-width: 44px; min-height: 44px; }
.theme-sync-status {
    position: fixed; bottom: 16px; right: 16px; z-index: 10001;
    width: min(420px, calc(100vw - 32px)); padding: 14px 16px;
    background: var(--bg-white); color: var(--text-dark);
    border: 1px solid var(--border-color); border-left: 4px solid #d97706;
    border-radius: 10px; box-shadow: 0 8px 24px rgb(0 0 0 / 20%);
}
.theme-sync-status button {
    display: block; margin-top: 8px; padding: 8px 12px; min-height: 44px;
    border: 1px solid var(--border-color); border-radius: 6px;
    background: var(--bg-light); color: var(--text-dark); cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
    [data-card-grid-id] > :is(a, .card), .gallery-card, .gallery-thumbnail img,
    #slide-menu, .login-btn, .login-btn-secondary {
        transition: none !important;
        transform: none !important;
    }
}
