/* Flow design system — MANUAL §6. Tokens on :root (light) and [data-theme="dark"]. */

:root {
  --bg: #f4f5f7;          /* page ground */
  --surface: #ffffff;     /* cards, panels, topbar */
  --surface-2: #eceef1;   /* inset areas, inactive tabs */
  --border: #d7dae0;
  --text: #1a1d23;
  --muted: #6b7280;
  --accent: #4f46e5;
  --red: #dc2626;
  --green: #15803d;
  --amber: #b45309;
  --flow: #2563eb;        /* selected-edge flow highlight */
  --shadow: 0 1px 3px rgba(16, 18, 24, 0.10);
}

[data-theme="dark"] {
  --bg: #101216;
  --surface: #181b21;
  --surface-2: #22262e;
  --border: #2e333d;
  --text: #e7e9ee;
  --muted: #8b93a1;
  --accent: #818cf8;
  --red: #f05252;
  --green: #4ade80;
  --amber: #fbbf24;
  --flow: #7cc0ff;        /* light blue reads best on the dark canvas */
  --shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, "Segoe UI", Inter, sans-serif;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none; /* no rubber-band/pull-to-refresh under the canvas */
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: stretch;
  gap: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
  height: 46px;
  flex: none;
}

.brand {
  align-self: center;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0 6px;
}

/* Back to the portal — full-screen /flow only (main.js hides it in the pane iframe) */
.back-btn {
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  text-decoration: none;
  padding-bottom: 3px; /* optically center the ‹ glyph */
}
.back-btn:hover { background: var(--surface-2); }
.back-btn[hidden] { display: none; }

/* Sheet tabs — spreadsheet-style, attached to the bottom edge of the bar */

.tabs {
  display: flex;
  align-items: flex-end;
  gap: 2px;
}

.tab {
  appearance: none;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: var(--surface-2);
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  padding: 7px 16px 6px;
  cursor: pointer;
}

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

.tab[aria-selected="true"] {
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  position: relative;
}

/* mask the topbar border under the active tab so it reads as connected to the sheet */
.tab[aria-selected="true"]::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 1px;
  background: var(--bg);
}

.topbar-right {
  margin-left: auto;
  align-self: center;
  display: flex;
  gap: 8px;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  width: 30px;
  height: 30px;
  cursor: pointer;
}

.icon-btn:hover { background: var(--surface-2); }

/* ---------- Sheets ---------- */

#sheets {
  flex: 1;
  min-height: 0;
  position: relative;
}

.sheet {
  position: absolute;
  inset: 0;
  overflow: auto;
}

/* ---------- Canvas — Sheet 1 (MANUAL §5.1, §6, §7) ---------- */

#sheet-flow { overflow: hidden; }

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: var(--bg);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: default;
}

#canvas.space-pan { cursor: grab; }
#canvas.panning { cursor: grabbing; }
#canvas[data-tool="rect"],
#canvas[data-tool="ellipse"],
#canvas[data-tool="line"] { cursor: crosshair; }
#canvas[data-tool="text"] { cursor: text; }

/* lanes */
.lane { fill: transparent; }
.lane-alt { fill: var(--surface-2); fill-opacity: 0.5; }
.lane-edge { stroke: var(--border); stroke-width: 1; vector-effect: non-scaling-stroke; }
.lane-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  fill: var(--muted);
}

/* step nodes — status treatments per §5.1 "Red = fix me" */
.node .body { fill: var(--surface); stroke: var(--border); stroke-width: 1.5; }
.node .title { fill: var(--text); font-size: 13px; font-weight: 600; }

/* question nodes: branch points that route people down different paths */
.node.type-question .body {
  fill: color-mix(in srgb, var(--accent) 10%, var(--surface));
  stroke: var(--accent);
}
.node.status-underdeveloped .body { stroke: var(--red); stroke-width: 2; stroke-dasharray: 7 5; }
.node.status-broken .body { stroke: var(--red); stroke-width: 2.5; }
.node.status-broken { filter: drop-shadow(0 0 7px var(--red)); }
.node.status-planned .body { stroke: var(--muted); stroke-dasharray: 5 5; }
.node.status-planned .title { fill: var(--muted); }

/* Gesture-time paint diet: SVG filters and dash animations force expensive
   repaints on every transform frame — the single biggest source of pan/zoom
   lag on phones. canvas.js flags live gestures with .moving; the glow and
   the flow animation come back the instant fingers lift. */
