:root {
  --bg: #f5f6f8; --surface: #fff; --text: #1d1d1f; --muted: #66666b; --line: #e1e3e8;
  --accent: #1f4e79; --accent-soft: #e8eff6;
  --danger: #b42318; --danger-soft: #fdecea; --warn: #8a5a00; --warn-soft: #fff5e0; --ok: #1e7a3c; --ok-soft: #e7f5ec;
  --radius: 8px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; font-size: 14px; color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); display: flex; min-height: 100vh; }
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 600; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 12px; margin: 18px 0 8px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); } .small { font-size: 12px; } .right { text-align: right; } .nowrap { white-space: nowrap; }
.mono { font-family: Consolas, "Cascadia Mono", monospace; font-size: 12px; word-break: break-all; }

/* Navigation */
.topbar { display: none; }
.sidebar { width: 230px; flex: none; background: #14263a; color: #dfe7f0; padding: 18px 12px; display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar[hidden] { display: none; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; padding: 0 8px; color: #fff; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: linear-gradient(135deg, #4f8cc9, #1f4e79); flex: none; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a { color: #c9d5e2; text-decoration: none; padding: 9px 10px; border-radius: 6px; }
.sidebar nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar nav a.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
.sidebar-new { display: flex; flex-direction: column; gap: 6px; margin-top: auto; }
.sidebar .btn:not(.primary) { background: transparent; color: #dfe7f0; border-color: rgba(255,255,255,.25); }
.sidebar .btn:not(.primary):hover { background: rgba(255,255,255,.08); }
.userbox { border-top: 1px solid rgba(255,255,255,.12); padding: 12px 8px 0; font-size: 12px; color: #aebdcc; }
.userbox strong { color: #fff; display: block; font-size: 13px; }
.userbox button { background: none; border: none; color: #c9d5e2; padding: 4px 0; margin-right: 12px; cursor: pointer; font: inherit; text-decoration: underline; }

main { flex: 1; min-width: 0; padding: 24px 28px 60px; outline: none; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Bausteine */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.kpi .card { padding: 14px 16px; margin: 0; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 22px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.btn { font: inherit; border: 1px solid #d0d4db; background: var(--surface); color: var(--text); padding: 8px 12px; border-radius: 6px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; line-height: 1.2; white-space: nowrap; min-height: 36px; }
.btn:hover { background: #f0f2f5; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #183f63; }
.btn.danger { color: var(--danger); border-color: #f2c4bf; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.block { width: 100%; }
.btn.small { padding: 4px 8px; font-size: 12px; min-height: 28px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: none; background: none; cursor: pointer; color: var(--muted); font-size: 20px; padding: 2px 8px; border-radius: 4px; line-height: 1; }
.icon-btn:hover { color: var(--danger); background: var(--danger-soft); }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); border: 1px solid #c9ced6; border-radius: 6px; padding: 8px 9px; background: #fff; width: 100%; min-height: 36px; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc0e4; outline-offset: 0; border-color: var(--accent); }
input[type=checkbox] { width: 18px; min-height: 18px; height: 18px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.list td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #f7f9fc; }
table.list .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: #eef0f3; color: var(--muted); white-space: nowrap; }
.badge.offen, .badge.teilbezahlt { background: var(--warn-soft); color: var(--warn); }
.badge.bezahlt { background: var(--ok-soft); color: var(--ok); }
.badge.storniert, .badge.Storno, .badge.teilstorniert { background: var(--danger-soft); color: var(--danger); }
.badge.Entwurf { background: var(--accent-soft); color: var(--accent); }
.badge.alt { background: #f3eefa; color: #5b3e8a; }
.msg { border-radius: 6px; padding: 10px 12px; font-size: 13px; white-space: pre-line; }
.msg + .msg { margin-top: 8px; }
.msg ul { margin: 4px 0 0; padding-left: 18px; white-space: normal; }
.msg.error { background: var(--danger-soft); color: var(--danger); }
.msg.warn { background: var(--warn-soft); color: var(--warn); }
.msg.ok { background: var(--ok-soft); color: var(--ok); }
.msg.info { background: var(--accent-soft); color: var(--accent); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters > * { width: auto; min-width: 140px; flex: 1 1 140px; max-width: 260px; }
.empty { text-align: center; padding: 40px 10px; color: var(--muted); }

/* Editor */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.editor aside { position: sticky; top: 16px; display: flex; flex-direction: column; gap: 16px; }
.editor aside .card + .card { margin-top: 0; }
.items-head, .item { display: grid; grid-template-columns: 24px 80px 92px 104px 70px minmax(130px, 1fr) 96px 28px; gap: 6px 8px; }
.items-head { font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 0 6px; border-bottom: 1px solid var(--line); }
.item { padding: 10px 0; border-bottom: 1px solid var(--line); align-items: start; }
.item .pos { color: var(--muted); padding-top: 9px; grid-row: 1 / 3; }
.item .desc { grid-column: 2 / 8; }
.item .rm { grid-column: 8; }
.item .qty { grid-column: 2; }
.item .total { text-align: right; padding-top: 9px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item input.num { text-align: right; }
.totals { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.totals td { padding: 4px 0; }
.totals td:last-child { text-align: right; white-space: nowrap; }
.totals tr.grand td { border-top: 2px solid var(--text); padding-top: 8px; font-weight: 700; font-size: 16px; }
.seg { display: inline-flex; border: 1px solid #c9ced6; border-radius: 6px; overflow: hidden; }
.seg button { border: none; background: #fff; padding: 7px 11px; font: inherit; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--accent); color: #fff; }
.savestate { font-size: 12px; color: var(--muted); }

/* Belegansicht */
.doc-view { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 16px; align-items: start; }
.pdf-frame { width: 100%; height: calc(100vh - 150px); min-height: 560px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
dl.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.cancel-lines td { vertical-align: middle; }
.cancel-lines input.num { width: 90px; text-align: right; }

/* Anmeldung */
.login-wrap { min-height: 100vh; width: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login { width: min(400px, 100%); }
.login .brand { color: var(--text); justify-content: center; margin-bottom: 16px; font-size: 20px; }

/* Modal & Toast */
.modal { position: fixed; inset: 0; background: rgba(16,24,40,.45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 50; }
.modal[hidden] { display: none; }
.modal .dialog { background: var(--surface); border-radius: 10px; width: min(760px, 100%); padding: 22px; box-shadow: 0 20px 40px rgba(0,0,0,.2); }
.modal .dialog.narrow { width: min(480px, 100%); }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #14263a; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; white-space: pre-line; max-width: 92vw; z-index: 60; }
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); }
.progress { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; margin: 8px 0; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.logo-preview { max-height: 60px; max-width: 220px; border: 1px solid var(--line); border-radius: 6px; padding: 4px; background: #fff; }

@media (max-width: 1150px) {
  .editor, .doc-view { grid-template-columns: 1fr; }
  .editor aside { position: static; }
  .pdf-frame { height: 70vh; min-height: 420px; }
}
/* Handy & Tablet */
@media (max-width: 860px) {
  body { flex-direction: column; }
  .topbar { display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 40; background: #14263a; color: #fff; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
  .topbar .brand { color: #fff; padding: 0; }
  .menu-btn { background: none; border: none; color: #fff; font-size: 24px; padding: 4px 8px; cursor: pointer; }
  .sidebar { position: fixed; inset: 0 30% 0 0; height: 100%; z-index: 45; width: auto; max-width: 300px; box-shadow: 0 0 40px rgba(0,0,0,.4); transform: translateX(-105%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .sidebar > .brand { display: none; }
  main { padding: 16px 14px 80px; }
  h1 { font-size: 19px; }
  .items-head { display: none; }
  .item { grid-template-columns: 22px 1fr 1fr 34px; }
  .item .desc { grid-column: 2 / 4; }
  .item .rm { grid-column: 4; grid-row: 1; }
  .item .qty, .item .price, .item .tax { grid-column: 2; }
  .item .unit, .item .disc { grid-column: 3; }
  .item .total { grid-column: 3; }
  .item .tax { grid-column: 2 / 4; }
  table.list.responsive thead { display: none; }
  table.list.responsive tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
  table.list.responsive td { border: none; padding: 0; }
  table.list.responsive td.hide-m { display: none; }
  .filters > * { max-width: none; }
}
@media print { .sidebar, .topbar, .actions { display: none; } }
.badge.angenommen { background: var(--ok-soft); color: var(--ok); }
.badge.abgelehnt, .badge.abgelaufen { background: var(--danger-soft); color: var(--danger); }
.badge.Mahnung { background: var(--warn-soft); color: var(--warn); }
.item .tax.inherit { display: flex; align-items: center; font-size: 12px; color: var(--muted); background: #f3f5f8; border: 1px dashed #c9ced6; border-radius: 6px; padding: 0 9px; min-height: 36px; }

/* Kundensuche */
.combo { position: relative; flex: 1; min-width: 0; }
.combo-list { position: absolute; z-index: 30; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(16,24,40,.14); max-height: 320px; overflow-y: auto; }
.combo-item { padding: 9px 11px; cursor: pointer; border-bottom: 1px solid var(--line); line-height: 1.35; }
.combo-item:last-child { border-bottom: 0; }
.combo-item.on, .combo-item:hover { background: var(--accent-soft); }
.combo-head { padding: 7px 11px; font-size: 12px; color: var(--muted); }
