*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #1f2937;
  --border: #30363d;
  --accent: #58a6ff;
  --accent2: #3fb950;
  --red: #f85149;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --win-chrome: #1c2128;
  --win-border: #373e47;
  --radius: 8px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Topbar ── */
#topbar {
  height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
  z-index: 1000;
}

#topbar-left, #topbar-right { display: flex; align-items: center; gap: 10px; }

#logo {
  font-weight: 700;
  font-size: 16px;
  color: var(--accent);
  letter-spacing: 0.5px;
  margin-right: 8px;
}

button {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 14px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s, border-color 0.15s;
}
button:hover { background: var(--win-border); border-color: var(--accent); }

#btn-new-group {
  background: var(--accent);
  color: #0d1117;
  border-color: var(--accent);
  font-weight: 600;
}
#btn-new-group:hover { background: #79c0ff; border-color: #79c0ff; }

#btn-new-company {
  background: transparent;
  color: var(--accent2);
  border-color: var(--accent2);
  font-weight: 600;
}
#btn-new-company:hover { background: rgba(63,185,80,0.12); border-color: #56d364; }

.feed-status {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface2);
  pointer-events: none;
  transition: color 0.3s;
}
.feed-off  { color: var(--text-muted); }
.feed-live { color: var(--accent2); }
.feed-err  { color: var(--red); }

/* ── Main layout ── */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── Canvas ── */
#canvas {
  flex: 1;
  position: relative;
  overflow: hidden;
}

/* ── Search panel ── */
#search-panel {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

#search-header {
  padding: 12px 16px;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

#search-input-wrap {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

#search-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}
#search-input:focus { border-color: var(--accent); }
#search-input::placeholder { color: var(--text-muted); }

#search-results {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}
#search-results::-webkit-scrollbar { width: 4px; }
#search-results::-webkit-scrollbar-track { background: transparent; }
#search-results::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.search-result {
  display: flex;
  align-items: center;
  padding: 8px 14px;
  cursor: grab;
  gap: 10px;
  transition: background 0.1s;
  user-select: none;
  border-bottom: 1px solid transparent;
}
.search-result:hover { background: var(--surface2); border-bottom-color: var(--border); }
.search-result:active { cursor: grabbing; }