#canvas.moving .node.status-broken { filter: none; }
#canvas.moving .drawing.selected { filter: none; }
#canvas.moving .edge-g.downstream .edge { animation: none; }

.sel-ring { fill: none; stroke: var(--accent); stroke-width: 2; visibility: hidden; }
.node.selected .sel-ring { visibility: visible; }
.drawing.selected { filter: drop-shadow(0 0 4px var(--accent)); }

/* LOD below zoom 0.2: titles become solid bars (§7) */
.lodbar { visibility: hidden; fill: var(--border); }
svg.lod .lodbar { visibility: visible; }
svg.lod .node .title { visibility: hidden; }
.node.status-broken .lodbar,
.node.status-underdeveloped .lodbar { fill: var(--red); }
.node.status-planned .lodbar { fill: var(--surface-2); }

/* edges — one group per edge; the label chip sits ON its line */
.edge-g { pointer-events: none; }
.edge { fill: none; stroke: var(--muted); stroke-width: 1.5; }
.edge-g.status-broken .edge { stroke: var(--red); stroke-dasharray: 7 5; }
.arrowhead { fill: var(--muted); }
.arrowhead-broken { fill: var(--red); }
.arrowhead-flow { fill: var(--flow); }

.chip-bg { fill: var(--surface); stroke: var(--border); stroke-width: 1.25; }
.chip-t { font-size: 10.5px; font-weight: 600; fill: var(--muted); }
.edge-g.status-broken .chip-bg { stroke: var(--red); }
.edge-g.status-broken .chip-t { fill: var(--red); }
svg.lod .chip { display: none; }

/* selected node → outgoing edges glow and FLOW toward the next box */
.edge-g.downstream .edge {
  stroke: var(--flow);
  stroke-width: 2.75;
  stroke-dasharray: 9 7;
  animation: edge-flow 0.8s linear infinite;
}
.edge-g.downstream.status-broken .edge { stroke: var(--red); }
.edge-g.downstream .chip-bg { stroke: var(--flow); }
.edge-g.downstream .chip-t { fill: var(--flow); }
@keyframes edge-flow { to { stroke-dashoffset: -16; } }

/* views: filtered-out elements vanish; problems-view context dims */
#canvas .hidden { display: none; }
.node.dimmed { opacity: 0.35; }

#view-bar {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 5px;
  box-shadow: var(--shadow);
}

#view-select {
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 4px 8px;
  cursor: pointer;
}

#focus-btn {
  font: inherit;
  font-size: 12.5px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--muted);
  padding: 4px 10px;
  cursor: pointer;
}

#focus-btn[aria-pressed="true"] {
  background: var(--flow);
  border-color: var(--flow);
  color: #fff;
  font-weight: 600;
}

/* section-label nodes: text only, no box */
.node.type-label .label-hit { fill: transparent; pointer-events: all; }
.node.type-label .label-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  fill: var(--muted);
}

/* drawings */
.drawing .hit { stroke: #000; stroke-opacity: 0; pointer-events: stroke; }
.drawn-text { font-weight: 500; }
.handle { fill: var(--surface); stroke: var(--accent); cursor: nwse-resize; }

/* floating chrome: toolbar, style panel, zoom controls */
#toolbar {
  position: absolute;
  top: 14px;
  left: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  box-shadow: var(--shadow);
}

.tool-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
}

.tool-btn:hover { background: var(--surface-2); }
.tool-btn[aria-pressed="true"] { background: var(--accent); color: #fff; }

#style-panel {
  position: absolute;
  top: 14px;
  left: 66px;
  width: 216px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 12.5px;
}

#style-panel[hidden] { display: none; }

.sp-row { display: flex; align-items: center; gap: 8px; }
.sp-row label { width: 42px; color: var(--muted); }
.sp-row input[type="color"] {
  width: 28px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px;
  background: none;
}
.sp-row input[type="range"] { flex: 1; min-width: 0; }
#sp-text {
  flex: 1;
  min-width: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  padding: 4px 6px;
}
#sp-size {
  width: 54px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  padding: 4px 6px;
}
#sp-width-val { color: var(--muted); min-width: 14px; text-align: right; font-variant-numeric: tabular-nums; }

#zoom-controls {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  box-shadow: var(--shadow);
}

#zoom-controls button {
  border: none;
  background: transparent;
  color: var(--text);
  min-width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  padding: 0 6px;
}

#zoom-controls button:hover { background: var(--surface-2); }
#zoom-fit { font-size: 12px; }
#zoom-readout {
  min-width: 46px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Inspector — right slide-in panel (MANUAL §5.1) ---------- */

