/* ============================================================
   Dataset builder — node canvas, rail, inspector, preview drawer.
   Built on the shared design tokens (--surface, --ink-*, --rule-*,
   --accent). Reuses shared primitives from design.css (.btn, .pill,
   .field, .menu-pop, .empty-state, .seg); only net-new rules live
   here. Locked rules honored: the output node is the single accent
   moment on the canvas; row-count/exclusion "fact chips" are neutral
   mono (amber stays reserved for real errors); hover/selection
   crossfade on compositor-only layers.
   ============================================================ */

/* ---- Index ---------------------------------------------------------------- */
/* Phone: title/sub and "Manage connections" used to share one nowrap row.
   Actions are flex-shrink:0, so at ~390 the heading collapsed into a
   ribbon. Stack them; keep every control. Desktop stays a single row. */
@media (max-width: 720px) {
  .datasets-page .page-head { flex-direction: column; align-items: stretch; gap: 14px; }
  .datasets-page .page-meta { flex-wrap: wrap; }
}
/* Keep the shared .page copilot gutter (padding-right ~84px). The
   header rule, actions, and empty state must use that content box —
   the same ~940px column Teams/Reports use at 1024 — not a full-bleed
   rule and not a skinnier inner wrap. */
.datasets-page .page-head,
.datasets-page .empty-state,
.datasets-page .dsb-list {
  width: 100%;
  max-width: none;
  box-sizing: border-box;
}
.dsb-list { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.dsb-card {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--rule-2);
  border-radius: 8px;
  color: inherit; text-decoration: none;
  position: relative; isolation: isolate;
}
.dsb-card::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  background: var(--surface-2); opacity: 0; transition: opacity .12s ease;
  pointer-events: none; z-index: -1;
}
.dsb-card:hover::after { opacity: 1; }
.dsb-card:hover { border-color: var(--rule-3); }
.dsb-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.dsb-card-icon {
  width: 34px; height: 34px; display: grid; place-items: center;
  border-radius: 7px; background: var(--surface-2); color: var(--ink-3); flex: 0 0 auto;
}
.dsb-card:hover .dsb-card-icon { color: var(--accent-ink); }
.dsb-card-main { min-width: 0; flex: 1; }
.dsb-card-name { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; }
.dsb-recipe {
  font-family: var(--mono); font-size: 11px; color: var(--ink-3);
  margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dsb-card-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 4px;
  flex: 0 0 auto; font-size: 12px; color: var(--ink-4);
}
.dsb-feeds { font-family: var(--mono); font-size: 11px; }

/* ---- Builder shell --------------------------------------------------------- */
.dsb-shell {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr) 312px;
  grid-template-rows: auto minmax(0, 1fr);
  height: calc(100vh - 48px);
  min-height: 540px;
}
.dsb-subhead {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 10px;
  height: 44px; padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule-2);
}
.dsb-name-input {
  border: 1px solid transparent; border-radius: var(--radius);
  background: none; padding: 2px 6px; margin-left: -6px;
  font-size: 14px; font-weight: 600; letter-spacing: -0.01em;
  min-width: 60px; max-width: 340px;
}
.dsb-name-input:hover { border-color: var(--rule-2); }
.dsb-name-input:focus { outline: none; border-color: var(--accent-edge); background: var(--surface); }
.dsb-autosave { font-family: var(--mono); font-size: 10.5px; color: var(--ink-4); }
.dsb-autosave.err { color: var(--bad-ink); }
.dsb-subhead-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.dsb-conflict {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px; font-size: 12.5px;
  background: var(--bad-soft); color: var(--bad-ink);
  border-bottom: 1px solid var(--rule-2);
  grid-column: 1 / -1;
}

