:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --ink: #18212f;
  --muted: #5b6675;
  --faint: #8b95a3;
  --line: #e3e7ee;
  --accent: #0e6b68;
  --accent-ink: #0a4f4d;
  --overdue: #b45309;
  --overdue-bg: #fbf1e3;
  --paid: #15803d;
  --paid-bg: #e9f5ec;
  --open: #33507a;
  --open-bg: #eef2fa;
  --danger: #b3261e;
  --mono: ui-monospace, "SF Mono", "SFMono-Regular", Menlo, Consolas, monospace;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--ui);
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.45;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 14px; }

/* ---- Topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand h1 { font-size: 15px; margin: 0; letter-spacing: -0.01em; }
.brand .sub { color: var(--faint); font-size: 12px; }
.demo-badge {
  font-size: 11px; color: var(--overdue); background: var(--overdue-bg);
  border: 1px solid #eddcc4; padding: 2px 8px; border-radius: 999px;
}
.kpis { display: flex; gap: 10px; margin-left: auto; }
.kpi {
  border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px;
  background: var(--surface); min-width: 108px;
}
.kpi .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi .value { font-family: var(--mono); font-size: 16px; font-weight: 600; }
.kpi.warn .value { color: var(--overdue); }

.sync-box { display: flex; flex-direction: column; align-items: center; gap: 2px; position: relative; }
.last-sync { font-size: 11px; color: var(--faint); white-space: nowrap; position: absolute; bottom: -18px; }
/* Split-Button: Hauptaktion + Caret-Menue */
.sync-split { display: flex; position: relative; }
.sync-split #syncBtn { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.btn.primary.sync-caret { border-top-left-radius: 0; border-bottom-left-radius: 0; border-left: 1px dotted #ffffff69; padding: 8px 8px; }
.sync-menu { position: absolute; top: calc(100% + 4px); right: 0; z-index: 30; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px rgba(20,30,50,.12); overflow: hidden; min-width: 200px; }
.sync-menu-item { display: block; width: 100%; text-align: left; border: none; background: transparent; padding: 9px 14px; font-size: 13px; color: var(--ink); }
.sync-menu-item:hover { background: #f0f2f5; }
.reload-link { color: var(--accent); text-decoration: none; font-size: 12px; margin-left: 4px; white-space: nowrap; }
.reload-link:hover { text-decoration: underline; }

/* ---- Buttons ---- */
.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 500;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: #f0f2f5; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); }
/* Zahlungserinnerung: hellblau, damit sie sich vom gruenen Buchen-Primary abhebt. */
.btn.mail { background: #eaf3fb; border-color: #b9d6ef; color: #1f5fa6; }
.btn.mail:hover { background: #dcecf8; border-color: #9cc5e8; }
.btn.mail.danger-armed { background: var(--overdue-bg); border-color: var(--overdue); color: var(--overdue); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--accent-ink); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 5px 10px; font-size: 12px; }

/* ---- Toolbar ---- */
.toolbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 22px; flex-wrap: wrap;
}
.tabs { display: flex; gap: 4px; background: #eceff3; padding: 3px; border-radius: 10px; }
.tab {
  border: none; background: transparent; padding: 6px 14px; border-radius: 7px;
  font-size: 13px; color: var(--muted); font-weight: 500;
}
.tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(20,30,50,.08); }
.search {
  flex: 1; min-width: 180px; max-width: 340px;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; background: var(--surface);
}
.search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---- Table ---- */
.table-wrap { padding: 0 22px 40px; }
table { width: 100%; border-collapse: collapse; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
thead th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--muted); font-weight: 600; padding: 11px 14px; border-bottom: 1px solid var(--line);
  background: #fbfcfd;
}
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--ink); background: #f2f5f8; }
/* Sortier-Pfeil aus data-arrow; nur die aktive Spalte zeigt einen. */
thead th.sortable::after { content: attr(data-arrow); font-size: 9px; margin-left: 5px; color: var(--accent); }
thead th.sortable.sorted { color: var(--ink); }
thead th.right.sortable::after { margin-left: 4px; }
tbody td { padding: 12px 14px; border-bottom: 1px solid #eef1f5; vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfc; }
.num { font-family: var(--mono); font-size: 13px; }
.amount { font-family: var(--mono); font-weight: 600; text-align: right; }
.cust { font-weight: 500; }
.cust .email { display: block; color: var(--faint); font-size: 12px; font-weight: 400; }
td.right, th.right { text-align: right; }

/* ---- Status + aging pills ---- */
.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; }
.pill.open { color: var(--open); background: var(--open-bg); }
.pill.paid { color: var(--paid); background: var(--paid-bg); }
.pill.void, .pill.uncollectible { color: var(--muted); background: #eef0f3; }
.aging { font-family: var(--mono); font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 6px; }
.aging.a0 { color: var(--muted); background: #eef0f3; }
.aging.a1 { color: #8a6d1b; background: #f7efd9; }
.aging.a2 { color: var(--overdue); background: var(--overdue-bg); }
.aging.a3 { color: #922b1f; background: #f6e2df; }

.empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }

/* ---- Drawer ---- */
.scrim { position: fixed; inset: 0; background: rgba(18,25,38,.32); opacity: 0; pointer-events: none; transition: opacity .18s; z-index: 40; }
.scrim.show { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100vh; width: 460px; max-width: 94vw;
  background: var(--surface); border-left: 1px solid var(--line); z-index: 50;
  transform: translateX(100%); transition: transform .2s ease; overflow-y: auto;
  box-shadow: -18px 0 40px rgba(20,30,50,.10);
}
.drawer.show { transform: translateX(0); }
.drawer-head { padding: 20px 22px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); }
.drawer-head .num { color: var(--faint); }
.drawer-head h2 { margin: 6px 0 2px; font-size: 18px; }
.drawer-head .meta { color: var(--muted); font-size: 13px; }
.drawer-head .meta.prod { margin-top: 6px; padding: 6px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; color: var(--ink); }
.drawer-head .meta.reminder-badge { margin-top: 6px; display: inline-block; padding: 3px 9px; background: #eaf3fb; border: 1px solid #b9d6ef; border-radius: 999px; color: #1f5fa6; font-weight: 500; font-size: 12px; }
.drawer-head .close { position: absolute; top: 16px; right: 16px; border: none; background: transparent; font-size: 20px; color: var(--faint); }
.drawer section { padding: 18px 22px; border-bottom: 1px solid #eef1f5; }
.drawer section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 12px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; background: var(--surface);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.note { border-left: 2px solid var(--line); padding: 2px 0 2px 12px; margin-bottom: 12px; }
.note .who { font-size: 12px; color: var(--muted); }
.note .who b { color: var(--ink); }
.note .body { white-space: pre-wrap; }
.activity li, .paylog li { list-style: none; }
.activity ul, .paylog ul { margin: 0; padding: 0; }
.activity li, .paylog li { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed #eef1f5; font-size: 13px; }
.activity li .t, .paylog li .t { color: var(--muted); font-family: var(--mono); font-size: 12px; }

/* ---- Modal (CSV) ---- */
.modal { position: fixed; inset: 0; z-index: 60; display: none; align-items: flex-start; justify-content: center; padding: 48px 16px; background: rgba(18,25,38,.34); overflow-y: auto; }
.modal.show { display: flex; }
.modal-card { background: var(--surface); border-radius: 14px; width: 780px; max-width: 100%; box-shadow: 0 24px 60px rgba(20,30,50,.24); }
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px 22px; }
.dropzone { border: 1.5px dashed var(--line); border-radius: 12px; padding: 22px; text-align: center; color: var(--muted); }
.dropzone.hot { border-color: var(--accent); background: #f0f8f7; }
.match-counts { display: flex; gap: 8px; margin: 16px 0 10px; flex-wrap: wrap; }
.chip { font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px; }
.chip.matched { color: var(--paid); background: var(--paid-bg); }
.chip.ambiguous { color: var(--overdue); background: var(--overdue-bg); }
.chip.unmatched { color: var(--muted); background: #eef0f3; }
.chip.applied { color: #fff; background: var(--accent); }
.chip.relevant { color: var(--accent-ink); background: #e3f0ef; }
.filter-chip { cursor: pointer; border: 1px solid transparent; }
.filter-chip.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.match-hint { font-size: 12px; color: var(--muted); margin: 0 0 10px; line-height: 1.4; }
.export-row { display: flex; gap: 8px; margin: 0 0 10px; flex-wrap: wrap; }
.export-row .btn { text-decoration: none; }
.match-table { max-height: 46vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.match-table table { border: none; border-radius: 0; }
.ref-cell { cursor: pointer; }
.ref-cell:hover { color: var(--accent-ink); }
.ref-cell .cp { display: block; color: var(--faint); font-size: 11px; }
.cand-select { max-width: 260px; border: 1px solid var(--line); border-radius: 7px; padding: 5px 8px; font-size: 12px; background: var(--surface); }
.raw-row td { background: #f8fafb; padding: 0; }
.raw-box { padding: 10px 14px; display: grid; grid-template-columns: 1fr; gap: 3px; }
.raw-box > div { display: flex; gap: 8px; font-size: 12px; }
.raw-box .rk { color: var(--muted); min-width: 130px; flex-shrink: 0; }
.raw-box .rv { color: var(--ink); word-break: break-word; }

/* ---- Vergleich Bank ↔ Rechnung ---- */
.cmp-wrap { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px 14px; }
.cmp-col { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: var(--surface); }
.cmp-col.cmp-empty { color: var(--muted); font-size: 12px; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; gap: 4px; }
.cmp-col.cmp-empty span { color: var(--faint); font-size: 11px; }
.cmp-head { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.cmp-kv { display: flex; gap: 8px; font-size: 12px; padding: 2px 0; }
.cmp-kv .k { color: var(--muted); min-width: 56px; flex-shrink: 0; }
.cmp-kv .v { color: var(--ink); word-break: break-word; }
.cmp-kv .v.mono { font-family: var(--mono); font-weight: 600; }
.cmp-ok { color: var(--paid); font-size: 11px; font-weight: 600; }
.cmp-warn { color: var(--overdue); font-size: 11px; font-weight: 600; }
/* Rechnungsnummer = wichtigster Vergleichswert: in beiden Spalten hervorheben. */
.cmp-kv .v.num-big { font-size: 14px; font-weight: 700; color: var(--ink); }
.cmp-col .hl { background: #fff3cd; font-weight: 700; padding: 0 2px; border-radius: 3px; }
.cmp-raw { padding: 0 14px 12px; }
.cmp-raw summary { font-size: 12px; color: var(--muted); cursor: pointer; }
@media (max-width: 640px) { .cmp-wrap { grid-template-columns: 1fr; } }

/* ---- Erinnerung-Vorschau ---- */
.preview-meta { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 12px; }
.preview-meta > div { display: flex; gap: 12px; padding: 9px 14px; border-bottom: 1px solid #eef1f5; font-size: 13px; }
.preview-meta > div:last-child { border-bottom: none; }
.preview-meta .k { color: var(--muted); min-width: 68px; }
.preview-meta .v { color: var(--ink); word-break: break-word; }
.preview-warn { background: var(--overdue-bg); color: var(--overdue); border: 1px solid #eddcc4; border-radius: 8px; padding: 10px 12px; font-size: 12px; margin-bottom: 12px; }
.preview-mail { border: 1px solid var(--line); border-radius: 10px; padding: 18px 20px; background: #fff; max-height: 44vh; overflow-y: auto; }

/* ---- Buchungs-Bestätigung ---- */
.modal.confirm { z-index: 70; align-items: center; padding: 16px; }
.modal-card.slim { width: 420px; }
.modal-foot { padding: 14px 22px 18px; display: flex; justify-content: flex-end; gap: 10px; border-top: 1px solid var(--line); }
.confirm-summary { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 12px; }
.confirm-summary .crow { display: flex; justify-content: space-between; gap: 12px; padding: 9px 14px; border-bottom: 1px solid #eef1f5; }
.confirm-summary .crow:last-child { border-bottom: none; }
.confirm-summary .crow .k { color: var(--muted); font-size: 12px; }
.confirm-summary .crow .v { font-weight: 500; text-align: right; }
.confirm-summary .crow .v.big { font-family: var(--mono); font-weight: 700; font-size: 16px; }
.confirm-hint { color: var(--muted); font-size: 12px; margin: 0; }

/* ---- Dokument-Links im Drawer ---- */
.doc-links { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* ---- Login + Nutzer ---- */
.user-box { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
/* hidden muss display:flex schlagen, sonst bleibt die Login-Karte trotz
   [hidden] sichtbar (Browser-Default display:none hat zu niedrige Prioritaet). */
[hidden] { display: none !important; }
.login-screen { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: flex; align-items: center; justify-content: center; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 40px 48px; text-align: center; box-shadow: 0 12px 40px rgba(20,30,50,.10); }
.login-card h1 { margin: 0 0 2px; font-size: 20px; }
.login-card p { margin: 0 0 20px; color: var(--muted); font-size: 13px; }
.login-card #gbtn { display: flex; justify-content: center; }
.login-card .login-err { color: var(--danger); background: #fdecea; border: 1px solid #f2c4c0; border-radius: 8px; padding: 10px 12px; font-size: 13px; margin-top: 16px; text-align: left; }
.mail-hint { font-size: 12px; color: var(--overdue); background: var(--overdue-bg); border: 1px solid #eddcc4; padding: 2px 8px; border-radius: 999px; text-decoration: none; }

/* ---- Erneut senden (Zwei-Klick-Bestätigung) ---- */
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn.danger-armed { border-color: var(--overdue); color: var(--overdue); background: var(--overdue-bg); }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff; padding: 11px 18px; border-radius: 10px; font-size: 13px; opacity: 0; pointer-events: none; transition: all .2s; z-index: 80; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); }

@media (max-width: 640px) {
  .kpis { display: none; }
  thead th.hide-sm, tbody td.hide-sm { display: none; }
}
