/*
  Shared dark-mode finish layer.

  This stylesheet loads after every page's local <style> block.  Tool and
  admin templates predate the shared color tokens and still include literal
  light colors, so the declarations below intentionally use !important only
  where it is needed to let the selected theme win over those page styles.
*/

html[data-theme="dark"] {
  color-scheme: dark;
  --cw-surface: #1E293B;
  --cw-surface-raised: #25364C;
  --cw-surface-input: #0F172A;
  --cw-line: #334155;
  --cw-line-strong: #475569;
  --cw-ink: #F1F5F9;
  --cw-ink-muted: #A8B5C7;
  --cw-hover: #293B52;
}

html[data-theme="dark"] body {
  background: var(--bg) !important;
  color: var(--text) !important;
}

html[data-theme="dark"] .hero-strip {
  background: linear-gradient(140deg, #082235 0%, #10415C 48%, #176682 100%);
}

html[data-theme="dark"] .hero-strip::before {
  background: radial-gradient(ellipse 60% 80% at 50% 120%, rgba(58,157,194,.19) 0%, transparent 70%);
}

/* App surfaces: cards, panels, dialogs, and tool-specific containers. */
html[data-theme="dark"] :is(
  .card,
  .tool-card,
  .detail-card,
  .line-preview,
  .modal,
  .modal-box,
  .modal-panel,
  .email-modal-panel,
  .account-card,
  .router-panel,
  .auth-container,
  .cc-card,
  .cc-results,
  .cc-summary,
  .cite-card,
  .cite-corrected-box,
  .cite-feedback-area,
  .cite-section,
  .fb-stat,
  .fb-analysis,
  .fb-card,
  .fb-section-text,
  .fb-section-feedback,
  .ec-form,
  .ec-aside,
  .ec-panel,
  .ec-capture-option,
  .ec-live,
  .ec-upload,
  .ec-paste,
  .ec-marker,
  .ec-update,
  .ef-panel,
  .ef-card,
  .ef-preview,
  dialog.ef-modal,
  .frr-card,
  .frr-modal,
  .cm-card,
  .cm-panel,
  .rt-card,
  .rt-filter,
  .rt-search,
  .rt-empty,
  .job-card,
  .adm-stat,
  .adm-table-wrap,
  .adm-empty,
  .delivery-history,
  .delivery-preview,
  .digest-status,
  .summary-refresh,
  .summary-refresh-history
) {
  background-color: var(--cw-surface) !important;
  border-color: var(--cw-line) !important;
  color: var(--cw-ink) !important;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .22);
}

html[data-theme="dark"] :is(
  .detail-card:hover,
  .ec-capture-option:hover,
  .ec-upload:hover,
  .ec-marker:hover,
  .ef-card:hover,
  .frr-card:hover,
  .rt-card:hover,
  .fb-card-head:hover,
  .cite-card-head:hover
) {
  background-color: var(--cw-hover) !important;
}

html[data-theme="dark"] :is(
  .detail-card.selected,
  .detail-card-template.selected,
  .filing-row.selected,
  .ec-mode.active,
  .ef-topics button.active,
  .ef-sort button.active,
  .cm-tab.active,
  .cm-rtab.active,
  .rt-filter.active
) {
  background-color: rgba(58,157,194,.16) !important;
  border-color: var(--primary) !important;
}

/* Native fields and custom field controls. */
html[data-theme="dark"] :is(
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea,
  .form-input,
  .form-select,
  .ft-trigger,
  .cm-select,
  .cm-input,
  .cc-dropzone,
  .ec-form input,
  .ec-form textarea,
  .ef-row input,
  .ef-row select,
  .ef-filters input,
  .ef-filters select,
  .frr-input,
  .frr-select,
  .rt-search input,
  .sub-email-input,
  .sub-date-input,
  .reorder-notes-input,
  .topic-field-input,
  .upload-filer-name
) {
  background-color: var(--cw-surface-input) !important;
  color: var(--cw-ink) !important;
  border-color: var(--cw-line) !important;
}

html[data-theme="dark"] :is(input, select, textarea)::placeholder {
  color: #71829A !important;
}

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

html[data-theme="dark"] :is(
  .drop-zone,
  .cc-dropzone,
  .cm-drop,
  .ec-paste,
  .ec-existing,
  .ec-instructions,
  .ef-alert,
  .frr-agencies-section,
  .frr-controls,
  .rt-feed-health,
  .sync-bar,
  .indexing-card,
  .info-box,
  .warnings-box,
  .possible-incomplete-note,
  .all-done-banner
) {
  background-color: var(--cw-surface-raised) !important;
  border-color: var(--cw-line) !important;
  color: var(--cw-ink) !important;
}

/* Tables across tool and admin pages. */
html[data-theme="dark"] :is(.table-wrap, .adm-table-wrap, .cm-tablewrap) {
  background: var(--cw-surface) !important;
  border-color: var(--cw-line) !important;
}

html[data-theme="dark"] table {
  background: var(--cw-surface) !important;
  border-color: var(--cw-line) !important;
  color: var(--cw-ink) !important;
}

html[data-theme="dark"] :is(thead, thead tr, th) {
  background: var(--cw-surface-raised) !important;
  color: var(--cw-ink-muted) !important;
  border-color: var(--cw-line) !important;
}

