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

:root {
  --bg: #ececee;
  --grid: rgba(0, 0, 0, 0.14);
  --surface: #ffffff;
  --node-body: #fafbfc;
  --text: #111827;
  --text-muted: #5b6472;
  --accent: #4262ff;
  --accent-hover: #3451e0;
  --border: #d8dce3;
  --border-hover: #b8bec8;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 4px 14px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 8px 24px rgba(15, 23, 42, 0.1);
  --note-bg: #fff9c4;
  --note-border: #f9e547;
  --note-text: #5c4d00;
  --note-placeholder: #c4a800;
  --active-bg: #e8edff;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --edge: #8b93a3;
  --edge-flow-highlight: #6f87ff;
  --port-bg: #ffffff;
  --port-border: #a0a0b0;
  --placeholder: #c0c0c8;
  --hint-bg: rgba(255,255,255,0.9);
  --media-bg: #f4f5f8;
  --media-bg-hover: #eceef4;
  --media-bg-active: #e8edff;
  --campaign-kpi-bg: #f0f5ff;
  --float-btn-bg: rgba(255,255,255,0.9);
  --overlay: rgba(0,0,0,0.3);
  --overlay-heavy: rgba(0,0,0,0.4);
  --stats-notice-bg: #f0f4ff;
  --stats-notice-text: #334155;
  --stats-demo-bg: #fff3e0;
  --stats-demo-text: #e65100;
  --radius: 10px;
  --radius-sm: 8px;
  --font-ui: 'Inter', -apple-system, sans-serif;
  --font-serif: 'Playfair Display', Georgia, serif;
}

html.theme-night {
  color-scheme: dark;
  --bg: #121216;
  --grid: rgba(255, 255, 255, 0.1);
  --surface: #232328;
  --node-body: #1a1a1f;
  --text: #f3f4f8;
  --text-muted: #9aa0ad;
  --accent: #5b7cff;
  --accent-hover: #4a6aee;
  --border: #3a3a44;
  --border-hover: #4d4d58;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 6px 18px rgba(0, 0, 0, 0.28);
  --shadow-lg: 0 10px 28px rgba(0, 0, 0, 0.42);
  --note-bg: #3a3524;
  --note-border: #5c5428;
  --note-text: #d4c878;
  --note-placeholder: #9a8c40;
  --active-bg: rgba(91, 124, 255, 0.18);
  --danger-bg: rgba(239, 68, 68, 0.15);
  --edge: #6b7280;
  --edge-flow-highlight: #8ba4ff;
  --port-bg: #2a2a30;
  --port-border: #5c5c6a;
  --placeholder: #6a6a7a;
  --hint-bg: rgba(42, 42, 48, 0.92);
  --media-bg: #222228;
  --media-bg-hover: #2a2a32;
  --media-bg-active: rgba(91, 124, 255, 0.15);
  --campaign-kpi-bg: rgba(91, 124, 255, 0.12);
  --float-btn-bg: rgba(42, 42, 48, 0.95);
  --overlay: rgba(0,0,0,0.55);
  --overlay-heavy: rgba(0,0,0,0.65);
  --stats-notice-bg: rgba(91, 124, 255, 0.1);
  --stats-notice-text: #b8c4e0;
  --stats-demo-bg: rgba(230, 81, 0, 0.15);
  --stats-demo-text: #fb923c;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: geometricPrecision;
}

#app {
  display: grid;
  grid-template-rows: 48px 1fr;
  grid-template-columns: 56px 1fr;
  height: 100vh;
}

/* ── Top Bar ── */
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
}

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

.topbar-center {
  flex: 1;
  justify-content: center;
  min-width: 0;
}

.format-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.format-toolbar[hidden] {
  display: none !important;
}

.format-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.format-divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.format-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  font-family: var(--font-ui);
  transition: all 0.15s;
}

.format-btn:hover { background: var(--surface); color: var(--text); }
.format-btn.active { background: var(--active-bg); color: var(--accent); }

