:root {
  --bg: #05060c;
  --panel: rgba(16, 20, 34, 0.62);
  --panel-brd: rgba(120, 150, 220, 0.18);
  --txt: #e7ecff;
  --muted: #7e88ab;
  --accent: #4ad8ff;
  --orange: #ff8a3d;
  --edpnet: #4ad8ff;
  --faastic: #b78bff;
  --good: #36f1a3;
  --bad: #ff3b6b;
  --mono: "SF Mono", "JetBrains Mono", "Fira Code", ui-monospace, Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--txt);
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  z-index: 0;
  touch-action: none; /* let OrbitControls handle drag/pinch, don't scroll page */
}

/* radial vignette glow behind the scene */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(120% 90% at 50% -10%, rgba(74, 216, 255, 0.10), transparent 55%),
              radial-gradient(80% 70% at 50% 120%, rgba(183, 139, 255, 0.10), transparent 60%);
  z-index: 1;
}

/* ---- top bar ---- */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 22px;
  pointer-events: none;
}
.brand { display: flex; gap: 14px; align-items: center; }
.logo {
  font-size: 30px;
  line-height: 1;
  color: var(--accent);
  filter: drop-shadow(0 0 12px var(--accent));
  animation: spin 9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#topbar h1 {
  margin: 0;
  font-size: 19px;
  letter-spacing: 6px;
  font-weight: 700;
}
.sub { margin: 2px 0 0; font-size: 11px; letter-spacing: 1.5px; color: var(--muted); text-transform: uppercase; }

.status {
  display: flex;
  align-items: center;
  gap: 9px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  padding: 7px 14px;
  border-radius: 999px;
  backdrop-filter: blur(14px);
  pointer-events: auto;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); box-shadow: 0 0 0 transparent; transition: .3s; }
.dot.live { background: var(--good); box-shadow: 0 0 12px var(--good); }
.dot.dead { background: var(--bad); box-shadow: 0 0 12px var(--bad); }

/* mobile drawer toggle + backdrop (hidden on desktop) */
.menu-toggle {
  display: none;
  pointer-events: auto;
  font-size: 18px; line-height: 1;
  color: var(--txt); background: var(--panel);
  border: 1px solid var(--panel-brd); border-radius: 10px;
  width: 38px; height: 38px; cursor: pointer; backdrop-filter: blur(14px);
}
.sel-backdrop {
  position: fixed; inset: 0; z-index: 14;
  background: rgba(3, 5, 11, 0.6); backdrop-filter: blur(2px);
  opacity: 0; pointer-events: none; transition: opacity .3s ease;
}
.sel-backdrop.show { opacity: 1; pointer-events: auto; }

