:root {
  --bg-app: #09090b;
  --bg-sidebar: #111113;
  --bg-canvas: #09090b;
  --bg-card: #18181b;
  --bg-card-hover: #27272a;
  --border-color: #27272a;
  --text-primary: #f4f4f5;
  --text-muted: #71717a;
  --accent: #8b5cf6;
  --accent-hover: #7c3aed;
  --danger: #ef4444;
  --font-sans: 'Outfit', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.55);
  --shadow-glow: 0 0 18px rgba(139,92,246,0.3);
  --sidebar-full: 260px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-sans);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea, select {
  font-family: var(--font-sans);
  color: inherit;
}

#app {
  display: flex;
  width: 100vw;
  height: 100vh;
}

/* ---------------- SIDEBAR ---------------- */
#sidebar {
  width: var(--sidebar-full);
  min-width: var(--sidebar-full);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease;
}
#sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-full)); }

.sidebar-header {
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border-color);
}
.brand { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.brand-logo {
  width: 10cm;
  height: 60px;
  object-fit: contain;
  border-radius: 6px;
  filter: drop-shadow(0 0 6px rgba(139,92,246,0.35));
}

.btn-primary {
  width: 100%;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 9px 12px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.btn-primary:hover { box-shadow: var(--shadow-glow); transform: translateY(-1px); }

.btn-ghost {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 12.5px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.btn-ghost:hover { background: var(--bg-card-hover); border-color: var(--accent); }

.btn-secondary {
  background: var(--bg-card-hover);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  cursor: pointer;
}
.btn-secondary:hover { border-color: var(--accent); }

.btn-danger {
  background: rgba(239,68,68,0.12);
  border: 1px solid var(--danger);
  color: #fca5a5;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  cursor: pointer;
}
.btn-danger:hover { background: rgba(239,68,68,0.22); }

.sidebar-actions {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.import-export-row { display: flex; gap: 6px; }
.import-export-row button { flex: 1; }

.folder-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 10px;
}
.folder-list::-webkit-scrollbar { width: 6px; }
.folder-list::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }

.folder-group { margin-bottom: 6px; }
.folder-head {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
}
.folder-head:hover { background: var(--bg-card); color: var(--text-primary); }
.folder-head .chevron { transition: transform 0.15s ease; font-size: 10px; }
.folder-head.open .chevron { transform: rotate(90deg); }
.folder-head .folder-name-label { flex: 1; }
.folder-head .folder-del {
  opacity: 0; font-size: 12px; color: var(--text-muted); padding: 2px 4px;
}
.folder-head:hover .folder-del { opacity: 1; }
.folder-head .folder-del:hover { color: var(--danger); }

.folder-maps {
  display: flex;
  flex-direction: column;
  padding-left: 14px;
  gap: 2px;
}
.map-item {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px;
  border-radius: 6px;
  font-size: 13px;
  cursor: grab;
  color: var(--text-muted);
  border: 1px solid transparent;
}
.map-item:hover { background: var(--bg-card); color: var(--text-primary); }
.map-item.active {
  background: rgba(139,92,246,0.14);
  color: var(--text-primary);
  border-color: rgba(139,92,246,0.4);
}
.map-item .map-del { margin-left: auto; opacity: 0; font-size: 12px; }
.map-item:hover .map-del { opacity: 1; }
.map-item .map-del:hover { color: var(--danger); }
.map-item.drag-over { outline: 1px dashed var(--accent); }
.folder-head.drag-over { outline: 1px dashed var(--accent); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
}

.author-link {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}
.author-link:hover {
  text-decoration: underline;
  color: var(--accent-hover);
}

/* ---------------- WORKSPACE ---------------- */
#workspace {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#topbar {
  height: 52px;
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-sidebar);
  z-index: 20;
}
.icon-btn {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  width: 32px; height: 32px;
  border-radius: 7px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px;
}
.icon-btn:hover { background: var(--bg-card); border-color: var(--accent); }

.map-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-muted);
}
.topbar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-box {
  display: flex; align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 7px;
  padding: 0 10px;
  height: 32px;
}
.search-box input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 12.5px;
  width: 170px;
}
.search-count { font-size: 11px; color: var(--text-muted); margin-left: 6px; white-space: nowrap; }

#canvasViewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  background-color: var(--bg-canvas);
  background-image: radial-gradient(rgba(139,92,246,0.18) 1px, transparent 1px);
  background-size: 26px 26px;
  cursor: grab;
}
#canvasViewport.panning { cursor: grabbing; }

