/* ============ Grundlagen ============ */
:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); font-size: 15px; line-height: 1.45; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.right { text-align: right; }

/* ============ Buttons & Formulare ============ */
.btn, button {
  font: inherit;
  cursor: pointer;
}
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
input[type=text], input[type=password] {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  width: 100%;
}
input:focus, button:focus-visible, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; align-items: center; gap: 8px; }
.wrap-gap { flex-wrap: wrap; gap: 12px; }
.grow { flex: 1; min-width: 200px; }
.inline { display: inline; }

/* ============ Übersicht ============ */
.page .wrap { max-width: 980px; margin: 0 auto; padding: 24px 16px 48px; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 24px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; }
.card h2 { margin-top: 0; font-size: 19px; }
.card.narrow { max-width: 420px; margin: 40px auto; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: #eff6ff; border: 1px solid #bfdbfe; }
.flash.error { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.table-card { padding: 0; overflow: visible; }
table.boards { width: 100%; border-collapse: collapse; }
table.boards th, table.boards td { padding: 12px 16px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.boards th { font-size: 13px; color: var(--muted); font-weight: 600; }
table.boards tr:last-child td { border-bottom: 0; }
table.boards .title { font-weight: 600; color: var(--ink); text-decoration: none; }
table.boards .title:hover { color: var(--primary); }
table.boards td.right { text-align: right; }
.actions { white-space: nowrap; }
.actions > * { margin-left: 4px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; }
.badge.open { background: #dcfce7; color: #166534; }
.badge.locked { background: #fee2e2; color: #991b1b; }
details.menu { display: inline-block; position: relative; }
details.menu summary { list-style: none; }
details.menu summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 10;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow);
  min-width: 210px; padding: 4px; text-align: left;
}
.menu-body button {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  border: 0; background: none; border-radius: 6px; font-size: 14px;
}
.menu-body button:hover { background: #f3f4f6; }
.menu-body button.danger { color: var(--danger); }

/* ============ Board ============ */
body.board { overflow: hidden; user-select: none; -webkit-user-select: none; }
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 52px; z-index: 20;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 0 12px; background: var(--card); border-bottom: 1px solid var(--line);
}
#topbar .left, #topbar .right { display: flex; align-items: center; gap: 10px; min-width: 0; }
#title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.status { font-size: 12px; color: var(--muted); white-space: nowrap; }
.status::before { content: '●'; margin-right: 4px; }
.status.ok::before { color: var(--ok); }
.status.warn::before { color: var(--warn); }
.status.error { color: var(--danger); }
.status.error::before { color: var(--danger); }
#users { display: flex; align-items: center; gap: 4px; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
}
.avatar.me { box-shadow: 0 0 0 2px var(--ink); }
#lockbar {
  position: fixed; top: 52px; left: 0; right: 0; z-index: 19; text-align: center;
  background: #fef3c7; color: #92400e; padding: 6px; font-size: 14px;
}

#stage {
  position: fixed; top: 52px; left: 0; right: 0; bottom: 0;
  background-color: #fafafa;
  background-image: radial-gradient(circle, #d4d4d8 1px, transparent 1.2px);
  touch-action: none;
}
#canvas { width: 100%; height: 100%; display: block; cursor: default; }
#stage[data-tool=hand] #canvas, #stage.space #canvas { cursor: grab; }
#stage.panning #canvas { cursor: grabbing; }
#stage[data-tool=sticky] #canvas, #stage[data-tool=rect] #canvas, #stage[data-tool=ellipse] #canvas,
#stage[data-tool=arrow] #canvas, #stage[data-tool=pen] #canvas { cursor: crosshair; }
#stage[data-tool=text] #canvas { cursor: text; }
#world [data-id] { cursor: move; }
#stage[data-tool]:not([data-tool=select]) #world [data-id] { cursor: inherit; }

.sel-box { fill: none; stroke: var(--primary); stroke-width: 1.5; stroke-dasharray: 5 4; pointer-events: none; }
.handle { fill: #fff; stroke: var(--primary); stroke-width: 1.5; cursor: nwse-resize; }
.connect { fill: #fff; stroke: var(--primary); stroke-width: 2; cursor: crosshair; }
.connect:hover { fill: var(--primary); }
.target { fill: rgba(22, 163, 74, .08); stroke: var(--ok); stroke-width: 2; stroke-dasharray: 6 4; pointer-events: none; }
.band { fill: rgba(37, 99, 235, .08); stroke: var(--primary); stroke-width: 1; pointer-events: none; }

#toolbar {
  position: fixed; left: 12px; top: 50%; transform: translateY(-50%); z-index: 15;
  display: flex; flex-direction: column; gap: 2px; padding: 6px;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
}
#toolbar button, #zoombar button {
  width: 40px; height: 40px; border: 0; border-radius: 8px; background: none;
  font-size: 18px; color: var(--ink);
}
#toolbar button:hover:not(:disabled), #zoombar button:hover { background: #f3f4f6; }
#toolbar button.active { background: #dbeafe; color: var(--primary); }
#toolbar button:disabled { opacity: .3; cursor: not-allowed; }
#toolbar hr { width: 70%; border: 0; border-top: 1px solid var(--line); margin: 4px auto; }

#zoombar {
  position: fixed; right: 12px; bottom: 12px; z-index: 15;
  display: flex; gap: 2px; padding: 4px; background: var(--card); border-radius: 10px; box-shadow: var(--shadow);
}
#zoombar #btn-zoom-reset { width: 56px; font-size: 13px; }

