/* ===================================================================
   NETWORK GRAPH VISUAL HIERARCHY & POLISH
   - CSS Custom Properties for centralized theming
   - Class-based styling for nodes, edges, labels
   - Visual effects: shadows, glows, transitions
   - Threat level color coding
   =================================================================== */

/* ===========================
   CSS CUSTOM PROPERTIES
   =========================== */
:root {
  /* Node Type Colors */
  --network-color-adtech: #3b82f6;
  --network-color-ais: #10b981;
  --network-color-acled-event: #8b5cf6;
  --network-color-areas: #6b7280;
  --network-color-humint-report: #a0522d;
  --network-color-entities: #d2691e;
  --network-color-default: #9ca3af;

  /* Special Node States */
  --network-color-focal: #ef4444;
  --network-color-focal-stroke: #e8edf5;
  --network-color-selected: #fbbf24;
  --network-color-selected-stroke: #b45309;
  --network-color-hover: #60a5fa;

  /* Edge Colors */
  --network-edge-color: #353b67;
  --network-edge-color-highlighted: #3b82f6;
  --network-edge-color-hover: #60a5fa;
  --network-edge-color-neighbor: #f59e0b;

  /* Threat Level Colors */
  --network-threat-low: #10b981;
  --network-threat-medium: #f59e0b;
  --network-threat-high: #ef4444;
  --network-threat-critical: #dc2626;

  /* Node Sizes */
  --network-node-radius-base: 12px;
  --network-node-radius-focal: 18px;
  --network-node-stroke-base: 1.5px;
  --network-node-stroke-focal: 5px;
  --network-node-stroke-selected: 2.5px;

  /* Edge Widths */
  --network-edge-width-base: 2px;
  --network-edge-width-highlighted: 3px;
  --network-edge-width-hover: 2.5px;

  /* Typography */
  --network-label-font-size: var(--font-size-xs);
  --network-label-font-weight: 500;
  --network-label-color: white;
  --network-label-stroke: #000;
  --network-label-stroke-width: 1px;
  --network-edge-label-font-size: var(--font-size-sm);

  /* Visual Effects */
  --network-shadow-base: 0 2px 4px rgba(0, 0, 0, 0.1);
  --network-shadow-hover: 0 4px 8px rgba(0, 0, 0, 0.15);
  --network-shadow-focal: 0 4px 32px rgba(239, 68, 68, 0.5);
  --network-glow-focal: 0 0 60px rgba(239, 68, 68, 0.9);
  --network-glow-threat-high: 0 0 16px rgba(239, 68, 68, 0.6);
  --network-transition-duration: 300ms;
  --network-transition-easing: ease-out;

  /* Opacity Values */
  --network-edge-opacity-base: 0.6;
  --network-edge-opacity-highlighted: 1;
  --network-edge-opacity-dimmed: 0.2;
  --network-node-opacity-dimmed: 0.3;
}

/* ===========================
   BASE ELEMENTS
   =========================== */

/* SVG Container */
.network-graph svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* ===========================
   EDGES (LINKS)
   =========================== */

.link {
  stroke: var(--network-edge-color);
  stroke-width: var(--network-edge-width-base);
  opacity: var(--network-edge-opacity-base);
  transition:
    stroke var(--network-transition-duration) var(--network-transition-easing),
    stroke-width var(--network-transition-duration)
      var(--network-transition-easing),
    opacity var(--network-transition-duration) var(--network-transition-easing);
  fill: none;
}

.link--highlighted {
  stroke: var(--network-edge-color-highlighted);
  stroke-width: var(--network-edge-width-highlighted);
  opacity: var(--network-edge-opacity-highlighted);
}

.link--neighbor {
  stroke: var(--network-edge-color-neighbor);
  opacity: var(--network-edge-opacity-highlighted);
}

.link--dimmed {
  opacity: var(--network-edge-opacity-dimmed);
}

.link--hover {
  stroke: var(--network-edge-color-hover);
  stroke-width: var(--network-edge-width-hover);
  opacity: var(--network-edge-opacity-highlighted);
}

/* Edge Labels */
.link-label {
  font-size: var(--network-edge-label-font-size);
  fill: #000;
  font-weight: 100;
  cursor: pointer;
  transition:
    fill var(--network-transition-duration) var(--network-transition-easing),
    filter var(--network-transition-duration) var(--network-transition-easing);
  user-select: none;
  paint-order: stroke;
  stroke: #171616;
  stroke-width: 0.5px;
  stroke-linejoin: round;
  /* Center text on the line */
  text-anchor: middle;
  dominant-baseline: middle;
  opacity: 0.7;
}

