:root {
  --bg: #0b0b0f;
  --panel: #14141c;
  --line: rgba(255, 255, 255, 0.08);
  --text: #f5f5f7;
  --dim: rgba(245, 245, 247, 0.6);
  --accent: #ff8a00;
  --green: #7de558;
  --red: #e83c37;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* iOS Safari: accounts for the dynamic address bar */
}

#hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.5px;
  font-size: 18px;
}
.brand span {
  color: var(--accent);
  font-weight: 800;
}

#status {
  font-size: 13px;
  color: var(--dim);
}
#status.live::before {
  content: "● ";
  color: var(--green);
}
#status.replay::before {
  content: "■ ";
  color: var(--accent);
}

main {
  display: flex;
  flex: 1 1 auto;
  min-height: 0; /* lets children compute height inside the flex column */
}

#map {
  flex: 1 1 0;
  min-height: 0;
  background: #1a1a22;
  position: relative; /* anchor Leaflet's absolutely-positioned children */
}

#side {
  flex: 0 0 320px;
  border-left: 1px solid var(--line);
  padding: 14px;
  overflow-y: auto;
  background: var(--bg);
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.driver {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #1a1a1a;
  background-size: cover;
  background-position: center;
}

.driver-meta { flex: 1; min-width: 0; }
.driver-meta .name {
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.driver-meta .sub {
  font-size: 13px;
  color: var(--dim);
}

.state-label {
  font-size: 11px;
  letter-spacing: 1.2px;
  color: var(--dim);
  margin-bottom: 6px;
}
.state-value {
  font-size: 18px;
  font-weight: 600;
}

/* Whole SESSION card tints to match the live diff sign.
   Card border + label color shift; badge inside keeps its own pill colors. */
#state-card.ahead {
  border-color: rgba(125, 229, 88, 0.55);
  background: rgba(125, 229, 88, 0.08);
}
#state-card.ahead .state-label {
  color: rgba(125, 229, 88, 1);
}
#state-card.behind {
  border-color: rgba(232, 60, 55, 0.55);
  background: rgba(232, 60, 55, 0.08);
}
#state-card.behind .state-label {
  color: rgba(232, 60, 55, 1);
}

.state-diff {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 7px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.12);
}
.state-diff.state-diff-ahead {
  background: rgba(125, 229, 88, 0.95);
  color: #0b1d05;
  border-color: rgba(125, 229, 88, 0.5);
}
.state-diff.state-diff-behind {
  background: rgba(232, 60, 55, 0.95);
  color: #fff;
  border-color: rgba(232, 60, 55, 0.5);
}

#laps {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 260px;
  overflow-y: auto;
}
#laps li {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
#laps li:last-child { border-bottom: 0; }
#laps .num { color: var(--dim); }
#laps .t   { font-weight: 600; }
#laps .invalid .t { color: var(--red); text-decoration: line-through; }
#laps .empty { color: var(--dim); justify-content: center; }

#others {
  list-style: none;
  padding: 0;
  margin: 0;
}
#others li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 13px;
}
#others li .dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--accent);
}
#others li.empty { color: var(--dim); }

/* Marker = top-down car PNG that rotates to match heading, with a speed
   badge underneath. Source PNGs are 256x256 top-down with the nose pointing
   up, so heading maps to CSS rotate() with no offset. */
.driver-pin-wrap {
  position: relative;
  pointer-events: none;
}
.driver-car {
  width: 44px;
  height: 44px;
  display: block;
  transform-origin: 50% 50%;
  transition: transform 200ms linear;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.55));
}
.driver-speed {
  position: absolute;
  top: 48px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.78);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 6px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.2px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}
.driver-diff {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 6px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.2px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  display: none;
}
.driver-diff.ahead {
  display: block;
  background: rgba(125, 229, 88, 0.95); /* var(--green) */
  color: #0b1d05;
}
.driver-diff.behind {
  display: block;
  background: rgba(232, 60, 55, 0.95);  /* var(--red) */
  color: #fff;
}

.car {
  display: flex;
  gap: 12px;
  align-items: center;
}
.car.hidden { display: none; }
.car-photo {
  width: 64px;
  height: 64px;
  flex: 0 0 64px;
  border-radius: 10px;
  background: #1a1a22;
  background-size: cover;
  background-position: center;
  border: 1px solid var(--line);
}
.car-photo.empty::before {
  content: "🏎";
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 28px;
  opacity: 0.4;
}
.car-meta {
  flex: 1;
  min-width: 0;
}
.car-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.car-stats {
  font-size: 12px;
  color: var(--dim);
  margin-top: 4px;
}
.car-stats .sep {
  margin: 0 6px;
  opacity: 0.4;
}

.banner {
  border-color: rgba(255, 138, 0, 0.4);
  background: rgba(255, 138, 0, 0.08);
}
.banner.hidden { display: none; }
.banner-title {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 4px;
}
.banner-sub {
  font-size: 13px;
  color: var(--dim);
}

@media (max-width: 720px) {
  main {
    flex-direction: column;
  }
  #map {
    flex: 1 1 0;
    min-height: 240px; /* never collapse on mobile */
  }
  #side {
    flex: 0 0 auto;
    border-left: none;
    border-top: 1px solid var(--line);
    max-height: 45vh;
    max-height: 45dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}
