/* ==========================================================================
   CSS Custom Properties – Theme Tokens
   ========================================================================== */
:root {
  /* Dark theme (default) */
  --bg-body:         #111111;
  --bg-map:          #121212;
  --bg-panel:        rgba(20, 20, 20, 0.94);
  --bg-config:       rgba(255, 255, 255, 0.04);
  --bg-config-border:rgba(255, 255, 255, 0.06);
  --bg-stat-card:    rgba(255, 255, 255, 0.05);
  --bg-metrics:      rgba(255, 255, 255, 0.03);
  --bg-track-detail: rgba(255, 255, 255, 0.02);
  --bg-track-border: rgba(255, 255, 255, 0.08);
  --bg-select:       #2c2c2e;
  --bg-filter-hover: rgba(255, 255, 255, 0.05);
  --bg-filter-count: rgba(255, 255, 255, 0.06);
  --bg-slider-track: #3a3a3c;
  --bg-progress-bar: #2c2c2e;
  --bg-popup:        #1c1c1e;
  --bg-leaflet-ctrl: rgba(20, 20, 20, 0.9);
  --bg-playback:     rgba(255, 255, 255, 0.04);
  --bg-playback-btn: rgba(255, 255, 255, 0.08);

  --text-primary:    #ffffff;
  --text-secondary:  #cccccc;
  --text-muted:      #8e8e93;
  --text-label:      #a1a1a6;
  --text-filter:     #e5e5ea;
  --text-popup:      #ffffff;
  --text-select:     #ffffff;

  --border-panel:    rgba(255, 255, 255, 0.12);
  --border-select:   rgba(255, 255, 255, 0.2);
  --border-popup:    rgba(255, 255, 255, 0.15);
  --border-leaflet:  rgba(255, 255, 255, 0.2);
  --border-playback: rgba(255, 255, 255, 0.15);
  --border-section:  rgba(255, 255, 255, 0.1);
  --border-clear:    rgba(255, 69, 58, 0.3);

  --accent-blue:     #0a84ff;
  --accent-green:    #30d158;
  --accent-red:      #ff453a;
  --accent-yellow:   #ffd60a;
  --accent-orange:   #ff9500;
  --accent-cyan:     #64d2ff;
  --accent-purple:   #bf5af2;
  --accent-gray:     #8e8e93;

  --shadow-panel:    0 8px 32px rgba(0, 0, 0, 0.6);
  --shadow-thumb:    0 2px 5px rgba(0, 0, 0, 0.4);

  --color-swatch-border: rgba(255, 255, 255, 0.3);

  --footer-color:    #555566;
  --footer-link:     #0a84ff;

  --btn-theme-bg:    rgba(255,255,255,0.08);
  --btn-theme-border:rgba(255,255,255,0.18);
  --btn-theme-color: #ffffff;
}

[data-theme="light"] {
  --bg-body:         #f0f2f5;
  --bg-map:          #e8eaf0;
  --bg-panel:        rgba(255, 255, 255, 0.96);
  --bg-config:       rgba(0, 0, 0, 0.03);
  --bg-config-border:rgba(0, 0, 0, 0.07);
  --bg-stat-card:    rgba(0, 0, 0, 0.04);
  --bg-metrics:      rgba(0, 0, 0, 0.03);
  --bg-track-detail: rgba(0, 0, 0, 0.02);
  --bg-track-border: rgba(0, 0, 0, 0.09);
  --bg-select:       #f5f5f7;
  --bg-filter-hover: rgba(0, 0, 0, 0.04);
  --bg-filter-count: rgba(0, 0, 0, 0.07);
  --bg-slider-track: #d1d1d6;
  --bg-progress-bar: #d1d1d6;
  --bg-popup:        #ffffff;
  --bg-leaflet-ctrl: rgba(255, 255, 255, 0.95);
  --bg-playback:     rgba(0, 0, 0, 0.03);
  --bg-playback-btn: rgba(0, 0, 0, 0.07);

  --text-primary:    #1c1c1e;
  --text-secondary:  #3a3a3c;
  --text-muted:      #6e6e73;
  --text-label:      #6e6e73;
  --text-filter:     #1c1c1e;
  --text-popup:      #1c1c1e;
  --text-select:     #1c1c1e;

  --border-panel:    rgba(0, 0, 0, 0.10);
  --border-select:   rgba(0, 0, 0, 0.18);
  --border-popup:    rgba(0, 0, 0, 0.12);
  --border-leaflet:  rgba(0, 0, 0, 0.15);
  --border-playback: rgba(0, 0, 0, 0.12);
  --border-section:  rgba(0, 0, 0, 0.10);
  --border-clear:    rgba(255, 69, 58, 0.35);

  --shadow-panel:    0 8px 32px rgba(0, 0, 0, 0.12);
  --shadow-thumb:    0 2px 5px rgba(0, 0, 0, 0.2);

  --color-swatch-border: rgba(0, 0, 0, 0.25);

  --footer-color:    #aaaaaa;
  --footer-link:     #0a84ff;

  --btn-theme-bg:    rgba(0,0,0,0.06);
  --btn-theme-border:rgba(0,0,0,0.14);
  --btn-theme-color: #1c1c1e;
}

