:root {
  --bg: #0f1115;
  --panel: #161a21;
  --line: #262c37;
  --text: #e7ebf2;
  --muted: #98a2b3;
  --accent: #4f8cff;
  --accent-dim: #2b5fc4;
  --danger: #e5484d;
  --warn: #f5a524;
  --ok: #30a46c;
}

* { box-sizing: border-box; }

/* Muss vor allen display-Regeln stehen: Klassen wie .modal{display:grid}
   wuerden das hidden-Attribut sonst aushebeln (Author-Style schlaegt
   Browser-Default) und Overlays blieben dauerhaft sichtbar. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, select, input, textarea { font: inherit; color: inherit; }

/* ---------- Kopfleiste ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 56px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand { font-weight: 700; letter-spacing: -.02em; }
.brand span { color: var(--accent); }

.docname {
  flex: 1;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar-actions { display: flex; gap: 8px; }

button, .primary {
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 8px 14px;
  transition: background .15s, border-color .15s;
}

.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  text-decoration: none;
  display: inline-block;
}
.primary:hover { background: var(--accent-dim); }

.ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.ghost:hover { border-color: var(--muted); color: var(--text); }
a.ghost { text-decoration: none; display: inline-block; }
.ghost.active { border-color: var(--accent); color: var(--accent); }

.danger { background: transparent; border-color: rgba(229,72,77,.4); color: var(--danger); }
.danger:hover { background: rgba(229,72,77,.12); }

.small { padding: 6px 10px; font-size: 13px; }

.icon { padding: 7px 12px; font-size: 16px; line-height: 1; }
button:disabled { opacity: .35; cursor: default; }
button:disabled:hover { border-color: var(--line); color: var(--muted); }

.btnrow { display: flex; gap: 8px; }
.toggle {
  flex: 1;
  background: #0e1116;
  border-color: var(--line);
  color: var(--muted);
  padding: 7px 0;
}
.toggle:hover { border-color: var(--muted); color: var(--text); }
.toggle.on { background: rgba(79,140,255,.16); border-color: var(--accent); color: var(--accent); }
.as-bold { font-weight: 700; }
.as-italic { font-style: italic; }

.sel-info {
  margin-bottom: 14px;
  padding: 8px 11px;
  background: rgba(79,140,255,.1);
  border: 1px solid rgba(79,140,255,.3);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--accent);
}

kbd {
  padding: 1px 5px;
  background: #0e1116;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: 11px/1.6 ui-monospace, monospace;
}

/* Auswahlrahmen beim Ziehen mit der Maus */
.marquee {
  position: absolute;
  border: 1px solid var(--accent);
  background: rgba(79,140,255,.14);
  pointer-events: none;
  z-index: 5;
}

/* ---------- Anmeldung ---------- */

.auth-wrap {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 32px;
}
body > .auth-wrap { min-height: 100vh; }
#view-login.auth-wrap { min-height: calc(100% - 56px); }

.auth-box {
  width: min(400px, 100%);
  padding: 32px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.auth-box .brand { margin-bottom: 20px; font-size: 18px; }
.auth-box h1 { margin: 0 0 6px; font-size: 20px; }
.auth-box form { margin-top: 18px; }
.auth-box .primary { width: 100%; text-align: center; margin-top: 4px; }

.auth-hint { margin: 0 0 4px; font-size: 13px; color: var(--muted); }
.auth-error {
  margin: 14px 0 0;
  padding: 10px 12px;
  background: rgba(229,72,77,.12);
  border: 1px solid rgba(229,72,77,.35);
  border-radius: 8px;
  font-size: 13px;
  color: #ffb4b6;
}
.auth-ok {
  margin: 14px 0 18px;
  padding: 10px 12px;
  background: rgba(48,164,108,.12);
  border: 1px solid rgba(48,164,108,.35);
  border-radius: 8px;
  font-size: 13px;
  color: #7ee2b0;
}
.auth-check { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; font-size: 13px; color: var(--muted); }
.auth-check input { width: auto; }

/* ---------- Nutzer in der Kopfleiste ---------- */

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px 5px 8px;
  border: 1px solid var(--line);
  border-radius: 20px;
  font-size: 12.5px;
  color: var(--muted);
}
.user-chip .dot-role {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--ok);
}
.user-chip.admin .dot-role { background: var(--warn); }