/* Background circle for edge labels (hidden by default) */
.link-label-bg {
  pointer-events: none;
}

/* Show background circle on hover */
.link-label-group:hover .link-label-bg {
  fill: rgba(240, 240, 240, 0.9) !important;
  stroke: rgba(0, 102, 204, 0.3) !important;
}

.link-label:hover {
  fill: #0066cc;
  opacity: 1;
  font-weight: 400;
}

/* ===========================
   NODES
   =========================== */

.node {
  cursor: grab;
  pointer-events: all;
}

.node:active {
  cursor: grabbing;
}

.node--dragging {
  cursor: grabbing;
}

/* Node Circle - Base */
.node-circle {
  r: var(--network-node-radius-base);
  stroke-width: var(--network-node-stroke-base);
  filter: drop-shadow(var(--network-shadow-base));
  pointer-events: all;
}

/* Node hover effect */
.node:hover .node-circle {
  filter: drop-shadow(var(--network-shadow-hover));
  stroke-width: calc(var(--network-node-stroke-base) + 0.5px);
}

/* Node - Focal (original/center node) */
.node--focal .node-circle {
  r: var(--network-node-radius-focal);
  fill: var(--network-color-focal);
  stroke: var(--network-color-focal-stroke);
  stroke-width: var(--network-node-stroke-focal);
  filter: drop-shadow(var(--network-shadow-focal));
  animation: focal-pulse 2s ease-in-out infinite;
}

@keyframes focal-pulse {
  0%,
  100% {
    filter: drop-shadow(var(--network-shadow-focal));
  }
  50% {
    filter: drop-shadow(var(--network-glow-focal));
  }
}

/* Node - Selected */
.node--selected .node-circle {
  fill: var(--network-color-selected);
  stroke: var(--network-color-selected-stroke);
  stroke-width: var(--network-node-stroke-selected);
  stroke-dasharray: 4, 2;
  animation: selection-dash 1s linear infinite;
}

@keyframes selection-dash {
  to {
    stroke-dashoffset: -12;
  }
}

/* Node - Dimmed (when filtering) */
.node--dimmed {
  opacity: var(--network-node-opacity-dimmed);
  pointer-events: none;
}

.node--dimmed .node-circle {
  filter: none;
}

/* Node Type Colors */
.node--type-adtech .node-circle {
  fill: var(--network-color-adtech);
  stroke: color-mix(in srgb, var(--network-color-adtech) 70%, black);
}

.node--type-ais .node-circle {
  fill: var(--network-color-ais);
  stroke: color-mix(in srgb, var(--network-color-ais) 70%, black);
}

.node--type-acled-event .node-circle {
  fill: var(--network-color-acled-event);
  stroke: color-mix(in srgb, var(--network-color-acled-event) 70%, black);
}

.node--type-areas .node-circle {
  fill: var(--network-color-areas);
  stroke: color-mix(in srgb, var(--network-color-areas) 70%, black);
}

.node--type-humint-report .node-circle {
  fill: var(--network-color-humint-report);
  stroke: color-mix(in srgb, var(--network-color-humint-report) 70%, black);
}

.node--type-entities .node-circle {
  fill: var(--network-color-entities);
  stroke: color-mix(in srgb, var(--network-color-entities) 70%, black);
}

.node--type-default .node-circle {
  fill: var(--network-color-default);
  stroke: color-mix(in srgb, var(--network-color-default) 70%, black);
}

/* ===========================
   THREAT LEVELS
   =========================== */

/* Threat level indicator via stroke */
.node--threat-low .node-circle {
  stroke: var(--network-threat-low);
  stroke-width: calc(var(--network-node-stroke-base) + 1px);
}

.node--threat-medium .node-circle {
  stroke: var(--network-threat-medium);
  stroke-width: calc(var(--network-node-stroke-base) + 1px);
}

.node--threat-high .node-circle {
  stroke: var(--network-threat-high);
  stroke-width: calc(var(--network-node-stroke-base) + 1.5px);
  filter: drop-shadow(var(--network-glow-threat-high));
  animation: threat-pulse 1.5s ease-in-out infinite;
}

.node--threat-critical .node-circle {
  stroke: var(--network-threat-critical);
  stroke-width: calc(var(--network-node-stroke-base) + 2px);
  filter: drop-shadow(var(--network-glow-threat-high));
  animation: threat-pulse-critical 1s ease-in-out infinite;
}

@keyframes threat-pulse {
  0%,
  100% {
    filter: drop-shadow(var(--network-glow-threat-high));
  }
  50% {
    filter: drop-shadow(0 0 20px rgba(239, 68, 68, 0.8));
  }
}

