/* LightOnOCR ‑ E-Buch – schlichtes, helles Layout */
:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --ink: #333333;
  --muted: #5f6368;
  --line: #cccccc;
  --accent: #da4353;
  --accent-hover: #dc1e35;
  --accent-ink: #ffffff;
  --err: #da4353;
  --radius: 4px;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  --header-h: 56px; /* Höhe des klebrigen Headers (für die Seiten-Navigation) */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.55 var(--font-family);
}

/* Klebriger Header: „⚙ Einstellungen“ bleibt immer erreichbar */

main {
  max-width: min(1500px, 96vw);
  margin: 0 auto;
  padding: 6px clamp(10px, 2vw, 20px) 24px;
  display: grid;
  gap: 10px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.card h2 {
  margin: 0 0 8px;
  font-size: 1.02rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Breite Bildschirme: Bedienkarten nebeneinander, Ergebnis darunter
   volle Breite – so bleiben PDF, Optionen, Modell laden und Konvertieren
   ohne Scrollen im Blick. */
@media (min-width: 1100px) {
  main { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
  #resultCard { grid-column: 1 / -1; }
}

/* Dropzone */
#dropzone {
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 14px 12px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
#dropzone:hover, #dropzone:focus-visible, #dropzone.drag {
  border-color: var(--accent);
  background: rgba(218, 67, 83, 0.07);
}
.dz-icon {
  display: inline-block;
  font-weight: 800;
  font-size: 1.4rem;
  letter-spacing: 0.08em;
  color: var(--accent);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  padding: 4px 10px;
  margin-bottom: 8px;
}

/* Felder */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
@media (max-width: 640px) { .grid { grid-template-columns: 1fr; } }

fieldset {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 0;
}
legend { font-weight: 600; font-size: 0.85rem; color: var(--muted); padding: 0 6px; }
fieldset label { display: block; margin: 6px 0; cursor: pointer; }

.field {
  display: block;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--ink);
}
.field input, .field select, .field textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  color: var(--ink);
  background: #fff;
}
.field textarea {
  line-height: 1.4;
  resize: vertical;
  white-space: pre-wrap;
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

/* Buttons */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  border: 1px solid var(--line);
  background: var(--line);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius);
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s, color 0.2s,
    filter 0.2s;
}
.btn:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn:disabled {
  background: #e4e4e6;
  color: #9a9a9e;
  border: 1px solid var(--line);
  opacity: 1;
  cursor: not-allowed;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  filter: brightness(0.88);
}

/* Fortschritt */
.progress {
  height: 10px;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 10px;
}
.progress > div {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.2s;
}

