:root {
  color-scheme: dark;
  --bg: #09111a;
  --panel: rgba(18, 24, 32, 0.88);
  --panel-solid: #111923;
  --panel-strong: rgba(10, 14, 20, 0.95);
  --line: rgba(255, 255, 255, 0.14);
  --line-strong: rgba(255, 255, 255, 0.24);
  --text: #f5f8fb;
  --muted: #b7c3cd;
  --quiet: #82909c;
  --accent: #36c7d7;
  --warn: #f0c14b;
}

* {
  box-sizing: border-box;
}

html,
body,
#app-shell,
#map {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* Critical Leaflet layout fallback in case the CDN stylesheet is blocked. */
.leaflet-container {
  overflow: hidden;
  background: #ddd;
  outline-offset: 1px;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
  position: absolute;
  left: 0;
  top: 0;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
  user-select: none;
  -webkit-user-drag: none;
}

.leaflet-tile {
  width: 256px;
  height: 256px;
  filter: inherit;
  visibility: hidden;
}

.leaflet-tile-loaded {
  visibility: inherit;
}

.leaflet-zoom-animated {
  transform-origin: 0 0;
}

.leaflet-control {
  position: relative;
  z-index: 800;
  pointer-events: auto;
}

.leaflet-top,
.leaflet-bottom {
  position: absolute;
  z-index: 1000;
  pointer-events: none;
}

.leaflet-top {
  top: 0;
}

.leaflet-right {
  right: 0;
}

.leaflet-bottom {
  bottom: 0;
}

.leaflet-left {
  left: 0;
}

.leaflet-control-zoom {
  margin: 10px;
}

.leaflet-control-zoom a {
  display: block;
  width: 30px;
  height: 30px;
  background: #fff;
  color: #111;
  line-height: 30px;
  text-align: center;
  text-decoration: none;
}

.leaflet-control-attribution {
  margin: 0;
  padding: 0 5px;
  color: #222;
  font-size: 11px;
}

.leaflet-popup {
  position: absolute;
  text-align: center;
  margin-bottom: 20px;
}

.leaflet-popup-content-wrapper {
  padding: 1px;
  text-align: left;
}

.leaflet-popup-content {
  min-width: 1px;
  line-height: 1.4;
}

.leaflet-popup-tip-container {
  width: 40px;
  height: 20px;
  position: absolute;
  left: 50%;
  margin-left: -20px;
  overflow: hidden;
  pointer-events: none;
}

.leaflet-popup-tip {
  width: 17px;
  height: 17px;
  padding: 1px;
  margin: -10px auto 0;
  transform: rotate(45deg);
}

.leaflet-popup-pane,
.leaflet-marker-pane,
.leaflet-tooltip-pane,
.leaflet-shadow-pane,
.leaflet-tile-pane,
.leaflet-overlay-pane {
  z-index: 400;
}

.leaflet-tile-pane {
  z-index: 200;
}

.leaflet-overlay-pane {
  z-index: 400;
}

.leaflet-shadow-pane {
  z-index: 500;
}

.leaflet-marker-pane {
  z-index: 600;
}

.leaflet-tooltip-pane {
  z-index: 650;
}

.leaflet-popup-pane {
  z-index: 700;
}

.current-particles-canvas,
.current-arrows-canvas {
  position: absolute;
  z-index: 450;
  pointer-events: none;
}

.leaflet-popup-content-wrapper {
  border-radius: 8px;
  background: rgba(15, 22, 31, 0.96);
  color: var(--text);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.28);
}

.leaflet-popup-tip {
  background: rgba(15, 22, 31, 0.96);
}

.leaflet-popup-content {
  margin: 10px 12px;
}

.leaflet-popup-close-button {
  position: absolute;
  top: 6px;
  right: 8px;
  width: 22px;
  height: 22px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 22px;
  text-align: center;
  text-decoration: none;
}

.leaflet-popup-close-button:hover {
  color: var(--text);
}

.value-popup {
  display: grid;
  gap: 5px;
  min-width: 210px;
}

.value-popup span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 760;
}

.value-popup strong {
  color: var(--text);
  font-size: 0.84rem;
}

.value-popup-title {
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}

.value-popup-variable {
  padding-top: 3px;
}

.value-popup .value-popup-value {
  color: var(--accent);
  font-size: 1.08rem;
}

.value-popup-time {
  color: var(--quiet);
}

button,
select,
input {
  font: inherit;
}

#app-shell {
  position: relative;
  overflow: hidden;
}

#map {
  z-index: 1;
}

.panel {
  position: absolute;
  z-index: 500;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(14px);
}

.sidebar {
  top: 16px;
  left: 16px;
  width: min(336px, calc(100vw - 32px));
  padding: 14px;
  border-radius: 8px;
  display: grid;
  gap: 14px;
}