/* ---- probe selector ---- */
#selector {
  position: fixed;
  top: 76px; left: 22px;
  z-index: 4;
  width: 290px;
  max-height: calc(100vh - 150px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  border-radius: 14px;
  backdrop-filter: blur(16px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.45);
  overflow: hidden;
}
.sel-head { padding: 10px 11px; border-bottom: 1px solid var(--panel-brd); display: flex; flex-direction: column; gap: 8px; }
#sel-search {
  font-family: var(--mono); font-size: 12px; color: var(--txt);
  background: rgba(0,0,0,.32); border: 1px solid var(--panel-brd);
  border-radius: 8px; padding: 6px 9px; outline: none;
}
#sel-search:focus { border-color: var(--accent); }
.sel-tabs { display: flex; gap: 5px; }
.sel-tab {
  flex: 1; font-family: var(--mono); font-size: 11px; color: var(--muted);
  background: rgba(120,150,220,.10); border: 1px solid transparent;
  padding: 5px 0; border-radius: 7px; cursor: pointer; text-transform: uppercase;
}
.sel-tab:hover { color: var(--txt); }
.sel-tab.on { color: #04121a; background: var(--accent); font-weight: 700; }
.sel-bar { display: flex; justify-content: space-between; align-items: center; font-family: var(--mono); font-size: 11px; color: var(--muted); }
#sel-clear { font-family: var(--mono); font-size: 11px; color: var(--muted); background: none; border: 1px solid var(--panel-brd); border-radius: 6px; padding: 3px 9px; cursor: pointer; }
#sel-clear:hover { color: var(--bad); border-color: var(--bad); }

#sel-list { overflow-y: auto; padding: 6px 6px 10px; }
#sel-list::-webkit-scrollbar { width: 8px; }
#sel-list::-webkit-scrollbar-thumb { background: rgba(120,150,220,.25); border-radius: 8px; }
.sel-type-h { font-family: var(--mono); font-size: 11px; font-weight: 700; letter-spacing: 1px; color: var(--c, var(--accent)); padding: 8px 6px 3px; display: flex; align-items: center; gap: 6px; }
.sel-type-n { color: var(--muted); font-weight: 400; }
.sel-grp-h { font-family: var(--mono); font-size: 10.5px; color: var(--muted); padding: 4px 6px 2px; }
.sel-row {
  display: flex; align-items: center; gap: 7px;
  padding: 3px 6px; border-radius: 7px; font-size: 11.5px;
  border-left: 2px solid transparent;
}
.sel-row:hover { background: rgba(120,150,220,.10); }
.sel-row.on { border-left-color: var(--c); background: rgba(120,150,220,.06); }
.sel-cb { accent-color: var(--c); cursor: pointer; flex: none; }
.sel-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c); box-shadow: 0 0 7px var(--c); flex: none; }
.sel-name { color: var(--txt); text-decoration: none; font-weight: 600; white-space: nowrap; }
.sel-name:hover { color: var(--c); text-decoration: underline; }
.sel-mtr {
  flex: none; font-family: var(--mono); font-size: 9px; letter-spacing: .5px;
  text-transform: uppercase; color: var(--muted); cursor: pointer;
  background: rgba(120,150,220,.12); border: 1px solid var(--panel-brd);
  border-radius: 5px; padding: 1px 5px; line-height: 1.4;
}
.sel-mtr:hover { color: #04121a; background: var(--c); border-color: var(--c); }
.sel-loc { color: var(--muted); font-family: var(--mono); font-size: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.sel-ms { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-left: auto; flex: none; min-width: 38px; text-align: right; }
.sel-loss { font-family: var(--mono); font-size: 10px; color: var(--bad); flex: none; min-width: 30px; text-align: right; }

/* ---- time machine (top centre) ---- */
#timemachine {
  position: fixed;
  top: 16px; left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  border-radius: 999px;
  backdrop-filter: blur(16px);
  box-shadow: 0 10px 36px rgba(0,0,0,.45);
  font-family: var(--mono);
}
.tm {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  padding: 5px 11px;
  border-radius: 999px;
  cursor: pointer;
  transition: .15s;
  white-space: nowrap;
}
.tm:hover:not(:disabled) { color: var(--txt); background: rgba(120,150,220,.12); }
.tm.on { color: #04121a; background: var(--accent); box-shadow: 0 0 16px rgba(74,216,255,.5); font-weight: 700; }
.tm.live.on { background: var(--good); box-shadow: 0 0 16px rgba(54,241,163,.5); }
.tm.nav { padding: 5px 9px; }
.tm:disabled { opacity: .35; cursor: default; }
.tm-sep { width: 1px; height: 18px; background: var(--panel-brd); margin: 0 3px; }
.tm-label { font-size: 11px; color: var(--muted); padding: 0 8px; min-width: 150px; }

/* ---- HUD bottom-left ---- */
#hud {
  position: fixed;
  left: 22px; bottom: 18px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}
.hud-row { display: flex; align-items: center; gap: 10px; }
.hud-key {
  background: rgba(120,150,220,.14);
  border: 1px solid var(--panel-brd);
  padding: 2px 7px;
  border-radius: 6px;
  color: var(--txt);
}
.controls { gap: 8px; }
.toggle {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  padding: 6px 11px;
  border-radius: 8px;
  cursor: pointer;
  backdrop-filter: blur(10px);
  transition: .2s;
}
.toggle:hover { color: var(--txt); }
.toggle.on { color: #04121a; background: var(--accent); border-color: var(--accent); box-shadow: 0 0 16px rgba(74,216,255,.5); }
.range { display: flex; align-items: center; gap: 7px; }
.range input { accent-color: var(--accent); width: 96px; }

/* ---- MTR launcher (bottom-right) ---- */
#mtrpanel {
  position: fixed;
  right: 22px; bottom: 18px;
  z-index: 4;
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  border-radius: 14px;
  backdrop-filter: blur(16px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  overflow: hidden;
}
.mtr-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.mtr-title { font-family: var(--mono); font-size: 12px; letter-spacing: 2px; color: var(--accent); text-transform: uppercase; }
#mtr-form { display: flex; gap: 7px; }
#mtr-ip {
  font-family: var(--mono);
  font-size: 12px;
  width: 168px;
  color: var(--txt);
  background: rgba(0,0,0,.32);
  border: 1px solid var(--panel-brd);
  border-radius: 8px;
  padding: 6px 9px;
  outline: none;
}
#mtr-ip:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(74,216,255,.18); }
#mtr-go {
  font-family: var(--mono);
  font-size: 12px;
  color: #04121a;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  padding: 6px 13px;
  cursor: pointer;
  font-weight: 700;
  white-space: nowrap;
}
#mtr-go:disabled { opacity: .5; cursor: progress; }