#inspector {
  position: absolute;
  top: 12px;
  right: 12px;
  bottom: 12px;
  width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transform: translateX(calc(100% + 24px));
  transition: transform 0.18s ease;
  z-index: 5;
}

#inspector.open { transform: translateX(0); }

.ins-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.ins-flow-tag {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.ins-title {
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 6px;
}

.ins-title:hover, .ins-title:focus { border-color: var(--border); background: var(--surface-2); outline: none; }

.ins-status { display: flex; gap: 4px; }

.ins-status-btn {
  flex: 1;
  font: inherit;
  font-size: 11px;
  padding: 4px 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--muted);
  cursor: pointer;
}

.ins-status-btn.active { font-weight: 700; color: var(--text); border-width: 1.5px; }
.ins-status-btn.status-solid.active { border-color: var(--green); color: var(--green); }
.ins-status-btn.status-underdeveloped.active,
.ins-status-btn.status-broken.active { border-color: var(--red); color: var(--red); }
.ins-status-btn.status-planned.active { border-color: var(--muted); }

.ins-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 6px;
}

.ins-label-row { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }
.ins-label-row .ins-label { margin-top: 0; }

.ins-mini-btn {
  font: inherit;
  font-size: 11px;
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}

#inspector textarea {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  resize: vertical;
}

#ins-details-list {
  margin: 4px 0 0;
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
}

#ins-details-list li::marker { color: var(--accent); font-weight: 600; }
.ins-details-empty { color: var(--muted); list-style: none; margin-left: -22px; }

#ins-evidence {
  margin: 4px 0 0;
  padding-left: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  color: var(--muted);
  word-break: break-all;
}

#ins-models { display: flex; gap: 6px; flex-wrap: wrap; }

.ins-model-chip {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}

#boot-error {
  margin: 18vh auto 0;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--red);
  border-radius: 10px;
  padding: 20px 24px;
  color: var(--red);
  text-align: center;
}

/* ---------- Shell placeholders (removed as real sheet views land) ---------- */

.placeholder-card {
  max-width: 460px;
  margin: 18vh auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 28px 32px;
  text-align: center;
}

.placeholder-card h2 {
  margin: 0 0 10px;
  font-size: 17px;
}

.placeholder-stats {
  margin: 0 0 6px;
  font-variant-numeric: tabular-nums;
}

.placeholder-note {
  margin: 0;
  color: var(--muted);
  font-size: 12.5px;
}

/* ---------- Mobile (≤ 700px): thumb-sized chrome, bottom-sheet inspector ----------
   The canvas itself is gesture-first on touch: one finger pans (or drags a
   node), two fingers pinch-zoom about the midpoint. The chrome moves to the
   edges of the thumb zone: tools bottom-left, zoom stack above them on the
   right, views full-width up top, inspector as a bottom sheet. */

@media (max-width: 700px) {
  .topbar { gap: 8px; padding: 0 8px; height: 50px; }
  .brand { display: none; } /* the back button / tabs carry the identity */
  .back-btn { width: 40px; height: 40px; font-size: 26px; }
  .tabs { overflow-x: auto; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { padding: 9px 12px 8px; font-size: 12.5px; white-space: nowrap; }
  .icon-btn { width: 38px; height: 38px; }

  #view-bar {
    top: 8px;
    left: 8px;
    right: 8px;
    transform: none;
  }
  #view-select { flex: 1; min-width: 0; padding: 8px; }
  #focus-btn { padding: 8px 12px; flex: none; }

  /* drawing tools: horizontal bar, bottom-left, out of the reach shadow */
  #toolbar {
    top: auto;
    left: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom));
    flex-direction: row;
  }
  .tool-btn { width: 44px; height: 44px; font-size: 17px; }

  /* zoom stack sits above the toolbar row so the two never collide */
  #zoom-controls {
    right: 8px;
    bottom: calc(66px + env(safe-area-inset-bottom));
  }
  #zoom-controls button { min-width: 36px; height: 36px; }

  #style-panel {
    top: auto;
    left: 8px;
    right: 8px;
    width: auto;
    bottom: calc(66px + env(safe-area-inset-bottom));
  }

  /* inspector becomes a bottom sheet */
  #inspector {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 62%;
    border-radius: 14px 14px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
    transform: translateY(calc(100% + 24px));
  }
  #inspector.open { transform: translateY(0); }
  .ins-status-btn { padding: 8px 0; }
  .ins-mini-btn { padding: 6px 14px; }
}