.format-select {
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-family: var(--font-ui);
  cursor: pointer;
  min-width: 56px;
}

.format-select:focus {
  outline: 2px solid var(--accent);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
  user-select: none;
}

.board-name {
  border: none;
  background: transparent;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 4px;
  min-width: 200px;
  transition: background 0.15s;
}

.board-name:hover { background: var(--bg); }
.board-name:focus { outline: 2px solid var(--accent); background: var(--bg); }

.save-indicator {
  font-size: 12px;
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 0.3s;
}

.save-indicator.visible { opacity: 1; }

.save-indicator.error {
  color: #ef4444;
  opacity: 1;
}

.btn-ghost {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-family: var(--font-ui);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-primary {
  padding: 7px 16px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font-ui);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary:hover { background: var(--accent-hover); }
.btn-primary.full { width: 100%; padding: 12px; font-size: 14px; }

/* ── Toolbar ── */
.toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 8px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 100;
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.tool-btn:hover { background: var(--bg); color: var(--text); }
.tool-btn.active { background: var(--active-bg); color: var(--accent); }
.tool-btn.danger:hover { background: var(--danger-bg); color: var(--danger); }

.toolbar-divider {
  width: 24px;
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* ── Canvas ── */
.canvas-wrapper {
  position: relative;
  overflow: hidden;
  cursor: default;
  background-color: var(--bg);
  background-image: radial-gradient(circle at 1px 1px, var(--grid) 1px, transparent 0);
  background-size: 20px 20px;
  background-position: -1px -1px;
}

.canvas-wrapper.panning { cursor: grab; }
.canvas-wrapper.panning:active { cursor: grabbing; }

.canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  transform-origin: 0 0;
  backface-visibility: hidden;
  will-change: transform;
}

.canvas,
.canvas .node,
.canvas [data-field] {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: geometricPrecision;
}

.edges-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  shape-rendering: geometricPrecision;
}

.nodes-layer {
  position: absolute;
  top: 0; left: 0;
  z-index: 19;
}

.edge-handles-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 20;
}

.edge-controls-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 25;
}

.canvas-hint {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 16px;
  background: var(--hint-bg);
  backdrop-filter: blur(8px);
  border-radius: 20px;
  font-size: 12px;
  color: var(--text-muted);
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.5s;
}

.canvas-hint.hidden { opacity: 0; }

/* ── Nodes ── */
.node {
  position: absolute;
  cursor: grab;
  user-select: none;
}

.node [data-field] {
  user-select: none;
}

.node-text [data-field] {
  cursor: text;
  user-select: text;
}

.node-box [data-field],
.node-note [data-field],
.node-channel [data-field] {
  cursor: inherit;
}

.node [data-field][contenteditable="true"] {
  outline: none;
  cursor: text;
  user-select: text;
}

.node:active { cursor: grabbing; }
.node.selected { z-index: 10; }

.node-box,
.node-channel,
.node-note,
.node-creative-display,
.node-campaign,
.node-performance-score {
  overflow: visible;
}

/* ── Ports (n8n-style) ── */
.port {
  position: absolute;
  width: 10px;
  height: 10px;
  background: var(--port-bg);
  border: 1.5px solid var(--port-border);
  border-radius: 50%;
  z-index: 15;
  cursor: crosshair;
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--surface) 70%, transparent);
  transition: border-color 0.12s ease, transform 0.12s ease;
}

.port-in {
  left: -5px;
  top: 50%;
  transform: translateY(-50%);
}

.port-in::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 28px;
  height: 28px;
  transform: translate(-50%, -50%);
}

.port-out {
  right: -5px;
  top: 50%;
  transform: translateY(-50%);
  overflow: visible;
  z-index: 20;
}

.port-out::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 36px;
  height: 36px;
  transform: translate(-50%, -50%);
}

