/* Load after the page styles. Map tiles, tactical symbols and photos retain their colors. */
html[data-theme="light"] { color-scheme: light; }
html[data-theme="dark"] {
  color-scheme: dark;
  color: #e4edf8;
  background: #101a29;
  --blue: #82b6ff;
  --muted: #acbdd1;
  --line: #36485f;
  --theme-text: #e4edf8;
  --theme-page: #101a29;
  --theme-surface: #182638;
  --theme-raised: #203147;
  --theme-subtle: #1c2c40;
  --theme-field: #111e2e;
  --theme-primary: #2563cb;
  --theme-primary-hover: #3375e3;
}

/* The appearance selector stays out of the main navigation. */
.theme-preference {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  max-width: 100%;
  color: inherit;
}
.theme-preference > label {
  flex: 0 0 auto;
  margin: 0;
  font-size: 13px;
  font-weight: 650;
}
.theme-preference > select {
  flex: 0 1 145px;
  width: 145px;
  min-width: 126px;
  min-height: 44px;
  margin: 0;
  padding: 9px 28px 9px 11px;
  font-size: 14px;
}
.theme-preference--projector {
  justify-content: space-between;
  border-top: 1px solid var(--line);
  padding-top: 12px;
  margin-top: 12px;
}
.theme-preference--panel { margin-top: 24px; }
.theme-preference--floating {
  position: fixed;
  z-index: 1000;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  max-width: calc(100vw - 32px);
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: #172b46;
  box-shadow: 0 2px 12px #0002;
}

html[data-theme="dark"] body { background: var(--theme-page); color: var(--theme-text); }
html[data-theme="dark"] header,
html[data-theme="dark"] header.site-header {
  background: var(--theme-surface);
  border-color: var(--line);
  box-shadow: 0 3px 14px #0002;
}
html[data-theme="dark"] .brand-logo {
  background: #f3f6fa;
  padding: 5px;
  border-radius: 12px;
}
html[data-theme="dark"] .site-header .nav-link {
  color: #d3e2f5;
  background: var(--theme-raised);
  border-color: transparent;
}
html[data-theme="dark"] .site-header .nav-link:hover {
  background: #2b4261;
  border-color: #567aa8;
  color: #eaf3ff;
}
html[data-theme="dark"] .site-header .nav-link[aria-current="page"] {
  background: var(--theme-primary);
  border-color: var(--theme-primary);
  color: #fff;
  box-shadow: 0 3px 8px #0003;
}
html[data-theme="dark"] .site-header .nav-link[aria-current="page"]:hover {
  background: var(--theme-primary-hover);
  border-color: var(--theme-primary-hover);
}
html[data-theme="dark"] .site-header #session-controls.nav-session {
  background: var(--theme-field);
  border-color: var(--line);
}
html[data-theme="dark"] .site-header .nav-session .session-name { color: var(--muted); }
html[data-theme="dark"] .site-header .nav-session .button {
  color: #d3e2f5;
  background: var(--theme-raised);
  border-color: var(--line);
}
html[data-theme="dark"] .site-header .nav-session .button:hover {
  color: #fff;
  background: #2b4261;
  border-color: #567aa8;
}

