/* Theme overrides are opt-in. Keep the original light palette in styles.css. */
.theme-toggle { flex: 0 0 auto; white-space: nowrap; }
.theme-toggle svg { width: 18px; height: 18px; flex: 0 0 18px; }

@media (max-width: 700px) {
  .utility-bar .theme-toggle { width: 38px; min-width: 38px; padding-inline: 0; }
  .theme-toggle-label { display: none; }
}

html[data-theme="dark"] {
  color-scheme: dark;
  --theme-page: #151c18;
  --theme-surface: #1d2721;
  --theme-raised: #26332b;
  --theme-inset: #17201b;
  --theme-hover: #334438;
  --theme-text: #eef0e8;
  --theme-muted: #b2beb4;
  --theme-subtle: #9fac9f;
  --theme-line: #425649;
  --theme-control-line: #6c8070;
  --theme-accent: #bfd783;
  --theme-accent-hover: #d0e3a1;
  --theme-on-accent: #1b2717;
  --theme-action: #365740;
  --theme-action-hover: #446d50;
  --theme-success: #a2d9b5;
  --theme-success-bg: #243d30;
  --theme-warning: #e8bd7c;
  --theme-warning-bg: #3a3020;
  --theme-warning-line: #856842;
  --theme-error: #f1a59a;
  --theme-error-bg: #432a28;
  --theme-info: #9ed1d0;
  --theme-info-bg: #213b3b;
  --theme-shadow: 0 18px 60px rgba(0, 0, 0, .18);

  --ink: var(--theme-text);
  --ink-soft: var(--theme-muted);
  --paper: var(--theme-page);
  --card: var(--theme-surface);
  --line: var(--theme-line);
  --acid: var(--theme-accent);
  --acid-dark: var(--theme-accent);
  --green: var(--theme-success);
  --mint: var(--theme-success-bg);
  --amber: var(--theme-warning);
  --amber-bg: var(--theme-warning-bg);
  --danger: var(--theme-error);
  --warning: var(--theme-warning);
  --white: var(--theme-text);
  --shadow: var(--theme-shadow);
  background: var(--theme-page);
}