.port-out:hover {
  border-color: var(--accent);
  transform: translateY(-50%) scale(1.2);
}

.port-in:hover {
  border-color: var(--accent);
  transform: translateY(-50%) scale(1.2);
}

.port-stub {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  pointer-events: all;
  z-index: 21;
}

.port-stub-line {
  width: 28px;
  height: 2px;
  background: var(--edge);
  flex-shrink: 0;
}

.port-plus {
  width: 22px;
  height: 22px;
  margin-left: -1px;
  border: 1.5px solid var(--port-border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  font-family: var(--font-ui);
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

.port-plus:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--active-bg);
}

.port-stub-compact {
  left: 6px;
}

.port-stub-compact .port-plus {
  margin-left: 0;
}

.port-bottom {
  left: 50%;
  right: auto;
  top: auto;
  bottom: -5px;
  transform: translateX(-50%);
}

.port-bottom:hover {
  transform: translateX(-50%) scale(1.2);
}

.port-bottom .port-stub-bottom {
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  flex-direction: column;
  align-items: center;
}

.port-stub-line-v {
  width: 2px;
  height: 28px;
}

.port-top {
  left: 50%;
  right: auto;
  top: -5px;
  bottom: auto;
  transform: translateX(-50%);
}

.port-top:hover,
.port-bottom-in:hover {
  transform: translateX(-50%) scale(1.2);
}

.port-bottom-in {
  left: 50%;
  right: auto;
  top: auto;
  bottom: -5px;
  transform: translateX(-50%);
}

.port-stub-outer {
  position: absolute;
  pointer-events: all;
  z-index: 21;
}

.port-stub-top-out {
  left: 50%;
  top: -54px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.port-stub-top-out .port-stub-line-v {
  margin-top: -1px;
}

.node-creative-display .port-bottom,
.node-performance-score .port-top,
.node-performance-score .port-stub-top-out {
  z-index: 30;
}

.edges-hit-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  overflow: visible;
  z-index: 18;
  pointer-events: none;
  shape-rendering: geometricPrecision;
}

.edges-hit-layer .edge-path-hit {
  pointer-events: stroke;
}

/* Text Node */
.node-text {
  min-width: 200px;
  max-width: 500px;
  padding: 8px 4px;
  background: transparent;
  border: 2px solid transparent;
  border-radius: 4px;
}

.node-text.selected { border-color: var(--accent); }
.node-text .content {
  font-family: var(--font-serif);
  font-size: 28px;
  line-height: 1.4;
  color: var(--text);
  text-align: left;
  outline: none;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.node-text .content div {
  text-align: inherit;
}

.node-text .content:empty::before {
  content: 'Skriv din tekst her...';
  color: var(--placeholder);
  font-style: italic;
}

/* Heading Node */
.node-heading {
  min-width: 220px;
  max-width: 560px;
  padding: 14px 20px 16px;
  background: color-mix(in srgb, var(--surface) 40%, transparent);
  border: 2px dashed color-mix(in srgb, var(--text-muted) 45%, var(--border));
  border-radius: 12px;
  box-shadow: none;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

.node-heading:hover {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border));
}

.node-heading.selected {
  border-style: solid;
  border-color: var(--accent);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 14%, transparent);
}

.node-heading .heading-label {
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 8px;
  pointer-events: none;
  user-select: none;
}

.node-heading [data-field] {
  outline: none;
  word-wrap: break-word;
}

.node-heading .heading-title {
  font-family: var(--font-ui);
  font-size: 22px;
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
  color: var(--text);
  min-height: 1.3em;
}

.node-heading .heading-title:empty::before {
  content: 'Skriv overskrift...';
  color: var(--placeholder);
  font-weight: 500;
}

/* Box Node */
.node-box {
  min-width: 220px;
  max-width: 320px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: visible;
}

.node-box.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-lg);
}

.node-box .header {
  padding: 11px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: calc(var(--radius) - 1px) calc(var(--radius) - 1px) 0 0;
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 70%, #000 30%);
}

