/* Shared screen palette. Literal fallbacks in each component preserve its light design. */
@media screen {
  html { color-scheme: light; }
  html:not([data-theme="dark"]) .btn-outline-secondary { --bs-btn-color: #56636c; }
  html:not([data-theme="dark"]) .app-main a:not([class]) { color: #126652; }
  html:not([data-theme="dark"]) .app-main .text-success { color: #146b43 !important; }
  html:not([data-theme="dark"]) .app-main .text-danger { color: #bb2938 !important; }
  html:not([data-theme="dark"]) .app-main .text-warning { color: #925806 !important; }
  html:not([data-theme="dark"]) .bug-status { color: color-mix(in srgb, var(--bug-color) 80%, #14272b); }
  html[data-theme="dark"] {
    color-scheme: dark;
    --theme-page: #0e1b1e;
    --theme-panel: #16272b;
    --theme-surface: #1d3236;
    --theme-text: #e5f0ed;
    --theme-muted: #afc3be;
    --theme-border: #385259;
    --theme-green-surface: #173e35;
    --theme-blue-surface: #1d344c;
    --theme-red-surface: #42292e;
    --theme-amber-surface: #423923;
    --theme-purple-surface: #342e47;
    --theme-green-text: #79dfbb;
    --theme-blue-text: #a1c5ff;
    --theme-red-text: #ffa8ae;
    --theme-amber-text: #f3ce83;
    --theme-purple-text: #d3b7fa;
    --ui-ink: var(--theme-text); --ui-muted: var(--theme-muted);
    --ui-accent: #79dfbb; --ui-accent-dark: #a1ebcf;
    --ui-line: var(--theme-border); --ui-surface: var(--theme-page);
    --ink: var(--theme-text); --muted: var(--theme-muted); --line: var(--theme-border); --bg: var(--theme-page); --accent: #79dfbb;
    --app-bg: var(--theme-page); --app-surface: var(--theme-panel); --app-surface-muted: var(--theme-surface);
    --app-border: var(--theme-border); --app-text: var(--theme-text); --app-muted: var(--theme-muted);
    --app-primary: #79dfbb; --app-primary-dark: #a1ebcf; --app-sidebar: #101f23; --app-sidebar-muted: #afc3be;
    --app-shadow: 0 12px 32px #0004; --app-focus: 0 0 0 .22rem #79dfbb33;
    --iv-ink: var(--theme-text); --iv-muted: var(--theme-muted); --iv-line: var(--theme-border); --iv-accent: #79dfbb; --iv-bg: var(--theme-page);
    --portal-ink: var(--theme-text); --portal-muted: var(--theme-muted); --portal-soft: var(--theme-muted);
    --portal-line: var(--theme-border); --portal-line-strong: #536e73;
    --portal-surface: var(--theme-panel); --portal-surface-muted: var(--theme-surface); --portal-bg: var(--theme-page);
    --portal-primary: #79dfbb; --portal-primary-dark: #a1ebcf; --portal-primary-soft: var(--theme-green-surface);
    --bs-body-bg: var(--theme-page); --bs-body-color: var(--theme-text);
    --bs-secondary-color: var(--theme-muted); --bs-secondary-bg: var(--theme-surface); --bs-tertiary-bg: var(--theme-panel);
    --bs-border-color: var(--theme-border); --bs-emphasis-color: var(--theme-text); --bs-link-color: #79dfbb; --bs-link-hover-color: #a1ebcf;
    --pwa-surface: var(--theme-panel); --pwa-text: var(--theme-text); --pwa-muted: var(--theme-muted);
    --pwa-primary: #247b65; --pwa-accent: #79dfbb;
    --portal-success: #79dfbb; --portal-danger: #ffa8ae;
  }
  html[data-theme="dark"] .unified-workspace {
    --ink: var(--theme-text); --muted: var(--theme-muted); --line: var(--theme-border); --accent: #79dfbb;
    --app-bg: var(--theme-page); --app-border: var(--theme-border); --app-text: var(--theme-text);
    --app-muted: var(--theme-muted); --app-primary: #79dfbb; --app-primary-dark: #a1ebcf;
    background: var(--theme-page);
  }
  html[data-theme="dark"] { scrollbar-color: #617e76 var(--theme-page); }
  html[data-theme="dark"] :is(.stat-card strong, .dashboard-bug-card strong, .module-license-metric strong) { color: var(--theme-text); }
  html[data-theme="dark"] .recent-license-column { --recent-soft: var(--theme-surface); --recent-accent: #a1c5ff; }
  html[data-theme="dark"] .table-light { --bs-table-bg: var(--theme-surface); --bs-table-color: var(--theme-text); }
  html[data-theme="dark"] .btn-outline-warning { --bs-btn-color: #f3ce83; --bs-btn-border-color: #b79757; }
  html[data-theme="dark"] body { color-scheme: dark; }
  html[data-theme="dark"] :is(dialog, .modal-content, .offcanvas, .dropdown-menu) { background-color: var(--theme-panel); color: var(--theme-text); }
  html[data-theme="dark"] :is(input, select, textarea) { color-scheme: dark; }
  html[data-theme="dark"] :is(.form-control, .form-select, .input-group-text, .form-control:disabled, .form-control[readonly]) { background-color: var(--theme-panel); color: var(--theme-text); border-color: var(--theme-border); }
  html[data-theme="dark"] .form-select { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23c4d8d2' 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(.card, .list-group-item) { background-color: var(--theme-panel); color: var(--theme-text); }
  html[data-theme="dark"] .card-header { border-color: var(--theme-border); }
  html[data-theme="dark"] .app-main a:not([class]) { color: var(--theme-green-text); }
  html[data-theme="dark"] .text-primary { color: var(--theme-blue-text) !important; }
  html[data-theme="dark"] .text-success { color: var(--theme-green-text) !important; }
  html[data-theme="dark"] .text-danger { color: var(--theme-red-text) !important; }
  html[data-theme="dark"] .text-warning { color: var(--theme-amber-text) !important; }
  html[data-theme="dark"] code { color: var(--theme-purple-text); }
  html[data-theme="dark"] .table :is(thead, th) { color: var(--theme-text); }
  html[data-theme="dark"] .alert-warning { color: var(--theme-amber-text); background: var(--theme-amber-surface); border-color: var(--theme-border); }
  html[data-theme="dark"] .alert-danger { color: var(--theme-red-text); background: var(--theme-red-surface); border-color: var(--theme-border); }
  html[data-theme="dark"] .alert-success { color: var(--theme-green-text); background: var(--theme-green-surface); border-color: var(--theme-border); }
  html[data-theme="dark"] .alert-info { color: var(--theme-blue-text); background: var(--theme-blue-surface); border-color: var(--theme-border); }
  html[data-theme="dark"] .btn-primary { background: #247b65; color: #fff; border-color: #36977c; }
  html[data-theme="dark"] .btn-primary:hover { background: #1b6654; color: #fff; border-color: #79dfbb; }
  html[data-theme="dark"] .btn-outline-primary { color: #79dfbb; border-color: #4b8f7b; }
  html[data-theme="dark"] .btn-outline-primary:hover { background: #173e35; color: #d9fff0; }
  html[data-theme="dark"] .btn-outline-secondary { color: #c4d8d2; border-color: #57736a; }
  html[data-theme="dark"] .btn-outline-danger { color: #ffa8ae; border-color: #9f6970; }
  html[data-theme="dark"] .btn-outline-warning { color: #f3ce83; border-color: #b79757; }
  html[data-theme="dark"] :is(.btn-light, .bg-white, .bg-light) { background-color: var(--theme-panel) !important; color: var(--theme-text) !important; }
  html[data-theme="dark"] :is(.text-dark, .text-body) { color: var(--theme-text) !important; }
  html[data-theme="dark"] :is(.text-muted, .text-secondary) { color: var(--theme-muted) !important; }
  html[data-theme="dark"] .form-text { color: var(--theme-muted); }
  html[data-theme="dark"] .badge.bg-info.text-dark { color: #082d35 !important; }
  html[data-theme="dark"] .badge.bg-warning.text-dark { color: #342400 !important; }
  html[data-theme="dark"] .report-tabs a.active { color: #0a2c22; }
  /* Status colors are configurable: keep the tint/border, but use readable text. */
  html[data-theme="dark"] .bug-status { color: var(--theme-text); }
  html[data-theme="dark"] .btn-outline-dark {
    --bs-btn-color: #e5f0ed; --bs-btn-border-color: #78938c;
    --bs-btn-hover-bg: #e5f0ed; --bs-btn-hover-color: #0e1b1e;
    --bs-btn-active-bg: #e5f0ed; --bs-btn-active-color: #0e1b1e;
  }
  html[data-theme="dark"] .btn-outline-success {
    --bs-btn-color: #79dfbb; --bs-btn-border-color: #4b8f7b;
    --bs-btn-hover-bg: #173e35; --bs-btn-hover-color: #d9fff0;
    --bs-btn-active-bg: #173e35; --bs-btn-active-color: #d9fff0;
  }
  html[data-theme="dark"] .table { --bs-table-color: var(--theme-text); --bs-table-bg: var(--theme-panel); --bs-table-border-color: var(--theme-border); --bs-table-striped-bg: #ffffff05; --bs-table-striped-color: var(--theme-text); --bs-table-hover-bg: var(--theme-surface); --bs-table-hover-color: var(--theme-text); }
  html[data-theme="dark"] :is(button.primary, .iv-primary, .crm-nav-logo, .crm-brand > span, .iv-brand > span, .persian-picker-day[aria-pressed=true], .training-episode-number) { background: #79dfbb; color: #0a2c22; border-color: #79dfbb; }
  html[data-theme="dark"] .btn-primary { --bs-btn-bg: #247b65; --bs-btn-border-color: #36977c; --bs-btn-color: #fff; --bs-btn-hover-bg: #1b6654; --bs-btn-hover-border-color: #79dfbb; }
  html[data-theme="dark"] .btn-outline-primary { --bs-btn-color: #79dfbb; --bs-btn-border-color: #4b8f7b; --bs-btn-hover-bg: #173e35; --bs-btn-hover-color: #d9fff0; }
  html[data-theme="dark"] :is(.app-main, .trial-main, .customer-portal-page) .btn-outline-secondary { --bs-btn-color: #c4d8d2; --bs-btn-border-color: #57736a; }
  html[data-theme="dark"] .customer-login-page { background: radial-gradient(circle at 88% 10%, #245c4e50, transparent 25rem), #0e1b1e; }
  html[data-theme="dark"] .form-control::placeholder { color: var(--theme-muted); opacity: .8; }
  html[data-theme="dark"] .btn-close { filter: invert(1) grayscale(1); }
  /* A document is paper, even inside a dark workspace and in canvas exports. */
  .iv-paper, #co-print {
    color-scheme: light;
    --theme-page: initial; --theme-panel: initial; --theme-surface: initial;
    --theme-text: initial; --theme-muted: initial; --theme-border: initial;
    --theme-green-surface: initial; --theme-blue-surface: initial; --theme-red-surface: initial;
    --theme-amber-surface: initial; --theme-purple-surface: initial;
    --theme-green-text: initial; --theme-blue-text: initial; --theme-red-text: initial;
    --theme-amber-text: initial; --theme-purple-text: initial;
  }

  .theme-switcher { position: relative; flex: 0 0 auto; color: var(--ui-ink, #203b3c); font: inherit; z-index: 1080; }
  .theme-switcher > summary { display: flex; align-items: center; justify-content: center; gap: 8px; list-style: none; cursor: pointer; min-height: 44px; padding: 8px 14px; border: 1px solid var(--ui-line, #dbe7e2); border-radius: 13px; background: var(--theme-panel, #fff); color: inherit; white-space: nowrap; font-size: 13px; }
  .theme-switcher > summary::-webkit-details-marker { display: none; }
  .theme-switcher[open] > summary { border-color: var(--ui-accent, #197d68); box-shadow: 0 0 0 3px #197d681a; }
  .theme-symbol { width: 21px; height: 21px; flex: none; }
  .theme-chevron { font-size: 14px; }
  .theme-options { position: absolute; inset-inline-end: 0; top: calc(100% + 8px); width: 230px; max-width: calc(100vw - 28px); padding: 8px; background: var(--theme-panel, #fff); border: 1px solid var(--ui-line, #dbe7e2); border-radius: 17px; box-shadow: 0 14px 44px #0003; text-align: start; }
  .theme-options-title { display: block; padding: 7px 12px; color: var(--ui-muted, #566c6b); font-size: 11px; }
  .theme-options button { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px; margin: 2px 0; padding: 9px 16px; border: 1px solid transparent; border-radius: 11px; background: transparent; color: inherit; text-align: start; font: inherit; line-height: 1.6; cursor: pointer; }
  .theme-options button > span:first-child { width: 23px; flex: none; font-size: 22px; }
  .theme-options small { display: block; color: var(--ui-muted, #566c6b); font-size: 11px; }
  .theme-options button:hover { background: var(--theme-surface, #f4f7f5); }
  .theme-options button[aria-pressed=true] { background: var(--theme-green-surface, #e9f5ee); color: var(--theme-green-text, #126652); border-color: var(--theme-border, #bcddd0); }
  .theme-check { margin-inline-start: auto; visibility: hidden; }
  [aria-pressed=true] > .theme-check { visibility: visible; }
  .theme-standalone-header { display: flex; justify-content: flex-end; padding: 16px 24px; position: relative; z-index: 2; width: 100%; }
  .customer-login-page:has(.theme-standalone-header), .login-page:has(.theme-standalone-header) { display: flex; flex-direction: column; }
  .theme-page-header { display: flex; justify-content: flex-end; margin-block-end: 18px; }
  :is(.crm-top, .iv-top, .app-topbar, .customer-portal-header-inner, .trial-header) { overflow: visible; }
  .crm-top-links { display: flex; align-items: center; gap: 12px; }
  .customer-portal-header-inner, .trial-header { flex-wrap: wrap; }
  @media (max-width: 600px) {
    .theme-switcher > summary { width: 44px; padding: 9px; }
    .theme-switcher > summary [data-theme-label], .theme-chevron { display: none; }
    .theme-options { position: fixed; top: 78px; inset-inline: 14px; width: auto; max-width: none; }
    .theme-standalone-header { padding: 12px 16px; }
    .crm-top, .iv-top { gap: 8px; flex-wrap: wrap; }
  }
  @media (forced-colors: active) {
    .theme-options, .theme-switcher > summary { border: 1px solid CanvasText; }
    .theme-options button[aria-pressed=true] { border: 2px solid Highlight; }
  }
}
@media print { .theme-switcher, .theme-standalone-header, .theme-page-header { display: none !important; } }