/* ==========================================================================
   Global Reset
   ========================================================================== */
* {
  box-sizing: border-box;
}

body, html {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-body);
  color: var(--text-primary);
  overflow: hidden;
  transition: background 0.3s ease, color 0.3s ease;
}

#map {
  height: 100vh;
  width: 100vw;
  background: var(--bg-map);
}

/* ==========================================================================
   Main Side Panel
   ========================================================================== */
#panel {
  position: absolute;
  top: 15px;
  left: 15px;
  z-index: 1000;
  background: var(--bg-panel);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: var(--text-primary);
  padding: 16px;
  border-radius: 12px;
  width: 350px;
  max-height: calc(100vh - 30px);
  overflow-y: auto;
  overflow-x: hidden;
  box-shadow: var(--shadow-panel);
  border: 1px solid var(--border-panel);
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: background 0.3s ease, box-shadow 0.3s ease, border-color 0.3s ease;
}

/* Panel scrollbar */
#panel::-webkit-scrollbar { width: 4px; }
#panel::-webkit-scrollbar-track { background: transparent; }
#panel::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 2px; }

/* --- Panel Header --- */
.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.panel-title-group {
  flex: 1;
  min-width: 0;
}

h3 {
  margin: 0;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.3px;
  color: var(--text-primary);
}

p.subtitle {
  margin: 2px 0 0 0;
  font-size: 11px;
  color: var(--text-muted);
}

/* --- Theme Toggle Button --- */
.btn-theme {
  flex-shrink: 0;
  background: var(--btn-theme-bg);
  border: 1px solid var(--btn-theme-border);
  color: var(--btn-theme-color);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.2s ease;
  padding: 0;
  line-height: 1;
}

.btn-theme:hover {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  transform: rotate(20deg) scale(1.05);
}

/* Show/hide icons based on theme */
[data-theme="dark"]  .theme-icon-dark  { display: inline; }
[data-theme="dark"]  .theme-icon-light { display: none;   }
[data-theme="light"] .theme-icon-dark  { display: none;   }
[data-theme="light"] .theme-icon-light { display: inline; }

/* ==========================================================================
   Drag & Drop Zone
   ========================================================================== */
#drop-zone {
  border: 2px dashed var(--accent-blue);
  border-radius: 8px;
  padding: 12px 10px;
  text-align: center;
  background: rgba(10, 132, 255, 0.05);
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

#drop-zone:hover, #drop-zone.drag-over {
  background: rgba(10, 132, 255, 0.15);
  border-color: var(--accent-green);
}

#drop-zone p {
  margin: 0;
  font-size: 12px;
  color: var(--text-secondary);
  pointer-events: none;
}

#drop-zone span {
  font-size: 10px;
  color: var(--accent-blue);
  display: block;
  margin-top: 4px;
  pointer-events: none;
}

/* ==========================================================================
   Config Blocks and Options
   ========================================================================== */
.config-box {
  background: var(--bg-config);
  border: 1px solid var(--bg-config-border);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.options-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
}

.color-options-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
}

select.style-select {
  background: var(--bg-select);
  color: var(--text-select);
  border: 1px solid var(--border-select);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 11px;
  outline: none;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}

input[type="color"] {
  -webkit-appearance: none;
  border: none;
  width: 26px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: 1px solid var(--color-swatch-border); border-radius: 4px; }
.color-picker-hidden { display: none; }

