:root {
  --bg: #0b0e14;
  --panel: #11151f;
  --text: #e6e6e6;
  --muted: #9aa4b2;
  --accent: #6aa9ff;
  --danger: #ff6a6a;
  --grid: #1b2230;
  --topbar-h: 50px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid #131a26;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
}
.topbar .brand { display: flex; align-items: center; gap: 10px; }
.topbar h1 {
  font-size: 16px;
  margin: 0;
  letter-spacing: 0.2px;
}
.topbar .about-btn {
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.topbar .about-btn:hover { border-color: var(--accent); }
.topbar .actions button {
  margin-left: 8px;
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.topbar .actions button:hover { border-color: var(--accent); }
.topbar .actions .mode-label { margin-right: 6px; color: var(--muted); font-size: 12px; }
.topbar .actions select {
  margin-left: 8px;
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  padding: 6px 8px;
  border-radius: 6px;
}
.topbar .actions > select:first-child { margin-left: 0; }

.topbar .actions { position: relative; }
.export-panel {
  position: absolute;
  right: 0;
  top: 38px;
  background: var(--panel);
  border: 1px solid #2a3346;
  border-radius: 8px;
  padding: 10px;
  width: 260px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  display: none;
  z-index: 20;
}
.export-panel[aria-hidden="false"] { display: block; }
.export-row { display: flex; flex-direction: column; gap: 6px; }
.export-row label { font-size: 12px; color: var(--muted); }
.export-row input {
  background: #0f1420;
  color: var(--text);
  border: 1px solid #1e2636;
  border-radius: 6px;
  padding: 7px 8px;
}
.export-row select {
  background: #0f1420;
  color: var(--text);
  border: 1px solid #1e2636;
  border-radius: 6px;
  padding: 7px 8px;
}
.export-actions { margin-top: 10px; display: flex; justify-content: flex-end; }
.btn-teal {
  background: #0b6e6e; /* dark teal */
  color: #ffffff;
  border: 1px solid #0d7f7f;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
}
.btn-teal:hover { background: #0d7f7f; }

.layout {
  position: fixed; /* pin canvas area to viewport under the topbar */
  top: var(--topbar-h);
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden; /* avoid shadows causing page scroll */
}
.left {
  position: absolute; /* float over the canvas */
  top: 12px;
  left: 12px;
  width: 420px; /* sensible default */
  height: 72vh;  /* default height; user can resize */
  padding: 12px;
  border: 1px solid #131a26;
  border-radius: 12px;
  /* Floating panel; custom resize handle (rounded) */
  overflow: auto;
  min-width: 280px;
  min-height: 200px;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - var(--topbar-h) - 20px);
  z-index: 40; /* render above the graph when overlapping */
  background: var(--panel);
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}

/* Rounded, custom resize handle */
.resize-handle {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 22px;
  height: 22px;
  border-bottom-right-radius: 12px; /* aligns with panel radius */
  border-top-left-radius: 10px; /* softens inner curve */
  background:
    radial-gradient(18px 18px at 100% 100%, rgba(255,255,255,0.06), rgba(255,255,255,0.02) 60%, transparent 61%),
    repeating-linear-gradient(135deg, rgba(255,255,255,0.08) 0, rgba(255,255,255,0.08) 2px, transparent 2px, transparent 4px);
  box-shadow: inset -1px -1px 0 0 #2a3346; /* continue border */
  cursor: se-resize;
}
.resize-handle:hover { background:
    radial-gradient(18px 18px at 100% 100%, rgba(255,255,255,0.10), rgba(255,255,255,0.04) 60%, transparent 61%),
    repeating-linear-gradient(135deg, rgba(255,255,255,0.12) 0, rgba(255,255,255,0.12) 2px, transparent 2px, transparent 4px);
}
.left label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; cursor: grab; user-select: none; }
.left.dragging label { cursor: grabbing; }
.left .editor {
  height: calc(100% - 70px);
  display: grid;
  grid-template-columns: auto 1fr; /* ensure gutter respects explicit width */
  gap: 0;
}
.line-gutter {
  background: #0f1420;
  color: var(--muted);
  border: 1px solid #1e2636;
  border-right: none;
  border-radius: 8px 0 0 8px;
  padding: 10px 8px 10px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px; /* match textarea for exact line height */
  line-height: 1.4;
  text-align: right;
  user-select: none;
  overflow: auto; /* allow programmatic scroll sync */
  scrollbar-width: none; /* Firefox hide */
  font-variant-numeric: tabular-nums; /* stable digit width */
  white-space: nowrap;
  min-width: 3ch;
}
.line-gutter::-webkit-scrollbar { width: 0; height: 0; }
.line-gutter div { height: var(--rowh, 1.4em); }
.left .editor textarea {
  width: 100%;
  height: 100%;
  resize: none;
  background: #0f1420;
  color: #e6e6e6;
  border: 1px solid #1e2636;
  border-left: none;
  border-radius: 0 8px 8px 0;
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 13px;
  line-height: 1.4;
  /* Keep line numbers in sync by disabling soft-wrap */
  white-space: pre;
  overflow-x: auto;
  word-break: normal;
  overflow-wrap: normal;
}
.hint { margin-top: 8px; color: var(--muted); font-size: 12px; }
.error { margin-top: 8px; color: var(--danger); font-size: 12px; min-height: 18px; }

.right { position: absolute; inset: 0; }
#viewport {
  width: 100%; height: 100%; display: block; background: var(--bg);
  outline: none;
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  cursor: grab;
}