@keyframes threat-pulse-critical {
  0%,
  100% {
    filter: drop-shadow(0 0 16px rgba(220, 38, 38, 0.6));
  }
  50% {
    filter: drop-shadow(0 0 24px rgba(220, 38, 38, 1));
  }
}

/* Focal node ALWAYS takes precedence over threat styling */
.node--focal.node--threat-low .node-circle,
.node--focal.node--threat-medium .node-circle,
.node--focal.node--threat-high .node-circle,
.node--focal.node--threat-critical .node-circle {
  r: var(--network-node-radius-focal);
  fill: var(--network-color-focal);
  stroke: var(--network-color-focal-stroke);
  stroke-width: var(--network-node-stroke-focal);
  filter: drop-shadow(var(--network-shadow-focal));
  animation: focal-pulse 2s ease-in-out infinite !important;
}

/* ===========================
   NODE LABELS
   =========================== */

.node text {
  font-size: var(--network-label-font-size);
  font-weight: var(--network-label-font-weight);
  fill: var(--network-label-color);
  stroke: var(--network-label-stroke);
  stroke-width: var(--network-label-stroke-width);
  paint-order: stroke;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
  transition:
    font-size var(--network-transition-duration)
      var(--network-transition-easing),
    font-weight var(--network-transition-duration)
      var(--network-transition-easing);
}

.node:hover text {
  font-weight: 600;
}

.node--focal text {
  font-weight: 600;
  font-size: calc(var(--network-label-font-size) + 1px);
}

/* Label truncation */
.node text.truncated {
  opacity: 0.9;
}

.node text.full {
  display: none;
  opacity: 1;
  font-weight: 600;
}

.node:hover text.truncated {
  display: none;
}

.node:hover text.full {
  display: block;
}

/* ===========================
   NEIGHBOR HIGHLIGHTING
   =========================== */

.node--neighbor .node-circle {
  stroke: var(--network-edge-color-neighbor);
  stroke-width: calc(var(--network-node-stroke-base) + 1px);
}

/* ===========================
   UTILITY CLASSES
   =========================== */

.network-hidden {
  display: none;
}

.network-invisible {
  visibility: hidden;
}

/* ===========================
   RESPONSIVE ADJUSTMENTS
   =========================== */

@media (max-width: 1200px) {
  :root {
    --network-node-radius-base: 6px;
    --network-node-radius-focal: 12px;
    --network-label-font-size: var(--font-size-xs);
    --network-edge-label-font-size: var(--font-size-xs);
  }
}

@media (max-width: 768px) {
  :root {
    --network-node-radius-base: 5px;
    --network-node-radius-focal: 10px;
    --network-label-font-size: var(--font-size-2xs);
    --network-edge-label-font-size: var(--font-size-2xs);
    --network-edge-width-base: 1.5px;
  }
}

/* ===========================
   DARK MODE SUPPORT (OPTIONAL)
   =========================== */

@media (prefers-color-scheme: dark) {
  :root {
    --network-edge-color: #000000;
    --network-label-color: #e5e7eb;
    --network-label-stroke: #1f2937;
  }

  .link-label {
    fill: #e5e7eb;
  }

  .link-label:hover {
    fill: #60a5fa;
  }
}

/* Network rendered inside a results tab. The frame (.network-tab-graph) fills
   the tab body and takes its size from it alone. */
.data-source-content:has(> .network-tab-graph) {
  display: flex !important;
  flex-direction: column;
  overflow: hidden !important;
}

.data-source-content > .network-tab-graph {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  position: relative;
}

/* Out of flow, so nothing inside the frame (canvas, panel) can grow it. */
.network-tab-graph > .network-content-wrapper {
  position: absolute;
  inset: 0;
}

/* ===========================
   GRAPH SIDE PANEL
   One per graph, beside the canvas (ensureGraphPanel in
   network_popups_and_legends.js). Every size here comes from the parent,
   never from the canvas, so canvas resizes cannot feed back into layout.
   =========================== */

.network-content-wrapper {
  display: flex;
  position: relative;
  width: 100%;
  height: 100%;
  /* Lets the panel switch to an overlay on narrow mounts (below) */
  container-type: inline-size;
}

.network-graph-container {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* The graph element fills the area beside the panel */
.network-graph-container > * {
  flex: 1 1 0;
  min-width: 0;
}

/* Block, never inline: an inline canvas adds a baseline gap below itself, so
   a content-sized host grows by that gap on every ResizeObserver pass — an
   infinite resize loop. */
.network-graph-container canvas {
  display: block;
}

.network-sidebar-panel {
  flex: 0 0 min(320px, 45%);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--gd-surface);
  border-left: 1px solid var(--gd-rim);
  color: var(--gd-text-hi);
  font-family: var(--gd-font-sans);
  transition: flex-basis 0.2s ease;
}

