* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #ffffff;
  --fg: #1a1a2e;
  --sidebar-bg: #ffffffee;
  --border: #e0e0e0;
  --stat-bg: #f5f5f5;
  --muted: #666;
  --slider-track: #ddd;
  --slider-thumb: #3b82f6;
}

[data-theme="dark"] {
  --bg: #0f0f23;
  --fg: #e0e0e0;
  --sidebar-bg: #1a1a2eee;
  --border: #333;
  --stat-bg: #252540;
  --muted: #999;
  --slider-track: #444;
  --slider-thumb: #60a5fa;
}

body {
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
  height: 100vh;
  width: 100vw;
}

#map {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

#sidebar {
  position: absolute;
  top: 16px;
  left: 16px;
  width: 280px;
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 16px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 6px;
}

#info-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 2px;
  display: inline-flex;
  align-items: center;
  opacity: 0.6;
  transition: opacity 0.15s;
}

#info-btn:hover {
  opacity: 1;
}

/* Info modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
}

.modal-content {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 100;
  background: var(--sidebar-bg);
  backdrop-filter: blur(16px);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 32px;
  max-width: 520px;
  width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25);
}

.modal-content h2 {
  font-size: 18px;
  margin-bottom: 16px;
  font-weight: 700;
}

.modal-content h3 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--muted);
  margin-top: 18px;
  margin-bottom: 6px;
}

.modal-content p {
  margin-bottom: 8px;
}

.modal-content ul {
  margin: 0 0 8px 18px;
  padding: 0;
}

.modal-content li {
  margin-bottom: 4px;
}

.modal-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}

.modal-content th,
.modal-content td {
  text-align: left;
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
}

.modal-content th {
  font-weight: 600;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}

.modal-close:hover {
  background: var(--stat-bg);
  color: var(--fg);
}

/* LIVE badge */
#live-badge {
  background: #ef4444;
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  animation: pulse-live 2s ease-in-out infinite;
  flex-shrink: 0;
}

@keyframes pulse-live {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  50% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
}

/* Theme toggle — top right square button */
#theme-toggle {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  color: var(--fg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: background 0.15s;
  padding: 0;
}

#theme-toggle:hover {
  background: var(--stat-bg);
}

#stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stat {
  background: var(--stat-bg);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 24px;
  font-weight: 700;
}

.stat-label {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

/* Direction stats */
#direction-stats {
  display: flex;
  gap: 8px;
}

.stat-row {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  background: var(--stat-bg);
  border-radius: 8px;
  padding: 10px 10px;
}

