:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --border: #e2e5ea;
  --text: #1a1f2b;
  --muted: #5b6573;
  --accent: #2563eb;
  --warn: #b45309;
  --ok: #047857;
  --sev-kontraindiziert: #b91c1c;
  --sev-schwer: #d97706;
  --sev-mittel: #ca8a04;
  --sev-info: #2563eb;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
header {
  padding: 16px 24px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: 18px; font-weight: 600; }
header h1 .version { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 6px; vertical-align: middle; }
header p  { margin: 4px 0 0; color: var(--muted); font-size: 12px; }

main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 16px 24px;
}
@media (max-width: 1100px) { main { grid-template-columns: 1fr; } }

.check-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-content: start;
}
@media (max-width: 1100px) {
  .check-grid { grid-template-columns: 1fr; }
}

.patcomor-grid {
  display: flex;
  flex-direction: row;
  gap: 24px;
  align-items: flex-start;
}
.patcomor-grid > div { flex: 1 1 0; min-width: 0; }
.patcomor-grid > div:last-child { flex: 1.25 1 0; }
.patcomor-grid h3 { margin: 0 0 8px; font-size: 13px; color: var(--accent); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}
.card h2 { margin: 0 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

textarea, input, select {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fafbfc;
  color: var(--text);
}
textarea { font-family: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, monospace; font-size: 13px; resize: vertical; min-height: 220px; }

/* Frutiger 45 Light lokal einbinden, falls vorhanden. Datei nicht im Repo
   (siehe fonts/README.md). Wenn die Datei fehlt, ignoriert der Browser
   das @font-face und fällt auf den restlichen family-stack zurück. */
@font-face {
  font-family: "Frutiger 45 Light";
  src: url("../fonts/frutiger-45-light.otf") format("opentype");
  font-weight: 300;
  font-style: normal;
  font-display: swap;
}

/* Ausgabe spiegelt das Word-Layout: Frutiger 45 Light 11pt mit
   Standard-Tabstopps von 1.27cm. Wenn die Schrift lokal installiert ist
   (Office-Installation), entspricht das Feld pixelgenau dem Word-Output;
   sonst greift der Sans-Serif-Fallback (proportional, näher an Word als
   Monospace). Die Tab-Anzahl pro Zeile wird in formatLines() bereits
   passend für diese Schrift/Tabstopp-Kombination berechnet. */
#output-flat {
  font-family: "Frutiger 45 Light", "Frutiger LT 45 Light", "Frutiger LT Std 45 Light", Frutiger, sans-serif;
  font-size: 11pt;
  tab-size: 1.55cm;
  -moz-tab-size: 1.55cm;
}

.row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.row label { color: var(--muted); font-size: 12px; min-width: 110px; }