.result-symbol {
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  min-width: 56px;
}
.result-info { flex: 1; min-width: 0; }
.result-name {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-exchange {
  font-size: 11px;
  color: var(--text-muted);
  opacity: 0.6;
}
.result-price { text-align: right; }
.result-price .price { font-size: 13px; font-weight: 600; }
.result-price .change { font-size: 11px; }
.change.up { color: var(--accent2); }
.change.down { color: var(--red); }

.search-msg {
  padding: 20px 14px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

/* ── Group Windows ── */
.group-window {
  position: absolute;
  min-width: 200px;
  min-height: 120px;
  background: var(--surface);
  border: 1px solid var(--win-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  overflow: visible;
}
.group-window.minimized { overflow: hidden; }
.group-window.active { border-color: var(--accent); z-index: 100; }

.win-connector-tab {
  height: 20px;
  background: var(--bg);
  border-radius: var(--radius) var(--radius) 0 0;
  border-bottom: 1px solid var(--win-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px;
  flex-shrink: 0;
  cursor: grab;
  user-select: none;
}
.win-connector-tab:active { cursor: grabbing; }

.win-port-left,
.win-port-right {
  width: 11px; height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
  position: relative;
}
.win-port-left {
  background: rgba(63,185,80,0.15);
  border: 2px solid var(--accent2);
  cursor: default;
}
.win-port-right {
  background: rgba(88,166,255,0.15);
  border: 2px solid var(--accent);
  cursor: crosshair;
}
.win-port-right:hover {
  background: rgba(88,166,255,0.4);
  transform: scale(1.3);
  box-shadow: 0 0 6px rgba(88,166,255,0.55);
}
.win-port-left.port-target-active {
  background: rgba(63,185,80,0.55);
  transform: scale(1.4);
  box-shadow: 0 0 8px rgba(63,185,80,0.7);
}

.win-titlebar {
  height: 36px;
  background: var(--win-chrome);
  border-bottom: 1px solid var(--win-border);
  border-radius: 0;
  display: flex;
  align-items: center;
  padding: 0 10px;
  cursor: grab;
  user-select: none;
  gap: 8px;
  flex-shrink: 0;
}
.win-titlebar:active { cursor: grabbing; }
.group-window.minimized .win-titlebar {
  border-radius: 0 0 var(--radius) var(--radius);
  border-bottom: 1px solid var(--win-border);
}

.win-dots { display: flex; gap: 5px; flex-shrink: 0; }
.win-dot {
  width: 11px; height: 11px;
  border-radius: 50%;
  cursor: pointer;
  transition: opacity 0.15s;
}
.win-dot:hover { opacity: 0.8; }
.dot-close { background: #f85149; }
.dot-min   { background: #e3b341; }

.win-title {
  flex: 1;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.win-title[contenteditable="true"] {
  outline: none;
  border-bottom: 1px dashed var(--accent);
  cursor: text;
}

.win-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  min-height: 60px;
}
.win-body::-webkit-scrollbar { width: 4px; }
.win-body::-webkit-scrollbar-track { background: transparent; }
.win-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.group-window.minimized .win-body { display: none; }
.group-window.minimized .win-resize { display: none; }

/* Drop zone highlight */
.win-body.drag-over {
  background: rgba(88, 166, 255, 0.05);
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
}

/* Stock row in group */
.stock-row {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  cursor: grab;
  gap: 8px;
  transition: background 0.1s;
  user-select: none;
  position: relative;
}
.stock-row:hover { background: var(--surface2); }
.stock-row:active { cursor: grabbing; opacity: 0.7; }
.stock-row.drag-placeholder {
  opacity: 0.3;
  border: 1px dashed var(--border);
}

.stock-symbol {
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  min-width: 52px;
}
.stock-name {
  flex: 1;
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stock-price { font-size: 13px; font-weight: 600; text-align: right; min-width: 60px; }
.stock-change { font-size: 11px; text-align: right; min-width: 50px; }
.stock-remove {
  opacity: 0;
  font-size: 14px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  transition: opacity 0.15s, color 0.15s;
  background: none;
  border: none;
  flex-shrink: 0;
}
.stock-row:hover .stock-remove { opacity: 1; }
.stock-remove:hover { color: var(--red); background: rgba(248,81,73,0.1); }

.win-empty {
  padding: 16px 12px;
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
  pointer-events: none;
}

/* ── Company node (private company card) ── */
.company-node {
  min-height: unset;
  width: 220px !important;
}
.company-titlebar {
  background: #162312;
  border-bottom: 1px solid #2ea04326;
  gap: 6px;
}
.company-badge {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.8px;
  color: var(--accent2);
  background: rgba(63,185,80,0.12);
  border: 1px solid rgba(63,185,80,0.3);
  border-radius: 3px;
  padding: 1px 5px;
  flex-shrink: 0;
}
.company-body {
  padding: 8px 12px 10px;
}
.company-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
  cursor: text;
  min-height: 28px;
}
.company-desc[contenteditable="true"] {
  outline: none;
  border-bottom: 1px dashed var(--accent2);
  color: var(--text);
}

/* Resize handle */
.win-resize {
  position: absolute;
  right: 0; bottom: 0;
  width: 16px; height: 16px;
  cursor: se-resize;
  opacity: 0.4;
}
.win-resize::after {
  content: '';
  position: absolute;
  right: 3px; bottom: 3px;
  width: 8px; height: 8px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
}
.win-resize:hover { opacity: 1; }

/* Window highlight when it's a valid drop target for a connection */
.group-window.connect-target {
  border-color: var(--accent2) !important;
  box-shadow: 0 0 0 2px rgba(63,185,80,0.3), 0 8px 32px rgba(0,0,0,0.5);
}
.group-window.connect-source {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 2px rgba(88,166,255,0.3), 0 8px 32px rgba(0,0,0,0.5);
}

/* ── SVG connection canvas ── */
#connections-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}

.conn-line {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
}
.conn-line.done {
  stroke: var(--accent);
  marker-end: url(#arrow);
}
.conn-line.pending {
  stroke: var(--text-muted);
  stroke-dasharray: 6 4;
  marker-end: url(#arrow-pending);
  animation: dash-flow 1s linear infinite;
}
.conn-line.error {
  stroke: var(--red);
  marker-end: url(#arrow);
}
.conn-line.drawing {
  stroke: var(--accent2);
  stroke-dasharray: 6 4;
  animation: dash-flow 0.6s linear infinite;
}
@keyframes dash-flow {
  to { stroke-dashoffset: -20; }
}

.arrow-head         { fill: var(--accent); }
.arrow-head-pending { fill: var(--text-muted); }

/* Connection label (foreignObject div) */
/* Endpoint handles */
.conn-handle {
  fill: var(--surface);
  stroke: var(--accent);
  stroke-width: 2;
  pointer-events: none;
  transition: fill 0.15s, r 0.15s;
}
.conn-handle.conn-handle-hover {
  fill: var(--accent);
}

.conn-label {
  background: var(--surface);
  border: 1px solid var(--win-border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 11px;
  color: var(--text);
  text-align: center;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
  line-height: 1.3;
  transition: border-color 0.15s;
  pointer-events: all;
  user-select: none;
}
.conn-label:hover { border-color: var(--accent); }
.conn-label.pending { color: var(--text-muted); font-style: italic; }
.conn-label.error   { color: var(--red); }

/* ── Connection detail modal ── */
#conn-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999;
}

#conn-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  width: 520px;
  max-width: 92vw;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#conn-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--win-chrome);
}
#conn-modal-title { font-weight: 700; font-size: 14px; }
#conn-modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
}
#conn-modal-close:hover { color: var(--red); background: none; }

#conn-groups-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.conn-group-card {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  min-width: 0;
}
.conn-group-card .cgc-name {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 6px;
  color: var(--accent);
}
.conn-group-card .cgc-stocks {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.6;
}
.conn-arrow-mid {
  font-size: 20px;
  color: var(--accent);
  flex-shrink: 0;
}

#conn-analysis-box {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
#conn-analysis-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 8px;
}
#conn-analysis-text {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
  min-height: 40px;
}
#conn-analysis-text.thinking {
  color: var(--text-muted);
  font-style: italic;
}

#conn-modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 18px;
}
.btn-danger { color: var(--red) !important; border-color: var(--red) !important; }
.btn-danger:hover { background: rgba(248,81,73,0.15) !important; }

/* ── Modals ── */
#modal-overlay, #load-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999;
}
.hidden { display: none !important; }

#modal, #load-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 480px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#modal-title, #load-title {
  font-weight: 700;
  font-size: 15px;
}

