:root {
  --bg: #f3f4f8;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --text: #12283f;
  --muted: #6b7183;
  --line: #e4e6ee;
  --navy: #112c45;
  --navy-deep: #0d2540;
  --teal: #34a68d;
  --green: #38b474;
  --cyan: #22b4b8;
  --accent: #2c9f86;
  --accent-strong: #227f6b;
  --accent-soft: #e2f3ee;
  --on-accent: #ffffff;
  --good: #0f8a5f;
  --good-soft: #e3f6ee;
  --warn: #b45309;
  --warn-soft: #fdf1dc;
  --bad: #c62838;
  --bad-soft: #fde8ea;
  --shadow: 0 1px 2px rgba(20, 22, 31, .05), 0 8px 24px rgba(20, 22, 31, .06);
  --radius: 18px;
  --radius-s: 11px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #191c26;
    --surface-2: #1f2330;
    --text: #eceef5;
    --muted: #9399ad;
    --line: #2b3040;
    --navy: #0b1b2c;
    --navy-deep: #08141f;
    --accent: #4cc5a9;
    --accent-strong: #6ad4bb;
    --accent-soft: #15332d;
    --on-accent: #0f1117;
    --good: #4ad29b;
    --good-soft: #143326;
    --warn: #f0b354;
    --warn-soft: #3a2c12;
    --bad: #ff7a85;
    --bad-soft: #3d1a1f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 28px; line-height: 1.15; letter-spacing: -.02em; font-weight: 700; text-wrap: balance; }
h2 { font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
h3 { font-size: 15px; font-weight: 650; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

.ico { display: inline-flex; width: 20px; height: 20px; flex: none; }
.ico svg { width: 100%; height: 100%; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* Grundgerüst */
#app { display: flex; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; align-self: flex-start;
  width: 236px; height: 100vh; flex: none;
  display: flex; flex-direction: column; gap: 6px;
  padding: 20px 14px;
  background: var(--navy);
  border-right: 1px solid var(--navy-deep);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; font-weight: 700; letter-spacing: -.01em; color: #fff; }
.brand .fair { color: var(--green); }
.brand img { width: 36px; height: 36px; border-radius: 10px; display: block; }
.brand-icon, .tile-icon, .auth-logo {
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--cyan), var(--green));
  color: #fff; border-radius: 10px;
}
.brand-icon { width: 34px; height: 34px; }
.nav-items { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 10px; border-radius: var(--radius-s);
  color: rgba(255, 255, 255, .72); font-weight: 550; font-size: 14.5px;
  border: 0; background: none; cursor: pointer; font-family: inherit; text-align: left;
}
.nav-item:hover { background: rgba(255, 255, 255, .08); color: #fff; text-decoration: none; }
.nav-item.active { background: rgba(255, 255, 255, .12); color: #fff; box-shadow: inset 3px 0 0 var(--green); }

.main { flex: 1; min-width: 0; padding: 32px clamp(16px, 4vw, 44px) 64px; max-width: 1240px; }
.main > * + * { margin-top: 18px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.sub { color: var(--muted); margin-top: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 550; font-size: 14px; }
.back .ico { width: 16px; height: 16px; }

/* Karten und Kennzahlen */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px 22px; }
.card > :not(:first-child) { margin-top: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card-head .muted { font-size: 13.5px; font-weight: 500; }
.hero { padding: 36px; background: linear-gradient(135deg, var(--accent-soft), var(--surface)); }
.hero h2 { font-size: 22px; }
.hero p { max-width: 52ch; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(175px, 1fr)); gap: 14px; }
.kpis.three { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.kpi { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; min-width: 0; }
.kpi-label { color: var(--muted); font-size: 13px; font-weight: 550; }
.kpi-value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi-hint { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.kpi.warn .kpi-value { color: var(--warn); }
.kpi.good .kpi-value { color: var(--good); }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; align-items: start; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.tile {
  display: flex; flex-direction: column; gap: 6px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px; color: inherit; transition: transform .12s ease;
}
.tile:hover { text-decoration: none; transform: translateY(-2px); }
.tile-icon { width: 42px; height: 42px; border-radius: 13px; margin-bottom: 6px; }
.tile-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; font-size: 13px; }

.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px 24px; }
.fact { display: flex; flex-direction: column; min-width: 0; }
.fact span { color: var(--muted); font-size: 12.5px; font-weight: 550; }
.fact strong { font-weight: 550; overflow-wrap: anywhere; white-space: pre-line; }
.fact.full strong { max-width: 90ch; font-weight: 450; }

/* Listen und Tabellen */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 0; border-top: 1px solid var(--line); flex-wrap: wrap; }
.list li:first-child { border-top: 0; padding-top: 2px; }
.list li > div:first-child { flex: 1 1 240px; }
.list li > div:first-child, .stack, .import-text { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; min-width: 0; }
.list li .muted, .stack .muted { font-size: 13px; }
.row-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
td .row-actions { flex-wrap: nowrap; }
.clickable { cursor: pointer; }

.table-wrap { overflow-x: auto; margin-inline: -8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; padding: 6px 8px 10px; white-space: nowrap; }
td { padding: 11px 8px; border-top: 1px solid var(--line); vertical-align: middle; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.clickable:hover td { background: var(--surface-2); }
td select { max-width: 220px; }
.import-text { max-width: 320px; overflow-wrap: anywhere; }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.amount.neg { color: var(--bad); }
.amount.pos { color: var(--good); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.muted { background: var(--surface-2); color: var(--muted); }

.empty { color: var(--muted); padding: 6px 0; }
.bullets { margin: 0; padding-left: 20px; color: var(--muted); font-size: 13.5px; }
.bullets li + li { margin-top: 5px; }

/* Schaltflächen und Formulare */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 40px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn .ico { width: 17px; height: 17px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: var(--on-accent); }
.btn.danger-ghost { color: var(--bad); }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 13px; }
.btn.block { width: 100%; min-height: 46px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { width: 32px; height: 32px; border: 0; border-radius: 50%; background: none; color: var(--muted); cursor: pointer; font-size: 14px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.link { border: 0; background: none; color: var(--accent); font: inherit; font-weight: 550; cursor: pointer; padding: 6px; }

input, select, textarea {
  width: 100%; min-height: 40px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--surface); color: var(--text); font: inherit;
}
textarea { resize: vertical; }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--accent); flex: none; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
input.invalid, select.invalid { border-color: var(--bad); }

.filters { display: flex; gap: 10px; flex-wrap: wrap; }
.filters > * { width: auto; flex: 0 1 220px; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field label { font-size: 13px; font-weight: 600; }
.field small { color: var(--muted); font-size: 12px; }
.full { grid-column: 1 / -1; }
.req { color: var(--bad); }
.form-section { margin-top: 8px; padding-top: 14px; border-top: 1px solid var(--line); }
.form-section:first-child { margin-top: 0; padding-top: 0; border-top: 0; }
.form-note { color: var(--muted); font-size: 13.5px; max-width: 80ch; }
.form-error { margin-top: 12px; padding: 10px 14px; border-radius: var(--radius-s); background: var(--bad-soft); color: var(--bad); font-weight: 550; font-size: 14px; }
.check { display: flex; align-items: center; gap: 10px; font-weight: 550; cursor: pointer; }
.rows { display: flex; flex-direction: column; gap: 8px; }
.row-item { display: flex; gap: 8px; align-items: center; }

.drop {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 22px; border: 2px dashed var(--line); border-radius: var(--radius);
  color: var(--muted); text-align: center;
}
.drop.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }

/* Dialoge und Meldungen */
.modal-wrap { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; background: rgba(15, 17, 23, .5); backdrop-filter: blur(3px); }
.modal { width: min(560px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; background: var(--surface); border-radius: 22px; box-shadow: 0 24px 64px rgba(0, 0, 0, .3); }
.modal.wide { width: min(960px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 22px 10px; }
.modal-head h2 { font-size: 19px; }
.modal-body { padding: 8px 22px 18px; overflow-y: auto; }
.modal-body > * + * { margin-top: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px; border-top: 1px solid var(--line); }

#toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; width: min(480px, calc(100vw - 32px)); pointer-events: none; }
.toast { padding: 11px 18px; border-radius: 14px; background: var(--text); color: var(--bg); font-weight: 550; font-size: 14px; box-shadow: var(--shadow); }
.toast.bad { background: var(--bad); color: var(--on-accent); }

/* Anmeldung */
.auth { flex: 1; display: grid; place-items: center; padding: 24px 16px; background: radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), transparent), var(--bg); }
.auth-card { width: min(420px, 100%); background: var(--surface); border-radius: 26px; box-shadow: var(--shadow); padding: 34px 30px; text-align: center; }
.auth-card > * + * { margin-top: 14px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.auth-brand { display: block; width: min(320px, 92%); height: auto; margin: 0 auto 4px; background: #fff; border-radius: 14px; padding: 6px 12px; }
.auth-card h1 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.recovery { padding: 16px; border-radius: var(--radius-s); background: var(--surface-2); border: 1px dashed var(--line); font: 600 17px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .04em; user-select: all; overflow-wrap: anywhere; }
.auth-card .check { text-align: left; font-size: 14px; }
.fatal { padding: 40px; color: var(--bad); }

/* Abrechnung als Dokument */
.paper { background: #fff; color: #14161f; max-width: 820px; padding: 56px 60px; border-radius: 6px; box-shadow: var(--shadow); font-size: 13.5px; }
.paper > * + * { margin-top: 12px; }
.paper h1 { font-size: 17px; letter-spacing: 0; margin-top: 26px; }
.paper-sender { font-size: 11px; color: #6b7183; border-bottom: 1px solid #c9ccd6; padding-bottom: 3px; display: inline-block; min-height: 18px; }
.paper address { font-style: normal; min-height: 96px; }
.paper-date { text-align: right; }
.paper table { font-size: 12.5px; margin-top: 16px; }
.paper th { color: #14161f; font-size: 11px; border-bottom: 1.5px solid #14161f; padding: 6px 6px; white-space: normal; }
.paper td { border-top: 1px solid #d9dbe3; padding: 6px 6px; }
.paper tfoot td { font-weight: 600; }
.paper tfoot tr:first-child td { border-top: 1.5px solid #14161f; }
.paper tr.total td { font-size: 14px; border-top: 1.5px solid #14161f; border-bottom: 3px double #14161f; }
.paper .small { color: #4a4f60; font-size: 11.5px; }
.paper-sign { margin-top: 44px; }

/* Mietvertrag */
.vertrag h1 { text-align: center; font-size: 19px; margin-bottom: 26px; }
.vertrag-parteien { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; margin: 22px 0; }
.vertrag-rolle { font-weight: 700; margin-bottom: 5px; }
.vertrag-partei div + div { margin-top: 1px; }
.vertrag-par { margin-top: 18px; break-inside: avoid; }
.vertrag-par h2 { font-size: 13.5px; margin-bottom: 5px; break-after: avoid; }
.vertrag-par p { margin-top: 5px; text-align: justify; hyphens: auto; }
.vertrag-par ul { margin: 6px 0 0; padding-left: 20px; }
.vertrag-par li + li { margin-top: 3px; }
.vertrag-unterschrift { display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px 40px; margin-top: 52px; break-inside: avoid; }
.vertrag-ort { margin-bottom: 42px; }
.vertrag-linie { border-top: 1px solid #14161f; margin-bottom: 4px; }
.vertrag .hinweise { margin-top: 44px; padding-top: 16px; border-top: 1px solid #c9ccd6; font-size: 11.5px; color: #4a4f60; break-before: page; }
.vertrag .hinweise h2 { color: #14161f; }

/* Schmale Bildschirme: Navigation wandert nach unten */
@media (max-width: 860px) {
  #app { flex-direction: column; }
  .sidebar {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; z-index: 40;
    width: auto; height: auto; flex-direction: row; gap: 0;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--line);
    overflow-x: auto;
  }
  .brand { display: none; }
  .nav-items { flex-direction: row; flex: none; }
  .nav-item { flex-direction: column; gap: 2px; padding: 6px 10px; font-size: 11px; min-width: 68px; align-items: center; }
  .main { padding: 20px 16px 110px; }
  h1 { font-size: 24px; }
  .kpis, .kpis.three { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-value { font-size: 21px; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .paper { padding: 28px 22px; }
  #toasts { bottom: 96px; }
  .filters > * { flex: 1 1 150px; }
}

@media print {
  @page { size: A4; margin: 18mm 16mm; }
  body { background: #fff; }
  p { orphans: 3; widows: 3; }
  .vertrag-parteien { gap: 20px; }
  .no-print, #overlay, #toasts { display: none !important; }
  #app { display: block; }
  .main { padding: 0; max-width: none; }
  .paper { box-shadow: none; padding: 0; max-width: none; border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* Anmeldung (Konto) und Abgleich */
.auth-wrap { width: min(440px, 100%); display: flex; flex-direction: column; gap: 14px; }
.auth-card .sub strong { color: var(--text); }
.auth-links { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; }
.auth-foot { text-align: center; font-size: 12.5px; color: var(--muted); }
.auth-foot a { color: var(--muted); }
.notice { padding: 10px 14px; border-radius: var(--radius-s); background: var(--warn-soft); color: var(--warn); text-align: left; }
.sync-status { font-size: 12px; color: rgba(255, 255, 255, .6); padding: 6px 10px; min-height: 18px; }
.sync-status::before { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 7px; background: rgba(255, 255, 255, .4); vertical-align: 1px; }
.sync-status.ok::before { background: var(--good); }
.sync-status.laeuft::before { background: var(--cyan); }
.sync-status.offline::before, .sync-status.abgemeldet::before { background: var(--warn); }
.sync-status.fehler::before { background: var(--bad); }
.sync-status.aus { display: none; }
@media (max-width: 860px) { .sync-status { display: none; } }
.legal { max-width: 72ch; margin: 0 auto; padding: 40px 20px 60px; }
.legal h1 { margin-bottom: 20px; }
.legal .legal-logo { display: block; width: min(280px, 70%); height: auto; margin: 0 0 24px; background: #fff; border-radius: 14px; padding: 6px 12px; }
.legal h2 { margin: 26px 0 8px; }
.legal p, .legal li { line-height: 1.55; }
.legal .offen { background: var(--warn-soft); color: var(--warn); padding: 2px 6px; border-radius: 6px; font-weight: 600; }

/* Umschalter Anmelden / Konto anlegen */
.auth-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: var(--radius-s); background: var(--surface-2); margin-bottom: 4px; }
.auth-tabs button { border: 0; background: none; font: inherit; font-weight: 600; font-size: 14px; color: var(--muted); padding: 9px 10px; border-radius: 8px; cursor: pointer; }
.auth-tabs button.active { background: var(--surface); color: var(--navy); box-shadow: var(--shadow); }
@media (prefers-color-scheme: dark) { .auth-tabs button.active { color: var(--text); } }
.auth-links .muted { font-size: 14px; }
.auth-links .muted .link { padding: 0; }
.auth-title { font-size: 19px; text-align: center; }
.notice.ok { background: var(--good-soft); color: var(--good); }
