/* Smart Bicycle live dashboard */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --band: rgba(137, 135, 129, 0.16);
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.05);

  /* One colour per bike, in this fixed order (slot 0: past eight bikes). */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --other: #898781;

  /* States: always shown with a shape and a label as well. */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --neutral: #898781;

  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --band: rgba(137, 135, 129, 0.2);
    --shadow: none;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --band: rgba(137, 135, 129, 0.2);
  --shadow: none;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

.slot-0 { --c: var(--other); }
.slot-1 { --c: var(--series-1); }
.slot-2 { --c: var(--series-2); }
.slot-3 { --c: var(--series-3); }
.slot-4 { --c: var(--series-4); }
.slot-5 { --c: var(--series-5); }
.slot-6 { --c: var(--series-6); }
.slot-7 { --c: var(--series-7); }
.slot-8 { --c: var(--series-8); }

/* ----- Page -------------------------------------------------------------- */

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--page);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.4;
}

button, input { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 16px; }
h2 { font-size: 14px; }

dl, dd { margin: 0; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.subtitle { color: var(--ink-2); font-weight: 400; }

.broker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  min-width: 0;
}

.broker-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--neutral);
  flex: none;
}
.broker[data-state="up"] .broker-dot { background: var(--good); }
.broker[data-state="down"] .broker-dot { background: var(--serious); border-radius: 2px; }
.broker[data-state="auth"] .broker-dot { background: var(--critical); border-radius: 2px; transform: rotate(45deg); }
.broker[data-state="connecting"] .broker-dot { background: transparent; border: 2px solid var(--neutral); }

.broker-info { color: var(--ink-2); }

.link-button {
  border: 0;
  padding: 2px 4px;
  background: none;
  color: var(--ink-2);
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}
.link-button:hover { color: var(--ink); }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  padding: 12px 16px 16px;
}

.side { display: flex; flex-direction: column; gap: 12px; min-height: 0; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 8px;
}

.panel-note { color: var(--ink-2); }

.empty { margin: 0; padding: 4px 12px 12px; color: var(--ink-2); }

.map-panel { overflow: hidden; }
#map { height: 60vh; min-height: 320px; }

/* Narrow windows and phones: one column, the map and chart before the events. */
@media (max-width: 999px) {
  .side { display: contents; }
  .bikes { order: 1; }
  .map-panel { order: 2; }
  .speed-panel { order: 3; }
  .events { order: 4; }
}

/* Desktop: everything fits the window, the side column scrolls. */
@media (min-width: 1000px) {
  body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
  .layout {
    flex: 1;
    min-height: 0;
    grid-template-columns: minmax(340px, 400px) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "side map" "side speed";
  }
  .side { grid-area: side; }
  .bikes { flex: 1 1 auto; min-height: 0; overflow: auto; }
  .events { flex: 0 1 30%; min-height: 120px; overflow: auto; }
  .map-panel { grid-area: map; }
  .speed-panel { grid-area: speed; }
  #map { height: 100%; min-height: 0; }
}

/* ----- Fleet summary ------------------------------------------------------ */

.fleet { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 10px; }
.fleet[hidden] { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: none;
  font-size: 13px;
  cursor: pointer;
}
.chip:hover { background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--surface-2); border-color: var(--ink-2); font-weight: 600; }
.chip-count { font-weight: 600; font-variant-numeric: tabular-nums; }
.chip[data-empty] { color: var(--muted); }
.chip[data-empty] .state-icon { opacity: 0.4; }
.chip[data-state="attention"]:not([data-empty]) { border-color: var(--critical); }
.chip[data-state="fall"]:not([data-empty]) {
  border-color: var(--critical);
  background: color-mix(in srgb, var(--critical) 16%, transparent);
  font-weight: 600;
}

/* ----- Fall banner -------------------------------------------------------- */

.fall-banner {
  display: grid;
  gap: 6px;
  margin: 12px 16px 0;
  padding: 10px 12px;
  border: 2px solid var(--critical);
  border-radius: 10px;
  background: color-mix(in srgb, var(--critical) 14%, var(--surface));
}
.fall-banner[hidden] { display: none; }
.fall-line { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-weight: 600; }
.fall-icon {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--critical);
  color: #fff;
  font-weight: 700;
  flex: none;
}
.fall-text { flex: 1; min-width: 12em; }
.fall-show {
  padding: 4px 10px;
  border: 1px solid var(--critical);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}
