:root {
  --bg: #e5e7eb;
  --panel: #ffffff;
  --panel-2: #f3f4f6;
  --border: #d1d5db;
  --text: #111827;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 4px 16px rgba(0,0,0,.08);
  --topbar-h: 52px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --panel: #1c1f26;
    --panel-2: #262a33;
    --border: #353a45;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #60a5fa;
    --accent-soft: #1e3a5f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 6px 14px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 20;
}
.group { display: flex; align-items: center; gap: 4px; }
.btn, .tool {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; min-width: 36px; padding: 0 10px;
  border: 1px solid transparent; border-radius: 8px;
  background: transparent; color: var(--text);
  font: inherit; cursor: pointer;
}
.btn:hover:not(:disabled), .tool:hover { background: var(--panel-2); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.icon, .tool { padding: 0; width: 36px; }
.btn.primary { background: var(--accent); color: #fff; padding: 0 16px; }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--border); }
.tool.active { background: var(--accent-soft); color: var(--accent); }
.btn svg, .tool svg { width: 20px; height: 20px; flex: none; }
.zoomlabel { min-width: 64px; font-variant-numeric: tabular-nums; }
.tools { padding: 2px; background: var(--panel-2); border-radius: 10px; }
.tools .tool.active { background: var(--panel); box-shadow: var(--shadow); }

.colors { display: flex; gap: 4px; padding: 0 4px; }
.swatch {
  width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid var(--panel); outline: 1px solid var(--border);
  background: var(--c); cursor: pointer; padding: 0;
}
.swatch.active { outline: 2px solid var(--accent); }
.select {
  height: 32px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font: inherit; padding: 0 6px;
}
.search { display: flex; align-items: center; gap: 6px; margin-left: 6px; }
.search input {
  height: 32px; width: 160px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); padding: 0 10px; font: inherit;
}

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 180px; flex: none; overflow-y: auto;
  background: var(--panel); border-right: 1px solid var(--border);
  padding: 12px;
}
.sidebar.collapsed { display: none; }
.thumbs { display: flex; flex-direction: column; gap: 14px; align-items: center; }
.thumb { cursor: pointer; text-align: center; }
.thumb canvas, .thumb .ph {
  display: block; width: 130px; background: #fff;
  box-shadow: var(--shadow); border: 2px solid transparent; border-radius: 3px;
}
.thumb.current canvas, .thumb.current .ph { border-color: var(--accent); }
.thumb .num { font-size: 12px; color: var(--muted); margin-top: 4px; }

.viewer {
  flex: 1; overflow: auto; position: relative;
  -webkit-overflow-scrolling: touch;
}
.pages { display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 20px 12px 80px; }
.viewer.dragover { outline: 3px dashed var(--accent); outline-offset: -10px; }

.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; }
.empty-card {
  background: var(--panel); border-radius: 16px; box-shadow: var(--shadow);
  padding: 36px 40px; text-align: center; max-width: 380px;
}
.empty-card h1 { font-size: 22px; margin: 8px 0 6px; }
.empty-card p { color: var(--muted); margin: 0 0 20px; }
.empty-icon svg { width: 48px; height: 48px; color: var(--accent); }

