:root {
  --bg: #f3f6f9; --card: #fff; --ink: #14212e; --muted: #5f6f80; --line: #dce3ea;
  --brand: #1b8fa3; --brand-dark: #13727f; --brand-soft: #e1f3f6;
  --ok: #1a7f4b; --ok-soft: #e3f4ea; --warn: #a15c00; --warn-soft: #fdf0d9; --bad: #b3261e; --bad-soft: #fbe6e4;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; color: var(--ink); background: var(--bg); }
h1 { font-size: 24px; margin: 0; }
h2 { font-size: 17px; margin: 0 0 12px; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* Aufbau */
.shell { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: #0f3b45; color: #fff; padding: 22px 14px; display: flex; flex-direction: column; gap: 4px; }
.side .logo { font-weight: 700; font-size: 18px; padding: 0 10px 18px; line-height: 1.2; }
.side .logo small { display: block; font-weight: 400; font-size: 12px; opacity: .65; margin-top: 2px; }
.side a, .side button { color: #dbe6f0; padding: 10px 12px; border-radius: 8px; font: inherit; background: none; border: 0; text-align: left; cursor: pointer; text-decoration: none; }
.side a { display: flex; align-items: center; gap: 10px; }
.side a svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.side a i { font-style: normal; }
.topbar { display: none; }
.side a:hover, .side button:hover { background: rgba(255,255,255,.08); }
.side a.active { background: var(--brand); color: #fff; }
.side .spacer { flex: 1; }
.main { padding: 28px 32px 60px; max-width: 1180px; width: 100%; }
.head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.head .sub { color: var(--muted); margin-top: 2px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; }
.card.flush { padding: 0; overflow: hidden; }
.card.flush > h2, .card.flush > .cardhead { padding: 16px 20px 0; }
.cardhead { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding-bottom: 12px !important; }
.cardhead h2 { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.stat b { display: block; font-size: 24px; margin-top: 2px; }
.empty { padding: 26px 20px; color: var(--muted); text-align: center; }

/* Bedienelemente */
.btn { font: inherit; font-weight: 600; padding: 9px 15px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; white-space: nowrap; }
.btn:hover { background: #f5f8fb; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: inherit; padding: 9px 11px; border: 1px solid #c6d0da; border-radius: 8px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
textarea { min-height: 70px; resize: vertical; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px 14px; }
.grid .full { grid-column: 1 / -1; }
.grid3 { display: grid; grid-template-columns: 110px 1fr; gap: 12px 14px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; color: var(--ink); font-size: 15px; }
.check input { width: auto; }

/* Tabellen */
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 20px; border-bottom: 1px solid var(--line); background: #f8fafc; }
td { padding: 11px 20px; border-bottom: 1px solid #edf1f5; vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tr.link { cursor: pointer; }
tr.link:hover td { background: #f7fafd; }
th.right, td.right { text-align: right; }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tablewrap { overflow-x: auto; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 99px; background: var(--brand-soft); color: var(--brand-dark); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.grey { background: #edf1f5; color: var(--muted); }
.month { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 14px 20px; background: #f8fafc; border-bottom: 1px solid var(--line); border-top: 1px solid var(--line); }
.month:first-child { border-top: 0; }
.month b { font-size: 16px; }

/* Lieferschein-Eingabe */
.items td { padding: 6px 8px; }
.items td:first-child, .items th:first-child { padding-left: 20px; }
.items td:last-child, .items th:last-child { padding-right: 20px; }
.items input { padding: 7px 9px; }
.items .qty { width: 110px; text-align: right; }
.items .price { width: 110px; text-align: right; }
.items .unit { width: 80px; }
.items tr.filled td { background: #f4f9fe; }
.sumbar { display: flex; justify-content: flex-end; gap: 24px; padding: 14px 20px; font-size: 16px; border-top: 1px solid var(--line); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, #0f3b45, #2aa3b7); }
.login form { background: #fff; padding: 32px; border-radius: 14px; width: 100%; max-width: 380px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login h1 { margin-bottom: 4px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login .btn { width: 100%; justify-content: center; margin-top: 14px; }
.error { color: var(--bad); margin-top: 10px; font-size: 14px; min-height: 20px; }

/* Dialog und Hinweis */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(620px, calc(100vw - 32px)); box-shadow: 0 24px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(13,39,64,.5); }
dialog form { padding: 24px; }
dialog h2 { font-size: 19px; margin-bottom: 16px; }
dialog .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #14212e; color: #fff; padding: 11px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); }

/* Dokument (Lieferschein, Rechnung) im Bayrak-Briefbogen */
.paper { --teal: #2aa3b7; background: #fff; width: 210mm; max-width: 100%; min-height: 297mm; margin: 0 auto; padding: 5mm; box-shadow: 0 4px 24px rgba(20,33,46,.12); display: flex; flex-direction: column; font: 11pt/1.3 Arial, Helvetica, sans-serif; color: #111; position: relative; }
.paper .band { background: var(--teal); color: #fff; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.paper .band.top { height: 38mm; display: flex; justify-content: flex-end; align-items: center; padding: 0 9mm; }
.paper .logo { display: grid; justify-items: end; width: 60mm; }
.paper .logoname { font: 400 43pt/.95 "Segoe UI", "Trebuchet MS", Arial, sans-serif; letter-spacing: -.3pt; }
.paper .logosub { font: 700 11pt/1 "Segoe UI", Arial, sans-serif; letter-spacing: .5pt; margin-top: -1.5mm; }
.paper .logo svg { width: 100%; height: 5mm; margin-top: .6mm; }
.paper .rule { height: 1.2mm; background: var(--teal); margin-top: 1mm; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.paper .rule.bottom { margin: 0 0 1mm; }
.paper .sheet { flex: 1; padding: 6mm 26mm 8mm 14mm; }
.paper .sender { font: 700 7pt/1 "Segoe UI", Arial, sans-serif; color: var(--teal); margin: 0 0 9mm 6mm; }
.paper .addr { display: flex; justify-content: space-between; align-items: flex-start; gap: 10mm; min-height: 42mm; }
.paper .to { font-size: 12pt; line-height: 1.3; padding-top: 2mm; }
.paper table { width: auto; }
.paper th, .paper td { background: none; color: #111; text-transform: none; letter-spacing: 0; font-size: inherit; border: 0; padding: 0; }
.paper .infobox { width: 58mm; border-collapse: collapse; font-size: 11pt; }
.paper .infobox th, .paper .infobox td { border: 1px solid #111; padding: .2mm 1.5mm; text-align: left; }
.paper .infobox th { border-width: 1.5px 1.5px 1px; font-weight: 700; }
.paper .infobox td:first-child { width: 30mm; white-space: nowrap; }
.paper p { margin: 2mm 0; }
.paper table.box { width: 100%; border-collapse: collapse; margin-top: 11mm; border: 1.5px solid #111; }
.paper .box th { border: 1px solid #111; border-bottom-width: 1.5px; font-size: 10pt; font-weight: 700; padding: 1mm 2mm; text-align: center; }
.paper .box td { border-left: 1px solid #111; padding: .3mm 1.5mm; vertical-align: top; }
.paper .box td:first-child { border-left: 0; }
.paper .box .cq { width: 26mm; text-align: center; }
.paper .box .cp { width: 30mm; }
.paper .box .cd { width: 20mm; }
.paper .box td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.paper .box tr.gap td { height: 9mm; }
.paper .box .delivery { padding-bottom: 7mm; }
.paper .remark { font-size: 9pt; color: #444; }
.paper .under { display: flex; justify-content: space-between; align-items: flex-start; }
.paper .qsum { width: 26mm; text-align: center; font-size: 8pt; padding-top: 1.5mm; }
.paper .sums { border-collapse: collapse; margin-left: auto; }
.paper .sums td { padding: .5mm 1.5mm; text-align: right; white-space: nowrap; }
.paper .sums td:first-child { padding-right: 3mm; }
.paper .sums td:last-child { width: 30mm; font-variant-numeric: tabular-nums; }
.paper .sums td.line { border: 1px solid #bbb; border-top: 0; height: 6mm; }
.paper .sums tr.vat td { padding-top: 5mm; padding-bottom: 4mm; }
.paper .sums tr.vat span, .paper .sums tr.total span { display: inline-block; min-width: 27mm; text-align: center; border-bottom: 1px solid #111; padding-bottom: .5mm; }
.paper .sums tr.total span { border-bottom: 0; border-top: 1px solid #111; font-weight: 700; padding-top: .5mm; }
.paper .sums tr.total td:last-child { border: 1.5px solid #111; }
.paper .payline { font-weight: 700; font-size: 9.5pt; max-width: 82mm; margin-top: 9mm; }
.paper .received { display: flex; justify-content: space-between; align-items: flex-start; margin-top: 24mm; font-size: 9.5pt; }
.paper .hours { white-space: pre-wrap; }
.paper .recdate { display: flex; gap: 9mm; margin-bottom: 13mm; }
.paper .signline { border-top: 1px solid #111; padding: 1mm 1.5mm 0; min-width: 52mm; }
.paper .band.foot { display: grid; grid-template-columns: 1.15fr 1.35fr 1.2fr; gap: 5mm; padding: 3mm 3mm 3mm 26mm; font: 700 8.5pt/1.3 "Segoe UI", Arial, sans-serif; }
.paper .band.foot table { border-collapse: collapse; align-self: start; }
.paper .band.foot td { color: #fff; font: inherit; padding: 0 5mm 0 0; }
.paper .stamp { position: absolute; top: 120mm; left: 50%; transform: translateX(-50%) rotate(-14deg); font-size: 46pt; font-weight: 800; color: rgba(179,38,30,.22); border: 4px solid rgba(179,38,30,.22); padding: 2mm 8mm; letter-spacing: .05em; pointer-events: none; }

@media (max-width: 860px) {
  body { font-size: 16px; }
  h1 { font-size: 21px; }
  .shell { display: block; }

  /* Kopfzeile oben, Navigation als feste Leiste unten wie in einer App */
  .topbar { display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 30; background: #0f3b45; color: #fff; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; font-size: 17px; }
  .topbar button { font: inherit; font-size: 14px; color: #dbe6f0; background: rgba(255,255,255,.1); border: 0; border-radius: 8px; padding: 7px 12px; }
  .side { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; flex-direction: row; gap: 0; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); box-shadow: 0 -2px 12px rgba(0,0,0,.18); }
  .side .logo, .side .spacer, .side .logout { display: none; }
  .side a { flex: 1 1 0; min-width: 0; flex-direction: column; gap: 3px; padding: 7px 2px 5px; font-size: 10.5px; letter-spacing: -.1px; text-align: center; border-radius: 10px; color: #a9c3ca; }
  .side a svg { width: 23px; height: 23px; }
  .side a span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .side a i { display: none; }
  .side a.active { background: none; color: #fff; }
  .side a.active svg { stroke: #5fd3e6; stroke-width: 2; }
  .main { padding: 16px 12px calc(96px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(84px + env(safe-area-inset-bottom)); width: max-content; }

  /* Seitenkopf: Knöpfe groß und über die ganze Breite */
  .head { align-items: stretch; flex-direction: column; gap: 12px; margin-bottom: 14px; }
  .actions { gap: 8px; }
  .head .actions .btn { flex: 1 1 auto; justify-content: center; padding: 12px 14px; }
  .head .actions .btn.small { padding: 9px 10px; }
  .head .actions input { width: 100% !important; }
  .btn { min-height: 44px; }
  .btn.small { min-height: 36px; }
  input, select, textarea { font-size: 16px; padding: 11px 12px; } /* 16px verhindert das Hineinzoomen am iPhone */
  .grid { grid-template-columns: 1fr; }
  .card { padding: 14px; margin-bottom: 14px; }
  .card.flush > h2, .card.flush > .cardhead { padding: 14px 14px 0; }
  .cardhead { flex-direction: column; align-items: flex-start; gap: 2px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
  .stat { padding: 12px; font-size: 13px; }
  .stat b { font-size: 19px; }
  .month { padding: 12px 14px; flex-direction: column; align-items: stretch; gap: 8px; }
  .month .btn { white-space: normal; text-align: center; justify-content: center; }
  .month .badge { align-self: flex-start; }

  /* Listen: jede Zeile wird zu einer Karte, nichts muss seitlich geschoben werden */
  .tablewrap { overflow: visible; }
  .tablewrap table, .tablewrap tbody { display: block; }
  .tablewrap thead { display: none; }
  .tablewrap tr { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; padding: 13px 14px; border-bottom: 1px solid #edf1f5; }
  .tablewrap tr:last-child { border-bottom: 0; }
  .tablewrap td { padding: 0; border: 0; }
  .tablewrap td:empty { display: none; }
  .tablewrap td.num { margin-left: auto; font-weight: 600; }
  .tablewrap td.right { margin-left: auto; }
  .tablewrap td.num + td.right { margin-left: 0; }
  .tablewrap td .btn { min-height: 40px; }

  /* Lieferschein-Eingabe: eine Karte je Artikel mit großen Feldern */
  .tablewrap table.items tr { display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: "name total" "qty price" "remark remark"; gap: 8px 10px; padding: 14px; align-items: end; }
  .items td:nth-child(1) { grid-area: name; font-size: 16px; align-self: center; }
  .items td:nth-child(2) { grid-area: qty; }
  .items td:nth-child(3) { display: none; }
  .tablewrap table.items tr.free { grid-template-areas: "name name" "qty unit" "price total" "remark remark"; }
  .items tr.free td:nth-child(3) { display: block; grid-area: unit; }
  .items tr.free td:nth-child(6) { align-self: end; padding-bottom: 12px; }
  .items td:nth-child(4) { grid-area: price; }
  .items td:nth-child(5) { grid-area: remark; }
  .items td:nth-child(6) { grid-area: total; text-align: right; align-self: center; }
  .items td:nth-child(7) { position: absolute; right: 14px; margin-top: -4px; }
  .items tr { position: relative; }
  .tablewrap .items td { padding: 0; }
  .items td[data-l="Sonstiges"]::before { display: none !important; }
  .items td[data-l]::before { content: attr(data-l); display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 3px; }
  .items .qty, .items .price, .items .unit { width: 100%; text-align: left; }
  .items input { padding: 11px 12px; }
  .items td:nth-child(1) input { margin-right: 44px; width: calc(100% - 44px); }
  .sumbar { padding: 14px; justify-content: space-between; }

  /* Dialog als Blatt von unten */
  dialog { width: 100vw; max-width: 100vw; max-height: 92dvh; margin: auto 0 0; border-radius: 16px 16px 0 0; overflow-y: auto; }
  dialog form { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  dialog .foot { flex-wrap: wrap; }
  dialog .foot .btn { flex: 1 1 auto; justify-content: center; }
  .login form { padding: 26px 20px; }

  /* Dokument bleibt eine echte A4-Seite und wird als Ganzes verkleinert (fitPaper) */
  .paper { max-width: none; }
}

/* Der Briefbogen füllt die ganze Seite, die Farbbalken reichen bis an den Rand */
@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .side, .head, #toast, dialog, .noprint { display: none !important; }
  .shell { display: block; }
  .main { padding: 0; max-width: none; }
  .paper { box-shadow: none; width: 210mm; min-height: 296mm; margin: 0; zoom: 1 !important; }
  .topbar { display: none !important; }
  .paper .box tr { break-inside: avoid; }
}
