:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --line: #e2e6ee;
  --text: #1f2a44;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --danger: #dc2626;
  --root-bg: #1f2a44;
  --shadow: 0 1px 3px rgba(31,42,68,.12), 0 4px 14px rgba(31,42,68,.06);
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); color: var(--text); background: var(--bg); font-size: 14px; overflow: hidden; -webkit-font-smoothing: antialiased; }
button, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
.hidden { display: none !important; }

#app { display: flex; height: 100%; }

/* ---------- サイドバー ---------- */
#sidebar { width: 240px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; transition: margin-left .2s ease; }
#sidebar.collapsed { margin-left: -240px; }
.sb-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px; }
.sb-title { font-weight: 700; font-size: 13px; color: var(--muted); letter-spacing: .04em; }
#mapList { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
#mapList li { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer; color: var(--text); }
#mapList li:hover { background: #f1f4f9; }
#mapList li.active { background: var(--accent-soft); color: #1e3a8a; font-weight: 600; }
#mapList li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#mapList li .date { font-size: 11px; color: var(--muted); font-weight: 400; }
#mapList li .del { opacity: 0; border: none; background: none; cursor: pointer; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
#mapList li:hover .del { opacity: 1; }
#mapList li .del:hover { background: #fee2e2; color: var(--danger); }
.sb-foot { padding: 10px 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.status::before { content: "●"; margin-right: 6px; color: #9ca3af; }
.status.saved::before { color: #10b981; }
.status.dirty::before, .status.saving::before { color: #f59e0b; }
.status.error::before, .status.offline::before { color: var(--danger); }

/* ---------- メイン ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
#title { border: 1px solid transparent; border-radius: 8px; padding: 6px 10px; font-size: 16px; font-weight: 700; min-width: 120px; width: 320px; max-width: 40vw; background: transparent; }
#title:hover { border-color: var(--line); }
#title:focus { outline: none; border-color: var(--accent); background: #fff; }
.spacer { flex: 1; }
.tools { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.zoom { font-size: 12px; color: var(--muted); min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; }

.btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 10px; cursor: pointer; line-height: 1.2; white-space: nowrap; }
.btn:hover { background: #f1f4f9; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #1d4ed8; }
.btn.icon { width: 32px; padding: 6px 0; text-align: center; }

/* ---------- キャンバス ---------- */
#canvas { flex: 1; position: relative; overflow: hidden; cursor: grab; touch-action: none;
  background-color: var(--bg);
  background-image: radial-gradient(circle, #d5dae4 1px, transparent 1px);
  background-size: 24px 24px; }
#canvas.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#links { position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: visible; pointer-events: none; }
#links path { fill: none; stroke-width: 2.5; stroke-linecap: square; stroke-linejoin: miter; }
#nodes { position: absolute; left: 0; top: 0; }
#empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }

.node { position: absolute; left: 0; top: 0; width: max-content; max-width: 280px; padding: 7px 12px; border-radius: 10px; background: #fff; border: 2px solid var(--c, #94a3b8); box-shadow: var(--shadow); cursor: default; user-select: none; -webkit-user-select: none; transition: transform .18s ease; }
.node.nomove { transition: none; }
.node.root { background: var(--root-bg); color: #fff; border-color: var(--root-bg); font-size: 17px; font-weight: 700; padding: 12px 20px; border-radius: 14px; max-width: 340px; }
.node.lv1 { font-weight: 600; background: color-mix(in srgb, var(--c) 12%, #fff); }
.node.lv2plus { border-width: 1.5px; border-color: color-mix(in srgb, var(--c) 55%, #fff); }
.node.selected { box-shadow: 0 0 0 3px var(--accent-soft), 0 0 0 5px var(--accent); }
.node.editing { cursor: text; }
.node.drop-target { box-shadow: 0 0 0 3px #d1fae5, 0 0 0 5px #10b981; }
.node.dragging { opacity: .35; }
.node .txt { white-space: pre-wrap; word-break: break-word; line-height: 1.45; min-width: 1em; outline: none; }
.node .txt:empty::before { content: "（空）"; color: #9ca3af; }
.node.root .txt:empty::before { color: #9ca3af; }
.node .txt[contenteditable="true"] { cursor: text; user-select: text; -webkit-user-select: text; }
.node .txt[contenteditable="true"]:empty::before { content: ""; }
.toggle { position: absolute; top: 50%; width: 18px; height: 18px; margin-top: -9px; border-radius: 50%; background: #fff; border: 1.5px solid var(--c, #94a3b8); color: var(--c, #64748b); font-size: 10px; line-height: 15px; text-align: center; cursor: pointer; font-weight: 700; }
.toggle.r { right: -12px; } .toggle.l { left: -12px; }
.toggle:hover { background: var(--c); color: #fff; }
.node.root .toggle { display: none; }

#ghost { position: fixed; z-index: 50; pointer-events: none; width: max-content; padding: 7px 12px; border-radius: 10px; background: #fff; border: 2px solid var(--accent); box-shadow: var(--shadow); opacity: .9; max-width: 280px; white-space: pre-wrap; }

/* ---------- モーダル ---------- */
.modal { position: fixed; inset: 0; background: rgba(31,42,68,.4); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-box { background: #fff; border-radius: 14px; padding: 22px 24px; width: 440px; max-width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,.25); max-height: 90vh; overflow: auto; }
.modal-box.wide { width: 640px; }
.modal-box h2 { margin: 0 0 10px; font-size: 17px; }
.modal-box p { margin: 0 0 12px; line-height: 1.6; }
.modal-box input[type=password], .modal-box textarea { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.modal-box textarea { height: 300px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; line-height: 1.5; resize: vertical; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; align-items: center; }
.modal .err { color: var(--danger); font-size: 13px; }
.hint { color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.tab { border: 1px solid var(--line); background: #f8fafc; border-radius: 8px 8px 0 0; padding: 6px 12px; cursor: pointer; }
.tab.active { background: #fff; border-bottom-color: #fff; font-weight: 600; color: var(--accent); }
table.keys { width: 100%; border-collapse: collapse; font-size: 13px; }
table.keys td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.keys td:first-child { white-space: nowrap; width: 44%; }
kbd { display: inline-block; border: 1px solid #cbd5e1; border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px; font-size: 11px; background: #f8fafc; font-family: inherit; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1f2a44; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; z-index: 200; box-shadow: var(--shadow); }

@media (max-width: 720px) {
  #sidebar { position: absolute; z-index: 20; height: 100%; box-shadow: 0 0 30px rgba(0,0,0,.15); }
  #title { width: 160px; }
  .tools .btn:not(.icon) { padding: 6px 8px; font-size: 13px; }
}

/* ---------- 共有・閲覧専用 ---------- */
.badge { font-size: 11px; font-weight: 700; color: #9a3412; background: #ffedd5; border: 1px solid #fed7aa; border-radius: 999px; padding: 3px 9px; white-space: nowrap; }
.share-row { display: flex; gap: 6px; }
.share-row input { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fee2e2; }
body.readonly #sidebar, body.readonly #btnSidebar, body.readonly #btnAddChild, body.readonly #btnAddSibling, body.readonly #btnDelete,
body.readonly #btnLink, body.readonly #btnUndo, body.readonly #btnRedo, body.readonly #btnShare, body.readonly #btnImport, body.readonly #btnLayout, body.readonly .tools .sep:nth-of-type(-n+2) { display: none; }
body.readonly #title { pointer-events: none; }
body.readonly .node { cursor: grab; }

/* ---------- リンク・ファイル読み込み ---------- */
.node.has-link { display: flex; align-items: center; gap: 6px; }
.lnk { flex: none; text-decoration: none; font-size: 12px; line-height: 1; opacity: .7; cursor: pointer; }
.lnk:hover { opacity: 1; }
body.dropping #canvas { outline: 3px dashed var(--accent); outline-offset: -10px; }
