/* Topology page: the device graph, its legend, and the table of address pairs */

/* The header is the Events header: the filter, time range and refresh controls on the left, the view tabs on the right */
.topo-header {
  flex-wrap: wrap;
}

.topo-stage {
  position: relative;
  display: flex;
  flex-direction: column;
  /* Until the script fits it to the window (fitStage) */
  height: 60vh;
  min-height: 22rem;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  background: var(--bg-surface);
  padding: 0.5rem;
}

.topo-svg {
  display: block;
  flex: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.topo-svg.is-panning {
  cursor: grabbing;
}

/* Zoom, Redraw and the view tabs share the header's right side, on one line */
.topo-tools {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* Zoom and Redraw: an overlay in the top-left corner of the graph. Faint until the pointer is on it, like the legend. */
.topo-controls {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.3rem;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  background: var(--bg-surface-elevated);
  opacity: 0.2;
  transition: opacity 0.2s;
  pointer-events: auto;
}

.topo-controls:hover {
  opacity: 0.8;
}

/* The four controls: small, and the glyphs stay uppercase as in the events bar. The global button rule
   sets padding and size with !important, so these need it too. */
.topo-controls .tool-btn {
  min-height: 0;
  margin: 0;
  padding: 0.2rem 0.5rem !important;
  font-size: 0.66rem !important;
  line-height: 1.4;
}

/* Reset and Redraw are words, shown as written */
.topo-controls .tool-btn.topo-word {
  text-transform: none;
}

/* Hover focus: the hovered device, the devices it links to and the links between them stay lit */
.topo-svg.is-focus .topo-node:not(.is-related),
.topo-svg.is-focus .topo-link:not(.is-related) {
  opacity: 0.12;
}

.topo-node.is-related,
.topo-link.is-related {
  opacity: 1;
}

.topo-node.is-related circle {
  stroke: var(--text-main);
}

.topo-node.is-pinned text {
  font-style: italic;
}

.topo-link {
  stroke: var(--text-subtle);
  stroke-linecap: round;
  opacity: 0.6;
}

.topo-link-hit {
  stroke: transparent;
  cursor: pointer;
}

.topo-node {
  cursor: pointer;
  outline: none;
}

.topo-node circle {
  fill: var(--chart-gray);
  stroke: var(--bg-surface);
  stroke-width: 1.5;
}

.topo-node.kind-asset circle {
  fill: var(--chart-green);
}

.topo-node.kind-external circle {
  fill: var(--chart-yellow);
}

.topo-node:hover circle,
.topo-node:focus-visible circle {
  stroke: var(--text-main);
}

.topo-node text {
  fill: var(--text-main);
  text-anchor: middle;
  pointer-events: none;
}

/* The device count and the time range, in the bottom-left corner of the graph */
.topo-note {
  position: absolute;
  left: 0.75rem;
  bottom: 0.75rem;
  z-index: 1;
  margin: 0;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  background: var(--bg-surface-elevated);
  font-size: 0.75rem;
  /* Keeps the spaces around the bullets between the parts of the note */
  white-space: pre-wrap;
  color: var(--text-muted);
  opacity: 0.2;
  transition: opacity 0.2s;
  pointer-events: auto;
}

.topo-note:hover {
  opacity: 0.8;
}

.topo-dot {
  display: inline-block;
  width: 0.6rem;
  height: 0.6rem;
  margin-right: 0.35rem;
  border-radius: 50%;
  background: var(--chart-gray);
}

.topo-dot.kind-asset {
  background: var(--chart-green);
}

.topo-dot.kind-external {
  background: var(--chart-yellow);
}

.topo-table-wrap {
  overflow-x: auto;
}

.topo-table {
  width: 100%;
  font-size: 0.85rem;
}

.topo-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* The cards over the graph: the selected device (top-right) and the legend (bottom-right). They share a width and
   let the pointer through, except for their own buttons and, for the legend, the card itself. */
.topo-card {
  position: absolute;
  right: 0.75rem;
  z-index: 1;
  width: 16rem;
  max-width: 55%;
  border: 1px solid var(--border-focus);
  border-radius: 6px;
  background: var(--bg-surface-elevated);
  font-size: 0.8rem;
  pointer-events: none;
}

.topo-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.45rem 0.65rem;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.topo-selected {
  top: 0.75rem;
}

.topo-selected .topo-card-body {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0;
  padding: 0.5rem 0.65rem;
}

.topo-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
}

.topo-row dt {
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.topo-row dd {
  flex: 1;
  margin: 0;
  text-align: right;
  overflow-wrap: anywhere;
}

/* The + and − buttons at the end of a row of the selection card, faint like the card */
.topo-filter-btns {
  display: flex;
  flex-shrink: 0;
  gap: 0.2rem;
}

.topo-filter-btn {
  margin: 0;
  padding: 0 0.2rem !important;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.2;
  text-transform: none;
  cursor: pointer;
}

.topo-filter-btn:hover:not(:disabled) {
  color: var(--text-main);
}

.topo-filter-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.topo-card-foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.45rem 0.65rem;
  border-top: 1px solid var(--border-subtle);
}

.topo-card-foot .topo-text-btn,
.topo-close {
  pointer-events: auto;
}

/* A plain text button: not uppercase, no box, and no margin so it sits flush with the card's edge */
.topo-text-btn {
  margin: 0;
  padding: 0.45rem 0 !important;
  border: none;
  background: none;
  color: var(--text-main);
  font-size: 0.8rem;
  text-transform: none;
  text-decoration: none;
  cursor: pointer;
}

.topo-close {
  margin: 0;
  padding: 0.45rem 0 !important;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
  text-transform: none;
  cursor: pointer;
}

.topo-close:hover {
  color: var(--text-main);
}

.topo-legend {
  bottom: 0.75rem;
  opacity: 0.2;
  transition: opacity 0.2s;
  pointer-events: auto;
}

.topo-legend:hover {
  opacity: 0.8;
}

.topo-legend-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin: 0;
  padding: 0.5rem 0.65rem;
  list-style: none;
}

