:root {
  --bg: #f5f6f8;
  --canvas-dot: #dde1e8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1c2230;
  --muted: #687185;
  --line: #d8dce4;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --accent-soft: rgba(47, 111, 222, 0.14);
  --edge: #8a93a6;
  --root-bg: #ffffff;
  --danger: #c62f3c;
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --shadow: 0 8px 28px rgba(20, 30, 50, 0.14), 0 1px 3px rgba(20, 30, 50, 0.08);
  --topbar-h: 52px;
  --toolbar-h: 62px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --canvas-dot: #262a33;
    --panel: #1d2028;
    --panel-2: #262a34;
    --text: #e5e8ee;
    --muted: #9aa2b4;
    --line: #353b47;
    --accent: #6f9dff;
    --accent-ink: #0c1530;
    --accent-soft: rgba(111, 157, 255, 0.2);
    --edge: #6d7588;
    --root-bg: #1d2028;
    --danger: #ff6b76;
    --warn-bg: #3a3014;
    --warn-ink: #ffdc8a;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------------------------------------------------------------- login */
#login {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 16px;
}
#login form {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 28px 24px; box-shadow: var(--shadow); display: grid; gap: 14px;
}
#login h1 { margin: 0; font-size: 22px; display: flex; align-items: center; gap: 10px; }
#login h1 img { width: 32px; height: 32px; }
#login p { margin: 0; color: var(--muted); font-size: 14px; }
.field { width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); outline: none; }
.field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 8px 14px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.err { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: fixed; left: 0; right: 0; top: 0; z-index: 20;
  height: calc(var(--topbar-h) + var(--safe-top)); padding: var(--safe-top) 8px 0;
  display: flex; align-items: center; gap: 4px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center; border: 0; border-radius: 10px;
  background: transparent; cursor: pointer; color: var(--text); flex: none;
}
.icon-btn:hover { background: var(--panel-2); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
.icon-btn.on { background: var(--accent-soft); color: var(--accent); }
.title-wrap { min-width: 0; display: flex; align-items: baseline; gap: 10px; padding: 0 4px; flex: 1 1 auto; }
.map-title {
  min-width: 0; max-width: 100%; border: 0; background: transparent; padding: 4px 6px; border-radius: 8px;
  font-weight: 650; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
}
.map-title:hover { background: var(--panel-2); }
.save-state { color: var(--muted); font-size: 12.5px; white-space: nowrap; flex: none; }
.save-state.bad { color: var(--danger); }
.top-actions { display: flex; align-items: center; gap: 2px; flex: none; }

#searchbar {
  position: fixed; z-index: 19; top: calc(var(--topbar-h) + var(--safe-top) + 8px); right: 8px;
  width: min(420px, calc(100% - 16px)); display: flex; gap: 4px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px; box-shadow: var(--shadow);
}
#searchbar input { flex: 1; min-width: 0; border: 0; outline: none; background: transparent; padding: 6px 8px; font-size: 16px; }
#search-count { color: var(--muted); font-size: 13px; padding: 0 4px; white-space: nowrap; }

#banner {
  position: fixed; z-index: 18; left: 50%; transform: translateX(-50%);
  top: calc(var(--topbar-h) + var(--safe-top) + 8px); width: min(560px, calc(100% - 16px));
  background: var(--warn-bg); color: var(--warn-ink); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow);
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 14px;
}
#banner span { flex: 1 1 220px; }

/* ---------------------------------------------------------------- canvas */
#canvas {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: grab;
  background-color: var(--bg);
  background-image: radial-gradient(var(--canvas-dot) 1px, transparent 1px);
  background-size: 22px 22px;
}
#canvas.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#edges path { fill: none; stroke-linecap: round; }
#edges path.arrow { stroke-dasharray: 5 4; stroke-width: 1.4; }

