:root {
  color-scheme: dark;

  /* ---- Design tokens ---- */
  /* Surfaces */
  --bg: #0c1116;
  --surface-1: #131a22;
  --surface-2: #18212b;
  --surface-3: #1f2a36;
  --elevated: #0f161d;

  /* Lines */
  --border: #243140;
  --border-strong: #2f3f50;

  /* Text */
  --text: #eef3f8;
  --text-muted: #93a3b4;
  --text-faint: #6c7d8e;

  /* Brand / accents */
  --accent: #3b8aff;
  --accent-hover: #5599ff;
  --accent-soft: rgba(59, 138, 255, .14);
  --wifi: #3b8aff;
  --ble: #c46bff;
  --route: #ffd166;
  --danger: #e0515c;
  --danger-hover: #ee636e;
  --ok: #5fd38a;

  /* Radii */
  --r-sm: 8px;
  --r-md: 11px;
  --r-lg: 16px;
  --r-pill: 999px;

  /* Spacing scale */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 20px;
  --s-6: 24px;

  /* Shadows */
  --shadow-1: 0 1px 2px rgba(0,0,0,.3);
  --shadow-2: 0 8px 28px rgba(0,0,0,.35);
  --shadow-panel: 0 0 1px rgba(0,0,0,.6);

  /* Layout */
  --header-h: 60px;
  --panel-w: 320px;
  --devices-w: 360px;

  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; overflow: hidden; background: var(--bg); }
button, input, select { font: inherit; }

/* ---- Buttons ---- */
button {
  border: 0;
  border-radius: var(--r-md);
  padding: 9px 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  transition: transform .08s ease, background .15s ease, box-shadow .15s ease;
}
button:hover { background: var(--accent-hover); }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--accent-hover); outline-offset: 2px; }
button.danger { background: var(--danger); }
button.danger:hover { background: var(--danger-hover); }
button.secondary { background: var(--surface-3); color: var(--text); }
button.secondary:hover { background: var(--border-strong); }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; padding: 8px; }

/* ---- Header ---- */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
  padding: 0 var(--s-5);
  background: linear-gradient(180deg, #141d27, var(--surface-1));
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-2);
  position: relative;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid rgba(59,138,255,.3);
}
h1 { margin: 0; font-size: 1.2rem; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; }
h1 span { color: var(--accent); }
h2 { margin: 0; font-size: 1rem; font-weight: 600; }
p { margin: 0; color: var(--text-muted); }

.tabs { display: flex; gap: 6px; background: var(--elevated); padding: 4px; border-radius: var(--r-pill); border: 1px solid var(--border); }
.tab { background: transparent; color: var(--text-muted); padding: 7px 16px; border-radius: var(--r-pill); font-weight: 600; }
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--accent); color: #fff; }
.tab.active:hover { background: var(--accent-hover); }

/* ---- Pages / layout ---- */
.page { display: none; height: calc(100vh - var(--header-h)); }
.page.active { display: flex; }
.map-page { min-height: 0; position: relative; }
.map-shell { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: #0a0e12; }

.left-panel, .right-panel {
  width: var(--panel-w);
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--s-4);
  background: var(--surface-1);
  z-index: 5;
}
.left-panel { border-right: 1px solid var(--border); }
.right-panel { border-left: 1px solid var(--border); }
.devices-panel {
  width: var(--devices-w);
  flex: 0 0 auto;
  position: relative;
}

/* Drag-to-resize handle: a zero-width flex item sitting on the seam between
   the map and the devices panel, with a wide absolutely-positioned hit zone
   so it is never clipped by either neighbour's overflow. */
.resize-handle {
  position: relative;
  flex: 0 0 0px;
  width: 0;
  align-self: stretch;
  z-index: 15;
}
.resize-handle::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: -7px;
  width: 14px;
  cursor: col-resize;
  touch-action: none;
}
.resize-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: -1px;
  transform: translateY(-50%);
  width: 2px;
  height: 36px;
  border-radius: 2px;
  background: var(--border-strong);
  transition: background .12s ease;
  pointer-events: none;
}
.resize-handle:hover::after,
.resize-handle:focus-visible::after { background: var(--accent); }
.resize-handle:focus-visible { outline: none; }
body.resizing-devices { cursor: col-resize; user-select: none; }
body.resizing-devices #map { pointer-events: none; }

