:root {
  --blue: #1d4e89;
  --blue-dark: #163c6a;
  --green: #5fb135;
  --bg: #f2f3f5;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #d5d8de;
  --danger: #b91c1c;
  --radius: 12px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}

/* Kopfzeile */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--blue); color: #fff;
  min-height: 52px;
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; grid-column: 2; }
.topbar #btn-back { grid-column: 1; justify-self: start; color: #fff; }
.save-state { grid-column: 3; justify-self: end; font-size: 12px; opacity: .8; }

.view { padding: 12px; max-width: 640px; margin: 0 auto; }
h2 { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--blue); margin: 0 0 10px; }
.view > h2 { margin: 20px 4px 8px; }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}

/* Formularelemente */
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
label.sr { margin-bottom: 4px; }
label.sr > span, .lbl { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=email], input[type=tel], input[type=time], input[type=date], textarea {
  display: block; width: 100%; margin-top: 4px;
  font: inherit; font-size: 16px; color: var(--text);
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: #fff; -webkit-appearance: none; appearance: none;
  min-height: 44px;
}
textarea { resize: vertical; line-height: 1.35; }
input:focus, textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(29,78,137,.15); }
input:invalid.touched { border-color: var(--danger); }
.req { color: var(--danger); }
.row { display: flex; gap: 10px; }
.row > label { flex: 1; min-width: 0; }
.row > label.w-plz { flex: 0 0 100px; }
.field { margin-bottom: 12px; }

