:root {
  --bg: #f6f7f9;
  --canvas: #fbfbfc;
  --grid: #e9ebef;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --border: #dde1e6;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --danger: #e03131;
  --ok: #2f9e44;
  --warn: #e67700;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 14px rgba(0, 0, 0, .07);
  --node-sub-bg: #ffffff;
  --radius: 8px;
  --topbar: 48px;
  --side: 264px;
  --panel: 300px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191d;
    --canvas: #1c1f24;
    --grid: #262a31;
    --surface: #23272e;
    --surface-2: #2b3038;
    --border: #363c45;
    --text: #e6e8eb;
    --muted: #9aa1ab;
    --accent: #6c8cff;
    --accent-text: #0e1117;
    --node-sub-bg: #262b33;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  display: grid;
  grid-template-columns: 0 1fr 0;
  grid-template-rows: var(--topbar) 1fr;
  grid-template-areas: "top top top" "maps view panel";
}
body.maps-open { grid-template-columns: var(--side) 1fr 0; }
body.panel-open { grid-template-columns: 0 1fr var(--panel); }
body.maps-open.panel-open { grid-template-columns: var(--side) 1fr var(--panel); }

button {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .4; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.icon-btn { min-width: 30px; padding: 4px 6px; }
.muted { color: var(--muted); font-size: 12px; }
kbd { font: 11px ui-monospace, monospace; padding: 0 4px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface-2); }

/* ---------- Kopfleiste ---------- */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
#topbar > * { flex-shrink: 0; }
#map-title {
  flex: 0 1 auto;
  min-width: 120px;
  font-size: 15px;
  font-weight: 600;
  margin: 0 2px;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: text;
}
#save-state { min-width: 80px; }
.group { display: flex; gap: 3px; padding-left: 8px; border-left: 1px solid var(--border); }
.group button { border-color: transparent; background: transparent; }
.group button:hover:not(:disabled) { background: var(--surface-2); }
.spacer { flex: 1; }
.search {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--canvas);
}
.search input { border: 0; background: transparent; color: inherit; font: inherit; width: 140px; outline: none; }
/* Schmale Fenster: Beschriftungen weglassen, Symbole bleiben */
@media (max-width: 1500px) {
  #btn-child, #btn-sibling, #btn-link { font-size: 0; }
  #btn-child::before { content: "＋⤷"; font-size: 14px; }
  #btn-sibling::before { content: "＋↓"; font-size: 14px; }
  #btn-link::before { content: "🔗"; font-size: 14px; }
  #save-state { display: none; }
}
#zoom-label { min-width: 58px; font-variant-numeric: tabular-nums; }

/* ---------- Seitenleisten ---------- */
#maplist, #panel {
  background: var(--surface);
  overflow: hidden auto;
  min-width: 0;
}
#maplist { grid-area: maps; border-right: 1px solid var(--border); display: flex; flex-direction: column; }
#panel { grid-area: panel; border-left: 1px solid var(--border); padding-bottom: 20px; }
body:not(.maps-open) #maplist, body:not(.panel-open) #panel { visibility: hidden; }
.side-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 14px 8px; }
.side-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0; }
.side-foot { margin-top: auto; padding: 10px 14px; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; border-top: 1px solid var(--border); }

#maplist-items { list-style: none; margin: 0; padding: 0 8px; }
#maplist-items li { display: flex; align-items: stretch; border-radius: 6px; }
#maplist-items li.active { background: color-mix(in srgb, var(--accent) 14%, transparent); }
#maplist-items li:hover:not(.active) { background: var(--surface-2); }
#maplist-items button { border: 0; background: transparent; }
#maplist-items .open { flex: 1; text-align: left; display: flex; flex-direction: column; min-width: 0; padding: 6px 8px; }
#maplist-items strong { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
#maplist-items small { color: var(--muted); font-size: 11px; }
#maplist-items .more { opacity: .5; }
#maplist-items li:hover .more { opacity: 1; }

.field { display: flex; flex-direction: column; gap: 5px; padding: 8px 14px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); }
.field em { font-style: normal; font-weight: 400; }
.field input, .field textarea {
  font: inherit;
  color: inherit;
  background: var(--canvas);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  resize: vertical;
  width: 100%;
}
.field input#p-link { font-family: ui-monospace, monospace; font-size: 12.5px; }
.row { display: flex; flex-wrap: wrap; gap: 4px; }
.row button { font-size: 12.5px; padding: 3px 8px; }
.hint { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
#p-link-status.ok { color: var(--ok); }
#p-link-status.bad { color: var(--danger); }
#panel.empty .field { opacity: .4; pointer-events: none; }

.palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px; }
.palette button { padding: 3px 0; font-size: 15px; border-color: transparent; background: var(--surface-2); }
.palette button.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.swatches { grid-template-columns: repeat(10, 1fr); }
.swatches button { height: 24px; background: var(--swatch); font-size: 11px; color: var(--muted); }
.swatches button[data-color=""] { background: var(--surface-2); }

