/* Explicit charcoal Dark palette; Light tokens remain the default. */
html[data-theme="dark"] .hmrc-journey-tracker li { color:var(--color-text-muted) !important; }
html[data-theme="dark"] .hmrc-journey-tracker .active { background:var(--color-primary-soft) !important; color:var(--color-text) !important; border-color:var(--color-primary) !important; }
html[data-theme="dark"] :is(.app-status-chip--danger,.status-chip.danger,.badge-danger,.text-bg-danger) { background:var(--color-danger-soft) !important; color:var(--color-danger) !important; border-color:var(--color-danger) !important; }
html[data-theme="dark"] :is(.app-status-chip--warning,.status-chip.warning,.badge-warning,.text-bg-warning) { background:var(--color-warning-soft) !important; color:var(--color-warning) !important; border-color:var(--color-warning) !important; }
html[data-theme="dark"] {
    color-scheme: dark;
    --color-bg: #0b0f14;
    --color-surface: #111827;
    --color-surface-muted: #0e1520;
    --color-surface-raised: #172033;
    --color-border: #243041;
    --color-border-strong: #63758a;
    --color-text: #f9fafb;
    --color-text-muted: #a6b3c5;
    --color-text-subtle: #94a3b8;
    --color-primary: #22d3ee;
    --color-primary-hover: #a0f0f2;
    --color-primary-soft: #123840;
    --color-on-primary: #0e1b2b;
    --color-on-danger: #0e1b2b;
    --color-danger-hover: #ffc3c9;
    --color-focus-rgb: 34, 211, 238;
    --color-success: #82e6ad;
    --color-success-soft: #173e35;
    --color-warning: #ffd17d;
    --color-warning-soft: #493814;
    --color-danger: #ffa7b0;
    --color-danger-soft: #492735;
    --color-info: #94d9ff;
    --color-info-soft: #193c55;
    --color-nav-bg: var(--color-surface);
    --color-nav-text: #f9fafb;
    --color-nav-muted: #a6b3c5;
    --color-nav-hover: #172033;
    --color-nav-active: #123840;
    --color-nav-active-text: #91eee4;
    --color-nav-accent: #22d3ee;
    --color-nav-on-accent: #06232d;
    --app-surface: var(--color-surface);
    --app-surface-subtle: var(--color-surface-muted);
    --app-text: var(--color-text);
    --app-text-muted: var(--color-text-muted);
    --app-border-color: var(--color-border);
    /* Compatibility tokens used by the ledger and its embedded Account tools. */
    --surface-card: var(--color-surface);
    --border-subtle: var(--color-border);
    --text-primary: var(--color-text);
    --text-muted: var(--color-text-muted);
    --color-grid-divider: #63758a;
    --bs-body-bg: var(--color-bg);
    --bs-body-color: var(--color-text);
    --bs-secondary-color: var(--color-text-muted);
    --bs-tertiary-bg: var(--color-surface-muted);
    --bs-secondary-bg: var(--color-surface-raised);
    --bs-border-color: var(--color-border);
    --bs-heading-color: var(--color-text);
    --bs-primary: var(--color-primary);
    --bs-primary-rgb: var(--color-focus-rgb);
    --bs-link-color: var(--color-primary);
    --bs-link-hover-color: var(--color-primary-hover);
    --bs-emphasis-color: var(--color-text);
    --bs-light-rgb: 17, 24, 39;
    --bs-dark-rgb: 249, 250, 251;
}

/* Legacy components use isolated fixed colours. Keep these compatibility rules
   scoped to Dark and reuse the same semantic palette across every workspace. */
