:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #15171c;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --accent-soft: #e8f0fe;
  --danger: #d92d20;
  --ok: #12805c;
  --ok-soft: #e3f5ec;
  --warn-soft: #fff4e0;
  --warn: #9a5b00;
  --field: #f1f2f5;
  --radius: 14px;
  --tabbar-h: 58px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --card: #1b1e25;
    --text: #f2f3f5;
    --muted: #9aa1ad;
    --line: #2b2f38;
    --accent: #4f8cff;
    --accent-soft: #1d2a44;
    --ok: #4cc38a;
    --ok-soft: #173327;
    --warn-soft: #3a2c12;
    --warn: #f0b552;
    --field: #252932;
  }
}

[hidden] { display: none !important; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  padding-top: env(safe-area-inset-top);
  min-height: 100vh;
}

.view { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px); }
#view-edit, #view-settings { padding-bottom: calc(110px + env(safe-area-inset-bottom)); }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px 8px; max-width: 680px; margin: 0 auto;
}
.topbar h1 { font-size: 30px; margin: 0; letter-spacing: -0.02em; flex: 1; }
.topbar .small-title { font-size: 17px; text-align: center; letter-spacing: 0; }
.topbar .spacer { width: 72px; }
.topbar .link-button { width: 72px; text-align: left; }

.content { max-width: 680px; margin: 0 auto; padding: 4px 16px; display: grid; gap: 14px; }
.card { background: var(--card); border-radius: var(--radius); padding: 16px; display: grid; gap: 12px; }
.card h2 { font-size: 17px; margin: 0; }
.hint { color: var(--muted); font-size: 13.5px; margin: -4px 0 0; }
.hint.warn { color: var(--danger); }
.opt { color: var(--muted); font-weight: 400; }

.notice { background: var(--accent-soft); }
.notice .primary { justify-self: start; }

label { display: block; font-size: 14px; font-weight: 600; color: var(--muted); }
label > input, label > select, label > textarea { display: block; margin-top: 5px; }
.grid-2 { align-items: end; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--text); font-weight: 400;
  background: var(--field); border: 1px solid transparent; border-radius: 10px;
  padding: 11px 12px; width: 100%; min-width: 0;
  -webkit-appearance: none; appearance: none;
}
input[type="date"] { min-height: 44px; }
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat; padding-right: 34px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { width: auto; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }

