/*
 * One quiet instrument, dark by default and light when the system asks
 * (A52). Everything is built from the tokens below; the graph and the pages
 * share them so that a state is the same colour on a card, in a pill and in
 * a toast.
 */

:root {
  --bg: #0e1014;
  --surface: #161920;
  --surface-2: #1d2129;
  --surface-3: #262b35;
  --line: #2a2f3a;
  --line-strong: #3a4150;
  --ink: #e7e9ee;
  --ink-2: #c3c8d4;
  --muted: #8a92a4;
  --accent: #7aa7ff;
  --accent-strong: #5b8ff5;
  --accent-ink: #0b1220;
  --accent-soft: rgba(122, 167, 255, .14);
  --ok: #3ad184;
  --warn: #f5b454;
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, .12);
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --grid-dot: rgba(255, 255, 255, .07);
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #f3f4f7;
    --surface-3: #e9ebf0;
    --line: #dde0e7;
    --line-strong: #c6cad4;
    --ink: #171a20;
    --ink-2: #363b47;
    --muted: #6b7280;
    --accent: #2563eb;
    --accent-strong: #1d4ed8;
    --accent-ink: #ffffff;
    --accent-soft: rgba(37, 99, 235, .1);
    --ok: #15803d;
    --warn: #b45309;
    --danger: #b42318;
    --danger-soft: rgba(180, 35, 24, .08);
    --shadow: 0 10px 30px rgba(20, 24, 40, .15);
    --grid-dot: rgba(0, 0, 0, .09);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 13.5px/1.5 var(--font);
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 20px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; font-weight: 600; margin: 0; }
h3 { font-size: 11px; font-weight: 600; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
p { margin: 0 0 10px; }
code, .mono { font-family: var(--mono); font-size: 12px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.crumbs { color: var(--muted); font-size: 12px; margin: 0 0 2px; }
.num { text-align: right; }
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; }
[hidden] { display: none !important; }

/* --- controls ----------------------------------------------------------- */

button, select, input, textarea {
  font: inherit;
  color: inherit;
}

input[type=text], input[type=email], input[type=password], select, textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  outline: none;
  transition: border-color .12s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; line-height: 1.5; }
textarea.mono { font-family: var(--mono); font-size: 12.5px; }
select { appearance: none; -webkit-appearance: none; padding-right: 28px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 55%, calc(100% - 10px) 55%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat; }

label { display: block; font-size: 12px; font-weight: 500; color: var(--ink-2); margin: 14px 0 5px; }
label:first-child { margin-top: 0; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  text-decoration: none;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: transparent; background: var(--danger-soft); }
.btn.danger:hover { border-color: var(--danger); }
.btn.quiet { background: transparent; border-color: transparent; color: var(--muted); }
.btn.quiet:hover { background: var(--surface-3); color: var(--ink); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.wide { width: 100%; justify-content: center; }
.btn.icon { padding: 4px 8px; font-size: 16px; line-height: 1; }

/* drop-down menus (ui.js menu) */
.menu-anchor { position: relative; display: flex; }
.menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 20;
  min-width: 170px;
  max-height: 60vh;
  overflow: auto;
  padding: 4px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu-anchor.align-right .menu { left: auto; right: 0; }
.menu hr { width: 100%; margin: 4px 0; border: none; border-top: 1px solid var(--line); }
.menu-item {
  display: block;
  width: 100%;
  padding: 6px 10px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--ink-2);
  font: inherit;
  font-size: 13px;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-3); color: var(--ink); text-decoration: none; outline: none; }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover, .menu-item.danger:focus-visible { background: var(--danger-soft); color: var(--danger); }

.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.form-error {
  margin: 14px 0 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 12.5px;
}

/* --- top bar ------------------------------------------------------------ */