/* ---------- Zeichenfläche ---------- */
#viewport {
  grid-area: view;
  position: relative;
  overflow: hidden;
  background-color: var(--canvas);
  background-image: radial-gradient(var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
  cursor: grab;
  outline: none;
  touch-action: none;
  user-select: none;
}
#viewport.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#world.smooth { transition: transform .25s ease; }
#edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.edge { fill: none; stroke-width: 2; stroke-linecap: round; opacity: .85; }
.edge.main { stroke-width: 3; }
#nodes { position: absolute; left: 0; top: 0; }

.node {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  gap: 5px;
  width: max-content;   /* sonst schrumpft der Knoten auf die Breite von #nodes (0) */
  max-width: 320px;
  padding: 5px 10px;
  border-radius: 6px;
  background: var(--node-sub-bg);
  border: 1px solid var(--border);
  border-bottom: 2px solid var(--branch);
  cursor: default;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  transition: transform .18s ease, box-shadow .1s;
  will-change: transform;
}
.node.main {
  padding: 7px 14px;
  border: 2px solid var(--branch);
  border-radius: 10px;
  background: color-mix(in srgb, var(--branch) 10%, var(--surface));
  font-size: 15px;
  font-weight: 600;
}
.node.root {
  padding: 12px 22px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 18px;
  font-weight: 700;
  max-width: 380px;
  box-shadow: var(--shadow);
}
.node .icon:empty, .node .badges:empty { display: none; }
.node .icon { font-size: 1.05em; }
.node .text { min-width: 1ch; outline: none; }
.node .badges { display: flex; gap: 2px; font-size: 12px; }
.badge { cursor: help; }
.badge.link { cursor: pointer; padding: 0 2px; border-radius: 4px; }
.badge.link:hover { background: color-mix(in srgb, var(--branch) 25%, transparent); }
.badge.broken { filter: saturate(2); }
.node.has-link .text { text-decoration: underline dotted color-mix(in srgb, currentColor 40%, transparent); text-underline-offset: 3px; }

