/* ------------------------------------------------------------------ tokens */

:root {
  color-scheme: light dark;

  --bg:            #f7f8fa;
  --surface:       #ffffff;
  --surface-sunk:  #f1f3f7;
  --line:          #e2e5ec;
  --text:          #161a22;
  --text-dim:      #5c6472;
  --accent:        #3b6ef5;
  --accent-soft:   #e8eeff;
  --on-accent:     #ffffff;
  --good:          #179c62;
  --warn:          #d98315;
  --bad:           #d9453f;

  /* Opaque: the sticky filter and controls sit over scrolling list rows,
     which show through any transparency. */
  --panel-sticky: #ffffff;
  --shadow: 0 1px 2px rgb(16 22 34 / .06), 0 8px 28px rgb(16 22 34 / .10);
  --radius: 18px;
  --ease:   cubic-bezier(.32, .72, 0, 1);

  /* Safe-area aware sheet heights (mobile). Stated in vh, upgraded to dvh
     below where supported — Safari only gained dvh in 15.4, and the usual
     two-declaration fallback does not work through a custom property: the
     token stream is stored unparsed, so an unsupported unit is not caught
     until var() is substituted, by which point the fallback is gone. */
  /* Comfortably clears grip + header; note rem is 15px here, not 16. */
  --sheet-peek: calc(5.6rem + env(safe-area-inset-bottom, 0px));
  --sheet-half: 52vh;
  --sheet-full: 92vh;

  /* How much of the map the bottom sheet currently covers. Kept in sync by
     sheet.js; stays 0 on desktop, where the panel is a floating card. */
  --sheet-h: 0px;
}

/* Dynamic viewport units track the iOS toolbar collapsing; plain vh does not. */
@supports (height: 1dvh) {
  :root {
    --sheet-half: 52dvh;
    --sheet-full: 92dvh;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:            #0d1016;
    --surface:       #171b24;
    --surface-sunk:  #11151d;
    --line:          #272d3a;
    --text:          #e8ecf4;
    --text-dim:      #98a1b3;
    --accent:        #6d94ff;
    --accent-soft:   #1d2740;
    --on-accent:     #0b1020;
    --good:          #35c98a;
    --warn:          #e5a03c;
    --bad:           #f2635d;

    --panel-sticky: #171b24;
    --shadow: 0 1px 2px rgb(0 0 0 / .5), 0 12px 40px rgb(0 0 0 / .55);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* --------------------------------------------------------------------- map */

#map {
  position: fixed;
  inset: 0;
  background: var(--surface-sunk);
}

/* Leaflet's own chrome, themed */
.leaflet-container { font: inherit; background: var(--surface-sunk); }

.leaflet-control-zoom a,
.leaflet-bar a {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
}
.leaflet-control-zoom a:hover { background: var(--surface-sunk); }

.leaflet-control-attribution {
  background: color-mix(in srgb, var(--surface) 80%, transparent) !important;
  color: var(--text-dim);
}
.leaflet-control-attribution a { color: var(--text-dim); }

/* Keep the bottom controls — attribution and zoom — against the bottom edge of
   the *visible* map rather than the viewport, so the sheet never covers them.
   Loaded after leaflet.css, so this wins on equal specificity. */
.leaflet-bottom { bottom: var(--sheet-h); }

/* At the full sheet position only a sliver of map is left; squeezing the
   controls into it looks broken, so take them out until the sheet collapses. */
:root[data-sheet="full"] .leaflet-bottom { display: none; }

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.leaflet-popup-content-wrapper { border-radius: 14px; }
.leaflet-popup-content { margin: .85rem 1rem; }
.leaflet-popup-content h3 { margin: 0 0 .4rem; font-size: .95rem; }
.leaflet-popup-content table { border-collapse: collapse; font-size: .85rem; }
.leaflet-popup-content td { padding: .1rem 0; color: var(--text-dim); }

/* Pulsing halo under the most recent position */
.pulse {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 55%, transparent);
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 55%, transparent);
  animation: pulse 2.4s var(--ease) infinite;
}

@keyframes pulse {
  70%  { box-shadow: 0 0 0 22px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}

@media (prefers-reduced-motion: reduce) {
  .pulse { animation: none; }
}

/* ------------------------------------------------------------------- panel */

#panel {
  position: fixed;
  z-index: 500;
  display: flex;
  flex-direction: column;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: blur(22px) saturate(180%);
  -webkit-backdrop-filter: blur(22px) saturate(180%);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
}

#grip { display: none; }