.buttons-row {
  gap: 8px;
  margin-top: 2px;
}

.btn-action {
  flex: 1;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.2s ease;
}

.btn-share {
  background: var(--accent-green);
  color: #ffffff;
}

.btn-share:hover {
  background: #25b347;
}

.btn-clear {
  background: var(--bg-select);
  color: var(--accent-red);
  border: 1px solid var(--border-clear);
}

.btn-clear:hover {
  background: rgba(255, 69, 58, 0.2);
}

/* ==========================================================================
   Progress Bar and Processing Stats
   ========================================================================== */
#progressBox {
  display: none;
}

.progress-box-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.stat-card {
  background: var(--bg-stat-card);
  padding: 6px 8px;
  border-radius: 6px;
}

.stat-card .label {
  font-size: 9px;
  color: var(--text-label);
  text-transform: uppercase;
}

.stat-card .value {
  font-size: 13px;
  font-weight: bold;
  margin-top: 2px;
  font-family: monospace;
}

.stat-card.success .value { color: var(--accent-green); }
.stat-card.error   .value { color: var(--accent-red);   }
.stat-card.total   .value { color: var(--accent-blue);  }
.stat-card.progress .value{ color: var(--accent-yellow);}

.progress-bar-bg {
  width: 100%;
  height: 5px;
  background: var(--bg-progress-bar);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent-green);
  transition: width 0.1s linear;
}

#status-msg {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  font-family: monospace;
}

/* ==========================================================================
   Filter Sections
   ========================================================================== */
#filter-section {
  margin-top: 15px;
  width: 100%;
}

.filter-section {
  padding-top: 10px;
  border-top: 1px solid var(--border-section);
  display: none;
}

.filter-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-label);
  text-transform: uppercase;
  margin-bottom: 8px;
}

/* Activity Filter List */
#filter-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
}

.filter-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.filter-item:hover {
  background: var(--bg-filter-hover);
}

.filter-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-filter);
  letter-spacing: 0.3px;
}

.filter-left input[type="checkbox"] {
  cursor: pointer;
  accent-color: var(--accent-blue);
  margin: 0;
}

.color-badge {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.filter-count {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text-muted);
  font-family: monospace;
  background: var(--bg-filter-count);
  padding: 2px 6px;
  border-radius: 10px;
}

/* Date Range Slider */
.date-range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-blue);
  margin-bottom: 8px;
}

.range-slider-container {
  position: relative;
  width: 100%;
  height: 20px;
  display: flex;
  align-items: center;
}

.slider-track {
  position: absolute;
  height: 6px;
  border-radius: 3px;
  background: var(--bg-slider-track);
  width: 100%;
  z-index: 1;
}

.range-slider-container input[type="range"] {
  position: absolute;
  width: 100%;
  height: 6px;
  background: none;
  pointer-events: none;
  -webkit-appearance: none;
  appearance: none;
  z-index: 2;
  margin: 0;
  outline: none;
}

.range-slider-container input[type="range"]::-webkit-slider-thumb {
  height: 18px;
  width: 18px;
  border-radius: 50%;
  background: var(--accent-blue);
  cursor: pointer;
  pointer-events: auto;
  -webkit-appearance: none;
  border: 2px solid var(--text-primary);
  box-shadow: var(--shadow-thumb);
  transition: transform 0.1s ease;
}

.range-slider-container input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

.range-slider-container input[type="range"]::-moz-range-thumb {
  height: 18px;
  width: 18px;
  border-radius: 50%;
  background: var(--accent-blue);
  cursor: pointer;
  pointer-events: auto;
  border: 2px solid var(--text-primary);
  box-shadow: var(--shadow-thumb);
  transition: transform 0.1s ease;
}

.range-slider-container input[type="range"]::-moz-range-thumb:hover {
  transform: scale(1.15);
}

/* ==========================================================================
   Selected Track Detail
   ========================================================================== */
#track-details-section {
  padding: 10px;
  background: var(--bg-track-detail);
  border-radius: 8px;
  border: 1px solid var(--bg-track-border);
  width: 100%;
  box-sizing: border-box;
}

.track-details-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.btn-close-details {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
}

.btn-close-details:hover {
  color: var(--accent-red);
}

.track-metrics-compact {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px 10px;
  padding: 8px 10px;
  background: var(--bg-metrics);
  border-radius: 6px;
  width: 100%;
}

