:root {
  --brand: #0D2C3F;
  --accent: #2EBF8B;
  --accent-soft: #A0EDC6;
  --bg: #f1f5f7;
  --card: #ffffff;
  --text: #1d2b35;
  --muted: #5b6f7c;
  --border: #dbe4e9;
  --danger: #c0392b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  min-height: 100%;
}

/* ---------- Header (wie alphaportal: weiß, Brand-Text dunkel-teal) ---------- */
.app-header {
  display: flex;
  align-items: center;
  gap: 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  color: var(--brand);
  padding: 10px 18px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  color: var(--brand);
  font-size: 1.25rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
}
.brand img { border-radius: 7px; }
.brand strong { color: #20A276; font-weight: 700; }
.privacy-hint {
  font-size: .8rem;
  color: var(--muted);
  margin-left: auto;
}
.lang-switch {
  font-family: inherit;
  font-size: .82rem;
  color: var(--brand);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 8px;
  cursor: pointer;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 22px 16px 60px;
}

/* ---------- Layout mit Werkzeug-Menü links ---------- */
.app-shell {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 1280px;
  margin: 0 auto;
}
.app-shell main { max-width: none; margin: 0; flex: 1 1 auto; min-width: 0; }
#tool-nav {
  flex: 0 0 212px;
  position: sticky;
  top: 64px;
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 14px 8px 24px 10px;
  max-height: calc(100vh - 64px);
  overflow-y: auto;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 9px;
  padding: 8px 11px;
  font-family: inherit;
  font-size: .9rem;
  font-weight: 500;
  color: var(--brand-900, #0D2C3F);
  cursor: pointer;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: #eef3f4; }
.nav-item.active { background: var(--brand, #0D2C3F); color: #fff; }
.nav-item svg { width: 19px; height: 19px; flex-shrink: 0; }
.nav-item .nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-sep { height: 1px; background: var(--border); margin: 7px 8px; }

/* Schmaler Bildschirm: Menü als horizontale Leiste oben */
@media (max-width: 720px) {
  .app-shell { flex-direction: column; gap: 0; }
  #tool-nav {
    flex-direction: row;
    flex: 0 0 auto;
    width: 100%;
    position: static;
    max-height: none;
    overflow-x: auto;
    gap: 4px;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border);
  }
  .nav-item { width: auto; flex-shrink: 0; }
  .nav-item .nav-label { display: none; }
  .nav-item svg { width: 22px; height: 22px; }
}

/* Eingebettet im alphaportal (iframe): eigenes Chrome ausblenden */
body.embedded .app-header { display: none; }
body.embedded main { padding-top: 14px; }

/* Dokument-Overlay im Portal (embed + file): kompakter Viewer ohne doppeltes Chrome */
body.embedded-file #tool-nav { display: none; }
body.embedded-file main { padding: 8px 8px 16px; max-width: none; }
body.embedded-file #view-viewer .tool-head { display: none; }
body.embedded-file #viewer-close { display: none; }

/* ---------- Tool-Grid ---------- */
.tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}
.tool-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 16px;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: box-shadow .15s, transform .15s, border-color .15s;
}
.tool-card:hover {
  border-color: var(--accent);
  box-shadow: 0 6px 18px rgba(13,44,63,.12);
  transform: translateY(-2px);
}
.tool-icon { color: #1B8460; }
.tool-icon svg { width: 30px; height: 30px; }
.tool-name { font-weight: 600; font-size: 1.02rem; color: var(--brand); }
.tool-desc { font-size: .84rem; color: var(--muted); }

/* ---------- Tool-Ansicht ---------- */
.tool-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.tool-head h1 { font-size: 1.3rem; margin: 0; color: var(--brand); }

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}
.panel h2 { font-size: 1rem; margin: 0 0 12px; color: var(--brand); }

.hint { color: var(--muted); font-size: .85rem; }
.filetag {
  display: inline-block;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: .85rem;
  margin: 0 0 12px;
}

/* ---------- Dropzone ---------- */
.dropzone {
  border: 2px dashed #9fb4bf;
  border-radius: 12px;
  background: var(--card);
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color .15s, background .15s;
  margin-bottom: 16px;
}
.dropzone strong { color: var(--brand); }
.dropzone:hover, .dropzone.drag {
  border-color: var(--accent);
  background: #f2fbf7;
}

/* ---------- Buttons & Formulare ---------- */
.btn {
  font-family: inherit;
  font-size: .9rem;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; vertical-align: -3px; }