.fall-show:hover { background: var(--surface-2); }

.filtered { margin: 0; padding: 0 12px 8px; color: var(--ink-2); }

/* ----- Bike cards --------------------------------------------------------- */

.bike-list { list-style: none; margin: 0; padding: 0 8px 8px; display: grid; gap: 8px; }

.bike {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px 10px;
  cursor: pointer;
}
.bike.is-selected { border-color: var(--c); box-shadow: inset 3px 0 0 var(--c); }

.bike-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  width: calc(100% + 24px);
  margin: 0 -12px;
  padding: 10px 12px 6px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.swatch { width: 12px; height: 12px; border-radius: 50%; background: var(--c); flex: none; }

.bike-name { font-weight: 600; flex: 1; min-width: 0; overflow-wrap: anywhere; }

.state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
  font-size: 13px;
}

/* State shapes: circle riding, square parked, ring no fix, diamond not reporting,
   cross offline, triangle needs attention. Bike cards and fleet chips share them. */
.state-icon { width: 10px; height: 10px; background: var(--neutral); border-radius: 50%; flex: none; }
:is(.bike, .chip)[data-state="riding"] .state-icon { background: var(--good); }
:is(.bike, .chip)[data-state="parked"] .state-icon { background: var(--neutral); border-radius: 2px; }
:is(.bike, .chip)[data-state="nofix"] .state-icon { background: transparent; border: 2.5px solid var(--warning); }
:is(.bike, .chip)[data-state="waiting"] .state-icon { background: transparent; border: 2px dotted var(--neutral); }
:is(.bike, .chip)[data-state="stale"] .state-icon { background: var(--serious); border-radius: 1px; transform: rotate(45deg) scale(0.85); }
.chip[data-state="attention"] .state-icon { background: var(--critical); border-radius: 0; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.chip[data-state="fall"] .state-icon { background: var(--critical); box-shadow: 0 0 0 2px var(--surface), 0 0 0 3.5px var(--critical); }
:is(.bike, .chip)[data-state="offline"] .state-icon {
  background: linear-gradient(45deg, transparent 38%, var(--critical) 38% 62%, transparent 62%),
              linear-gradient(-45deg, transparent 38%, var(--critical) 38% 62%, transparent 62%);
  border-radius: 0;
}

/* Alerts: a bordered badge per problem, on their own line under the bike's name. */
.alerts { display: inline-flex; flex-wrap: wrap; gap: 4px; order: 1; flex-basis: 100%; padding-left: 20px; }
.alerts:empty { display: none; }
.alert {
  padding: 1px 7px;
  border: 1px solid var(--a);
  border-radius: 999px;
  background: color-mix(in srgb, var(--a) 16%, transparent);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.alert-warning { --a: var(--warning); }
.alert-serious { --a: var(--serious); }
.alert-critical { --a: var(--critical); }

/* Only the selected bike shows everything; the others shrink to one line. */
.bike-brief { display: none; margin: 0; color: var(--ink-2); font-size: 13px; font-variant-numeric: tabular-nums; }
.bike:not(.is-selected) .bike-brief { display: block; }
.bike:not(.is-selected) :is(.speeds, .note, .trip, .facts, .device) { display: none; }
.bike:not(.is-selected) { padding-bottom: 8px; }

.speeds { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 4px 16px; }

.speed-main { display: grid; grid-template-columns: auto auto; align-items: baseline; column-gap: 4px; }
.speed-value { font-size: 32px; font-weight: 600; line-height: 1.1; }
.speed-unit { color: var(--ink-2); }
.speed-caption { grid-column: 1 / -1; color: var(--ink-2); font-size: 12px; }

.speed-compare { display: flex; gap: 16px; padding-bottom: 2px; }
.speed-compare dt, .trip dt, .facts dt, .device dt { color: var(--ink-2); font-size: 12px; }
.speed-compare dd { font-weight: 600; }

.note {
  margin: 8px 0 0;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  border-left: 3px solid var(--neutral);
}
.bike[data-state="nofix"] .note { border-left-color: var(--warning); }
.bike[data-state="stale"] .note { border-left-color: var(--serious); }
.bike[data-state="offline"] .note { border-left-color: var(--critical); }

.trip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--grid);
}
.trip dd { font-weight: 600; }

.facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 12px;
  margin-top: 8px;
}
.facts dd { overflow-wrap: anywhere; }

.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 12px; margin-right: 6px; vertical-align: -1px; }
.bars i { width: 4px; background: var(--grid); border-radius: 1px; }
.bars i:nth-child(1) { height: 4px; }
.bars i:nth-child(2) { height: 6px; }
.bars i:nth-child(3) { height: 9px; }
.bars i:nth-child(4) { height: 12px; }
.bars[data-bars="1"] i:nth-child(-n+1),
.bars[data-bars="2"] i:nth-child(-n+2),
.bars[data-bars="3"] i:nth-child(-n+3),
.bars[data-bars="4"] i:nth-child(-n+4) { background: var(--ink-2); }

/* Nothing to show before the first telemetry. */
.bike[data-no-telemetry] :is(.speeds, .trip) { display: none; }

/* A bike that has gone quiet keeps its last values, dimmed. */
.bike[data-state="stale"] :is(.speeds, .trip, .facts),
.bike[data-state="offline"] :is(.speeds, .trip, .facts) { opacity: 0.55; }

.device { margin-top: 8px; }
.device summary { color: var(--ink-2); cursor: pointer; width: fit-content; }
.device dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 2px 12px; margin: 6px 0; }
.device dl div { display: contents; }
.device dd { overflow-wrap: anywhere; }

/* ----- Events ------------------------------------------------------------- */

.event-list { list-style: none; margin: 0; padding: 0 12px 10px; }
.event-list li { display: grid; grid-template-columns: auto auto minmax(0, 1fr); gap: 8px; align-items: baseline; padding: 3px 0; }
.event-time { color: var(--ink-2); font-variant-numeric: tabular-nums; font-size: 12px; }
.event-list .swatch { width: 8px; height: 8px; align-self: center; }
.event-list .swatch.broker-event { background: none; border: 1.5px solid var(--neutral); }

/* ----- Map ---------------------------------------------------------------- */

.leaflet-container { font: inherit; background: var(--surface-2); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaflet-tile-pane { filter: brightness(0.82) contrast(1.05); }
}
:root[data-theme="dark"] .leaflet-tile-pane { filter: brightness(0.82) contrast(1.05); }

.bike-icon { background: none; border: 0; }