.node-box .header .title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.35;
  letter-spacing: -0.01em;
  outline: none;
  font-family: var(--font-ui);
}

.node-box .body {
  padding: 12px 14px 14px;
  background: var(--node-body);
  border-radius: 0 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px);
}

.node-box .body .desc {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-muted);
  outline: none;
  white-space: pre-wrap;
  font-family: var(--font-ui);
}

.node-box .header .title:empty::before {
  content: 'Funnel trin';
  opacity: 0.65;
}

.node-box .body .desc:empty::before {
  content: 'Beskriv dette trin...';
  color: var(--placeholder);
}

/* Note Node */
.node-note {
  min-width: 180px;
  max-width: 280px;
  padding: 14px;
  background: var(--note-bg);
  border: 1px solid var(--note-border);
  border-radius: 2px;
  box-shadow: var(--shadow);
  transform: rotate(-1deg);
}

.node-note.selected { border-color: var(--accent); transform: rotate(0deg); }

.node-note .content {
  font-size: 14px;
  line-height: 1.5;
  outline: none;
  color: var(--note-text);
  white-space: pre-wrap;
}

.node-note .content:empty::before {
  content: 'Skriv en note...';
  color: var(--note-placeholder);
  font-style: italic;
}

/* Channel Node */
.node-channel {
  min-width: 160px;
  background: var(--surface);
  border-radius: 999px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: 8px 16px 8px 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.node-channel.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow);
}

.node-channel .icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.12);
}