#panel-head {
  display: flex;
  align-items: baseline;
  gap: .6rem;
  padding: .85rem 1.1rem .6rem;
  flex-shrink: 0;
}

#panel-head h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -.01em;
  /* Holds a device name when the sheet is collapsed — must not push the
     status pill out of the row. */
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#status {
  margin-left: auto;
  font-size: .75rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  white-space: nowrap;
}

#status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  flex-shrink: 0;
}
#status[data-state="live"]::before    { background: var(--good); }
#status[data-state="loading"]::before { background: var(--warn); animation: blink 1s steps(2) infinite; }
#status[data-state="error"]::before   { background: var(--bad); }

@keyframes blink { 50% { opacity: .25; } }

/* One scroll area for the whole panel. The filter sticks to the top and the
   controls to the bottom, so a long list never pushes either out of reach —
   and the detail card cannot squeeze the list to nothing. */
#panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 .6rem calc(.7rem + env(safe-area-inset-bottom));
}

/* ------------------------------------------------------------------- filter */

#filter-row {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  padding: .1rem .55rem .45rem;
  margin: 0 -.05rem;
  background: var(--panel-sticky);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

#filter {
  flex: 1;
  min-width: 0;
  padding: .45rem .7rem .45rem 2rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-sunk) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a93a5' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M20 20l-3.5-3.5'/%3E%3C/svg%3E") no-repeat .55rem center / 15px;
  color: var(--text);
  font: inherit;
  font-size: .875rem;
}

#filter::placeholder { color: var(--text-dim); }
#filter:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}
#filter::-webkit-search-cancel-button { cursor: pointer; }

#count {
  flex-shrink: 0;
  margin: 0 .65rem .4rem;
  text-align: right;
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

#count:empty { display: none; }

#empty {
  margin: .6rem .55rem;
  color: var(--text-dim);
  font-size: .85rem;
  text-align: center;
}
#empty[hidden] { display: none; }

/* -------------------------------------------------------------------- login */

#login {
  display: flex;
  flex-direction: column;
  gap: .55rem;
  margin: .2rem .55rem .6rem;
}
#login[hidden] { display: none; }

#login .intro {
  margin: 0;
  font-size: .85rem;
  color: var(--text-dim);
}

#login label {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  font-size: .8rem;
  color: var(--text-dim);
}

#login input[type="text"],
#login input[type="password"] {
  padding: .5rem .7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-sunk);
  color: var(--text);
  font: inherit;
  font-size: .9rem;
}

#login input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

#login .remember {
  flex-direction: row;
  align-items: center;
  gap: .5rem;
  color: var(--text);
}

#login-error {
  margin: 0;
  font-size: .8rem;
  color: var(--bad);
}
#login-error[hidden] { display: none; }

#login button {
  padding: .6rem 1rem;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
#login button:disabled { opacity: .6; cursor: progress; }

#signin,
#logout {
  margin: .4rem .55rem 0;
  padding: .5rem 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: .85rem;
  cursor: pointer;
}
#signin:hover,
#logout:hover { background: var(--surface-sunk); color: var(--text); }
#signin[hidden],
#logout[hidden] { display: none; }

/* -------------------------------------------------------------- device list */

#devices {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* An element rule with display:flex outranks the UA's [hidden]{display:none},
   so anything toggled by the hidden attribute needs saying explicitly. */
#devices[hidden],
#filter-row[hidden],
#controls[hidden] { display: none; }

#devices mark {
  background: color-mix(in srgb, var(--accent) 28%, transparent);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
}

#devices button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: .65rem;
  padding: .6rem .55rem;
  border: 0;
  border-radius: 12px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background .16s var(--ease);
}

#devices button:hover  { background: var(--surface-sunk); }
#devices button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

#devices .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--text-dim);
  flex-shrink: 0;
  transition: all .16s var(--ease);
}

#devices .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#devices [aria-current="true"] { background: var(--accent-soft); }
#devices [aria-current="true"] .name { font-weight: 600; }
#devices [aria-current="true"] .dot {
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
}

/* ----------------------------------------------------------------- details */

#detail {
  flex-shrink: 0;
  margin: .5rem .55rem .2rem;
  padding: .75rem .8rem;
  border-radius: 14px;
  background: var(--surface-sunk);
  border: 1px solid var(--line);
  font-size: .85rem;
}

#detail[hidden] { display: none; }

.battery {
  display: flex;
  align-items: center;
  gap: .55rem;
  margin-bottom: .55rem;
}

.battery .bar {
  flex: 1;
  height: 7px;
  border-radius: 99px;
  background: color-mix(in srgb, var(--text-dim) 22%, transparent);
  overflow: hidden;
}

