:root {
  --bg: #090a0c;
  --ink: #ebe6dc;
  --dim: #8f897e;
  --line: rgba(235, 230, 220, 0.12);
  --tokyo: #8fd8ff;
  --hk: #e2a15a;
  --hot: #e07a5f;
  --live: #b6f3c4;
  --panel: #101114;
  color-scheme: dark;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  height: 100%;
  overflow: hidden;
}
button, summary, input, select { font: inherit; color: inherit; }
button, summary, a { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:focus-visible, summary:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--tokyo);
  outline-offset: 2px;
}
#app { height: 100%; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
#gate { height: 100%; }
#gate[hidden], #app[hidden] { display: none !important; }

#gate {
  min-height: 100%;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}
.gate-card {
  width: min(22rem, 100%);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  background:
    linear-gradient(180deg, rgba(143, 216, 255, 0.08), transparent 46%),
    var(--panel);
  border: 1px solid rgba(235, 230, 220, 0.16);
  border-radius: 28px;
  padding: 32px 28px 26px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.05),
    0 28px 70px rgba(0, 0, 0, 0.42);
}
.gate-card h1 {
  margin: 8px 0 0;
  font-size: 44px;
  line-height: 0.95;
  letter-spacing: -0.045em;
}
.gate-card .lede { margin: 14px 0 0; }
.gate-card .lede[hidden] { display: none; }
#gate-action, .text-button, .filters button, .tools button {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
}
#gate-action {
  align-self: flex-start;
  margin-top: 28px;
  background: var(--ink);
  color: #16140f;
  border: 0;
  font-weight: 650;
  padding: 13px 22px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
}
.gate-card .lede:not([hidden]) ~ #gate-action { margin-top: 20px; }
#gate-action:disabled { opacity: 0.55; cursor: wait; box-shadow: none; }
.setup-label { display: block; margin-top: 14px; color: var(--dim); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
#setup-code {
  display: block;
  width: 100%;
  margin: 8px 0 14px;
  min-height: 44px;
  font-size: 16px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #0c0d10;
}
#setup-code[hidden], .setup-label[hidden] { display: none; }
.gate-error { color: var(--hot); margin: 16px 0 0; font-size: 14px; }
.gate-error:empty { display: none; }
.fine, .mapped { color: var(--dim); font-size: 12px; line-height: 1.45; }
header, footer {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 8px 16px 4px;
  flex: none;
}
footer {
  align-items: center;
  color: var(--dim);
  font-size: 12px;
  border-top: 1px solid var(--line);
  padding: 6px 16px 8px;
}
footer a { color: var(--dim); }
.head-side { display: flex; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; max-width: 760px; }
.kicker {
  margin: 0;
  color: var(--dim);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 11px;
}
h1 { margin: 2px 0 0; font-size: 22px; font-weight: 600; letter-spacing: -0.03em; }
h2 { margin: 0 0 10px; font-size: 13px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--dim); font-weight: 600; }
.status { margin: 0; color: var(--dim); font-variant-numeric: tabular-nums; font-size: 13px; text-align: right; }
.status.bad { color: var(--hot); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.chip {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  border: 1px solid rgba(235, 230, 220, 0.28);
  background: #17191e;
  border-radius: 999px;
  padding: 5px 10px 4px;
  font-size: 13px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.chip strong { color: var(--ink); font-weight: 650; }
.chip small {
  color: #b7b1a6;
  font-size: 10px;
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.chip.live {
  border-color: rgba(182, 243, 196, 0.55);
  background: rgba(182, 243, 196, 0.1);
}
.chip.live strong { color: var(--live); }

main {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 320px;
  gap: 8px;
  padding: 4px 12px 8px;
}
.rail {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 14px 10px;
  overflow: auto;
  min-height: 0;
}
.lede { color: var(--dim); font-size: 13px; line-height: 1.4; margin: 8px 0 12px; }
.bill .lede { margin: 4px 0 8px; }
.bill .spaced { margin-top: 14px; }
.spaced { margin-top: 22px; }
#search {
  width: 100%;
  margin-top: 0;
  background: #0c0d10;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
}
.filters { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; }
.filters button.on, .tools button.on {
  background: rgba(143, 216, 255, 0.16);
  border-color: rgba(143, 216, 255, 0.5);
  color: var(--ink);
}
.stage {
  position: relative;
  min-height: 0;
  height: 100%;
  background: #07090c;
  container-type: inline-size;
}
#globe { position: absolute; inset: 0; cursor: grab; touch-action: none; }
#globe:active { cursor: grabbing; }
#globe canvas { width: 100%; height: 100%; display: block; }
.globe-label {
  position: absolute;
  z-index: 3;
  pointer-events: none;
  padding: 2px 7px;
  border-radius: 7px;
  border: 1px solid rgba(214, 236, 255, 0.45);
  background: rgba(5, 10, 16, 0.78);
  font: 600 12px "Avenir Next", "Segoe UI", sans-serif;
  color: #f4f7fb;
  white-space: nowrap;
  transform: translate(0, -50%);
}
.globe-label[hidden] { display: none !important; }
.legend, .home-note, .tools, .empty-globe {
  position: absolute;
  color: var(--dim);
  font-size: 12px;
}
.legend { top: 12px; left: 16px; display: flex; gap: 14px; align-items: center; flex-wrap: wrap; max-width: 62%; padding: 6px 10px; border-radius: 999px; background: rgba(5, 8, 12, 0.62); }
.tools { top: 12px; right: 14px; display: flex; gap: 6px; padding: 4px; border-radius: 999px; background: rgba(5, 8, 12, 0.55); }
.home-note {
  right: 16px;
  left: auto;
  bottom: 36px;
  max-width: 280px;
  text-align: right;
  line-height: 1.4;
  color: var(--hk);
}
.empty-globe {
  left: 50%;
  top: 58%;
  transform: translate(-50%, -50%);
  max-width: 280px;
  text-align: center;
  line-height: 1.4;
  background: rgba(9, 10, 12, 0.78);
  border-radius: 12px;
  padding: 12px 14px;
  pointer-events: none;
}
.cable-card {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 4;
  width: min(248px, calc(100% - 16px));
  pointer-events: none;
  background: rgba(8, 12, 16, 0.94);
  border: 1px solid rgba(214, 226, 216, 0.22);
  border-radius: 14px;
  padding: 12px 12px 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}
.cable-card[hidden] { display: none !important; }
.cable-card h3 { margin: 2px 0 0; font-size: 18px; letter-spacing: -0.03em; }
.cable-pip {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 6px;
  vertical-align: baseline;
}
.cable-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 7px 10px; margin: 10px 0 8px; }
.cable-stats div { min-width: 0; }
.cable-stats .wide { grid-column: 1 / -1; }
.cable-stats dt { color: var(--dim); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; }
.cable-stats dd { margin: 1px 0 0; font-variant-numeric: tabular-nums; font-size: 13px; overflow-wrap: anywhere; }
.cable-flows { list-style: none; margin: 0; padding: 8px 0 0; border-top: 1px solid var(--line); }
.cable-flows li { color: var(--dim); font-size: 12px; line-height: 1.45; font-variant-numeric: tabular-nums; }

.legend-button {
  background: transparent;
  border: 0;
  padding: 0;
  display: inline-flex;
  gap: 6px;
  align-items: center;
  cursor: pointer;
  color: inherit;
}
.swatch { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.swatch.tokyo { background: var(--tokyo); }
.swatch.hk { background: var(--hk); }
.swatch.live { background: var(--live); }
.swatch.out { background: #f4fbff; }
.swatch.in { background: var(--live); }
.peer { margin: 0 0 12px; }
.peer button.name {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 4px;
  cursor: pointer;
  font-size: 14px;
}
.peer button.name.on, .peer button.name:hover { background: rgba(143, 216, 255, 0.08); }
.peer .bytes { color: var(--dim); font-variant-numeric: tabular-nums; font-size: 12px; }
.bar { margin: 4px 4px 0; height: 4px; background: #232226; border-radius: 4px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--tokyo); }
.bar.port span { background: #d5c4a1; }
.list { display: flex; flex-direction: column; gap: 4px; }
.place, .flow, .port-row, .event {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 8px 8px;
  cursor: pointer;
}
.place {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 12px;
  align-items: start;
}
.place strong { font-weight: 600; line-height: 1.3; }
.place em, .flow small { font-style: normal; color: var(--dim); font-variant-numeric: tabular-nums; }
.place em { white-space: nowrap; text-align: right; padding-top: 1px; }
.place.on, .place:hover, .flow:hover, .port-row:hover { background: rgba(143, 216, 255, 0.08); }
.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--live);
  box-shadow: 0 0 8px var(--live);
  display: inline-block;
  margin-right: 6px;
}
.flow, .event { display: block; color: var(--dim); font-size: 12px; cursor: default; }
.event { border-bottom: 1px solid var(--line); border-radius: 0; }
.port-row { display: grid; grid-template-columns: 88px 1fr auto; gap: 8px; align-items: center; font-size: 13px; }
.detail { margin-top: 8px; }
.ring-wrap { position: relative; width: 168px; margin: 4px auto 2px; }
.ring { width: 100%; height: auto; transform: rotate(-90deg); }
.track, .value { fill: none; stroke-width: 8; }
.track { stroke: #26241f; }
.value { stroke: var(--tokyo); stroke-linecap: round; stroke-dasharray: 0 427; }
.value.hot { stroke: var(--hot); }
.ring-label {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ring-label strong { font-size: 32px; letter-spacing: -0.04em; font-variant-numeric: tabular-nums; }
.ring-label span { color: var(--dim); font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; }
.figures { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin: 4px 0 8px; }
.figures div { min-width: 0; }
.figures dt { color: var(--dim); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.figures dd { margin: 2px 0 0; font-variant-numeric: tabular-nums; font-size: 15px; }
.spark { width: 100%; height: 52px; display: block; }
details { color: var(--dim); font-size: 13px; line-height: 1.45; }
summary { cursor: pointer; color: var(--ink); }
.empty { color: var(--dim); font-size: 13px; line-height: 1.4; }
.dock { display: none; }
.dock button {
  border: 0;
  background: transparent;
  color: var(--dim);
  border-radius: 14px;
  min-height: 46px;
  padding: 6px 8px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
}
.dock button.on {
  color: var(--ink);
  background: rgba(143, 216, 255, 0.14);
}
.dock-glyph {
  width: 16px;
  height: 16px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  display: block;
}
.dock button[data-pane="places"] .dock-glyph { border-radius: 3px; height: 13px; }
.dock button[data-pane="bill"] .dock-glyph { box-shadow: inset 0 0 0 3px rgba(143, 216, 255, 0.45); }

/* A tablet keeps the globe and one reading column. The phone uses the dock as full pages. */
@media (min-width: 760px) and (max-width: 1179px) {
  #app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) clamp(320px, 36vw, 400px);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
  }
  header { grid-column: 1 / -1; grid-row: 1; flex-wrap: wrap; align-items: flex-start; }
  .head-side { max-width: none; flex: 1 1 320px; justify-content: flex-start; }
  .status, .chips { text-align: left; justify-content: flex-start; }
  main { display: contents; }
  .stage { grid-column: 1; grid-row: 2 / span 2; height: auto; }
  .dock {
    display: flex;
    grid-column: 2;
    grid-row: 2;
    gap: 4px;
    margin: 10px 12px 0;
    padding: 4px;
    background: rgba(16, 17, 20, 0.94);
    border: 1px solid var(--line);
    border-radius: 999px;
  }
  .dock button { flex: 1; min-height: 40px; flex-direction: row; gap: 8px; border-radius: 999px; }
  .dock button[data-pane="map"] { display: none; }
  #app[data-pane="map"] .dock button[data-pane="places"] {
    color: var(--ink);
    background: rgba(143, 216, 255, 0.14);
  }
  .rail:not(.bill), .bill {
    grid-column: 2;
    grid-row: 3;
    min-height: 0;
    align-self: start;
    height: calc(100% - 16px);
    margin: 8px 12px 0 0;
  }
  .filters { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .filters::-webkit-scrollbar { display: none; }
  .filters button { flex: none; }
  #app:not([data-pane="bill"]) .bill { display: none; }
  #app[data-pane="bill"] .rail:not(.bill) { display: none; }
  footer { grid-column: 1 / -1; grid-row: 4; }
  .legend { max-width: calc(100% - 210px); }
}

@media (max-width: 759px) {
  html, body, #app {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }
  #app {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 6px 8px;
    padding: 8px 14px 8px;
  }
  .brand { grid-column: 1; grid-row: 1; min-width: 0; }
  .brand h1 { font-size: 22px; }
  .head-side { display: contents; }
  .status {
    grid-column: 1 / -1;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #lock { grid-column: 2; grid-row: 1; min-height: 40px; justify-self: end; }
  .chips {
    grid-column: 1 / -1;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 1px;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; padding: 6px 11px; }
  main {
    display: block;
    padding: 0;
    overflow: hidden;
  }
  .stage, .rail { display: none; height: 100%; border-radius: 0; }
  .rail { border: 0; overflow: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 16px 16px 24px; }
  #app[data-pane="map"] .stage { display: block; }
  #app[data-pane="places"] .rail:not(.bill) { display: block; }
  #app[data-pane="bill"] .bill { display: block; }
  #app[data-pane="map"] footer,
  #app[data-pane="places"] footer { display: none; }
  #app[data-pane="bill"] footer {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    flex: none;
  }
  .dock {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    flex: none;
    padding: 6px 10px calc(8px + env(safe-area-inset-bottom));
    background: rgba(9, 10, 12, 0.94);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(18px);
  }
  .legend {
    top: 10px;
    left: 10px;
    right: 10px;
    max-width: none;
    border-radius: 16px;
    padding: 8px 10px;
    gap: 10px 14px;
  }
  .tools {
    top: auto;
    bottom: 12px;
    left: 10px;
    right: 10px;
    justify-content: center;
    z-index: 3;
  }
  .tools button, .filters button, .place, .peer button.name, #search, #gate-action {
    min-height: 44px;
  }
  #search { font-size: 16px; padding: 12px 12px; }
  .filters { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  .filters::-webkit-scrollbar { display: none; }
  .filters button { flex: none; }
  .home-note {
    top: 58px;
    right: 12px;
    bottom: auto;
    left: 12px;
    max-width: none;
    text-align: left;
    background: rgba(8, 12, 16, 0.92);
    border-radius: 12px;
    padding: 10px 12px;
  }
  .empty-globe { top: 46%; max-width: min(280px, calc(100% - 32px)); }
  .cable-card {
    width: calc(100% - 20px);
    max-height: 42%;
    overflow: auto;
    pointer-events: auto;
  }
  .gate-card { width: min(22rem, 100%); padding: 28px 22px 22px; border-radius: 24px; }
  .gate-card h1 { font-size: 40px; }
  #gate-action { width: 100%; align-self: stretch; }
  .ring-wrap { width: min(210px, 70vw); }
  .place, .port-row, .event, .peer button.name { font-size: 16px; }
  .event { padding-top: 12px; padding-bottom: 12px; }
}

@media (max-width: 759px) and (max-height: 480px) {
  .chips { display: none; }
  header { padding-top: 4px; padding-bottom: 4px; }
  .dock button { min-height: 40px; flex-direction: row; gap: 6px; }
  .legend { display: none; }
  #app[data-pane="bill"] footer { display: none; }
}

@media (max-height: 520px) {
  footer { display: none; }
}

/* When the map column is narrow, the key and the buttons cannot share the top edge. */
@container (max-width: 720px) {
  .legend {
    top: 10px;
    left: 10px;
    right: 10px;
    max-width: none;
  }
  .tools {
    top: auto;
    bottom: 12px;
    left: 10px;
    right: 10px;
    justify-content: center;
    z-index: 3;
  }
  .home-note {
    top: 62px;
    right: 12px;
    bottom: auto;
    left: 12px;
    max-width: none;
    text-align: left;
  }
}