/* Segment-Auswahl */
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { flex: 1; margin: 0; }
.seg input { position: absolute; opacity: 0; width: 0; height: 0; }
.seg span {
  display: flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 6px 8px; font-size: 15px; color: var(--text);
  background: #fff; text-align: center; border-right: 1px solid var(--border);
  -webkit-user-select: none; user-select: none;
}
.seg label:last-child span { border-right: 0; }
.seg input:checked + span { background: var(--blue); color: #fff; font-weight: 600; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 16px; font-weight: 600;
  padding: 12px 16px; border-radius: 10px; border: 0; cursor: pointer;
  text-decoration: none; min-height: 48px; width: 100%;
  -webkit-tap-highlight-color: transparent;
}
.btn.primary { background: var(--blue); color: #fff; }
.btn.primary:active { background: var(--blue-dark); }
.btn.secondary { background: #e8edf5; color: var(--blue); }
.btn.secondary:active { background: #d7e0ee; }
.btn.big { font-size: 17px; min-height: 52px; }
.btn[disabled] { opacity: .5; }
.link-btn {
  font: inherit; font-size: 15px; background: none; border: 0; padding: 8px 4px;
  color: var(--blue); cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.link-btn.strong { font-weight: 600; }
.link-btn.danger { color: var(--danger); }
.file-btn { margin: 0; }

/* Liste */
.cards { list-style: none; margin: 0; padding: 0; }
.cards li {
  background: var(--card); border-radius: var(--radius); margin-bottom: 10px;
  display: flex; align-items: center; box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.cards .open { flex: 1; text-align: left; padding: 14px; background: none; border: 0; font: inherit; color: var(--text); cursor: pointer; }
.cards .open strong { display: block; font-size: 17px; }
.cards .open span { display: block; font-size: 13px; color: var(--muted); margin-top: 2px; }
.cards .badge { font-size: 11px; color: var(--green); font-weight: 600; margin-left: 6px; }
.cards .del { padding: 14px; background: none; border: 0; color: var(--muted); font-size: 20px; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hint {
  background: #fff8e1; border: 1px solid #f5d77a; border-radius: var(--radius);
  padding: 12px; font-size: 14px; margin-bottom: 12px;
}

/* Material / Artikelsuche */
.search-wrap { margin-bottom: 10px; }
input[type=search] {
  display: block; width: 100%; font: inherit; font-size: 16px; color: var(--text);
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff;
  -webkit-appearance: none; appearance: none; min-height: 44px;
}
input[type=search]:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(29,78,137,.15); }
.results {
  list-style: none; margin: 6px 0 0; padding: 0; background: #fff;
  border: 1px solid var(--border); border-radius: 8px; max-height: 320px; overflow-y: auto;
}
.results li {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px;
  padding: 10px 12px; border-bottom: 1px solid #eef0f3; cursor: pointer; font-size: 15px;
}
.results li:last-child { border-bottom: 0; }
.results li:active { background: #e8edf5; }
.results .nr { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.results .src { font-size: 11px; color: var(--green); font-weight: 600; text-transform: uppercase; }
.results .txt { grid-column: 1 / -1; line-height: 1.3; }
.results .none { display: block; color: var(--muted); font-size: 14px; cursor: default; }
.mat-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.mat-row {
  display: grid; grid-template-columns: 60px 54px 1fr 30px; gap: 6px; align-items: start;
  background: #f7f8fa; border-radius: 8px; padding: 6px;
}
.mat-row input { margin-top: 0; min-height: 40px; padding: 8px; font-size: 15px; }
.mat-row .menge { text-align: right; }
.mat-row .desc { display: flex; flex-direction: column; gap: 4px; min-width: 0; padding-top: 2px; }
.mat-row .desc .txt { font-size: 15px; line-height: 1.25; word-break: break-word; }
.mat-row .desc .nr { font-size: 12px; color: var(--muted); }
.mat-row .rm { background: none; border: 0; color: var(--muted); font-size: 22px; line-height: 40px; padding: 0; cursor: pointer; }
#mat-manual { margin-bottom: 8px; }

/* Checkliste */
.checkliste .card.cl-head { border-left: 4px solid var(--green); }
.check-list { display: flex; flex-direction: column; }
.check-row { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid #eef0f3; min-height: 48px; }
.check-row:last-child { border-bottom: 0; }
.check-row > label { flex: 1; display: flex; align-items: center; gap: 12px; margin: 0; padding: 8px 0; font-size: 15px; color: var(--text); cursor: pointer; -webkit-tap-highlight-color: transparent; }
.check-row input[type=checkbox] { position: absolute; opacity: 0; width: 0; height: 0; }
.check-row .box { flex: 0 0 26px; width: 26px; height: 26px; border: 2px solid #b9bec8; border-radius: 6px; background: #fff; position: relative; }
.check-row input:checked + .box { background: var(--blue); border-color: var(--blue); }
.check-row input:checked + .box::after { content: ''; position: absolute; left: 8px; top: 3px; width: 6px; height: 12px; border: solid #fff; border-width: 0 3px 3px 0; transform: rotate(45deg); }
.check-row .lbl { flex: 1; font-size: 15px; color: var(--text); margin: 0; line-height: 1.3; }
.check-row input.typ { flex: 0 0 128px; width: 128px; margin: 0; min-height: 38px; padding: 6px 8px; font-size: 14px; }
.check-row.static { padding: 8px 0; }
.check-row.static .lbl { flex: 1; }
.check-row input.bar { flex: 0 0 64px; width: 64px; margin: 0; min-height: 38px; padding: 6px 8px; font-size: 15px; text-align: right; }
.check-row.static .unit { color: var(--muted); font-size: 14px; }
.checkliste textarea { margin-top: 4px; }
.checkliste label.block { margin-top: 12px; }

/* Fotos */
.photo-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 10px; }
.photo { position: relative; background: #f5f5f5; border-radius: 8px; overflow: hidden; }
.photo img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
.photo input { border-radius: 0; border: 0; border-top: 1px solid var(--border); font-size: 14px; min-height: 36px; padding: 6px 8px; margin: 0; }
.photo .rm {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; border-radius: 50%;
  background: rgba(0,0,0,.6); color: #fff; border: 0; font-size: 18px; line-height: 30px; cursor: pointer;
}

/* Unterschrift */
.sig-block { margin-bottom: 16px; }
.sig-box {
  position: relative; height: 90px; border: 1px dashed var(--border); border-radius: 8px;
  background: #fff; display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.sig-box img { max-height: 100%; max-width: 100%; }
.sig-placeholder { color: #b0b5bf; font-size: 14px; }
.sig-box.has-sig .sig-placeholder { display: none; }
.sig-actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.sig-actions .btn { width: auto; flex: 1; }

/* Aktionsleiste unten */
.spacer { height: 120px; }
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: stretch; gap: 2px;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  background: rgba(242,243,245,.92); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}
.actionbar .btn { max-width: 616px; width: 100%; margin: 0 auto; display: flex; }
.actionbar .link-btn { padding: 4px; }

/* Modal Unterschrift */
.modal {
  position: fixed; inset: 0; z-index: 50; background: #fff;
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--border); font-weight: 600; }
.sig-canvas-wrap { flex: 1; position: relative; background: #fafafa; touch-action: none; overscroll-behavior: contain; }
#sig-canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.sig-line { position: absolute; left: 8%; right: 8%; bottom: 25%; border-bottom: 1px solid #c9ccd3; pointer-events: none; }
.modal-foot { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-top: 1px solid var(--border); }
.modal-foot .btn { width: auto; }

/* Inbetriebnahmeprotokoll */
.checkliste .card.ibn-head { border-left: 4px solid #e08a1e; }
#btn-add-ibn { margin-top: 8px; }
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 14px 0 6px; }
.val-row { display: flex; align-items: center; gap: 8px; min-height: 46px; border-bottom: 1px solid #eef0f3; }
.val-row:last-child { border-bottom: 0; }
.val-row .lbl { flex: 1; margin: 0; font-size: 15px; color: var(--text); }
.val-row input { flex: 0 0 84px; width: 84px; margin: 0; min-height: 38px; padding: 6px 8px; text-align: right; font-size: 15px; }
.val-row .unit { flex: 0 0 30px; color: var(--muted); font-size: 14px; }
.seg.many { display: grid; grid-template-columns: repeat(auto-fit, minmax(105px, 1fr)); }
.seg.many span { border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); font-size: 14px; }

/* Textbausteine */
.modal-body { flex: 1; overflow-y: auto; padding: 12px; background: var(--bg); }
.tb-list { margin-top: 10px; max-height: none; }
.tb-list .grp { display: block; padding: 8px 12px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--blue); background: #f4f6f9; cursor: default; }
.tb-list li[data-i] { display: flex; align-items: flex-start; gap: 10px; line-height: 1.35; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; touch-action: manipulation; }
.tb-list li.sel { background: #e8edf5; }
.tb-list .ord { flex: 0 0 22px; height: 22px; border-radius: 11px; background: var(--blue); color: #fff; font-size: 12px; font-weight: 600; text-align: center; line-height: 22px; margin-top: 1px; }
.tb-hint { margin: 8px 2px 0; }
#tb-ok[disabled] { opacity: .4; }
.tb-open { padding: 6px 0; font-size: 14px; }

/* Warten */
.busy {
  position: fixed; inset: 0; z-index: 60; background: rgba(255,255,255,.85);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
}
.spinner { width: 36px; height: 36px; border: 4px solid #d5d8de; border-top-color: var(--blue); border-radius: 50%; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.update-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(env(safe-area-inset-bottom) + 80px); z-index: 40;
  background: #111; color: #fff; padding: 10px 14px; border-radius: 10px; font-size: 14px;
  display: flex; justify-content: space-between; align-items: center;
}
.update-banner .link-btn { color: #9cd3ff; }

[hidden] { display: none !important; }