.panel-header { display: flex; justify-content: space-between; gap: var(--s-3); align-items: center; margin-bottom: var(--s-4); }
.panel-hint { font-size: .85rem; margin-bottom: var(--s-3); }

.badge {
  display: inline-grid; place-items: center;
  min-width: 26px; height: 22px; padding: 0 8px;
  background: var(--accent-soft); color: var(--accent);
  border-radius: var(--r-pill); font-size: .78rem; font-weight: 700;
}

.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s-4);
}

/* ---- Capture list ---- */
.capture-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s-2); min-width: 0; }
.capture-item {
  width: 100%;
  text-align: left;
  display: grid;
  gap: 3px;
  padding: 10px 12px;
  border-radius: var(--r-md);
  background: var(--elevated);
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 400;
  transition: background .12s ease, border-color .12s ease;
}
.capture-item:hover { background: var(--surface-2); border-color: var(--border-strong); }
.capture-item.active { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.capture-line { min-width: 0; }
.capture-meta-line { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.capture-name { display: block; font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .9rem; color: var(--text); }
.capture-date { color: var(--text); font-weight: 600; font-size: .82rem; white-space: nowrap; }
.capture-counts { color: var(--text-muted); font-size: .76rem; white-space: nowrap; }

/* ---- Device list ---- */
.device-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s-2); min-width: 0; }
.device-item {
  width: 100%;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--r-md);
  background: var(--elevated);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.device-item:hover { background: var(--surface-2); border-color: var(--border-strong); }