/* vantage chips (community agents) */
.mtr-vantages { display: flex; flex-wrap: wrap; gap: 5px; padding: 0 12px 11px; max-width: 360px; }
.mtr-vantages .vh { width: 100%; font-family: var(--mono); font-size: 10px; color: var(--muted); letter-spacing: .5px; padding: 2px 0; }
.vchip {
  font-family: var(--mono); font-size: 10.5px; color: var(--muted);
  background: rgba(120,150,220,.10); border: 1px solid var(--panel-brd);
  border-radius: 999px; padding: 3px 9px; cursor: pointer; white-space: nowrap; transition: .15s;
}
.vchip:hover { color: var(--txt); }
.vchip.on { color: #04121a; background: var(--c, var(--accent)); border-color: var(--c, var(--accent)); font-weight: 700; }
.vchip.fixed { cursor: default; color: #04121a; background: var(--accent); border-color: var(--accent); font-weight: 700; }

/* ---- MTR stage (full-screen latency map) ---- */
.mtr-stage {
  position: fixed;
  inset: 0;
  z-index: 8;
  background:
    radial-gradient(120% 80% at 50% 0%, rgba(20,26,46,0.55), transparent 60%),
    rgba(5,7,13,0.92);
  backdrop-filter: blur(12px);
  transition: opacity .35s ease;
}
.mtr-stage.hidden { opacity: 0; pointer-events: none; }
.stage-head {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  pointer-events: none;
}
.stage-title { font-family: var(--mono); font-size: 14px; letter-spacing: 1px; color: var(--txt); display: flex; align-items: center; gap: 9px; }
.stage-title .arrow { color: var(--accent); }
.stage-title b { color: var(--accent); }
.stage-meta { color: var(--muted); font-size: 11px; margin-left: 6px; }
.pulse-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--good); box-shadow: 0 0 12px var(--good); animation: blink 1.1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .25; } }
.stage-close {
  pointer-events: auto;
  font-family: var(--mono); font-size: 14px;
  color: var(--muted); background: rgba(120,150,220,.12);
  border: 1px solid var(--panel-brd); border-radius: 8px;
  width: 30px; height: 30px; cursor: pointer; transition: .15s;
}
.stage-close:hover { color: var(--bad); border-color: var(--bad); }
.stage-right { display: flex; align-items: center; gap: 10px; pointer-events: auto; }
.stage-view { display: flex; gap: 0; background: rgba(120,150,220,.10); border: 1px solid var(--panel-brd); border-radius: 8px; overflow: hidden; }
.sv { font-family: var(--mono); font-size: 11px; color: var(--muted); background: transparent; border: none; padding: 5px 11px; cursor: pointer; transition: .15s; }
.sv:hover { color: var(--txt); }
.sv.on { color: #04121a; background: var(--accent); font-weight: 700; }
.stage-share {
  pointer-events: auto; font-family: var(--mono); font-size: 12px; color: var(--txt);
  background: rgba(120,150,220,.12); border: 1px solid var(--panel-brd); border-radius: 8px;
  padding: 5px 12px; cursor: pointer; transition: .15s; white-space: nowrap;
}
.stage-share:hover { color: #04121a; background: var(--accent); border-color: var(--accent); }
.stage-share.copied { color: #04121a; background: var(--good); border-color: var(--good); }
#mtr-canvas { width: 100%; height: 100%; display: block; cursor: crosshair; touch-action: none; }

/* full-screen takeover: hide the rest of the UI while the latency map is up */
body.mtr-open #hud,
body.mtr-open #mtrpanel,
body.mtr-open #selector,
body.mtr-open #topbar,
body.mtr-open #timemachine { opacity: 0; pointer-events: none; transition: opacity .3s ease; }

/* hover detail card */
.mtr-tip {
  position: fixed;
  z-index: 9;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 16px));
  min-width: 200px;
  background: rgba(10,13,24,0.96);
  border: 1px solid var(--c, var(--accent));
  border-radius: 11px;
  padding: 10px 13px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.55;
  box-shadow: 0 12px 40px rgba(0,0,0,0.65);
  white-space: nowrap;
}
.mtr-tip.hidden { display: none; }
.mtr-tip .tip-head { display: flex; justify-content: space-between; gap: 16px; font-weight: 700; margin-bottom: 4px; }
.mtr-tip .tip-rt { color: var(--c, var(--accent)); }
.mtr-tip .tip-name { color: var(--txt); }
.mtr-tip .tip-as { color: #7ab0ff; }
.mtr-tip .tip-row { color: var(--muted); }
.mtr-tip .tip-row b { color: var(--txt); font-weight: 600; }
.mtr-tip .tip-loss { color: var(--bad); }

/* ---- floating readout on hover ---- */
.readout {
  position: fixed;
  z-index: 6;
  pointer-events: none;
  transform: translate(-50%, -130%);
  background: rgba(8, 10, 20, 0.92);
  border: 1px solid var(--c, var(--accent));
  border-radius: 10px;
  padding: 8px 11px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.5;
  box-shadow: 0 8px 26px rgba(0,0,0,.6);
  white-space: nowrap;
}
.readout .rt { color: var(--c, var(--accent)); font-weight: 700; }
.readout.hidden { display: none; }

/* ---- mobile / narrow screens ---- */
@media (max-width: 720px) {
  .menu-toggle { display: flex; align-items: center; justify-content: center; }
  #topbar { padding: 10px 12px; gap: 10px; }
  .brand .sub { display: none; }
  #topbar h1 { font-size: 15px; letter-spacing: 3px; }
  .logo { font-size: 22px; }
  .status { padding: 6px 11px; font-size: 11px; }

  /* selector becomes a slide-in drawer */
  #selector {
    top: 0; left: 0; bottom: 0;
    width: 86vw; max-width: 340px; max-height: 100vh; height: 100%;
    border-radius: 0 16px 16px 0;
    transform: translateX(-100%);
    transition: transform .3s cubic-bezier(.16,1,.3,1);
    z-index: 15;
  }
  #selector.open { transform: none; }

  /* time machine: compact, scrollable, under the top bar */
  #timemachine {
    top: 56px; left: 10px; right: 10px;
    transform: none;
    max-width: none;
    justify-content: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
    padding: 5px 7px;
  }
  #timemachine::-webkit-scrollbar { display: none; }
  .tm { padding: 6px 10px; }       /* bigger touch targets */
  .tm-label { display: none; }

  #hud { display: none; }

  /* MTR launcher: full-width bottom bar */
  #mtrpanel { left: 10px; right: 10px; bottom: 10px; }
  #mtrpanel .mtr-head { width: 100%; }
  #mtr-form { flex: 1; }
  #mtr-ip { flex: 1; width: auto; }

  .mtr-stage .stage-head { padding: 10px 14px; }
  .stage-title { font-size: 12px; }
  .stage-meta { display: none; }
  .readout, .mtr-tip { font-size: 10.5px; }
}

