/*
 * AF-Health: dark-mode surface and contrast integrity (v1).
 * Keep this file after the theme, module and custom CSS in every HTML layout.
 * All selectors are dark-only: light themes, print/PDF layouts, image canvases,
 * QR codes, CAPTCHA artwork and intentionally light preview mockups stay intact.
 * Surface colors come from each active frontend/admin theme, not a fixed palette.
 */
html[data-theme="dark"] { color-scheme:dark; }
html[data-theme="light"] { color-scheme:light; }

html[data-theme="dark"] body.ui-v2 {
  --success:#78e6b0;
  --warning:#ffcf7a;
  --danger:#ffa5b5;
  --success-soft:color-mix(in srgb,#34d399 16%,var(--surface));
  --warning-soft:color-mix(in srgb,#f59e0b 16%,var(--surface));
  --danger-soft:color-mix(in srgb,#fb7185 15%,var(--surface));
  --mode-panel:color-mix(in srgb,var(--surface) 96%,var(--primary) 4%);
  --mode-panel-soft:color-mix(in srgb,var(--surface-2) 96%,var(--primary) 4%);
  --mode-line:color-mix(in srgb,var(--border) 83%,var(--primary) 17%);
}

/* Reset light page washes without removing theme-specific atmosphere images. */
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"],
html[data-theme="dark"] body.app-body.portal-experience[class*="frontend-theme-"] {
  color:var(--text);
  --theme-page-surface:var(--mode-panel);
  --theme-page-line:var(--mode-line);
}
html[data-theme="dark"] body.app-body.portal-experience[class*="frontend-theme-"] :is(.app-main,.page-content),
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] :is(.public-main,.auth-wrap) {
  color:var(--text);
}

/* Public navigation: editor-configured light header colors must not stay white
   in dark mode. The per-theme surface keeps all ten theme palettes distinct. */
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] {
  --afghan-public-header-background:color-mix(in srgb,var(--surface) 91%,transparent);
  --mm-header-text:var(--text);
  --mm-header-item-bg:transparent;
  --mm-header-hover-bg:var(--surface-2);
  --mm-header-active-bg:var(--primary-soft);
  --mm-header-active-text:var(--text);
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] .public-header :is(
  .public-nav-link.active,.public-nav-dropdown.active>.public-nav-link
) {
  color:var(--text)!important;
  background:var(--primary-soft)!important;
  border-color:var(--border)!important;
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] .public-header {
  background:color-mix(in srgb,var(--surface) 91%,transparent)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] .public-header :is(.public-nav-link,.public-nav-static,.public-theme-toggle,.public-menu-toggle) {
  color:var(--text);
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] .public-header :is(.public-nav-link,.public-nav-static):hover,
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] .public-header :is(.public-nav-link,.public-nav-static):focus-visible {
  background-color:var(--surface-2);
  color:var(--text);
}

/* Public and registration surfaces, including theme-customized page blocks. */
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] :where(
  .auth-card,.auth-card-modern,.auth-form-panel,.signup-form-surface,
  .registration-form-card,.marketplace-registration-panel,.signup-section,
  .signup-section-card,.signup-summary-card,.signup-step-card,
  .home-provider-finder-panel,.home-doctor-specialties-panel,
  .feature-card-premium,.feature-card,.provider-card,.directory-provider-card,
  .marketplace-provider-card,.marketplace-product-card,
  .provider-directory-managed-html__surface,.public-page-managed-html__surface,
  .temporary-search-card,.temporary-capability-card,.temporary-registration-panel,
  .temporary-provider-card,.temporary-marketplace-card,.error-card
) {
  background:var(--theme-card-bg,var(--mode-panel))!important;
  border-color:var(--mode-line)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] :where(
  .auth-card,.auth-card-modern,.auth-form-panel,.signup-form-surface,
  .registration-form-card,.marketplace-registration-panel,.signup-section,
  .home-provider-finder-panel,.feature-card-premium,.provider-card,
  .directory-provider-card,.marketplace-provider-card,.error-card
) :is(h1,h2,h3,h4,.card-title,.form-label) {
  color:var(--text);
}
html[data-theme="dark"] body.guest-shell[class*="frontend-theme-"] :where(
  .signup-benefit,.signup-help,.auth-card-subtitle,.feature-description,
  .provider-meta,.card-subtitle,.form-help
) { color:var(--muted); }

/* Never turn decorative artwork or previews into dark cards. A few special
   themes use opaque light cards in their late home-page layout rules. */
html[data-theme="dark"] body.guest-shell.frontend-theme-kinetic_spectrum.route-home :is(
  .home-provider-finder-panel,.feature-card-premium
) { background:var(--theme-card-bg,var(--mode-panel))!important; }
html[data-theme="dark"] body.guest-shell:is(.frontend-theme-sakura_health,.frontend-theme-sunrise_gold,.frontend-theme-crimson_pulse) :is(
  .home-hero-title-below-inner,.signup-guide-panel
) { color:var(--text); }

