:root {
  --bg: #1f2328;
  --bg2: #272c33;
  --bg3: #313740;
  --line: #3b424c;
  --fg: #e6e8eb;
  --muted: #9aa3ad;
  --accent: #3b82f6;
  --accent2: #2563eb;
  --danger: #e5484d;
  --viewer: #4a5059;
  --radius: 6px;
  font-family: "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  font-size: 13px;
  color: var(--fg);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body { display: flex; flex-direction: column; }
button, select, input { font: inherit; color: inherit; }
button {
  background: var(--bg3); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 5px 10px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
button:hover:not(:disabled) { background: #3a414b; }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent2); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--accent2); }
button.big { font-size: 15px; padding: 10px 22px; }
button.icon-only { padding: 5px 7px; }
button.small { padding: 2px 6px; font-size: 11px; }
button svg { width: 17px; height: 17px; flex: none; }
select, input[type=text], input[type=number], input[type=search], input[type=password] {
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 7px;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.muted { color: var(--muted); }
.grow { flex: 1; }

#topbar { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: var(--bg2); border-bottom: 1px solid var(--line); flex-wrap: nowrap; overflow-x: auto; }
#topbar .group { display: flex; align-items: center; gap: 4px; padding-right: 8px; border-right: 1px solid var(--line); }
#topbar .group:last-child, #topbar .group.grow { border-right: none; }
#pageInput { width: 46px; text-align: center; }
#pageCount { color: var(--muted); min-width: 34px; }
#searchInput { width: 190px; }
#searchInfo { color: var(--muted); min-width: 48px; font-size: 12px; text-align: center; }

#toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 10px; padding: 4px 8px; background: var(--bg2); border-bottom: 1px solid var(--line); }
#toolbar .tools { display: flex; align-items: center; gap: 2px; flex-wrap: wrap; }
#toolbar .tool { flex-direction: column; gap: 2px; padding: 4px 6px; min-width: 54px; background: transparent; border-color: transparent; font-size: 11px; }
#toolbar .tool:hover { background: var(--bg3); }
#toolbar .tool.active { background: rgba(59,130,246,.22); border-color: var(--accent); }
#toolbar .tool.danger svg { color: #ff8a8d; }
#toolbar .tool.accent svg { color: #7fb0ff; }
#toolbar .sep { width: 1px; height: 34px; background: var(--line); margin: 0 4px; }
#toolbar .props { display: flex; align-items: center; gap: 10px; white-space: nowrap; flex-wrap: wrap; }
#toolbar .props label { display: flex; align-items: center; gap: 5px; color: var(--muted); }
#toolbar .props input[type=range] { width: 70px; }
#toolbar .props input[type=number] { width: 54px; }
#toolbar .props input[type=color] { width: 28px; height: 24px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; }
#propWidthOut { min-width: 20px; color: var(--fg); }
.swatches { display: flex; gap: 3px; }
.swatches span { width: 16px; height: 16px; border-radius: 50%; border: 1px solid rgba(255,255,255,.35); cursor: pointer; }

#main { flex: 1; display: flex; min-height: 0; }
#sidebar { width: 190px; background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-actions { display: flex; justify-content: space-between; gap: 2px; padding: 6px; border-bottom: 1px solid var(--line); }
#thumbs { flex: 1; overflow-y: auto; padding: 10px 8px; outline: none; }
.side-hint { font-size: 10.5px; color: var(--muted); padding: 6px 8px; border-top: 1px solid var(--line); }
.thumb { position: relative; display: flex; flex-direction: column; align-items: center; padding: 6px; border-radius: 6px; margin-bottom: 6px; cursor: pointer; border: 2px solid transparent; }
.thumb:hover { background: var(--bg3); }
.thumb.current { background: rgba(255,255,255,.06); }
.thumb.selected { border-color: var(--accent); background: rgba(59,130,246,.15); }
.thumb canvas { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.5); max-width: 150px; }
.thumb .num { font-size: 11px; color: var(--muted); margin-top: 4px; }
.thumb .badge { position: absolute; top: 8px; right: 10px; background: var(--danger); color: #fff; font-size: 9px; padding: 1px 4px; border-radius: 3px; }
.thumb.drop-before::before, .thumb.drop-after::after { content: ""; position: absolute; left: 4px; right: 4px; height: 3px; background: var(--accent); border-radius: 2px; }
.thumb.drop-before::before { top: -5px; }
.thumb.drop-after::after { bottom: -5px; }

#viewerWrap { flex: 1; position: relative; min-width: 0; background: var(--viewer); }
#viewer { position: absolute; inset: 0; overflow: auto; outline: none; padding: 20px 0 40px; }
.page { position: relative; margin: 0 auto 16px; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.45); }
.page canvas.pdf { position: absolute; left: 0; top: 0; }
.page svg.overlay { position: absolute; left: 0; top: 0; overflow: visible; z-index: 2; }
.page .pagelabel { position: absolute; left: -2px; top: -16px; font-size: 10px; color: #cfd4da; }
.page textarea.text-edit {
  position: absolute; z-index: 5; border: 1px dashed var(--accent); background: rgba(255,255,255,.85); resize: none; overflow: hidden;
  padding: 0; margin: 0; line-height: 1.2; white-space: pre; outline: none; min-width: 20px;
}
body.tool-draw .page svg.overlay { cursor: crosshair; }
body.tool-text .page svg.overlay { cursor: text; }
body.tool-place .page svg.overlay { cursor: copy; }
.overlay .hit { fill: transparent; cursor: move; pointer-events: all; }
.overlay .sel-box { fill: none; stroke: var(--accent); stroke-dasharray: 4 3; pointer-events: none; }
.overlay .handle { fill: #fff; stroke: var(--accent); cursor: nwse-resize; pointer-events: all; }
.overlay .search-hit { fill: rgba(255, 200, 0, .4); pointer-events: none; }
.overlay .search-hit.cur { fill: rgba(255, 120, 0, .55); }

/* pdf.js text layer */
.textLayer { position: absolute; text-align: initial; inset: 0; overflow: clip; opacity: 1; line-height: 1; text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; caret-color: CanvasText; z-index: 1; }
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(0, 90, 255, .3); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.textLayer.selecting .endOfContent { top: 0; }
[data-main-rotation="90"] { transform: rotate(90deg) translateY(-100%); }
[data-main-rotation="180"] { transform: rotate(180deg) translate(-100%, -100%); }
[data-main-rotation="270"] { transform: rotate(270deg) translateX(-100%); }

#welcome { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--viewer); }
.welcome-card { text-align: center; background: var(--bg2); padding: 40px 50px; border-radius: 12px; border: 1px solid var(--line); max-width: 480px; }
.logo { font-size: 30px; font-weight: 700; letter-spacing: -.5px; margin-bottom: 10px; }
.logo span { color: var(--accent); margin-left: 4px; font-weight: 400; }
#dropHint { position: absolute; inset: 12px; border: 3px dashed var(--accent); border-radius: 12px; background: rgba(59,130,246,.15); display: none; align-items: center; justify-content: center; font-size: 22px; pointer-events: none; z-index: 10; }
body.dragging #dropHint { display: flex; }

#panel { width: 290px; background: var(--bg2); border-left: 1px solid var(--line); overflow-y: auto; }
#panel[hidden] { display: none; }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; font-weight: 600; border-bottom: 1px solid var(--line); }
#panel section { padding: 12px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; }
#panel h3 { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.ff { display: flex; flex-direction: column; gap: 3px; margin-bottom: 6px; }
.ff label { font-size: 11.5px; color: var(--muted); word-break: break-all; }
.ff input[type=text], .ff select, .ff textarea { width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 5px; padding: 4px 6px; color: var(--fg); font: inherit; }
.ff.cb { flex-direction: row; align-items: center; gap: 6px; }
#docInfo { font-size: 12px; line-height: 1.6; word-break: break-all; }

#statusbar { display: flex; gap: 10px; padding: 3px 10px; background: var(--bg2); border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }

#toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); background: #111; color: #fff; padding: 10px 18px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,.5); opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 100; max-width: 70vw; }
#toast.show { opacity: 1; }
#toast.error { background: #7a1d20; }

.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50; }
.modal { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; z-index: 51; min-width: 440px; max-width: 92vw; max-height: 90vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.6); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; font-weight: 600; font-size: 14px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.modal-body.grid2 { display: grid; grid-template-columns: 140px 1fr; align-items: center; }
.modal-foot { display: flex; gap: 8px; align-items: center; padding: 12px 16px; border-top: 1px solid var(--line); }
.radio, .check { display: flex; align-items: center; gap: 8px; }
.help table { border-collapse: collapse; }
.help td { padding: 4px 10px 4px 0; vertical-align: top; }
.help td:first-child { white-space: nowrap; color: #b8c7ff; font-family: Consolas, monospace; }
.help p { max-width: 560px; line-height: 1.5; margin: 6px 0; }

#sigModal { width: 680px; }
.tabs { display: flex; gap: 4px; padding: 10px 16px 0; }
.tab { border-bottom-left-radius: 0; border-bottom-right-radius: 0; background: transparent; }
.tab.active { background: var(--bg3); border-bottom-color: var(--bg3); }
.tab-body { padding: 12px 16px; background: var(--bg3); margin: 0 16px; border-radius: 0 6px 6px 6px; display: flex; flex-direction: column; gap: 10px; }
.tab-body[hidden] { display: none; }
#sigPad { width: 100%; height: 210px; background: #fff; border-radius: 6px; cursor: crosshair; touch-action: none; }
.row { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
#sigText { font-size: 16px; padding: 8px; }
.sig-fonts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sig-fonts div { background: #fff; color: #0d1b6b; border-radius: 6px; padding: 10px; font-size: 30px; cursor: pointer; border: 3px solid transparent; text-align: center; overflow: hidden; white-space: nowrap; min-height: 60px; }
.sig-fonts div.active { border-color: var(--accent); }
.sig-preview { background: repeating-conic-gradient(#ddd 0% 25%, #fff 0% 50%) 50% / 16px 16px; border-radius: 6px; min-height: 120px; display: flex; align-items: center; justify-content: center; }
.sig-preview img { max-width: 100%; max-height: 180px; }
.sig-saved { display: flex; gap: 8px; padding: 10px 16px 0; flex-wrap: wrap; }
.sig-saved:empty { display: none; }
.sig-saved .item { position: relative; background: #fff; border-radius: 6px; padding: 4px; cursor: pointer; border: 2px solid transparent; }
.sig-saved .item:hover { border-color: var(--accent); }
.sig-saved .item img { height: 46px; display: block; }
.sig-saved .item .x { position: absolute; top: -7px; right: -7px; width: 18px; height: 18px; border-radius: 50%; background: var(--danger); color: #fff; font-size: 11px; display: flex; align-items: center; justify-content: center; }
.sig-saved::before { content: "Gespeichert – anklicken zum Platzieren:"; width: 100%; font-size: 11.5px; color: var(--muted); }

body:not(.has-doc) .needs-doc { opacity: .4; pointer-events: none; }
body:not(.has-doc) #toolbar { opacity: .4; pointer-events: none; }
body.has-doc #welcome { display: none; }
body.busy, body.busy * { cursor: progress !important; }

.subform { margin: 0 0 6px 26px; padding: 10px 12px; background: var(--bg3); border-radius: 8px; }
.subform .grid2, .modal-body .grid2 { display: grid; grid-template-columns: 150px 1fr; gap: 8px; align-items: center; }
.subform.disabled { opacity: .45; pointer-events: none; }
.row-l { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row-l select { flex: 1; min-width: 160px; }
.small-text { font-size: 11.5px; line-height: 1.45; }
#certInfo { margin-top: 8px; }
#protectModal { width: 640px; }
.progress { position: relative; height: 22px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.progress .bar { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--accent); transition: width .2s; }
.progress .lbl { position: relative; display: block; text-align: center; line-height: 20px; font-size: 12px; }
.overlay .hover-run { fill: rgba(59,130,246,.12); stroke: var(--accent); stroke-dasharray: 3 2; pointer-events: none; }
body.tool-edittext .page svg.overlay { cursor: text; }
.textLayer span.ocr { color: transparent; }