.node.selected { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 55%, transparent), var(--shadow); z-index: 2; }
.node.root.selected { box-shadow: 0 0 0 3px var(--canvas), 0 0 0 6px color-mix(in srgb, var(--accent) 60%, transparent), var(--shadow); }
.node.editing { cursor: text; user-select: text; z-index: 3; }
.node.editing .text { min-width: 4ch; }
.node.match { background: color-mix(in srgb, #fcc419 35%, var(--surface)); color: var(--text); }
.node.current-match { box-shadow: 0 0 0 3px #fab005, var(--shadow); }
.node.dragging { opacity: .75; transition: none; z-index: 5; pointer-events: none; box-shadow: var(--shadow); }
.node.drop-child { box-shadow: 0 0 0 3px var(--ok); }
.node.drop-before { box-shadow: 0 -4px 0 0 var(--ok); }
.node.drop-after { box-shadow: 0 4px 0 0 var(--ok); }
#viewport.dragging-node { cursor: grabbing; }

.fold {
  position: absolute;
  left: 0;
  top: 0;
  min-width: 16px;
  height: 16px;
  padding: 0 3px;
  border-radius: 8px;
  border: 1.5px solid var(--branch);
  background: var(--surface);
  color: var(--branch);
  font-size: 10px;
  font-weight: 700;
  line-height: 13px;
  text-align: center;
  z-index: 1;
  opacity: 0;
  transition: transform .18s ease, opacity .1s;
}
.fold.collapsed { opacity: 1; background: var(--branch); color: #fff; }
#viewport:hover .fold { opacity: .9; }
.fold:hover { opacity: 1 !important; }

/* ---------- Overlays ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  max-width: min(640px, calc(100vw - 32px));
  padding: 9px 16px;
  border-radius: 8px;
  background: #2b2f36;
  color: #fff;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }
#toast.warn { background: var(--warn); }
#toast.ok { background: var(--ok); }

.popup-menu {
  position: fixed;
  z-index: 40;
  min-width: 220px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.popup-menu button { border: 0; background: transparent; display: flex; justify-content: space-between; gap: 18px; text-align: left; padding: 5px 10px; }
.popup-menu button:hover:not(:disabled), .popup-menu button:focus { background: var(--surface-2); outline: none; }
.popup-menu kbd { border: 0; background: none; color: var(--muted); }
.popup-menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 2px; width: auto; }

#help {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  display: grid;
  place-items: center;
  z-index: 45;
}
#help[hidden] { display: none; }
.help-card {
  position: relative;
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 20px 24px;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.help-card h2 { margin: 0 0 12px; font-size: 18px; }
.help-card .close { position: absolute; right: 12px; top: 12px; }
.help-card .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 28px; }
.help-card dl { display: grid; grid-template-columns: max-content 1fr; gap: 5px 12px; margin: 0; }
.help-card dt { font-weight: 600; font-size: 13px; }
.help-card dd { margin: 0; color: var(--muted); }
@media (max-width: 700px) { .help-card .cols { grid-template-columns: 1fr; } }

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  width: min(560px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog h2 { margin: 0 0 6px; font-size: 17px; }
dialog p { margin: 0 0 12px; color: var(--muted); }
dialog p:empty { display: none; }
dialog input { width: 100%; font: inherit; padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; background: var(--canvas); color: inherit; }
dialog menu { display: flex; flex-direction: row-reverse; gap: 8px; padding: 0; margin: 16px 0 0; }

#settings fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
#settings legend { font-weight: 600; padding: 0 4px; }
#settings label { display: flex; gap: 8px; align-items: baseline; cursor: pointer; }
#settings input[type=radio] { width: auto; }
#settings code { font-size: 12px; }
.side-foot { flex-direction: row; flex-wrap: wrap; }
#settings form { display: flex; flex-direction: column; gap: 12px; }
#settings .row button { font-size: 13px; }
.folder-state { font-size: 12px; padding: 2px 8px; border-color: transparent; background: transparent; color: var(--muted); max-width: 200px; overflow: hidden; text-overflow: ellipsis; }
.folder-state.warn { color: #fff; background: var(--warn); border-color: var(--warn); }
.node.fresh, .fold.fresh { transition: none !important; }

/* ---------- Handy (Web-App) ---------- */
.phone-only { display: none; }

@media (pointer: coarse) {
  button { min-height: 34px; }
  .fold { min-width: 22px; height: 22px; line-height: 19px; border-radius: 11px; opacity: .9; }
  .popup-menu button { padding: 10px 12px; }
}

@media (max-width: 760px) {
  :root { --topbar: 52px; }
  body, body.maps-open, body.panel-open, body.maps-open.panel-open { grid-template-columns: 0 1fr 0; }
  .phone-only { display: inline-block; }

  /* Kopfleiste: nur das Nötigste, der Rest steckt im ⋯-Menü */
  #topbar { gap: 2px; padding: 0 4px; padding-left: max(4px, env(safe-area-inset-left)); }
  #topbar .group { padding-left: 2px; border-left: 0; gap: 0; }
  #map-title { min-width: 0; flex: 1 1 auto; font-size: 14px; }
  #save-state, #folder-state, #btn-redo, #btn-open-link, #btn-check-links, #btn-collapse-all, #btn-expand-all,
  #btn-zoom-out, #btn-zoom-in, #zoom-label, #btn-fit, #btn-import, #btn-export, #btn-help, .spacer { display: none !important; }
  #topbar > * { flex-shrink: 1; }
  #btn-child, #btn-sibling, #btn-link { font-size: 0; padding: 4px 5px; }
  #btn-child::before { content: "＋⤷"; font-size: 15px; }
  #btn-sibling::before { content: "＋↓"; font-size: 15px; }
  #btn-link::before { content: "🔗"; font-size: 15px; }

  /* Suche als Leiste unter der Kopfleiste (über ⋯ → Suchen) */
  .search { display: none; }
  body.search-open .search {
    display: flex; position: fixed; z-index: 32; top: calc(var(--topbar) + 6px); left: 8px; right: 8px;
    background: var(--surface); box-shadow: var(--shadow);
  }
  body.search-open .search input { width: 100%; }

  /* Mindmap-Liste als Schublade von links */
  #maplist {
    position: fixed; z-index: 35; top: var(--topbar); bottom: 0; left: 0; width: min(86vw, 320px);
    box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .2s ease; visibility: visible !important;
  }
  body.maps-open #maplist { transform: none; }

  /* Eigenschaften als Bereich von unten */
  #panel {
    position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; max-height: 58vh;
    border-left: 0; border-top: 1px solid var(--border); border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, .18); padding-bottom: env(safe-area-inset-bottom);
    transform: translateY(105%); transition: transform .2s ease; visibility: visible !important;
  }
  body.panel-open #panel { transform: none; }
  #panel .side-head { position: sticky; top: 0; background: var(--surface); z-index: 1; }
  #panel .side-head::before {
    content: ""; position: absolute; left: 50%; top: 5px; width: 40px; height: 4px; margin-left: -20px;
    border-radius: 2px; background: var(--border);
  }
  .palette { grid-template-columns: repeat(8, 1fr); }
  .swatches { grid-template-columns: repeat(10, 1fr); }
  .field input, .field textarea { font-size: 16px; }   /* verhindert Auto-Zoom beim Tippen */

  .node { max-width: 240px; }
  .node.root { max-width: 260px; }
  #toast { bottom: calc(16px + env(safe-area-inset-bottom)); }
  .help-card .cols { grid-template-columns: 1fr; }
}
