:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-muted: #e9e6e0;
  --text: #1d1b18;
  --text-soft: #6d6862;
  --line: #ddd8d0;
  --accent: #2f7d5d;
  --accent-text: #ffffff;
  --danger: #b3382f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 12px rgba(0, 0, 0, .05);
  --radius: 14px;
  --tap: 44px;
  --karte-hoehe: 106px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #212327;
    --surface-muted: #2c2f34;
    --text: #ecebe8;
    --text-soft: #9a968f;
    --line: #34383d;
    --accent: #4aa97e;
    --accent-text: #10221a;
    --danger: #e0685c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

body { padding-bottom: env(safe-area-inset-bottom); }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }

/* Beim langen Tippen fängt der Browser sonst an, den Text zu markieren –
   die Auswahl schluckt dann das Ereignis und der Dialog geht nicht auf.
   Eingabefelder bleiben davon ausgenommen. */
.card, .chip, .btn, .who-pick, .alphabet button, .topbar, .section-title {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

input, textarea { -webkit-user-select: text; user-select: text; }

.app { max-width: 720px; margin: 0 auto; padding: 0 16px 120px; }

/* ---------- Kopfzeile ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 58px;
  padding: max(8px, env(safe-area-inset-top)) 0 8px;
  background: linear-gradient(var(--bg) 78%, transparent);
}
/* Mit zwei Symbolen und dem Kürzel rechts wird es auf schmalen Geräten
   eng – der Listenname kürzt sich dann lieber ab, als umzubrechen. */
.topbar h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.22rem;
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar .sub {
  display: block;
  font-size: .78rem;
  font-weight: 400;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  margin-inline: -4px;
  border-radius: 12px;
  color: var(--text-soft);
}
.icon-btn:active { background: var(--surface-muted); }
.icon-btn svg { width: 22px; height: 22px; }

.who {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: .82rem;
  font-weight: 700;
  color: #fff;
}

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

/* Fotos und Artikel stehen in getrennten Rastern. Der Innenabstand eines
   Rasters wirkt nicht nach außen, sonst klebten die beiden aneinander. */
.cards + .cards { margin-top: 10px; }

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--karte-hoehe);
  padding: 16px 15px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform .12s ease, opacity .18s ease, background .18s ease;
}
.card:active { transform: scale(.975); }
.card .title { font-weight: 600; letter-spacing: -.005em; overflow-wrap: anywhere; }
/* Die Mengenangabe ist beim Einkaufen die zweitwichtigste Information
   auf der Karte – sie soll im Vorbeigehen lesbar sein. */
.card .meta {
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-soft);
}

/* Im Wagen: hellgrau, aber weiter antippbar – ein zweiter Tipp holt sie zurück. */
.card.carted { background: var(--surface-muted); box-shadow: none; }
.card.carted .title { color: var(--text-soft); text-decoration: line-through; }

.countdown {
  position: absolute;
  top: 8px;
  right: 10px;
  font-size: .7rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-soft);
}

.badge {
  position: absolute;
  top: 8px;
  right: 10px;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  font-size: .64rem;
  font-weight: 700;
  color: #fff;
}

.count-pill {
  align-self: flex-start;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-muted);
  font-size: .74rem;
  color: var(--text-soft);
}

.card.add {
  align-items: center;
  justify-content: center;
  border: 1.5px dashed var(--line);
  background: transparent;
  box-shadow: none;
  color: var(--text-soft);
  font-weight: 600;
}

.section-title {
  margin: 22px 0 9px;
  font-size: .76rem;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-soft);
}

.empty {
  padding: 42px 20px;
  color: var(--text-soft);
  text-align: center;
}

/* ---------- Anmeldung ---------- */
.gate {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100dvh;
  max-width: 380px;
  margin: 0 auto;
  padding: 24px 20px;
}
.gate h1 { margin: 0 0 4px; font-size: 1.6rem; }
.gate p { margin: 0 0 26px; color: var(--text-soft); }

.who-grid { display: flex; flex-wrap: wrap; gap: 9px; margin-bottom: 18px; }
.who-pick {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 15px 9px 9px;
  border: 1.5px solid transparent;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-weight: 600;
}
.who-pick[aria-pressed="true"] { border-color: var(--accent); }