/* Nav pad */
.navpad {
  position: fixed; /* follow the visible viewport */
  left: 10px; /* JS will override to align with visible area */
  /* top is set dynamically in JS; do not set bottom to avoid stretch */
  display: grid;
  grid-template-columns: repeat(3, 28px);
  grid-auto-rows: 28px;
  gap: 6px;
  padding: 8px;
  background: rgba(17, 21, 31, 0.88); /* var(--panel) with slight opacity */
  border: 1px solid #2a3346;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  backdrop-filter: saturate(120%) blur(4px);
  z-index: 30;
}
.navpad button {
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
.navpad button:hover { border-color: var(--accent); }

/* Toast (dismissible popup) */
.toast {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(17, 21, 31, 0.98);
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 10px;
  padding: 10px 12px;
  display: none;
  z-index: 100;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
  max-width: 80vw;
}
.toast[aria-hidden="false"] { display: flex; align-items: center; gap: 10px; }
.toast button {
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
}
.toast button:hover { border-color: var(--accent); }

/* Tooltip for node full text */
.node-tooltip {
  position: fixed;
  display: none;
  background: rgba(17, 21, 31, 0.98);
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 10px;
  padding: 8px 10px;
  z-index: 120;
  max-width: 70vw;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
  font-size: 13px;
  line-height: 1.4;
  pointer-events: none; /* don't steal hover */
}

.about-panel[aria-hidden="true"] { display: none; }
.about-panel[aria-hidden="false"] { display: block; }
.about-panel {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 110;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: calc(var(--topbar-h) + 24px);
}
.about-content {
  background: rgba(17, 21, 31, 0.98);
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 12px;
  width: min(720px, 92vw);
  padding: 16px 18px 18px 18px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.45);
  position: relative;
}
.about-content h2 { margin: 0 0 8px 0; font-size: 16px; }
.about-content a { color: var(--accent); text-decoration: none; }
.about-content a:hover { text-decoration: underline; }
.about-content .muted { color: var(--muted); }
.about-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: #1b2230;
  color: var(--text);
  border: 1px solid #2a3346;
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
}
.about-close:hover { border-color: var(--accent); }

/* SVG node styles */
.node rect {
  fill: #141b29;
  stroke: #273149;
  stroke-width: 1;
  rx: 6; ry: 6;
}
.node .label .line { transition: transform 6s linear; will-change: transform; }
.node.hover .label .line[data-overflow="1"] {
  transform: translateX(calc(-1px * var(--overflow)));
}
.node text { font-size: 12px; fill: var(--text); }
.node .key { fill: #a7c5ff; }
.node .value { fill: #e6e6e6; }
.node .hint { fill: var(--muted); }
.node:hover rect { stroke: var(--accent); }

/* Collapsed indicator */
.node .expander {
  fill: var(--muted);
  font-size: 12px;
  opacity: 0.9;
  pointer-events: none; /* let group handle clicks */
}
.node:hover .expander { fill: var(--accent); }

.link { stroke: #394766; stroke-width: 1.2; fill: none; }
