:root {
  --bg: #0f1419;
  --panel: rgba(18, 24, 32, 0.92);
  --text: #f2f5f8;
  --muted: #9aa7b5;
  --line: rgba(255, 255, 255, 0.12);
  --accent: #e91e8c;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
}

#app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 500;
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--accent);
  white-space: nowrap;
}

.filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  flex-wrap: wrap;
  min-width: 0;
}

.dd {
  position: relative;
  min-width: 0;
  flex: 1;
  max-width: 320px;
}

.dd-toggle {
  width: 100%;
  text-align: left;
  background: #1b2430;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  cursor: pointer;
}

.dd-toggle[aria-expanded="true"] {
  border-color: rgba(233, 30, 140, 0.45);
}

.dd-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  min-width: min(280px, 88vw);
  max-height: min(62vh, 420px);
  overflow: auto;
  background: #151c26;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  z-index: 600;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}

.dd-actions {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.dd-actions button {
  flex: 1;
  background: #243041;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 8px;
  font-size: 11px;
  cursor: pointer;
}

.dd-nets {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 6px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.dd-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 6px;
  font-size: 13px;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}

.dd-item:hover { background: rgba(255, 255, 255, 0.04); }

.dd-item input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  flex: 0 0 auto;
}

.dd-item .swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.admin-btn, #closeAdmin {
  border: 1px solid var(--line);
  background: #243041;
  color: var(--text);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
}

.admin-btn.hidden, .hidden { display: none !important; }

#map {
  flex: 1;
  width: 100%;
  min-height: 0;
}

.legend {
  position: absolute;
  left: 10px;
  bottom: calc(12px + var(--safe-bottom));
  z-index: 450;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 11px;
  max-width: min(280px, 70vw);
}

/* Fixed counts panel when zoomed in (replaces scattered cluster plaques) */
.viewport-panel {
  position: absolute;
  top: calc(58px + var(--safe-top));
  right: 10px;
  z-index: 450;
  background: rgba(16, 22, 30, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  padding: 7px 9px;
  font-size: 11px;
  line-height: 1.35;
  color: var(--text);
  max-width: min(200px, 46vw);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  pointer-events: none;
}
.viewport-panel-title {
  font-weight: 600;
  font-size: 10px;
  letter-spacing: 0.02em;
  color: var(--muted);
  margin-bottom: 4px;
  text-transform: uppercase;
}
.viewport-panel .pmc-cluster-row {
  white-space: nowrap;
}
.viewport-panel .pmc-cluster-foot {
  border-top-color: rgba(255, 255, 255, 0.1);
}

.legend.legend-dim {
  opacity: 0.55;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 14, 0.86);
  display: grid;
  place-items: center;
  z-index: 800;
}

.overlay .card {
  background: #18202a;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 24px;
  text-align: center;
  max-width: 320px;
}

.admin-panel {
  position: absolute;
  inset: 0;
  z-index: 700;
  background: #10161e;
  overflow: auto;
  padding: calc(12px + var(--safe-top)) 14px calc(20px + var(--safe-bottom));
}

.admin-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.table {
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  font-size: 12px;
}

.row {
  display: grid;
  grid-template-columns: 1.1fr 1.4fr 0.7fr;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}

.row:nth-child(odd) { background: rgba(255,255,255,0.03); }
.row.head { font-weight: 600; color: var(--muted); }

.leaflet-container { background: #0a0e14; }

.pmc-arrow {
  background: transparent !important;
  border: none !important;
}
.pmc-arrow span {
  display: block;
  width: 0;
  height: 0;
  border-left: 7px solid #e6b422;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  filter: drop-shadow(0 0 1px #000);
}

.pmc-seq {
  background: transparent !important;
  border: none !important;
}
.pmc-seq-badge {
  width: 34px;
  min-height: 38px;
  border-radius: 12px;
  background: #111820;
  color: #f5f7fa;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  border: 2.5px solid #e6b422;
  box-shadow: 0 2px 8px rgba(0,0,0,.45);
  line-height: 1.05;
  padding: 3px 0 4px;
}
.pmc-seq-primary {
  font-size: 14px;
  color: #fff;
}
.pmc-seq-unload {
  font-size: 11px;
  font-weight: 800;
  margin-top: 1px;
}
.pmc-seq-unload-empty {
  opacity: 0.35;
  color: #9aa3ad !important;
}
.pmc-cluster-simple {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(16, 22, 30, 0.78);
  border: 2px solid rgba(233, 30, 140, 0.65);
  color: #f2f5f8;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.legend-item .dot.seq {
  background: #e6b422;
  border-radius: 4px;
  color: #111;
  font-size: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.route-block {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
}
.stop-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}
.stop-actions button {
  background: #243041;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 6px;
  margin-left: 4px;
  cursor: pointer;
}

.legend-item .dot.occ {
  background: repeating-linear-gradient(
    45deg,
    rgba(139, 58, 58, 0.55),
    rgba(139, 58, 58, 0.55) 2px,
    rgba(255, 255, 255, 0.08) 2px,
    rgba(255, 255, 255, 0.08) 4px
  );
  border: 1px solid rgba(196, 92, 92, 0.5);
  border-radius: 2px;
}

/* Multi-network cluster card (not a single number) */
.marker-cluster {
  background: transparent !important;
}
.pmc-cluster {
  background: rgba(16, 22, 30, 0.52);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 6px 8px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.28);
  color: #f2f5f8;
  font-size: 11px;
  line-height: 1.35;
  white-space: nowrap;
  min-width: 108px;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}
.pmc-cluster-icon {
  background: transparent !important;
  border: none !important;
}
.pmc-cluster-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 2px 0;
}
.pmc-cluster-row .swatch {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: 0 0 auto;
}
.pmc-cluster-foot {
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  color: #c9b26b;
  font-weight: 600;
}

@media (orientation: portrait) and (max-width: 480px) {
  .topbar { gap: 8px; }
  .dd { max-width: none; width: 100%; flex-basis: 100%; order: 3; }
  .dd-menu {
    left: 0;
    right: 0;
    min-width: 0;
    width: 100%;
  }
  .brand { font-size: 15px; }
  .pmc-cluster { font-size: 10px; padding: 5px 7px; }
  .viewport-panel {
    top: calc(96px + var(--safe-top));
    right: 8px;
    font-size: 10px;
    padding: 6px 8px;
    max-width: min(180px, 48vw);
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .topbar { padding-top: 6px; padding-bottom: 6px; }
  .legend { font-size: 10px; padding: 6px 8px; }
  .viewport-panel {
    top: calc(48px + var(--safe-top));
    font-size: 10px;
    padding: 5px 7px;
  }
  .brand { font-size: 14px; }
  .dd-menu { max-height: 70vh; }
}
