:root {
  --bg: #f4f4f5;
  --surface: #ffffff;
  --text: #18181b;
  --muted: #71717a;
  --border: #e4e4e7;
  --accent: #9f1239;
  --accent-soft: #ffe4e6;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --highlight: #fff7ed;
  --tabbar-h: 60px;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --surface: #18181b;
    --text: #f4f4f5;
    --muted: #a1a1aa;
    --border: #2e2e33;
    --accent: #fb7185;
    --accent-soft: #4c0519;
    --ok: #4ade80;
    --ok-soft: #14532d;
    --warn: #fbbf24;
    --warn-soft: #451a03;
    --danger: #f87171;
    --highlight: #27201a;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] {
  display: none !important;
}

button,
input {
  font: inherit;
  color: inherit;
}

/* 16px verhindert das automatische Hineinzoomen auf iOS */
input {
  font-size: 16px;
  min-height: 44px;
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

button {
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.danger {
  color: var(--danger);
}

button.wide {
  display: block;
  width: 100%;
  margin-top: 16px;
}

a {
  color: var(--accent);
}

/* ---------- Kopfzeile ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(6px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  min-height: calc(52px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-left:empty {
  display: none;
}

.back {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding-right: 4px;
  text-decoration: none;
  font-weight: 500;
  white-space: nowrap;
}

.icon-btn {
  width: 44px;
  padding: 0;
  font-weight: 700;
  font-size: 18px;
  color: var(--accent);
}

.icon-btn.off {
  color: var(--muted);
  text-decoration: line-through;
}

.sync {
  font-size: 13px;
  color: var(--muted);
}

.sync.busy::before {
  content: "Speichert";
}

.sync.ok::before {
  content: "✓";
  color: var(--ok);
}

.sync.error {
  color: var(--danger);
  font-weight: 600;
}

/* ---------- Inhalt ---------- */
#view {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px max(12px, env(safe-area-inset-left)) calc(var(--tabbar-h) + 88px + env(safe-area-inset-bottom));
}

.group-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 20px 4px 6px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}

.list {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.row-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 12px;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: none;
  width: 100%;
  text-align: left;
}

.row-item:last-child {
  border-bottom: 0;
}

.list.row-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-top: 16px;
}

.row-item.clickable {
  cursor: pointer;
}

.row-item.clickable:active {
  background: var(--bg);
}

.row-item.has-qty {
  background: var(--highlight);
}

.row-item.dim {
  opacity: 0.5;
}

button.row-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 0;
}

.grow {
  flex: 1;
  min-width: 0;
}

.name {
  font-weight: 500;
  overflow-wrap: anywhere;
}

.row-item small,
.name small {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 400;
}

.right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.amount {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.big {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.big small {
  display: inline;
  font-size: 13px;
  font-weight: 400;
  color: var(--muted);
}

.chevron {
  color: var(--muted);
  font-size: 22px;
}

.danger-text {
  color: var(--danger);
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
}

.hint {
  margin: -6px 4px 12px;
  color: var(--muted);
  font-size: 14px;
}

/* ---------- Badges ---------- */
.badge {
  min-height: 32px;
  padding: 4px 12px;
  border-radius: 999px;
  border: 0;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

span.badge {
  display: inline-flex;
  align-items: center;
}

.badge.paid {
  background: var(--ok-soft);
  color: var(--ok);
}

.badge.open {
  background: var(--warn-soft);
  color: var(--warn);
}

.badge.free {
  background: var(--bg);
  color: var(--muted);
}

/* ---------- Suche & Filter ---------- */
.toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chip {
  min-height: 36px;
  padding: 4px 14px;
  border-radius: 999px;
  white-space: nowrap;
  font-size: 14px;
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---------- Stepper ---------- */
.stepper {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface);
}

.stepper button {
  width: 44px;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-radius: 0;
  font-size: 22px;
  line-height: 1;
  color: var(--accent);
}

.stepper button:active {
  background: var(--accent-soft);
}

.stepper input {
  width: 56px;
  min-height: 44px;
  padding: 0 4px;
  border: 0;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  border-radius: 0;
  text-align: center;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.stepper input.zero {
  color: var(--muted);
  font-weight: 400;
}

/* ---------- Formulare ---------- */
.form {
  display: flex;
  flex-direction: column;
}

.field {
  display: block;
  margin-bottom: 12px;
}

.field > span {
  display: block;
  margin: 0 4px 4px;
  font-size: 13px;
  color: var(--muted);
}

.segmented {
  display: flex;
  padding: 3px;
  margin-bottom: 16px;
  border-radius: 12px;
  background: var(--border);
}

.segmented button {
  flex: 1;
  border: 0;
  background: none;
  border-radius: 9px;
  min-height: 40px;
}

.segmented button.active {
  background: var(--surface);
  font-weight: 600;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.15);
}

.button-row {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.button-row button {
  flex: 1;
}

.totals {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 16px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--accent-soft);
}

.totals div {
  display: flex;
  justify-content: space-between;
  font-variant-numeric: tabular-nums;
}

.totals b {
  font-size: 18px;
}

/* Schalter (iOS-Stil) */
.switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex-shrink: 0;
  width: 52px;
  min-height: 0;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
  transition: transform 0.15s;
}

.switch:checked {
  background: var(--ok);
}

.switch:checked::after {
  transform: translateX(20px);
}

.switch-row {
  cursor: pointer;
}

/* ---------- Übersicht ---------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}

.card {
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.card .label {
  font-size: 13px;
  color: var(--muted);
}

.card .value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.card .sub {
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Schwebender Knopf ---------- */
.fab {
  position: fixed;
  right: max(16px, calc(50vw - 360px + 16px));
  bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  z-index: 5;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.25);
}

/* ---------- Tableiste ---------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  color: var(--muted);
  text-decoration: none;
}

.tabbar a.active {
  color: var(--accent);
  font-weight: 600;
}

.tabbar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tabbar circle {
  fill: currentColor;
  stroke: none;
}

.tabbar a[data-tab="products"] circle {
  fill: none;
  stroke: currentColor;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + 84px + env(safe-area-inset-bottom));
  z-index: 20;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.25);
}

/* ---------- Login ---------- */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 24px;
}

.login form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 340px;
}

.login h1 {
  margin: 0 0 8px;
  text-align: center;
  color: var(--accent);
}

.error {
  min-height: 1.4em;
  margin: 0;
  color: var(--danger);
  text-align: center;
}
