*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0f172a;
  --bg-elev: #111c30;
  --card: #1e293b;
  --card-hover: #263548;
  --accent: #3b82f6;
  --accent-dark: #2563eb;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --border: #334155;
  --green: #22c55e;
  --red: #ef4444;
  --orange: #f59e0b;
  --header-h: 56px;
}
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); line-height: 1.45; }

/* Header */
.app-header {
  height: var(--header-h);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex; align-items: center; justify-content: space-between;
}
.app-header h1 { font-size: 1.05rem; font-weight: 700; color: var(--accent); }
.header-right { display: flex; gap: 10px; align-items: center; }

/* Buttons / chips */
.btn { border: none; border-radius: 8px; padding: 8px 14px; font-size: 0.85rem; font-weight: 600; cursor: pointer; transition: background .15s, color .15s; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-dark); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); background: var(--card-hover); }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
.chip-dim { background: var(--card-hover); color: var(--text-muted); }
.chip-ok { background: rgba(34, 197, 94, .15); color: var(--green); }
.chip-err { background: rgba(239, 68, 68, .15); color: var(--red); }
.chip-warn { background: rgba(245, 158, 11, .15); color: var(--orange); }

/* Layout */
.layout { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - var(--header-h)); }
.sidebar { background: var(--bg-elev); border-right: 1px solid var(--border); overflow-y: auto; padding: 16px; }
.sidebar-title { font-size: 0.75rem; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); margin-bottom: 12px; }
.map-wrap { position: relative; }
#map { position: absolute; inset: 0; background: #0b1220; }

/* Vehicle cards */
.vehicle-list { display: flex; flex-direction: column; gap: 10px; }
.vehicle-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.vehicle-card:hover { background: var(--card-hover); border-color: #475569; }
.vehicle-card.active { border-color: var(--accent); }
.vehicle-card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.vehicle-name { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.vehicle-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.vehicle-van { font-size: 0.75rem; color: var(--text-dim); }
.vehicle-meta { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 0.8rem; color: var(--text-muted); }
.vehicle-meta dt { color: var(--text-dim); }
.vehicle-meta dd { color: var(--text); }
.vehicle-addr { margin-top: 8px; font-size: 0.78rem; color: var(--text-muted); line-height: 1.35; }
.vehicle-stale { color: var(--orange); }
.vehicle-offline { color: var(--red); font-style: italic; font-size: 0.8rem; }

/* Trip panel */
.panel {
  position: fixed; right: 16px; top: calc(var(--header-h) + 16px); width: 380px; max-height: calc(100vh - var(--header-h) - 32px);
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0,0,0,.5);
  display: flex; flex-direction: column; overflow: hidden;
  z-index: 1000;
}
.panel-hidden { display: none; }
.panel-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.panel-header h3 { font-size: 1rem; }
.panel-body { padding: 14px 16px; overflow-y: auto; }

.trip-item { padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
.trip-item:last-child { border-bottom: none; }
.trip-row { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 2px; }
.trip-time { color: var(--text-muted); font-size: 0.75rem; }
.trip-dist { color: var(--accent); font-weight: 600; }
.trip-addr { color: var(--text); font-size: 0.8rem; }
.trip-addr small { color: var(--text-dim); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 2000; }
.modal-hidden { display: none; }
.modal-card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; width: 480px; max-width: calc(100vw - 32px); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--border); gap: 16px; }
.toggle-row:last-of-type { border-bottom: none; }
.toggle-row small { color: var(--text-muted); font-size: 0.75rem; }
.toggle-row input[type=checkbox] { width: 40px; height: 22px; appearance: none; background: var(--border); border-radius: 999px; position: relative; cursor: pointer; transition: background .15s; }
.toggle-row input[type=checkbox]::after { content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; background: #fff; border-radius: 50%; transition: transform .15s; }
.toggle-row input[type=checkbox]:checked { background: var(--accent); }
.toggle-row input[type=checkbox]:checked::after { transform: translateX(18px); }
.settings-status { font-size: 0.8rem; color: var(--text-muted); padding-top: 10px; min-height: 20px; }
.settings-status.ok { color: var(--green); }
.settings-status.err { color: var(--red); }

/* Misc */
.loading, .error { text-align: center; padding: 24px; color: var(--text-muted); font-size: 0.85rem; }
.error { color: var(--red); }

/* Leaflet overrides for dark map */
.leaflet-container { background: #0b1220; }
.leaflet-tile-pane { filter: brightness(0.7) contrast(1.1) hue-rotate(180deg) invert(1); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.leaflet-popup-content { margin: 10px 14px; font-size: 0.85rem; }
.leaflet-control-attribution { background: rgba(15,23,42,.8) !important; color: var(--text-dim) !important; }
.leaflet-control-attribution a { color: var(--text-muted) !important; }

/* Custom vehicle marker */
.vehicle-marker {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 0.75rem;
  border: 2px solid #fff; box-shadow: 0 2px 8px rgba(0,0,0,.5);
}

@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { max-height: 40vh; border-right: none; border-bottom: 1px solid var(--border); }
  .panel { left: 8px; right: 8px; width: auto; }
}

/* Vehicle Details Modal */
#vehicle-details-modal .modal-card {
  width: 800px;
  max-height: 85vh;
  overflow-y: auto;
}

.vehicle-details-card {
  width: 800px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
}

.vehicle-details-grid {
  display: grid;
  gap: 20px;
}

.detail-section {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
}

.detail-section h4 {
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 12px;
  font-weight: 600;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.metric-item {
  text-align: center;
  padding: 12px;
}

.metric-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  display: block;
  margin-bottom: 4px;
}

.metric-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.metric-unit {
  font-size: 0.85rem;
  color: var(--text-dim);
  font-weight: 400;
}

/* Status indicators */
.status-excellent { color: var(--green); }
.status-good { color: #22d3ee; }
.status-warning { color: var(--orange); }
.status-danger { color: var(--red); }

/* Fuel level specific styling */
.fuel-indicator {
  position: relative;
  height: 80px;
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 8px;
}

.fuel-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  transition: height 0.3s ease;
  border-radius: 0 0 6px 6px;
}

.fuel-level-high { background: var(--green); opacity: 0.8; }
.fuel-level-medium { background: var(--orange); opacity: 0.8; }
.fuel-level-low { background: var(--red); opacity: 0.8; }

.fuel-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* Performance meters */
.performance-meter {
  position: relative;
  height: 8px;
  background: var(--card);
  border-radius: 4px;
  margin-bottom: 8px;
  overflow: hidden;
}

.performance-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
}