.topo-legend-list li {
  display: flex;
  align-items: center;
}

/* The selection card is faint until hovered, like the other overlays */
.topo-selected {
  opacity: 0.5;
  transition: opacity 0.2s;
  pointer-events: auto;
}

.topo-selected:hover {
  opacity: 0.9;
}

/* A value that wraps onto lines of its own, each right-aligned under the last, as the packets and bytes of a link */
.topo-row dd.topo-lines {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.1rem;
}

/* A group of rows in the selection card, set off from the rows above it */
.topo-row.topo-rule {
  margin-top: 0.7rem;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border-subtle);
}

/* The device count: a small select at the start of the controls, sized like their buttons */
.topo-limit {
  width: auto;
  margin: 0;
  padding: 0.2rem 1.6rem 0.2rem 0.5rem;
  font-size: 0.66rem;
  line-height: 1.4;
}

/* The Sankey view: a band for each pair of devices, from the device that sent the packets to the one that received them.
   A band takes the colour of the device it leaves, as a device takes its own colour. */
.topo-sankey {
  cursor: default;
}

/* A thin band is a fraction of a pixel tall, so a transparent stroke of HIT_PX on its edge is what the pointer meets */
.sankey-link {
  fill: var(--chart-gray);
  fill-opacity: 0.3;
  stroke: transparent;
  stroke-width: 4px;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: fill-opacity 0.15s;
}

.sankey-link.kind-asset {
  fill: var(--chart-green);
}

.sankey-link.kind-external {
  fill: var(--chart-yellow);
}

.sankey-node {
  cursor: pointer;
  outline: none;
}

/* As the bands, a device too small to see is still hit by a transparent stroke */
.sankey-node rect,
.sankey-node path {
  fill: var(--chart-gray);
  stroke: transparent;
  stroke-width: 4px;
  vector-effect: non-scaling-stroke;
}

.sankey-node.kind-asset rect,
.sankey-node.kind-asset path {
  fill: var(--chart-green);
}

.sankey-node.kind-external rect,
.sankey-node.kind-external path {
  fill: var(--chart-yellow);
}

