:root {
  --bg: #0b0f14;
  --surface: #121821;
  --surface-2: #17202b;
  --border: #233042;
  --text: #e6edf3;
  --muted: #8b9bb0;
  --accent: #00d4aa;
  --blue: #3b82f6;
  --amber: #f59e0b;
  --red: #ef4444;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  max-width: 960px;
  margin: 0 auto;
  padding: 12px 12px 72px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.2px;
}
.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}
.user-badge {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  padding: 4px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

/* Navigation Tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.tab-btn {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
}
.tab-btn.active {
  background: rgba(0, 212, 170, 0.12);
  color: var(--accent);
  border-color: rgba(0, 212, 170, 0.45);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Controls & Pills */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.period-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.pill-btn {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 11px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.pill-btn.active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { border-color: #3c506b; }
.btn-primary {
  background: var(--accent);
  color: #06211b;
  border-color: var(--accent);
}
.btn-sm { padding: 4px 8px; font-size: 11px; }
.btn-danger { color: #fca5a5; border-color: rgba(239,68,68,0.35); }

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
@media (min-width: 680px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.kpi-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.kpi-val {
  font-size: 18px;
  font-weight: 700;
  margin-top: 4px;
}
.kpi-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}
.text-accent { color: var(--accent); }
.text-amber { color: var(--amber); }

/* Cards / Sections */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 8px;
}
.card-title {
  font-size: 14px;
  font-weight: 700;
}
.badge {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}
.badge-cex {
  color: var(--blue);
  border-color: rgba(59,130,246,0.4);
  background: rgba(59,130,246,0.12);
}
.badge-unknown {
  color: var(--amber);
  border-color: rgba(245,158,11,0.4);
  background: rgba(245,158,11,0.12);
}

/* Entity Bars */
.bar-row { margin-bottom: 10px; }
.bar-row:last-child { margin-bottom: 0; }
.bar-meta {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  margin-bottom: 4px;
}
.bar-track {
  height: 7px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--blue));
  border-radius: 999px;
}

/* Lists / Rows */
.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid rgba(35, 48, 66, 0.7);
}
.list-item:last-child { border-bottom: none; padding-bottom: 0; }
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}
.addr-link {
  color: var(--text);
  text-decoration: none;
}
.addr-link:hover { color: var(--accent); text-decoration: underline; }
.subline { font-size: 11px; color: var(--muted); margin-top: 2px; }
.actions { display: flex; gap: 5px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

/* Inputs & Modal */
.input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 13px;
  outline: none;
}
.input:focus { border-color: var(--accent); }

.form-row { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.chip {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(5, 8, 12, 0.75);
  display: none; align-items: center; justify-content: center;
  padding: 16px; z-index: 50;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: 100%; max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
}

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; border: 1px solid var(--accent);
  padding: 8px 14px; border-radius: 999px; font-size: 12px; font-weight: 600;
  display: none; z-index: 100;
}
.toast.show { display: block; }

.empty { color: var(--muted); font-size: 13px; padding: 8px 0; }

/* Stage 3: Graph Workspace */
.graph-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}
.select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
}
.graph-wrap {
  position: relative;
  width: 100%;
  height: 440px;
  background: radial-gradient(circle at 50% 40%, #131d2b 0%, #090d12 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 10px;
}
#cyCanvas {
  width: 100%;
  height: 100%;
}
.graph-overlay {
  position: absolute;
  inset: 0;
  background: rgba(9, 13, 18, 0.78);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  z-index: 10;
  color: var(--text);
  font-size: 13px;
  text-align: center;
  padding: 16px;
}
.graph-overlay.show { display: flex; }
.spinner {
  width: 28px; height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.graph-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 10px;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 11px;
  color: var(--muted);
}
.leg-dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}
.inspector-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin: 8px 0 12px;
}
.kv-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
.kv-k { font-size: 11px; color: var(--muted); }
.kv-v { font-size: 13px; font-weight: 600; margin-top: 2px; word-break: break-word; }

/* Stage 4: Overlap Engine */
.chip.selected {
  background: rgba(0, 212, 170, 0.16);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}
.ov-matrix-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 6px;
  margin-top: 8px;
}
.ov-cell {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
}
.ov-graph-wrap {
  width: 100%;
  height: 340px;
  background: radial-gradient(circle at 50% 40%, #131d2b 0%, #090d12 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
  display: none;
}
.ov-graph-wrap.open { display: block; }

/* Interactive Step-by-Step Counterparties & History */
.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}
.cp-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0 8px;
}
.cp-list {
  border-top: 1px solid rgba(35, 48, 66, 0.7);
  padding-top: 6px;
}
.cp-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(35, 48, 66, 0.55);
}
.cp-item:last-child { border-bottom: none; }

/* Flow Breakdown Chart & Cases */
.flow-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-top: 10px;
}
.flow-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  margin-bottom: 6px;
}
.dual-bar {
  display: flex;
  height: 8px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--bg);
  margin-bottom: 8px;
}
.dual-in { background: #00d4aa; }
.dual-out { background: #3b82f6; }

.mini-bars {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}
@media (min-width: 680px) {
  .mini-bars { grid-template-columns: 1fr 1fr; gap: 12px; }
}
.mini-col-title {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
  font-weight: 600;
}
.mini-row { margin-bottom: 5px; }
.mini-row-top {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  margin-bottom: 2px;
}
.mini-track {
  height: 4px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}
.mini-fill-in { height: 100%; background: rgba(0, 212, 170, 0.85); }
.mini-fill-out { height: 100%; background: rgba(59, 130, 246, 0.85); }
