:root {
  --bg: #0f1218;
  --grid: #1c212b;
  --panel: #161a22;
  --panel-2: #1d222c;
  --line: #2a303c;
  --text: #e5e7eb;
  --muted: #8b93a3;
  --accent: #38bdf8;
  --danger: #f87171;
  --node-bg: #1b202a;
  --edge: #7c8597;
  --topbar-h: 48px;
  --palette-w: 132px;
  --insp-w: 280px;
  font-family: "Segoe UI", "Yu Gothic UI", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 13px; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 10px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: #3d4556; background: #242a36; }
button.primary { background: #0e7490; border-color: #0e7490; }
button.primary:hover { background: #0891b2; }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); }
button.wide { width: 100%; }
.icon-btn { width: 30px; padding: 5px 0; text-align: center; }
input, select, textarea {
  background: #0d1016; border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; outline: none; width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 60px; line-height: 1.5; }
.muted { color: var(--muted); }
.small { font-size: 12px; line-height: 1.6; }
code { background: #0d1016; padding: 1px 4px; border-radius: 4px; }
[hidden] { display: none !important; }

/* ---------- top bar */
#topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h); z-index: 20;
  display: flex; align-items: center; gap: 10px; padding: 0 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-size: 15px; letter-spacing: .3px; white-space: nowrap; }
.brand b { color: var(--accent); }
.group { display: flex; gap: 4px; align-items: center; }
#board-select { width: 170px; }
.search input { width: 200px; }
.spacer { flex: 1; }
#users { display: flex; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; margin-left: -6px; border: 2px solid var(--panel);
  display: grid; place-items: center; font-size: 11px; font-weight: 700; color: #0b0d12; cursor: pointer;
}
.me-chip { display: flex; align-items: center; gap: 6px; }
.me-chip .dot, .conn .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.conn { display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.conn.on .dot { background: #22c55e; box-shadow: 0 0 6px #22c55e; }
.conn.off .dot { background: var(--danger); }

/* ---------- palette */
#palette {
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0; width: var(--palette-w); z-index: 10;
  background: var(--panel); border-right: 1px solid var(--line); padding: 10px 8px; overflow-y: auto;
}
.pal-title, .insp-title { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 8px; }
.pal-item {
  display: flex; align-items: center; gap: 8px; width: 100%; margin-bottom: 6px; padding: 7px 8px;
  border-left: 4px solid var(--c); text-align: left; cursor: grab;
}
.pal-item .ic { width: 16px; text-align: center; }
.pal-hint { color: var(--muted); font-size: 11px; text-align: center; margin-top: 12px; line-height: 1.6; }

/* ---------- inspector */
#inspector {
  position: fixed; top: var(--topbar-h); bottom: 0; right: 0; width: var(--insp-w); z-index: 10;
  background: var(--panel); border-left: 1px solid var(--line); padding: 12px; overflow-y: auto;
}
.insp-sec label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 12px; }
.insp-sec label > * { margin-top: 4px; color: var(--text); font-size: 13px; }
.insp-sec .wide { margin-bottom: 6px; }
.row { display: flex; gap: 6px; }
.row.end { justify-content: flex-end; margin-top: 14px; }
.edge-ends { background: #0d1016; border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-bottom: 10px; line-height: 1.6; }
.edge-ends a, .links a { color: var(--accent); cursor: pointer; text-decoration: none; }
.edge-ends a:hover, .links a:hover { text-decoration: underline; }
.links { font-size: 12px; color: var(--muted); margin-bottom: 12px; line-height: 1.7; }
.links .lbl { color: #cbd5e1; }

/* ---------- viewport */
#viewport {
  position: fixed; top: var(--topbar-h); left: var(--palette-w); right: var(--insp-w); bottom: 0;
  overflow: hidden; outline: none; user-select: none; -webkit-user-select: none; touch-action: none;
  background-color: var(--bg);
  background-image: radial-gradient(circle, var(--grid) 1.2px, transparent 1.3px);
  cursor: default;
}
#viewport.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#edge-svg { position: absolute; left: 0; top: 0; overflow: visible; }
#edge-layer .hit { stroke: transparent; stroke-width: 14; fill: none; cursor: pointer; pointer-events: stroke; }
#edge-layer .line { stroke: var(--edge); stroke-width: 2; fill: none; pointer-events: none; }
#edge-layer .dashed .line { stroke-dasharray: 7 5; }
#edge-layer .sel .line { stroke: var(--accent); stroke-width: 2.5; }
#edge-layer g:hover .line { stroke: #aab3c5; }
#edge-layer .sel:hover .line { stroke: var(--accent); }
.arr-fill { fill: var(--edge); }
.arr-fill-sel { fill: var(--accent); }
#temp-edge { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; fill: none; pointer-events: none; }

.edge-label {
  position: absolute; transform: translate(-50%, -50%); white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
  background: #11151c; border: 1px solid #343b49; border-radius: 10px; padding: 2px 8px; font-size: 12px; color: #cbd5e1; cursor: pointer;
}
.edge-label.sel { border-color: var(--accent); color: #fff; }

/* ---------- nodes */
.node { position: absolute; left: 0; top: 0; width: max-content; --c: #3b82f6; }
.node .shape {
  min-width: 150px; max-width: 250px; padding: 8px 12px 9px; border-radius: 8px;
  background: var(--node-bg); border: 1px solid #2d3441; border-left: 4px solid var(--c);
  box-shadow: 0 2px 8px rgba(0,0,0,.35); cursor: move;
}
.node .head { display: flex; gap: 6px; align-items: baseline; font-weight: 600; font-size: 14px; line-height: 1.35; }
.node .ic { color: var(--c); flex: none; font-size: 12px; }
.node .ttl { word-break: break-word; }
.node .ttl i { color: var(--muted); font-weight: 400; }
.node .scn { margin-top: 4px; font-size: 11px; color: #93c5fd; font-family: Consolas, "Cascadia Mono", monospace; }
.node .desc {
  margin-top: 5px; font-size: 12px; color: #aab1bf; white-space: pre-wrap; word-break: break-word; line-height: 1.45;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.node .foot { margin-top: 6px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 11px; }
.node .st { padding: 1px 7px; border-radius: 9px; background: color-mix(in srgb, var(--sc) 22%, transparent); color: var(--sc); border: 1px solid color-mix(in srgb, var(--sc) 50%, transparent); }
.node .asg { color: var(--muted); }

.node.t-start .shape, .node.t-end .shape { border-radius: 999px; border-left-width: 1px; border-color: var(--c); padding-left: 18px; padding-right: 18px; min-width: 120px; background: color-mix(in srgb, var(--c) 16%, var(--node-bg)); }
.node.t-branch .shape {
  border: none; padding: 12px 30px; min-width: 170px; text-align: center; border-radius: 0;
  background: color-mix(in srgb, var(--c) 22%, var(--node-bg));
  clip-path: polygon(18px 0, calc(100% - 18px) 0, 100% 50%, calc(100% - 18px) 100%, 18px 100%, 0 50%);
}
.node.t-branch .head { justify-content: center; }
.node.t-note .shape { background: #2a2717; border: 1px dashed #6b6440; border-left: 1px dashed #6b6440; box-shadow: none; }
.node.t-note .desc { -webkit-line-clamp: 12; color: #d6d0a8; }
.node.t-note .head { color: #e7dfb0; }

.node.selected { z-index: 3; }
.node.selected .shape { outline: 2px solid var(--accent); outline-offset: 3px; }
.node.t-branch.selected .shape { outline: none; }
.node.t-branch.selected { filter: drop-shadow(0 0 0 var(--accent)) drop-shadow(0 0 3px var(--accent)); }
.node.remote .shape { box-shadow: 0 0 0 2px var(--rc), 0 2px 8px rgba(0,0,0,.35); }
.node.remote::after {
  content: attr(data-by); position: absolute; left: 0; top: -20px; font-size: 11px; padding: 1px 6px; border-radius: 4px;
  background: var(--rc); color: #0b0d12; font-weight: 600; white-space: nowrap;
}
.node.drop-target .shape { outline: 2px dashed var(--accent); outline-offset: 4px; }
.node.hit .shape { animation: flash 1s ease 2; }
@keyframes flash { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 60%, transparent); } }

.port {
  position: absolute; width: 12px; height: 12px; border-radius: 50%; background: var(--accent); border: 2px solid var(--bg);
  opacity: 0; transition: opacity .12s; cursor: crosshair; z-index: 2;
}
.node:hover .port, .node.selected .port { opacity: 1; }
.port:hover { transform: scale(1.35); }
.pt { top: -6px; left: calc(50% - 6px); }
.pb { bottom: -6px; left: calc(50% - 6px); }
.pl { left: -6px; top: calc(50% - 6px); }
.pr { right: -6px; top: calc(50% - 6px); }

/* ---------- remote cursors */
.rcursor { position: absolute; left: 0; top: 0; pointer-events: none; transform-origin: 0 0; transition: transform .08s linear; z-index: 10; }
.rcursor svg { display: block; }
.rcursor span {
  position: absolute; left: 14px; top: 16px; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 4px;
  color: #0b0d12; white-space: nowrap;
}

#selbox { position: absolute; border: 1px solid var(--accent); background: rgba(56,189,248,.08); display: none; pointer-events: none; }
#zoom-ind { position: absolute; left: 10px; bottom: 10px; color: var(--muted); font-size: 12px; background: var(--panel); border: 1px solid var(--line); padding: 3px 8px; border-radius: 6px; }
#offline-banner {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); display: none;
  background: #7f1d1d; color: #fee2e2; padding: 8px 14px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,.4);
}
body.offline #offline-banner { display: block; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; transition: all .2s;
  background: #243042; border: 1px solid #36465e; padding: 8px 14px; border-radius: 8px; pointer-events: none; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- dialogs */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 18px 20px; min-width: 320px; max-width: 560px;
  box-shadow: 0 20px 50px rgba(0,0,0,.5);
}
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h3 { margin: 0 0 6px; font-size: 16px; }
#color-choices { display: flex; gap: 8px; margin-top: 12px; }
#color-choices button { width: 26px; height: 26px; border-radius: 50%; padding: 0; border: 2px solid transparent; }
#color-choices button.on { border-color: #fff; }
.keys { border-collapse: collapse; width: 100%; margin: 8px 0; }
.keys td { padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.keys td:first-child { color: var(--muted); white-space: nowrap; }

@media (max-width: 1100px) {
  .search input { width: 130px; }
  .brand { display: none; }
}
@media (max-width: 800px) {
  :root { --insp-w: 0px; --palette-w: 0px; }
  #palette, #inspector { display: none; }
}
