:root {
  --bg: #0f1419;
  --panel: #1a2129;
  --panel-2: #232d38;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #3b82f6;
  --border: #2c3742;
  --danger: #e6194b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Plus Jakarta Sans", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ---- Sidebar ---- */
#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 16px;
  gap: 18px;
}

.brand h1 { margin: 0; font-size: 22px; letter-spacing: -0.5px; }
.brand .tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin: 0 0 8px; }

.add-friend .row { display: flex; gap: 8px; margin-bottom: 8px; }
.add-friend input {
  flex: 1; min-width: 0;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 14px;
}
#q-bib { max-width: 90px; }
.add-friend input:focus { outline: none; border-color: var(--accent); }

button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 14px;
  background: var(--accent);
  color: white;
  transition: filter 0.15s;
}
button:hover { filter: brightness(1.1); }
button:disabled { opacity: 0.5; cursor: progress; }

.results { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.results:empty { margin-top: 0; }
.results-head {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; color: var(--muted); padding: 2px 2px 0;
}
.results-clear {
  background: transparent; color: var(--muted); border: 1px solid var(--border);
  padding: 3px 9px; font-size: 12px; border-radius: 6px; font-weight: 600;
}
.results-clear:hover { color: var(--text); filter: none; border-color: var(--muted); }
.result-item {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}
.result-item .meta { color: var(--muted); font-size: 12px; }
.result-item button { padding: 5px 10px; font-size: 12px; }
.results .empty, .results .error { color: var(--muted); font-size: 13px; padding: 4px 2px; }
.results .error { color: var(--danger); }

/* ---- Friend list ---- */
.friend-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.friend-item {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--dot, #888);
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.friend-item:hover { background: #2a3540; border-color: var(--muted); }
.friend-item .top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.friend-item .fname { font-weight: 700; font-size: 14px; }
.friend-item .fbib { color: var(--muted); font-size: 12px; }
.friend-item .fstatus { font-size: 12.5px; margin-top: 4px; color: var(--text); }
.friend-item .fsub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.friend-item .remove {
  background: transparent; color: var(--muted); padding: 2px 6px; font-size: 16px; line-height: 1;
}
.friend-item .remove:hover { color: var(--danger); filter: none; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.badge.not_started { background: #3b4250; color: #cbd5e1; }
.badge.on_course { background: #14532d; color: #86efac; }
.badge.finished { background: #1e3a5f; color: #93c5fd; }
.badge.unknown { background: #4a3b1e; color: #fcd34d; }

.count { color: var(--muted); font-weight: 400; }
.hint { color: var(--muted); font-size: 13px; }

.disclaimer { margin-top: auto; }
.disclaimer p { font-size: 11.5px; color: var(--muted); line-height: 1.5; border-top: 1px solid var(--border); padding-top: 12px; }

/* ---- Map area ---- */
#map-area { position: relative; }
#topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 500;
  display: flex; justify-content: space-between; align-items: flex-start;
  flex-wrap: wrap; gap: 8px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.toggle { display: flex; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; box-shadow: 0 2px 12px rgba(0,0,0,0.4); }
.toggle button { background: transparent; color: var(--muted); border-radius: 0; font-size: 13px; padding: 8px 14px; }
.toggle button.active { background: var(--accent); color: white; }
.status {
  position: absolute; z-index: 500;
  top: calc(10px + env(safe-area-inset-top)); right: 14px;
  max-width: 52%;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; font-size: 12.5px; color: var(--muted);
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
}

.mapview { position: absolute; inset: 0; }
#leaflet-map { background: #aad3df; }

/* Floating "Riders" panel toggle — mobile only */
#panel-toggle {
  display: none;
  position: absolute;
  right: 14px;
  bottom: calc(14px + env(safe-area-inset-bottom));
  z-index: 600;
  border-radius: 999px;
  padding: 12px 20px;
  font-size: 15px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
}

/* Bottom-sheet grab handle (mobile only) — tap to close the panel */
#sheet-handle { display: none; }

/* Leaflet rider marker */
.rider-dot {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid white; box-shadow: 0 0 0 2px rgba(0,0,0,0.4);
}
.rider-label {
  background: rgba(15,20,25,0.85); color: #fff; padding: 1px 6px; border-radius: 6px;
  font-size: 11px; font-weight: 700; white-space: nowrap; border: 1px solid var(--border);
}
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel); color: var(--text); }
.leaflet-popup-content { font-size: 13px; }
table.splits {
  width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 12px;
}
table.splits th {
  text-align: left; color: var(--muted); font-weight: 600;
  border-bottom: 1px solid var(--border); padding: 2px 6px 4px 0;
}
table.splits th:last-child, table.splits td:last-child,
table.splits th:nth-child(2), table.splits td:nth-child(2) { text-align: right; }
table.splits td { padding: 3px 6px 3px 0; border-bottom: 1px solid rgba(255,255,255,0.05); }
table.splits tr.avg td { font-weight: 700; color: #86efac; border-bottom: none; padding-top: 5px; }
table.splits .dwell { color: #fcd34d; font-weight: 600; white-space: nowrap; }
.cp-label { color: #0b3d2e; font-size: 11px; font-weight: 700; text-shadow: 0 0 3px #fff; }

@media (max-width: 720px) {
  /* Full-screen map; the sidebar becomes a slide-up bottom sheet. */
  #app { display: block; }

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

  #panel-toggle { display: block; }

  #sidebar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 700;
    height: 82dvh;
    max-height: 82dvh;
    border-right: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.5);
    transform: translateY(100%);
    transition: transform 0.28s ease;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
  body.panel-open #sidebar { transform: translateY(0); }
  /* floating button only opens the sheet; closing is via the handle */
  body.panel-open #panel-toggle { display: none; }

  #sheet-handle {
    display: block;
    position: sticky;
    top: 0;
    z-index: 2;
    width: calc(100% + 32px);
    margin: -16px -16px 6px;
    padding: 11px 0 13px;
    border: none;
    border-radius: 16px 16px 0 0;
    background: var(--panel);
    cursor: pointer;
  }
  #sheet-handle::before {
    content: "";
    display: block;
    width: 44px; height: 5px;
    margin: 0 auto;
    border-radius: 999px;
    background: var(--muted);
  }
  #sheet-handle:active::before { background: var(--text); }

  .disclaimer { margin-top: 16px; }

  /* Move the status line to the bottom-left, clear of the top controls and the
     bottom-right Riders button. */
  .status {
    top: auto;
    right: auto;
    left: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    max-width: 60%;
    font-size: 12px;
  }
}