/* The radial view's names sit outside the ring, and the names of the devices a focus reaches take the accent colour */
.radial-label.is-related {
  fill: var(--ev-accent, #f59e0b);
}

.topo-radial.is-focus .radial-label:not(.is-related) {
  opacity: 0.12;
}

.topo-sankey text {
  font-size: 6px;
  fill: var(--text-main);
  pointer-events: none;
}

/* Hover and selection: the traffic that matches stays lit, the rest fades as on the graph */
.topo-sankey.is-focus .sankey-link:not(.is-related),
.topo-sankey.is-focus .sankey-node:not(.is-related) {
  opacity: 0.12;
}

.sankey-link.is-related {
  fill-opacity: 0.6;
}

/* The name of a device that the layout left unnamed, shown while the device or its band is hovered. The outline keeps it
   readable over the bands. */
/* The names of the devices that are highlighted (connected to a hovered or selected device) take the accent colour */
.topo-sankey .sankey-node.is-related text,
.topo-sankey .sankey-hover-label {
  fill: var(--ev-accent, #f59e0b);
}

.topo-sankey .sankey-hover-label {
  stroke: var(--bg-surface);
  stroke-width: 3px;
  paint-order: stroke;
  pointer-events: none;
}

.sankey-node.is-related rect,
.sankey-node.is-related path {
  stroke: var(--text-main);
  stroke-width: 1;
}

/* The graph type: a button that names the view shown, and a flyout with a row for each graph type */
.topo-view-button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  text-transform: none;
  letter-spacing: normal;
  font-size: 0.8rem;
  font-weight: 600;
}

.topo-view-button svg {
  flex: none;
}

.topo-view-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.topo-view-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.2rem;
  width: 100%;
  margin: 0;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--ev-rule-strong, #333);
  border-radius: 6px;
  background: transparent;
  color: var(--text-main, #e5e5e5);
  text-align: left;
  text-transform: none;
  letter-spacing: normal;
  cursor: pointer;
}

.topo-view-option:hover,
.topo-view-option:focus-visible {
  background: var(--bg-surface, #111);
}

.topo-view-option.is-current {
  border-color: var(--ev-accent, #f59e0b);
}

.topo-view-name {
  font-size: 0.9rem;
  font-weight: 600;
}

.topo-view-option.is-current .topo-view-name {
  color: var(--ev-accent, #f59e0b);
}

.topo-view-hint {
  font-size: 0.78rem;
  color: var(--text-muted, #888);
}

/* The map view: a Leaflet map in the stage, with the devices as circles and the traffic between them as arcs */
/* A stacking context of its own, so the map's panes stay under the note, the legend and the cards of the stage */
.topo-map.leaflet-container {
  position: relative;
  z-index: 0;
  flex: 1;
  width: 100%;
  min-height: 0;
  border-radius: 4px;
  background: var(--bg-surface-elevated);
  font: inherit;
}

.topo-map .leaflet-bar a {
  width: 1.9rem;
  height: 1.9rem;
  line-height: 1.9rem;
  background: var(--bg-surface-elevated);
  border-color: var(--border-subtle);
  color: var(--text-main);
}

.topo-map .leaflet-bar a:hover {
  background: var(--bg-surface);
}

/* The credit line stays readable, but fades into the map until the pointer is over it */

[data-theme="dark"] .topo-map .leaflet-tile-pane {
  filter: invert(1) hue-rotate(180deg) brightness(0.85) saturate(0.6);
}

.topo-map .topo-map-node {
  fill: var(--chart-gray);
  stroke: var(--bg-surface);
  stroke-width: 1.5;
  cursor: pointer;
}

/* Assets stand out from the amber arcs and the other devices */
.topo-map .topo-map-node.kind-asset {
  fill: #4ade80;
}

.topo-map .topo-map-node.kind-external {
  fill: var(--chart-yellow);
}

.topo-map .topo-map-node.is-related {
  stroke: var(--text-main);
}

/* The traffic arcs are the brand amber, which the roads and labels of the basemap do not use */
.topo-map .topo-map-arc {
  fill: none;
  stroke: #f59e0b;
  stroke-linecap: round;
  opacity: 0.55;
  cursor: pointer;
}

.topo-map .topo-map-accuracy {
  fill: var(--chart-gray);
  fill-opacity: 0.08;
  stroke: none;
}

/* The site: a dashed ring where the devices without a location sit, and a ringed point at its centre */
.topo-map .topo-map-ring {
  fill: none;
  stroke: var(--text-subtle);
  stroke-width: 1;
  stroke-dasharray: 4 4;
  opacity: 0.7;
}

.topo-map .topo-map-site {
  fill: var(--bg-surface);
  fill-opacity: 0.9;
  stroke: var(--text-main);
  stroke-width: 2;
}

/* Focus, as on the graph: the selected device or link and what it is joined to stay lit */
.topo-map.is-focus .topo-map-node:not(.is-related),
.topo-map.is-focus .topo-map-arc:not(.is-related) {
  opacity: 0.12;
}

.topo-map .is-related {
  opacity: 1;
}