/* Shared fields, selects, search controls, textareas and native popups.
   Exclude image controls, radios, checkboxes and color/file pickers. */
html[data-theme="dark"] body.ui-v2 :where(
  .input,.select,.textarea,.auth-input-control,.form-control,
  input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="color"]):not([type="file"]):not([type="range"]):not([type="submit"]):not([type="button"]):not([type="reset"]),
  select,textarea
):not(.btn):not(.btn-primary):not(.btn-secondary) {
  background-color:var(--surface-2)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
  caret-color:var(--text);
}
html[data-theme="dark"] body.ui-v2 :where(input,textarea)::placeholder { color:var(--muted)!important;opacity:1; }
html[data-theme="dark"] body.ui-v2 :where(input,textarea,select):disabled {
  background-color:var(--surface-3,var(--surface-2))!important;
  color:var(--muted)!important;
}
html[data-theme="dark"] body.ui-v2 :where(input,textarea,select):focus-visible {
  border-color:var(--primary)!important;
  outline-color:var(--primary);
}
html[data-theme="dark"] body.ui-v2 :is(.modal-panel,.command-panel,.inline-review-form,.popover-panel,.dropdown-menu) {
  background:var(--mode-panel)!important;
  border-color:var(--mode-line)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.ui-v2 :is(.modal-header,.chat-header,.chat-form,.command-search) {
  background:var(--mode-panel)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.ui-v2 :is(.table-wrap,.data-table-wrapper) {
  border-color:var(--border)!important;
  color:var(--text);
}
html[data-theme="dark"] body.ui-v2 .data-table :is(thead,th) {
  background:var(--mode-panel-soft)!important;
  color:var(--muted)!important;
  border-color:var(--border)!important;
}
html[data-theme="dark"] body.ui-v2 .data-table td { border-color:var(--border);color:var(--text); }
html[data-theme="dark"] body.ui-v2 :is(.fieldset,.detail-item,.package-summary-card,.empty-state) {
  background-color:var(--mode-panel-soft);
  border-color:var(--border);
  color:var(--text);
}
html[data-theme="dark"] body.ui-v2 :is(.badge,.tag,.alert) { border-color:var(--border); }

/* Authenticated provider, laboratory, hospital, pharmacy and patient shells. */
html[data-theme="dark"] body.app-body.portal-experience:not(.role-admin) :where(
  .card,.workspace-section,.subscription-panel,.doctor-dashboard-metric,
  .metric-card-pro,.quick-action-pro,.doctor-command-card,.doctor-command-panel,
  .doctor-practice-panel,.doctor-plan-panel,.doctor-activation-panel,
  .clinic-context-bar,.clinic-patient-item,.clinic-appointment-list>a,
  .dashboard-lower-grid>*,.doctor-dashboard-lower-grid>*,
  .provider-page-toolbar,.patient-content-card,.patient-dashboard-card,
  .reseller-stat-card,.reseller-package-card,.support-panel
) {
  background:var(--theme-card-bg,var(--mode-panel))!important;
  border-color:var(--mode-line)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.app-body.portal-experience:not(.role-admin) :where(
  .topbar,.provider-topbar-context,.command-trigger,.icon-button,.btn-outline,
  .topbar-profile,.provider-topbar-create-menu,.provider-nav-search
) {
  background-color:var(--surface)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.app-body.portal-experience:not(.role-admin) .sidebar :is(.workspace-chip,.sidebar-user-card) {
  border-color:color-mix(in srgb,var(--theme-sidebar-text,#fff) 16%,transparent)!important;
  background:color-mix(in srgb,var(--theme-sidebar-text,#fff) 8%,transparent)!important;
}
/* Aether Glass previously used semitransparent white cards even after the
   provider shell had turned dark. Dark glass uses the active surface instead. */
html[data-theme="dark"] body.app-body.portal-experience.frontend-theme-aether_glass :where(
  .card,.subscription-panel,.metric-card-pro,.doctor-dashboard-metric,
  .quick-action-pro,.doctor-command-card,.doctor-plan-panel,
  .dashboard-lower-grid>*,.doctor-dashboard-lower-grid>*
) {
  background:color-mix(in srgb,var(--surface) 88%,transparent)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.app-body.portal-experience:not(.role-admin) :where(
  .drug-assistant__results,.drug-assistant__selected,
  .doctor-command-search-results,.prescription-line-item
) {
  background:var(--mode-panel)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.ui-v2 .drug-assistant-dialog,
html[data-theme="dark"] body.ui-v2 .drug-assistant-dialog :is(.drug-assistant-dialog__header,.drug-assistant-dialog__footer) {
  background:var(--surface)!important;
  border-color:var(--border)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.ui-v2 .drug-assistant-dialog__section { background:var(--surface-2)!important; }

/* Administration uses its own panel and text tokens. Older modules still had
   #fff / #f8fafc declarations later than the original admin dark styles. */
html[data-theme="dark"] body.role-admin.ui-v2 {
  --admin-pos-canvas:var(--bg);
  --admin-pos-panel:var(--surface);
  --admin-pos-panel-alt:var(--surface-2);
  --admin-pos-line:var(--border);
  --admin-pos-text:var(--text);
  --admin-pos-muted:var(--muted);
  --admin-panel:var(--surface);
  --admin-panel-alt:var(--surface-2);
  --admin-line:var(--border);
  --admin-ui-bg:var(--bg);
  --admin-ui-panel:var(--surface);
  --admin-ui-panel-alt:var(--surface-2);
  --admin-ui-border:var(--border);
  --admin-ui-text:var(--text);
  --admin-ui-muted:var(--muted);
  --admin-pos-blue-soft:color-mix(in srgb,var(--admin-pos-blue,#60a5fa) 17%,var(--surface-2));
  --admin-ui-accent-soft:var(--admin-pos-blue-soft);
  --admin-ui-accent-strong:color-mix(in srgb,var(--admin-pos-blue,#60a5fa) 70%,#fff);
  --admin-accent-soft:var(--admin-pos-blue-soft);
  --studio-panel:var(--surface);
  --studio-line:var(--border);
  background-color:var(--bg)!important;
  color:var(--text)!important;
}
html[data-theme="dark"] body.role-admin.ui-v2 :where(
  .admin-workspace-shell,.admin-workspace-content,.admin-console-head,
  .admin-console,.admin-console-identity,.admin-panel,.admin-provider-card,
  .data-table-wrapper,.fieldset,.page-header,.admin-theme-card,
  .frontend-theme-card,.admin-pos-kpi-card,.admin-quick-launch-panel,
  .admin-module-panel,.admin-module-card,.admin-control-deck,
  .admin-ui-page .card,.admin-user-card,.admin-page-section,
  .account-mode-card,.account-preset-card
) {
  background:var(--admin-ui-panel)!important;
  border-color:var(--admin-ui-border)!important;
  color:var(--admin-ui-text)!important;
}
html[data-theme="dark"] body.role-admin.ui-v2 :where(
  .admin-workspace-nav,.admin-nav-search,.admin-ops-chart-bars,
  .admin-pos-clock,.admin-pos-icon-button,.admin-pos-search-button,
  .admin-pos-public-button,.admin-pos-kpi-top>span,.admin-quick-launch-grid>a>span,
  .admin-quick-launch-grid>button>span,.admin-console-tabs,.admin-tabs,
  .admin-theme-card-copy,.account-feature-toggle
) {
  background:var(--admin-ui-panel-alt)!important;
  border-color:var(--admin-ui-border)!important;
  color:var(--admin-ui-text)!important;
}
html[data-theme="dark"] body.role-admin.ui-v2 :is(.admin-quick-launch-grid>a,.admin-quick-launch-grid>button) {
  background:var(--admin-ui-panel-alt)!important;
  border-color:var(--admin-ui-border)!important;
  color:var(--admin-ui-text)!important;
}
html[data-theme="dark"] body.role-admin.ui-v2 .admin-workspace-nav :is(.tab-button,a.tab-button):hover,
html[data-theme="dark"] body.role-admin.ui-v2 .admin-workspace-nav :is(.tab-button,a.tab-button).active {
  background:var(--admin-ui-panel-alt)!important;
  border-color:var(--admin-ui-border)!important;
  color:var(--admin-ui-text)!important;
}
html[data-theme="dark"] body.role-admin.ui-v2 :is(
  .admin-console-head h2,.admin-panel-head h3,.admin-ui-page h1,
  .admin-ui-page h2,.admin-ui-page h3,.card-title,.page-header h1
) { color:var(--admin-ui-text)!important; }
html[data-theme="dark"] body.role-admin.ui-v2 :is(
  .admin-console-head p,.admin-panel-head p,.card-subtitle,
  .admin-workspace-nav .tab-button small,.admin-nav-group>span,
  .admin-pos-kpi-foot,.admin-ops-chart-value
) { color:var(--admin-ui-muted)!important; }
html[data-theme="dark"] body.role-admin.ui-v2 .admin-panel>.admin-panel-head,
html[data-theme="dark"] body.role-admin.ui-v2 .card>.card-header {
  border-color:var(--admin-ui-border)!important;
}

/* Theme-card thumbnails, PDF previews and QR images are intentionally shown on
   white "paper" and are NOT altered; only their surrounding chrome is fixed. */
@media print {
  html[data-theme="dark"] body.ui-v2 { color-scheme:light; }
}