.btn-icon svg { width: 15px; height: 15px; vertical-align: -2px; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn-primary:hover:not(:disabled) { background: #27aa7b; }
.btn-ghost { background: transparent; }
.btn-icon {
  padding: 4px 9px;
  font-size: .85rem;
  line-height: 1;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-top: 14px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px 18px;
}
.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: .85rem;
  color: var(--muted);
}
input[type="text"], input[type="number"], select {
  font-family: inherit;
  font-size: .92rem;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
}
input[type="range"] { accent-color: var(--accent); }

/* ---------- Datei-Listen ---------- */
.file-list {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.file-list li {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.file-list .fname {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: .9rem;
}
.file-list .fsize { color: var(--muted); font-size: .8rem; }
.file-list img.preview {
  width: 42px;
  height: 42px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* ---------- Viewer ---------- */
.viewer-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  margin-bottom: 12px;
  font-size: .9rem;
}
.viewer-toolbar .sep {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 4px;
}
.viewer-toolbar label { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: .85rem; }
.pageinfo { display: flex; align-items: center; gap: 5px; color: var(--muted); }
#viewer-page { width: 60px; text-align: center; }

.viewer-body {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
#viewer-thumbs {
  width: 130px;
  flex: none;
  max-height: 75vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px;
}
#viewer-thumbs .thumb {
  position: relative;
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 6px;
  overflow: hidden;
  background: var(--card);
}
#viewer-thumbs .thumb.active { border-color: var(--accent); }
#viewer-thumbs .thumb canvas { width: 100%; display: block; }
#viewer-thumbs .thumb span {
  position: absolute;
  right: 4px;
  bottom: 4px;
  background: rgba(13,44,63,.75);
  color: #fff;
  font-size: .7rem;
  border-radius: 4px;
  padding: 1px 5px;
}
#viewer-main {
  flex: 1;
  min-width: 0;
  background: #cfd9df;
  border-radius: 10px;
  padding: 12px;
  max-height: 78vh;
  overflow: auto;
  text-align: center;
}
/* Durchgehendes Scrollen: jede Seite als eigener Block, lazy gerendert */
.vpage {
  margin: 0 auto 12px;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0,0,0,.25);
  max-width: 100%;
}
.vpage:last-child { margin-bottom: 0; }
.vpage canvas { display: block; width: 100%; }
#viewer-main canvas {
  box-shadow: 0 2px 12px rgba(0,0,0,.25);
  background: #fff;
}
.vpage canvas { box-shadow: none; }
@media (max-width: 640px) {
  #viewer-thumbs { display: none; }
}

/* ---------- Seiten bearbeiten ---------- */
.pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 6px;
}
.page-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.page-card.deleted { opacity: .4; }
.page-card.deleted .pthumb { filter: grayscale(1); }
.page-card .pthumb {
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border-radius: 6px;
  overflow: hidden;
}
.page-card .pthumb canvas {
  max-width: 92%;
  max-height: 92%;
  box-shadow: 0 1px 5px rgba(0,0,0,.2);
  transition: transform .15s;
}
.page-card .prow {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;
}
.page-card .pnum { font-size: .78rem; color: var(--muted); margin-right: auto; }
.page-card input[type="checkbox"] { accent-color: var(--accent); }

/* ---------- Unterschrift ---------- */
#sig-pad {
  width: 100%;
  max-width: 560px;
  height: 200px;
  border: 1px dashed #9fb4bf;
  border-radius: 10px;
  background: #fff;
  touch-action: none;
  cursor: crosshair;
  display: block;
}
#sign-preview-wrap,
#txt-preview-wrap {
  background: #cfd9df;
  border-radius: 10px;
  padding: 12px;
  text-align: center;
  max-height: 70vh;
  overflow: auto;
}
#sign-preview,
#txt-preview {
  max-width: 100%;
  box-shadow: 0 2px 12px rgba(0,0,0,.25);
  background: #fff;
  cursor: crosshair;
}

/* ---------- Text bearbeiten (OCR) ---------- */
#edit-preview-wrap {
  background: #cfd9df;
  border-radius: 10px;
  padding: 12px;
  text-align: center;
  max-height: 70vh;
  overflow: auto;
}
#edit-stack {
  position: relative;
  display: inline-block;
  line-height: 0;
}
#edit-preview {
  max-width: 100%;
  box-shadow: 0 2px 12px rgba(0,0,0,.25);
  background: #fff;
  cursor: crosshair;
}
#edit-words {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.ed-word {
  position: absolute;
  margin: 0;
  padding: 0;
  border: 1px solid rgba(32,162,118,.6);
  background: rgba(46,191,139,.18);
  border-radius: 2px;
  cursor: pointer;
  pointer-events: auto;
  transition: background .1s;
}
.ed-word:hover { background: rgba(46,191,139,.4); }
.edit-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.edit-modes { display: flex; flex-wrap: wrap; gap: 8px; }
.ed-mode.active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}
.ed-pagewrap { display: inline-flex; align-items: center; gap: 6px; }
.ed-tools { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.ed-tools label { font-size: .8rem; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }
.ed-inline {
  position: absolute;
  margin: 0;
  padding: 0 1px;
  border: 1px dashed #20A276;
  background: rgba(255,255,255,.7);
  font-family: Helvetica, Arial, sans-serif;
  line-height: 1.05;
  outline: none;
  border-radius: 2px;
  pointer-events: auto;
  min-width: 1.5em;
  box-sizing: content-box;
}
.btn-edit {
  border-color: #20A276;
  color: #157a57;
}
.btn-edit:hover { background: #ecfdf5; }

/* ---------- Busy / Toast ---------- */
#busy {
  position: fixed;
  inset: 0;
  background: rgba(13,44,63,.55);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 200;
  color: #fff;
}
#busy[hidden] { display: none; }
.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255,255,255,.3);
  border-top-color: var(--accent-soft);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#result-bar {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(13,44,63,.25);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  z-index: 150;
  max-width: min(92vw, 620px);
}
#result-bar[hidden] { display: none; }
.result-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
#result-name {
  font-weight: 600;
  font-size: .9rem;
  color: var(--brand);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 260px;
}
#result-size { font-size: .78rem; color: var(--muted); }
.result-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: var(--brand);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: .9rem;
  z-index: 300;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
  max-width: 90vw;
}
#toast.error { background: var(--danger); }
#toast[hidden] { display: none; }