#canvasWorld {
  position: absolute;
  top: 0; left: 0;
  width: 0; height: 0;
  transform-origin: 0 0;
}

#connectionsLayer {
  position: absolute;
  overflow: visible;
  pointer-events: none;
}
#nodesLayer {
  position: absolute;
  top: 0; left: 0;
}

.marquee-box {
  position: absolute;
  border: 1px solid var(--accent);
  background: rgba(139,92,246,0.12);
  pointer-events: none;
  z-index: 30;
}

/* ---------------- NODES ---------------- */
.mm-node {
  position: absolute;
  transform: translate(-50%, -50%);
  padding: 10px 22px 10px 18px;
  border-radius: 12px;
  background: var(--bg-card);
  border: 1.5px solid var(--border-color);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-primary);
  max-width: 320px;
  min-width: 90px;
  width: max-content;
  text-align: center;
  white-space: normal;
  word-break: normal;
  overflow-wrap: break-word;
  user-select: none;
  resize: none;
  overflow: visible;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}
.mm-node:hover { background: var(--bg-card-hover); }

.node-resize-handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: var(--bg-sidebar);
  border: 1.5px solid var(--accent);
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity 0.15s ease;
  z-index: 12;
}
.mm-node:hover .node-resize-handle,
.mm-node.selected .node-resize-handle {
  opacity: 1;
}

.item-rename-icon, .item-edit-icon {
  opacity: 0;
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  transition: opacity 0.15s;
}
.folder-head:hover .item-rename-icon, .map-item:hover .item-rename-icon { opacity: 1; }
.item-rename-icon:hover { color: var(--accent); background: var(--bg-card-hover); }

