/* =========================================================
   Theme tokens
   ========================================================= */
:root[data-theme="dark"] {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2230;
  --panel-3: #232b3a;
  --border: #2a3038;
  --border-soft: #21262d;
  --text: #e6edf3;
  --text-2: #adbac7;
  --muted: #7d8590;
  --accent: #58a6ff;
  --accent-soft: rgba(88, 166, 255, 0.14);
  --ok: #3fb950;
  --err: #f85149;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);

  --canvas-bg: #0b0f16;
  --canvas-dot: #1a2130;

  --node-bg: #1c2230;
  --node-border: #313a4b;
  --node-head: #232b3a;
  --node-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  --row-sep: #262d3a;
  --key: #7ee787;
  --val-str: #a5d6ff;
  --val-num: #ffa657;
  --val-bool: #d2a8ff;
  --val-null: #768390;
  --head-text: #8b98a8;
  --edge: #3d4658;
}

:root[data-theme="light"] {
  --bg: #f6f8fa;
  --panel: #ffffff;
  --panel-2: #f0f3f6;
  --panel-3: #e7ecf1;
  --border: #d5dbe1;
  --border-soft: #e6eaef;
  --text: #1f2328;
  --text-2: #424a53;
  --muted: #6b7480;
  --accent: #0969da;
  --accent-soft: rgba(9, 105, 218, 0.10);
  --ok: #147029;
  --err: #bc1420;
  --shadow: 0 8px 24px rgba(31, 35, 40, 0.12);
  --shadow-sm: 0 1px 4px rgba(31, 35, 40, 0.10);

  --canvas-bg: #eef1f5;
  --canvas-dot: #d3dae2;

  --node-bg: #ffffff;
  --node-border: #d5dbe1;
  --node-head: #f2f5f8;
  --node-shadow: none;
  --row-sep: #eaeef2;
  --key: #0550ae;
  --val-str: #0a7d33;
  --val-num: #953800;
  --val-bool: #8250df;
  --val-null: #a0a8b0;
  --head-text: #57606a;
  --edge: #c2cad3;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Instant theme switch — no animation when toggling dark/light */
:root.no-transition,
:root.no-transition *,
:root.no-transition *::before,
:root.no-transition *::after {
  transition: none !important;
  animation: none !important;
}

/* Professional scrollbars */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--muted) transparent;
}
::-webkit-scrollbar { width: 12px; height: 12px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 10px;
  border: 3px solid var(--panel);
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--muted); background-clip: padding-box; }
::-webkit-scrollbar-corner { background: transparent; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

body { display: flex; flex-direction: column; overflow: hidden; }

/* =========================================================
   Top bar
   ========================================================= */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 56px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 11px; }
.logo {
  color: #fff;
  font-size: 16px;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #7c5cff);
  border-radius: 8px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-name { font-weight: 650; font-size: 14px; letter-spacing: 0.2px; }
.brand-sub { font-size: 11px; color: var(--muted); }

.topbar-right { display: flex; align-items: center; gap: 8px; }
.actions { display: flex; align-items: center; gap: 8px; }

.sep { width: 1px; height: 22px; background: var(--border); margin: 0 2px; }
.sep--v { height: 18px; }

/* Burger + mobile tabs are desktop-hidden by default
   (higher specificity than .btn--icon so the hide wins) */
.topbar-right .burger { display: none; }
.mobile-tabs { display: none; }

/* Buttons */
.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 13px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.14s, border-color 0.14s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-3); border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn--ghost { background: transparent; }
.btn--ghost:hover { background: var(--panel-2); }

.btn--icon {
  width: 34px; height: 34px;
  padding: 0;
  font-size: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* Flat theme icon */
.icon { display: block; }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="light"] .icon-moon { display: none; }

/* Status pill — matches button shape */
.status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  font-size: 13px;
  font-weight: 600;
  padding: 0 13px;
  border-radius: 7px;
  border: 1px solid transparent;
  max-width: 320px;
}
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: currentColor;
  flex: 0 0 auto;
  box-shadow: 0 0 0 3px transparent;
}
.status-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status--ok {
  color: var(--ok);
  background: color-mix(in srgb, var(--ok) 12%, transparent);
  border-color: color-mix(in srgb, var(--ok) 35%, transparent);
}
.status--err {
  color: var(--err);
  background: color-mix(in srgb, var(--err) 12%, transparent);
  border-color: color-mix(in srgb, var(--err) 35%, transparent);
}

/* =========================================================
   Workspace
   ========================================================= */
.workspace { flex: 1 1 auto; display: flex; min-height: 0; }