.network-content-wrapper.is-collapsed > .network-sidebar-panel {
  flex-basis: 0;
  border-left-width: 0;
}

/* Narrow mounts (e.g. beside the Detailed view's master list): sharing the
   width would leave the panel too thin to use, so it overlays the graph's
   right edge at a usable width instead. The graph keeps its full width. */
@container (max-width: 560px) {
  .network-sidebar-panel {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 102;
    width: min(280px, 100%);
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.4);
  }

  .network-content-wrapper.is-collapsed > .network-sidebar-panel {
    display: none;
  }
}

.graph-panel-header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  flex-shrink: 0;
  background: var(--gd-header-bg);
  border-bottom: 1px solid var(--gd-rim);
}

.graph-panel-tabs {
  display: flex;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.graph-panel-tab {
  min-width: 0;
  overflow: hidden;
}

/* Collapse (in the header) and reopen (graph corner, only when collapsed):
   borderless icon buttons drawn with a CSS mask, so no markup or emoji. */
.graph-panel-collapse,
.graph-panel-reopen {
  width: 26px;
  height: 26px;
  padding: 0;
  flex-shrink: 0;
  border: none;
  border-radius: var(--gd-r);
  background: transparent;
  color: var(--gd-text-mid);
  cursor: pointer;
}

.graph-panel-collapse:hover,
.graph-panel-reopen:hover {
  background: var(--gd-elevated);
  color: var(--gd-text-hi);
}

.graph-panel-collapse::before,
.graph-panel-reopen::before {
  content: "";
  display: block;
  width: 14px;
  height: 14px;
  margin: auto;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23000' stroke-width='1.5'%3E%3Crect x='1.75' y='2.75' width='12.5' height='10.5'/%3E%3Cpath d='M10 2.75v10.5'/%3E%3C/svg%3E")
    center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23000' stroke-width='1.5'%3E%3Crect x='1.75' y='2.75' width='12.5' height='10.5'/%3E%3Cpath d='M10 2.75v10.5'/%3E%3C/svg%3E")
    center / contain no-repeat;
}

.graph-panel-reopen {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 101;
  background: var(--gd-surface);
}

.network-content-wrapper.is-collapsed .graph-panel-reopen {
  display: block;
}

.graph-panel-pane {
  display: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.graph-panel-pane.active {
  display: block;
}

.graph-panel-empty {
  padding: 10px;
  color: var(--gd-text-low);
  font-size: var(--gd-sz-sm);
}

/* Summary and key rows: marker, label, right-aligned count */
.graph-panel-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 22px;
  font-size: var(--gd-sz-sm);
  color: var(--gd-text-mid);
}

.graph-panel-row-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.graph-panel-row-value {
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--gd-font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--gd-text-hi);
}

.graph-panel-row.is-toggle {
  cursor: pointer;
}

.graph-panel-row.is-toggle:hover {
  color: var(--gd-text-hi);
}

.graph-panel-row.is-hidden {
  opacity: 0.35;
}

.graph-panel-row.is-hidden .graph-panel-row-label {
  text-decoration: line-through;
}

.graph-key-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 14px;
  padding: 0 3px;
  flex-shrink: 0;
  background: var(--gd-surface);
  border: 1px solid var(--gd-stroke);
  color: var(--gd-text-mid);
  font-family: var(--gd-font-mono);
  font-size: var(--gd-sz-xs);
}

/* Circles because nodes are circles: the key mirrors the canvas */
.graph-key-swatch {
  width: 10px;
  height: 10px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--gd-text-dead);
}

.graph-key-focal {
  width: 10px;
  height: 10px;
  margin: 0 3px;
  flex-shrink: 0;
  box-sizing: border-box;
  border-radius: 50%;
  background: var(--gd-surface);
  border: 2px solid var(--gd-text-hi);
  box-shadow:
    0 0 0 2px var(--gd-surface),
    0 0 0 4px var(--gd-text-hi);
}

/* Controls — native inputs in the panel's colours. Rows that hold a control
   are <label>s, so the whole row is the hit area. */
label.graph-panel-row {
  cursor: pointer;
}

label.graph-panel-row:hover {
  color: var(--gd-text-hi);
}

.graph-panel-range {
  display: block;
  width: 100%;
  height: 14px;
  margin: 0 0 6px;
  accent-color: var(--gd-amber);
  cursor: pointer;
}