.mm-node.level-0 {
  border-radius: 999px;
  padding: 16px 30px;
  font-size: 17px;
  font-weight: 700;
  background: linear-gradient(135deg, #7c3aed, #a855f7);
  border: none;
  color: #fff;
  box-shadow: var(--shadow-lg), var(--shadow-glow);
}
.mm-node.level-1 { font-size: 15px; font-weight: 600; border-color: #3f3f46; }
.mm-node.level-2 { font-size: 13.5px; font-weight: 500; border-color: var(--border-color); }
.mm-node.level-3 { font-size: 12.5px; font-weight: 400; color: #d4d4d8; border-color: #232326; opacity: 0.95; }
.mm-node.level-4 { font-size: 11.5px; font-weight: 400; color: #a1a1aa; border-color: #1e1e21; opacity: 0.9; padding: 7px 12px; }

.mm-node.selected {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 2px rgba(139,92,246,0.5), var(--shadow-glow);
}

.mm-node.linked {
  border-color: #f59e0b !important;
  box-shadow: 0 0 0 1px rgba(245,158,11,0.4);
}
.mm-node.linked::after {
  content: "🔗";
  font-size: 10px;
  margin-left: 6px;
  opacity: 0.8;
}

.mm-node.match-pulse {
  animation: pulseMatch 1.1s ease-in-out infinite;
}
@keyframes pulseMatch {
  0%, 100% { box-shadow: 0 0 0 2px rgba(34,197,94,0.8); }
  50% { box-shadow: 0 0 0 6px rgba(34,197,94,0.15); }
}

.mm-node[data-color="red"] { background: rgba(239,68,68,0.16); border-color: #ef4444; }
.mm-node[data-color="orange"] { background: rgba(249,115,22,0.16); border-color: #f97316; }
.mm-node[data-color="green"] { background: rgba(34,197,94,0.16); border-color: #22c55e; }
.mm-node[data-color="slate"] { background: rgba(100,116,139,0.2); border-color: #64748b; }

.mm-node .node-edit-input {
  background: transparent;
  border: none;
  outline: none;
  color: inherit;
  font: inherit;
  width: 100%;
  min-width: 80px;
  resize: none;
  overflow: hidden;
  text-align: inherit;
}

.node-toggle {
  position: absolute;
  right: -10px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--bg-sidebar);
  border: 1.5px solid var(--accent);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  z-index: 10;
  user-select: none;
}
.node-toggle:hover { background: var(--accent); color: #fff; }

/* ---------------- TOOLBAR ---------------- */
.toolbar {
  position: absolute;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: 8px 12px;
  box-shadow: var(--shadow-lg);
  z-index: 25;
}
.tb-btn {
  background: transparent;
  border: none;
  color: var(--text-primary);
  padding: 7px 11px;
  border-radius: 999px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.tb-btn:hover { background: var(--bg-card-hover); }
.tb-danger { color: #fca5a5; }
.tb-danger:hover { background: rgba(239,68,68,0.15); }
.tb-select {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}
.tb-divider { width: 1px; height: 22px; background: var(--border-color); }
.color-dots { display: flex; gap: 5px; padding: 0 4px; }
.color-dot {
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.15);
  cursor: pointer;
  padding: 0;
}
.color-dot:hover { transform: scale(1.15); }

/* ---------------- NOTES DRAWER ---------------- */
.notes-drawer {
  position: fixed;
  top: 0; right: 0;
  width: 340px;
  height: 100vh;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border-color);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.22s ease;
  z-index: 40;
}
.notes-drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--border-color);
}
.notes-title-input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
}
.notes-title-input:focus { border-color: var(--accent); }

.drawer-tabs { display: flex; gap: 4px; padding: 10px 14px 0; }
.drawer-tab {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 7px 12px;
  border-radius: 7px 7px 0 0;
  font-size: 12.5px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.drawer-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.notes-textarea {
  flex: 1;
  margin: 10px 14px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: none;
  outline: none;
}
.notes-textarea:focus { border-color: var(--accent); }

.notes-preview {
  flex: 1;
  margin: 10px 14px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px 14px;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
}
.notes-preview h1, .notes-preview h2, .notes-preview h3 { color: var(--text-primary); }
.notes-preview code { background: var(--bg-card-hover); padding: 1px 5px; border-radius: 4px; }
.notes-preview a { color: var(--accent); }

/* Themed scrollbar for the notes drawer (Firefox) */
.notes-textarea,
.notes-preview {
  scrollbar-width: thin;
  scrollbar-color: var(--accent) var(--bg-card);
}

/* Themed scrollbar for the notes drawer (Chrome/Edge/Safari) */
.notes-textarea::-webkit-scrollbar,
.notes-preview::-webkit-scrollbar {
  width: 8px;
}
.notes-textarea::-webkit-scrollbar-track,
.notes-preview::-webkit-scrollbar-track {
  background: var(--bg-card);
  border-radius: 8px;
}
.notes-textarea::-webkit-scrollbar-thumb,
.notes-preview::-webkit-scrollbar-thumb {
  background: var(--accent);
  border-radius: 8px;
  border: 2px solid var(--bg-card);
  background-clip: padding-box;
}
.notes-textarea::-webkit-scrollbar-thumb:hover,
.notes-preview::-webkit-scrollbar-thumb:hover {
  background: var(--accent-hover);
  background-clip: padding-box;
}

/* ---------------- MODALS ---------------- */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] {
  display: none !important;
}
.modal-card {
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 22px;
  width: 340px;
  box-shadow: var(--shadow-lg);
}
.modal-card h3 { margin: 0 0 6px; font-size: 16px; }
.modal-sub { margin: 0 0 16px; font-size: 12.5px; color: var(--text-muted); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions.column { flex-direction: column; }
.modal-actions.column button { width: 100%; }

.link-map-list {
  max-height: 220px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  margin-bottom: 14px;
}
.link-map-option {
  padding: 8px 10px;
  border-radius: 7px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  font-size: 13px;
  cursor: pointer;
}
.link-map-option:hover { border-color: var(--accent); background: var(--bg-card-hover); }
.link-map-option.current { border-color: #f59e0b; color: #f59e0b; }

.folder-name-input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 7px;
  padding: 9px 11px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  margin-bottom: 8px;
}
.folder-name-input:focus { border-color: var(--accent); }

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.form-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}
.form-select {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
}
.form-select:focus { border-color: var(--accent); }

.tb-btn i, .btn-primary i, .btn-ghost i, .icon-btn i {
  width: 15px;
  height: 15px;
  vertical-align: -3px;
  margin-right: 5px;
}
.icon-btn i {
  margin-right: 0;
  width: 16px;
  height: 16px;
  vertical-align: middle;
}

.tb-btn svg,
.btn-primary svg,
.btn-ghost svg,
.icon-btn svg {
  width: 14px;
  height: 14px;
  vertical-align: -2px;
  margin-right: 4px;
  flex-shrink: 0;
}
.icon-btn svg {
  margin-right: 0;
  width: 15px;
  height: 15px;
}

.tb-btn svg,
.btn-primary svg,
.btn-ghost svg,
.icon-btn svg {
  stroke-width: 2;
}

.item-rename-icon svg,
.folder-del svg,
.map-del svg {
  width: 13px;
  height: 13px;
  vertical-align: -2px;
}