.device-item.active { border-color: var(--route); background: rgba(255,209,102,.1); box-shadow: inset 0 0 0 1px var(--route); }
.device-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.device-dot.wifi { background: var(--wifi); box-shadow: 0 0 0 3px rgba(59,138,255,.18); }
.device-dot.ble { background: var(--ble); box-shadow: 0 0 0 3px rgba(196,107,255,.18); }
.device-body { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 1px; }
.device-name { font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .9rem; }
.device-auth { font-size: .74rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---- Device filters ---- */
.device-filters { display: flex; flex-wrap: wrap; gap: var(--s-3); margin-bottom: var(--s-3); }
.filter-group { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.filter-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); }
.seg-filter { display: inline-flex; background: var(--elevated); border: 1px solid var(--border); border-radius: var(--r-md); padding: 3px; gap: 2px; }
.seg-btn { background: transparent; color: var(--text-muted); padding: 5px 11px; border-radius: 8px; font-size: .82rem; font-weight: 600; }
.seg-btn:hover { background: var(--surface-2); color: var(--text); }
.seg-btn.active { background: var(--accent); color: #fff; }
.seg-btn.active:hover { background: var(--accent-hover); }

/* Shared dropdown styling for filter/sort menus (Auth filter + Captures Sort) */
.menu-filter { position: relative; }
.menu-filter > summary {
  list-style: none; cursor: pointer; user-select: none;
  display: inline-flex; align-items: center; justify-content: space-between; gap: 6px;
  background: var(--elevated); border: 1px solid var(--border); border-radius: var(--r-md);
  padding: 7px 11px; font-size: .82rem; font-weight: 600; color: var(--text);
}
.menu-filter > summary::-webkit-details-marker { display: none; }
.menu-filter > summary::after { content: "▾"; color: var(--text-faint); font-size: .8rem; }
.menu-filter[open] > summary { border-color: var(--accent); }
.menu-filter-menu {
  position: absolute; top: calc(100% + 4px); z-index: 50;
  min-width: 200px; max-height: 260px; overflow-y: auto;
  background: var(--surface-2); border: 1px solid var(--border-strong);
  border-radius: var(--r-md); padding: 6px; box-shadow: var(--shadow-2);
  display: grid; gap: 2px;
}
/* Auth menu anchors to the right edge; sort menu to the left. */
.auth-filter .menu-filter-menu { right: 0; }
.sort-filter .menu-filter-menu { left: 0; }

.auth-opt { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: var(--r-sm); cursor: pointer; font-size: .85rem; }
.auth-opt:hover { background: var(--surface-3); }
.auth-opt input { accent-color: var(--accent); width: 15px; height: 15px; flex: 0 0 auto; }
.auth-opt span:nth-of-type(1) { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-opt-count { color: var(--text-faint); font-size: .75rem; }

/* Single-select option rows (Sort menu) */
.menu-opt {
  width: 100%; text-align: left;
  background: transparent; color: var(--text);
  padding: 6px 8px; border-radius: var(--r-sm); font-size: .85rem; font-weight: 500;
  display: flex; align-items: center; gap: 8px;
}
.menu-opt:hover { background: var(--surface-3); }
.menu-opt.active { color: var(--accent); font-weight: 600; }
.menu-opt.active::before { content: "✓"; font-size: .8rem; }
.menu-opt:not(.active)::before { content: ""; width: 9px; }

.sort-group { width: 100%; margin-bottom: var(--s-4); }
.sort-filter { width: 100%; }
.sort-filter > summary { width: 100%; }
.sort-filter .menu-filter-menu { min-width: 100%; }

/* Auth pill (replaces raw [WPA2_PSK] text) */
.auth-pill {
  flex: 0 0 auto;
  font-size: .68rem; font-weight: 600; letter-spacing: .02em;
  padding: 3px 8px; border-radius: var(--r-pill);
  background: var(--surface-3); color: var(--text-muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.auth-pill.open { background: rgba(95,211,138,.12); color: var(--ok); border-color: rgba(95,211,138,.3); }
.auth-pill.secure { background: rgba(59,138,255,.12); color: var(--accent); border-color: rgba(59,138,255,.3); }
.auth-pill.unknown { background: var(--surface-3); color: var(--text-faint); }

.muted { color: var(--text-muted); }
.muted-block { padding: var(--s-4); text-align: center; color: var(--text-muted); font-size: .9rem; }

/* ---- Map overlays ---- */
.summary-overlay {
  position: absolute;
  top: var(--s-4);
  right: var(--s-4);
  z-index: 600;
  width: min(300px, calc(100% - 32px));
  background: rgba(15,22,29,.94);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  padding: var(--s-4);
  box-shadow: var(--shadow-2);
  line-height: 1.5;
}
.summary-overlay[hidden] { display: none; }
.summary-overlay .summary-title { font-weight: 600; font-size: .98rem; margin-bottom: 10px; padding-right: 26px; display: flex; align-items: center; gap: 8px; }
.summary-close {
  position: absolute;
  top: 10px;
  right: 10px;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--text-muted);
}
.summary-close:hover { background: var(--surface-3); color: var(--text); }
.summary-overlay dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; font-size: .85rem; }
.summary-overlay dt { color: var(--text-faint); }
.summary-overlay dd { margin: 0; min-width: 0; overflow-wrap: anywhere; text-align: right; }
.summary-stats { display: flex; gap: 8px; margin-bottom: 12px; }
.stat { flex: 1; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-md); padding: 8px 10px; text-align: center; }
.stat-val { font-size: 1.1rem; font-weight: 700; }
.stat-lbl { font-size: .68rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); }
.stat.wifi .stat-val { color: var(--wifi); }
.stat.ble .stat-val { color: var(--ble); }

.legend {
  position: absolute;
  bottom: var(--s-5);
  right: var(--s-5);
  display: flex;
  gap: var(--s-3);
  align-items: center;
  padding: 9px 13px;
  border-radius: var(--r-pill);
  background: rgba(15,22,29,.94);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-strong);
  z-index: 600;
  box-shadow: var(--shadow-2);
  font-size: .8rem;
}
.legend i { display: inline-block; width: 11px; height: 11px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.legend .route { width: 20px; height: 4px; border-radius: 8px; background: var(--route); }
.legend .wifi { background: var(--wifi); }
.legend .ble { background: var(--ble); }

.status { min-height: 1.3em; margin: 8px 0 0; color: var(--ok); }
.status.error, .map-status.error { color: var(--danger-hover); }
.map-status {
  position: absolute;
  left: var(--s-4);
  bottom: var(--s-4);
  z-index: 500;
  color: var(--ok);
  background: rgba(15,22,29,.94);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-pill);
  padding: 8px 13px;
  font-size: .85rem;
  max-width: min(420px, calc(100% - 200px));
}
.map-status:empty { display: none; }