html[data-theme="dark"] body { color: var(--color-text); }
html[data-theme="dark"] :is(.page:has(> .main-content), .sole-overview, .page-content-placeholder) { background: var(--color-bg) !important; color: var(--color-text) !important; }
html[data-theme="dark"] :where(.top-row, .account-tools, .account-tool-card, .journey-tool, .account-panel, .account-summary-strip article, .account-status-card, .account-loading-card,
    .overview-card, .overview-detail, .dashboard-chart, .tax-estimate, .panel, .metric-card, .app-panel, .app-card, .journey-card, .card, .simple-metrics article, .journey-metrics article, .tax-block,
    .app-page-header, .business-grid-footer, .business-action-menu, .business-action-menu button,
    .account-dropdown, .account-tabs, .dropdown-menu, .popover, .modal-content, .app-modal, .account-tool-dialog,
    .account-tool-body, .account-tool-dialog footer, .taxflow-modal-header,
    [class$="-panel"], [class$="-card"], [class$="-modal-header"], [class$="-modal-footer"]) {
    background: var(--color-surface) !important;
    border-color: var(--color-border) !important;
    color: var(--color-text) !important;
}
html[data-theme="dark"] :is(h1,h2,h3,h4,h5,h6, label, legend, dt, dd, .top-row-title, .workspace-context-field strong, .metric-card strong,
    .overview-metrics strong, .simple-metrics strong, .account-status-card strong, .account-summary-strip strong, .account-menu-toggle, .account-dropdown a, .account-dropdown button, .account-tabs button) { color: var(--color-text) !important; }
html[data-theme="dark"] :is(.top-row-subtitle, .workspace-context-field, .eyebrow, .subtle, .business-caption, .currency-label,
    .metric-label, .metric-card small, .text-muted, .text-secondary, .form-text, .taxflow-modal-header__product, .taxflow-modal-header__copy p,
    .account-tools p, .account-panel p, .account-status-card small, .account-note, .account-summary-strip p, .account-summary-strip span, .account-tool-card p, .account-summary-card p, .account-page-header p, .simple-card-heading p, .simple-metrics span, .journey-metrics span,
    .overview-metrics span, .overview-metrics small, .submission-totals dt, .dashboard-chart p, .dashboard-chart .month,
    .dashboard-chart .legend span:last-child, .business-grid-toolbar label, .panel-header p, .journey-tool span) { color: var(--color-text-muted) !important; }
 html[data-theme="dark"] :is(.account-tool-icon, .account-title-icon, .account-summary-icon, .summary-icon, .app-page-title-icon, .receipt-detail) {
    background: var(--color-surface-muted) !important; color: var(--color-primary) !important;
}
html[data-theme="dark"] :is(.header-icon-button, .account-menu-toggle, .page-link) {
    background: var(--color-surface) !important; border-color: var(--color-border) !important; color: var(--color-text) !important;
}
html[data-theme="dark"] .page-item.active .page-link {
    background: var(--color-primary) !important; border-color: var(--color-primary) !important; color: var(--color-bg) !important;
}
html[data-theme="dark"] :is(.header-icon-button:hover, .dropdown-item:hover,
    .account-dropdown a:hover, .account-dropdown button:hover, .business-action-menu button:hover, .business-action-menu button:focus-visible,
    .account-tabs button.active, .account-tabs button[aria-selected="true"]) { background: var(--color-primary-soft) !important; color: var(--color-primary) !important; }