.field {
  width: 100%;
  height: 50px;
  padding: 0 15px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
.field:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.btn {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 50px;
  margin-top: 12px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}
.btn[disabled] { opacity: .45; }
.btn.ghost { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.btn.danger { background: var(--danger); color: #fff; }

.error { min-height: 20px; margin-top: 10px; color: var(--danger); font-size: .86rem; }

/* ---------- Hinzufügen-Panel ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, .35);
}
.sheet {
  position: fixed;
  inset: auto 0 0;
  z-index: 41;
  max-height: 86dvh;
  padding: 10px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0;
  background: var(--bg);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .22);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sheet .grip {
  width: 38px;
  height: 4px;
  margin: 2px auto 4px;
  border-radius: 2px;
  background: var(--line);
}
.sheet-scroll { overflow-y: auto; -webkit-overflow-scrolling: touch; }

.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  min-height: 38px;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: .92rem;
  font-weight: 500;
}
.chip:active { background: var(--surface-muted); }
.chip.new { border: 1.5px dashed var(--accent); background: transparent; box-shadow: none; color: var(--accent); font-weight: 600; }

.alphabet {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}
.alphabet::-webkit-scrollbar { display: none; }
.alphabet button {
  flex: 0 0 auto;
  min-width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--text-soft);
  font-size: .84rem;
  font-weight: 600;
}
.alphabet button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

.fab-row {
  position: fixed;
  right: max(16px, calc(50vw - 360px + 16px));
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 10px;
}

.fab {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 54px;
  padding: 0 22px;
  border-radius: 27px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
}

/* Der Fotoknopf steht bewusst zurückhaltender daneben – der Regelfall
   bleibt das Eintippen eines Artikels. */
.fab-rund {
  width: 54px;
  padding: 0;
  justify-content: center;
  background: var(--surface);
  color: var(--text);
}

/* ---------- Bestätigungsdialog ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, .4);
}
.modal-box {
  width: 100%;
  max-width: 330px;
  padding: 22px;
  border-radius: 16px;
  background: var(--surface);
}
.modal-box h2 { margin: 0 0 8px; font-size: 1.05rem; }
.modal-box p { margin: 0 0 18px; color: var(--text-soft); font-size: .92rem; }

/* ---------- Verbindungshinweis ---------- */
.offline {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  z-index: 35;
  transform: translateX(-50%);
  padding: 7px 15px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: .82rem;
  box-shadow: var(--shadow);
}

[hidden] { display: none !important; }

/* ---------- Langes Tippen ---------- */
/* Zwei Sekunden ohne Rückmeldung fühlen sich wie ein Fehler an – der Balken
   zeigt, dass gerade etwas passiert, und wie lange es noch dauert. */
.card.pressing::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--accent);
  animation: druck-fortschritt 2s linear forwards;
}

@keyframes druck-fortschritt {
  from { width: 0; }
  to   { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .card.pressing::after { animation: none; width: 100%; opacity: .4; }
}

/* ---------- Bearbeiten-Dialog ---------- */
.feld-label {
  display: block;
  margin: 14px 0 6px;
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-soft);
}
.feld-label:first-child { margin-top: 0; }

.chip.chosen {
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: none;
}

/* ---------- Kurze Rückmeldung ---------- */
.hinweis {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  z-index: 45;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%);
  padding: 11px 18px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: .9rem;
  font-weight: 500;
  text-align: center;
  box-shadow: var(--shadow);
}

.hinweis.fehler { background: var(--danger); color: #fff; }

/* ---------- Fotokarten ---------- */
/* Ohne feste Höhe bestimmt das Bild die Kartengröße – ein Hochformatfoto
   würde die Karte doppelt so hoch machen wie ihre Nachbarn. */
.card.foto {
  height: var(--karte-hoehe);
  padding: 0;
  overflow: hidden;
  background: var(--surface-muted);
}

.card.foto img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  /* Ohne das übernimmt iOS beim langen Tippen die eigene Bild-Geste: Es
     hebt das ungeschnittene Foto als Vorschau an und bricht dabei die
     Berührung ab – der Zähler fürs lange Tippen käme nie ans Ziel.
     Die Ereignisse sollen an der Karte darunter ankommen, nicht am Bild. */
  pointer-events: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
  user-select: none;
}

/* Im Wagen: dasselbe Zurücktreten wie bei einer Textkarte. */
.card.foto.carted img { opacity: .35; filter: grayscale(1); }

.card.foto .countdown,
.card.foto .badge {
  background: rgba(0, 0, 0, .55);
  padding: 1px 6px;
  border-radius: 999px;
  color: #fff;
}

/* ---------- Große Fotoansicht ---------- */
.foto-voll {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, .92);
}

.foto-voll img {
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
}

/* Die Eingaben stehen unter dem Bild; bei wenig Platz – Querformat oder
   aufgeklappter Tastatur – wird dieser Teil scrollbar, das Bild schrumpft. */
.foto-felder {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 62dvh;
  overflow-y: auto;
  padding-bottom: 2px;
}

.foto-felder .btn { margin-top: 4px; }

/* Beschriftung wie auf einer Textkarte: Name oben, Menge unten. Der
   Verlauf dahinter hält die Schrift auf hellen wie dunklen Fotos lesbar. */
.foto-name,
.foto-menge {
  position: absolute;
  left: 0;
  right: 0;
  /* Gleiche Ränder wie auf einer Textkarte (16px 15px), damit Name und
     Menge auf beiden Kachelarten auf derselben Linie sitzen. */
  padding: 16px 15px;
  color: #fff;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.foto-name {
  top: 0;
  padding-bottom: 22px;
  background: linear-gradient(rgba(0, 0, 0, .72), transparent);
  font-weight: 600;
  letter-spacing: -.005em;
}

.foto-menge {
  bottom: 0;
  padding-top: 22px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .72));
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.card.foto.carted .foto-name,
.card.foto.carted .foto-menge { opacity: .5; }
