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

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #273548;
  --border: #334155;
  --text: #f1f5f9;
  --text2: #94a3b8;
  --accent: #3b82f6;
  --green: #22c55e;
  --red: #ef4444;
  --amber: #f59e0b;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  overflow: hidden;
}

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
}

/* Leaflet overrides */
.leaflet-control-attribution { font-size: 10px; }
.user-dot {
  width: 16px; height: 16px;
  background: var(--accent);
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(59,130,246,0.8);
}
.replay-dot {
  width: 20px; height: 20px;
  background: var(--amber);
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(245,158,11,0.8);
}
.pin-dot {
  width: 14px; height: 14px;
  background: var(--green);
  border: 2px solid #fff;
  border-radius: 50%;
}

/* Top bar */
#top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: linear-gradient(to bottom, rgba(15,23,42,0.95) 70%, transparent);
  backdrop-filter: none;
}

#app-title {
  flex: 1;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

#rec-indicator {
  display: none;
  align-items: center;
  gap: 6px;
  color: var(--red);
  font-size: 12px;
  font-weight: 600;
}
#rec-indicator.active { display: flex; }

#wakelock-indicator {
  display: none;
  font-size: 16px;
  opacity: 0.85;
}
#wakelock-indicator.active { display: block; }
#rec-dot {
  width: 8px; height: 8px;
  background: var(--red);
  border-radius: 50%;
  animation: blink 1s infinite;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.3} }

/* Icon buttons */
.icon-btn {
  width: 40px; height: 40px;
  border: none;
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  touch-action: manipulation;
}
.icon-btn:active { opacity: 0.7; }

/* FAB */
#fab {
  position: fixed;
  bottom: calc(280px + 16px + var(--safe-bottom));
  right: 16px;
  z-index: 200;
  width: 56px; height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  transition: background 0.2s, bottom 0.35s ease;
  touch-action: manipulation;
}
#fab.tracking { background: var(--red); }
#fab.sheet-collapsed { bottom: calc(80px + 16px + var(--safe-bottom)); }

/* Live stats bar (shown during tracking) */
#live-stats {
  position: fixed;
  left: 16px; right: 76px;
  bottom: calc(280px + 88px + var(--safe-bottom));
  z-index: 150;
  background: rgba(15,23,42,0.9);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: none;
  gap: 16px;
  transition: bottom 0.35s ease;
}
#live-stats.active { display: flex; flex-wrap: wrap; }
#live-stats.sheet-collapsed { bottom: calc(80px + 88px + var(--safe-bottom)); }

.replay-controls-hidden { display: none; }

.live-stat { display: flex; flex-direction: column; gap: 2px; }
.live-stat-label { font-size: 10px; color: var(--text2); text-transform: uppercase; letter-spacing: 0.5px; }
.live-stat-val { font-size: 14px; font-weight: 600; }

/* Bottom sheet */
#bottom-sheet {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 100;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  display: flex;
  flex-direction: column;
  height: 75vh;
  transform: translateY(calc(100% - 280px));
  touch-action: none;
}
#bottom-sheet.collapsed {
  transform: translateY(calc(100% - 80px));
}
#bottom-sheet.expanded {
  transform: translateY(0);
}

#sheet-handle-area {
  padding: 12px 0 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  flex-shrink: 0;
}
#sheet-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 2px;
}

#sheet-tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px;
  flex-shrink: 0;
}
.sheet-tab {
  flex: 1;
  padding: 8px;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  color: var(--text2);
  border-radius: 8px;
  cursor: pointer;
  border: none;
  background: transparent;
  transition: background 0.15s, color 0.15s;
  touch-action: manipulation;
}
.sheet-tab.active {
  background: var(--surface2);
  color: var(--text);
}

#sheet-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px;
  padding-bottom: calc(20px + var(--safe-bottom));
}

/* Routes list */
.route-item {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: background 0.15s;
}
.route-item:active { background: var(--border); }
.route-item.selected { border: 1px solid var(--accent); }

.route-name { font-weight: 600; font-size: 15px; }
.route-meta { font-size: 12px; color: var(--text2); display: flex; gap: 10px; flex-wrap: wrap; }
.route-actions {
  display: flex; gap: 8px; margin-top: 4px;
}

/* Buttons */
.btn {
  padding: 8px 14px;
  border-radius: 8px;
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
  transition: opacity 0.15s;
}
.btn:active { opacity: 0.7; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: var(--red); color: #fff; }
.btn-ghost { background: var(--surface); color: var(--text); }
.btn-sm { padding: 6px 10px; font-size: 12px; }

/* Stats panel */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}
.stat-card {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 12px;
}
.stat-card-label { font-size: 11px; color: var(--text2); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.4px; }
.stat-card-val { font-size: 18px; font-weight: 700; }
.stat-card-unit { font-size: 12px; color: var(--text2); }

/* Replay controls */
#replay-controls {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}
.replay-header { font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--text2); text-transform: uppercase; letter-spacing: 0.4px; }
.replay-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.replay-progress { flex: 1; height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; }
.replay-progress-fill { height: 100%; background: var(--amber); transition: width 0.1s; border-radius: 2px; }
.speed-btns { display: flex; gap: 6px; }
.speed-btn {
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text2);
  font-size: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.speed-btn.active { background: var(--amber); color: #000; border-color: var(--amber); }

/* Fuel section */
.fuel-section {
  margin-bottom: 14px;
}
.fuel-section h3 { font-size: 13px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: 0.4px; margin-bottom: 10px; }

.fuel-trip-item {
  background: var(--surface2);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 8px;
  font-size: 12px;
}
.fuel-trip-header { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.fuel-color-swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.fuel-trip-route { color: var(--text2); }
.fuel-trip-stats { display: flex; gap: 12px; color: var(--text2); }

/* Modal */
#modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0,0,0,0.7);
  display: none;
  align-items: flex-end;
  justify-content: center;
}
#modal-overlay.open { display: flex; }
#modal {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 24px 20px;
  padding-bottom: calc(24px + var(--safe-bottom));
}
#modal h2 { font-size: 18px; font-weight: 700; margin-bottom: 16px; }
.modal-close { position: absolute; top: 16px; right: 16px; font-size: 20px; }

/* Form elements */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12px; color: var(--text2); margin-bottom: 6px; }
.field input, .field textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

/* Toast */
#toast {
  position: fixed;
  bottom: calc(300px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 13px;
  z-index: 400;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
  pointer-events: none;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text2);
}
.empty-state-icon { font-size: 40px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; line-height: 1.5; }

/* Section title */
.section-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
}

/* Divider */
.divider { height: 1px; background: var(--border); margin: 14px 0; }