html[data-theme="dark"] .org-card,
html[data-theme="dark"] .sidebar,
html[data-theme="dark"] .widget,
html[data-theme="dark"] .empty,
html[data-theme="dark"] .maintenance,
html[data-theme="dark"] .login-panel,
html[data-theme="dark"] .panel,
html[data-theme="dark"] .legal-content,
html[data-theme="dark"] dialog {
  background: var(--theme-surface);
  color: var(--theme-text);
  border-color: var(--line);
}
html[data-theme="dark"] .org-card:hover { box-shadow: 0 8px 24px #0004; }
html[data-theme="dark"] .org-card { border-top-color: var(--accent); }
html[data-theme="dark"] .org-emblem { color: var(--theme-text); }
html[data-theme="dark"] .card-action { color: var(--blue); }
html[data-theme="dark"] .tag,
html[data-theme="dark"] .count {
  color: #d3e2f5;
  background: var(--theme-raised);
  border-color: var(--line);
}
html[data-theme="dark"] .info-strip,
html[data-theme="dark"] .status-location,
html[data-theme="dark"] #leader-connection {
  background: #20334c;
  border-color: var(--line);
}
html[data-theme="dark"] .group-button { color: #d3e2f5; }
html[data-theme="dark"] .group-button:hover { background: var(--theme-raised); }
html[data-theme="dark"] .group-button[aria-current="true"] {
  background: #223f66;
  color: #afd0ff;
}
html[data-theme="dark"] .widget-head { border-color: var(--line); }
html[data-theme="dark"] input,
html[data-theme="dark"] textarea,
html[data-theme="dark"] select {
  color: var(--theme-text);
  background: var(--theme-field);
  border-color: #5b718e;
}
html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder { color: #94a8c1; opacity: 1; }
html[data-theme="dark"] input[type="checkbox"],
html[data-theme="dark"] input[type="radio"] { accent-color: #82b6ff; }
html[data-theme="dark"] :focus-visible { outline-color: #ffc861; }
html[data-theme="dark"] .button {
  color: #dfeafb;
  background: var(--theme-raised);
  border-color: #526a89;
}
html[data-theme="dark"] .button:hover { background: #2b4261; border-color: #7896bc; }
html[data-theme="dark"] .button.primary,
html[data-theme="dark"] .tutorial-role-nav .button[aria-current="page"] {
  color: #fff;
  background: var(--theme-primary);
  border-color: var(--theme-primary);
}
html[data-theme="dark"] .button.primary:hover,
html[data-theme="dark"] .tutorial-role-nav .button[aria-current="page"]:hover {
  background: var(--theme-primary-hover);
  border-color: var(--theme-primary-hover);
}
html[data-theme="dark"] .button.danger,
html[data-theme="dark"] .form-error { color: #ffabb7; }
html[data-theme="dark"] .empty { border-color: #7286a2; }
html[data-theme="dark"] .photo img { background: var(--theme-field); }
html[data-theme="dark"] .upload-box,
html[data-theme="dark"] .route-template,
html[data-theme="dark"] .legal-toc,
html[data-theme="dark"] .tutorial-toc,
html[data-theme="dark"] .audit-values,
html[data-theme="dark"] .change-values pre {
  background: var(--theme-subtle);
  border-color: #526a89;
}
html[data-theme="dark"] .maintenance-symbol,
html[data-theme="dark"] .banner,
html[data-theme="dark"] .tutorial-important {
  background: #3f321b;
  color: #ffe2a1;
  border-color: #a47a31;
}
html[data-theme="dark"] .legal-notice,
html[data-theme="dark"] .radio-address-note {
  background: #20334c;
  border-color: #82b6ff;
}
html[data-theme="dark"] .admin-row,
html[data-theme="dark"] .catalog-card,
html[data-theme="dark"] .catalog-group,
html[data-theme="dark"] .station-row { border-color: var(--line); }
html[data-theme="dark"] .catalog-card { border-top-color: var(--accent); }
html[data-theme="dark"] dialog { box-shadow: 0 20px 70px #0009; }
html[data-theme="dark"] dialog::backdrop { background: #030914bb; }
html[data-theme="dark"] #toast {
  background: #2b4261;
  color: #fff;
  border: 1px solid #6a84a6;
  box-shadow: 0 3px 16px #0005;
}
html[data-theme="dark"] .text-link { color: var(--blue); text-decoration: underline; text-underline-offset: 3px; }

/* Administration: retain clear, large summary targets in both collapsed states. */
html[data-theme="dark"] body[data-page="admin"] .admin-section {
  background: var(--theme-surface);
  border-color: var(--line);
  box-shadow: 0 2px 7px #0002;
}
html[data-theme="dark"] body[data-page="admin"] .admin-section > .admin-section-heading { color: #d3e2f5; }
html[data-theme="dark"] body[data-page="admin"] .admin-section > .admin-section-heading:hover,
html[data-theme="dark"] body[data-page="admin"] .admin-section > .admin-section-heading:focus-visible {
  background: #253c5a;
  color: #afd0ff;
}
html[data-theme="dark"] body[data-page="admin"] .admin-section[open] > .admin-section-heading {
  background: var(--theme-raised);
  color: #afd0ff;
  border-color: var(--line);
}

/* Status colors retain their meaning with brighter text and darker surfaces. */
html[data-theme="dark"] .status-panel { border-top-color: var(--blue); }
html[data-theme="dark"] .status-pill { color: #d7e9ff; background: #314c70; }
html[data-theme="dark"] .status-pill.s3 { color: #ffe1a0; background: #4c391b; }
html[data-theme="dark"] .status-pill.s4,
html[data-theme="dark"] .status-pill.s1,
html[data-theme="dark"] .status-pill.s2 { color: #b0f0cb; background: #204836; }
html[data-theme="dark"] .status-pill.s0,
html[data-theme="dark"] .status-pill.s6 { color: #ffc3ce; background: #532634; }
html[data-theme="dark"] .status-pill.s5 { color: #e0c6ff; background: #46315d; }
html[data-theme="dark"] .status-key {
  color: #dceaff;
  background: var(--theme-raised);
  border-color: #526a89;
}
html[data-theme="dark"] .status-key:hover:not(:disabled) { background: #2b4261; }
html[data-theme="dark"] .status-key.selected { color: #fff; background: #223f66; border-color: #82b6ff; }
html[data-theme="dark"] .status-key:disabled { color: #a5b5ca; background: #162131; }
html[data-theme="dark"] .route-list li.current { color: #dceaff; background: #223f66; }
html[data-theme="dark"] .status-table th { background: var(--theme-raised); color: #e4edf8; }
html[data-theme="dark"] .status-table td { border-color: var(--line); }
html[data-theme="dark"] .speech-card { border-color: var(--line); }
html[data-theme="dark"] .speech-card.pending { background: #38232e; border-left-color: #f98c99; }
html[data-theme="dark"] .speech-card.handled { background: #1b352d; border-left-color: #63d79e; }
html[data-theme="dark"] .speech-card small { color: var(--muted); }
html[data-theme="dark"] .emergency-card,
html[data-theme="dark"] #emergency-banner {
  color: #ffd0d6;
  background: #431f2a;
  border-color: #ef7183;
}
html[data-theme="dark"] .emergency-card > strong,
html[data-theme="dark"] .status-table .emergency-label { color: #ffb5c1; }
html[data-theme="dark"] #emergency-popup { border-color: #ef7183; }
html[data-theme="dark"] #emergency-popup::backdrop { background: #25050bb3; }
html[data-theme="dark"] .status-table tr.has-emergency td { background: #431f2a; }
html[data-theme="dark"] .position-source.gps,
html[data-theme="dark"] .map-position-legend .gps { color: #81e1a7; }
html[data-theme="dark"] .position-source.fallback,
html[data-theme="dark"] .map-position-legend .estimated { color: #f2cb78; }

/* Only map chrome changes. Tile imagery and all vehicle/tactical markers stay original. */
html[data-theme="dark"] .ops-map { border-color: var(--line); }
html[data-theme="dark"] .leaflet-container { background: #263649; }
html[data-theme="dark"] .leaflet-container a { color: #82b6ff; }
html[data-theme="dark"] .leaflet-popup-content-wrapper,
html[data-theme="dark"] .leaflet-popup-tip,
html[data-theme="dark"] .leaflet-tooltip,
html[data-theme="dark"] .leaflet-control-layers {
  color: var(--theme-text);
  background: var(--theme-surface);
  border-color: var(--line);
}
html[data-theme="dark"] .leaflet-container a.leaflet-popup-close-button { color: #bfd0e4; }
html[data-theme="dark"] .leaflet-bar a {
  color: #e4edf8;
  background: var(--theme-surface);
  border-color: var(--line);
}
html[data-theme="dark"] .leaflet-bar a:hover { background: #2b4261; }
html[data-theme="dark"] .leaflet-bar a.leaflet-disabled { color: #879bb6; background: #182638; }
html[data-theme="dark"] .leaflet-control-attribution { color: #ccdaea; background: #172638ed; }
html[data-theme="dark"] .leaflet-control-attribution a { color: #a6caff; }
html[data-theme="dark"] .leaflet-tooltip-top::before { border-top-color: var(--line); }
html[data-theme="dark"] .leaflet-tooltip-bottom::before { border-bottom-color: var(--line); }
html[data-theme="dark"] .leaflet-tooltip-left::before { border-left-color: var(--line); }
html[data-theme="dark"] .leaflet-tooltip-right::before { border-right-color: var(--line); }
html[data-theme="dark"] .projector-page #projector-location-legend,
html[data-theme="dark"] .projector-page #projector-alarm-controls {
  background: #182638f5;
  color: var(--theme-text);
  border-color: var(--line);
}
html[data-theme="dark"] .projector-page .map-connection-warning {
  color: #ffe2a1;
  background: #3f321b;
  border-color: #a47a31;
}
html[data-theme="dark"] .projector-page .projector-alarm-dialog { border-color: var(--line); }
html[data-theme="dark"] .projector-page [data-projector-alarm][aria-pressed="true"] {
  color: #c5f5d9;
  background: #24503a;
  border-color: #70c992;
}
html[data-theme="dark"] .theme-preference--floating { background: var(--theme-surface); color: var(--theme-text); }

/* Keep the static tutorial fallback readable when scripts are unavailable. */
html[data-theme="dark"]:not(.tutorial-enhanced):has(#admins:target,#admins :target) .tutorial-role-nav [data-guide-role="groups"] {
  color: #dfeafb;
  background: var(--theme-raised);
  border-color: #526a89;
}
html[data-theme="dark"]:not(.tutorial-enhanced):has(#admins:target,#admins :target) .tutorial-role-nav [data-guide-role="admins"] {
  color: #fff;
  background: var(--theme-primary);
  border-color: var(--theme-primary);
}

@media (max-width: 650px) {
  footer > .theme-preference { width: 100%; padding-top: 6px; }
  .theme-preference--projector { gap: 8px; }
  .theme-preference--projector > label { font-size: 12px; }
  .theme-preference--projector > select { flex-basis: 126px; width: 126px; font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) {
  .theme-preference { scroll-behavior: auto; }
}