/* ---------- Drucken ---------- */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block !important; }
  #print-area img {
    display: block;
    width: 100%;
    page-break-after: always;
  }
  #print-area img:last-child { page-break-after: auto; }
}
@page { margin: 0; }

/* ===== „Aus alphaportal wählen" — Picker ===== */
.apx-portal-btn { display: inline-flex; align-items: center; gap: .4rem; margin-top: .75rem; padding: .5rem .9rem; border: 1px solid #cdd7dd; border-radius: .55rem; background: #fff; color: #0d2c3f; font-weight: 600; font-size: .9rem; cursor: pointer; }
.apx-portal-btn:hover { border-color: #2EBF8B; color: #1f8261; }
.apx-overlay { position: fixed; inset: 0; background: rgba(13,44,63,.45); display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 9999; }
.apx-overlay[hidden] { display: none; }
.apx-modal { background: #fff; border-radius: 14px; width: 100%; max-width: 640px; max-height: 86vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(13,44,63,.35); overflow: hidden; }
.apx-head { display: flex; align-items: center; justify-content: space-between; padding: .9rem 1.1rem; border-bottom: 1px solid #eef2f4; font-size: 1.05rem; color: #0d2c3f; }
.apx-x { border: 0; background: transparent; font-size: 1.1rem; cursor: pointer; color: #64748b; padding: .2rem .45rem; border-radius: .4rem; }
.apx-x:hover { background: #f1f5f7; color: #0d2c3f; }
.apx-controls { display: flex; gap: .5rem; padding: .8rem 1.1rem; flex-wrap: wrap; border-bottom: 1px solid #eef2f4; }
.apx-controls select, .apx-controls .apx-q { border: 1px solid #cdd7dd; border-radius: .5rem; padding: .45rem .6rem; font-size: .9rem; color: #0d2c3f; background: #fff; }
.apx-controls .apx-q { flex: 1; min-width: 150px; }
.apx-list { overflow-y: auto; flex: 1; padding: .4rem .6rem; }
.apx-row { display: flex; align-items: center; gap: .7rem; padding: .55rem .6rem; border-radius: .55rem; cursor: pointer; }
.apx-row:hover { background: #f1f5f7; }
.apx-row input[type=checkbox] { width: 1.05rem; height: 1.05rem; flex: none; accent-color: #2EBF8B; }
.apx-main { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.apx-name { font-weight: 600; color: #0d2c3f; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.apx-sub { font-size: .78rem; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.apx-badge { flex: none; font-size: .68rem; font-weight: 700; text-transform: uppercase; padding: .15rem .5rem; border-radius: 999px; background: #eef2f4; color: #475569; }
.apx-badge-beleg { background: #e8f7f1; color: #1f8261; }
.apx-badge-lohn { background: #eef0fb; color: #4f46e5; }
.apx-badge-personal { background: #fef3e8; color: #b45309; }
.apx-empty { padding: 2rem; text-align: center; color: #94a3b8; font-size: .9rem; }
.apx-foot { display: flex; align-items: center; justify-content: space-between; gap: .6rem; padding: .8rem 1.1rem; border-top: 1px solid #eef2f4; }
.apx-count { font-size: .85rem; color: #64748b; }
.apx-foot button { border-radius: .55rem; padding: .5rem 1rem; font-weight: 600; font-size: .9rem; cursor: pointer; border: 1px solid transparent; }
.apx-cancel { background: #fff; border-color: #cdd7dd; color: #475569; margin-right: .5rem; }
.apx-ok { background: #2EBF8B; color: #fff; }
.apx-ok:disabled { opacity: .45; cursor: not-allowed; }