.node-channel .icon.meta { background: #1877f2; }
.node-channel .icon.google { background: #ea4335; }
.node-channel .icon.tiktok { background: #000; }
.node-channel .icon.email { background: #6366f1; }
.node-channel .icon.organic { background: #10b981; }
.node-channel .icon.other { background: #8b5cf6; }

.node-channel .label {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
  outline: none;
  font-family: var(--font-ui);
}

.node-channel .label:empty::before {
  content: 'Kanal navn';
  color: var(--placeholder);
}

/* Creative Display Node */
.node-creative-display {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: 0;
  cursor: grab;
  position: relative;
  touch-action: none;
}

.node-creative-display:active {
  cursor: grabbing;
}

.node-creative-display.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-lg);
}

body.is-dragging-node {
  user-select: none;
  cursor: grabbing !important;
}

body.is-dragging-node .creative-display-media,
body.is-dragging-node .creative-display-media img {
  cursor: grabbing !important;
  -webkit-user-drag: none !important;
}

.node-creative-display .port,
.node-creative-display .port-stub {
  z-index: 30;
}

.creative-display-frame {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.creative-display-media {
  flex: 1;
  min-height: 300px;
  background: var(--media-bg);
  border-radius: 10px 10px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: grab;
  transition: background 0.15s;
  -webkit-user-drag: none;
}

.creative-display-media:hover {
  background: var(--media-bg-hover);
}

.creative-display-media.drag-over {
  background: var(--media-bg-active);
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
}

.creative-display-media img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
  image-rendering: auto;
}

.creative-display-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
  pointer-events: none;
}

.creative-display-placeholder svg {
  opacity: 0.5;
}

.creative-display-placeholder span {
  font-size: 13px;
  font-weight: 500;
}

.creative-display-hint {
  font-size: 11px !important;
  font-weight: 400 !important;
  opacity: 0.7;
}

.creative-display-label {
  padding: 10px 14px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-align: center;
  border-top: 1px solid var(--border);
  outline: none;
  font-family: var(--font-ui);
}

.creative-display-label:empty::before {
  content: 'Statisk reklame';
  color: var(--placeholder);
}

/* ── Edges ── */
.edge-path-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 20;
  pointer-events: stroke;
  cursor: pointer;
}

.edge-path-track {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  opacity: 0.58;
  stroke-dasharray: none;
  pointer-events: none;
}

.edge-path-flow {
  fill: none;
  stroke: var(--edge-flow-highlight);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 56 280;
  stroke-dashoffset: 0;
  opacity: 0.82;
  pointer-events: none;
  animation: edge-flow 3.2s linear infinite;
}

.edge-g.is-hovered .edge-path-track,
.edge-g.is-selected .edge-path-track {
  opacity: 0.72;
}

.edge-g.is-hovered .edge-path-flow,
.edge-g.is-selected .edge-path-flow {
  stroke-width: 3.5;
  opacity: 0.92;
  animation-duration: 2.6s;
}

.edge-g.is-preview .edge-path-track {
  opacity: 0.52;
}

.edge-g.is-preview .edge-path-flow {
  opacity: 0.85;
  animation-duration: 2.8s;
}

.edge-arrow,
.edge-preview-arrow {
  pointer-events: none;
  transition: fill 0.2s ease, opacity 0.2s ease;
}

.edge-g .edge-arrow {
  fill: var(--accent);
  opacity: 0.72;
}

.edge-g.is-hovered .edge-arrow,
.edge-g.is-selected .edge-arrow {
  opacity: 1;
}

.edge-g.is-preview .edge-preview-arrow {
  fill: var(--accent);
  opacity: 0.9;
}

@keyframes edge-flow {
  from { stroke-dashoffset: 0; }
  to { stroke-dashoffset: -336; }
}

@media (prefers-reduced-motion: reduce) {
  .edge-path-flow {
    animation: none;
    stroke-dasharray: none;
    opacity: 0.75;
  }
}

/* Legacy selectors kept for safety */
.edge-path {
  fill: none;
  stroke: var(--edge);
  stroke-width: 2;
  pointer-events: none;
}

.edge-path.hovered,
.edge-path.selected { stroke: var(--accent); stroke-width: 2.5; }

.edge-arrow {
  fill: var(--edge);
}

.edge-path.hovered + .edge-arrow,
.edge-path.selected + .edge-arrow { fill: var(--accent); }

.edge-preview {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: none;
  opacity: 1;
  pointer-events: none;
}

.edge-preview-arrow {
  fill: var(--accent);
  pointer-events: none;
}

.edge-controls {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  pointer-events: all;
  z-index: 30;
}

.edge-ctrl-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.edge-ctrl-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.edge-ctrl-delete:hover {
  background: var(--danger-bg);
  color: var(--danger);
}

.edge-ctrl-add:hover {
  background: var(--active-bg);
  color: var(--accent);
}

/* ── Canvas Quick Actions ── */
.canvas-actions {
  position: fixed;
  top: 64px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 150;
  transition: right 0.2s ease;
}

.canvas-actions.picker-open {
  right: 376px;
}

.canvas-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-muted);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: all 0.15s;
  padding: 0;
}

.canvas-action-btn:hover {
  background: var(--bg);
  color: var(--text);
  border-color: var(--border-hover);
}

.canvas-action-btn.active {
  background: var(--active-bg);
  color: var(--accent);
  border-color: var(--accent);
}

/* ── Zoom Controls ── */
.zoom-controls {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--surface);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
  z-index: 100;
}

.zoom-controls button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}

.zoom-controls button:hover { background: var(--bg); color: var(--text); }

.zoom-controls .zoom-divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

#theme-toggle .icon-sun { display: none; }
#theme-toggle .icon-moon { display: block; }

#theme-toggle.is-night .icon-sun { display: block; }
#theme-toggle.is-night .icon-moon { display: none; }

#zoom-level {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  min-width: 44px;
  text-align: center;
}

/* ── Context Menu ── */
.context-menu {
  position: fixed;
  background: var(--surface);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  padding: 4px;
  z-index: 200;
  min-width: 140px;
}

.context-menu button {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: transparent;
  text-align: left;
  font-size: 13px;
  font-family: var(--font-ui);
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
}