#modal-code, #load-code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: monospace;
  font-size: 12px;
  padding: 10px;
  resize: vertical;
  min-height: 80px;
  max-height: 200px;
  width: 100%;
  outline: none;
}
#modal-code:focus, #load-code:focus { border-color: var(--accent); }

#modal-actions, #load-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ── Chart tooltip ── */
.chart-tooltip {
  position: fixed;
  z-index: 99998;
  background: var(--surface);
  border: 1px solid var(--win-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  width: 286px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.7);
  pointer-events: none;
  transition: none;
}

.ct-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}
.ct-symbol {
  font-weight: 700;
  font-size: 14px;
  color: var(--accent);
  flex-shrink: 0;
}
.ct-name {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.ct-price {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 10px;
}
.ct-chg { font-size: 12px; font-weight: 600; }
.ct-chg.up   { color: var(--accent2); }
.ct-chg.down { color: var(--red); }

.ct-spark {
  margin-bottom: 8px;
  line-height: 0;
  border-radius: 4px;
  overflow: hidden;
  background: rgba(255,255,255,0.02);
}

.ct-footer {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
  padding-top: 4px;
  border-top: 1px solid var(--border);
}

.ct-loading {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  padding: 20px 0 8px;
  text-align: center;
}

/* Ghost/drag image */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 99999;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 6px 12px;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  transform: translate(-50%, -50%);
  opacity: 0.9;
}