.bike-marker svg { display: block; overflow: visible; }
.bike-marker .dot { fill: var(--c); stroke: #fff; stroke-width: 2.5; }
.bike-marker .heading { fill: var(--c); stroke: #fff; stroke-width: 1.5; stroke-linejoin: round; }
.bike-marker.is-selected .dot { stroke-width: 3.5; r: 8.5; }
.bike-marker .halo { fill: none; stroke: none; stroke-width: 3; }
.bike-marker[data-alert="warning"] .halo { stroke: var(--warning); }
.bike-marker[data-alert="serious"] .halo { stroke: var(--serious); }
.bike-marker[data-alert="critical"] .halo { stroke: var(--critical); animation: halo-pulse 1s ease-in-out infinite; }
@keyframes halo-pulse { 50% { stroke-width: 7; } }
@media (prefers-reduced-motion: reduce) {
  .bike-marker[data-alert="critical"] .halo { animation: none; }
}
.bike-marker[data-state="nofix"] .dot { fill: #fff; stroke: var(--c); stroke-width: 3.5; }
.bike-marker:is([data-state="stale"], [data-state="offline"]) { opacity: 0.6; }
.bike-marker:is([data-state="stale"], [data-state="offline"]) .dot { fill: var(--other); }

.leaflet-tooltip.bike-label {
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
  font-size: 12px;
  font-weight: 600;
}
.leaflet-tooltip.bike-label::before { display: none; }

.track { stroke: var(--c); }

.map-buttons { display: flex; flex-direction: column; background: var(--surface); }
.map-buttons button {
  border: 0;
  border-bottom: 1px solid var(--border);
  padding: 6px 10px;
  background: var(--surface);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  font-size: 13px;
}
.map-buttons button:last-child { border-bottom: 0; }
.map-buttons button:hover { background: var(--surface-2); }
.map-buttons button[aria-pressed="true"] { font-weight: 600; }
.map-buttons button[aria-pressed="true"]::before { content: "\2713\00a0"; }

/* ----- Speed chart and table ---------------------------------------------- */

.tabs { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; }
.tabs button { border: 0; border-radius: 6px; padding: 3px 10px; background: none; color: var(--ink-2); cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--surface-2); color: var(--ink); font-weight: 600; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; list-style: none; margin: 0; padding: 0 12px 4px; color: var(--ink-2); font-size: 12px; }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.key-line { width: 16px; height: 2px; border-radius: 1px; }
.key-computed { background: var(--c, var(--series-1)); }
.key-gnss { background: var(--muted); }
.key-band { width: 12px; height: 12px; border-radius: 2px; background: var(--band); border: 1px solid var(--grid); }

.chart { position: relative; padding: 0 4px 6px; min-height: 220px; }
.chart svg { display: block; width: 100%; height: 220px; overflow: visible; }
.chart svg:focus { outline: none; }
.chart svg:focus-visible { outline: 2px solid var(--series-1); outline-offset: -2px; border-radius: 6px; }
.chart .chart-empty { margin: 0; padding: 72px 16px; text-align: center; color: var(--ink-2); }

.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .band { fill: var(--band); }
.chart .band-label { fill: var(--ink-2); font-size: 11px; }
.chart .series { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .series-computed { stroke: var(--c); }
.chart .series-gnss { stroke: var(--muted); }
.chart .point { stroke: var(--surface); stroke-width: 2; }
.chart .point-computed { fill: var(--c); }
.chart .point-gnss { fill: var(--muted); }
.chart .end-label { fill: var(--ink); font-size: 12px; font-weight: 600; }
.chart .crosshair { stroke: var(--ink-2); stroke-width: 1; shape-rendering: crispEdges; }

.tooltip {
  position: absolute;
  top: 8px;
  z-index: 2;
  min-width: 170px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 4px 16px rgba(11, 11, 11, 0.12);
  pointer-events: none;
  font-size: 12px;
}
.tooltip-time { color: var(--ink-2); margin-bottom: 4px; }
.tooltip-row { display: flex; align-items: center; gap: 6px; }
.tooltip-row strong { min-width: 64px; font-size: 13px; }
.tooltip-row .key-line { flex: none; }
.tooltip-foot { margin-top: 4px; color: var(--ink-2); }

.table-scroll { max-height: 236px; overflow: auto; margin: 0 12px 12px; border: 1px solid var(--grid); border-radius: 6px; }
.samples { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 13px; }
.samples th, .samples td { padding: 4px 8px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--grid); }
.samples th:first-child, .samples td:first-child { text-align: left; }
.samples thead th { position: sticky; top: 0; background: var(--surface); color: var(--ink-2); font-weight: 600; vertical-align: bottom; }
.samples small { font-weight: 400; }

/* ----- Login -------------------------------------------------------------- */

.login {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
}
.login::backdrop { background: rgba(11, 11, 11, 0.45); }
.login form { display: grid; gap: 12px; }
.login label { display: grid; gap: 4px; color: var(--ink-2); }
.login input:not([type="checkbox"]) {
  padding: 7px 9px;
  border: 1px solid var(--axis);
  border-radius: 6px;
  background: var(--page);
  color: var(--ink);
}
.login .check { display: flex; align-items: center; gap: 8px; color: var(--ink); }
.login-message { margin: 0; padding: 8px 10px; border-left: 3px solid var(--critical); background: var(--surface-2); border-radius: 6px; }
.login-hint { margin: 0; color: var(--ink-2); font-size: 13px; }
.login code { font-size: 12px; }
.login-actions { display: flex; justify-content: flex-end; gap: 8px; }
.login-actions button { padding: 7px 14px; border-radius: 6px; cursor: pointer; }
.login .primary { border: 1px solid var(--series-1); background: var(--series-1); color: #fff; font-weight: 600; }
.login .secondary { border: 1px solid var(--axis); background: none; }