/* ---- Left rail ------------------------------------------------------------- */
.dsb-rail {
  border-right: 1px solid var(--rule-2);
  background: var(--surface);
  overflow-y: auto;
  padding: 14px 10px 20px;
}
.dsb-rail-label {
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--ink-4);
  padding: 4px 8px; margin-top: 12px;
}
.dsb-rail-label:first-child { margin-top: 0; }
.dsb-src-head {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; font-size: 12.5px; font-weight: 500; color: var(--ink-2);
}
.dsb-src-head .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); flex: 0 0 auto; }
.dsb-src-head .sub { font-family: var(--mono); font-size: 10px; color: var(--ink-4); }
.dsb-rail-row {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 6px 8px; border-radius: 6px;
  font-size: 12.5px; color: var(--ink-2); text-align: left;
}
.dsb-rail-row:hover { background: var(--surface-2); color: var(--ink); }
.dsb-rail-row .n { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dsb-rail-row .add { opacity: 0; font-family: var(--mono); font-size: 12px; color: var(--accent-ink); }
.dsb-rail-row:hover .add { opacity: 1; }
.dsb-rail-hint { font-size: 11px; color: var(--ink-4); padding: 8px; line-height: 1.5; }
.dsb-rail-error { font-size: 11.5px; color: var(--bad-ink); padding: 8px; line-height: 1.5; }

/* ---- Canvas ---------------------------------------------------------------- */
.dsb-canvas-wrap { position: relative; display: flex; flex-direction: column; min-width: 0; }
.dsb-canvas {
  position: relative; flex: 1; overflow: auto;
  background-image: radial-gradient(var(--rule-2) 1px, transparent 1px);
  background-size: 22px 22px;
}
.dsb-canvas-inner { position: relative; width: 1100px; height: 640px; }
.dsb-edges { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.dsb-edges path { fill: none; stroke: var(--rule-3); stroke-width: 1.6; }
.dsb-edges path.hot { stroke: var(--accent-edge); }
.dsb-edges path.hit { stroke: transparent; stroke-width: 18; pointer-events: stroke; cursor: pointer; }

.dsb-node {
  position: absolute; width: 158px;
  background: var(--surface);
  border: 1px solid var(--rule-2);
  border-radius: 9px;
  padding: 9px 11px 10px;
  cursor: grab; user-select: none; -webkit-user-select: none; touch-action: none;
  isolation: isolate;
}
.dsb-node::before { /* hover tint on a compositor layer */
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  background: var(--hover); opacity: 0; transition: opacity .1s ease;
  pointer-events: none; z-index: -1;
}
.dsb-node:hover::before { opacity: 1; }
.dsb-node:hover { border-color: var(--rule-3); }
.dsb-node.sel { border-color: var(--accent-edge); box-shadow: 0 0 0 3px var(--accent-soft); } /* focus rings snap */
.dsb-node:focus-visible { outline: none; border-color: var(--accent-edge); box-shadow: 0 0 0 3px var(--accent-soft); }
.dsb-node.drag { cursor: grabbing; }
.dsb-node.anim { transition: left .24s ease, top .24s ease; }
.dsb-node.born { animation: dsb-pop .2s ease; }
@keyframes dsb-pop { from { opacity: 0; transform: scale(.92); } to { opacity: 1; transform: scale(1); } }

.dsb-node-kind {
  font-family: var(--mono); font-size: 9px; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--ink-4);
  display: flex; align-items: center; gap: 5px;
}
.dsb-node-name { font-size: 13px; font-weight: 600; margin-top: 3px; letter-spacing: -0.01em; }
.dsb-node-sub {
  font-family: var(--mono); font-size: 10.5px; color: var(--ink-3); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* Row counts/exclusions are stated as neutral facts — never amber. */
.dsb-fact {
  margin-top: 6px; display: inline-block;
  font-family: var(--mono); font-size: 9.5px;
  color: var(--ink-3); background: var(--surface-2);
  border-radius: 4px; padding: 2px 6px; white-space: nowrap;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis;
}
.dsb-fact.err { color: var(--bad-ink); background: var(--bad-soft); } /* real errors only */

/* The one accent moment on the canvas: the output/metric node. */
.dsb-node.out { background: var(--accent-soft); border-color: var(--accent-edge); }
.dsb-node.out .dsb-node-kind, .dsb-node.out .dsb-node-name { color: var(--accent-ink); }
.dsb-node.out .dsb-node-sub { color: var(--accent-ink); opacity: 0.75; }
.dsb-node.out .dsb-fact { background: var(--surface); color: var(--accent-ink); }

.dsb-node-plus, .dsb-edge-plus {
  position: absolute; width: 22px; height: 22px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--rule-3); color: var(--ink-3);
  display: grid; place-items: center; font-size: 14px; line-height: 1;
  opacity: 0; transition: opacity .1s ease; z-index: 5;
}
.dsb-node-plus { right: -11px; top: 50%; transform: translateY(-50%); }
.dsb-node:hover .dsb-node-plus, .dsb-node.sel .dsb-node-plus { opacity: 1; }
.dsb-edge-plus { transform: translate(-50%, -50%); pointer-events: none; }
.dsb-edge-plus.show { opacity: 1; pointer-events: auto; }
.dsb-node-plus:hover, .dsb-edge-plus:hover {
  color: var(--accent-ink); border-color: var(--accent-edge); background: var(--accent-soft);
}

/* Add-step picker: extends .menu-pop with positioning inside the canvas. */
.dsb-picker { position: absolute; z-index: 80; min-width: 170px; }

.dsb-canvas-empty {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none;
}

/* ---- Preview drawer -------------------------------------------------------- */
.dsb-drawer { border-top: 1px solid var(--rule-2); background: var(--surface); flex: 0 0 auto; }
.dsb-drawer-bar {
  display: flex; align-items: center; gap: 10px;
  height: 34px; padding: 0 14px; width: 100%; text-align: left;
  font-size: 12px; color: var(--ink-3); cursor: pointer;
}
.dsb-drawer-bar:hover { background: var(--surface-2); }
.dsb-drawer-bar .tri { font-size: 9px; color: var(--ink-4); transition: transform .15s ease; }
.dsb-drawer.open .tri { transform: rotate(180deg); }
.dsb-drawer-bar .at { font-family: var(--mono); font-size: 11px; color: var(--ink-2); }
.dsb-drawer-bar .cnt { margin-left: auto; font-family: var(--mono); font-size: 11px; color: var(--ink-4); }
.dsb-drawer-body { display: none; max-height: 190px; overflow: auto; border-top: 1px solid var(--rule); }
.dsb-drawer.open .dsb-drawer-body { display: block; }
.dsb-preview-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.dsb-preview-table th {
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 500; text-align: left;
  padding: 7px 14px; border-bottom: 1px solid var(--rule);
  position: sticky; top: 0; background: var(--surface);
}
.dsb-preview-table td {
  padding: 6px 14px; border-bottom: 1px solid var(--rule);
  color: var(--ink-2); font-family: var(--mono); font-size: 11.5px; white-space: nowrap;
}
.dsb-preview-table th.num, .dsb-preview-table td.num { text-align: right; }
.dsb-preview-table tr:hover td { background: var(--surface-2); }
.dsb-preview-empty, .dsb-preview-error { padding: 14px; font-size: 12.5px; color: var(--ink-4); }
.dsb-preview-error { color: var(--bad-ink); }

/* ---- Inspector ------------------------------------------------------------- */
.dsb-inspector {
  border-left: 1px solid var(--rule-2);
  background: var(--surface);
  display: flex; flex-direction: column; overflow: hidden;
}
.dsb-inspector-head {
  padding: 12px 14px 10px; border-bottom: 1px solid var(--rule); flex: 0 0 auto;
}
.dsb-inspector-kind {
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--ink-4); margin: 0 0 2px;
}
.dsb-inspector-title { font-size: 14px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.dsb-inspector-body { flex: 1; overflow-y: auto; padding: 14px; }
.dsb-inspector-empty { font-size: 12.5px; color: var(--ink-4); line-height: 1.5; }
.dsb-stat {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; background: var(--surface-2); border-radius: 6px;
  font-size: 12px; color: var(--ink-3); margin-bottom: 12px;
}
.dsb-stat b { font-family: var(--mono); font-size: 11.5px; font-weight: 500; color: var(--ink); }
.dsb-inspector-note {
  font-size: 11.5px; color: var(--ink-4); line-height: 1.5;
  padding-top: 10px; border-top: 1px solid var(--rule);
}
/* The SQL step's own field. A query needs room and must not re-wrap on every
   keystroke, so it keeps its lines: no soft wrap, scroll sideways instead. */
.dsb-sql {
  min-height: 180px; font-size: 11.5px; line-height: 1.55;
  white-space: pre; overflow-wrap: normal; overflow-x: auto; tab-size: 2;
}
.field label .opt {
  font-size: 9.5px; letter-spacing: 0.1em; color: var(--ink-4);
  margin-left: 6px; text-transform: uppercase;
}
.dsb-inspector-foot {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; border-top: 1px solid var(--rule); flex: 0 0 auto;
}
.dsb-inspector-foot .btn { white-space: nowrap; flex: 0 0 auto; }
.dsb-inspector-foot-hint { font-size: 11px; color: var(--ink-4); flex: 1; min-width: 0; line-height: 1.4; }

/* Publish panel slides over the inspector. */
.dsb-publish { display: none; position: absolute; inset: 0; background: var(--surface); z-index: 10;
  flex-direction: column; overflow-y: auto; }
.dsb-inspector { position: relative; }
.dsb-publish.on { display: flex; }
.dsb-publish-body { padding: 14px; flex: 1; overflow-y: auto; }
.dsb-publish-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 12px 14px; border-top: 1px solid var(--rule); flex: 0 0 auto;
}

/* ---- Scorecard row chip ---------------------------------------------------- */
.dsb-chip {
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--accent-ink); background: var(--accent-soft);
  border-radius: 4px; padding: 1px 6px; margin-left: 6px; text-decoration: none;
}
.dsb-chip:hover { border-radius: 4px; filter: brightness(0.96); }

/* ---- Narrow screens: inspector overlays ------------------------------------ */
@media (max-width: 980px) {
  .dsb-shell { grid-template-columns: 200px minmax(0, 1fr); }
  .dsb-inspector { position: fixed; right: 0; top: 92px; bottom: 0; width: 300px;
    box-shadow: var(--shadow-pop); z-index: 55; }
}

@media (prefers-reduced-motion: reduce) {
  .dsb-node.anim { transition: none; }
  .dsb-node.born { animation: none; }
  .dsb-card::after, .dsb-node::before, .dsb-node-plus, .dsb-edge-plus { transition: none; }
}