/* Zwei unabhängige lokale Modelle (OCR + Bildbeschreibung) nebeneinander */
.load-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 14px;
}
.load-block {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.load-block .btn { margin: 0; }
.load-note { font-size: 0.85em; }
.load-block .progress, .load-block p { width: 100%; margin: 0; }

.muted { color: var(--muted); }
.gpu-diag {
  white-space: pre-wrap;
  font-size: 0.82rem;
  line-height: 1.35;
  background: var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 10px 0 0;
  overflow-x: auto;
}
#status { min-height: 1.4em; }

/* Ergebnis */
.result-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

.page {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 12px;
}
.page header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: #f0f0f0;
  border-bottom: 1px solid var(--line);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 8px 12px;
  position: sticky;
  top: 0;
  z-index: 5;
}
.page header h3 { margin: 0; font-size: 0.95rem; }
.page header .hdr-left {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.page header .ctx {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.page header .ctx::after { content: " \00B7"; }
.page .meta { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.page.error .meta { color: var(--err); }
.page pre {
  margin: 0;
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  max-height: 420px;
  overflow: auto;
  background: #ffffff;
}

/* ── Einstellungen (VLM-Endpunkt) ─────────────────────────────────── */
.settings-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
  margin-top: 10px;
}
.settings-row .grow { flex: 1 1 220px; }

/* ── Ergebnis-Seite: Bild + Overlay | Block-Panel ─────────────────── */
.page .chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 9px 0 8px;
  border: 1px solid var(--line);
  border-radius: 99px;
  background: #f0f0f0;
  font-size: 12px;
  color: var(--ink);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.chip i { width: 8px; height: 8px; border-radius: 50%; background: var(--c, #9ca3af); }
.chip b { font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.chip[aria-pressed="false"] { opacity: 0.45; text-decoration: line-through; }

.page-body {
  display: grid;
  grid-template-columns: minmax(260px, 44%) 1fr;
  gap: 12px;
  padding: 12px;
  align-items: start;
}
.page-body.no-img { grid-template-columns: 1fr; }
@media (max-width: 760px) {
  .page-body { grid-template-columns: 1fr; }
}

.page-img {
  position: relative;
  align-self: start;
  max-width: 100%;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}
.page-img img {
  display: block;
  width: 100%;
  height: auto;
}
.boxes { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }
.box {
  position: absolute;
  border: 1px solid var(--c, #9ca3af);
  border-radius: 2px;
  background: color-mix(in srgb, var(--c, #9ca3af) 7%, transparent);
  cursor: pointer;
  transition: background 0.12s;
}
.box.cont { border-style: dashed; }
.box.on {
  border-width: 1.5px;
  background: color-mix(in srgb, var(--c, #9ca3af) 26%, transparent);
  box-shadow: 0 0 0 2px var(--c, #9ca3af);
  z-index: 2;
}
.box::before {
  content: attr(data-label);
  position: absolute;
  left: -1px;
  bottom: 100%;
  padding: 0 5px;
  border-radius: 3px 3px 0 0;
  background: var(--c, #9ca3af);
  color: #fff;
  font: 600 10px/15px ui-monospace, monospace;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s;
}
.box.on::before { opacity: 1; }

/* ── Ansichten: Blocks / Document / Raw ───────────────────────────── */
.view-tabs {
  display: flex;
  gap: 6px;
  padding-bottom: 10px;
}
.view-tab {
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f0f0f0;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.view-tab.on {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(218, 67, 83, 0.08);
}
.view-tab[hidden] { display: none; }

.panel { min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.blk {
  min-width: 0;
  padding: 10px 14px 11px 16px;
  border-left: 3px solid var(--c, #9ca3af);
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  cursor: pointer;
}
.blk.on { box-shadow: 0 0 0 2px var(--c, #9ca3af); }
.blk .meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 3px;
  font-size: 11px;
}
.blk .lab { color: var(--c, #9ca3af); font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; }
.blk .bbox { color: var(--muted); font: 11px/1.6 ui-monospace, monospace; }

.doc {
  padding: 18px 20px;
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  font-size: 14.5px;
  line-height: 1.6;
}
pre.raw {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: #ffffff;
  font: 12.5px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 520px;
  overflow: auto;
}
.note { padding: 20px 0; color: var(--muted); text-align: center; }
.cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ── Rendered Markdown (Block-/Doc-Sicht) ─────────────────────────── */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1 { font-size: 1.4em; } .md h2 { font-size: 1.25em; }
.md h3, .md h4 { font-size: 1.05em; }
.md h1, .md h2, .md h3, .md h4 { margin: 0.6em 0 0.4em; line-height: 1.25; font-weight: 700; }
.md p, .md ul, .md ol { margin: 0.5em 0; }
.md img { max-width: 100%; }
/* Dokument-Ansichten (.doc) und der Formel-Renderer setzen das Markdown
   direkt in den Container – hier fehlte die Breitenbegrenzung, dadurch
   liefen hochauflösende Abbildungen aus dem Vorschaubereich heraus. */
.doc img,
.view-content img { max-width: 100%; height: auto; }
/* Scroll-Container für breite Tabellen (wird beim Rendern gesetzt: .md/.doc) */
.scroll { margin: 8px 0; max-width: 100%; overflow-x: auto; }
.md table { margin: 0; border-collapse: collapse; font-size: 13px; width: max-content; }
.md th, .md td { padding: 4px 9px; border: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: #f0f0f0; font-weight: 600; }
.md code { background: #f0f0f0; border-radius: 4px; padding: 1px 5px; font: 12px/1.4 ui-monospace, monospace; }
.md pre { background: #f0f0f0; border-radius: var(--radius); padding: 10px; overflow-x: auto; }
.md blockquote { margin: 0.5em 0; padding: 4px 12px; border-left: 3px solid var(--line); color: var(--muted); }


/* ── Einstellungen-Dialog ────────────────────────────────────────────── */
/* explicit top/right/bottom/left statt "inset": älteres iOS-Safari
   (iPadOS < 14.5) kennt "inset" nicht und würde das Overlay unsichtbar
   am Dokumentende platzieren – der Dialog ließe sich dann nicht öffnen. */
.overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px clamp(10px, 2vw, 20px);
  z-index: 50;
}
.overlay[hidden] { display: none; }
/* Einstellungen: auf großen Bildschirmen den vorhandenen Platz nutzen.
   Die Rechts-/Info-Dialoge bleiben bewusst schmal (Lesbarkeit). */
#settingsDialog {
  width: min(1240px, calc(96vw - 2 * clamp(10px, 2vw, 20px)));
  max-width: 1240px;
}
.dialog {
  background: var(--card);
  border-radius: 8px;
  /* Breite wie der App-Inhalt (96vw – Polster), maximal 760px */
  width: min(760px, calc(96vw - 2 * clamp(10px, 2vw, 20px)));
  max-width: 760px;
  /* iOS-Safari: vh meint das größte Viewport (Toolbar weggeklappt) –
     dvh (Safari 15.4+) behebt das; Fallback-Kette für ältere Geräte. */
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.dialog header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 1;
}
.dialog h2 { margin: 0; font-size: 1.05rem; }
.btn.icon {
  background: var(--line);
  border: 1px solid var(--line);
  color: var(--ink);
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}
.dialog-body { padding: 14px 18px 18px; }

/* ── Pausen/Stoppen ─────────────────────────────────────────────────── */
.btn.danger {
  background: #a02a37;
  border-color: #a02a37;
  color: #fff;
}
.btn.danger:hover:not(:disabled) {
  background: #7c1f2a;
  border-color: #7c1f2a;
  color: #fff;
}

/* ── Seiten-Navigation (Einzelansicht) ──────────────────────────────── */
/* Klebrig unter dem Header: Blättern bleibt erreichbar, egal wie tief
   in der Seite man liest. */
.pagenav {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 0;
  background: none;
  box-shadow: none;
  border-radius: 0;
}
.pagenav-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.pagenav input {
  width: 4.5em;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  color: var(--ink);
  background: #fff;
  text-align: center;
}
.pagenav input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}

/* ── Akkordeons (details/summary) wie in der Vorlage ─────────────────── */
dialog .dialog-body details,
.dialog-body details {
  background: rgba(128, 128, 128, 0.05);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 10px;
}
.dialog-body summary {
  font-weight: 600;
  cursor: pointer;
  padding: 9px 10px;
  list-style: none;
}
.dialog-body summary::-webkit-details-marker { display: none; }
.dialog-body summary::after {
  content: "+";
  float: right;
  font-weight: bold;
}
.dialog-body details[open] > summary::after { content: "\2212"; }
.dialog-body .details-body {
  padding: 4px 10px 10px;
  border-top: 1px solid var(--line);
}

/* Zeilenformular (Label links, Steuerung rechts) wie in der Vorlage */
.field.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.field.row select {
  width: 240px;
  max-width: 55%;
  flex: none;
  margin-top: 0;
}
.field.row input {
  width: 55%;
  max-width: 240px;
  margin-top: 0;
}
.field.row input[type="number"] { width: 90px; }

.subhead {
  margin: 0 0 8px;
  font-size: 0.95rem;
  font-weight: 700;
}

/* ── Abbildungen (Figuren) ──────────────────────────────────────────── */
.blk .fig-thumb {
  display: block;
  max-width: 100%;
  max-height: 220px;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  cursor: zoom-in;
}
.doc .md img { border-radius: var(--radius); }
.blk .fig-cap {
  margin: 6px 0 0;
  font-size: 12.5px;
  color: var(--muted);
  font-style: italic;
}

/* ── Formeln (LaTeX) ─────────────────────────────────────────────────── */
.latex-src {
  display: block;
  padding: 8px 10px;
  background: #f0f0f0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}
.latex-out {
  margin-top: 8px;
  overflow-x: auto;
}

/* ── Zeichen-Ersetzungstabelle (Dialog: Formate + Ersetzungen) ──────── */
.repl-table .repl-group td {
  background: #f0f0f0;
  font-weight: 600;
}
input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 38px;
  height: 20px;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 20px;
  background-color: #e0e0e0;
  background-image: radial-gradient(circle closest-side, #ffffff 98%, transparent 100%);
  background-size: 16px 16px;
  background-repeat: no-repeat;
  background-position: left 1px center;
  cursor: pointer;
  transition: background-color 0.15s ease;
}
input[type="checkbox"]:checked {
  background-color: var(--accent);
  background-position: right 1px center;
  border-color: var(--accent);
}
input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
/* Kein eigener Scrollbalken: es scrollt der Dialog (ein Balken).
   Waagerecht nur, wenn die Tabelle wirklich breiter ist. */
.repl-scroll { overflow: visible; }
.repl-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13.5px;
}
#replSettingsTable { table-layout: fixed; }
/* Optionstabelle: nur noch zwei Spalten (Option | Aktiv) */
#replSettingsTable th:nth-child(1) { width: auto; }
#replSettingsTable th:nth-child(2) { width: 72px; }
#replaceTable { table-layout: fixed; }
#replaceTable th:nth-child(1) { width: 68px; }   /* Zeichen */
#replaceTable th:nth-child(2) { width: 104px; }  /* Code (einzeilig) */
#replaceTable th:nth-child(3) { width: 76px; }   /* Anzahl */
#replaceTable th:nth-child(4) { width: auto; }   /* Ersetzen durch – bekommt den Rest */
#replaceTable th:nth-child(5) { width: 66px; }   /* Aktiv */
.repl-table th,
.repl-table td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
  vertical-align: middle;
}
.repl-table th { background: #f0f0f0; font-weight: 600; }
/* Schmale Spalten (Code/Anzahl): wenig Innenabstand, damit der Platz bei
   „Ersetzen durch“/„Wert“ landet */
#replaceTable th:nth-child(2), #replaceTable td:nth-child(2),
#replaceTable th:nth-child(3), #replaceTable td:nth-child(3),
#replSettingsTable th:nth-child(2), #replSettingsTable td:nth-child(2),
#replSettingsTable th:nth-child(4), #replSettingsTable td:nth-child(4),
#replSettingsTable th:nth-child(5), #replSettingsTable td:nth-child(5),
#replaceTable th:nth-child(5), #replaceTable td:nth-child(5) {
  padding-left: 4px;
  padding-right: 4px;
}
#replaceTable th:nth-child(3), #replaceTable td:nth-child(3) {
  text-align: center;
  overflow-wrap: anywhere;
}
.repl-table th { white-space: nowrap; }
/* Kopfzeilen der schmalen Spalten einzeilig halten */
#replaceTable th:nth-child(2), #replaceTable th:nth-child(3), #replaceTable th:nth-child(5),
#replSettingsTable th:nth-child(2), #replSettingsTable th:nth-child(4), #replSettingsTable th:nth-child(5) {
  white-space: nowrap;
}
.repl-table .repl-char { font-size: 1em; font-weight: 700; text-align: center; }
.repl-table .repl-code {
  white-space: nowrap; /* Code bleibt einzeilig (z. B. U+2000–200B) */
  color: var(--muted);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  white-space: nowrap;
}
.repl-table input[type="text"] {
  width: 100%;
  min-width: 110px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
}
.repl-table input[type="checkbox"] {
  width: 38px;
  min-width: 38px;
  height: 20px;
}

/* ── Seiten-Layout (sehr breite Bildschirme): Bedienkarten links gestapelt
   am linken Rand, Ergebnis rechts daneben. #controls ist unterhalb der
   Schwelle „display: contents“ (Karten fließen direkt in das main-Grid);
   ab 1300 px wird der Wrapper zum eigenen Grid und bleibt oben verankert –
   die hohe Ergebnis-Karte kann die Bedienkarten nicht mehr „ziehen“. */
#controls { display: contents; }

@media (min-width: 1300px) {
  main {
    grid-template-columns: minmax(300px, 360px) 1fr;
    grid-template-areas: "controls result";
    align-items: start;
  }
  #controls {
    grid-area: controls;
    display: grid;
    gap: 10px;
    align-content: start;
    /* Klebrig unter dem Header: Die Bedienleiste wandert nicht mehr mit
       dem wachsenden Ergebnis-Text und bleibt beim Lesen immer erreichbar.
       Bei zu hoher Spalte scrollt sie intern. */
    position: sticky;
    align-self: start;
    top: 8px;
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
    overflow-y: auto;
    /* Wichtig: 'visible' wird bei overflow-y:auto zu 'auto' – dadurch entstand
       ein waagerechter Scrollbalken, obwohl nichts breiter war. clip schneidet
       nur ab und erzeugt keinen Balken. */
    overflow-x: clip;
    overscroll-behavior: contain;
  }
  #resultCard { grid-area: result; }
  /* Ergebnis bekommt die volle Restbreite (Seitenbild + Text nebeneinander) */
  #resultCard .page-body { grid-template-columns: minmax(300px, 42%) 1fr; }
}

/* ── Ausgabe: Dropdown + Button in „Konvertieren“ ────────────────────── */
.export-row {
  display: flex;
  gap: 8px;
  margin: 10px 0 4px;
}
.export-row select {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  color: var(--ink);
  background: #fff;
}
.export-row select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* Einstellungs-Knopf unterhalb von „PDF wählen“ (volle Breite) */
.settings-entry {
  width: 100%;
  margin-top: 2px;
  background: var(--line);
  border: 1px solid var(--line);
  color: var(--ink);
}
.settings-entry:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* ── Kompaktere PDF-Wahl (auf allen Breiten) ─────────────────────────── */
#dropzone { padding: 10px 10px; font-size: 0.88rem; }
#dropzone p { margin: 0; }
.dz-icon {
  font-size: 1rem;
  padding: 2px 8px;
  margin-bottom: 4px;
}
#pdfInfo { margin-top: 6px; font-size: 0.8rem; }

/* ── Linke Seitenleiste kompakt (Sidebar-Layout) ─────────────────────── */
@media (min-width: 1300px) {
  #controls { gap: 8px; }
  #controls .card { padding: 10px 12px; }
  #controls .card h2 { font-size: 0.97rem; margin-bottom: 6px; }
  #controls .grid { gap: 8px; }
  #controls .field { font-size: 0.82rem; }
  #controls .field input,
  #controls .field select { padding: 6px 8px; }
  #controls .muted { font-size: 0.8rem; }
  #controls .btn { padding: 7px 12px; }
  #controls .export-row { margin-top: 8px; }
}

/* ── Modell-Info unterhalb von „Seiten“ ──────────────────────────────── */
.model-info {
  margin: 6px 0 2px;
  font-size: 0.8rem;
  white-space: pre-line;
}


/* ── Logo + Anwendungsname in der „PDF wählen“-Sektion ───────────────── */
.title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 6px;
  min-width: 0;
}
.site-logo {
  height: 34px;
  width: auto;
  max-width: 45%;
  flex: none;
  object-fit: contain;
  object-position: left center;
}
.app-name {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--accent);
  min-width: 0;
}

/* Schalter-Reihen im Dialog: Schrift wie die übrigen Feld-Beschriftungen */
.switch-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
}
.switch-row .muted { font-weight: 400; }

/* display:flex darf [hidden] nicht überschreiben (Schalter-Zeilen) */
.switch-row[hidden] { display: none; }

/* Bild-Prompt nutzt die volle Rasterbreite */
.grid .full { grid-column: 1 / -1; }

/* Erweiterte Einstellungen (nur auf Wunsch sichtbar) */
.adv-box {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(128, 128, 128, 0.04);
  padding: 8px 10px;
  margin: 6px 0 10px;
}
.adv-box .switch-row:first-child { margin-top: 0; }
.adv-box .grid { margin-top: 6px; }

/* Trenner zwischen OCR- und Bildteil in „API-Einstellungen“ */
.api-sub {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

/* ── Bubble-Help: erscheint nach ~0,6 s Hover bzw. per Tastatur-Fokus ── */
/* Ausgeblendete Tooltips ragen über den rechten Rand und erzeugten auf
   schmalen Geräten ~50 px Seitwärts-Scroll (opacity:0 bleibt im Layout).
   overflow-x: clip unterbindet das, ohne einen Scroll-Container zu erzeugen
   (position:fixed-Elemente wie der Dialog bleiben unberührt). */
html, body { overflow-x: clip; }

[data-tip] { position: relative; }
[data-tip]:not(summary)::after,
summary[data-tip]::before {
  content: attr(data-tip);
  position: absolute;
  left: 0;
  top: calc(100% + 4px);
  z-index: 90;
  width: max-content;
  max-width: min(320px, 80vw);
  padding: 6px 9px;
  border-radius: var(--radius);
  background: #333333;
  color: #ffffff;
  font-size: 12px;
  font-weight: 400;
  line-height: 1.4;
  white-space: normal;
  text-align: left;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
[data-tip]:not(summary):hover::after,
[data-tip]:not(summary):focus-visible::after,
summary[data-tip]:hover::before,
summary[data-tip]:focus-visible::before {
  opacity: 1;
  transition: opacity 0.15s ease 0.6s;
}
/* Hinweis-Kommentar: ::after der Vorlage („+“/„−“) gilt für summary
   weiterhin; der Tooltip nutzt bewusst ::before. */
.dialog-body summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* ── Responsive: sehr schmale Geräte ─────────────────────────────────── */
@media (max-width: 520px) {
  /* Ersetzungstabellen: schmale Wert-Spalte, alles sichtbar ohne horizontales
     Scrollen (Label-Spalte darf umbrechen) */
  .repl-table { font-size: 12.5px; }
  #replSettingsTable th:nth-child(1) { width: auto; }
  #replSettingsTable th:nth-child(2) { width: 86px; }
  #replSettingsTable th:nth-child(3) { width: auto; }
  #replSettingsTable th:nth-child(4) { width: 52px; }
  #replSettingsTable th:nth-child(5) { width: 52px; }
  #replaceTable th:nth-child(1) { width: 54px; }
  #replaceTable th:nth-child(2) { width: 86px; }
  #replaceTable th:nth-child(3) { width: 60px; }
  #replaceTable th:nth-child(4) { width: auto; }
  #replaceTable th:nth-child(5) { width: 50px; }
  .repl-table input[type="text"] { min-width: 0; }
  .repl-table .repl-code { white-space: normal; word-break: break-word; }

  .field.row {
    flex-direction: column;
    align-items: stretch;
  }
  .field.row select,
  .field.row input,
  .field.row input[type="number"] {
    width: 100%;
    max-width: none;
  }
  .page header { flex-wrap: wrap; }
  .pagenav { flex-wrap: wrap; }
  .export-row { flex-direction: column; }
  .export-row select { width: 100%; }
  .settings-row { flex-direction: column; align-items: stretch; }
  .settings-row .btn { width: 100%; }
  /* Spaltenanordnung: flex-basis220px von .grow wäre die HÖHE und
     erzeugt ~154px Leerraum pro Zeile (z. B. Passcode-Sektion) */
  .settings-row .grow { flex: 0 0 auto; width: 100%; }
}

/* [hidden] muss grundsätzlich gewinnen – Autor-Styles wie display:flex/
   inline-flex würden es sonst überschreiben (Pause/Stop, Chips, Entsperr-Knopf). */
[hidden] { display: none !important; }

/* max Tokens pro Seite: gleiche Breite wie die Dropdowns der Optionen */
#maxTokensInput { width: 240px; max-width: 60%; }

/* ── Rechtlicher Fuß: KI-Hinweis + Impressum ─────────────────────────── */
.legal-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
  justify-content: center;
  padding: 14px 12px 22px;
  margin-top: 18px;
  border-top: 1px solid var(--line);
  font-size: 12.5px;
}
.legal-footer .ki-hint {
  color: var(--ink);
  font-weight: 600;
}
.legal-footer .link-btn {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}
.legal-footer .link-btn:hover { color: var(--accent-hover); }
.legal-footer .link-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.dialog-body section + section { margin-top: 14px; }
.dialog-body section h3 { margin: 0 0 6px; font-size: 14px; }

/* Endpunkt-Vorschläge: natives Auswahlfeld (auf dem Mobiltelefon
   zuverlässiger als die datalist-Combobox) */
/* Hinweis „lokaler Endpunkt von HTTPS-Seite“ (CORS) – kompakt, nur sichtbar
   wenn er zutrifft */
.cors-note {
  margin: 0 0 10px;
  padding: 8px 10px;
  border-left: 3px solid var(--accent);
  background: rgba(218, 67, 83, 0.06);
  border-radius: var(--radius);
  font-size: 0.78rem;
  line-height: 1.35;
  color: var(--ink);
}
.cors-note[hidden] {
  display: none;
}
/* Ungültige Endpunkt-Eingabe */
.field input.invalid,
.field select.invalid {
  border-color: var(--accent);
  background: rgba(218, 67, 83, 0.05);
}
.suggest-select {
  margin-top: 4px;
  font-size: 12.5px;
  color: var(--muted);
}
.suggest-select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Vorschläge des VLM in der Ersetzungstabelle */
.repl-vorschlag {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.25rem;
  padding: 0.2rem 0.35rem;
  border-radius: 0.35rem;
  background: color-mix(in srgb, currentColor 8%, transparent);
  font-size: 0.85rem;
}
.repl-vorschlag .btn {
  flex: 0 0 auto;
  padding: 0.1rem 0.45rem;
  font-size: 0.8rem;
  line-height: 1.4;
  white-space: nowrap;
}
.repl-vorschlag.latex {
  border-left: 3px solid #16a34a;
}

/* Prompt und Antwort nebeneinander (Fehlersuche Zeichenvorschläge) */
@media (max-width: 820px) {
  }

/* ── Eigener Bereich: Zeichen-Ersetzungen ───────────────────────────── */
.repl-dialog {
  width: min(1040px, calc(96vw - 2 * clamp(10px, 2vw, 20px)));
  max-width: 1040px;
  max-height: 96vh;
  /* Kein waagerechter Balken: der Inhalt passt in die Breite. (Bei
     overflow-y: auto wuerde 'visible' sonst zu 'auto' werden.) */
  overflow-x: clip;
}
/* Auf dem PC so viel Hoehe wie noetig – der Inhalt soll ohne Scrollen passen.
   Erst wenn er die Fensterhoehe uebersteigt, scrollt der Dialog. */
@media (min-width: 900px) {
  .repl-dialog { max-height: 96vh; }
}
.repl-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}
.repl-dialog-head h2 { margin: 0; font-size: 1.15rem; }
.repl-dialog-body { overflow: visible; }
.badge {
  display: inline-block;
  min-width: 1.5em;
  margin-left: 0.35em;
  padding: 0 0.4em;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75em;
  line-height: 1.5;
  text-align: center;
}

/* Reiter im Einstellungsdialog – deutlich sichtbar, aktiver Reiter farbig */
.tabs {
  display: flex;
  gap: 4px;
  align-items: flex-end;
  border-bottom: 2px solid var(--line);
  margin: 0.2rem 0 0.9rem;
  padding: 0 0.15rem;
  position: relative;
  z-index: 1;
}
.tab {
  flex: 0 0 auto;
  width: auto;
  padding: 0.55rem 1.15rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--muted);
  background: #f1f3f6;
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 9px 9px 0 0;
  cursor: pointer;
  position: relative;
  top: 2px;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}
.tab:hover {
  background: #e6eaf0;
  color: var(--text, #1f2328);
}
.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
}
.tab.active:hover { background: var(--accent); color: #fff; }
.tab-pane[hidden] { display: none; }
@media (max-width: 640px) {
  .tab { padding: 0.45rem 0.75rem; font-size: 0.92rem; }
}

/* ── Bedienfeld ein-/ausblenden + kein waagerechtes Scrollen ────────── */
#controls.collapsed { display: none !important; }
/* Flex-/Grid-Kinder duerfen schrumpfen – sonst schiebt eine breite Tabelle
   die ganze Seite auf (waagerechter Scrollbalken). */
#controls, #controls > *, #controls .card, .card, .panel, .result-actions { min-width: 0; max-width: 100%; }
.repl-scroll, .settings-row, .field { max-width: 100%; }

/* Ist das Bedienfeld ausgeblendet, nutzt das Ergebnis die volle Breite */
@media (min-width: 1300px) {
  main:has(#controls.collapsed) {
    grid-template-columns: 1fr;
    grid-template-areas: "result";
  }
}

/* Griff am linken Rand: immer sichtbar, unabhaengig vom Panel */
.sidebar-handle {
  position: fixed;
  left: 0;
  /* hoeher: direkt unter der Kopfzeile statt mittig */
  top: 6rem;
  z-index: 40;
  width: 1.5rem;
  padding: 0.9rem 0;
  border: 1px solid var(--line);
  border-left: none;
  border-radius: 0 8px 8px 0;
  background: var(--card);
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}
.sidebar-handle:hover { color: var(--accent); border-color: var(--accent); }
@media (max-width: 520px) {
  .sidebar-handle { top: 5rem; padding: 0.7rem 0; width: 1.35rem; }
}

/* Kompakte Zeilen in der Ersetzungsliste – moeglichst viele Eintraege ohne
   Scrollen sichtbar (auf dem PC; auf schmalen Geraeten bleibt es scrollbar). */
.repl-table th,
.repl-table td { padding: 3px 6px; }
.repl-table input[type="text"] { padding: 2px 5px; }
.repl-table input[type="checkbox"] { width: 34px; min-width: 34px; height: 18px; }
.repl-table .repl-vorschlag { margin-top: 0.15rem; padding: 0.1rem 0.3rem; font-size: 0.8rem; }
.repl-table .repl-vorschlag .btn { padding: 0.05rem 0.4rem; font-size: 0.75rem; }
.repl-dialog .settings-row { margin: 0.15rem 0; }

/* Reiter im Einstellungsdialog: der bisherige Inhalt (.dialog-body) gehoert zum
   Reiter 'Einstellungen'. Die beiden anderen Panes liegen darueber im Dialog. */
#settingsDialog .tab-pane { margin-top: 0.4rem; }
#settingsDialog[data-tab="list"] .dialog-body,
#settingsDialog[data-tab="fmt"] .dialog-body { display: none; }
#settingsDialog:not([data-tab="list"]) #replPaneList,
#settingsDialog:not([data-tab="fmt"]) #replPaneFmt { display: none; }

/* Panes im Einstellungsdialog wie den bisherigen Dialoginhalt einruecken und
   keinen waagerechten Balken erzeugen (dieselbe Regel wie .dialog-body). */
#settingsDialog .tab-pane { padding: 14px 18px 18px; }
#settingsDialog { overflow-x: clip; }

/* ── Einheitliche Schrift in den Einstellungen ──────────────────────── */
#settingsDialog input,
#settingsDialog select,
#settingsDialog textarea,
#settingsDialog button {
  font-family: inherit;
}
#settingsDialog textarea,
#settingsDialog input[type="text"],
#settingsDialog input[type="password"],
#settingsDialog input[type="number"],
#settingsDialog select {
  font-size: 1rem;
  line-height: 1.5;
}
#settingsDialog .muted { font-size: 0.95rem; }

/* ── Mobile: Platz in den Einstellungen besser nutzen ────────────────── */
@media (max-width: 640px) {
  /* Reiterleiste seitlich scrollbar statt Beschriftungen abzuschneiden */
  .tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }
  .tab { white-space: nowrap; }

  /* Tabellenbreiten bleiben wie am Desktop (keine Spalten ausblenden -
     das machte die Tabelle schmaler statt besser). */
}

/* Optionstabelle (Export-Formatierung): den verfuegbaren Platz voll nutzen -
   auch in der mobilen Ansicht. */
#replPaneFmt .repl-scroll { width: 100%; }
#replSettingsTable { width: 100%; table-layout: fixed; }
#replSettingsTable th:nth-child(1),
#replSettingsTable td:nth-child(1) { width: auto; }
#replSettingsTable th:nth-child(2),
#replSettingsTable td:nth-child(2) { width: 72px; }
@media (max-width: 640px) {
  #replSettingsTable { width: 100%; }
  #replSettingsTable th:nth-child(2),
  #replSettingsTable td:nth-child(2) { width: 64px; }
}

/* ── Mobile: Ersetzungsliste – Code/Anzahl schmal, Ersetzen durch breit ── */
@media (max-width: 640px) {
  #replaceTable { width: 100%; min-width: 0; }
  #replaceTable th:nth-child(1), #replaceTable td:nth-child(1) { width: 58px; }  /* Zeichen */
  #replaceTable th:nth-child(2), #replaceTable td:nth-child(2) { width: 72px; }  /* Code – schmal */
  #replaceTable th:nth-child(3), #replaceTable td:nth-child(3) { width: 50px; }  /* Anzahl – schmal */
  #replaceTable th:nth-child(4), #replaceTable td:nth-child(4) { width: auto; }  /* Ersetzen durch – breit */
  #replaceTable th:nth-child(5), #replaceTable td:nth-child(5) { width: 58px; }  /* Aktiv */
  #replaceTable th, #replaceTable td { padding: 4px 4px; }
  #replaceTable .repl-code { white-space: nowrap; }
  #replaceTable input[type="text"] { min-width: 0; }
  /* Kopfzeilen duerfen umbrechen und sind etwas kleiner – nowrap liess sie
     in die Nachbarspalte laufen, in Normalgroesse passten sie nicht. */
  #replaceTable th { white-space: normal; font-size: 12px; }
}

/* Kopfzeile des Einstellungsdialogs deckend halten: beim Scrollen liefen die
   Reiter sonst sichtbar unter dem Titel durch (Ueberlappung). */
#settingsDialog > header {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--card, #fff);
}
/* Reiter nicht nach oben versetzen - sie sollen unter der Kopfzeile bleiben */
.tab { top: 0; }
.tabs { z-index: 1; }

/* Modell-Karte als Akkordeon wie in den Einstellungen: + / − rechts. */
.card-details > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.card-details > summary::-webkit-details-marker { display: none; }
.card-details > summary::after {
  content: "+";
  float: right;
  font-weight: bold;
  font-size: 1.25em;
  padding-left: 8px;
}
.card-details[open] > summary::after { content: "\2212"; }
.card-details > summary h2 { margin: 0; }
.card-details .details-body { margin-top: 10px; }

/* Endpunkt-Felder im lokalen Rechenpfad: gespeicherter Endpunkt bleibt erhalten. */
input.local-endpoint { color: var(--muted, #666); font-style: italic; opacity: 0.85; }