/* ---------- Page ---------- */
.page {
  position: relative; background: #fff; box-shadow: var(--shadow);
  flex: none;
}
.page canvas.render { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .textLayer { z-index: 1; }
.page .annotationLayer { z-index: 2; position: absolute; inset: 0; }
.page .annot-layer { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.page .annot-layer.capture { pointer-events: auto; touch-action: pan-x pan-y pinch-zoom; }
.page .annot-layer.capture.crosshair { cursor: crosshair; }
.page .annot-layer.capture.textcursor { cursor: text; }
.page .annot-layer > svg.vec { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.page .pageno {
  position: absolute; bottom: -18px; right: 0; font-size: 11px; color: var(--muted);
}
.textLayer .search-hit { background: rgba(250, 204, 21, .45); border-radius: 2px; }
.textLayer .search-hit.current { background: rgba(249, 115, 22, .6); }

/* annotations */
.annot { position: absolute; pointer-events: auto; }
.annot-layer.capture .annot { pointer-events: none; }
.annot.selected { outline: 1.5px dashed var(--accent); outline-offset: 2px; }
.annot.text {
  font-family: Helvetica, Arial, sans-serif;
  line-height: 1.2; white-space: pre-wrap; word-break: break-word;
  cursor: move; padding: 0;
  font-size: calc(var(--scale-factor) * var(--fs) * 1px);
}
.annot.text[contenteditable="true"] { cursor: text; outline: 1.5px solid var(--accent); }
.annot.text:empty::before { content: "Text"; color: #9ca3af; }
.annot.note {
  width: calc(var(--scale-factor) * 22px); height: calc(var(--scale-factor) * 22px);
  border-radius: 4px 4px 4px 0; background: var(--c); cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.35);
  display: grid; place-items: center; color: rgba(0,0,0,.6);
}
.annot.note svg { width: 70%; height: 70%; }
.annot.box { cursor: move; }
.annot.signature img { width: 100%; height: 100%; display: block; pointer-events: none; }
.annot.field {
  border: 1.5px solid #2563eb; background: rgba(37, 99, 235, .1);
  font-size: calc(var(--scale-factor) * 10px); color: #1d4ed8;
  display: flex; align-items: center; padding-left: 3px; overflow: hidden; white-space: nowrap;
}
.annot.field.checkbox { justify-content: center; padding: 0; }
.annot .handle {
  position: absolute; right: -7px; bottom: -7px; width: 14px; height: 14px;
  background: var(--accent); border: 2px solid #fff; border-radius: 50%;
  cursor: nwse-resize; touch-action: none;
}
svg.vec .hit { pointer-events: stroke; cursor: move; }
svg.vec .hit-fill { pointer-events: fill; cursor: move; }
.annot-layer.capture svg.vec * { pointer-events: none !important; }
svg.vec .sel { stroke: var(--accent); stroke-dasharray: 4 3; fill: none; }

/* ---------- Seitenübersicht ---------- */
.organizer {
  position: fixed; inset: 0; z-index: 50; background: var(--bg);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.organizer.busy .org-grid { opacity: .6; pointer-events: none; }
.organizer.dragover { outline: 3px dashed var(--accent); outline-offset: -8px; }
.org-bar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px;
  padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.org-count { font-size: 13px; }
.org-grid {
  flex: 1; overflow-y: auto; padding: 20px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 22px 18px; align-content: start;
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}
.org-item {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px;
  cursor: pointer; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  touch-action: manipulation;
}
.org-thumb {
  position: relative; width: 100%; max-width: 160px; background: #fff;
  box-shadow: var(--shadow); border-radius: 4px; outline: 3px solid transparent; outline-offset: 2px;
  transition: outline-color .1s, transform .1s;
}
.org-thumb img { width: 100%; height: 100%; display: block; border-radius: 4px; opacity: 0; transition: opacity .2s; }
.org-thumb img.loaded { opacity: 1; }
.org-item.selected .org-thumb { outline-color: var(--accent); }
.org-item .check {
  position: absolute; right: -8px; top: -8px; width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 14px; display: none; place-items: center;
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
}
.org-item.selected .check { display: grid; }
.org-item .badge, .org-ghost .badge {
  position: absolute; left: 6px; bottom: 6px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: #f59e0b; color: #fff; font-size: 11px; display: grid; place-items: center;
}
.org-ghost .badge { left: auto; right: -8px; top: -8px; bottom: auto; background: var(--accent); }
.org-item .num { font-size: 12px; color: var(--muted); }
.org-item.dragging { opacity: .35; }
.org-item.drop-before::before, .org-item.drop-after::after {
  content: ""; position: absolute; top: 0; bottom: 22px; width: 4px; border-radius: 2px; background: var(--accent);
}
.org-item.drop-before::before { left: -11px; }
.org-item.drop-after::after { right: -11px; }
.org-ghost {
  position: fixed; left: 0; top: 0; z-index: 200; pointer-events: none; background: #fff;
  box-shadow: 0 10px 30px rgba(0,0,0,.3); border-radius: 4px; opacity: .92;
}
.org-ghost img { width: 100%; display: block; opacity: 1; }
.org-ghost .check { display: none; }

/* ---------- Statusbar ---------- */
.statusbar {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 4px 12px; padding-bottom: max(4px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--border); font-size: 12px;
}

/* ---------- Dialog ---------- */
.dialog {
  border: none; border-radius: 14px; padding: 0; box-shadow: var(--shadow);
  background: var(--panel); color: var(--text); width: min(560px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-inner { padding: 20px; }
.dialog h2 { margin: 0 0 12px; font-size: 18px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; align-items: center; }
.sig-canvas {
  width: 100%; height: 180px; background: #fff; border: 1px dashed var(--border);
  border-radius: 10px; touch-action: none; display: block;
}
.sig-saved { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.sig-item {
  position: relative; background: #fff; border: 1px solid var(--border); border-radius: 8px;
  padding: 6px; cursor: pointer; height: 64px; display: grid; place-items: center;
}
.sig-item:hover { border-color: var(--accent); }
.sig-item img { max-height: 50px; max-width: 160px; }
.sig-item .del {
  position: absolute; top: -8px; right: -8px; width: 22px; height: 22px; border-radius: 50%;
  border: none; background: var(--danger); color: #fff; cursor: pointer; font-size: 13px; line-height: 1;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%) translateY(20px);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .btn span { display: none; }
  .btn:not(.primary):not(.zoomlabel) { padding: 0; width: 36px; }
  .search input { width: 110px; }
  .sidebar { position: absolute; top: 0; bottom: 0; z-index: 15; box-shadow: var(--shadow); }
  .layout { position: relative; }
}
@media (max-width: 600px) {
  .org-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); padding: 14px; gap: 18px 12px; }
  .org-count { width: 100%; order: 5; }
  .topbar { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .tools { order: 10; width: 100%; overflow-x: auto; justify-content: space-between; }
  .zoomlabel { min-width: 52px; font-size: 12px; }
  #searchInfo { display: none; }
}