/* ============================ Globe view ============================ */
.globe-canvas {
  position: fixed; inset: 0; width: 100vw; height: 100vh;
  display: block; z-index: 2; touch-action: none;
}
.globe-labels {
  position: fixed; inset: 0; z-index: 3; pointer-events: none; overflow: hidden;
}
.globe-label {
  position: absolute; top: 0; left: 0; pointer-events: auto; cursor: pointer;
  display: flex; align-items: center; gap: 5px; white-space: nowrap;
  padding: 3px 8px; border-radius: 999px;
  background: rgba(8, 12, 24, 0.74); border: 1px solid color-mix(in srgb, var(--c) 70%, transparent);
  box-shadow: 0 0 12px color-mix(in srgb, var(--c) 35%, transparent);
  font: 600 11px/1 ui-monospace, Menlo, monospace; color: #eaf0ff;
  backdrop-filter: blur(3px); transition: transform .05s linear, box-shadow .2s ease;
}
.globe-label b { color: var(--c); font-weight: 700; }
.globe-label i { color: #ff6b8b; font-style: normal; font-size: 10px; }
.globe-label .gl-name { color: var(--muted); font-weight: 500; font-size: 10px; max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
.globe-label.hot { box-shadow: 0 0 18px color-mix(in srgb, var(--c) 60%, transparent); z-index: 5; }
.globe-label.hot .gl-name { color: #cdd7f5; }

/* view switcher in the top bar */
.viewswitch { pointer-events: auto; flex: 0 0 auto; display: flex; gap: 4px; padding: 4px;
  background: rgba(16, 22, 40, 0.72); border: 1px solid rgba(120, 140, 200, 0.32);
  border-radius: 999px; backdrop-filter: blur(6px); box-shadow: 0 4px 18px rgba(0,0,0,0.35); }
.vbtn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  font: 600 12px/1 ui-monospace, Menlo, monospace; letter-spacing: .4px;
  padding: 7px 13px; border-radius: 999px; transition: background .2s, color .2s;
}
.vbtn:hover { color: #dfe7ff; }
.vbtn.on { background: rgba(74, 216, 255, 0.16); color: var(--accent); box-shadow: inset 0 0 0 1px rgba(74,216,255,.35); }

/* globe legend */
.globe-hud {
  position: fixed; left: 22px; bottom: 22px; z-index: 4;
  padding: 12px 14px; border-radius: 14px;
  background: rgba(10, 14, 26, 0.66); border: 1px solid rgba(120, 140, 200, 0.16);
  backdrop-filter: blur(8px); font: 500 11.5px/1.5 ui-monospace, Menlo, monospace; color: var(--muted);
}
.globe-hud .gh-row { display: flex; align-items: center; gap: 8px; }
.globe-hud .gh-row + .gh-row { margin-top: 6px; }
.globe-hud .gh-grad { width: 46px; height: 7px; border-radius: 4px;
  background: linear-gradient(90deg, #36f1a3, #ffd23f, #ff3b6b); }
.globe-hud .gh-w { width: 46px; height: 0; border-top: 3px solid #6f86c0;
  border-image: linear-gradient(90deg, rgba(111,134,192,.4) 0 2px, #aebbe0) 1; }
.globe-hud .gh-tip { color: #aeb9da; margin-top: 8px; }

/* in globe view, hide the graph-only controls */
body.globe-view #hud,
body.globe-view #timemachine { opacity: 0; pointer-events: none; }
body.mtr-open .globe-canvas, body.mtr-open .globe-labels, body.mtr-open .globe-hud { opacity: 0; pointer-events: none; transition: opacity .3s ease; }

@media (max-width: 720px) {
  .viewswitch { padding: 3px; }
  .vbtn { padding: 6px 9px; font-size: 11px; }
  .globe-hud { left: 12px; bottom: 84px; font-size: 10.5px; padding: 9px 11px; }
  .globe-label .gl-name { display: none; }
}
