:root {
    --bg: #f4f5f7; --card: #fff; --text: #1f2430; --muted: #6b7280; --line: #e3e6eb;
    --primary: #2f5bea; --primary-dark: #2448c4; --danger: #c62828;
    --ok-bg: #e6f4ea; --ok: #1e7b34; --warn-bg: #fff4e5; --warn: #a15c00; --err-bg: #fdecec;
    --radius: 8px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 .8rem; }

.topbar { display: flex; align-items: center; justify-content: space-between; padding: .7rem 1.5rem; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; color: var(--text); font-size: 1.1rem; }
.topbar nav { display: flex; gap: 1rem; align-items: center; }
.topbar nav a:not(.btn) { color: var(--muted); }
.topbar nav a.active { color: var(--text); font-weight: 600; }
main { max-width: 1150px; margin: 1.5rem auto; padding: 0 1.5rem; }

.btn { display: inline-block; padding: .45rem .9rem; border: 1px solid var(--line); background: #fff; color: var(--text); border-radius: 6px; cursor: pointer; font: inherit; font-size: .92rem; }
.btn:hover { background: #f0f2f5; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); }
button.link { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
button.link.danger { color: var(--danger); }
button.icon { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1rem; }
button.icon:hover { color: var(--danger); }
form.inline { display: inline; }
button.link.muted { color: var(--muted); }
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { width: 340px; }
.login-box h1 { font-size: 1.3rem; text-align: center; }
.login-box .btn { width: 100%; margin-top: .3rem; }

.flash { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; background: var(--ok-bg); color: var(--ok); }
.flash.error { background: var(--err-bg); color: var(--danger); }
.flash ul { margin: 0; padding-left: 1.2rem; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; margin-bottom: 1.2rem; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.grid.wide-left { grid-template-columns: 2fr 1fr; align-items: start; }
.row { display: flex; gap: .8rem; }
.row > label { flex: 1; }
.row > .w30 { flex: 0 0 30%; }
.hidden { display: none; }

label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .7rem; }
input, select, textarea { display: block; width: 100%; margin-top: .25rem; padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 6px; font: inherit; color: var(--text); background: #fff; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 30%, transparent); border-color: var(--primary); }
textarea { resize: vertical; }

.stats { display: flex; gap: 1rem; margin-bottom: 1rem; }
.stat { flex: 1; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; }
.stat span { display: block; color: var(--muted); font-size: .85rem; }
.stat strong { font-size: 1.3rem; font-variant-numeric: tabular-nums; }

.filter { display: flex; gap: .6rem; margin-bottom: 1rem; }
.filter input, .filter select { margin: 0; }
.filter input { flex: 1; }
.filter select { width: auto; }

table { width: 100%; border-collapse: collapse; }
table.list { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table.list th, table.list td { padding: .6rem .8rem; border-bottom: 1px solid var(--line); text-align: left; }
table.list th { background: #fafbfc; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
table.list tr:last-child td { border-bottom: 0; }
table.list tbody tr:hover { background: #fafbff; }
.r { text-align: right !important; font-variant-numeric: tabular-nums; }
.actions a, .actions form { margin-left: .7rem; }
.empty { background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); padding: 2rem; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: .1rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 600; text-transform: capitalize; }
.badge.offen { background: var(--warn-bg); color: var(--warn); }
.badge.bezahlt { background: var(--ok-bg); color: var(--ok); }
.badge.storniert { background: #eceff3; color: var(--muted); text-decoration: line-through; }
.badge.overdue { background: var(--err-bg); color: var(--danger); }

/* Positionen im Formular */
table.items { counter-reset: pos; margin-bottom: .8rem; }
table.items th { font-size: .8rem; color: var(--muted); text-align: left; padding: .3rem .4rem; font-weight: 500; }
table.items td { padding: .3rem .4rem; vertical-align: top; }
table.items td input, table.items td select, table.items td textarea { margin: 0; }
table.items td.pos { counter-increment: pos; color: var(--muted); padding-top: .75rem; width: 2rem; }
table.items td.pos::before { content: counter(pos); }
table.items .qty { width: 80px; } table.items .unit { width: 75px; } table.items .price { width: 120px; } table.items .tax { width: 80px; }
table.items .line-total { padding-top: .75rem; white-space: nowrap; width: 110px; }

.totals { margin-left: auto; width: 320px; margin-top: 1rem; }
.totals div { display: flex; justify-content: space-between; padding: .25rem 0; font-variant-numeric: tabular-nums; }
.totals .grand { border-top: 2px solid var(--text); margin-top: .3rem; padding-top: .5rem; font-weight: 700; font-size: 1.1rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .6rem; }

/* Rechnungsansicht */
.toolbar { display: flex; align-items: center; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.toolbar form { display: flex; gap: .5rem; }
.toolbar .spacer { flex: 1; }
.paper { background: #fff; width: 210mm; min-height: 297mm; margin: 0 auto; padding: 18mm 20mm 15mm 25mm; box-shadow: 0 2px 12px rgba(0,0,0,.08); display: flex; flex-direction: column; font-size: 10.5pt; }
.paper-head { display: flex; justify-content: space-between; align-items: flex-end; min-height: 40mm; }
.sender-line { font-size: 7.5pt; color: var(--muted); text-decoration: underline; align-self: flex-end; }
.company { text-align: right; font-size: 9pt; color: #444; }
.recipient { margin: 4mm 0 12mm; min-height: 25mm; }
.meta { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 8mm; }
.meta h1 { margin: 0; font-size: 20pt; }
.meta h1 small { font-size: 11pt; color: var(--danger); }
.meta dl { display: grid; grid-template-columns: auto auto; gap: .1rem 1.2rem; margin: 0; font-size: 9.5pt; }
.meta dt { color: var(--muted); } .meta dd { margin: 0; text-align: right; }
.paper-items th { text-align: left; border-bottom: 1.5px solid #222; padding: .35rem .3rem; font-size: 9pt; }
.paper-items td { padding: .45rem .3rem; border-bottom: 1px solid #e5e5e5; vertical-align: top; }
.paper-totals { width: 75mm; }
.notes { margin: 8mm 0 0; }
.paper-foot { margin-top: auto; padding-top: 6mm; border-top: 1px solid #ccc; display: flex; justify-content: space-between; gap: 1rem; font-size: 7.5pt; color: #555; }

@media (max-width: 800px) {
    .grid, .grid.wide-left { grid-template-columns: 1fr; }
    .paper { width: 100%; min-height: 0; padding: 1.2rem; }
}

@media print {
    @page { size: A4; margin: 0; }
    body { background: #fff; }
    .no-print, .toolbar { display: none !important; }
    main { margin: 0; padding: 0; max-width: none; }
    .paper { box-shadow: none; margin: 0; height: 297mm; }
}