.battery .fill {
  height: 100%;
  border-radius: 99px;
  background: var(--good);
  transition: width .4s var(--ease);
}
.battery .fill[data-level="warn"] { background: var(--warn); }
.battery .fill[data-level="bad"]  { background: var(--bad); }

.battery .pct {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  min-width: 2.6em;
  text-align: right;
}

.battery .charging { color: var(--good); }

#detail dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: .2rem .7rem;
}

#detail dt { color: var(--text-dim); }
#detail dd { margin: 0; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- controls */

#controls {
  position: sticky;
  bottom: calc(-.7rem - env(safe-area-inset-bottom));
  z-index: 2;
  margin-top: .7rem;
  padding: .7rem 0 calc(.7rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel-sticky);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  gap: .1rem;
}

.switch {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .5rem .55rem;
  border-radius: 12px;
  cursor: pointer;
  transition: background .16s var(--ease);
}

.switch:hover { background: var(--surface-sunk); }
.switch:has(input:disabled) { opacity: .45; cursor: not-allowed; }

.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch .track {
  position: relative;
  width: 38px;
  height: 22px;
  border-radius: 99px;
  background: color-mix(in srgb, var(--text-dim) 32%, transparent);
  flex-shrink: 0;
  transition: background .18s var(--ease);
}

.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / .3);
  transition: transform .18s var(--ease);
}

.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.switch .label { display: flex; flex-direction: column; line-height: 1.25; }
.switch .label small { color: var(--text-dim); font-size: .75rem; }

#googleEarth {
  margin: .5rem .55rem 0;
  padding: .6rem 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
  transition: background .16s var(--ease), border-color .16s var(--ease);
}

#googleEarth:hover:not(:disabled) { background: var(--surface-sunk); border-color: var(--accent); }
#googleEarth:disabled { opacity: .45; cursor: not-allowed; }

/* ------------------------------------------------------- desktop: side card */

@media (min-width: 760px) {
  #panel {
    top: 1rem;
    left: 1rem;
    width: 22.5rem;
    max-height: calc(100dvh - 2rem);
    border-radius: var(--radius);
  }
}

/* ------------------------------------------------------ mobile: bottom sheet */

@media (max-width: 759.98px) {
  #panel {
    left: 0;
    right: 0;
    bottom: 0;
    height: var(--sheet-half);
    border-radius: var(--radius) var(--radius) 0 0;
    border-width: 1px 0 0;
    transition: height .34s var(--ease);
  }

  #panel[data-state="peek"] { height: var(--sheet-peek); }
  #panel[data-state="full"] { height: var(--sheet-full); }
  #panel[data-dragging]     { transition: none; }

  #grip {
    display: block;
    width: 100%;
    padding: .85rem 0 .45rem;      /* with the header below, a 44px+ target */
    border: 0;
    background: none;
    cursor: grab;
    flex-shrink: 0;
  }

  /* The grip and the header are both drag handles. touch-action:none stops
     iOS treating the gesture as a scroll or a pinch — a stray pinch scales
     the visual viewport, which makes a position:fixed panel look wider
     mid-drag. user-select stops the drag turning into a text selection. */
  #grip,
  #panel-head {
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    cursor: grab;
  }

  #panel[data-dragging] #grip,
  #panel[data-dragging] #panel-head { cursor: grabbing; }

  #grip::before {
    content: "";
    display: block;
    width: 38px;
    height: 4px;
    margin: 0 auto;
    border-radius: 99px;
    background: color-mix(in srgb, var(--text-dim) 45%, transparent);
  }

  #panel-head { padding-top: .35rem; }

  /* Collapsed the sheet shows the grip and the device name, nothing else —
     otherwise the top of the filter field peeks out below the header.
     data-collapsed is set while dragging near the bottom, so a low drag looks
     like the peek state instead of showing clipped content; above that a drag
     still previews the list. */
  #panel[data-state="peek"]:not([data-dragging]) #panel-body,
  #panel[data-collapsed] #panel-body { display: none; }

  /* Handing a KML to Google Earth is a desktop workflow — on a phone the
     browser just renders the XML. display:none also drops it from tab order. */
  #googleEarth { display: none; }

  .leaflet-control-attribution { font-size: 10px; }

  /* Leaflet pins the attribution flush into the corner, which would leave it
     sitting right on the sheet's edge — lift the whole bottom stack slightly. */
  .leaflet-bottom { bottom: calc(var(--sheet-h) + 6px); }
}