.direction-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.inbound-label { color: #4caf50; }
.outbound-label { color: #f44336; }

.stat-value-sm {
  font-size: 20px;
  font-weight: 700;
}

/* Source filter toggle */
#source-filter h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 4px;
}

.source-buttons {
  display: flex;
  gap: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.source-btn {
  flex: 1;
  padding: 6px 0;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  letter-spacing: 0.05em;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.source-btn:not(:last-child) {
  border-right: 1px solid var(--border);
}

.source-btn:hover {
  background: var(--stat-bg);
}

.source-btn.active {
  background: var(--stat-bg);
  color: var(--fg);
}

#legend h3,
#zones-legend h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 4px;
}

#legend,
#zones-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
}

#legend h3,
#zones-legend h3 {
  width: 100%;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 1px 0;
  margin-right: 10px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot.cargo { background: #3b82f6; }
.dot.tanker { background: #ef4444; }
.dot.fishing { background: #22c55e; }
.dot.passenger { background: #f59e0b; }
.dot.other { background: #a855f7; }

.zone-swatch {
  width: 14px;
  height: 8px;
  border-radius: 2px;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.15);
}

.zone-swatch.bbox { background: rgba(255, 193, 7, 0.3); border-color: #ffc107; }
.zone-swatch.west { background: rgba(76, 175, 80, 0.3); border-color: #4caf50; }
.zone-swatch.east { background: rgba(33, 150, 243, 0.3); border-color: #2196f3; }
.zone-swatch.tss { background: rgba(255, 152, 0, 0.3); border-color: #ff9800; }

.line-swatch {
  width: 16px;
  height: 2px;
  flex-shrink: 0;
}

.line-swatch.inbound { background: #4caf50; }
.line-swatch.outbound { background: #f44336; }

#snapshot-info {
  font-size: 11px;
  color: var(--muted);
}

#attribution,
#attribution p,
#attribution a {
  font-size: 10px;
  color: var(--muted);
  line-height: 1.4;
}

#attribution {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  margin-top: 4px;
}

#attribution a {
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Timeline bar */
#timeline-bar {
  position: absolute;
  bottom: 16px;
  left: 16px;
  right: 16px;
  height: 48px;
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 0 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
}

#timeline-play,
#timeline-chart-toggle {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--stat-bg);
  color: var(--fg);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

#timeline-play:hover,
#timeline-chart-toggle:hover {
  background: var(--slider-thumb);
  color: #fff;
}

#timeline-chart-toggle.active {
  background: var(--slider-thumb);
  color: #fff;
}

#timeline-slider {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--slider-track);
  border-radius: 2px;
  outline: none;
}

#timeline-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--slider-thumb);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

#timeline-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--slider-thumb);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

#timeline-date {
  font-size: 13px;
  font-weight: 600;
  min-width: 90px;
  text-align: center;
  flex-shrink: 0;
}

#timeline-count {
  font-size: 11px;
  color: var(--muted);
  min-width: 60px;
  text-align: right;
  flex-shrink: 0;
}

/* Chart panel — expands upward from timeline bar */
#chart-panel {
  position: absolute;
  bottom: 72px;
  left: 16px;
  right: 16px;
  height: 210px;
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  border-radius: 12px;
  border: 1px solid var(--border);
  z-index: 10;
  padding: 12px 16px 8px;
  overflow: hidden;
}

#chart-tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 6px;
}

.chart-tab {
  padding: 3px 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: all 0.15s;
}

.chart-tab:hover {
  color: var(--fg);
}

.chart-tab.active {
  background: var(--stat-bg);
  color: var(--fg);
}

#chart-container {
  width: 100%;
  height: calc(100% - 28px);
}

/* Observable Plot overrides */
#chart-container figure {
  margin: 0;
}

#chart-container svg {
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Hide Observable Plot's built-in SVG tip */
#chart-container [aria-label="tip"] {
  display: none !important;
}

/* Custom chart tooltip — matches vessel popup style */
.chart-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--muted);
  font-size: 13px;
}

.chart-tooltip {
  position: absolute;
  z-index: 20;
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--sidebar-bg);
  color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  backdrop-filter: blur(12px);
  pointer-events: none;
  white-space: nowrap;
  line-height: 1.5;
}

.hidden {
  display: none !important;
}

.maplibregl-popup-content {
  font-family: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  padding: 14px 16px;
  border-radius: 10px;
  background: var(--sidebar-bg);
  max-height: 240px;
  overflow-y: auto;
  color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.maplibregl-popup-close-button {
  font-size: 18px;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  padding: 0;
  top: 6px;
  right: 6px;
  color: var(--muted);
  border-radius: 4px;
}

.maplibregl-popup-close-button:hover {
  background: var(--stat-bg);
  color: var(--fg);
}

.maplibregl-popup-tip {
  border-top-color: var(--sidebar-bg);
}

[data-theme="dark"] .maplibregl-popup-tip {
  border-top-color: var(--sidebar-bg);
}

.popup-title {
  font-weight: 600;
  margin-bottom: 4px;
}

.popup-detail {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.popup-header {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.popup-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 8px 0;
}

@media (max-width: 768px) {
  #sidebar {
    width: 240px;
    left: 8px;
    top: 8px;
    padding: 14px;
  }

  #timeline-bar {
    left: 8px;
    right: 8px;
    bottom: 8px;
  }
}