.node {
  position: absolute; left: 0; top: 0; max-width: 300px; width: max-content;
  padding: 3px 7px 4px; border-radius: 7px; font-size: 14px; line-height: 1.32;
  white-space: pre-wrap; overflow-wrap: anywhere; user-select: none; -webkit-user-select: none;
  cursor: default; color: var(--text); border: 1.5px solid transparent;
}
.node.l1 { font-size: 15.5px; }
.node.root {
  font-size: 19px; font-weight: 650; padding: 10px 18px; border-radius: 16px; max-width: 360px;
  background: var(--root-bg); border: 2px solid var(--accent); box-shadow: var(--shadow); text-align: center;
}
.node.bubble { border-color: var(--edge-c, var(--edge)); background: var(--panel); border-radius: 11px; padding: 4px 10px 5px; }
.node.bold { font-weight: 650; }
.node.italic { font-style: italic; }
.node.empty .n-text::before { content: "\00a0\00a0\00a0\00a0"; }
.node.sel { background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent); }
.node.root.sel { box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
.node.match { box-shadow: 0 0 0 2px #f5b400; }
.node.match.sel { box-shadow: 0 0 0 2px var(--accent), 0 0 0 5px rgba(245, 180, 0, 0.5); }
.node.dragging { opacity: 0.35; }
.node.drop-child { box-shadow: 0 0 0 2px var(--accent); background: var(--accent-soft); }
.node.drop-before { box-shadow: inset 0 3px 0 var(--accent); }
.node.drop-after { box-shadow: inset 0 -3px 0 var(--accent); }
.n-icons { margin-right: 3px; letter-spacing: 1px; }
.n-icon-name { font-size: 10px; padding: 0 3px; border-radius: 4px; background: var(--panel-2); color: var(--muted); margin-right: 3px; vertical-align: 2px; }
.n-badges { margin-left: 4px; color: var(--muted); font-size: 0.85em; }
.n-link { cursor: pointer; }
.n-link:hover { color: var(--accent); }
.n-fold {
  position: absolute; bottom: -9px; width: 18px; height: 18px; border-radius: 50%;
  border: 1.5px solid var(--edge-c, var(--edge)); background: var(--bg); color: var(--muted);
  font-size: 9.5px; line-height: 15px; text-align: center; font-weight: 600; cursor: pointer; font-style: normal;
}
.side-r .n-fold { right: -16px; }
.side-l .n-fold { left: -16px; }
.root .n-fold, .bubble .n-fold { bottom: calc(50% - 9px); }

.drag-ghost {
  position: fixed; z-index: 40; pointer-events: none; padding: 5px 10px; border-radius: 8px; max-width: 220px;
  background: var(--accent); color: var(--accent-ink); font-size: 13px; box-shadow: var(--shadow);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transform: translate(12px, -50%);
}

.editor {
  position: fixed; z-index: 45; margin: 0; resize: none; overflow: hidden;
  border: 2px solid var(--accent); border-radius: 8px; padding: 3px 7px; line-height: 1.32;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow); outline: none;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

#empty {
  position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none; padding: 16px;
}
#empty > div {
  pointer-events: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px;
  text-align: center; max-width: 380px; box-shadow: var(--shadow); display: grid; gap: 12px;
}
#empty h2 { margin: 0; font-size: 19px; }
#empty p { margin: 0; color: var(--muted); }
#empty .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- toolbar */
#toolbar {
  position: fixed; z-index: 20; left: 50%; transform: translateX(-50%);
  bottom: calc(14px + var(--safe-bottom)); display: flex; gap: 2px; padding: 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
#toolbar button {
  min-width: 64px; height: 50px; border: 0; border-radius: 11px; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11.5px; color: var(--text);
}
#toolbar button:hover { background: var(--panel-2); }
#toolbar button:disabled { opacity: 0.35; cursor: default; background: transparent; }
#toolbar button.on { color: var(--accent); background: var(--accent-soft); }
#toolbar .sep { width: 1px; background: var(--line); margin: 6px 3px; }
@media (min-width: 641px) { body.insp-open #toolbar { left: calc(50% - 170px); } }

@media (max-width: 640px) {
  #toolbar {
    left: 0; right: 0; bottom: 0; transform: none; border-radius: 0; border-width: 1px 0 0;
    padding: 4px 4px calc(4px + var(--safe-bottom)); justify-content: space-around; box-shadow: none;
  }
  #toolbar button { min-width: 0; flex: 1; height: 54px; font-size: 11px; }
  #toolbar .sep { display: none; }
  .hide-narrow { display: none !important; }
  .save-state { font-size: 11.5px; }
}

/* ---------------------------------------------------------------- panels */
.scrim { position: fixed; inset: 0; z-index: 29; background: rgba(10, 15, 25, 0.32); }
#drawer {
  position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(340px, 88vw);
  background: var(--panel); border-right: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column; padding-top: var(--safe-top);
  transform: translateX(-105%); transition: transform 0.18s ease;
}
#drawer.open { transform: none; }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 16px; border-bottom: 1px solid var(--line); }
.panel-head h2 { margin: 0; font-size: 16px; flex: 1; }
.drawer-tools { display: flex; gap: 8px; padding: 10px 12px; flex-wrap: wrap; }
.drawer-tools .field { flex: 1 1 100%; padding: 8px 10px; }
.drawer-tools .btn { flex: 1; }
#map-list { list-style: none; margin: 0; padding: 4px 8px 16px; overflow-y: auto; flex: 1; }
#map-list li {
  padding: 10px 10px; border-radius: 10px; cursor: pointer; display: flex; gap: 8px; align-items: center;
}
#map-list li:hover { background: var(--panel-2); }
#map-list li.current { background: var(--accent-soft); }
#map-list .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#map-list .m { font-size: 12.5px; color: var(--muted); }
#map-list .grow { flex: 1; min-width: 0; }
.list-note { color: var(--muted); font-size: 13px; padding: 8px 10px; }
.drawer-foot { border-top: 1px solid var(--line); padding: 8px 12px calc(8px + var(--safe-bottom)); display: flex; justify-content: space-between; }