button { font: inherit; border: 0; cursor: pointer; border-radius: 10px; color: inherit; }
.primary, .secondary, .as-button {
  padding: 12px 18px; font-weight: 600; font-size: 16px; text-align: center; display: inline-block;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary, .as-button { background: var(--field); color: var(--text); border-radius: 10px; cursor: pointer; }
.small { padding: 8px 14px; font-size: 15px; }
.full { width: 100%; }
.big { padding: 15px; font-size: 17px; }
.link-button { background: none; color: var(--accent); padding: 8px 0; font-weight: 600; }
.danger { color: var(--danger); }
button:disabled { opacity: .5; }
.button-row { display: flex; gap: 10px; flex-wrap: wrap; }
.button-row > * { flex: 1; }

/* Liste */
.search { margin: 0; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 14px; }
.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 20px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.invoice-list { list-style: none; margin: 0; padding: 0; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.invoice-list:empty { display: none; }
.invoice-list li { border-bottom: 1px solid var(--line); }
.invoice-list li:last-child { border-bottom: 0; }
.invoice-list button {
  width: 100%; background: none; border-radius: 0; text-align: left;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 13px 16px;
}
.invoice-list button:active { background: var(--field); }
.inv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-sum { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.inv-meta { font-size: 13.5px; color: var(--muted); }
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; justify-self: end; }
.badge.open { background: var(--warn-soft); color: var(--warn); }
.badge.overdue { background: #fde8e6; color: var(--danger); }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
@media (prefers-color-scheme: dark) { .badge.overdue { background: #3d1a17; } }
.empty { color: var(--muted); text-align: center; margin: 24px 0; }

/* Positionen */
.positions { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; counter-reset: pos; }
.positions li {
  border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 8px;
  counter-increment: pos;
}
.pos-head { display: flex; justify-content: space-between; align-items: center; }
.pos-head::before { content: "Position " counter(pos); font-size: 13px; font-weight: 700; color: var(--muted); }
.pos-head button { background: none; color: var(--danger); font-size: 14px; font-weight: 600; padding: 4px 0; }
.pos-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pos-grid label { font-size: 12.5px; }
.pos-sum { text-align: right; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pos-sum strong { color: var(--text); }

.totals { margin: 4px 0 0; display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-variant-numeric: tabular-nums; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; }
.totals { column-gap: 0; }
.totals dd { padding-left: 12px; }
.totals .grand { font-weight: 700; font-size: 18px; color: var(--text); border-top: 1px solid var(--line); padding-top: 8px; }

.switch-label { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 500; font-size: 14px; }
.switch-label input {
  width: 44px; height: 26px; border-radius: 99px; background: var(--line); position: relative; padding: 0; transition: background .15s;
}
.switch-label input::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s;
}
.switch-label input:checked { background: var(--accent); }
.switch-label input:checked::after { transform: translateX(18px); }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--field); border-radius: 10px; padding: 3px; }
.segmented label { display: block; color: var(--text); font-weight: 500; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; text-align: center; padding: 8px 4px; border-radius: 8px; font-size: 14.5px; cursor: pointer; }
.segmented input:checked + span { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.12); font-weight: 600; }

.check { display: flex; gap: 10px; align-items: flex-start; color: var(--text); font-weight: 500; }
.check input {
  width: 22px; height: 22px; flex: none; border-radius: 6px; padding: 0; margin-top: 1px;
  border: 1.5px solid var(--muted); background: var(--card); display: grid; place-items: center;
}
.check input:checked { background: var(--accent); border-color: var(--accent); }
.check input:checked::after { content: "✓"; color: #fff; font-size: 15px; font-weight: 700; line-height: 1; }
.check small { color: var(--muted); font-weight: 400; display: block; }

.b2b summary { font-size: 14.5px; font-weight: 600; color: var(--accent); cursor: pointer; padding: 4px 0; }
.b2b[open] { display: grid; gap: 12px; }

.sticky-actions {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.sticky-actions button { max-width: 648px; margin: 0 auto; display: block; }

/* Logo */
.logo-row { display: flex; gap: 14px; align-items: center; }
.logo-box {
  width: 140px; height: 70px; border-radius: 10px; background: var(--field);
  display: grid; place-items: center; overflow: hidden; flex: none;
}
.logo-box span { color: var(--muted); font-size: 13px; }
.logo-box img { max-width: 100%; max-height: 100%; object-fit: contain; background: #fff; }
.logo-actions { display: grid; gap: 4px; justify-items: start; }

/* Tab-Leiste */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: 1fr 1fr;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar[hidden] { display: none; }
.tabbar button {
  background: none; border-radius: 0; height: var(--tabbar-h); color: var(--muted);
  display: grid; justify-items: center; align-content: center; gap: 2px; font-size: 11px; font-weight: 600;
}
.tabbar svg { width: 25px; height: 25px; }
.tabbar button.active { color: var(--accent); }

/* Aktionsblatt */
.sheet {
  border: 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  width: 100%; max-width: 520px; margin: auto auto 0; border-radius: 18px 18px 0 0;
  background: var(--card); color: var(--text);
}
.sheet::backdrop { background: rgba(0,0,0,.45); }
.sheet-head { display: grid; gap: 2px; margin-bottom: 12px; }
.sheet-head span { color: var(--muted); font-size: 14px; }
.sheet-buttons { display: grid; gap: 8px; margin-bottom: 8px; }
.sheet-buttons button { padding: 13px; font-weight: 600; background: var(--field); }
.sheet-buttons button.primary { background: var(--accent); color: var(--accent-text); }
.sheet-buttons button.danger { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 20px + env(safe-area-inset-bottom)); z-index: 50;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 14px; font-size: 15px; font-weight: 500;
  transition: opacity .2s, transform .2s; width: max-content; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* In den Einstellungen sitzt der Speichern-Knopf über der Tab-Leiste */
#view-settings { padding-bottom: calc(var(--tabbar-h) + 100px + env(safe-area-inset-bottom)); }
#view-settings .sticky-actions { bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: 10px; }

/* Vorlagen */
.pos-actions { display: flex; gap: 14px; }
.pos-head .tpl { background: none; color: var(--accent); font-size: 14px; font-weight: 600; padding: 4px 0; }
.tpl-list { list-style: none; padding: 0; margin: 10px 0; max-height: 55vh; overflow: auto; display: grid; gap: 6px; }
.tpl-list button {
  width: 100%; text-align: left; background: var(--field); padding: 11px 13px;
  display: grid; gap: 2px;
}
.tpl-list button:active { opacity: .7; }
.tpl-text { font-weight: 600; overflow-wrap: anywhere; }
.tpl-price { font-size: 13.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tpl-settings { list-style: none; padding: 0; margin: 0; }
.tpl-settings li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.tpl-settings li:last-child { border-bottom: 0; }
.tpl-settings .info { flex: 1; min-width: 0; }
.small-empty { margin: 4px 0; font-size: 14px; }

/* Nichts darf breiter als der Bildschirm werden */
.content { grid-template-columns: minmax(0, 1fr); }
.card { min-width: 0; }
.grid-2, .pos-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.button-row > * { min-width: 0; }
input[type="date"] { min-width: 0; max-width: 100%; }
