:root {
  --bg: #f4f1ea;
  --toolbar-bg: #ffffff;
  --toolbar-fg: #333;
  --root-fill: #3a3a3a;
  --selected: #1d6fe0;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  -webkit-user-select: none;
  user-select: none;
}

#toolbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid #e0ddd5;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  z-index: 10;
}

#toolbar .brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--toolbar-fg);
  letter-spacing: .3px;
}

#toolbar .group { display: flex; align-items: center; gap: 6px; }

#toolbar button {
  font: inherit;
  font-size: 13px;
  padding: 6px 11px;
  border: 1px solid #d4d0c7;
  border-radius: 7px;
  background: #fff;
  color: #333;
  cursor: pointer;
}
#toolbar button:hover { background: #f0ede6; }
#toolbar button:active { transform: translateY(1px); }

#toolbar .hint {
  margin-left: auto;
  font-size: 12px;
  color: #999;
}

button.chrome {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}
.map-title {
  font-weight: 600;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.save-state { font-size: 12px; color: #9a9588; min-width: 56px; }
.save-state.saving { color: #c98a16; }
.save-state.saved { color: #2f9e44; }
.save-state.error { color: #c0392b; }

.user-area { margin-left: auto; position: relative; }
.avatar {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent, #b8860b); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
}
.menu {
  position: absolute; right: 0; top: 42px;
  background: #fff; border: 1px solid #e0ddd5; border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.14);
  display: flex; flex-direction: column; min-width: 160px; overflow: hidden; z-index: 30;
}
.menu button {
  border: none; background: #fff; text-align: left; padding: 10px 14px; border-radius: 0; cursor: pointer;
}
.menu button:hover { background: #f3f0e9; }

/* Drawer */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: 320px; max-width: 86vw;
  background: #fff; box-shadow: 4px 0 30px rgba(0,0,0,.18); z-index: 40;
  display: flex; flex-direction: column; padding: 16px;
}
.drawer header { display: flex; align-items: center; justify-content: space-between; }
.drawer h2 { font-size: 17px; margin: 0 0 4px; }
.drawer ul { list-style: none; margin: 14px 0 0; padding: 0; overflow-y: auto; }
.drawer li { display: flex; align-items: stretch; gap: 6px; margin-bottom: 6px; }
.drawer li.current .map-open { border-color: var(--accent, #b8860b); background: #fff7e6; }
.drawer li.empty { color: #9a9588; font-size: 13px; padding: 8px; }
.map-open {
  flex: 1; text-align: left; border: 1px solid #e6e2da; border-radius: 9px; background: #fcfbf8;
  padding: 9px 11px; cursor: pointer; display: flex; flex-direction: column; gap: 2px;
}
.map-open .t { font-weight: 600; font-size: 14px; }
.map-open .d { font-size: 11px; color: #9a9588; }
.icon { border: none; background: transparent; cursor: pointer; font-size: 15px; padding: 6px 8px; border-radius: 8px; }
.icon:hover { background: #f0ede6; }

/* Drawer: 'Shared with me' section + rights badge */
.drawer li.section-head {
  display: block; margin: 14px 0 6px; padding: 4px 2px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: #9a9588;
}
.drawer .map-open .t .badge { margin-left: 6px; }
.badge {
  display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 6px;
  border-radius: 8px; text-transform: uppercase; letter-spacing: .03em; vertical-align: middle;
}
.badge.view { background: #efeadd; color: #6f6952; }
.badge.edit { background: #d6ecd9; color: #2f6b35; }

/* Share modal */
.share-link-row { display: flex; gap: 6px; align-items: stretch; margin-top: 6px; }
.share-link-row input { flex: 1; }
.share-link-actions { display: flex; gap: 8px; margin-top: 8px; }
.share-link-actions .ghost { flex: 1; font-size: 12.5px; padding: 6px 10px; }
.share-revoked-note { margin-top: 8px; font-size: 12.5px; color: #9a8a52; background: #fff7df; border-radius: 8px; padding: 8px 10px; }
.claims-list { list-style: none; padding: 0; margin: 4px 0 0; }
.claims-list li { display: flex; align-items: center; gap: 8px; padding: 6px 4px; border-bottom: 1px solid #f0ede6; }
.claims-list li:last-child { border-bottom: none; }
.claims-list li.empty { color: #9a9588; font-size: 13px; padding: 8px 4px; }
.claim-name { flex: 1; font-size: 13.5px; }

/* Read-only banner shown above the canvas for shared-with-me viewers */
.readonly-banner {
  position: fixed; top: 48px; left: 0; right: 0; z-index: 5;
  background: #fff7df; border-bottom: 1px solid #e9d896;
  color: #5a4a18; font-size: 13px; text-align: center; padding: 6px 12px;
}
body.readonly #toolbar #btn-add-child,
body.readonly #toolbar #btn-add-sibling,
body.readonly #toolbar #btn-delete,
body.readonly #toolbar #btn-image,
body.readonly #toolbar #swatches,
body.readonly #toolbar #btn-undo,
body.readonly #save-state { display: none; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 45; display: flex; align-items: center; justify-content: center; }
.dialog {
  background: #fff; border-radius: 16px; padding: 22px 24px; width: 380px; max-width: calc(100vw - 32px);
  box-shadow: 0 24px 70px rgba(0,0,0,.25); display: flex; flex-direction: column; gap: 12px;
  max-height: 86vh; overflow-y: auto;
}
.dialog.wide { width: 560px; }
.dialog header { display: flex; align-items: center; justify-content: space-between; }
.dialog h2 { margin: 0; font-size: 18px; }
.dialog h3 { margin: 4px 0 0; font-size: 14px; }
.dialog label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 600; color: #8a857a; }
.dialog input, .dialog select {
  font: inherit; padding: 9px 11px; border: 1.5px solid #e0ddd5; border-radius: 9px; background: #fcfbf8;
}
.dialog input:focus, .dialog select:focus { outline: none; border-color: var(--accent, #b8860b); }
.dialog .show-pw { flex-direction: row; align-items: center; gap: 7px; font-weight: 500; color: #8a857a; cursor: pointer; }
.dialog .show-pw input[type="checkbox"] { width: auto; padding: 0; border: none; }
.dialog hr { border: none; border-top: 1px solid #eee7d8; margin: 4px 0; width: 100%; }
.dialog footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dialog .row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.dialog .row input { flex: 1; min-width: 90px; }
button.primary { background: var(--accent, #b8860b); color: #fff; border: none; font-weight: 700; padding: 10px 14px; border-radius: 9px; cursor: pointer; }
button.ghost { background: #fff; border: 1px solid #d4d0c7; padding: 9px 13px; border-radius: 9px; cursor: pointer; }
button.ghost.sm { padding: 5px 9px; font-size: 12px; }
button.ghost.danger { color: #c0392b; border-color: #e3b4ad; }
.msg { font-size: 13px; color: #c0392b; min-height: 16px; margin: 0; }
.msg.ok { color: #2f9e44; }

#users-table { width: 100%; border-collapse: collapse; font-size: 13px; }
#users-table th, #users-table td { text-align: left; padding: 7px 6px; border-bottom: 1px solid #f0ebdd; }
#users-table td.actions { display: flex; gap: 6px; justify-content: flex-end; }
#users-table .dim { color: #9a9588; font-size: 11px; }

#overlay { position: fixed; inset: 0; background: rgba(30,25,10,.28); z-index: 35; }

#app-version {
  position: fixed; right: 8px; bottom: 6px; z-index: 5;
  font-size: 11px; color: #b3ada0; pointer-events: none; user-select: none;
  letter-spacing: .2px;
}
.menu-version {
  padding: 8px 14px 4px; font-size: 11px; color: #9a9588;
  border-top: 1px solid #eee7d8;
}
body[data-theme="dark"] #app-version { color: #6b6f78; }

/* Themes */
body[data-theme="light"] { --bg: #fafafa; }
body[data-theme="light"] #canvas { background: #fafafa; }
body[data-theme="dark"] { --bg: #23252b; }
body[data-theme="dark"] #canvas { background: #23252b; }
body[data-theme="dark"] #toolbar { background: #2c2f37; border-bottom-color: #3a3d46; }
body[data-theme="dark"] #toolbar .brand,
body[data-theme="dark"] .map-title,
body[data-theme="dark"] #toolbar button { color: #e7e7ea; }
body[data-theme="dark"] #toolbar button { background: #353944; border-color: #444855; }
body[data-theme="dark"] .branch-text { stroke: rgba(20,22,26,.85); fill: #f0f0f2 !important; }

.swatches { display: flex; gap: 5px; }
.swatch {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #ccc;
  cursor: pointer;
}
.swatch.active { box-shadow: 0 0 0 2px #333; }
.swatch.custom {
  background: conic-gradient(#e8453c, #e08a16, #f4d300, #3aa655, #16b3a7, #1d9bf0, #8a4fc7, #e0529c, #e8453c);
}
.swatch-wrap {
  position: relative; display: inline-block;
  width: 18px; height: 18px;
  vertical-align: middle;
}
.swatch-wrap .swatch.custom {
  position: absolute; inset: 0;
  pointer-events: none;
}
#custom-color {
  position: absolute; inset: -2px;
  width: 22px; height: 22px;
  opacity: 0;
  border: 0; padding: 0; margin: 0;
  cursor: pointer;
  -webkit-appearance: none; appearance: none;
}

#canvas {
  position: fixed;
  top: 48px; left: 0;
  width: 100%;
  height: calc(100% - 48px);
  display: block;
  cursor: grab;
  touch-action: none;
}
#canvas.panning { cursor: grabbing; }

.node { cursor: pointer; }

/* Central topic: free organic shape */
.root ellipse {
  fill: #fff7e6;
  stroke: #caa14a;
  stroke-width: 2.5;
}
.root.selected ellipse { stroke: var(--selected); stroke-width: 3.5; }
.root-text {
  font-size: 20px;
  font-weight: 700;
  fill: #b8860b;
  dominant-baseline: middle;
  text-anchor: middle;
  cursor: pointer;          /* clickable so the central topic can be renamed */
}

/* Tapering branch (filled path) */
.branch { stroke: none; }
.branch.selected { stroke: rgba(0,0,0,.35); stroke-width: 1.5; }

/* Word written above its branch */
.branch-text {
  font-weight: 600;
  dominant-baseline: alphabetic;
  text-anchor: middle;
  pointer-events: visiblePainted;
  cursor: pointer;
  paint-order: stroke;
  stroke: rgba(255,255,255,.85);
  stroke-width: 3px;
}
.label.selected .branch-text { text-decoration: underline; }

.handle { stroke: #fff; stroke-width: 2; cursor: grab; opacity: .9; }
.handle:hover { opacity: 1; }
.handle.dragging { stroke: var(--selected); stroke-width: 3.5; opacity: 1; }

.node-image { cursor: pointer; }
.node-image.selected { outline: 2px solid var(--selected); outline-offset: 2px; cursor: move; }

/* Resize / delete affordances on top of the SELECTED node's image */
.image-handle { cursor: pointer; }
.image-resize {
  fill: #fff; stroke: var(--selected); stroke-width: 2;
  cursor: nwse-resize;
}
.image-delete {
  fill: var(--selected); stroke: #fff; stroke-width: 2;
}
.image-delete-x {
  fill: #fff; font-size: 12px; font-weight: 700;
  text-anchor: middle; dominant-baseline: central;
  pointer-events: none; user-select: none;
}

/* Inline add-branch buttons on the selected node */
.add-btn { cursor: pointer; }
.add-btn circle { filter: drop-shadow(0 1px 2px rgba(0,0,0,.25)); }
.add-btn:hover circle { filter: drop-shadow(0 2px 4px rgba(0,0,0,.35)); }
.add-plus { text-anchor: middle; dominant-baseline: middle; font-weight: 700; pointer-events: none; user-select: none; }

#editor {
  position: fixed;
  z-index: 20;
  font-family: inherit;
  font-size: 15px;
  text-align: center;
  padding: 6px 12px;
  border: 2px solid var(--selected);
  border-radius: 12px;
  outline: none;
  background: #fff;
  box-shadow: 0 4px 18px rgba(0,0,0,.18);
}