/* ---- Upload page ---- */
.upload-page { overflow: auto; padding: var(--s-6); background: var(--bg); }
.upload-panel { width: min(1180px, 100%); margin: 0 auto; }
.upload-form { margin-bottom: var(--s-5); }
.upload-form > label { display: block; margin-bottom: var(--s-2); font-weight: 600; color: var(--text-muted); font-size: .85rem; }
.file-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
input[type="file"] {
  color: var(--text-muted);
  background: var(--elevated);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 9px 11px;
  flex: 1; min-width: 220px;
}
input[type="file"]::file-selector-button {
  margin-right: 12px; border: 0; border-radius: var(--r-sm);
  padding: 6px 12px; background: var(--surface-3); color: var(--text); cursor: pointer; font-weight: 600;
}

.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--r-lg); }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th { color: var(--text-muted); background: var(--elevated); position: sticky; top: 0; z-index: 1; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
tbody tr:hover { background: var(--surface-2); }
td input[type="text"] {
  width: 100%; color: var(--text);
  background: var(--elevated); border: 1px solid var(--border);
  border-radius: var(--r-sm); padding: 8px 9px;
}
td input[type="text"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.table-actions { display: flex; gap: 6px; }
.table-actions button { padding: 7px 11px; border-radius: var(--r-sm); }
.file-cell { color: var(--text-muted); max-width: 240px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---- Route endpoint markers ---- */
.endpoint-marker { background: transparent; border: 0; }
.endpoint-marker { display: flex; align-items: center; gap: 0; width: 88px; height: 22px; }
.endpoint-marker.start { flex-direction: row; }       /* label … dot(anchor, right) */
.endpoint-marker.end { flex-direction: row-reverse; }  /* dot(anchor, left) … label */
.endpoint-label {
  padding: 3px 9px;
  border-radius: var(--r-pill); color: #fff;
  font-weight: 700; font-size: 11px; letter-spacing: .05em; text-align: center;
  border: 2px solid #fff; box-shadow: var(--shadow-2); white-space: nowrap;
  position: relative; z-index: 2;
}
/* Short connector bridging label and dot so they read as one tag. */
.endpoint-marker.start .endpoint-label { margin-right: -2px; }
.endpoint-marker.end .endpoint-label { margin-left: -2px; }
.endpoint-dot {
  width: 13px; height: 13px; flex: 0 0 auto;
  border-radius: 50%; border: 2px solid #fff; box-shadow: var(--shadow-2);
  position: relative; z-index: 1;
}
.endpoint-marker.start .endpoint-label,
.endpoint-marker.start .endpoint-dot { background: #1f9d57; }
.endpoint-marker.end .endpoint-label,
.endpoint-marker.end .endpoint-dot { background: var(--danger); }

/* Directional arrowheads along the route line */
.route-arrow { background: transparent; border: 0; }
.route-arrow span {
  display: block; width: 16px; height: 16px;
  font-size: 13px; line-height: 16px; text-align: center;
  color: #1a1207;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff;
}

/* Leaflet popup theming */
.leaflet-popup-content-wrapper { background: var(--surface-2); color: var(--text); border-radius: var(--r-md); border: 1px solid var(--border-strong); }
.leaflet-popup-tip { background: var(--surface-2); border: 1px solid var(--border-strong); }
.leaflet-popup-content { margin: 12px 14px; line-height: 1.5; }
.leaflet-container a.leaflet-popup-close-button { color: var(--text-muted); }

/* ---- Mobile segmented switcher (hidden on desktop) ---- */
.mobile-switch { display: none; }

/* ======================================================
   Tablet: narrower side panels
   ====================================================== */
@media (max-width: 1100px) {
  :root { --panel-w: 280px; --devices-w: 300px; }
}

/* ======================================================
   Mobile / small screens: single panel at a time
   ====================================================== */
@media (max-width: 820px) {
  :root { --header-h: 56px; }
  body { overflow: auto; }

  .app-header { padding: 0 var(--s-4); }
  h1 { font-size: 1.05rem; }

  .page.active { height: calc(100vh - var(--header-h)); flex-direction: column; }

  /* Segmented switcher visible, fixed under header */
  .mobile-switch {
    display: flex;
    gap: 6px;
    padding: 8px var(--s-4);
    background: var(--surface-1);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
  }
  .seg {
    flex: 1; background: var(--elevated); color: var(--text-muted);
    border: 1px solid var(--border); border-radius: var(--r-md); padding: 9px 0;
    font-weight: 600;
  }
  .seg.active { background: var(--accent); color: #fff; border-color: var(--accent); }

  /* Only the selected panel shows; map fills remaining space */
  .resize-handle { display: none; }
  .left-panel, .right-panel, .map-shell {
    display: none;
    width: 100%; max-width: none; height: auto;
    border: 0;
  }
  .map-page[data-mobile-panel="captures"] .captures-panel,
  .map-page[data-mobile-panel="devices"] .devices-panel { display: block; flex: 1; min-height: 0; overflow-y: auto; }
  .map-page[data-mobile-panel="map"] .map-shell { display: block; flex: 1; min-height: 0; }

  .summary-overlay { width: calc(100% - 32px); }
  .map-status { max-width: calc(100% - 32px); }

  /* Larger touch targets in the upload table */
  .upload-page { padding: var(--s-4); }
  .table-actions button { padding: 9px 13px; }
}

/* Honour reduced-motion */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ====== Static-version additions: header file button + drop zone ====== */
.header-actions { display: flex; align-items: center; gap: var(--s-3); }
.file-btn {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--accent); color: #fff;
  border: 0; border-radius: var(--r-md);
  padding: 9px 14px; font-weight: 600; font-size: .9rem; cursor: pointer;
  transition: background .15s ease, transform .08s ease;
}
.file-btn:hover { background: var(--accent-hover); }
.file-btn:active { transform: translateY(1px); }

/* Empty-state / drag-drop zone over the map */
.drop-zone {
  position: absolute; inset: 0; z-index: 700;
  display: grid; place-items: center;
  background: rgba(10, 14, 18, .82);
  backdrop-filter: blur(2px);
  text-align: center; padding: var(--s-5);
}
.drop-zone[hidden] { display: none; }
.drop-inner {
  max-width: 360px;
  border: 2px dashed var(--border-strong);
  border-radius: var(--r-lg);
  padding: var(--s-6) var(--s-5);
  background: rgba(19, 26, 34, .7);
  color: var(--text-muted);
  display: grid; gap: 10px; justify-items: center;
}
.drop-inner svg { color: var(--accent); }
.drop-inner h3 { margin: 4px 0 0; color: var(--text); font-size: 1.05rem; }
.drop-inner p { font-size: .9rem; line-height: 1.5; }
.drop-inner strong { color: var(--text); }
.map-shell.dragover .drop-zone { background: rgba(10, 14, 18, .9); }
.map-shell.dragover .drop-inner { border-color: var(--accent); background: rgba(59,138,255,.12); }

@media (max-width: 820px) {
  .header-actions .file-btn span { display: none; }
  .file-btn { padding: 9px; }
}

/* Capture row with inline remove button (static version) */
.capture-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.capture-row .capture-name { flex: 1; }
.capture-remove {
  flex: 0 0 auto;
  width: 20px; height: 20px; padding: 0;
  display: grid; place-items: center;
  background: transparent; color: var(--text-faint);
  border-radius: var(--r-sm); font-size: 1.1rem; line-height: 1; cursor: pointer;
}
.capture-remove:hover { background: var(--surface-3); color: var(--danger-hover); }
.capture-item { cursor: pointer; }