.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.pane--editor { flex: 0 0 25%; background: var(--panel); }
.pane--graph {
  flex: 1 1 auto;
  position: relative;
  background-color: var(--canvas-bg);
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 36px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.pane-head--graph {
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.pane-title { font-size: 12px; font-weight: 600; letter-spacing: 0.4px; text-transform: uppercase; color: var(--muted); }
.pane-meta { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

.divider {
  position: relative;
  width: 5px;
  margin: 0 -2px;
  background: transparent;
  flex: 0 0 auto;
  cursor: col-resize;
  z-index: 10;
}
.divider::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 2px;
  width: 1px;
  background: var(--border);
  transition: background 0.15s, box-shadow 0.15s;
}
.divider:hover::before,
.divider.resizing::before {
  background: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
body.resizing-h { cursor: col-resize; user-select: none; }

/* Editor — highlighted overlay behind a transparent textarea */
.editor-wrap { flex: 1; position: relative; display: flex; min-height: 0; }
.code-stack { position: relative; flex: 1; min-height: 0; overflow: hidden; }

.code-highlight,
#editor {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 16px 18px;
  font-family: "JetBrains Mono", "SF Mono", "Fira Code", Consolas, monospace;
  font-size: 13px;
  line-height: 1.65;
  tab-size: 2;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  border: 0;
}

.code-highlight {
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  background: var(--panel);
  color: var(--text-2);
}
.code-highlight code { font: inherit; color: inherit; }

#editor {
  z-index: 1;
  resize: none;
  outline: none;
  overflow-y: auto;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
}
#editor::placeholder { color: var(--muted); }
#editor::selection { background: color-mix(in srgb, var(--accent) 30%, transparent); }

/* Syntax tokens — shared by the editor (<span>, styled via color)
   and the diagram (<tspan>, styled via fill). Each rule sets both;
   the irrelevant property is simply ignored per element type. */
.tok-key   { color: var(--key);       fill: var(--key); }
.tok-str   { color: var(--val-str);   fill: var(--val-str); }
.tok-num   { color: var(--val-num);   fill: var(--val-num); }
.tok-bool  { color: var(--val-bool);  fill: var(--val-bool); }
.tok-null  { color: var(--val-null);  fill: var(--val-null); font-style: italic; }
.tok-punct { color: var(--muted);     fill: var(--muted); }
.tok-head  { color: var(--head-text); fill: var(--head-text); font-weight: 600; }

.error-bar {
  position: absolute;
  left: 12px; right: 12px; bottom: 12px;
  background: color-mix(in srgb, var(--err) 14%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--err) 45%, transparent);
  color: var(--err);
  font-size: 12px;
  font-family: "JetBrains Mono", Consolas, monospace;
  padding: 9px 12px;
  border-radius: 8px;
}
/* Neutral (non-error) notice — e.g. truncation of very large documents */
.error-bar--warn {
  background: var(--panel-2);
  border-color: var(--border);
  color: var(--text-2);
}

/* =========================================================
   Graph canvas
   ========================================================= */
.canvas {
  position: absolute;
  inset: 0;
  cursor: grab;
  touch-action: none; /* we handle pan/pinch ourselves via pointer events */
  background-image: radial-gradient(circle at 1px 1px, var(--canvas-dot) 1.2px, transparent 0);
  background-size: 22px 22px;
}
.canvas:active { cursor: grabbing; }
#graph { width: 100%; height: 100%; display: block; }

.graph-toolbar {
  position: absolute;
  top: 46px; right: 14px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 4px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  backdrop-filter: blur(8px);
}
.graph-toolbar .btn--icon { width: 30px; height: 30px; background: transparent; border-color: transparent; }
.graph-toolbar .btn--icon:hover { background: var(--panel-2); }
.zoom-label {
  font-size: 12px; color: var(--text-2);
  min-width: 46px; text-align: center;
  font-variant-numeric: tabular-nums;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  color: var(--muted);
  pointer-events: none;
}
.empty-icon { font-size: 40px; opacity: 0.4; }
.empty-state p { margin: 0; font-size: 13px; }

/* =========================================================
   SVG node styling
   ========================================================= */
.node-rect { fill: var(--node-bg); stroke: var(--node-border); stroke-width: 1; }
.node-head-bg { fill: var(--node-head); }
.edge { fill: none; stroke: var(--edge); stroke-width: 1.6; }
.row-sep { stroke: var(--row-sep); stroke-width: 1; }
.port-dot { fill: var(--accent); }
.node-title { fill: var(--head-text); font-weight: 600; }
.mono {
  font-family: "JetBrains Mono", "SF Mono", "Fira Code", Consolas, monospace;
  font-size: 12px;
}

/* =========================================================
   Mobile tabs (JSON / Diagram switcher)
   ========================================================= */
.mobile-tabs {
  flex: 0 0 auto;
  gap: 4px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.mtab {
  flex: 1;
  padding: 9px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
}
.mtab.is-active {
  color: var(--text);
  background: var(--panel-2);
  border-color: var(--border);
}

/* =========================================================
   Responsive — phones & small tablets
   ========================================================= */
@media (max-width: 720px) {
  .brand-sub { display: none; }
  .topbar { padding: 0 12px; }

  /* Burger reveals the actions as a dropdown */
  .topbar-right .burger { display: inline-flex; }
  .topbar-right > .sep { display: none; }

  .actions {
    position: absolute;
    top: 54px; right: 10px;
    z-index: 30;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    min-width: 168px;
    padding: 6px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    display: none;
  }
  .actions.open { display: flex; }
  .actions .btn {
    width: 100%;
    justify-content: flex-start;
    text-align: left;
    border-color: transparent;
    background: transparent;
  }
  .actions .btn:hover { background: var(--panel-2); }

  /* Tabs visible; only the active pane is shown */
  .mobile-tabs { display: flex; }
  .divider { display: none; }

  .pane {
    flex: 1 1 100% !important;
    width: 100%;
  }
  .workspace[data-mobile-pane="editor"] .pane--graph { display: none; }
  .workspace[data-mobile-pane="graph"] .pane--editor { display: none; }

  /* Larger touch targets */
  .btn--icon { width: 38px; height: 38px; }
  .graph-toolbar { top: auto; bottom: 16px; right: 50%; transform: translateX(50%); }
}

@media (max-width: 400px) {
  .status-text { display: none; }
  .status { padding: 0 9px; }
}