#inspector {
  position: fixed; z-index: 25; right: 0; top: calc(var(--topbar-h) + var(--safe-top)); bottom: 0; width: 340px;
  background: var(--panel); border-left: 1px solid var(--line); box-shadow: var(--shadow);
  display: flex; flex-direction: column; transform: translateX(105%); transition: transform 0.18s ease;
}
#inspector.open { transform: none; }
.insp-body { overflow-y: auto; padding: 12px 14px 24px; display: grid; gap: 14px; align-content: start; flex: 1; }
.insp-body label, .lbl { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; display: block; margin-bottom: 5px; }
.insp-body textarea { width: 100%; min-height: 64px; resize: vertical; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); outline: none; }
.insp-body textarea#insp-note { min-height: 120px; }
.insp-body textarea:focus, .insp-body input:focus { border-color: var(--accent); }
.row-inline { display: flex; gap: 6px; align-items: center; }
.row-inline .field { padding: 8px 10px; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch {
  width: 28px; height: 28px; border-radius: 50%; border: 1.5px solid var(--line); cursor: pointer; padding: 0;
  display: grid; place-items: center; font-size: 13px; color: var(--muted); background: var(--panel);
}
.swatch.on { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--accent); }
.swatch input { opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 4px; }
.icon-grid button {
  height: 36px; border: 1px solid transparent; border-radius: 8px; background: var(--panel-2); cursor: pointer; font-size: 18px;
}
.icon-grid button:hover { border-color: var(--line); }
.icon-grid button.on { border-color: var(--accent); background: var(--accent-soft); }
.meta { font-size: 12.5px; color: var(--muted); display: grid; gap: 4px; }
.meta code { font-size: 12px; background: var(--panel-2); padding: 1px 5px; border-radius: 5px; user-select: all; }

@media (max-width: 640px) {
  #inspector {
    top: auto; left: 0; width: auto; height: min(62vh, 560px); border-left: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0; transform: translateY(105%); padding-bottom: var(--safe-bottom);
  }
  #inspector.open { transform: none; }
}

/* ---------------------------------------------------------------- menus, dialogs */
.popover {
  position: fixed; z-index: 50; min-width: 220px; max-width: calc(100vw - 16px); max-height: calc(100vh - 16px); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 5px;
}
.popover button {
  width: 100%; display: flex; align-items: center; gap: 10px; border: 0; background: transparent; text-align: left;
  padding: 9px 10px; border-radius: 8px; cursor: pointer; font-size: 14.5px;
}
.popover button:hover, .popover button:focus-visible { background: var(--panel-2); outline: none; }
.popover button.danger { color: var(--danger); }
.popover button .k { margin-left: auto; color: var(--muted); font-size: 12px; }
.popover hr { border: 0; border-top: 1px solid var(--line); margin: 5px 4px; }
@media (pointer: coarse) { .popover button { padding: 12px 12px; font-size: 15.5px; } }

dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--panel); color: var(--text);
  width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 48px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 15, 25, 0.4); }
.dlg-body { padding: 4px 18px 18px; overflow-y: auto; max-height: calc(100vh - 140px); }
.dlg-body p { margin: 8px 0; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; padding: 0 18px 18px; flex-wrap: wrap; }
.kbd-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.kbd-table td { padding: 5px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.kbd-table td:first-child { white-space: nowrap; color: var(--muted); width: 40%; }
kbd { font: 12px ui-monospace, monospace; background: var(--panel-2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; }
pre.code { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; overflow-x: auto; font-size: 12.5px; white-space: pre; }
.versions { list-style: none; margin: 0; padding: 0; }
.versions li { display: flex; align-items: center; gap: 10px; padding: 9px 2px; border-bottom: 1px solid var(--line); }
.versions .grow { flex: 1; min-width: 0; }
.versions .m { font-size: 12.5px; color: var(--muted); }
h3.dlg-title { margin: 0; padding: 16px 18px 6px; font-size: 17px; }

#toast {
  position: fixed; z-index: 60; left: 50%; bottom: calc(var(--toolbar-h) + 30px + var(--safe-bottom));
  transform: translateX(-50%) translateY(10px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-size: 14px;
  transition: opacity 0.18s, transform 0.18s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.drop-overlay {
  position: fixed; inset: 10px; z-index: 70; border: 3px dashed var(--accent); border-radius: 18px;
  background: var(--accent-soft); display: grid; place-items: center; font-size: 20px; font-weight: 600; color: var(--accent); pointer-events: none;
}