html[data-theme="dark"] :is(td, tr) {
  border-color: var(--cw-line) !important;
}

html[data-theme="dark"] td {
  background: transparent !important;
  color: var(--cw-ink) !important;
}

html[data-theme="dark"] tr:hover td {
  background: var(--cw-hover) !important;
}

/* Modals and secondary controls. */
html[data-theme="dark"] :is(.modal-backdrop, .modal-overlay, .frr-overlay) {
  background: rgba(0,0,0,.7) !important;
}

html[data-theme="dark"] :is(
  .btn-secondary,
  .btn-cancel,
  .modal-cancel,
  .ec-btn.outline,
  .ef-text-btn,
  .cm-btn.ghost,
  .frr-link-secondary,
  .rt-action-btn,
  .pagination-btn,
  .frr-page-btn,
  .cm-textbtn
) {
  background-color: var(--cw-surface-raised) !important;
  border-color: var(--cw-line) !important;
  color: var(--cw-ink) !important;
}

html[data-theme="dark"] :is(
  .btn-secondary:hover,
  .btn-cancel:hover,
  .modal-cancel:hover,
  .ec-btn.outline:hover,
  .ef-text-btn:hover,
  .cm-btn.ghost:hover,
  .rt-action-btn:hover,
  .pagination-btn:hover,
  .frr-page-btn:hover
) {
  background-color: var(--cw-hover) !important;
  border-color: var(--cw-line-strong) !important;
}

/* Titles, labels, and supporting text with fixed light-theme colors. */
html[data-theme="dark"] main :is(h1, h2, h3, h4, h5, h6, label, legend),
html[data-theme="dark"] :is(
  .card-title,
  .modal-title,
  .card-label,
  .account-heading,
  .adm-email,
  .adm-section-title,
  .cc-card-label,
  .cite-card-title,
  .cite-section-label,
  .fb-stat-num,
  .fb-analysis-title,
  .fb-card-preview,
  .ef-panel-title h2,
  .ef-card h3,
  .frr-card-title,
  .cm-fname,
  .rt-title,
  .rt-card h3,
  .job-card h3
) {
  color: var(--cw-ink) !important;
}

html[data-theme="dark"] main :is(
  p,
  small,
  .card-desc,
  .modal-subtitle,
  .form-note,
  .field-hint,
  .account-card-description,
  .adm-section-note,
  .cc-dropzone-sub,
  .cite-explanation,
  .fb-stat-label,
  .fb-card-date,
  .ef-note,
  .ef-panel-title p,
  .ef-host,
  .ef-details,
  .ef-notes,
  .frr-card-meta,
  .frr-abstract-placeholder,
  .cm-fmeta,
  .cm-note,
  .rt-meta,
  .rt-empty-sub,
  .job-step-text,
  .done-sub,
  .error-msg,
  .weber-ring-status
) {
  color: var(--cw-ink-muted) !important;
}

html[data-theme="dark"] main a:not(.btn-primary):not(.btn-download):not(.btn-submit):not(.sub-submit-btn) {
  color: #7DD3FC;
}

/* Existing inline light theme styles are limited to content panels and dynamic
   browser-rendered result cards. Keep these selectors narrow so semantic
   primary, success, and error controls retain their intentional colors. */
html[data-theme="dark"] main :is(
  [style*="background:#fff"],
  [style*="background: #fff"],
  [style*="background:#ffffff"],
  [style*="background: #ffffff"],
  [style*="background:#F8FAFC"],
  [style*="background: #F8FAFC"],
  [style*="background:#FAFAFA"],
  [style*="background: #FAFAFA"]
) {
  background-color: var(--cw-surface) !important;
}

html[data-theme="dark"] main :is(
  [style*="color:#0F172A"],
  [style*="color: #0F172A"],
  [style*="color:#1E293B"],
  [style*="color: #1E293B"]
) {
  color: var(--cw-ink) !important;
}

html[data-theme="dark"] main :is(
  [style*="color:#64748B"],
  [style*="color: #64748B"],
  [style*="color:#475569"],
  [style*="color: #475569"],
  [style*="color:#94A3B8"],
  [style*="color: #94A3B8"]
) {
  color: var(--cw-ink-muted) !important;
}

/* Preserve readable semantic states while giving them dark surfaces. */
html[data-theme="dark"] :is(
  .alert-error,
  .error-box,
  .cc-error,
  .rt-error,
  .delivery-error,
  .summary-failure-badge
) {
  background: rgba(220,38,38,.16) !important;
  border-color: rgba(248,113,113,.42) !important;
  color: #FCA5A5 !important;
}

html[data-theme="dark"] :is(
  .alert-success,
  .sub-success,
  .summary-available,
  .sync-badge.up_to_date,
  .sync-badge.live
) {
  background: rgba(16,185,129,.15) !important;
  border-color: rgba(52,211,153,.34) !important;
  color: #86EFAC !important;
}

html[data-theme="dark"] :is(
  .cap-warning,
  .upload-warning,
  .warnings-note,
  .possible-incomplete-note,
  .sync-badge.sync_error,
  .sync-badge.not_indexed
) {
  background: #2A210D !important;
  border-color: #80620F !important;
  color: #FCD34D !important;
}

html[data-theme="dark"] .weber-ring::before,
html[data-theme="dark"] .ring-spinner {
  border-color: var(--cw-line);
  border-top-color: var(--primary);
}