.top {
  display: flex;
  align-items: center;
  gap: 18px;
  height: 46px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex: none;
}
.wordmark { font-weight: 700; letter-spacing: -.02em; color: var(--ink); font-size: 14px; }
.wordmark:hover { text-decoration: none; }
.wordmark.big { font-size: 18px; }
.top-nav { display: flex; align-items: center; gap: 6px; }
.top-link { padding: 5px 10px; border-radius: 6px; color: var(--muted); font-weight: 500; }
.top-link:hover { color: var(--ink); text-decoration: none; background: var(--surface-2); }
.top-link.active { color: var(--ink); background: var(--surface-3); }
.scope-tab { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-sep { width: 1px; height: 18px; background: var(--line); margin: 0 4px; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 12.5px; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: auto; }

/* --- pages -------------------------------------------------------------- */

.page { width: 100%; max-width: 760px; margin: 0 auto; padding: 28px 24px 60px; }
.page.wide { max-width: 1100px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; }
.page-head p { margin: 2px 0 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 20px;
}
.card + .card { margin-top: 16px; }
.danger-zone { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.danger-zone p { margin: 0; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.checklist { border: 1px solid var(--line); border-radius: 6px; max-height: 160px; overflow: auto; padding: 4px 8px; background: var(--surface-2); }
.check { display: flex; align-items: center; gap: 8px; margin: 3px 0; font-weight: 400; color: var(--ink); font-size: 13px; }
.check input { width: auto; margin: 0; }

.centered { margin: auto; text-align: center; padding: 40px 24px; max-width: 460px; }
.centered h1 { margin-bottom: 8px; }
.centered .btn { margin-top: 4px; }

.grid { border-collapse: collapse; width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.grid th, .grid td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid th { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.grid tbody tr:last-child td { border-bottom: none; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: var(--surface-2); }
.row-link { color: var(--ink); font-weight: 600; }
.grid td:first-child { white-space: nowrap; }

.empty-state { text-align: center; max-width: 380px; }
.empty-state h2 { margin-bottom: 6px; }
.empty-state.static { margin: 40px auto; }

/* --- auth --------------------------------------------------------------- */

.auth { flex: 1; display: flex; align-items: center; justify-content: center; padding: 40px 24px; }
.auth-card { width: 360px; }
.auth-card .wordmark { display: block; margin-bottom: 18px; }
.auth-card h1 { font-size: 18px; margin-bottom: 14px; }
.login-code {
  margin: 18px 0 8px;
  padding: 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 26px;
  letter-spacing: .12em;
  text-align: center;
}

/* --- graph page --------------------------------------------------------- */

.graph-page { flex: 1; min-height: 0; display: flex; }
.canvas {
  flex: 1;
  position: relative;
  overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(circle, var(--grid-dot) 1px, transparent 1.2px);
  background-size: 24px 24px;
  cursor: grab;
}
.canvas.panning { cursor: grabbing; }
svg.graph { display: block; width: 100%; height: 100%; user-select: none; touch-action: none; }

.canvas-actions { position: absolute; top: 12px; right: 12px; display: flex; gap: 8px; z-index: 2; }
.canvas-tools {
  position: absolute;
  bottom: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 2;
}
.canvas-tools button {
  min-width: 28px;
  height: 26px;
  padding: 0 8px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  font-size: 12.5px;
}
.canvas-tools button:hover { background: var(--surface-3); color: var(--ink); }
.canvas-tools .sep { width: 1px; height: 16px; background: var(--line); margin: 0 3px; }
.canvas-hint { position: absolute; bottom: 16px; left: 14px; margin: 0; color: var(--muted); font-size: 11.5px; pointer-events: none; }
.canvas .empty-state { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

/* cards */
.node { cursor: pointer; }
.node-box { fill: var(--surface); stroke: var(--line-strong); stroke-width: 1; transition: stroke .12s; }
.node:hover .node-box { stroke: var(--muted); }
.node.selected .node-box { stroke: var(--accent); stroke-width: 2; }
.node.target .node-box { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 4 3; }
.node-bar { fill: var(--line-strong); }
.node.working .node-bar { fill: var(--ok); }
.node.waiting_approval .node-bar { fill: var(--warn); }
.node.idle .node-bar { fill: var(--muted); }
.node.unknown .node-bar { fill: var(--accent); }
.node-name { font-size: 13.5px; font-weight: 600; fill: var(--ink); }
.node.offline .node-name { fill: var(--ink-2); }
.node-role-box { fill: var(--accent-soft); }
.node-role { font-size: 10.5px; font-weight: 600; fill: var(--accent); letter-spacing: .02em; }
.node-desc { font-size: 11.5px; fill: var(--muted); }
.node-desc.empty { fill: var(--danger); }
.node-state { font-size: 11px; fill: var(--ink-2); }
.node.offline .node-state { fill: var(--muted); }
.node-branch { font-size: 10.5px; fill: var(--muted); font-family: var(--mono); }
/* The dot carries the session state: green working, amber waiting on a
   permission dialog, grey idle, hollow for an online session that has not
   reported anything, dim for offline. */
.node-dot { fill: var(--line-strong); }
.node.working .node-dot { fill: var(--ok); }
.node.waiting_approval .node-dot { fill: var(--warn); }
.node.idle .node-dot { fill: var(--muted); }
.node.unknown .node-dot { fill: none; stroke: var(--accent); stroke-width: 1.5; }
.badge-box { fill: var(--ink); }
.badge-text { font-size: 10.5px; font-weight: 600; fill: var(--bg); }
.port { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; cursor: crosshair; transition: fill .12s; }
.port:hover, .node.selected .port { fill: var(--accent); }

/* edges */
.edge-line { stroke: var(--line-strong); stroke-width: 1.5; fill: none; transition: stroke .12s; }
.edge:hover .edge-line { stroke: var(--muted); }
.edge-hit { stroke: transparent; stroke-width: 14; fill: none; cursor: pointer; }
.edge.selected .edge-line { stroke: var(--accent); stroke-width: 2.5; }
.arrow { fill: var(--line-strong); }
.arrow.selected { fill: var(--accent); }
.link-ghost { stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 4; fill: none; pointer-events: none; }

/* panel */
.panel {
  width: 380px;
  flex: none;
  border-left: 1px solid var(--line);
  background: var(--surface);
  overflow: auto;
  padding: 16px 18px 40px;
}
.panel-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 14px; }
.panel section { padding: 14px 0; border-top: 1px solid var(--line); }
.panel section:first-of-type { border-top: none; padding-top: 0; }
.panel .danger-zone { justify-content: flex-start; }
.panel h3 label { display: inline; font: inherit; color: inherit; margin: 0; }
.panel textarea { margin-top: 2px; }
.panel .form-actions { margin-top: 8px; }

.facts { display: grid; grid-template-columns: 84px 1fr; gap: 6px 10px; margin: 0; font-size: 12.5px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.facts dd.mono { font-size: 11.5px; }

.pill {
  display: inline-block;
  padding: 1px 8px 1px 18px;
  border-radius: 10px;
  font-size: 11.5px;
  font-weight: 500;
  position: relative;
  background: var(--surface-3);
  color: var(--ink-2);
}
.pill::before {
  content: '';
  position: absolute;
  left: 7px;
  top: 50%;
  width: 6px;
  height: 6px;
  margin-top: -3px;
  border-radius: 50%;
  background: var(--muted);
}
.pill.working::before { background: var(--ok); }
.pill.waiting_approval::before { background: var(--warn); }
.pill.unknown::before { background: transparent; box-shadow: inset 0 0 0 1.5px var(--accent); }
.pill.offline { color: var(--muted); }
.pill.offline::before { background: var(--line-strong); }

.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--surface-3);
  color: var(--ink);
  font-size: 12px;
}
.chip:hover { background: var(--accent-soft); color: var(--accent); text-decoration: none; }

.code-row { display: flex; align-items: center; gap: 8px; }
.code-row code {
  flex: 1;
  padding: 7px 10px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  overflow-x: auto;
  white-space: nowrap;
}

/* --- toasts ------------------------------------------------------------- */

/* Bottom centre: the canvas tools own the bottom-right corner and the
   panel's own buttons sit above it on the right. */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 60px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 20;
  width: max-content;
  max-width: 480px;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  box-shadow: var(--shadow);
  font-size: 12.5px;
  animation: toast-in .15s ease-out;
}
.toast.error { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.toast span { flex: 1; overflow-wrap: anywhere; }
.toast-close { border: none; background: none; color: inherit; cursor: pointer; font-size: 15px; line-height: 1; padding: 0; opacity: .7; }
.toast-close:hover { opacity: 1; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* --- dialog ------------------------------------------------------------- */

.dialog {
  width: 420px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dialog-form { padding: 20px; margin: 0; }
.dialog-form h2 { margin-bottom: 10px; }
.dialog-form p { color: var(--ink-2); }