.context-menu button:hover { background: var(--bg); }
.context-menu button.danger { color: var(--danger); }
.context-menu button.danger:hover { background: var(--danger-bg); }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  backdrop-filter: blur(4px);
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 32px;
  max-width: 420px;
  width: 90%;
  box-shadow: var(--shadow-lg);
  position: relative;
}

.modal h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 8px;
}

.modal p {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.5;
  margin-bottom: 20px;
}

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  border: none;
  background: transparent;
  font-size: 24px;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
}

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

/* ── Resize Handle ── */
.resize-handle {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 12px;
  height: 12px;
  cursor: nwse-resize;
  opacity: 0;
}

.node.selected .resize-handle { opacity: 0.4; }
.resize-handle:hover { opacity: 1 !important; }

/* ── Node Picker (n8n-style) ── */
.node-picker {
  position: fixed;
  top: 48px;
  right: 0;
  width: 360px;
  height: calc(100vh - 48px);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.08);
  z-index: 200;
  display: flex;
  flex-direction: column;
  animation: slideIn 0.2s ease;
}

.node-picker[hidden] {
  display: none !important;
}

@keyframes slideIn {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.node-picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.node-picker-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.node-picker-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  font-size: 24px;
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  line-height: 1;
}

.node-picker-close:hover {
  background: var(--bg);
  color: var(--text);
}

.node-picker-search {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 20px;
  padding: 0 12px;
  border: 2px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
}

.node-picker-search svg {
  color: var(--text-muted);
  flex-shrink: 0;
}

.node-picker-search input {
  flex: 1;
  border: none;
  outline: none;
  padding: 10px 0;
  font-size: 14px;
  font-family: var(--font-ui);
  background: transparent;
}

.node-picker-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px 20px;
}

.picker-category {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 16px 8px 8px;
}

.picker-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: none;
  background: transparent;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  font-family: var(--font-ui);
  transition: background 0.15s;
}

.picker-item:hover {
  background: var(--bg);
}