html[data-theme="dark"] body { background: var(--theme-page); color: var(--theme-text); }
html[data-theme="dark"] ::selection { color: var(--theme-text); background: #48643d; }
html[data-theme="dark"] .page-grain { opacity: .06; }
html[data-theme="dark"] :is(.utility-bar, .topbar, .period-actions, .hero-stamp),
html[data-theme="dark"] .hero-stamp::after { border-color: var(--theme-line); }
/* Dark frosted surfaces keep a high-opacity base for text legibility. */
html[data-theme="dark"] .utility-bar {
  background: linear-gradient(125deg, rgba(178,205,166,.07), transparent 65%), rgba(21,28,24,.96);
  box-shadow: inset 0 1px 0 rgba(238,240,232,.08), 0 9px 28px rgba(0,0,0,.12);
}
html[data-theme="dark"] .workspace { background: var(--theme-surface); border-color: var(--theme-line); }
html[data-theme="dark"] .sync-state i { box-shadow: 0 0 0 4px rgba(162, 217, 181, .1); }

html[data-theme="dark"] :is(button, input, select, textarea, a, summary, [tabindex]):focus-visible {
  outline: 3px solid var(--theme-accent);
  outline-offset: 3px;
}
html[data-theme="dark"] :is(input, select, textarea) { color-scheme: dark; accent-color: var(--theme-accent); }
html[data-theme="dark"] :is(select option, select optgroup) { background: var(--theme-inset); color: var(--theme-text); }
html[data-theme="dark"] :is(.field input, .field select, .field textarea, .mini-field input, .mini-field select, .team-role, .tax-rate-input, .tax-default-row input) {
  background: var(--theme-inset);
  color: var(--theme-text);
  border-color: var(--theme-control-line);
  caret-color: var(--theme-accent);
}
html[data-theme="dark"] .owner-control select { background-color: var(--theme-inset); border-color: var(--theme-control-line); }
html[data-theme="dark"] :is(.field input, .field select, .field textarea, .mini-field input, .mini-field select, .team-role, .tax-rate-input, .tax-default-row input):hover:not(:disabled) { border-color: var(--theme-success); }
html[data-theme="dark"] :is(input, textarea)::placeholder { color: var(--theme-subtle); opacity: 1; }
html[data-theme="dark"] :is(input, select, textarea):disabled { color: var(--theme-subtle); cursor: default; }
html[data-theme="dark"] .field.autofilled :is(input, select) { background: var(--theme-success-bg); border-color: var(--theme-success); }
html[data-theme="dark"] input:autofill { box-shadow: 0 0 0 1000px var(--theme-inset) inset; -webkit-text-fill-color: var(--theme-text); caret-color: var(--theme-accent); }

html[data-theme="dark"] .button-primary { background: var(--theme-accent); color: var(--theme-on-accent); box-shadow: 0 5px 18px rgba(0, 0, 0, .12); }
html[data-theme="dark"] .button-primary:hover:not(:disabled) { background: var(--theme-accent-hover); }
html[data-theme="dark"] .button-dark { background: var(--theme-action); color: var(--theme-text); border-color: var(--theme-control-line); box-shadow: none; }
html[data-theme="dark"] .button-dark:hover:not(:disabled) { background: var(--theme-action-hover); }
html[data-theme="dark"] .button-ghost { color: var(--theme-text); border-color: var(--theme-control-line); background: transparent; box-shadow: none; }
html[data-theme="dark"] .button-ghost:hover:not(:disabled) { background: var(--theme-hover); }
html[data-theme="dark"] :is(.mini-button, .icon-button) { background: var(--theme-raised); color: var(--theme-text); border-color: var(--theme-control-line); box-shadow: none; }
html[data-theme="dark"] :is(.mini-button, .icon-button):hover:not(:disabled) { background: var(--theme-hover); color: var(--theme-success); border-color: var(--theme-success); }
html[data-theme="dark"] .mini-button[aria-pressed="true"] { background: var(--theme-success-bg); color: var(--theme-success); border-color: var(--theme-success); }
html[data-theme="dark"] .mini-button.danger:hover:not(:disabled) { background: var(--theme-error-bg); color: var(--theme-error); border-color: var(--theme-error); }
html[data-theme="dark"] :is(.delete-button, .invoice-remove) { color: var(--theme-muted); }
html[data-theme="dark"] :is(.delete-button, .invoice-remove):hover { background: var(--theme-error-bg); color: var(--theme-error); }
html[data-theme="dark"] :is(.mini-button, .icon-button, .button, .status-button):disabled { opacity: .55; transform: none; cursor: default; }
html[data-theme="dark"] .invoice-link { border-color: var(--theme-control-line); }
html[data-theme="dark"] .invoice-link:hover { color: var(--theme-success); border-color: var(--theme-success); }

/* Small copy was hardcoded in the light stylesheet, not inherited from ink-soft. */
html[data-theme="dark"] :is(
  .section-kicker, .updated-at, .lifetime-label small, .lifetime-scope small,
  .lifetime-scope span, .lifetime-usd, .currency-tag, .summary-caption,
  .progress-copy small, .ledger-legend, .group-title, .expense-main small,
  .expense-period small, .expense-amount small, .expense-period span,
  .empty-state p, .selection-bar small, .footer, .modal-note, .batch-intro,
  .field > span, .field > span small, .upload-copy small, .batch-toolbar span,
  .invoice-file small, .mini-field > span, .invoice-details summary,
  .form-divider, .conversion-preview span, .settings-hint, .user-copy small,
  .team-empty, .audit-page, .audit-row small, .audit-meta strong,
  .notification-row small,
  .notification-read, .payout-preview-item, .month-closure-bar small,
  .conflict-head, .billing-group-title, .billing-scope small, .billing-preview-head,
  .billing-rows th,
  .billing-reconcile, .tax-rates-head, .tax-rate-readonly
) { color: var(--theme-muted); }
html[data-theme="dark"] :is(.expense-main .expense-owner, .expense-main .expense-note, .expense-amount .expense-rub, .billing-rows .row-name small, .billing-totals .billing-words) { color: var(--theme-muted); }
html[data-theme="dark"] .payout-row.reversed > span:nth-child(2) > strong { color: var(--theme-muted); }
html[data-theme="dark"] :is(.expense-main .expense-plan, .expense-amount .expense-rate) { color: var(--theme-success); }
html[data-theme="dark"] :is(.lifetime-missing, .expense-main .status-note, .tax-rate-current, .package-missing, .act-selection-row .act-coverage) { color: var(--theme-warning); }
html[data-theme="dark"] :is(.bill-owner-summary, .billing-totals, .selection-bar strong, .package-component .mini-field > span) { color: var(--theme-text); }

html[data-theme="dark"] .lifetime-overview { background: var(--theme-inset); border-color: var(--theme-line); }
html[data-theme="dark"] .lifetime-pending { background: var(--theme-warning-bg); color: var(--theme-warning); }
html[data-theme="dark"] .lifetime-pending:hover:not(:disabled) { box-shadow: inset 0 0 0 1px var(--theme-warning); }
html[data-theme="dark"] .summary-card::before { background: rgba(191, 215, 131, .06); }
html[data-theme="dark"] .summary-value.zero { color: var(--theme-muted); }
html[data-theme="dark"] .summary-live-rate { color: var(--theme-muted); }
html[data-theme="dark"] .summary-live-rate[data-summary="payable"] { color: var(--theme-text); border-color: var(--theme-line); }
html[data-theme="dark"] :is(.document-help, #billNumberHint, #actNumberHint, #actDateHint) { color: var(--theme-muted); }
html[data-theme="dark"] .document-help > summary { color: var(--theme-success); }
html[data-theme="dark"] .document-help-body { border-color: var(--theme-line); }
html[data-theme="dark"] .summary-card.bank-total { background: #2d3e29; color: #e2edc6; border-color: #64794a; }
html[data-theme="dark"] .summary-card.bank-total::before { background: rgba(191, 215, 131, .08); }
html[data-theme="dark"] .summary-card.bank-total :is(.currency-tag, .summary-caption, .summary-live-rate, .summary-value.zero) { color: #c5d5b0; }
html[data-theme="dark"] .progress-card { background: var(--theme-inset); color: var(--theme-text); border-color: var(--theme-line); }
html[data-theme="dark"] .progress-card::before { background: rgba(191, 215, 131, .05); }
html[data-theme="dark"] .progress-ring { background: conic-gradient(var(--theme-accent) var(--progress), var(--theme-line) 0); }
html[data-theme="dark"] .progress-ring::after { background: var(--theme-inset); }
html[data-theme="dark"] .progress-copy .progress-rub { color: var(--theme-accent); }
html[data-theme="dark"] .legend-dot.pending { background: var(--theme-warning); }
html[data-theme="dark"] :is(.service-avatar, .upload-icon, .user-avatar) { background: var(--theme-inset); color: var(--theme-accent); }
html[data-theme="dark"] .status-button.status-needs_review { background: var(--theme-error-bg); color: var(--theme-error); }
html[data-theme="dark"] .status-button.status-verified { background: var(--theme-info-bg); color: var(--theme-info); }
html[data-theme="dark"] button.status-button:hover:not(:disabled) { border-color: currentColor; }

html[data-theme="dark"] :is(.invoice-card, .team-row, .audit-row, .payout-preview-group, .month-closure-bar, .billing-preview, .tax-rate-row, .package-component) { background: var(--theme-raised); }
/* Access state is written in the row; do not fade its still-actionable content. */
html[data-theme="dark"] .team-row.inactive { opacity: 1; }
html[data-theme="dark"] :is(.card-conversion, .conversion-preview, .buyer-summary) { background: var(--theme-inset); color: var(--theme-muted); }
html[data-theme="dark"] .conversion-preview strong { color: var(--theme-text); }
html[data-theme="dark"] :is(.invoice-details, .billing-group, .billing-rows td, .conflict-row) { border-color: var(--theme-line); }
html[data-theme="dark"] .form-divider::before,
html[data-theme="dark"] .form-divider::after { background: var(--theme-line); }
html[data-theme="dark"] .invoice-state.parsing { background: var(--theme-hover); color: var(--theme-muted); }
html[data-theme="dark"] .parse-result { background: var(--theme-raised); color: var(--theme-muted); }
html[data-theme="dark"] .parse-result.success { background: var(--theme-success-bg); color: var(--theme-success); }
html[data-theme="dark"] :is(.parse-result.partial, .billing-missing, .payout-preview-notice) { background: var(--theme-warning-bg); color: var(--theme-warning); }
html[data-theme="dark"] :is(.parse-result.error, .payout-state.reversed) { background: var(--theme-error-bg); color: var(--theme-error); }
html[data-theme="dark"] .payout-reversal { color: var(--theme-error); }
html[data-theme="dark"] :is(#duplicateWarning, .conflict-box) { background: var(--theme-warning-bg); border-color: var(--theme-warning-line); color: var(--theme-text); }
html[data-theme="dark"] :is(.conflict-title, .conflict-label small) { color: var(--theme-warning); }
html[data-theme="dark"] .conflict-row.changed > span:nth-child(2) { color: var(--theme-text); }
html[data-theme="dark"] .conflict-row > span:nth-child(n + 2)::before { color: var(--theme-muted); }
html[data-theme="dark"] .upload-field { background: var(--theme-inset); border-color: var(--theme-control-line); }
html[data-theme="dark"] :is(.upload-field:hover, .upload-field.has-file, .upload-field.dragging) { background: var(--theme-success-bg); border-color: var(--theme-success); }
html[data-theme="dark"] .upload-field:has(input:focus-visible) { outline: 3px solid var(--theme-accent); outline-offset: 3px; }
html[data-theme="dark"] .upload-field.dragging { box-shadow: 0 0 0 5px rgba(162, 217, 181, .12); }
html[data-theme="dark"] .billing-scope label { background: var(--theme-raised); }
html[data-theme="dark"] .billing-scope label:has(input:checked) { background: var(--theme-success-bg); border-color: var(--theme-success); }
html[data-theme="dark"] .package-component .mini-field:hover { background: var(--theme-hover); }

/* Native controls retain their appearance, including package/act checkboxes. */
html[data-theme="dark"] input:is([type="checkbox"], [type="radio"]) { accent-color: var(--theme-accent); }
html[data-theme="dark"] .modal { box-shadow: 0 30px 100px rgba(0, 0, 0, .42); }
html[data-theme="dark"] .modal::backdrop { background: rgba(7, 12, 9, .7); }
html[data-theme="dark"] .modal-close { background: var(--theme-hover); color: var(--theme-text); }
html[data-theme="dark"] .modal-close:hover { background: var(--theme-line); }
html[data-theme="dark"] :is(.selection-bar, .batch-actions) { background: var(--theme-raised); border-color: var(--theme-line); box-shadow: var(--theme-shadow); }
html[data-theme="dark"] .selection-bar {
  background: linear-gradient(125deg, rgba(191,215,131,.06), transparent 65%), rgba(38,51,43,.96);
  box-shadow: inset 0 1px 0 rgba(238,240,232,.08), var(--theme-shadow);
}
html[data-theme="dark"] .more-menu { background: var(--theme-raised); border-color: var(--theme-line); box-shadow: var(--theme-shadow); }
html[data-theme="dark"] .more-item:hover,
html[data-theme="dark"] .more-item:focus-visible { background: var(--theme-success-bg); color: var(--theme-success); }
html[data-theme="dark"] .loading-row { background-image: linear-gradient(90deg, var(--theme-inset), var(--theme-hover), var(--theme-inset)); }
html[data-theme="dark"] .toast { background: var(--theme-raised); color: var(--theme-text); box-shadow: 0 0 0 1px var(--theme-control-line), var(--theme-shadow); }
html[data-theme="dark"] .toast.error { background: var(--theme-error-bg); color: var(--theme-error); }

html[data-theme="dark"] .notification-panel {
  background: linear-gradient(140deg, rgba(191,215,131,.04), transparent 65%), rgba(38,51,43,.97);
  border-color: var(--theme-line);
  box-shadow: inset 0 1px 0 rgba(238,240,232,.08), var(--theme-shadow);
}
html[data-theme="dark"] .notification-panel::before { background: var(--theme-raised); border-color: var(--theme-line); }
html[data-theme="dark"] .notification-row { border-color: var(--theme-line); }
html[data-theme="dark"] .notification-panel .notification-row { background: var(--theme-surface); border-color: var(--theme-line); }
html[data-theme="dark"] .notification-row.unread,
html[data-theme="dark"] .notification-panel .notification-row.unread { background: var(--theme-warning-bg); border-left-color: var(--theme-warning); }
html[data-theme="dark"] #notificationBadge { background: var(--theme-error-bg); color: var(--theme-error); }
html[data-theme="dark"] .notification-panel :is(.mini-button, .icon-button) { background: var(--theme-raised); border-color: var(--theme-control-line); box-shadow: none; }
html[data-theme="dark"] .notification-panel :is(.mini-button, .icon-button):hover:not(:disabled) { background: var(--theme-hover); border-color: var(--theme-success); }
html[data-theme="dark"] .utility-bar .button-ghost { background: transparent; border-color: var(--theme-control-line); box-shadow: none; }
html[data-theme="dark"] .utility-bar .button-ghost:hover:not(:disabled) { background: var(--theme-hover); }
html[data-theme="dark"] .utility-bar .notification-bell[aria-expanded="true"] { background: var(--theme-success-bg); border-color: var(--theme-success); }
html[data-theme="dark"] .selection-bar .button-dark { background: var(--theme-action); box-shadow: none; }
html[data-theme="dark"] .selection-bar .button-dark:hover:not(:disabled) { background: var(--theme-action-hover); }
html[data-theme="dark"] .workflow-warnings { background: var(--theme-warning-bg); color: var(--theme-warning); }

@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  html[data-theme="dark"] .utility-bar { background: var(--theme-page); }
  html[data-theme="dark"] :is(.notification-panel, .selection-bar) { background: var(--theme-raised); }
}

@media (prefers-reduced-transparency: reduce) {
  html[data-theme="dark"] :is(.utility-bar, .notification-panel, .selection-bar, .batch-actions),
  html[data-theme="dark"] .modal::backdrop { -webkit-backdrop-filter: none; backdrop-filter: none; }
  html[data-theme="dark"] .modal::backdrop { background: var(--theme-page); }
  html[data-theme="dark"] .utility-bar { background: var(--theme-page); }
  html[data-theme="dark"] :is(.notification-panel, .selection-bar) { background: var(--theme-raised); }
  html[data-theme="dark"] .page-grain { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .theme-toggle { transition: none; }
  .theme-toggle:hover { transform: none; }
  html[data-theme="dark"] .loading-row { animation: none; }
}

/* Let the platform own contrast; do not opt native controls out of forced colors. */
@media (forced-colors: active) {
  html[data-theme="dark"] {
    color-scheme: light dark;
    --theme-page: Canvas;
    --theme-surface: Canvas;
    --theme-raised: Canvas;
    --theme-inset: Canvas;
    --theme-hover: Canvas;
    --theme-text: CanvasText;
    --theme-muted: CanvasText;
    --theme-subtle: GrayText;
    --theme-line: CanvasText;
    --theme-control-line: ButtonText;
    --theme-accent: Highlight;
    --theme-accent-hover: Highlight;
    --theme-on-accent: HighlightText;
    --theme-action: ButtonFace;
    --theme-action-hover: ButtonFace;
    --theme-success: LinkText;
    --theme-success-bg: Canvas;
    --theme-warning: CanvasText;
    --theme-warning-bg: Canvas;
    --theme-warning-line: CanvasText;
    --theme-error: CanvasText;
    --theme-error-bg: Canvas;
    --theme-info: CanvasText;
    --theme-info-bg: Canvas;
    --theme-shadow: none;
  }
  html[data-theme="dark"] .page-grain { display: none; }
  html[data-theme="dark"] :is(input, select, textarea) { color-scheme: light dark; }
  /* Native forced-color text backplates use ButtonFace, not Highlight. */
  html[data-theme="dark"] .button-primary,
  html[data-theme="dark"] .button-primary:hover:not(:disabled) {
    background: ButtonFace;
    color: ButtonText;
    border-color: ButtonText;
    box-shadow: none;
  }
  html[data-theme="dark"] :is(.utility-bar, .notification-panel, .selection-bar) { background: Canvas; box-shadow: none; }
  html[data-theme="dark"] :is(.utility-bar, .notification-panel, .selection-bar, .batch-actions),
  html[data-theme="dark"] .modal::backdrop { -webkit-backdrop-filter: none; backdrop-filter: none; }
  html[data-theme="dark"] :is(.summary-card.bank-total, .progress-ring, .loading-row) { background: Canvas; color: CanvasText; border-color: CanvasText; }
  html[data-theme="dark"] .summary-card.bank-total :is(.currency-tag, .summary-caption, .summary-live-rate, .summary-value.zero) { color: CanvasText; }
  html[data-theme="dark"] .summary-card::before { display: none; }
  html[data-theme="dark"] .toast { border: 1px solid CanvasText; box-shadow: none; }
}
