:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1c2530;
  --muted: #5d6b7a;
  --line: #dde3ea;
  --line-strong: #c6d0da;
  --accent: #b3261e;        /* Feuerlöscher-Rot, gedämpft */
  --accent-ink: #ffffff;
  --primary: #1f5fb0;
  --primary-ink: #ffffff;
  --ok: #1a7a44;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(20,30,45,.06), 0 4px 16px rgba(20,30,45,.06);
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.45;
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.logo { font-size: 24px; }
.brand-text { font-weight: 700; font-size: 18px; letter-spacing: .2px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--line-strong); background: #fff; color: var(--ink);
  padding: 8px 14px; border-radius: 8px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: background .12s, border-color .12s, transform .02s;
  white-space: nowrap;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-icon { line-height: 1; }
/* inline-flex wuerde das HTML-Attribut hidden uebersteuern */
.btn[hidden] { display: none; }
.btn:hover { background: #f0f3f7; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: #1a5399; }
.btn-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #9d2019; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: #eef1f5; }

/* Abmelden-Knopf */
.btn-quit { color: var(--danger); border-color: var(--line-strong); }
.btn-quit:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-block { display: block; width: 100%; padding: 11px 14px; font-size: 15px; }

/* Angemeldeter Benutzer in der Topbar – Klapp-Menü mit Konto-Aktionen */
.account { position: relative; padding-left: 6px; }
.account-btn { display: flex; align-items: center; gap: 8px; }
.account-avatar { font-size: 16px; line-height: 1; }
.account-name {
  color: var(--muted); font-weight: 600; white-space: nowrap;
  max-width: 180px; overflow: hidden; text-overflow: ellipsis;
}
.account-caret { color: var(--muted); font-size: 11px; transition: transform .12s; }
.account-btn[aria-expanded="true"] { background: #f0f3f7; }
.account-btn[aria-expanded="true"] .account-caret { transform: rotate(180deg); }

.account-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); overflow: hidden; min-width: 200px;
}
.account-item {
  display: block; width: 100%; text-align: left; border: 0; background: none;
  padding: 10px 14px; font-size: 14px; font-weight: 600; color: var(--ink);
  cursor: pointer; font-family: inherit; white-space: nowrap;
}
.account-item:hover { background: #f0f3f7; }
.account-item-danger { color: var(--danger); border-top: 1px solid var(--line); }
.account-item-danger:hover { background: var(--danger); color: #fff; }

/* ---------- Anmeldeseite ---------- */
.login-body {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px;
}
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.login-brand h1 { margin: 0; font-size: 20px; letter-spacing: .2px; }
.login-hint { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.login-card label {
  display: block; margin: 14px 0 5px; font-size: 13px; font-weight: 600; color: var(--muted);
}
.login-card input {
  width: 100%; padding: 10px 12px; font-size: 15px;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--panel); color: var(--ink);
}
.login-card input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.login-card button { margin-top: 20px; }
.login-error {
  margin-top: 16px; padding: 10px 12px; border-radius: 8px; font-size: 14px;
  background: #fbe6e5; color: #9d2019; border: 1px solid #f0c2bf;
}
.login-foot { margin: 16px 0 0; font-size: 12px; color: var(--danger); text-align: center; }

/* ---------- Dialog (Passwort ändern) ---------- */
.modal {
  position: fixed; inset: 0; z-index: 150;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  background: rgba(20, 30, 45, .45);
}
.modal[hidden] { display: none; }
.modal-box {
  width: 100%; max-width: 380px; background: var(--panel);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px;
}
.modal-box h2 { margin: 0 0 12px; font-size: 18px; }
.modal-box label {
  display: block; margin: 12px 0 5px; font-size: 13px; font-weight: 600; color: var(--muted);
}
.modal-box input {
  width: 100%; padding: 9px 12px; font-size: 15px;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--panel); color: var(--ink);
}
.modal-hint { margin: 12px 0 0; font-size: 13px; color: var(--danger); min-height: 18px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }

/* ---------- Passwortfeld mit Auge (Anmeldung, Passwort ändern) ---------- */
.pw-wrap { position: relative; }
.login-card .pw-wrap input, .modal-box .pw-wrap input { padding-right: 46px; }
.pw-wrap .pw-toggle {
  position: absolute; top: 0; right: 0; bottom: 0; width: 44px; margin: 0; padding: 0;
  display: flex; align-items: center; justify-content: center;
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  border-radius: 0 8px 8px 0;
}
.pw-wrap .pw-toggle:hover { color: var(--ink); }
.pw-wrap .pw-toggle:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
/* Edge-eigenes Auge ausblenden, sonst erscheinen zwei Symbole */
.pw-wrap input::-ms-reveal { display: none; }
.pw-toggle .eye-closed { display: none; }
.pw-toggle[aria-pressed="true"] .eye-open { display: none; }
.pw-toggle[aria-pressed="true"] .eye-closed { display: block; }

/* Export-Menü */
.export { position: relative; }
.export-menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); overflow: hidden; min-width: 260px;
}
.export-menu a {
  display: block; padding: 10px 14px; color: var(--ink); text-decoration: none; font-size: 14px;
}
.export-menu a:hover { background: #f0f3f7; }

/* ---------- Layout ---------- */
main { max-width: 1400px; margin: 20px auto; padding: 0 20px 60px; }

/* ---------- Toolbar / Suche ---------- */
.toolbar { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.search { position: relative; flex: 1; max-width: 560px; }
.search input {
  width: 100%; padding: 11px 38px 11px 14px; font-size: 15px;
  border: 1px solid var(--line-strong); border-radius: 10px; background: #fff;
}
.search input:focus { outline: 2px solid #bcd3ef; border-color: var(--primary); }
#search::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }
.search-clear#search-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  padding: 4px 8px; font-size: 13px;
}
.filter { display: flex; align-items: center; gap: 8px; }
.filter label { color: var(--muted); font-size: 14px; }
.filter input {
  padding: 10px 12px; font-size: 14px; border: 1px solid var(--line-strong);
  border-radius: 10px; background: #fff; color: var(--ink); width: 200px;
}
.filter input:focus { outline: 2px solid #bcd3ef; border-color: var(--primary); }
.filter-label { color: var(--muted); font-size: 14px; }
.multiselect { position: relative; }
.ms-btn {
  appearance: none; font: inherit; font-size: 14px; text-align: left; cursor: pointer;
  padding: 10px 30px 10px 12px; width: 160px; border: 1px solid var(--line-strong);
  border-radius: 10px; background: #fff; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: relative;
}
.ms-btn::after {
  content: "▾"; position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted);
}
.ms-btn:focus { outline: 2px solid #bcd3ef; border-color: var(--primary); }
.ms-btn.ms-active { border-color: var(--primary); font-weight: 600; }
.ms-menu {
  position: absolute; left: 0; top: calc(100% + 6px); z-index: 30; min-width: 100%;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); padding: 4px 0; max-height: 320px; overflow-y: auto;
}
.ms-item {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  font-size: 14px; cursor: pointer; white-space: nowrap;
}
.ms-item:hover { background: #f0f3f7; }
.ms-item input { margin: 0; width: 16px; height: 16px; }
.ms-count { margin-left: auto; padding-left: 12px; color: var(--muted); font-size: 13px; }
.ms-reset {
  display: block; width: 100%; text-align: left; border: 0; border-top: 1px solid var(--line);
  background: none; padding: 9px 14px; margin-top: 4px; font: inherit; font-size: 14px;
  font-weight: 600; color: var(--primary); cursor: pointer;
}
.ms-reset:hover { background: #f0f3f7; }
.count { color: var(--muted); font-size: 14px; margin-left: auto; }

/* ---------- Tabelle ---------- */
.table-wrap {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: auto;
}
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; font-size: 13px; font-weight: 700; color: var(--muted);
  padding: 11px 14px; border-bottom: 1px solid var(--line); background: #fafbfc;
  position: sticky; top: 0; white-space: nowrap; cursor: default;
}
thead th.sortable { cursor: pointer; }
thead th.sortable:hover { color: var(--ink); }
thead th .arrow { color: var(--primary); font-size: 11px; margin-left: 4px; }
tbody td {
  padding: 10px 14px; border-bottom: 1px solid var(--line);
  vertical-align: top; font-size: 14px;
}
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f3f7fc; }
tbody tr:last-child td { border-bottom: none; }
td.col-name { font-weight: 600; }
td.col-geraete { color: var(--muted); max-width: 320px; }
th.col-mehrere_anwesen, td.col-mehrere_anwesen { white-space: nowrap; width: 1%; }
td.col-mehrere_anwesen { color: var(--muted); }
.cell-clip {
  display: block; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* Suchtreffer in der Liste */
mark {
  background: #ffe28a; color: inherit;
  border-radius: 3px; padding: 0 2px; font-weight: 600;
}

/* ---------- Sortieren und Auswahl auf schmalen Geräten ---------- */
/* Die Karten-Ansicht hat keine klickbaren Spaltenköpfe, deshalb gibt es dort
   ein Sortierfeld und ein eigenes „Seite auswählen". Am Rechner unsichtbar. */
.listbar { display: none; }
.sortbar { display: flex; align-items: center; gap: 8px; flex: 1 1 auto; min-width: 0; }
.sortbar label { color: var(--muted); font-size: 14px; }
.sortbar select {
  flex: 1 1 auto; min-width: 0; padding: 10px 12px; font-size: 15px;
  border: 1px solid var(--line-strong); border-radius: 10px;
  background: #fff; color: var(--ink); font-family: inherit;
}
.sort-dir { border: 1px solid var(--line-strong); min-width: 44px; }
.selectall {
  display: flex; align-items: center; gap: 8px;
  font-size: 14px; color: var(--muted); white-space: nowrap;
}
.selectall input { width: 20px; height: 20px; }

/* ---------- Mehrfachauswahl ---------- */
th.col-check, td.col-check {
  width: 1%; text-align: center; cursor: default;
  padding-left: 14px; padding-right: 8px;
}
.col-check input { width: 16px; height: 16px; cursor: pointer; }
.bulkbar {
  display: flex; align-items: center; gap: 12px; margin-bottom: 14px;
  padding: 10px 14px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.bulkbar[hidden] { display: none; }
.bulk-count { font-weight: 600; margin-right: auto; }

/* ---------- Pager ---------- */
.pager { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 16px; }
.pager #pageinfo { color: var(--muted); font-size: 14px; min-width: 160px; text-align: center; }

/* ---------- Formular ---------- */
.edit-head { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }
.edit-head h2 { margin: 0; font-size: 20px; }
#form { display: block; }
.group {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); margin: 14px 0; overflow: hidden;
}
.group > summary {
  list-style: none; cursor: pointer; padding: 13px 18px; font-weight: 700; font-size: 15px;
  background: #fafbfc; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 10px;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::before { content: "▸"; color: var(--primary); font-size: 13px; }
.group[open] > summary::before { content: "▾"; }
.group-body {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px 22px; padding: 18px;
}
.field.full { grid-column: 1 / -1; }
.field label {
  display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin-bottom: 5px;
}
.field label .req { color: var(--accent); margin-left: 3px; }
.field input, .field textarea {
  width: 100%; padding: 9px 11px; font-size: 14.5px; font-family: inherit;
  border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; color: var(--ink);
}
.field input:focus, .field textarea:focus { outline: 2px solid #bcd3ef; border-color: var(--primary); }
.field textarea { resize: vertical; min-height: 60px; }

.form-actions {
  position: sticky; bottom: 0; display: flex; gap: 10px; align-items: center;
  padding: 14px 0 4px; margin-top: 6px;
  background: linear-gradient(to top, var(--bg) 60%, rgba(244,246,249,0));
}
.form-actions .btn-danger { margin-left: auto; }

/* ---------- Protokoll / Log ---------- */
.log-head { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }
.log-head h2 { margin: 0; font-size: 20px; }
.log-hint {
  margin: 0 0 12px; color: var(--danger); font-size: 13.5px; font-weight: 600;
}
.log-hint[hidden] { display: none; }
.log-toolbar { flex-wrap: wrap; }
.log-toolbar .search { flex: 1 1 240px; max-width: 420px; }
.log-toolbar .filter select,
.log-toolbar .filter input[type="date"] {
  padding: 10px 12px; font-size: 14px; border: 1px solid var(--line-strong);
  border-radius: 10px; background: #fff; color: var(--ink);
}
.log-toolbar .filter select:focus,
.log-toolbar .filter input[type="date"]:focus {
  outline: 2px solid #bcd3ef; border-color: var(--primary);
}

#log-table td { font-size: 13.5px; }
.log-col-ts { white-space: nowrap; width: 1%; color: var(--muted); font-variant-numeric: tabular-nums; }
.log-col-action { white-space: nowrap; width: 1%; }
.log-col-obj { white-space: nowrap; }
.log-col-actor { white-space: nowrap; width: 1%; }
.log-col-obj a { color: var(--primary); text-decoration: none; }
.log-col-obj a:hover { text-decoration: underline; }
.log-id { font-weight: 400; font-size: 12px; }

/* Aktions-Abzeichen */
.log-badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 700; line-height: 1.5;
  border: 1px solid transparent; white-space: nowrap;
}
.log-badge-create { background: #e4f4ea; color: #1a7a44; border-color: #b8e0c8; }
.log-badge-update { background: #e6eefb; color: #1a5399; border-color: #c2d6f2; }
.log-badge-delete { background: #fbe6e5; color: #9d2019; border-color: #f0c2bf; }
.log-badge-export { background: #eef1f5; color: #5d6b7a; border-color: #d5dde6; }
.log-badge-login { background: #e4f4ea; color: #1a7a44; border-color: #b8e0c8; }
.log-badge-logout { background: #eef1f5; color: #5d6b7a; border-color: #d5dde6; }
.log-badge-login_failed { background: #fbe6e5; color: #9d2019; border-color: #f0c2bf; }
.log-badge-user { background: #fdf1dd; color: #8a5a12; border-color: #f0dcb6; }

/* Detailspalte */
.log-details { display: block; }
.log-muted { color: var(--muted); font-size: 12.5px; }
.log-changes { margin: 3px 0 0; padding-left: 16px; }
.log-changes li { margin: 1px 0; }
.log-field { color: var(--muted); }
.log-old { color: #9d2019; text-decoration: line-through; }
.log-new { color: #1a7a44; font-weight: 600; }
.log-arrow { color: var(--muted); }
.log-snapshot { display: flex; flex-wrap: wrap; gap: 2px 16px; margin-top: 3px; }
.log-snap-item { font-size: 13px; }

@media (prefers-color-scheme: dark) {
  .log-toolbar .filter select,
  .log-toolbar .filter input[type="date"] { background: #10161d; color: var(--ink); }
  .log-badge-create { background: #16311f; color: #6fd699; border-color: #26512f; }
  .log-badge-update { background: #16263c; color: #7fb1ee; border-color: #274261; }
  .log-badge-delete { background: #341a19; color: #ea8b85; border-color: #542a27; }
  .log-badge-export { background: #23303d; color: var(--muted); border-color: #35485c; }
  .log-badge-login { background: #16311f; color: #6fd699; border-color: #26512f; }
  .log-badge-logout { background: #23303d; color: var(--muted); border-color: #35485c; }
  .log-badge-login_failed { background: #341a19; color: #ea8b85; border-color: #542a27; }
  .log-badge-user { background: #342b16; color: #e0b568; border-color: #55441f; }
  .log-old { color: #ea8b85; }
  .log-new { color: #6fd699; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #212b36; color: #fff; padding: 11px 18px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 14px; z-index: 50; max-width: 90vw;
}
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }

/* ---------- Dark Mode ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10151b; --panel: #171e26; --ink: #e7edf4; --muted: #9aa8b6;
    --line: #263140; --line-strong: #35485c; --primary: #4b90e2; --primary-ink: #08121e;
  }
  .btn { background: #1d2732; color: var(--ink); }
  .btn:hover { background: #23303d; }
  thead th, .group > summary { background: #141b23; }
  tbody tr:hover { background: #1b2a3a; }
  .search input, .field input, .field textarea, .filter input,
  .sortbar select { background: #10161d; color: var(--ink); }
  .export-menu, .account-menu, .ms-menu { background: #171e26; }
  .ms-btn { background: #10161d; color: var(--ink); }
  .ms-item:hover, .ms-reset:hover { background: #23303d; }
  /* Standard-Linkblau ist auf dunklem Grund kaum lesbar (z. B. Mail-Spalte) */
  tbody td a { color: var(--primary); }
  .export-menu a { color: var(--ink); }
  .export-menu a:hover { background: #23303d; }
  .account-item:hover { background: #23303d; }
  .account-btn[aria-expanded="true"] { background: #23303d; }
  .login-card input, .modal-box input { background: #10161d; color: var(--ink); }
  mark { background: #6d5511; color: #ffe28a; }
  .login-error { background: #341a19; color: #ea8b85; border-color: #542a27; }
}

/* ---------- Druckvorschau (Overlay am Bildschirm) ---------- */
#print-view { position: fixed; inset: 0; z-index: 60; background: #52565a; overflow: auto; }
.print-toolbar {
  position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.print-info { margin-right: auto; font-weight: 600; color: var(--ink); }
.print-pages { padding: 24px; display: flex; flex-direction: column; align-items: center; gap: 24px; }

/* Ein "Blatt" = eine A4-Seite mit genau zwei Datensätzen */
.sheet {
  width: 210mm; min-height: 297mm; box-sizing: border-box; padding: 12mm;
  background: #fff; color: #000; box-shadow: 0 3px 14px rgba(0, 0, 0, .45);
  display: flex; flex-direction: column; gap: 8mm;
}
.print-card {
  flex: 1; box-sizing: border-box; border: 1px solid #999; border-radius: 6px;
  padding: 3mm 4mm; overflow: hidden;
}
/* Zweiter Datensatz auf dem Blatt steht auf dem Kopf (um 180° gedreht),
   damit er von der unteren Blattkante aus gelesen wird */
.sheet > .print-card:nth-child(2) { transform: rotate(180deg); }
.print-name {
  margin: 0 0 2.5mm; font-size: 12pt; color: #111;
  border-bottom: 2px solid #333; padding-bottom: 1.5mm;
  display: flex; align-items: baseline; justify-content: space-between; gap: 6mm;
}
.print-faellig {
  font-size: 11pt; font-weight: 700; white-space: nowrap;
  text-align: right; flex: none;
}
.print-group { margin-bottom: 2mm; }
.print-group-title {
  font-size: 8.5pt; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: #666; margin-bottom: .6mm;
}
.print-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .6mm 8mm; }
.print-field { font-size: 8.5pt; line-height: 1.25; display: flex; gap: 5px; break-inside: avoid; }
.pf-label { color: #666; white-space: nowrap; }
.pf-label::after { content: ":"; }
.pf-value { color: #111; font-weight: 600; }
/* Leere Felder behalten ihre Zeilenhöhe, damit das Layout gleich bleibt */
.print-field .pf-value:empty { flex: 1; align-self: flex-start; height: 1.15em; }

/* ---------- Wartebildschirm (z.B. beim Erstellen der Druckvorschau) ---------- */
#wait-screen {
  position: fixed; inset: 0; z-index: 200; background: rgba(20, 30, 45, .45);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#wait-screen[hidden] { display: none; }
.wait-box {
  background: var(--panel); color: var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 22px 28px; min-width: 260px; max-width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center;
}
.wait-spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--primary);
  animation: wait-spin .8s linear infinite;
}
@keyframes wait-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .wait-spinner { animation-duration: 2.4s; } }
.wait-text { font-weight: 600; }
.wait-detail { color: var(--muted); font-size: 14px; min-height: 1.2em; }

/* ---------- Tatsächlicher Druck ---------- */
@page { size: A4 portrait; margin: 12mm; }
@media print {
  .topbar, #app, #toast, #wait-screen { display: none !important; }
  #print-view { position: static; background: #fff; overflow: visible; }
  #print-view[hidden] { display: none; }
  .print-toolbar { display: none !important; }
  .print-pages { display: block; padding: 0; }
  .sheet {
    width: auto; min-height: 0; height: 273mm;  /* 297mm - 2×12mm Seitenrand */
    padding: 0; box-shadow: none; page-break-after: always; break-after: page;
  }
  .sheet:last-child { page-break-after: auto; break-after: auto; }
  .print-card { break-inside: avoid; }
}

/* ==================================================================
   Handy und Tablet
   ------------------------------------------------------------------
   Alle Abfragen gelten nur für den Bildschirm (`screen`), damit das
   Druck-Layout (A4, zwei Datensätze pro Seite) unberührt bleibt.

   Stufen:  <= 1024px  Tablet: schmalere Ränder, Tabelle scrollt
            <=  760px  Handy:  Liste als Karten, Knöpfe nur mit Symbol
            <=  400px  sehr schmal: kürzere Beschriftungsspalte
   Dazu:    (pointer: coarse) größere Tippflächen und 16px in Feldern
            (16px verhindert das Hineinzoomen von iOS beim Tippen)
   ================================================================== */

/* ---------- Tablet ---------- */
@media screen and (max-width: 1024px) {
  main { margin: 16px auto; padding: 0 16px 56px; }
  .topbar { padding: 10px 16px; }
  .filter input { width: 160px; }
}

/* ---------- Tippbedienung (Handy, Tablet) ---------- */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .ms-btn { min-height: 44px; font-size: 16px; }
  .ms-item { padding: 12px 14px; font-size: 16px; }
  .btn-ghost { padding-left: 10px; padding-right: 10px; }
  .search input, .filter input, .field input, .field textarea,
  .sortbar select, .modal-box input, .login-card input,
  .log-toolbar .filter select, .log-toolbar .filter input[type="date"] {
    font-size: 16px;                    /* verhindert iOS-Zoom beim Fokus */
  }
  .col-check input { width: 22px; height: 22px; }
  tbody td { padding: 12px 14px; }
  .group > summary { padding: 15px 18px; }
  .account-item { padding: 13px 16px; }
  .export-menu a { padding: 13px 16px; }
  tbody tr:hover { background: none; }  /* „klebender" Hover nach dem Tippen */
}

/* ---------- Topbar ohne Beschriftungen (Tablet hochkant und schmaler) ----
   Mit vollen Beschriftungen braucht die Knopfreihe rund 830 px; darunter
   bleiben nur die Symbole, sonst schiebt sie die Seite seitlich auf. */
@media screen and (max-width: 900px) {
  .btn-label, .account-name { display: none; }
  .topbar-actions { gap: 6px; }
  .topbar-actions .btn { padding: 8px 12px; min-width: 44px; }
  .topbar-actions .btn-icon { font-size: 17px; }
  .account { padding-left: 0; }
  .account-menu, .export-menu { min-width: 230px; }
}

/* ---------- Tablet quer: Tabelle enger, damit alle Spalten passen ----------
   Mit den Standardabständen ist die Tabelle rund 1230 px breit und muesste
   auf einem 1180-px-Tablet seitlich geschoben werden. */
@media screen and (min-width: 901px) and (max-width: 1250px) {
  /* mit #table auch spezifischer als die Regeln für Tippbedienung */
  #table thead th { padding: 10px 8px; font-size: 12.5px; }
  #table tbody td { padding: 9px 8px; font-size: 13.5px; }
  #table td.col-faelligkeit, #table td.col-plz { white-space: nowrap; }
  #table .cell-clip { max-width: 190px; }
  /* lange E-Mail-Adressen umbrechen statt die Tabelle zu verbreitern */
  #table td.col-mail { max-width: 170px; }
  #table td.col-mail a { overflow-wrap: anywhere; }
  #table th.col-check, #table td.col-check { padding-left: 10px; padding-right: 4px; }
}

/* schmale Fenster zwischen Karten- und Tabellenansicht noch etwas enger */
@media screen and (min-width: 901px) and (max-width: 1023px) {
  #table .cell-clip { max-width: 150px; }
  #table td.col-mail { max-width: 140px; }
}

/* ---------- Liste als Karten (Tablet hochkant, Handy) ----------
   Neun Spalten brauchen rund 1230 px. Schmaler wird aus jeder Zeile eine
   Karte mit Beschriftung vor dem Wert – kein seitliches Schieben mehr.
   Auf Tablets stehen zwei Karten nebeneinander. */
@media screen and (max-width: 900px) {
  /* Sortieren und „Seite auswählen" – Ersatz für die Spaltenköpfe */
  .listbar {
    display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px;
    margin-bottom: 12px;
  }

  /* ---- Liste: Karten statt Tabelle ---- */
  .table-wrap { overflow: visible; }
  #table, #table tbody, #table tr, #table td { display: block; width: 100%; }
  #table thead { display: none; }
  #table tr {
    position: relative; padding: 12px 44px 12px 14px;
    border-bottom: 1px solid var(--line);
    display: flex; flex-direction: column;   /* für die Reihenfolge der Zeilen */
  }
  #table tr:last-child { border-bottom: none; }
  #table td {
    border: none; padding: 1px 0; font-size: 14px;
    display: flex; align-items: baseline; gap: 10px;
  }
  #table td::before {
    content: attr(data-label);
    flex: 0 0 8.5em; color: var(--muted); font-size: 12.5px; line-height: 1.5;
  }
  /* Lange Werte (E-Mail, Straße) umbrechen statt die Karte zu verbreitern */
  #table td > * { min-width: 0; overflow-wrap: anywhere; }
  #table td.is-empty { display: none; }        /* leere Felder weglassen */
  #table td.is-title {
    display: block; font-size: 16.5px; font-weight: 700; margin-bottom: 4px;
    order: -1;                                /* Name steht oben in der Karte */
  }
  #table td.is-title::before { display: none; }
  #table td.col-mehrere_anwesen { width: auto; }
  #table .cell-clip { max-width: none; white-space: normal; }
  #table td.col-geraete { max-width: none; }
  /* Auswahl-Kästchen oben rechts in der Karte */
  #table td.col-check {
    position: absolute; top: 10px; right: 10px; width: auto; padding: 0;
  }
  #table td.col-check::before { display: none; }

  /* ---- Protokoll: ebenfalls Karten ---- */
  #log-table, #log-table tbody, #log-table tr, #log-table td { display: block; width: 100%; }
  #log-table thead { display: none; }
  #log-table tr { padding: 12px 14px; border-bottom: 1px solid var(--line); }
  #log-table tr:last-child { border-bottom: none; }
  #log-table td {
    border: none; padding: 1px 0; white-space: normal; width: auto;
    display: flex; align-items: baseline; gap: 10px;
  }
  #log-table td::before {
    content: attr(data-label);
    flex: 0 0 7em; color: var(--muted); font-size: 12.5px;
  }
  #log-table td > * { min-width: 0; overflow-wrap: anywhere; }
  #log-table td.log-col-details { display: block; margin-top: 6px; }
  #log-table td.log-col-details::before { display: none; }
  .log-snapshot { gap: 2px 10px; }

}

/* zwei Karten nebeneinander, sobald die Breite dafür reicht */
@media screen and (min-width: 601px) and (max-width: 900px) {
  #table tbody { display: grid; grid-template-columns: 1fr 1fr; }
  #table tbody tr:nth-child(odd) { border-right: 1px solid var(--line); }
  #table tbody tr:nth-last-child(2):nth-child(odd) { border-bottom: none; }
}

/* ---------- Handy ---------- */
@media screen and (max-width: 760px) {
  /* unten Platz für die feststehende Knopfleiste des Formulars */
  main { margin: 12px auto; padding: 0 12px 120px; }

  /* Topbar: Rand am Display-Ausschnitt (Notch) aussparen */
  .topbar {
    gap: 8px;
    padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  }
  .brand-text { display: none; }
  .logo { font-size: 26px; }

  /* In der Topbar bleibt nur der Benutzer-Knopf (mit „Abmelden" im Menü).
     Neuer Kunde, Protokoll und CSV-Export sind hier ausgeblendet. */
  #btn-new, #btn-log, .export { display: none; }
  /* Dort steht der Benutzername statt des Symbols. */
  .account-avatar { display: none; }
  .account-name { display: inline; max-width: 46vw; }
  /* Im Kontomenü bleibt am Handy nur „Abmelden" – ohne Trennlinie darüber. */
  #btn-password { display: none; }
  .account-item-danger { border-top: none; }

  /* Suche und Filter untereinander, jeweils über die ganze Breite */
  .toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
  /* In der gestapelten Leiste wirkt flex-basis als Höhe – deshalb zurücksetzen
     (sonst wird das Suchfeld im Protokoll 240px hoch). */
  .toolbar .search, .log-toolbar .search { flex: 0 0 auto; max-width: none; }
  .log-toolbar #log-reset { border-color: var(--line-strong); }
  .filter { justify-content: space-between; }
  .filter input, .log-toolbar .filter select,
  .log-toolbar .filter input[type="date"] { flex: 1 1 auto; width: auto; min-width: 0; }
  .multiselect { flex: 1 1 auto; min-width: 0; }
  .ms-btn { width: 100%; }
  .count { margin-left: 0; }
  /* Kundenzahl auf dem Handy weglassen – sie kostet dort eine eigene Zeile.
     Die Zahl der Protokolleinträge (#log-count) bleibt sichtbar. */
  #count { display: none; }
  .log-toolbar #log-reset { align-self: flex-start; }

  /* Auswahl-Leiste: Zahl in eigener Zeile, Knöpfe teilen sich den Platz */
  .bulkbar { flex-wrap: wrap; gap: 8px; }
  .bulk-count { flex: 1 1 100%; margin-right: 0; }
  .bulkbar .btn { flex: 1 1 auto; }

  /* ---- Blättern, Formular, Kopfzeilen ---- */
  .pager { gap: 10px; }
  .pager .btn { flex: 1 1 auto; }
  .pager #pageinfo { min-width: 0; flex: 1 1 auto; font-size: 13.5px; }

  .edit-head, .log-head { flex-wrap: wrap; gap: 8px 12px; }
  .edit-head h2, .log-head h2 { font-size: 18px; }
  .group-body { grid-template-columns: 1fr; gap: 12px; padding: 14px; }

  .form-actions {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
    padding: 12px 0 max(6px, env(safe-area-inset-bottom));
  }
  .form-actions .btn { width: 100%; }
  .form-actions .btn-danger { margin-left: 0; }

  /* Dialoge und Meldungen */
  .modal { padding: 16px; }
  .modal-box { padding: 18px; }
  .modal-actions .btn { flex: 1 1 auto; }
  .toast { bottom: calc(18px + env(safe-area-inset-bottom)); max-width: calc(100vw - 24px); }

  /* Druckvorschau: Werkzeugleiste umbrechen lassen */
  .print-toolbar { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .print-info { flex: 1 1 100%; margin-right: 0; font-size: 14px; }
  .print-toolbar .btn { flex: 1 1 auto; }
  .print-pages { padding: 12px; }
}

/* ---------- sehr schmale Geräte ---------- */
@media screen and (max-width: 400px) {
  main { padding: 0 10px 120px; }
  #table td::before { flex-basis: 7em; font-size: 12px; }
  #log-table td::before { flex-basis: 6em; }
  #table tr { padding-left: 12px; }
  .selectall span { font-size: 13px; }
}

/* ---------- Dialoge in niedrigen Fenstern (Handy quer) ---------- */
@media screen and (max-height: 560px) {
  .modal { align-items: flex-start; }
  .modal-box { max-height: calc(100vh - 32px); overflow: auto; }
  .login-body { align-items: flex-start; }
}

/* ---------- Druckvorschau am kleinen Bildschirm ----------
   Das Blatt ist 210 mm breit (~794 px) und passt nicht auf ein Handy.
   `zoom` verkleinert die Vorschau inklusive Layout; beim PDF-Erzeugen
   setzt app.js die Klasse `pdf-export` und hebt die Verkleinerung auf,
   damit die Blätter in Originalgröße in die Datei kommen. */
#print-view { -webkit-overflow-scrolling: touch; }
@media screen and (max-width: 900px) {
  .print-pages { zoom: .62; }
}
@media screen and (max-width: 500px) {
  .print-pages { zoom: .44; }
}
body.pdf-export .print-pages { zoom: 1; }