.picker-item-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.picker-item-icon.meta { background: #1877f2; }
.picker-item-icon.google { background: #ea4335; }
.picker-item-icon.tiktok { background: #000; }
.picker-item-icon.email { background: #6366f1; }
.picker-item-icon.organic { background: #10b981; }
.picker-item-icon.creative { background: #8b5cf6; }
.picker-item-icon.display { background: #f59e0b; }
.picker-item-icon.funnel { background: linear-gradient(135deg, #4262ff, #6b8aff); }
.picker-item-icon.note { background: #f9e547; color: #5c4d00; }
.picker-item-icon.heading { background: linear-gradient(135deg, #1a1a2e 0%, #4262ff 100%); color: #fff; font-family: var(--font-serif); font-weight: 600; }
.picker-item-icon.branch { background: linear-gradient(135deg, #4262ff 0%, #8b5cf6 55%, #f59e0b 100%); }

.picker-item-text {
  flex: 1;
  min-width: 0;
}

.picker-item-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.picker-item-desc {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
  line-height: 1.4;
}

.picker-item-arrow {
  color: var(--text-muted);
  flex-shrink: 0;
}

.picker-empty {
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.picker-loading {
  padding: 16px 24px;
  color: var(--text-muted);
  font-size: 13px;
}

.picker-item-kpi {
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  margin-top: 2px;
}

.picker-item-icon.campaign { background: #1877f2; }

/* Campaign Node */
.node-campaign {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.node-campaign:hover {
  box-shadow: var(--shadow-lg);
}

.node-campaign.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-lg);
}

.campaign-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.campaign-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: white;
}

.campaign-icon.meta { background: #1877f2; }

.campaign-status {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 10px;
}

.campaign-status.ACTIVE { background: #e6f9ee; color: #0d9f4f; }
.campaign-status.PAUSED { background: #fff3e0; color: #e65100; }
.campaign-status.ARCHIVED { background: #f0f0f0; color: #888; }

html.theme-night .campaign-status.ACTIVE { background: rgba(13, 159, 79, 0.2); color: #4ade80; }
html.theme-night .campaign-status.PAUSED { background: rgba(230, 81, 0, 0.2); color: #fb923c; }
html.theme-night .campaign-status.ARCHIVED { background: rgba(136, 136, 136, 0.2); color: #9ca3af; }

.campaign-name {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.35;
  margin-bottom: 10px;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.campaign-kpi {
  background: var(--campaign-kpi-bg);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
}

.campaign-kpi-label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.campaign-kpi-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  margin-top: 2px;
}

.campaign-spend {
  font-size: 11px;
  color: var(--text-muted);
}

/* Performance Score Node */
.node-performance-score {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: 14px 16px 12px;
  cursor: grab;
  position: relative;
}

.node-performance-score.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), var(--shadow-lg);
}

.perf-score-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.perf-score-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4262ff, #7c5cff);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  flex-shrink: 0;
}

.perf-score-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}

.perf-score-ring {
  border-radius: 12px;
  padding: 14px 12px;
  text-align: center;
  margin-bottom: 10px;
  background: var(--campaign-kpi-bg);
}

.perf-score-ring.high {
  background: rgba(13, 159, 79, 0.12);
}

.perf-score-ring.mid {
  background: rgba(255, 152, 0, 0.12);
}

.perf-score-ring.low {
  background: rgba(239, 68, 68, 0.12);
}

.perf-score-ring.empty {
  background: var(--campaign-kpi-bg);
}

.perf-score-value {
  font-size: 34px;
  font-weight: 700;
  line-height: 1;
  color: var(--accent);
}

.perf-score-ring.high .perf-score-value { color: #0d9f4f; }
.perf-score-ring.mid .perf-score-value { color: #e65100; }
.perf-score-ring.low .perf-score-value { color: #ef4444; }

.perf-score-sub {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.35;
}

.perf-score-kpi {
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
}

.perf-score-kpi-label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.perf-score-kpi-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin-top: 2px;
}

.perf-score-meta {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
}

.picker-item-icon.score {
  background: linear-gradient(135deg, #4262ff, #7c5cff);
  color: white;
}

.campaign-open-stats {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  border: none;
  background: var(--float-btn-bg);
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 5;
}

.node-campaign:hover .campaign-open-stats,
.node-campaign.selected .campaign-open-stats {
  opacity: 1;
}

.campaign-open-stats:hover {
  background: var(--active-bg);
  color: var(--accent);
}

/* Stats Panel */
.stats-panel-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-heavy);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  animation: fadeIn 0.2s ease;
}

.stats-panel-overlay[hidden] {
  display: none !important;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.stats-panel {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.2);
  width: min(820px, 90vw);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  animation: panelUp 0.25s ease;
}

@keyframes panelUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.stats-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 24px 28px 16px;
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.stats-panel-title-wrap h2 {
  font-size: 20px;
  font-weight: 600;
  margin-top: 6px;
}

.stats-panel-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

.stats-panel-platform {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stats-platform-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: white;
}

.stats-platform-icon.meta { background: #1877f2; }

.stats-panel-mode {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

.stats-panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.stats-date-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  font-family: var(--font-ui);
  background: var(--surface);
}

.stats-panel-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  font-size: 24px;
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  line-height: 1;
}

.stats-panel-close:hover {
  background: var(--bg);
}

.stats-panel-body {
  padding: 24px 28px 28px;
  overflow-y: auto;
}

.stats-loading {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

.stats-kpi-hero {
  background: linear-gradient(135deg, #1877f2 0%, #4262ff 100%);
  border-radius: 12px;
  padding: 24px;
  color: white;
  margin-bottom: 24px;
}

.stats-kpi-hero-label {
  font-size: 12px;
  opacity: 0.85;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stats-kpi-hero-value {
  font-size: 36px;
  font-weight: 700;
  margin-top: 4px;
}

.stats-kpi-hero-sub {
  font-size: 13px;
  opacity: 0.8;
  margin-top: 8px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.stats-card {
  background: var(--bg);
  border-radius: 10px;
  padding: 16px;
}

.stats-card-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.stats-card-value {
  font-size: 22px;
  font-weight: 700;
  margin-top: 4px;
  color: var(--text);
}

.stats-card-value.small {
  font-size: 16px;
}

.stats-demo-badge {
  display: inline-block;
  font-size: 10px;
  background: var(--stats-demo-bg);
  color: var(--stats-demo-text);
  padding: 3px 8px;
  border-radius: 4px;
  margin-top: 12px;
  font-weight: 500;
}

.stats-notice {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--stats-notice-bg);
  color: var(--stats-notice-text);
  font-size: 13px;
  line-height: 1.5;
}

.stats-notice-detail {
  display: block;
  margin-top: 6px;
  font-size: 12px;
  opacity: 0.85;
}

.meta-connect-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.meta-connect-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #94a3b8;
  flex-shrink: 0;
}

.meta-connect-dot.connected {
  background: #22c55e;
}

.meta-token-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin: 16px 0 6px;
}

.meta-token-input {
  width: 100%;
  min-height: 72px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  resize: vertical;
}

.meta-token-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin: 10px 0 0;
  line-height: 1.5;
}

.meta-token-feedback {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.45;
}

.meta-token-feedback.is-error {
  background: var(--danger-bg);
  color: var(--danger);
}

.meta-token-feedback.is-success {
  background: rgba(13, 159, 79, 0.15);
  color: #0d9f4f;
}

html.theme-night .meta-token-feedback.is-success {
  color: #4ade80;
}

.meta-token-feedback[hidden] {
  display: none !important;
}

.meta-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

/* ── Marketing Branch (composite node) ── */
.node-marketing-branch {
  background: transparent;
  box-shadow: none;
  border: 2px solid transparent;
  border-radius: 0;
  overflow: visible;
}

.node-marketing-branch.selected {
  border-color: transparent;
}

.node-marketing-branch.selected .branch-frame {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 12px;
}

.branch-frame {
  position: relative;
  width: 620px;
  height: 600px;
}

.branch-connections {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}

.branch-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: none;
  opacity: 0.55;
}

.branch-junction {
  fill: var(--surface);
  stroke: var(--port-border);
  stroke-width: 2;
}

.branch-junction-label {
  fill: var(--text-muted);
  font-size: 14px;
  font-family: var(--font-ui);
  font-weight: 500;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}

.node-marketing-branch .sub-box {
  position: absolute;
  z-index: 2;
  min-width: 0;
  max-width: none;
}

.node-marketing-branch .branch-onsker {
  left: 0;
  top: 0;
  width: 268px;
}

.node-marketing-branch .branch-vaerditilbud {
  left: 332px;
  top: 0;
  width: 268px;
}

.node-marketing-branch .sub-display {
  position: absolute;
  left: 0;
  top: 168px;
  width: 280px;
  height: 380px;
  z-index: 2;
  padding: 0;
}

.node-marketing-branch .sub-display .creative-display-media {
  min-height: 320px;
}

.node-marketing-branch .sub-channel {
  position: absolute;
  left: 306px;
  top: 438px;
  z-index: 2;
  min-width: 180px;
}

.node-marketing-branch .branch-port-left {
  left: -5px;
  top: 58px;
  transform: translateY(-50%);
}

.node-marketing-branch .branch-port-right {
  right: -5px;
  top: 58px;
  left: auto;
  transform: translateY(-50%);
}

.node-marketing-branch .branch-port-bottom {
  left: 466px;
  top: 128px;
  transform: translate(-50%, -50%);
}

.node-marketing-branch .branch-port-right .port-stub {
  left: 8px;
}
