:root {
  --ink: #12151a;
  --body: #343a44;
  --muted: #6c7078;
  --line: #e3e6ea;
  --line-strong: #d2d5db;
  --bg: #f2f3f5;
  --panel: #ffffff;
  --accent: #d8232a;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--body);
  font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px clamp(16px, 3vw, 32px);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; }

.mark {
  display: grid;
  place-items: center;
  width: 34px; height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.02em;
}

.brand strong { display: block; color: var(--ink); font-size: 16px; letter-spacing: -0.01em; }
.brand .sub { display: block; color: var(--muted); font-size: 12.5px; margin-top: -1px; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
  font-weight: 550;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, transform .05s ease;
}
.btn:hover { background: #f7f8f9; border-color: #c3c7ce; }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}
.btn.primary:hover { background: #23272f; border-color: #23272f; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }

/* ---------- Layout ---------- */

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px clamp(16px, 3vw, 32px) 80px;
}

@media (max-width: 1100px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .previewwrap { position: static !important; }
}

.panel { display: grid; gap: 16px; min-width: 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 16px 18px;
}

.card h2 {
  margin: 0 0 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.cardhead { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.cardhead h2 { margin: 0; }

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 12px;
}
.grid .span2 { grid-column: span 2; }

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  font-weight: 550;
  color: var(--muted);
  min-width: 0;
}

input, textarea, select {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 7px 9px;
  min-width: 0;
}
input:focus, textarea:focus {
  outline: 2px solid rgba(216, 35, 42, 0.22);
  outline-offset: 1px;
  border-color: var(--accent);
}
textarea { resize: vertical; }

label.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--body);
  font-weight: 500;
  align-self: end;
  padding-bottom: 8px;
}
label.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ---------- Positionen ---------- */

.items { display: grid; gap: 8px; }

.item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 62px 74px 88px 82px 26px;
  gap: 6px;
  align-items: center;
}
.item input { padding: 6px 8px; font-size: 13.5px; }
.item .it-amount {
  text-align: right;
  font-size: 13px;
  color: var(--body);
  overflow: hidden;
  text-overflow: ellipsis;
}
.item .it-del {
  border: 0;
  background: transparent;
  color: #aeb3bb;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
  padding: 2px 4px;
}
.item .it-del:hover { color: var(--accent); background: #fdf0f0; }

@media (max-width: 640px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .grid .span2 { grid-column: span 1; }
  .item { grid-template-columns: minmax(0, 1fr) 54px 62px 74px 26px; }
  .item .it-amount { display: none; }
}

.privacy { font-size: 12px; color: var(--muted); margin: 2px 4px 0; }

/* ---------- Vorschau (A4) ---------- */

.previewwrap {
  position: sticky;
  top: 84px;
  min-width: 0;
}

.sheet {
  width: 100%;
  max-width: 210mm;
  min-height: 200px;
  margin: 0 auto;
  background: #fff;
  color: var(--ink);
  padding: 16mm 18mm 14mm;
  border-radius: 3px;
  box-shadow: 0 1px 2px rgba(16, 20, 28, .06), 0 12px 32px rgba(16, 20, 28, .10);
  font-size: 12.5px;
  line-height: 1.45;
  overflow-wrap: break-word;
}

.s-head { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; }
.s-company { font-weight: 700; font-size: 15px; letter-spacing: -0.01em; }
.s-small { color: var(--muted); font-size: 11.5px; white-space: pre-line; }
.s-head .s-small { margin-top: 3px; }

.s-meta { text-align: right; flex: 0 0 auto; }
.s-title {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  margin-bottom: 10px;
}
.s-metatable { border-collapse: collapse; margin-left: auto; font-size: 11.5px; }
.s-metatable td { padding: 1px 0 1px 12px; text-align: right; }
.s-metatable td:first-child { color: var(--muted); padding-left: 0; }

.s-client { margin-top: 14mm; }
.s-label {
  font-size: 10px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}
.s-client .strong { font-weight: 700; font-size: 13px; margin-bottom: 2px; }

.s-items { width: 100%; border-collapse: collapse; margin-top: 10mm; }
.s-items th {
  text-align: left;
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #464a52;
  background: #f4f5f7;
  padding: 6px 4px;
  font-weight: 700;
}
.s-items th:first-child { padding-left: 6px; }
.s-items td { padding: 6px 4px; vertical-align: top; border-bottom: 1px solid #f0f1f3; font-size: 12px; }
.s-items td:first-child { padding-left: 6px; }
.s-items .num { text-align: right; white-space: nowrap; }
.s-items .c-unit { color: var(--body); font-size: 11.5px; }
.s-items tr.empty td { color: var(--muted); font-style: italic; border-bottom: 1px solid var(--line); }

.s-totals { margin-top: 6mm; margin-left: auto; width: 62mm; }
.s-totals .row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 12px;
  padding: 3px 0;
  color: var(--body);
}
.s-totals .row.total {
  border-top: 1px solid var(--line-strong);
  margin-top: 4px;
  padding-top: 7px;
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
}

.s-pay { margin-top: 10mm; max-width: 110mm; font-size: 12px; }
.s-pay .s-small { margin-top: 5px; }

.s-foot {
  margin-top: 16mm;
  padding-top: 4mm;
  border-top: 1px solid var(--line);
  color: #8c9098;
  font-size: 10px;
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(16, 20, 28, .25);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 50;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Druck ---------- */

@page { size: A4; margin: 0; }

@media print {
  body { background: #fff; }
  .topbar, .panel, .toast, .privacy { display: none !important; }
  .app { display: block; padding: 0; margin: 0; max-width: none; }
  .previewwrap { position: static; }
  .sheet {
    box-shadow: none;
    border-radius: 0;
    max-width: none;
    width: 210mm;
    min-height: 297mm;
    padding: 16mm 18mm 14mm;
  }
  .s-items tr { break-inside: avoid; }
}