.graph-panel-input,
.graph-panel-select {
  box-sizing: border-box;
  height: 24px;
  background: var(--gd-surface);
  border: 1px solid var(--gd-rim);
  border-radius: var(--gd-r);
  color: var(--gd-text-hi);
  font-family: var(--gd-font-sans);
  font-size: var(--gd-sz-sm);
}

.graph-panel-input {
  display: block;
  width: 100%;
  margin-bottom: 6px;
  padding: 0 8px;
}

.graph-panel-select {
  padding: 0 4px;
}

/* Two commands side by side */
.graph-panel-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: 4px;
}

.graph-panel-actions .network-popup-btn.is-cancel {
  color: var(--gd-red) !important;
}

/* Small inline toggle inside a row (e.g. Map on the geography option) */
.graph-panel-chip {
  height: 18px;
  padding: 0 6px;
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--gd-rim);
  border-radius: var(--gd-r);
  color: var(--gd-text-mid);
  font-family: var(--gd-font-sans);
  font-size: var(--gd-sz-xs);
  cursor: pointer;
}

.graph-panel-chip.active {
  background: var(--gd-amber-dim);
  border-color: transparent;
  color: var(--gd-amber);
}

.graph-panel-link {
  padding: 0;
  background: none;
  border: none;
  color: var(--gd-text-low);
  font-family: var(--gd-font-sans);
  font-size: var(--gd-sz-xs);
  cursor: pointer;
}

.graph-panel-link:hover {
  color: var(--gd-amber);
}

.graph-filter-status {
  min-height: 18px;
  font-size: var(--gd-sz-xs);
  color: var(--gd-text-low);
}

.graph-filter-status.is-empty .graph-panel-row-label {
  color: var(--gd-red);
}

/* The chosen grouping's key, overlaid on the graph's top-left corner */
.graph-cluster-key:not(:empty) {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 100;
  max-height: 300px;
  overflow-y: auto;
  padding: 8px 12px;
  background: rgba(13, 17, 23, 0.95);
  border: 1px solid var(--gd-rim);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  font-size: var(--gd-sz-xs);
}

/* The results panel sits inside .panel-content / .collapsible-section, whose
   form styles use !important: every label becomes a bold block, every
   checkbox an orange tick box, every select gets padding that clips its
   text. Scoped to the side panel, with enough specificity to win. */
.network-sidebar-panel label.graph-panel-row {
  display: flex; /* not !important: hidden options use inline display:none */
  margin: 0 !important;
  color: var(--gd-text-mid) !important;
  font-size: var(--gd-sz-sm) !important;
  font-weight: 400 !important;
}

.network-sidebar-panel label.graph-panel-row:hover {
  color: var(--gd-text-hi) !important;
}

/* The one option control, shared by on/off and one-of options: a round
   white button with an amber centre when on. */
.network-sidebar-panel .graph-panel-row input.graph-check {
  appearance: none !important;
  position: relative !important;
  flex-shrink: 0;
  width: 13px !important;
  height: 13px !important;
  margin: 0 !important;
  border: 1px solid var(--gd-text-mid) !important;
  border-radius: 50% !important;
  background: var(--gd-text-hi) !important;
  cursor: pointer;
}

.network-sidebar-panel .graph-panel-row input.graph-check:checked {
  border-color: var(--gd-amber) !important;
  background: radial-gradient(
    circle,
    var(--gd-amber) 0 3px,
    var(--gd-text-hi) 3.5px
  ) !important;
}

.network-sidebar-panel .graph-panel-row input.graph-check::after {
  content: none !important;
}

.network-sidebar-panel select.graph-panel-select {
  width: auto !important;
  height: 24px !important;
  padding: 0 4px !important;
  font-size: var(--gd-sz-sm) !important;
}

.graph-panel-actions .network-popup-btn {
  height: 24px;
  padding: 0 6px;
  white-space: nowrap;
  font-family: var(--gd-font-sans);
  font-size: var(--gd-sz-sm);
  font-weight: 400;
}

.graph-panel-actions .network-popup-btn.secondary {
  background: transparent !important;
  border: 1px solid var(--gd-rim) !important;
  color: var(--gd-text-mid) !important;
}

.graph-panel-actions .network-popup-btn.secondary:hover {
  background: var(--gd-elevated) !important;
  color: var(--gd-text-hi) !important;
}

/* Node and edge details wrap heading text in a span, which the app-wide
   panel span colour would otherwise restyle; headings match in both tabs. */
.network-sidebar-panel .network-popup-section h4 span {
  color: inherit !important;
  font: inherit !important;
}