/* ---------- Nutzerverwaltung ---------- */

.users-view { height: calc(100% - 56px); overflow-y: auto; padding: 28px 32px 60px; }
.users-inner { max-width: 900px; margin: 0 auto; }
.users-inner h1 { margin: 0 0 6px; font-size: 22px; }

.utable { width: 100%; border-collapse: collapse; margin-top: 18px; font-size: 13.5px; }
.utable th {
  text-align: left; padding: 8px 10px; color: var(--muted);
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  border-bottom: 1px solid var(--line);
}
.utable td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.utable tr.inactive td { opacity: .5; }
.utable .acts { display: flex; gap: 6px; justify-content: flex-end; }
.role-tag {
  padding: 2px 8px; border-radius: 20px; font-size: 11.5px;
  background: rgba(48,164,108,.14); border: 1px solid rgba(48,164,108,.3); color: var(--ok);
}
.role-tag.admin { background: rgba(245,165,36,.14); border-color: rgba(245,165,36,.3); color: var(--warn); }

.newuser {
  margin-top: 26px; padding: 18px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.newuser h2 { margin: 0 0 14px; font-size: 15px; }
.newuser .grid { display: grid; grid-template-columns: 1fr 1fr 160px 150px; gap: 12px; align-items: end; }
@media (max-width: 860px) { .newuser .grid { grid-template-columns: 1fr; } }

/* ---------- Startbildschirm ---------- */

.drop-view {
  display: grid;
  place-items: center;
  min-height: calc(100% - 56px);
  padding: 32px;
}

.drop {
  width: min(620px, 100%);
  padding: 48px 32px;
  text-align: center;
  border: 2px dashed var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--muted);
  transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--accent); background: #17203a; }
.drop h1 { margin: 16px 0 4px; font-size: 22px; color: var(--text); }

.link { color: var(--accent); cursor: pointer; text-decoration: underline; }

.facts {
  margin: 28px auto 0;
  padding: 0;
  max-width: 460px;
  list-style: none;
  text-align: left;
  font-size: 13px;
}
.facts li { position: relative; padding-left: 22px; margin-bottom: 10px; }
.facts li::before {
  content: "";
  position: absolute;
  left: 4px; top: 7px;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
.facts strong { color: var(--text); }

.drop-more { margin-top: 22px; font-size: 13px; color: var(--muted); }

.linkbtn {
  padding: 0;
  background: none;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
}

/* ---------- Ablage ---------- */

.library {
  height: calc(100% - 56px);
  overflow-y: auto;
  padding: 28px 32px 60px;
}

.lib-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  max-width: 980px;
  margin: 0 auto 24px;
}
.lib-head h1 { margin: 0 0 6px; font-size: 22px; }
.lib-note { margin: 0; max-width: 560px; font-size: 13px; color: var(--muted); }
.lib-note strong { color: var(--text); }
.lib-actions { display: flex; gap: 8px; flex: none; }
.lib-actions label { cursor: pointer; }

.lib-list { max-width: 980px; margin: 0 auto; display: grid; gap: 14px; }

.lib-empty {
  max-width: 980px;
  margin: 40px auto;
  padding: 32px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 12px;
}

.lib-item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 18px;
}