#props {
  position: fixed; right: 12px; top: 64px; z-index: 15;
  width: 272px; max-height: calc(100vh - 140px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
  padding: 10px 12px; background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
}
.prop-group { display: flex; flex-direction: column; gap: 6px; }
.prop-group + .prop-group, .prop-group + .actions-row { border-top: 1px solid var(--line); padding-top: 8px; }
.prop-group h4 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.prop-row { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.prop-label { font-size: 12px; color: var(--muted); width: 100%; }
.colors { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.swatch.custom {
  position: relative; display: inline-block; cursor: pointer; overflow: hidden;
  background: conic-gradient(#e53935, #fb8c00, #fdd835, #43a047, #1e88e5, #8e24aa, #e53935);
}
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; border: 0; padding: 0; }
#props input.hex { width: 84px; font: 12px ui-monospace, Consolas, monospace; padding: 3px 5px; border: 1px solid var(--line); border-radius: 6px; }
.hex.bad { border-color: var(--danger); background: #fef2f2; }
#props input.num { width: 58px; font-size: 13px; padding: 3px 4px; border: 1px solid var(--line); border-radius: 6px; }
.font-select { width: 100%; font-size: 14px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.opt.bold { font-weight: 700; }
.opt.italic { font-style: italic; font-family: Georgia, serif; }
.actions-row { justify-content: space-between; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); padding: 0;
}
.swatch.active { box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--primary); }
.swatch.none { background: linear-gradient(135deg, #fff 45%, #e53935 45%, #e53935 55%, #fff 55%); }
.opt {
  border: 1px solid var(--line); background: var(--card); border-radius: 6px;
  padding: 3px 8px; font-size: 13px; min-width: 30px;
}
.opt:hover { background: #f3f4f6; }
.opt.active { background: #dbeafe; border-color: #93c5fd; }

#editor {
  position: absolute; z-index: 12; resize: none; border: 2px solid var(--primary); border-radius: 4px;
  outline: none; overflow: hidden; line-height: 1.25; font-family: inherit; margin: 0;
}
#editor.box { text-align: center; }
#editor.text { white-space: pre; }

.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(17, 24, 39, .35);
  display: flex; align-items: center; justify-content: center;
}
.modal .card { margin: 0; width: min(420px, calc(100vw - 32px)); }

#toast {
  position: fixed; top: 64px; left: 50%; transform: translateX(-50%); z-index: 40;
  background: #111827; color: #fff; padding: 8px 14px; border-radius: 8px; font-size: 14px;
  max-width: calc(100vw - 32px);
}

@media (max-width: 640px) {
  #title { max-width: 30vw; }
  #props { width: calc(100vw - 80px); top: auto; bottom: 64px; max-height: 45vh; }
  table.boards th:nth-child(2), table.boards td:nth-child(2),
  table.boards th:nth-child(3), table.boards td:nth-child(3) { display: none; }
}

/* ============ Version 4: Workshop-Funktionen ============ */
#topbar .tools, #topbar .exports { display: flex; gap: 4px; }
#topbar .btn.active { background: #dbeafe; border-color: #93c5fd; color: var(--primary); }
body.readonly .edit-only, body.readonly #props { display: none !important; }
.timer {
  font: 600 15px ui-monospace, Consolas, monospace; padding: 3px 10px; border-radius: 99px;
  background: #eef2ff; color: #3730a3;
}
.timer.soon { background: #fef3c7; color: #92400e; }
.timer.done { background: #fee2e2; color: #991b1b; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .4; } }

.infobar {
  position: fixed; left: 50%; transform: translateX(-50%); top: 62px; z-index: 18;
  display: flex; align-items: center; gap: 10px; max-width: calc(100vw - 360px);
  padding: 6px 8px 6px 14px; border-radius: 10px; font-size: 14px;
  background: #ecfdf5; border: 1px solid #a7f3d0; color: #065f46; box-shadow: var(--shadow);
}
.infobar.follow { top: 108px; background: #eff6ff; border-color: #bfdbfe; color: #1e3a8a; }
#lockbar ~ #votebar { top: 92px; }

#popover {
  position: fixed; z-index: 40; min-width: 220px; max-width: 340px; padding: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 8px;
}
.pop-title { font-weight: 600; font-size: 14px; }
.pop-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; font-size: 13px; }
#popover input.num { width: 60px; font-size: 13px; padding: 3px 4px; border: 1px solid var(--line); border-radius: 6px; }
.menu-item {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  border: 0; background: none; padding: 7px 8px; border-radius: 8px; font-size: 14px;
}
.menu-item:hover { background: #f3f4f6; }
.menu-item small { color: var(--muted); font-size: 12px; }

#results {
  position: fixed; left: 76px; top: 64px; z-index: 16; width: 300px; max-height: calc(100vh - 140px); overflow-y: auto;
  padding: 12px; background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 8px;
}
#results ol { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 4px; }
#results li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
#results .count {
  min-width: 28px; height: 24px; border-radius: 99px; background: var(--primary); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px;
}
.linklike { border: 0; background: none; padding: 0; text-align: left; color: var(--ink); cursor: pointer; }
.linklike:hover { color: var(--primary); text-decoration: underline; }

.vote-badge { fill: var(--primary); stroke: #fff; stroke-width: 2; pointer-events: none; }
.vote-badge.mine { fill: #f59e0b; }
.vote-count { fill: #fff; font: 700 13px system-ui, sans-serif; text-anchor: middle; pointer-events: none; }
#stage.voting #canvas, #stage.voting #world [data-id] { cursor: cell; }
.guide { stroke: #ec4899; stroke-width: 1; stroke-dasharray: 4 3; pointer-events: none; }
.lock-badge { font-size: 14px; pointer-events: none; }
#zoombar #btn-snap.active { background: #dbeafe; color: var(--primary); }

/* Bereiche (Frames): nur Rand und Titel sind anklickbar */
#world .frame-body { pointer-events: none; }
#world .frame-hit { pointer-events: stroke; cursor: move; }
#world .frame-title { cursor: move; user-select: none; }

.versions-card { width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 80px); overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.versions-card h2 { margin: 0; }
.row.space { justify-content: space-between; }
.version-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.version-row small { color: var(--muted); }

@media (max-width: 1100px) {
  #topbar .tools .btn { font-size: 0; padding: 5px 8px; }
  #topbar .tools .btn::first-letter { font-size: 15px; }
}
