:root {
  --bg: #f5f3ef; --card: #fff; --ink: #1d1b19; --muted: #6f6a63; --line: #e3ded6;
  --gold: #b0892e; --gold-dark: #8d6c1f; --ok: #1f7a4d; --ok-bg: #e7f4ec;
  --warn: #8a5a00; --warn-bg: #fff4dc; --err: #b3261e; --err-bg: #fdecea; --info: #2b5b8a; --info-bg: #e9f1f9;
  --r: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
h2 { font-size: 1rem; margin: 1.4rem 0 .7rem; }
h2:first-child { margin-top: 0; }
.muted { color: var(--muted); }
small { font-size: .8rem; }
code { font-size: .8rem; background: #f1eee9; padding: 1px 4px; border-radius: 4px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 1.2rem; }
.brand { display: flex; gap: .6rem; align-items: center; }
.brand small { display: block; color: var(--muted); }
.logo { width: 34px; height: 34px; border-radius: 8px; background: var(--ink); color: var(--gold); display: grid; place-items: center; font-weight: 700; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: min(360px, 100%); display: grid; gap: .9rem; }
.error-text { color: var(--err); margin: 0; min-height: 1.2em; }

/* Kopfzeile */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--ink); color: #fff; display: flex; flex-wrap: wrap; gap: .6rem 1.2rem; align-items: center; padding: .6rem 1.2rem; }
.topbar .brand small { color: #b9b2a8; }
.topbar .logo { background: var(--gold); color: var(--ink); }
nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
nav button { background: transparent; color: #d9d3ca; border: 0; padding: .5rem .8rem; border-radius: 8px; font: inherit; cursor: pointer; }
nav button.active, nav button:hover { background: #ffffff1a; color: #fff; }
.user { display: flex; gap: .7rem; align-items: center; color: #d9d3ca; }
.user .link { color: var(--gold); }

main { max-width: 1320px; margin: 0 auto; padding: 1.2rem; }

/* Formulare */
label { display: grid; gap: .25rem; font-size: .82rem; color: var(--muted); }
label.inline { display: inline-flex; gap: .3rem; align-items: center; margin-left: .6rem; color: var(--ink); font-size: .9rem; }
input, select { font: inherit; color: var(--ink); background: #fff; border: 1px solid #cfc8bd; border-radius: 8px; padding: .55rem .65rem; min-height: 42px; width: 100%; }
input[type=radio] { width: auto; min-height: 0; }
input:focus, select:focus { outline: 2px solid var(--gold); outline-offset: 0; border-color: var(--gold); }
input:invalid:not(:placeholder-shown) { border-color: var(--err); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .8rem; }
.grid .wide { grid-column: span 2; }
.grid .full { grid-column: 1 / -1; }
.pricemode { margin-bottom: .6rem; font-size: .9rem; }

button { font: inherit; cursor: pointer; }
.primary, .secondary, .btn { display: inline-flex; align-items: center; justify-content: center; border-radius: 8px; padding: .6rem 1rem; min-height: 42px; border: 1px solid transparent; text-decoration: none; font-weight: 600; }
.primary { background: var(--gold); color: #fff; }
.primary:hover { background: var(--gold-dark); }
.primary:disabled { background: #cfc6b5; cursor: not-allowed; }
.secondary { background: #fff; color: var(--ink); border-color: #cfc8bd; }
.small { min-height: 34px; padding: .35rem .75rem; font-size: .85rem; }
.big { width: 100%; min-height: 50px; font-size: 1rem; margin-top: 1rem; }
.link { background: none; border: 0; color: var(--gold-dark); padding: 0; text-decoration: underline; }
.link.danger { color: var(--err); text-decoration: none; font-size: 1.1rem; padding: .3rem; }
.editor-side .link { display: block; margin: .8rem auto 0; }

/* Upload */
.drop { border: 2px dashed #cbbf9f; border-radius: var(--r); background: #fffdf8; padding: 3rem 1rem; text-align: center; }
.drop.over { background: #fbf2dc; border-color: var(--gold); }

/* Editor */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 1.2rem; align-items: start; }
.editor-side { display: grid; gap: 1rem; position: sticky; top: 76px; }
@media (max-width: 1000px) { .editor { grid-template-columns: 1fr; } .editor-side { position: static; } }
.tag { font-size: .75rem; font-weight: 500; background: #f1eadb; color: var(--gold-dark); padding: 2px 8px; border-radius: 999px; margin-left: .4rem; }

.lines-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: .75rem; color: var(--muted); font-weight: 600; padding: .4rem; border-bottom: 1px solid var(--line); }
td { padding: .4rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.lines td.desc { min-width: 260px; }
.lines input.sub { margin-top: .3rem; font-size: .85rem; min-height: 36px; }
.lines input.n { width: 90px; text-align: right; }
.lines select { width: auto; }
.num { text-align: right; white-space: nowrap; }
.lines .sum { padding-top: .9rem; }
#addLine { margin-top: .6rem; }
#exemptionBox { margin-top: 1rem; }
@media (max-width: 900px) { .lines td.desc { min-width: 220px; } .lines input.n { width: 76px; } }

.totals dl { display: grid; grid-template-columns: 1fr auto; gap: .35rem 1rem; margin: 0; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.totals .strong { font-weight: 700; color: var(--ink); border-top: 1px solid var(--line); padding-top: .35rem; }

.hints { display: grid; gap: .4rem; margin-bottom: 1rem; }
.hint { padding: .6rem .8rem; border-radius: 8px; font-size: .9rem; border-left: 4px solid; }
.hint.ok { background: var(--ok-bg); border-color: var(--ok); color: var(--ok); }
.hint.warn { background: var(--warn-bg); border-color: #e0a100; color: var(--warn); }
.hint.error { background: var(--err-bg); border-color: var(--err); color: var(--err); }
.hint.info { background: var(--info-bg); border-color: var(--info); color: var(--info); }
.msgs { padding-left: 1.1rem; font-size: .85rem; }
.msgs li.error { color: var(--err); }
.msgs li.warning { color: var(--warn); }
.downloads { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .8rem; }
.list .downloads { margin: 0; }

.pdfbox iframe { width: 100%; height: 520px; border: 1px solid var(--line); border-radius: 8px; background: #eee; }
.table-wrap { overflow-x: auto; }
.badge { font-size: .78rem; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.error { background: var(--err-bg); color: var(--err); }
.badge.info { background: var(--info-bg); color: var(--info); }

#settingsForm .grid { margin-bottom: 1rem; }

/* Fehlerhafte Felder */
input.invalid, select.invalid { border: 2px solid var(--err); background: #fff6f5; }
input.invalid:focus, select.invalid:focus { outline-color: var(--err); }
.field-error { display: block; color: var(--err); font-size: .8rem; font-weight: 600; line-height: 1.3; }
.field-error::before { content: "⚠ "; }
td .field-error { margin-bottom: .2rem; white-space: normal; max-width: 160px; }
td.desc .field-error { max-width: none; }
@keyframes flash { 0%, 100% { box-shadow: 0 0 0 0 transparent; } 30% { box-shadow: 0 0 0 6px #b3261e55; } }
.flash { animation: flash .9s ease 2; }
.hint ul.jump { margin: .4rem 0 0; padding-left: 1.1rem; }
.hint ul.jump .link { color: var(--err); text-align: left; }
.primary.blocked { background: #c9bfae; }
.primary.blocked:hover { background: #b9ae9b; }
.gen-status { color: var(--err); font-size: .85rem; text-align: center; margin: .5rem 0 0; min-height: 1em; }
/* Eingabefelder nicht in die Höhe ziehen, wenn daneben ein Fehlertext steht */
.grid { align-items: end; }
.grid > label { align-content: end; }

/* 0.4.0 – deutlichere Feldhinweise */
.field-error, .field-warn { display: block; font-size: .82rem; font-weight: 600; line-height: 1.3; padding: .25rem .5rem; border-radius: 6px; margin-bottom: .25rem; }
.field-error { color: #fff; background: var(--err); }
.field-error::before { content: "⚠ "; }
.field-warn { color: #6b4500; background: #ffe7a8; }
.field-warn::before { content: "! "; font-weight: 800; }
input.warned, select.warned { border: 2px solid #e0a100; background: #fffaf0; }
td .field-error, td .field-warn { max-width: 190px; white-space: normal; }
td.desc .field-error, td.desc .field-warn { max-width: none; }
.gen-status .ok-text { color: var(--ok); font-weight: 600; }
.version { color: #9a9288; margin-right: .3rem; }
.version-warning { background: var(--err); color: #fff; padding: .7rem 1.2rem; font-weight: 600; }
.versions { margin-top: 1.2rem; }
.versions h3 { font-size: .95rem; margin: 1rem 0 .3rem; }
.versions ul { margin: 0; padding-left: 1.2rem; font-size: .9rem; }

/* 0.4.1 – ruhigere Feldhinweise (überschreibt 0.4.0) */
.field-error, .field-warn { font-weight: 500; font-size: .8rem; padding: .15rem .45rem; border-radius: 4px; border-left: 3px solid; margin-bottom: .2rem; }
.field-error { color: #9b2c24; background: #fbeeec; border-left-color: #d9837b; }
.field-error::before { content: ""; }
.field-warn { color: #7a5a14; background: #fbf4e2; border-left-color: #e2c27a; }
.field-warn::before { content: ""; }
input.invalid, select.invalid { border: 1px solid #d9837b; background: #fffafa; box-shadow: 0 0 0 2px #f6dcd9; }
input.warned, select.warned { border: 1px solid #e2c27a; background: #fffdf7; box-shadow: none; }
input.invalid:focus, select.invalid:focus { outline: 2px solid #d9837b; }
@keyframes flash { 0%, 100% { box-shadow: 0 0 0 2px #f6dcd9; } 40% { box-shadow: 0 0 0 5px #f0c4bf; } }
.gen-status { color: #9b2c24; font-weight: 400; }
.version-warning { background: #fbeeec; color: #9b2c24; border-bottom: 1px solid #f0c4bf; font-weight: 500; }

/* ===== 0.5.0 – Handy und Tablet ===== */
.mobile-total { display: none; }

@media (max-width: 1000px) {
  main { padding: 1rem; }
  .pdfbox iframe { height: 420px; }
}

@media (max-width: 700px) {
  html, body { font-size: 15px; }
  /* iOS zoomt bei Schrift unter 16 px in Eingabefeldern – verhindern */
  input, select { font-size: 16px; min-height: 46px; }
  main { padding: .75rem .75rem 7.5rem; }
  .card { padding: 1rem .9rem; border-radius: 8px; }
  h2 { margin: 1.2rem 0 .6rem; }

  /* Kopfzeile: Marke + Benutzer oben, Reiter wischbar darunter */
  .topbar { padding: .5rem .75rem 0; gap: .4rem .8rem; }
  .topbar .logo { width: 30px; height: 30px; }
  .user { margin-left: auto; font-size: .9rem; }
  .user .version { display: none; }
  nav { order: 3; flex: 0 0 100%; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; margin: 0 -.75rem; padding: 0 .5rem .35rem; }
  nav::-webkit-scrollbar { display: none; }
  nav button { white-space: nowrap; padding: .55rem .75rem; }

  /* Formular einspaltig */
  .grid { grid-template-columns: 1fr; gap: .7rem; }
  .grid .wide, .grid .full { grid-column: auto; }

  /* Hochladen */
  .drop { padding: 2rem 1rem; }
  .drop-desktop { display: none; }
  #pickFile { width: 100%; min-height: 50px; font-size: 1rem; }

  /* Positionen als Karten statt Tabelle */
  .lines-wrap { overflow: visible; }
  table.lines, table.lines tbody { display: block; }
  table.lines thead { display: none; }
  table.lines tr { display: grid; grid-template-columns: 1fr 1fr; gap: .55rem .6rem; padding: .8rem; margin-bottom: .7rem; border: 1px solid var(--line); border-radius: 8px; background: #fcfbf9; position: relative; }
  table.lines td { display: block; padding: 0; border: 0; }
  table.lines td::before { content: attr(data-label); display: block; font-size: .75rem; color: var(--muted); margin-bottom: .15rem; }
  table.lines td.desc { grid-column: 1 / -1; min-width: 0; padding-right: 2.2rem; }
  table.lines td.desc::before { content: "Bezeichnung"; }
  table.lines input.n, table.lines select { width: 100%; }
  table.lines td.sum { text-align: left; padding-top: 0; font-weight: 600; align-self: end; }
  table.lines td.sum::before { font-weight: 400; }
  table.lines td.del { position: absolute; top: .45rem; right: .4rem; }
  table.lines td.del::before { content: none; }
  .link.danger { padding: .4rem .6rem; font-size: 1.2rem; }
  #addLine { width: 100%; min-height: 44px; }
  td .field-error, td .field-warn { max-width: none; }

  /* Summe + Button fest am unteren Rand */
  .editor-side { position: static; }
  .totals { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; border-radius: 12px 12px 0 0; border-bottom: 0; padding: .6rem .9rem calc(.6rem + env(safe-area-inset-bottom)); box-shadow: 0 -4px 18px #0000001a; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: .1rem .8rem; }
  .totals h2, .totals dl { display: none; }
  .mobile-total { display: block; font-weight: 700; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
  .totals .big { margin: 0; width: auto; min-height: 46px; font-size: .95rem; padding: .5rem 1rem; grid-row: 1 / span 2; grid-column: 2; }
  .totals .gen-status { grid-column: 1; text-align: left; margin: 0; font-size: .78rem; }
  .totals #reset { display: none; }
  #result { scroll-margin-bottom: 7rem; }
  .pdfbox iframe { height: 360px; }

  /* Listen (Erstellte Rechnungen, Protokoll) als Karten */
  table.list, table.list tbody { display: block; }
  table.list thead { display: none; }
  table.list tr { display: grid; grid-template-columns: 1fr 1fr; gap: .3rem .8rem; padding: .75rem 0; border-bottom: 1px solid var(--line); }
  table.list td { display: block; padding: 0; border: 0; text-align: left; }
  table.list td::before { content: attr(data-label); display: block; font-size: .72rem; color: var(--muted); }
  table.list td.nr { font-weight: 700; }
  table.list td.files { grid-column: 1 / -1; }
  table.list td.files::before { content: none; }
  table.list td[colspan] { grid-column: 1 / -1; }
  .downloads .btn { flex: 1 1 auto; min-height: 42px; }
}
.next { width: 100%; margin-top: .6rem; }
.mobile-total small { font-weight: 500; font-size: .8rem; }

/* 0.5.0 – Tablet hochkant: Reiter in eigener Zeile, Summe + Button unten fest */
@media (max-width: 1000px) {
  .topbar { padding: .5rem 1rem 0; gap: .4rem .8rem; }
  .user { margin-left: auto; }
  nav { order: 3; flex: 0 0 100%; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; margin: 0 -1rem; padding: 0 .75rem .35rem; }
  nav::-webkit-scrollbar { display: none; }
  nav button { white-space: nowrap; }
  main { padding-bottom: 7.5rem; }
  .editor-side { position: static; }
  .totals { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; border-radius: 12px 12px 0 0; border-bottom: 0; padding: .6rem 1rem calc(.6rem + env(safe-area-inset-bottom)); box-shadow: 0 -4px 18px #0000001a; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: .1rem .8rem; }
  .totals h2, .totals dl, .totals #reset { display: none; }
  .mobile-total { display: block; font-weight: 700; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
  .totals .big { margin: 0; width: auto; min-height: 46px; font-size: .95rem; padding: .5rem 1.2rem; grid-row: 1 / span 2; grid-column: 2; }
  .totals .gen-status { grid-column: 1; text-align: left; margin: 0; font-size: .8rem; }
  #result { scroll-margin-bottom: 7rem; }
}