.lib-top { display: flex; align-items: flex-start; gap: 14px; }
.lib-title {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  font-weight: 600;
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 6px;
  margin: -4px 0 0 -6px;
}
.lib-title:hover { border-color: var(--line); }
.lib-title:focus { border-color: var(--accent); outline: none; background: #0e1116; }

.lib-meta { margin: 6px 0 0; font-size: 12.5px; color: var(--muted); }
.lib-meta .badge {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 7px;
  border-radius: 20px;
  background: rgba(245,165,36,.14);
  border: 1px solid rgba(245,165,36,.35);
  color: var(--warn);
  font-size: 11.5px;
}
.lib-meta .badge.src {
  background: rgba(79,140,255,.12);
  border-color: rgba(79,140,255,.35);
  color: var(--accent);
}

.lib-btns { display: flex; gap: 8px; flex: none; }

.lib-exports { margin: 14px 0 0; padding: 12px 0 0; border-top: 1px solid var(--line); }
.lib-exports h3 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}
.lib-export {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 0;
  font-size: 13px;
}
.lib-export + .lib-export { border-top: 1px solid rgba(38,44,55,.6); }
.lib-export .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-export .when { color: var(--muted); font-size: 12px; flex: none; }
.lib-export .acts { display: flex; gap: 6px; flex: none; }

.tiny { padding: 4px 9px; font-size: 12px; border-radius: 6px; }


/* ---------- Einfuegen-Leiste ---------- */

.insert-bar {
  margin: -4px -4px 16px;
  padding: 12px;
  background: #0e1116;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.insert-title {
  display: block;
  margin-bottom: 9px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}
.insert-bar .btnrow { margin-bottom: 8px; }
.toggle.wide { width: 100%; }
.mini {
  width: 100%;
  margin-bottom: 8px;
  padding: 6px 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12.5px;
}
.insert-hint {
  margin: 10px 0 0;
  padding: 7px 10px;
  background: rgba(79,140,255,.12);
  border: 1px solid rgba(79,140,255,.35);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--accent);
}

/* ---------- Frei platzierte Elemente ---------- */

.overlay.placing { cursor: crosshair; }
.overlay.placing .span { pointer-events: none; }

.add {
  position: absolute;
  box-sizing: border-box;
  /* outline statt border: nimmt keinen Platz ein, der Text steht dadurch
     exakt dort, wo ihn auch die erzeugte PDF setzt. */
  outline: 1px dashed rgba(79,140,255,.45);
  border-radius: 2px;
  cursor: move;
  overflow: visible;
}
.add:hover { outline-color: rgba(79,140,255,.8); }
.add.sel { outline: 1px solid var(--accent); box-shadow: 0 0 0 3px rgba(79,140,255,.2); }

.add-text {
  width: 100%;
  height: 100%;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.15;
  padding: 0;
  outline: none;
}
.add-text[contenteditable="true"] {
  cursor: text;
  background: rgba(255,255,255,.75);
  box-shadow: 0 0 0 2px rgba(245,165,36,.5);
}

.add.sig { outline-style: dashed; }
.add.sig img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: block;
}

.add .handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 11px;
  height: 11px;
  background: var(--accent);
  border: 1px solid #fff;
  border-radius: 2px;
  cursor: nwse-resize;
}
.add:not(.sel) .handle { display: none; }

/* ---------- Unterschriften ---------- */