button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  font: inherit;
}
button:hover { background: #eef1f6; }
button.primary { background: var(--accent); color: white; border-color: var(--accent); }
button.primary:hover { background: #1d4fd1; }

.group { margin-bottom: 14px; }
.group h3 { margin: 0 0 6px; font-size: 13px; color: var(--accent); }
.group ul { list-style: none; margin: 0; padding: 0; }
.group li {
  padding: 4px 0;
  border-bottom: 1px dashed var(--border);
  display: flex; justify-content: space-between; gap: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.row-side {
  display: inline-flex; align-items: center; gap: 8px;
  flex-shrink: 0;
}
.dosing-link {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 11px;
  color: var(--accent);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 6px;
  background: #fafbfc;
  white-space: nowrap;
}
.dosing-link:hover { background: #eef1f6; text-decoration: underline; }
.embryotox-link { color: #9d2c6b; }
.embryotox-link:hover { background: #fbeef4; }
.group li.unknown { background: #fef3c7; padding: 4px 6px; border-radius: 4px; }
.line { white-space: pre-wrap; }
.meta { color: var(--muted); font-size: 12px; }
.meta.warn { color: var(--warn); }
.meta.online { color: var(--accent); }

.interaction {
  border-left: 4px solid;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: #fafbfc;
  border-radius: 0 4px 4px 0;
}
.interaction.sev-kontraindiziert { border-color: var(--sev-kontraindiziert); }
.interaction.sev-schwer          { border-color: var(--sev-schwer); }
.interaction.sev-mittel          { border-color: var(--sev-mittel); }
.interaction.sev-info            { border-color: var(--sev-info); }
.interaction strong { font-size: 11px; padding: 2px 6px; border-radius: 3px; background: #1a1f2b; color: white; margin-right: 8px; }
.interaction.sev-kontraindiziert strong { background: var(--sev-kontraindiziert); }
.interaction.sev-schwer strong          { background: var(--sev-schwer); }
.interaction.sev-mittel strong          { background: var(--sev-mittel); }
.interaction.sev-info strong            { background: var(--sev-info); }
.interaction .pair { font-weight: 500; }
.interaction p { margin: 4px 0 0; color: var(--muted); }
.ok { color: var(--ok); font-weight: 500; }
.empty { color: var(--muted); font-style: italic; }

/* === BMP-Druckbild gem. KBV Anlage 3, Spec 2.7 (DIN A4 quer) === */
.bmp-print {
  font-family: Arial, Helvetica, sans-serif;
  background: white;
  color: black;
  padding: 8.5mm;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 297mm;
  max-width: 100%;
  box-sizing: border-box;
  font-size: 12pt;
}
/* Kopf: Identifikationsblock 70mm x 40mm, Adminblock 167mm x 40mm, Carrier 40x40 */
.bmp-header { display: grid; grid-template-columns: 70mm 167mm 40mm; gap: 0; align-items: stretch; }
.bmp-id { border: 1px solid black; padding: 2mm 3mm; height: 40mm; }
.bmp-id-name { font-size: 20pt; font-weight: bold; line-height: 1.05; }
.bmp-id-page { font-size: 14pt; margin-top: 4mm; }
.bmp-admin { border: 1px solid black; border-left: 0; padding: 2mm 3mm; height: 40mm; display: flex; flex-direction: column; }
.bmp-admin-row1 { display: flex; justify-content: space-between; font-size: 14pt; margin-bottom: 2mm; }
.bmp-pat strong { font-weight: 700; }
.bmp-admin-row2 { display: flex; justify-content: space-between; gap: 8mm; font-size: 11pt; flex: 1; min-height: 0; }
.bmp-arzt { display: flex; flex-direction: column; min-height: 0; }
.bmp-arzt > div { line-height: 1.15; }
.bmp-arzt .bmp-stamp { margin-top: auto; padding-top: 1mm; }
.bmp-params { text-align: right; font-size: 11pt; line-height: 1.15; }
.bmp-carrier { width: 40mm; height: 40mm; padding: 3mm; box-sizing: border-box; }

/* Medikationstabelle: 28cm Gesamtbreite, exakte Spaltenbreiten in cm */
.bmp-table {
  width: 280mm;
  border-collapse: collapse;
  margin-top: 3mm;
  table-layout: fixed;
  font-size: 12pt;
}
.bmp-table th, .bmp-table td {
  border: 1px solid black;
  padding: 1mm 1.5mm;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;
}
.bmp-table thead th {
  background: #e8eaee;
  font-weight: bold;
  font-size: 11pt;            /* statt 14pt – damit "Stärke" und "Dosierung" passen */
  text-align: left;
  white-space: normal;        /* Header dürfen umbrechen */
  text-overflow: clip;
}
.bmp-table thead .bmp-dose-head th {
  font-size: 8.5pt;
  font-family: "Arial Narrow", Arial, sans-serif;
  height: 16mm;               /* genug Platz für 60°-rotierten Text */
  vertical-align: bottom;
  text-align: left;
  padding: 0 1mm 1mm;
  white-space: nowrap;
  overflow: visible;          /* rotierter Text darf in die obere Zelle ragen */
}
.bmp-table thead .bmp-dose-head th > span {
  display: inline-block;
  transform: rotate(-60deg);
  transform-origin: left bottom;
  line-height: 1;
  font-weight: bold;
}
.bmp-table .bmp-c1  { width: 40mm; }
.bmp-table .bmp-c2  { width: 44mm; }
.bmp-table .bmp-c3  { width: 18mm; text-align: right; }
.bmp-table .bmp-c4  { width: 18mm; }
.bmp-table .bmp-c5a, .bmp-table .bmp-c5b,
.bmp-table .bmp-c5c, .bmp-table .bmp-c5d { width: 8mm; text-align: center; }
.bmp-table .bmp-c6  { width: 20mm; }
.bmp-table .bmp-c7  { width: 64mm; }
.bmp-table .bmp-c8  { width: 44mm; }
.bmp-table .bmp-dose-text { text-align: center; font-style: italic; }
.bmp-table .bmp-section td { background: #f5f6fa; font-size: 14pt; padding: 2mm 3mm; }

/* Fußbereich: 12cm Disclaimer/Version + 11cm Hersteller + 5cm Freifeld */
.bmp-foot { display: grid; grid-template-columns: 120mm 110mm 50mm; border-top: 1px solid black; margin-top: 4mm; padding-top: 1mm; font-size: 8pt; }
.bmp-disclaimer { padding-right: 4mm; line-height: 1.3; }
.bmp-hersteller { text-align: center; }
.bmp-freifeld { /* MUSS leer bleiben */ }

@media (max-width: 1200px) {
  .bmp-print { width: 100%; }
  .bmp-table { width: 100%; }
  .bmp-header { grid-template-columns: 1.6fr 4fr 1fr; }
}

.sub { color: var(--muted); font-size: 11px; margin-top: 4px; }
.warn-note {
  margin-top: 8px;
  padding: 6px 10px;
  background: #fffbeb;
  border-left: 3px solid var(--warn);
  border-radius: 4px;
  color: #78350f;
  font-size: 12px;
  line-height: 1.4;
}

.banner {
  margin-top: 8px;
  padding: 8px 12px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 6px;
  font-size: 12px;
  color: #78350f;
}

/* Komorbiditäts-Checkboxen */
.comorbidities {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  gap: 4px 10px;
  font-size: 12px;
  margin-top: 4px;
}
.comorbidities label.co {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 6px; border-radius: 4px;
  white-space: nowrap;
}
.comorbidities label.co:hover { background: #f1f3f7; }
.comorbidities label.gfr input { width: 80px; }

/* Editier-Tabelle */
.edit-table {
  width: 100%; border-collapse: collapse; font-size: 12px;
}
.edit-table th, .edit-table td {
  border: 1px solid var(--border); padding: 3px 5px; vertical-align: middle;
}
.edit-table th { background: #f1f3f7; text-align: left; }
.edit-table input, .edit-table select { width: 100%; border: none; background: transparent; padding: 4px; font-size: 12px; }
.edit-table input.num { width: 50px; text-align: center; }
.edit-table .empty { color: var(--muted); text-align: center; padding: 12px; font-style: italic; }
.edit-table .prn { display: inline-flex; align-items: center; gap: 4px; margin-right: 6px; }
.edit-table button[data-act="del"] { padding: 2px 6px; font-size: 14px; line-height: 1; color: var(--sev-kontraindiziert); border-color: transparent; background: transparent; }
.edit-table button[data-act="del"]:hover { background: #fee2e2; }

.bmp-layout { display: grid; grid-template-columns: 1fr 220px; gap: 16px; align-items: start; }
.bmp-side h3 { margin: 0 0 6px; font-size: 13px; color: var(--accent); }
.bmp-side canvas { width: 180px; height: 180px; border: 1px solid var(--border); }

@media (max-width: 800px) {
  .bmp-layout { grid-template-columns: 1fr; }
}

/* === Mobile-Layout (≤640px): kompakter, ohne iOS-Auto-Zoom === */
@media (max-width: 640px) {
  body { font-size: 14px; }
  header { padding: 12px 14px; }
  header h1 { font-size: 16px; }
  header p { font-size: 11px; }
  main { padding: 12px 14px; gap: 12px; }
  .card { padding: 12px; border-radius: 6px; }
  .card h2 { font-size: 13px; }

  /* iOS zoomt nicht, wenn das Input-Font ≥16px ist */
  textarea, input, select { font-size: 16px; padding: 10px; }
  /* Checkboxen/Radios dürfen kein Padding bekommen — sonst variable
     Box-Größen → ungleiche Label-Einrückung in der Komorbiditäten-Liste. */
  input[type="checkbox"], input[type="radio"] { padding: 0; width: auto; }
  textarea { min-height: 160px; font-size: 14px; }
  #output-flat { font-size: 11pt; }

  /* Labels in eigener Zeile, volle Breite */
  .row { gap: 6px; }
  .row label { min-width: 0; width: 100%; }
  .row label:has(> input[type="checkbox"]) { width: auto; }

  /* Buttons als komfortable Touch-Ziele, volle Zeilenbreite */
  button { padding: 10px 14px; flex: 1 1 auto; min-height: 40px; }
  button.modal-close { flex: 0 0 auto; min-height: 0; padding: 4px 10px; }

  /* Patient/Komorbidität untereinander */
  .patcomor-grid { flex-direction: column; gap: 16px; }

  /* Komorbiditäten als 1-spaltiges Grid → konsistente, linksbündige
     Einrückung; einzelne Label fluchten exakt untereinander. */
  .comorbidities {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2px 0;
  }
  .comorbidities label.co {
    padding: 6px 4px;
    white-space: normal;
    min-height: 32px;
  }
  .comorbidities label.gfr { gap: 8px; }
  .comorbidities label.gfr input { width: 100px; }

  /* Edit-Tabelle horizontal scrollbar machen, statt zu quetschen */
  .edit-table { display: block; overflow-x: auto; white-space: nowrap; }
  .edit-table thead, .edit-table tbody, .edit-table tr { display: table; width: max-content; min-width: 100%; table-layout: auto; }
  .edit-table input.num { width: 44px; }

  /* BMP-Druckbild auf Mobile horizontal scrollbar (Layout muss exakt bleiben) */
  .bmp-print { overflow-x: auto; padding: 4mm; }
  .bmp-table { font-size: 10pt; }

  /* Modal nutzt volle Höhe/Breite */
  .modal-backdrop { padding: 0; }
  .modal-dialog { width: 100%; height: 100%; border-radius: 0; }

  /* Interaktions-Pillen umbrechen lassen */
  .group li { flex-wrap: wrap; }
  .row-side { width: 100%; justify-content: flex-end; }
}

/* Severity-Erweiterungen */
.interaction.sev-warn { border-color: var(--sev-mittel); }
.interaction.sev-warn strong { background: var(--sev-mittel); }

/* === Print-Preview-Modal === */
/* Verhindert Body-Scroll, wenn das Modal offen ist. */
body.modal-open { overflow: hidden; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}
.modal-backdrop[hidden] { display: none; }

.modal-dialog {
  background: var(--card);
  border-radius: 10px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.35);
  width: min(1100px, 100%);
  height: min(85vh, 100%);
  display: flex;
  flex-direction: column;
  outline: none;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 14px; }

.modal-close {
  background: transparent;
  border: none;
  font-size: 22px;
  line-height: 1;
  padding: 4px 10px;
  cursor: pointer;
  color: var(--muted);
}
.modal-close:hover { color: var(--text); background: #eef1f6; border-radius: 4px; }

.modal-body {
  flex: 1;
  min-height: 0;
  background: #1f2937;     /* dunkler Hintergrund hebt das PDF ab */
  padding: 8px;
}
.modal-body iframe {
  width: 100%;
  height: 100%;
  border: 0;
  background: white;
  border-radius: 4px;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

@media (max-width: 800px) {
  .modal-backdrop { padding: 8px; }
  .modal-dialog { height: 100%; }
}

/* === Druck-CSS gem. KBV Anlage 3 (DIN A4 quer) === */
@media print {
  @page { size: A4 landscape; margin: 8.5mm; }
  body { background: white; font-size: 12pt; font-family: Arial, Helvetica, sans-serif; }
  header, .card:not(.bmp-section), .no-print, .banner, #api-status { display: none !important; }
  main { display: block !important; padding: 0; }
  .bmp-section { border: none; padding: 0; }
  .bmp-section h2 { display: none; }
  .bmp-print { border: none; padding: 0; width: auto; }
  .bmp-side { display: none; }
  textarea { display: none; }
  .dosing-link, .embryotox-link { display: none !important; }
  .modal-backdrop { display: none !important; }
}