html[data-theme="dark"] :is(.form-control, .form-select, textarea, select, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="submit"]):not([type="button"])) {
    background-color: var(--color-surface-muted) !important; color: var(--color-text) !important; border-color: var(--color-border-strong) !important;
}
html[data-theme="dark"] :is(input,textarea)::placeholder { color: var(--color-text-subtle) !important; opacity: 1; }
html[data-theme="dark"] :is(input, select, textarea, button, a):focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }
html[data-theme="dark"] .form-select { --bs-form-select-bg-img: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23edf4ff' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/%3e%3c/svg%3e"); }
html[data-theme="dark"] :is(table, .table) { --bs-table-color: var(--color-text); --bs-table-bg: var(--color-surface); --bs-table-border-color: var(--color-grid-divider); --bs-table-striped-bg: var(--color-surface-muted); --bs-table-striped-color: var(--color-text); --bs-table-hover-bg: var(--color-surface-raised); --bs-table-hover-color: var(--color-text); }
html[data-theme="dark"] :is(th,td) { background-color: var(--color-surface) !important; color: var(--color-text) !important; border-color: var(--color-grid-divider) !important; }
html[data-theme="dark"] th { background-color: var(--color-surface-muted) !important; color: var(--color-text-muted) !important; }
html[data-theme="dark"] :is(.alert-success, .status-chip:not(.info):not(.warning):not(.danger), .badge-success) { background: var(--color-success-soft) !important; color: var(--color-success) !important; border-color: var(--color-success) !important; }
html[data-theme="dark"] .alert-warning { background: var(--color-warning-soft) !important; color: var(--color-warning) !important; border-color: var(--color-warning) !important; }
html[data-theme="dark"] :is(.alert-danger, .validation-message, .invalid-feedback) { color: var(--color-danger) !important; }
html[data-theme="dark"] .alert-danger { background: var(--color-danger-soft) !important; border-color: var(--color-danger) !important; }
html[data-theme="dark"] .alert-info { background: var(--color-info-soft) !important; color: var(--color-info) !important; border-color: var(--color-info) !important; }
html[data-theme="dark"] .tooltip { --bs-tooltip-bg: var(--color-text); --bs-tooltip-color: var(--color-bg); }
html[data-theme="dark"] .popover { --bs-popover-body-color: var(--color-text); --bs-popover-header-bg: var(--color-surface-muted); --bs-popover-header-color: var(--color-text); }
html[data-theme="dark"] :is(.bars, .submission-totals > div, .business-column-settings, .account-tabs) { border-color: var(--color-border) !important; }
html[data-theme="dark"] .income-bar { background: var(--color-primary) !important; }
html[data-theme="dark"] .dashboard-chart .legend span:first-child { color: var(--color-primary) !important; }
html[data-theme="dark"] .expense-bar { background: var(--color-text-muted) !important; }
html[data-theme="dark"] .theme-logo--light { display:none; }
html[data-theme="dark"] .income-tax-review { --tax-line:var(--color-border); --tax-muted:var(--color-text-muted); --tax-teal:var(--color-primary); --tax-number:var(--color-primary-soft); --tax-amount:var(--color-surface-raised); }
html[data-theme="dark"] :is(.journey-period, .journey-note, .business-filter-popover, .business-filter-popover-panel, .business-filter-menu) { background:var(--color-surface-muted) !important; color:var(--color-text) !important; border-color:var(--color-border) !important; }
html[data-theme="dark"] :is(.journey-period.selected, .journey-period:hover, .journey-tag) { background:var(--color-primary-soft) !important; color:var(--color-primary) !important; }
html[data-theme="dark"] :is(.journey-date, .journey-steps li.active) { color:var(--color-primary) !important; }
html[data-theme="dark"] .journey-steps li { color:var(--color-text-muted); }
html[data-theme="dark"] .journey-steps li span { background:var(--color-surface-raised); box-shadow:0 0 0 7px var(--color-bg); }
html[data-theme="dark"] .journey-tag.success { background:var(--color-success-soft) !important; color:var(--color-success) !important; }
html[data-theme="dark"] .btn-close { filter:invert(1) grayscale(100%) brightness(200%); }
html[data-theme="dark"] :is(.period, .period strong) { color:var(--color-text) !important; border-color:var(--color-border); }
html[data-theme="dark"] :is(.period div > span, .period-status) { color:var(--color-text-muted) !important; }
html[data-theme="dark"] .period.accepted .period-status { color:var(--color-success) !important; }
html[data-theme="dark"] .period:hover { background:var(--color-surface-raised); }
html[data-theme="dark"] :is(.tax-row-number, .tax-chip, .tax-notice, .tax-figure-row.is-editing) { background:var(--color-primary-soft); color:var(--color-primary); }
html[data-theme="dark"] :is(.tax-review-totals, .tax-chip-neutral) { background:var(--color-surface-muted); color:var(--color-text-muted); }
html[data-theme="dark"] .tax-result--accepted { background:var(--color-success-soft); }
html[data-theme="dark"] .tax-result--attention { background:var(--color-warning-soft); }
html[data-theme="dark"] :is(.tax-inline-confirm, .tax-modes button[aria-pressed=true]) { background:var(--color-primary); color:var(--color-bg); }

/* Ledger totals and history use class lists that do not match the legacy suffix rules. */
html[data-theme="dark"] :is(.sole-trader-summary article, .sole-trader-summary .profit, .hmrc-history-grid, .support-card,
    .businesses-page-header, .business-register, .business-workspace, .business-tab-content, .business-kpi,
    .business-modal, .tenant-modal, .journey-modal, .sole-trader-modal, .hmrc-guided-card, .setup-preparation article,
    .business-modal-footer, .tenant-modal > footer, .journey-actions, .sole-trader-modal footer,
    .analytics-header, .analytics-filter-row, .analytics-report-section details) {
    background: var(--color-surface) !important; color: var(--color-text) !important; border-color: var(--color-border) !important;
}
html[data-theme="dark"] :is(.account-copy strong, .account-caret, .sole-trader-summary strong,
    .business-kpi strong, .business-reconcile-summary strong, .business-record-summary strong, .business-table-empty strong, .notification-preference-grid article, .business-tool-list button, .business-advanced-tools > summary, .business-link-details > summary,
    .business-section-heading, .business-workspace-heading, .business-tab-heading, .tenant-modal-close,
    .analytics-kpi-card strong, .analytics-report-section summary) { color: var(--color-text) !important; }
html[data-theme="dark"] :is(.account-copy span, .support-card p, .editor-note, .editor-grid small, .editor-footer span,
    .business-tab small, .business-tab-heading p, .business-table-empty span, .business-table-footer, .business-reconcile-summary span, .business-record-summary span, .account-status-card p, .business-tool-list small, .business-kpi small, .business-kpi em, .business-readiness p,
    .business-details-help, .business-details-footer p, .business-action-grid p, .business-hmrc-note p,
    .analytics-kpi-card span, .analytics-kpi-card small, .analytics-chart-header p, .analytics-latest-time) { color: var(--color-text-muted) !important; }
html[data-theme="dark"] :is(.business-tab, .business-reconcile-summary, .business-record-summary, .business-action-grid article,
    .business-controls dl > div, .business-mapping-journey > div > span, .business-journey > div > span,
    .hmrc-route button span, .bar-track, .tenant-security-note, .people-tabs button.active) {
    background: var(--color-surface-muted) !important; color: var(--color-text) !important; border-color: var(--color-border) !important;
}
html[data-theme="dark"] :is(.business-context-banner, .business-tour, .business-workspace-name, .business-why,
    .business-info-strip, .business-hmrc-note, .hmrc-environment, .setup-assurance, .setup-handoff,
    .hmrc-route li.current, .business-tab.active, .business-tab:hover, .tenant-tab.active, .editor-tabs button.active) {
    background: var(--color-primary-soft) !important; color: var(--color-primary) !important; border-color: var(--color-primary) !important;
}
html[data-theme="dark"] :is(.business-empty-icon, .business-row-icon, .business-kpi > span) { background:var(--color-primary-soft) !important; color:var(--color-primary) !important; }
html[data-theme="dark"] :is(.business-readiness, .business-readiness > span) { background:var(--color-warning-soft) !important; color:var(--color-warning) !important; border-color:var(--color-warning) !important; }
html[data-theme="dark"] :is(.business-readiness.is-ready, .business-readiness.is-ready > span, .business-context-check, .sole-trader-type.income) { background:var(--color-success-soft) !important; color:var(--color-success) !important; }
html[data-theme="dark"] .sole-trader-type.expense { background:var(--color-danger-soft) !important; color:var(--color-danger) !important; }
html[data-theme="dark"] :is(.editor-tabs button, .hmrc-route button, .business-context-banner p) { color:var(--color-text-muted) !important; }
html[data-theme="dark"] :is(.app-required-marker, .sole-trader-required, .editor-grid label span, .text-danger) { color:var(--color-danger) !important; }
html[data-theme="dark"] :is(.business-tab.active, .editor-tabs button.active) { box-shadow:inset 0 -2px 0 var(--color-primary); }
html[data-theme="dark"] .financial-charts .income-key { color:var(--color-primary); }
html[data-theme="dark"] .financial-charts .expense-key { color:var(--color-text-muted); }

/* Preserve existing border widths and layout; only recolour visible grid rules. */
html[data-theme="dark"] :is(table, thead, tbody, tfoot, tr, .app-data-table-wrapper, .sole-trader-table-wrapper,
    .business-grid-toolbar, .business-grid-footer, .hmrc-history-footer, .business-filter-summary,
    .business-column-settings, .business-resize, .sole-trader-ledger-heading, .business-table-toolbar, .business-table-footer) {
    border-color: var(--color-grid-divider) !important;
}
html[data-theme="dark"] :is(tr:hover > td, tr.selected > td, tr.is-selected > td, .business-selected-row > td) { background:var(--color-surface-raised) !important; }
/* One separator token and thickness for native and shared record grids. */
html[data-theme="dark"] table :is(th, td) { border-bottom:1px solid var(--color-grid-divider) !important; }
html[data-theme="dark"] table th:not(:last-child) { border-right:1px solid var(--color-grid-divider) !important; }
html[data-theme="dark"] :is(.business-resize, .standard-grid-resize),
html[data-theme="dark"] .standard-grid-table th::after { border-right:1px solid var(--color-grid-divider) !important; opacity:1; }
html[data-theme="dark"] .text-bg-light { background:var(--color-surface-muted) !important; color:var(--color-text) !important; }
html[data-theme="dark"] .readiness-list strong { color:var(--color-success); }
html[data-theme="dark"] .hmrc-guided { color:var(--color-text); }
html[data-theme="dark"] :is(.hmrc-guided > header p, .hmrc-route small) { color:var(--color-text-muted); }
html[data-theme="dark"] .hmrc-route button span { box-shadow:0 0 0 7px var(--color-surface); }
html[data-theme="dark"] .hmrc-route li:not(:last-child)::after { background:var(--color-border); }
html[data-theme="dark"] :is(.hmrc-route li.current button span, .hmrc-route li.complete button span) { background:var(--color-primary) !important; color:var(--color-bg); }

/* Backdrops are overlays, not card surfaces. Keep the page visible beneath them. */
html[data-theme="dark"] :is(.sole-trader-modal-shell, .business-modal-backdrop, .journey-modal-backdrop, .tenant-modal-backdrop) { background:rgba(3,12,23,.65) !important; }
html[data-theme="dark"] .business-warning-list li { background:var(--color-warning-soft); color:var(--color-warning); border-color:var(--color-warning); }
html[data-theme="dark"] .journey-confirm { background:var(--color-surface-muted); color:var(--color-text); border:1px solid var(--color-border-strong); }
html[data-theme="dark"] :is(.import-destination, .import-upload, .import-managed, .import-field-grid, .import-table-scroll) { background:var(--color-surface-muted); color:var(--color-text); border-color:var(--color-border-strong); }
html[data-theme="dark"] :is(.import-destination > span, .import-upload p, .import-upload small, .import-field small, .import-required, .import-sample, .import-managed span, .import-defaults p, .import-guidance, .import-preview-heading > span, .import-validate > span, .import-matches > summary span, .import-defaults > summary span, .import-match-heading p, .import-steps > span) { color:var(--color-text-muted); }
html[data-theme="dark"] :is(.import-field, .import-managed, .import-defaults, .import-steps) { border-color:var(--color-border); }
html[data-theme="dark"] .import-match-badge { background:var(--color-success-soft); color:var(--color-success); }
html[data-theme="dark"] .import-steps .active { color:var(--color-primary); }
html[data-theme="dark"] .import-steps .complete { color:var(--color-success); }
html[data-theme="dark"] :is(th, td) { border-left-color:var(--color-grid-divider) !important; border-right-color:var(--color-grid-divider) !important; }
html[data-theme="dark"] .business-resize { border-right-color:var(--color-grid-divider) !important; }
html[data-theme="dark"] input:is([type="checkbox"], [type="radio"]) { accent-color:var(--color-primary); }
html[data-theme="dark"] .app-data-table td small { color:var(--color-text-muted) !important; }

html[data-theme="dark"] table > tbody:not(:has(tr[data-grid-last-row])) > tr:nth-last-child(1 of :not([hidden]):not([data-grid-hidden])) > td,
html[data-theme="dark"] table > tbody > tr[data-grid-last-row] > td { border-bottom-width:0 !important; }