.sig-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
  margin-top: 18px;
}
.sig-card {
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.sig-card .preview {
  display: grid;
  place-items: center;
  height: 90px;
  margin-bottom: 10px;
  background: #fff;
  border-radius: 8px;
}
.sig-card .preview img { max-width: 92%; max-height: 78px; object-fit: contain; }
.sig-card .row2 { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sig-card .row2 strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sig-pad-wrap { position: relative; }
#sig-pad {
  width: 100%;
  height: 190px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: crosshair;
  touch-action: none;
}
.sig-pad-hint {
  position: absolute;
  left: 0; right: 0; top: 50%;
  margin: 0;
  transform: translateY(-50%);
  text-align: center;
  font-size: 13px;
  color: #b9bfc9;
  pointer-events: none;
}
.sig-pad-wrap.drawn .sig-pad-hint { display: none; }

.sig-tools { display: flex; align-items: flex-end; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.sig-label { flex: 1; min-width: 200px; margin-bottom: 0; }

/* ---------- Editor-Layout ---------- */

.editor {
  display: grid;
  grid-template-columns: 156px 1fr 290px;
  height: calc(100% - 56px);
  overflow: hidden;
}

.pages {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 12px 10px;
}

.thumb {
  display: block;
  width: 100%;
  margin-bottom: 10px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 6px;
  background: none;
  cursor: pointer;
  overflow: hidden;
  position: relative;
}
.thumb img { display: block; width: 100%; border-radius: 4px; }
.thumb.active { border-color: var(--accent); }
.thumb .no { display: block; padding: 4px 0 6px; font-size: 12px; color: var(--muted); }
.thumb .dot {
  position: absolute; top: 6px; right: 6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--warn);
}

.stage {
  overflow: auto;
  padding: 28px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background:
    repeating-conic-gradient(#12151b 0% 25%, #0f1115 0% 50%) 50% / 24px 24px;
}

.canvas-wrap {
  position: relative;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
  background: #fff;
  flex: none;
}
.canvas-wrap img { display: block; width: 100%; height: auto; }

.overlay { position: absolute; inset: 0; }
.overlay.hidden { display: none; }

/* ---------- Text-Spans ---------- */

.span {
  position: absolute;
  margin: 0;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 2px;
  white-space: pre;
  overflow: visible;
  color: transparent;           /* Unveraendert: das Seitenbild zeigt den Text */
  background: transparent;
  cursor: text;
  outline: none;
  transform-origin: 0 0;
}
.span:hover { border-color: rgba(79,140,255,.55); background: rgba(79,140,255,.08); }
.span.sel { border-color: var(--accent); background: rgba(79,140,255,.12); }

.span.mod {
  color: inherit;               /* Geaendert: wir zeichnen selbst */
  border-color: rgba(245,165,36,.5);
}
/* Geloescht: die abgetastete Hintergrundfarbe muss deckend bleiben, sonst
   scheint der Originaltext aus dem Seitenbild durch. Der rote Ton kommt
   darum als Inset-Schatten darueber, nicht als background. */
.span.del {
  border-color: rgba(229,72,77,.6);
  box-shadow: inset 0 0 0 999px rgba(229,72,77,.15);
}

/* ---------- Eigenschaften ---------- */

.props {
  background: var(--panel);
  border-left: 1px solid var(--line);
  padding: 16px;
  overflow-y: auto;
}
.props-empty { color: var(--muted); font-size: 13px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 5px; font-size: 12px; color: var(--muted); }

.field textarea, .field select, .field input {
  width: 100%;
  padding: 8px 10px;
  background: #0e1116;
  border: 1px solid var(--line);
  border-radius: 8px;
  resize: vertical;
}
.field textarea:focus, .field select:focus, .field input:focus {
  border-color: var(--accent);
  outline: none;
}
.field input[type=color] { padding: 3px; height: 36px; cursor: pointer; }

.row { display: grid; grid-template-columns: 1fr 92px; gap: 10px; }

.hint {
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--muted);
  min-height: 16px;
}
.hint.warn { color: var(--warn); }

.props-actions { display: flex; gap: 8px; }

/* ---------- Modal, Spinner, Toast ---------- */

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0,0,0,.6);
  z-index: 40;
}
.modal-box {
  width: min(520px, calc(100% - 32px));
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.modal-box h2 { margin: 0 0 12px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

.result-list { margin: 0; padding: 0; list-style: none; font-size: 13px; }
.result-list li {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 7px 0; border-bottom: 1px solid var(--line);
}
.result-list li span:last-child { color: var(--muted); }
.result-warn {
  margin-top: 14px; padding: 10px 12px;
  background: rgba(245,165,36,.1);
  border: 1px solid rgba(245,165,36,.3);
  border-radius: 8px;
  font-size: 12.5px; color: var(--warn);
}

.busy {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; gap: 14px;
  align-content: center;
  background: rgba(15,17,21,.78);
}
.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.busy p { color: var(--muted); }

.toast {
  position: fixed; bottom: 22px; left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  padding: 11px 18px;
  background: #22262f;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
}
.toast.bad { border-color: rgba(229,72,77,.5); color: #ffb4b6; }
