:root {
  --bg: #f4f6f9; --surface: #fff; --surface2: #f0f3f7; --text: #1b2733; --muted: #66768a; --border: #dde3ea;
  --accent: #1d6fb8; --accent-ink: #fff; --danger: #c93c3c; --ok: #1f9d5b; --warn: #c98a16;
  --shadow: 0 8px 30px rgba(20, 40, 70, .14);
  --st-frei: #27ae60; --st-vergeben: #3a6fd8; --st-reserviert: #f0a429; --st-unbekannt: #98a2ae; --st-nicht_vermietbar: #d64545;
  /* Plan */
  --water1: #bfe0f2; --water2: #9dcbe8; --wave: rgba(255,255,255,.35); --green: #d9e9b8; --road: #e4e5ea; --path: #ddd3b8;
  --land: #f4e5c8; --rail: #d8c0c0; --border-line: #e0457b; --walk: #8b5e3c; --line: #8a7a62; --house: #646a73;
  --plantext: #3b3326; --waterText: rgba(255,255,255,.8); --stegText: #1d3b57; --poi: #2b7bb9; --poi2: #7d8c5c;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f151c; --surface: #17202a; --surface2: #1e2935; --text: #e5ebf1; --muted: #93a3b5; --border: #2b3847;
  --accent: #4b9be0; --shadow: 0 8px 30px rgba(0,0,0,.5);
  --water1: #183a55; --water2: #122d45; --wave: rgba(255,255,255,.06); --green: #2c3b26; --road: #2a2e35; --path: #3a3529;
  --land: #3b3324; --rail: #3d2c2f; --walk: #a8764f; --line: #a8987e; --house: #555b66;
  --plantext: #e6dcc8; --waterText: rgba(255,255,255,.35); --stegText: #cfe3f5; --border-line: #c2386a;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f151c; --surface: #17202a; --surface2: #1e2935; --text: #e5ebf1; --muted: #93a3b5; --border: #2b3847;
    --accent: #4b9be0; --shadow: 0 8px 30px rgba(0,0,0,.5);
    --water1: #183a55; --water2: #122d45; --wave: rgba(255,255,255,.06); --green: #2c3b26; --road: #2a2e35; --path: #3a3529;
    --land: #3b3324; --rail: #3d2c2f; --walk: #a8764f; --line: #a8987e; --house: #555b66;
    --plantext: #e6dcc8; --waterText: rgba(255,255,255,.35); --stegText: #cfe3f5; --border-line: #c2386a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; }
button, input, select, textarea { font: inherit; color: inherit; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 0; }

/* Login */
.login-page { align-items: center; justify-content: center; background: linear-gradient(160deg, #1d4e7a, #0f2c47); }
.login-card { background: var(--surface); padding: 32px; border-radius: 16px; width: min(380px, calc(100vw - 32px)); display: grid; gap: 14px; box-shadow: var(--shadow); }
.login-card label { display: grid; gap: 4px; font-weight: 600; font-size: 13px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.err { color: var(--danger); margin: 0; }

/* Header */
.top { display: flex; align-items: center; gap: 20px; padding: 0 16px; height: 56px; background: var(--surface); border-bottom: 1px solid var(--border); flex: none; }
.brand { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.brand img { width: 32px; height: 32px; }
.brand strong { display: block; font-size: 15px; line-height: 1.1; }
.brand span { display: block; font-size: 12px; color: var(--muted); }
.brand.big img { width: 44px; height: 44px; }
.brand.big strong { font-size: 18px; }
nav { display: flex; gap: 2px; overflow-x: auto; }
nav button { border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 600; white-space: nowrap; }
nav button:hover { background: var(--surface2); color: var(--text); }
nav button.active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.top-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.user-btn { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); background: var(--surface); border-radius: 20px; padding: 3px 12px 3px 3px; cursor: pointer; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.icon-btn { border: 0; background: none; width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 16px; color: var(--muted); }
.icon-btn svg { width: 18px; height: 18px; vertical-align: middle; }
.icon-btn:hover { background: var(--surface2); color: var(--text); }

body:not([data-role="admin"]) .admin-only { display: none !important; }
body[data-role="viewer"] .editor-only { display: none !important; }

main { flex: 1; min-height: 0; position: relative; }
.view { display: none; height: 100%; overflow: auto; padding: 16px; }
.view.active { display: block; }
#view-plan { padding: 0; display: none; flex-direction: column; }
#view-plan.active { display: flex; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; text-decoration: none; color: var(--text); white-space: nowrap; }
.btn:hover { background: var(--surface2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.ms { justify-content: center; }

/* Plan */
.plan-toolbar { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.search input, .filters input, .filters select { padding: 7px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); min-width: 240px; }
.filters select { min-width: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 16px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: 13px; user-select: none; }
.chip .dot { width: 10px; height: 10px; border-radius: 3px; }
.chip b { font-variant-numeric: tabular-nums; }
.chip.off { opacity: .4; }
.plan-tools { display: flex; gap: 10px; align-items: center; margin-left: auto; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; color: var(--muted); }
.zoom { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.zoom button { border: 0; background: var(--surface); width: 32px; height: 30px; cursor: pointer; font-size: 16px; }
.zoom button + button { border-left: 1px solid var(--border); }
.plan-wrap { flex: 1; min-height: 0; position: relative; overflow: hidden; background: var(--water2); }
#plan { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; user-select: none; }
#plan.dragging { cursor: grabbing; }
#plan text { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }

.berth { cursor: pointer; }
.berth polygon { stroke: rgba(255,255,255,.95); stroke-width: 1.6; transition: fill .2s, opacity .2s; }
.berth text { font-size: 17px; font-weight: 700; fill: #fff; pointer-events: none; text-anchor: middle; paint-order: stroke; stroke: rgba(0,0,0,.25); stroke-width: 2px; }
.berth.s-frei polygon { fill: var(--st-frei); }
.berth.s-vergeben polygon { fill: var(--st-vergeben); }
.berth.s-reserviert polygon { fill: var(--st-reserviert); }
.berth.s-unbekannt polygon { fill: var(--st-unbekannt); }
.berth.s-nicht_vermietbar polygon { fill: var(--st-nicht_vermietbar); }
.berth:hover polygon { filter: brightness(1.15); stroke: #fff; stroke-width: 3; }
.berth.sel polygon { stroke: #111; stroke-width: 4; }
:root[data-theme="dark"] .berth.sel polygon { stroke: #fff; }
.berth.dim { opacity: .18; }
.berth.match polygon { stroke: #ffde3b; stroke-width: 4; }
.berth.misfit polygon { stroke: #ff2d2d; stroke-width: 4; stroke-dasharray: 6 3; }
.berth .warn { fill: #ff2d2d; stroke: none; font-size: 13px; }
#scan[visibility="visible"] ~ #berths .berth polygon { opacity: .78; }
.geo-mode .berth { opacity: .55; }
.geo-mode .berth.sel { opacity: 1; }
.geo-mode .berth.sel polygon { fill-opacity: .35; }
.handle { fill: #fff; stroke: #111; stroke-width: 2; cursor: move; }
.handle:hover { fill: #ffde3b; }
.edge { stroke: transparent; stroke-width: 10; cursor: copy; fill: none; }

.tooltip { position: absolute; pointer-events: none; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; box-shadow: var(--shadow); font-size: 13px; max-width: 280px; z-index: 5; }
.tooltip .t1 { font-weight: 700; }
.tooltip .t2 { color: var(--muted); }
.geo-hint { position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; box-shadow: var(--shadow); font-size: 13px; z-index: 4; display: flex; gap: 8px; align-items: center; max-width: calc(100% - 32px); flex-wrap: wrap; }

/* Drawer */
.drawer { position: fixed; top: 56px; right: 0; bottom: 0; width: min(460px, 100vw); background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow); transform: translateX(105%); transition: transform .22s ease; display: flex; flex-direction: column; z-index: 10; }
.drawer.open { transform: none; }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 16px 16px 8px; }
.drawer-head h2 { font-size: 22px; }
.eyebrow { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.tabs { display: flex; gap: 4px; padding: 0 16px; border-bottom: 1px solid var(--border); }
.tabs button { border: 0; background: none; padding: 8px 10px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--accent); border-color: var(--accent); }
.drawer-body { flex: 1; overflow: auto; padding: 16px; display: grid; gap: 14px; align-content: start; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.card.pad { padding: 14px 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); gap: 8px; }
.card-head h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.card-body { padding: 12px 14px; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 4px 12px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.dims { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.dim { background: var(--surface2); border-radius: 10px; padding: 8px 10px; }
.dim .l { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.dim .v { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.dim .v small { font-size: 12px; font-weight: 500; color: var(--muted); }
.dim.bad { background: color-mix(in srgb, var(--danger) 16%, transparent); }
.dim.bad .v { color: var(--danger); }
.dim.good .v { color: var(--ok); }
.fit { margin-top: 8px; font-size: 13px; padding: 6px 10px; border-radius: 8px; }
.fit.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.fit.bad { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.fit.na { background: var(--surface2); color: var(--muted); }
.status-pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 12px; font-size: 12.5px; font-weight: 700; color: #fff; }
.status-select { display: flex; flex-wrap: wrap; gap: 6px; }
.status-select button { border: 2px solid transparent; border-radius: 16px; padding: 4px 12px; cursor: pointer; font-weight: 600; font-size: 13px; color: #fff; opacity: .45; }
.status-select button.on { opacity: 1; border-color: var(--text); }
.status-select button:disabled { cursor: default; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.empty { color: var(--muted); font-style: italic; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { background: var(--surface2); border-radius: 6px; padding: 2px 8px; font-size: 12px; }

.timeline { display: grid; gap: 0; }
.timeline.wide { max-width: 900px; }
.tl-item { display: grid; grid-template-columns: 14px 1fr; gap: 10px; padding-bottom: 14px; position: relative; }
.tl-item::before { content: ""; position: absolute; left: 6px; top: 14px; bottom: 0; width: 2px; background: var(--border); }
.tl-item:last-child::before { display: none; }
.tl-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); margin-top: 3px; border: 3px solid var(--surface); box-shadow: 0 0 0 1px var(--border); }
.tl-dot.a-angelegt, .tl-dot.a-vergeben, .tl-dot.a-import { background: var(--ok); }
.tl-dot.a-geloescht { background: var(--danger); }
.tl-dot.a-beendet { background: var(--warn); }
.tl-meta { font-size: 12px; color: var(--muted); }
.tl-sum { font-weight: 600; }
.tl-ch { font-size: 12.5px; margin-top: 4px; display: grid; gap: 2px; }
.tl-ch del { color: var(--danger); text-decoration: line-through; opacity: .8; }
.tl-ch ins { color: var(--ok); text-decoration: none; }
.tl-sum a, .linkish { color: var(--accent); cursor: pointer; text-decoration: none; }

/* Tabellen */
.view-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px; margin: 4px 0 12px; }
.view-head + .card, .table-wrap + .view-head { margin-top: 20px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.table-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.grid th, .grid td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.grid th { position: sticky; top: 0; background: var(--surface2); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); cursor: pointer; z-index: 1; }
.grid th.sorted::after { content: " ▲"; }
.grid th.sorted.desc::after { content: " ▼"; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: var(--surface2); }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid .bad { color: var(--danger); font-weight: 700; }

/* Modal & Formulare */
.modal { position: fixed; inset: 0; background: rgba(10, 20, 30, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 14px; width: min(640px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 16px 18px; overflow: auto; }
.form { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px 12px; }
.form .f { display: grid; gap: 3px; grid-column: span 6; }
.form .f.w2 { grid-column: span 2; } .form .f.w3 { grid-column: span 3; } .form .f.w4 { grid-column: span 4; }
.form label { font-size: 12px; font-weight: 600; color: var(--muted); }
.form input, .form select, .form textarea { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); width: 100%; }
.form textarea { min-height: 70px; resize: vertical; }
.form input:focus, .form select:focus, .form textarea:focus, .search input:focus, .filters input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); border-color: var(--accent); }
.form .sec { grid-column: span 6; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); margin-top: 6px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.form .cb { display: flex; gap: 8px; align-items: center; }
.form .cb input { width: auto; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; grid-column: span 6; }
.seg button { flex: 1; border: 0; background: var(--surface); padding: 7px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--accent); color: #fff; }
.hint { font-size: 12px; color: var(--muted); grid-column: span 6; }

.menu { position: fixed; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); padding: 6px; z-index: 60; min-width: 200px; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--surface2); }
.menu .mi { padding: 6px 10px; color: var(--muted); font-size: 12px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #1b2733; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow); z-index: 70; max-width: calc(100vw - 32px); }
.toast.err { background: var(--danger); }

@media (max-width: 760px) {
  .top { gap: 8px; height: auto; flex-wrap: wrap; padding: 8px 12px; }
  .brand span { display: none; }
  nav { order: 3; width: 100%; }
  .drawer { top: 0; z-index: 30; }
  .search input, .filters input { min-width: 0; width: 100%; }
  .search { flex: 1 1 100%; }
  .plan-tools { margin-left: 0; }
  .kv { grid-template-columns: 110px 1fr; }
  .form .f.w2, .form .f.w3, .form .f.w4 { grid-column: span 6; }
  .uname { display: none; }
  #edit-geo { display: none; }
}