/* Driver score styling */
.driver-score-circle {
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 auto 8px;
}

.score-background {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 6px solid var(--border);
  position: relative;
  overflow: hidden;
}

.score-fill {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  transition: height 0.3s ease;
  border-radius: 0 0 50px 50px;
}

.score-fill.status-excellent { background: var(--green); opacity: 0.8; }
.score-fill.status-good { background: #22d3ee; opacity: 0.8; }
.score-fill.status-warning { background: var(--orange); opacity: 0.8; }
.score-fill.status-danger { background: var(--red); opacity: 0.8; }
.score-fill.fuel-level-high { background: var(--green); opacity: 0.8; }
.score-fill.fuel-level-medium { background: var(--orange); opacity: 0.8; }
.score-fill.fuel-level-low { background: var(--red); opacity: 0.8; }

.score-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
}

/* Recent trips list */
.recent-trips {
  max-height: 300px;
  overflow-y: auto;
}

.trip-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.trip-summary:last-child {
  border-bottom: none;
}

.trip-time {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.trip-distance {
  color: var(--accent);
  font-weight: 600;
}

/* Harsh events */
.harsh-events-list {
  max-height: 200px;
  overflow-y: auto;
}

.harsh-event {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}

.harsh-event:last-child {
  border-bottom: none;
}

.event-type {
  font-weight: 600;
  text-transform: capitalize;
}

.event-force {
  color: var(--red);
  font-weight: 600;
}

.event-type-turning { color: var(--orange); }
.event-type-acceleration { color: var(--red); }
.event-type-deceleration { color: var(--red); }

/* No data states */
.no-data {
  text-align: center;
  padding: 20px;
  color: var(--text-muted);
  font-style: italic;
}

/* Responsive adjustments for vehicle details */
@media (max-width: 900px) {
  .vehicle-details-card {
    width: 95vw;
  }
  
  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 600px) {
  .metric-grid {
    grid-template-columns: 1fr;
  }
}
