:root {
  --bg: #ffffff;
  --panel: #f7f7f8;
  --line: #e4e4e7;
  --text: #18181b;
  --muted: #71717a;
  --accent: #2563eb;
  --moving: #16a34a;
  --idle: #d97706;
  --parked: #2563eb;
  --offline: #a1a1aa;
  --track: #7c3aed;
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; }
button { font: inherit; cursor: pointer; }

#sidebar {
  width: 352px; flex: none; display: flex; flex-direction: column;
  border-right: 1px solid var(--line); background: var(--bg); overflow: hidden;
}
#map { flex: 1; position: relative; } /* Leaflet sets this only if a narrow screen's sticky isn't there at load */

.bar { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.bar h1 { font-size: 17px; margin: 0; }
.bar-side { display: flex; align-items: center; gap: 10px; }
.seg { display: flex; }
.seg button { padding: 1px 8px; border: 1px solid var(--line); background: var(--bg); font-size: 12px; }
.seg button:first-child { border-radius: 6px 0 0 6px; border-right: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.conn { font-size: 12px; color: var(--muted); white-space: nowrap; }
.conn.live::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--moving); margin-right: 6px; vertical-align: 1px; }

.muted { color: var(--muted); }
.pad { padding: 16px; }

/* Route history panel */
#history { padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.hist-head { display: flex; justify-content: space-between; align-items: start; }
.eyebrow { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
#history h2 { font-size: 15px; margin: 2px 0 10px; word-break: break-all; }
/* Two range inputs share one track; only their thumbs take pointer events. */
.rs { position: relative; height: 22px; --thumb: 10px; }
.rs-track { position: absolute; left: 0; right: 0; top: 9px; height: 4px; border-radius: 2px; background: var(--line); }
#rs-fill { position: absolute; top: 0; bottom: 0; background: var(--accent); left: calc(5px + (100% - 10px) * var(--from, 0)); right: calc(5px + (100% - 10px) * (1 - var(--to, 1))); }
.rs input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; appearance: none; background: none; pointer-events: none; }
.rs input::-webkit-slider-runnable-track { background: none; }
.rs input::-moz-range-track { background: none; }
.rs input::-webkit-slider-thumb { appearance: none; box-sizing: border-box; pointer-events: auto; width: var(--thumb); height: 20px; border: 2px solid var(--accent); border-radius: 3px; background: var(--bg); cursor: ew-resize; }
.rs input::-moz-range-thumb { box-sizing: border-box; pointer-events: auto; width: var(--thumb); height: 20px; border: 2px solid var(--accent); border-radius: 3px; background: var(--bg); cursor: ew-resize; }
.rs input:focus-visible { outline: none; }
.rs input:focus-visible::-webkit-slider-thumb { background: var(--accent); }
.rs input:focus-visible::-moz-range-thumb { background: var(--accent); }
.rs-ticks { position: relative; height: 16px; }
.rs-ticks button { position: absolute; left: calc(5px + (100% - 10px) * var(--p, 0)); transform: translateX(-50%); padding: 0 2px; border: 0; background: none; font-size: 11px; color: var(--muted); white-space: nowrap; }
.rs-ticks button:first-child { left: 0; transform: none; }
.rs-ticks button:last-child { left: auto; right: 0; transform: none; }
.rs-ticks button:hover { color: var(--text); }
.rs-ticks button.active { color: var(--accent); font-weight: 600; }
.rs-line { display: flex; align-items: center; gap: 8px; font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rs-first { margin-top: 6px; min-height: 18px; }
.rs-label { width: 54px; flex: none; color: var(--muted); }
.rs-line .btn { margin-left: auto; }
.rs-line .btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
#custom { display: grid; gap: 6px; margin-top: 8px; }
#custom label { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
#custom input { font: inherit; }
#custom button { padding: 5px; border: 1px solid var(--accent); background: var(--accent); color: #fff; border-radius: 6px; }
#custom[hidden] { display: none; }
#hist-info { margin: 8px 0 0; font-size: 12px; }
.badge { display: inline-block; padding: 0 6px; border-radius: 4px; background: var(--moving); color: #fff; font-size: 11px; margin-left: 4px; }

.icon-btn { border: 0; background: none; color: var(--muted); padding: 2px 6px; border-radius: 4px; }
.icon-btn:hover { background: var(--line); color: var(--text); }

/* Device list */
#devices { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#devices li { display: flex; gap: 10px; align-items: start; padding: 10px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
#devices li:hover { background: var(--panel); }
#devices li.selected { background: #eff6ff; }
.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex: none; }
.dot.moving { background: var(--moving); }
.dot.idle { background: var(--idle); }
.dot.parked { background: var(--parked); }
.dot.offline, .dot.nofix { background: var(--offline); }
.dev-main { flex: 1; min-width: 0; }
.dev-name { display: flex; align-items: center; gap: 4px; font-weight: 600; }
.dev-name .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-name .unnamed { font-weight: 400; color: var(--muted); }
.dev-meta { font-size: 12px; color: var(--muted); }

/* Map markers */
.veh { width: 26px; height: 26px; display: grid; place-items: center; }
.veh svg { width: 24px; height: 24px; stroke: #fff; stroke-width: 1.5; filter: drop-shadow(0 1px 1px rgba(0,0,0,.4)); }
.veh span { width: 14px; height: 14px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 2px rgba(0,0,0,.4); }
.veh.moving svg { fill: var(--moving); }
.veh.idle span { background: var(--idle); }
.veh.parked span { background: var(--parked); }
.veh.offline span, .veh.nofix span { background: var(--offline); }
.veh-label { font-weight: 600; padding: 1px 5px; }

/* Avatar markers: upright, with a status badge and a heading arrow when moving. */
.ava { position: relative; width: 40px; height: 40px; }
.ava svg, .list-ava svg, .avatar-opt svg { display: block; overflow: visible; }
.ava svg { width: 40px; height: 40px; filter: drop-shadow(0 1px 1px rgba(0,0,0,.45)); }
.ava .stat { position: absolute; right: 0; bottom: 0; width: 10px; height: 10px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 2px rgba(0,0,0,.4); }
.ava.moving .stat { background: var(--moving); }
.ava.idle .stat { background: var(--idle); }
.ava.parked .stat { background: var(--parked); }
.ava.offline .stat, .ava.nofix .stat { background: var(--offline); }
.ava.offline svg, .ava.nofix svg { opacity: .6; filter: grayscale(.7) drop-shadow(0 1px 1px rgba(0,0,0,.45)); }
.ava .hdg { position: absolute; inset: -8px; pointer-events: none; }
.ava .hdg b { position: absolute; left: 50%; top: 0; margin-left: -5px; border: 5px solid transparent; border-top: 0; border-bottom: 8px solid var(--moving); filter: drop-shadow(0 0 1px #fff); }
.list-ava { flex: none; width: 22px; height: 22px; }
.list-ava svg { width: 22px; height: 22px; }

/* The drunk bee staggers, buzzes and hiccups. */
.ava.bee svg, .list-ava.bee svg, .avatar-opt.bee svg { animation: stagger 2.8s ease-in-out infinite; transform-origin: 50% 75%; }
.bee .wing { animation: buzz .12s linear infinite alternate; transform-origin: 21px 22px; }
.bee .hic { animation: hic 2.8s ease-out infinite; }
.rocket .flame { animation: flicker .18s linear infinite alternate; }
@keyframes stagger {
  0%, 100% { transform: rotate(-11deg) translate(-1px, 0); }
  22% { transform: rotate(9deg) translate(2px, -2px); }
  48% { transform: rotate(-4deg) translate(1px, 1px); }
  70% { transform: rotate(14deg) translate(-2px, -1px); }
}
@keyframes buzz { to { transform: scaleY(.75) rotate(-4deg); } }
@keyframes hic {
  0%, 55% { opacity: 0; transform: translate(-2px, 4px); }
  65% { opacity: 1; }
  100% { opacity: 0; transform: translate(2px, -5px); }
}
@keyframes flicker { to { opacity: .55; } }
@media (prefers-reduced-motion: reduce) {
  .ava svg, .list-ava svg, .avatar-opt svg, .bee .wing, .bee .hic, .rocket .flame { animation: none; }
}

/* Record popup */
.rec h3 { margin: 0 0 6px; font-size: 14px; }
.rec table { border-collapse: collapse; font-size: 12px; }
.rec th { text-align: left; font-weight: 400; color: var(--muted); padding: 1px 12px 1px 0; white-space: nowrap; }
.rec td { padding: 1px 0; font-variant-numeric: tabular-nums; }
.rec .sep th { padding-top: 8px; font-weight: 600; color: var(--text); }
.rec .scroll { max-height: 260px; overflow-y: auto; }

/* Tracker panel */
#dash { width: 380px; flex: none; overflow-y: auto; border-left: 1px solid var(--line); background: var(--bg); }
.dash-head { display: flex; justify-content: space-between; align-items: start; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#dash h2 { font-size: 15px; margin: 2px 0 0; word-break: break-all; }
#dash-imei { font-size: 12px; font-variant-numeric: tabular-nums; }
#dash section { padding: 12px 16px; border-bottom: 1px solid var(--line); }
#dash h3 { font-size: 13px; margin: 0 0 8px; }
#dash h4 { font-size: 12px; margin: 0; }
.note { margin: 0; padding: 8px 16px; font-size: 12px; background: #fffbeb; border-bottom: 1px solid var(--line); }
.note.error { background: #fef2f2; color: #b91c1c; }
.pill { display: inline-block; padding: 0 7px; border-radius: 9px; font-size: 11px; line-height: 18px; white-space: nowrap; background: var(--line); color: var(--text); }
.pill.good { background: #dcfce7; color: #166534; }
.pill.bad { background: #fee2e2; color: #991b1b; }
.pill.warn { background: #fef3c7; color: #92400e; }
.kv { width: 100%; border-collapse: collapse; font-size: 12px; }
.kv th { text-align: left; font-weight: 400; color: var(--muted); padding: 2px 12px 2px 0; }
.kv td { padding: 2px 0; text-align: right; font-variant-numeric: tabular-nums; word-break: break-all; }
.kv .sep th { padding-top: 10px; font-weight: 600; color: var(--text); }
.kv .id { color: var(--muted); }
.btn { padding: 3px 10px; border: 1px solid var(--line); background: var(--bg); border-radius: 6px; font-size: 12px; }
.btn:hover { background: var(--panel); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.armed { background: #b91c1c; border-color: #b91c1c; color: #fff; }
.output { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 4px 0; }
.output .name { font-weight: 600; font-size: 13px; width: 52px; }
.output .src { margin-right: auto; font-size: 11px; color: var(--muted); }
.output .reading { font-size: 12px; font-variant-numeric: tabular-nums; }
.output-read { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.output-read .hint { margin: 0; }
.revert { flex-basis: 100%; display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.revert input { width: 74px; font: inherit; font-size: 12px; padding: 2px 4px; }
.hint { margin: 4px 0 0; font-size: 11px; color: var(--muted); }
.pgroup + .pgroup { margin-top: 12px; }
.pgroup-head { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.pgroup-head h4 { margin-right: auto; }
.param { display: grid; grid-template-columns: 1fr 68px 82px 32px; gap: 5px; align-items: center; font-size: 12px; padding: 2px 0; }
.param .cur { color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* The spinner's reserved space would clip long range placeholders. */
.param input { appearance: textfield; }
.param input::-webkit-inner-spin-button { appearance: none; }
.param .unit { color: var(--muted); white-space: nowrap; }
.param input, .param select { width: 100%; font: inherit; padding: 2px 4px; }
#dash-cmds { list-style: none; margin: 0; padding: 0; font-size: 12px; }
#dash-cmds li { padding: 6px 0; border-top: 1px solid var(--line); }
#dash-cmds li:first-child { border-top: 0; }
.cmd-head { display: flex; align-items: center; gap: 6px; }
.cmd-head code { margin-right: auto; word-break: break-all; }
.cmd-meta { color: var(--muted); font-size: 11px; }
.cmd-reply { margin: 2px 0 0; white-space: pre-wrap; word-break: break-all; font-family: ui-monospace, monospace; font-size: 11px; }

/* Narrow screens: the map stays at the top and everything else scrolls under
   it as one page, so no panel is cut short or left with a gap. */
@media (max-width: 700px) {
  html, body { height: auto; }
  body { flex-direction: column; min-height: 100%; }
  #map { order: -1; position: sticky; top: 0; z-index: 1; flex: none; height: 45vh; height: 45dvh; border-bottom: 1px solid var(--line); }
  #sidebar { width: auto; overflow: visible; border-right: 0; }
  #dash { width: auto; overflow: visible; border-left: 0; }
  /* A selected tracker's route history and details replace the list until
     it's closed. */
  body:has(#dash:not([hidden])) :is(#devices, #empty, #list-foot) { display: none; }
}

/* People section of the tracker panel */
.check { display: flex; align-items: center; gap: 6px; font-size: 12px; }
#dash-access h4 { margin: 0 0 4px; }
#dash-access h4 .muted { font-weight: normal; }
.people-group + .people-group { margin-top: 14px; }
.people-list { margin: 0 0 6px; font-size: 12px; word-break: break-all; }
.who-sees { margin-bottom: 4px; }
.picker-list .invite-row { padding: 6px 8px; }
.picker-list .invite-row .btn { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Viewer picker */
.picker {
  width: min(440px, 92vw); height: min(560px, 85vh); padding: 16px; border: 1px solid var(--line); border-radius: 10px;
  display: flex; flex-direction: column; gap: 8px; color: var(--text);
}
.picker:not([open]) { display: none; }
.picker::backdrop { background: rgb(0 0 0 / .3); }
.picker h3 { margin: 0; font-size: 15px; }
.picker .hint { margin: 0; }
.picker input[type=search] { font: inherit; padding: 4px 6px; }
.picker-list { flex: 1; list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 0; border: 1px solid var(--line); border-radius: 6px; }
.picker-list li { padding: 4px 8px; border-bottom: 1px solid var(--line); }
.picker-list li:last-child { border-bottom: 0; }
.picker-list .email { flex: 1; min-width: 0; word-break: break-all; }
.picker-list .grow { flex: 1; min-width: 0; }
.picker-list .btn { padding: 1px 8px; }
.watch-limit { flex-wrap: wrap; }
.watch-limit select, .watch-limit input { font: inherit; }
.picker-foot { display: flex; align-items: center; gap: 8px; }
.picker-foot .hint { flex: 1; }
.name-form { display: flex; gap: 6px; }
.name-form input { flex: 1; min-width: 0; font: inherit; padding: 4px 6px; }

/* Adding a phone */
#list-foot { padding: 10px 16px; border-top: 1px solid var(--line); }
/* A modal dialog is pinned top and bottom, so auto would fill the height. */
.phone-dlg { height: fit-content; max-height: 85vh; }
.phone-body { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; font-size: 13px; }
.phone-body h3 { margin: 0; font-size: 15px; }
.phone-body .note { border: 0; border-radius: 6px; padding: 8px 10px; }
.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 10px; }
.app-links { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 2px; }
.app-links a.btn { text-decoration: none; color: inherit; }
.app-links a.btn.primary { color: #fff; }
.phone-body details { font-size: 12px; }
.phone-body summary { cursor: pointer; color: var(--muted); }
.avatar-pick { display: flex; flex-wrap: wrap; gap: 6px; }
.avatar-opt { width: 46px; height: 46px; padding: 4px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); cursor: pointer; }
.avatar-opt:hover { background: var(--panel); }
.avatar-opt[aria-pressed="true"] { border: 2px solid var(--accent); padding: 3px; }
.avatar-opt svg { width: 36px; height: 36px; }
.avatar-opt .plain { width: 14px; height: 14px; border-radius: 50%; background: var(--parked); border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); }
.name-form input { flex: 1; min-width: 0; font: inherit; padding: 3px 6px; }
.param.ro { grid-template-columns: 1fr auto; }
a.btn { color: inherit; text-decoration: none; }

/* Sign-in, welcome and account pages */
body.page { display: block; background: var(--panel); min-height: 100%; height: auto; padding: 24px 16px; }
.page .card { max-width: 380px; margin: 0 auto 16px; padding: 20px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
.page.wide main { max-width: 860px; margin: 0 auto; }
.page.wide .card { max-width: none; }
.page h1 { font-size: 18px; margin: 0 0 12px; }
.page h1 a { color: inherit; text-decoration: none; }
.page h2 { font-size: 15px; margin: 0 0 10px; }
.page h3 { font-size: 13px; margin: 16px 0 6px; }
.page form { display: grid; gap: 10px; }
.page form[hidden] { display: none; }
.page label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.page input, .page select { font: inherit; color: var(--text); padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; }
.page .btn { padding: 6px 12px; font-size: 13px; }
.page p { margin: 0; }
.status { min-height: 1.2em; font-size: 12px; color: #166534; }
.status:empty { display: none; }
.status.error { color: #b91c1c; }
.links { font-size: 12px; }
.links a { color: var(--accent); }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; }
.who { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.row { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.row .grow { flex: 1; min-width: 200px; }
.list { width: 100%; border-collapse: collapse; font-size: 13px; }
.list th { text-align: left; font-weight: 600; font-size: 12px; color: var(--muted); padding: 4px 8px 4px 0; border-bottom: 1px solid var(--line); }
.list td { padding: 6px 8px 6px 0; border-bottom: 1px solid var(--line); vertical-align: middle; word-break: break-all; }
.list .actions { text-align: right; white-space: nowrap; }
.list .actions .btn { padding: 3px 10px; font-size: 12px; margin-left: 4px; }
.list .owns { font-size: 12px; color: var(--muted); word-break: normal; }
.list select { padding: 2px 4px; font-size: 12px; }
@media (max-width: 640px) {
  .list thead { display: none; }
  .list tr { display: grid; grid-template-columns: 1fr auto; padding: 6px 0; border-bottom: 1px solid var(--line); }
  .list td { border: 0; padding: 2px 0; }
  .list .actions { text-align: left; grid-column: 1 / -1; }
}