.metric-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
}

.metric-item .m-label {
  font-size: 9px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  line-height: 1;
}

.metric-item .m-val {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ==========================================================================
   Playback Controls
   ========================================================================== */
.playback-container {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding: 6px 8px;
  background: var(--bg-playback);
  border-radius: 6px;
  border: 1px solid var(--border-playback);
  width: 100%;
}

.playback-btn-group {
  display: flex;
  gap: 4px;
}

.btn-playback {
  background: var(--bg-playback-btn);
  border: 1px solid var(--border-playback);
  color: var(--text-primary);
  border-radius: 4px;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-playback:hover {
  background: var(--accent-blue);
  border-color: var(--accent-blue);
  color: #ffffff;
}

.playback-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--border-section);
  outline: none;
  cursor: pointer;
}

.playback-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent-blue);
  cursor: pointer;
  transition: transform 0.1s ease;
}

.playback-slider::-webkit-slider-thumb:hover {
  transform: scale(1.3);
}

.playback-speed-select {
  background: var(--bg-playback-btn);
  color: var(--text-primary);
  border: 1px solid var(--border-playback);
  border-radius: 4px;
  font-size: 10px;
  padding: 3px 4px;
  cursor: pointer;
  outline: none;
}

.playback-speed-select option {
  background: var(--bg-popup);
  color: var(--text-popup);
}

/* Pulsing Marker on Map */
.simulation-marker {
  width: 14px;
  height: 14px;
  background: var(--accent-orange);
  border: 2px solid #ffffff;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255, 149, 0, 0.8);
  animation: pulse-sim 1.5s infinite;
}

@keyframes pulse-sim {
  0%   { box-shadow: 0 0 0 0   rgba(255, 149, 0, 0.7); }
  70%  { box-shadow: 0 0 0 10px rgba(255, 149, 0, 0);   }
  100% { box-shadow: 0 0 0 0   rgba(255, 149, 0, 0);   }
}

/* ==========================================================================
   Integrated Elevation Profile
   ========================================================================== */
#elevation-panel {
  display: flex !important;
  flex-direction: column !important;
  width: 100% !important;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border-section);
  box-sizing: border-box !important;
}

.elevation-header {
  display: flex !important;
  flex-direction: row !important;
  justify-content: space-between !important;
  align-items: center !important;
  width: 100% !important;
  margin-bottom: 8px !important;
}

.elevation-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-label);
  text-transform: uppercase;
  margin: 0 !important;
}

.elevation-info-text {
  font-size: 11px;
  font-weight: 500;
  color: var(--accent-blue);
  background: rgba(10, 132, 255, 0.15);
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}

.chart-wrapper {
  position: relative !important;
  width: 100% !important;
  height: 130px !important;
  display: block !important;
  clear: both !important;
}

canvas#elevationChart {
  width: 100% !important;
  height: 100% !important;
  display: block !important;
}

/* ==========================================================================
   Leaflet Popups and Controls
   ========================================================================== */
.leaflet-popup-content-wrapper {
  background: var(--bg-popup) !important;
  color: var(--text-popup) !important;
  border: 1px solid var(--border-popup);
  border-radius: 8px !important;
  font-size: 12px !important;
}

.leaflet-popup-tip {
  background: var(--bg-popup) !important;
}

.popup-title {
  font-weight: bold;
  color: var(--accent-blue);
  margin-bottom: 4px;
  word-break: break-all;
}

.popup-info {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.leaflet-top.leaflet-right {
  top: 10px;
  right: 10px;
}

.leaflet-control-layers {
  background: var(--bg-leaflet-ctrl) !important;
  color: var(--text-primary) !important;
  border: 1px solid var(--border-leaflet) !important;
  border-radius: 8px !important;
}

.leaflet-control-layers-expanded {
  padding: 10px !important;
}

/* ==========================================================================
   Panel Footer
   ========================================================================== */
.panel-footer {
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--border-section);
  text-align: center;
  font-size: 10px;
  color: var(--footer-color);
  letter-spacing: 0.3px;
}

.panel-footer a {
  color: var(--footer-link);
  text-decoration: none;
  font-weight: 600;
  transition: opacity 0.2s ease;
}

.panel-footer a:hover {
  opacity: 0.75;
  text-decoration: underline;
}