.sidebar-toggle {
  display: none;
  position: absolute;
  z-index: 620;
  top: 12px;
  left: 12px;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(12px);
}

.timeline {
  left: 50%;
  bottom: 18px;
  width: min(920px, calc(100vw - 32px));
  transform: translateX(-50%);
  padding: 10px 12px;
  border-radius: 8px;
  display: grid;
  grid-template-columns: repeat(5, 38px) minmax(180px, 1fr) 68px minmax(190px, 250px);
  align-items: center;
  gap: 8px;
}

.legend-head span,
label {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  letter-spacing: 0.02em;
}

.brand-block {
  display: grid;
  grid-template-columns: 34px 1fr;
  align-items: center;
  gap: 12px;
  padding-bottom: 11px;
  border-bottom: 1px solid var(--line);
}

.brand-logo {
  width: 34px;
  height: auto;
  display: block;
  opacity: 0.95;
}

.brand-block strong {
  font-size: 1.2rem;
  line-height: 1.18;
}

.control-stack {
  display: grid;
  gap: 11px;
}

label {
  display: grid;
  gap: 6px;
}

select,
button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
}

select {
  width: 100%;
  padding: 0 10px;
}

button {
  cursor: pointer;
}

button:hover,
button.active {
  border-color: rgba(54, 199, 215, 0.7);
}

button.active {
  background: rgba(54, 199, 215, 0.18);
}

button:disabled,
input:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(3, 42px);
  gap: 8px;
}

.tool-row button,
.timeline button {
  font-size: 1rem;
  font-weight: 800;
}

#nowTimeButton {
  min-width: 68px;
  padding: 0 10px;
  color: var(--muted);
  font-size: 0.78rem;
}

.basemap-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}

.basemap-button {
  min-width: 0;
  padding: 0 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 750;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.toggle {
  grid-template-columns: 18px 1fr;
  align-items: center;
  gap: 9px;
  color: var(--text);
}

.toggle input {
  accent-color: var(--accent);
}

.legend {
  display: grid;
  gap: 8px;
  padding-top: 11px;
  border-top: 1px solid var(--line);
}

.legend-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.legend-head strong {
  color: var(--text);
  font-size: 0.78rem;
}

.legend-ramp {
  height: 12px;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: linear-gradient(90deg, #2d4f95, #27a075, #e6cb34, #d4522d);
}

.legend-ticks {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--quiet);
  font-size: 0.72rem;
  font-weight: 700;
}

.sidebar-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding-top: 2px;
  color: var(--quiet);
  font-size: 0.73rem;
  font-weight: 720;
}

.sidebar-meta span {
  color: var(--quiet);
  white-space: nowrap;
}

#timeLabel {
  color: var(--text);
  font-size: 0.86rem;
  font-weight: 740;
  text-align: right;
  white-space: nowrap;
}

#status {
  position: absolute;
  z-index: 530;
  right: 16px;
  top: 16px;
  max-width: min(380px, calc(100vw - 32px));
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(8, 12, 18, 0.86);
  color: var(--muted);
  font-size: 0.82rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

#status.visible {
  opacity: 1;
}

#loading {
  position: absolute;
  z-index: 520;
  top: 18px;
  right: 18px;
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255, 255, 255, 0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 140ms ease;
  animation: spin 0.8s linear infinite;
  pointer-events: none;
}

#loading.visible {
  opacity: 1;
}

.leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.78);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 720px) {
  .sidebar-toggle {
    display: block;
  }

  .sidebar {
    top: 10px;
    left: 10px;
    width: calc(100vw - 20px);
    max-height: calc(100vh - 102px);
    overflow: auto;
    transform: translateY(calc(-100% - 18px));
    opacity: 0;
    pointer-events: none;
    transition: transform 180ms ease, opacity 180ms ease;
  }

  body.sidebar-open .sidebar {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  body.sidebar-open .sidebar-toggle {
    left: auto;
    right: 18px;
  }

  .timeline {
    bottom: 8px;
    width: calc(100vw - 20px);
    padding: 8px;
    grid-template-columns: repeat(5, 30px) 54px minmax(0, 1fr);
    grid-auto-rows: 30px 20px;
    gap: 6px;
  }

  .timeline button {
    min-height: 30px;
    height: 30px;
    padding: 0;
    font-size: 0.9rem;
  }

  #nowTimeButton {
    min-width: 0;
    padding: 0 6px;
    font-size: 0.72rem;
  }

  .timeline input {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
  }

  #timeLabel {
    grid-column: 7;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
    text-align: right;
    text-overflow: ellipsis;
    font-size: 0.76rem;
  }

  #status {
    right: 10px;
    top: auto;
    bottom: 92px;
  }
}
