:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1d2330;
  --muted: #5d6576;
  --border: #d7dbe3;
  --accent: #2357c6;
  --accent-text: #ffffff;
  --error-bg: #fdecec;
  --error-text: #a01d1d;
  --credit: #1f7a3d;
  --debit: #b3261e;
  --info-bg: #e8f0fe;
  --warning-text: #a35a00;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}

* { box-sizing: border-box; }

body { margin: 0; background: var(--bg); }

main { max-width: 1200px; margin: 0 auto; padding: 16px; }

h1 { font-size: 1.4rem; margin: 0 0 12px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }

button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.icon { padding: 4px 8px; color: var(--muted); }

input, select {
  font: inherit;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
}
input.num { text-align: right; }
input.invalid { border-color: var(--error-text); background: var(--error-bg); }
input.overridden { background: var(--bg); color: var(--muted); text-decoration: line-through; cursor: not-allowed; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 1;
}
.toolbar select { width: auto; min-width: 260px; max-width: 100%; }
.buttons { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.backup-status { font-size: 0.85rem; color: var(--credit); }
.backup-status.warning { color: var(--warning-text); font-weight: 600; }
.separator { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

.message { max-width: 1200px; margin: 12px auto 0; padding: 10px 16px; border-radius: 6px; background: var(--info-bg); }
.message.error { background: var(--error-bg); color: var(--error-text); }

/* Startfehler erst zeigen, wenn die Seite fertig geladen ist und das Programm trotzdem nicht lief
   (js/startup-check.js) – so blitzt die Meldung auch bei langsamer Verbindung nicht auf. */
#startup-error { display: none; }
#startup-error.visible { display: block; }

.tabs { display: flex; gap: 4px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs [aria-selected="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
}
.empty p { max-width: 60ch; color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field-head { display: flex; align-items: center; gap: 6px; }
.field-head label { font-size: 0.85rem; color: var(--muted); }
.column-head { display: inline-flex; align-items: center; gap: 4px; }

button.help {
  width: 18px;
  height: 18px;
  padding: 0;
  border-radius: 50%;
  font-size: 0.7rem;
  font-weight: 700;
  font-style: italic;
  line-height: 1;
  color: var(--accent);
  border-color: var(--accent);
  flex: none;
}
.help-popover {
  max-width: min(420px, calc(100vw - 32px));
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  line-height: 1.45;
}
.help-popover::backdrop { background: rgba(0, 0, 0, 0.15); }
.app-help { max-width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 48px); overflow-y: auto; padding: 20px 24px; }
.app-help h2 { font-size: 1.2rem; }
.app-help h3 { font-size: 1rem; margin: 16px 0 6px; }
.app-help li { margin-bottom: 6px; }

.hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 8px; }
.error { color: var(--error-text); background: var(--error-bg); padding: 8px 12px; border-radius: 6px; margin: 0; }

.table-scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; font-size: 0.8rem; color: var(--muted); font-weight: 600; padding: 4px 6px; }
td { padding: 4px 6px; vertical-align: top; }
td.num { text-align: right; white-space: nowrap; }

.input-table { margin-bottom: 8px; }
.input-table input { min-width: 90px; }
.input-table tfoot td { border-top: 2px solid var(--border); font-weight: 600; }
.input-table.narrow { max-width: 560px; }
.computed-row td { background: var(--bg); padding: 8px 6px; }
.input-table tfoot td { padding-top: 8px; }

.report td { border-bottom: 1px solid var(--border); }
.report td:last-child { text-align: right; white-space: nowrap; }
.grid-table td:not(:first-child) { text-align: right; white-space: nowrap; }
.grid-table tbody tr:last-child td { font-weight: 600; }
.report small { color: var(--muted); }
.grid-table.plain-last tbody tr:last-child td { font-weight: normal; }
.override-card { border-color: #e0a800; }
/* Überschriften stehen über ihren Werten: Zahlenspalten rechtsbündig wie die Zahlen darunter. */
.report th { vertical-align: bottom; border-bottom: 2px solid var(--border); }
.grid-table th:not(:first-child),
.bill-table th:not(:first-child) { text-align: right; }
.overrides td:last-child { white-space: normal; text-align: left; color: var(--muted); }
.overrides th:last-child { text-align: left; }

.apartment { border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 12px; }
.apartment-header { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin-bottom: 8px; }
.apartment-header .field { width: 200px; }
.checkbox { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.checkbox label { display: flex; gap: 8px; align-items: center; }
.card h3 { font-size: 0.95rem; margin: 16px 0 4px; }
.card .checkbox + .grid { margin-top: 12px; }
.checkbox input { width: auto; }

.totals { display: grid; grid-template-columns: max-content max-content; gap: 4px 24px; margin: 0; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; }

.report-header dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0 0 12px; }
.report-header dt { color: var(--muted); }
.report-header dd { margin: 0; }

.bill { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px; margin-bottom: 12px; }
.bill-title { font-weight: 600; margin: 0 0 4px; }
.bill-address { font-style: normal; display: flex; flex-direction: column; margin-bottom: 24px; max-width: 85mm; }
.bill-address .sender { font-size: 0.75rem; color: var(--muted); border-bottom: 1px solid var(--border); margin-bottom: 6px; padding-bottom: 2px; }
.bill-table .group td { font-weight: 600; padding-top: 12px; border-bottom: none; }
.bill-table tfoot td { border-bottom: none; }
.bill-table tfoot tr:first-child td { border-top: 2px solid var(--text); }
.saldo.credit td { color: var(--credit); }
.bill-actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin: 16px 0 8px; }
.bill-actions .hint { margin: 0; }
.tenant-print { display: none; }
.annex-label { font-weight: 600; margin: 0 0 12px; }
.saldo.debit td { color: var(--debit); }

@media (max-width: 600px) {
  .toolbar select { min-width: 0; width: 100%; }
  .separator { display: none; }
}

@media print {
  :root { font-size: 11pt; }
  body { background: #fff; }
  main { padding: 0; max-width: none; }
  .no-print { display: none !important; }
  .print-section { display: block !important; }
  .card, .bill { border: none; padding: 0; margin-bottom: 16px; }
  .report-section { break-inside: avoid; }
  #bills { break-before: page; }
  .bill { break-inside: avoid; break-after: page; }
  .bill:last-child { break-after: auto; }
  .table-scroll { overflow: visible; }
  .saldo.credit td, .saldo.debit td { color: #000; }
  body[data-print="tenant"] .print-section { display: none !important; }
  body[data-print="tenant"] .tenant-print { display: block; }
  .annex { break-before: page; }
}
