/* Kompaktes Mobile-First-Layout. Alles in Design-Tokens, damit sich Dichte
   und Farben an einer Stelle ändern lassen. */

:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #eeeeea;
  --line: #dcdcd6;
  --text: #1a1a18;
  --muted: #74746c;
  --accent: #2f7d4f;
  --accent-soft: #e2f0e6;
  --danger: #b3392f;

  --row-h: 34px;
  --gap: 6px;
  --pad: 8px;
  --radius: 7px;
  --font: 15px/1.25 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --surface: #1e1f23;
    --surface-2: #26282d;
    --line: #33353b;
    --text: #e9e9e6;
    --muted: #9b9ca3;
    --accent: #5ab97f;
    --accent-soft: #22352a;
    --danger: #e0685c;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  margin: 0;
}
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }

/* ------------------------------------------------------------------ Topbar */

/* Kopfbereich (Listenwahl + Suche) bleibt beim Scrollen stehen. */
#header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
}

#topbar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  padding: 6px var(--pad);
  padding-top: max(6px, env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

#listPicker {
  flex: 1;
  min-width: 0;
  height: 30px;
  padding: 0 22px 0 8px;
  font-weight: 600;
  border-radius: var(--radius);
  background: var(--surface-2) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6'><path d='M0 0l5 6 5-6z' fill='%23888'/></svg>") no-repeat right 7px center;
  -webkit-appearance: none;
  appearance: none;
}

.icon-btn {
  flex: none;
  width: 32px;
  height: 30px;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: 16px;
  line-height: 1;
}
.icon-btn:active { background: var(--line); }

/* ------------------------------------------------------------------- Suche */

#searchbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: var(--gap) var(--pad);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

#searchWrap {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  height: 34px;
  padding: 0 6px 0 8px;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
#searchWrap:focus-within { border-color: var(--accent); }

#search {
  flex: 1;
  min-width: 0;
  height: 100%;
  outline: none;
  background: none;
}
#search::placeholder { color: var(--muted); }
#search::-webkit-search-cancel-button,
#search::-webkit-search-decoration { -webkit-appearance: none; appearance: none; }

#qtyBadge {
  flex: none;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
  white-space: nowrap;
}
#qtyBadge:empty { display: none; }

#clearSearch { flex: none; width: 24px; color: var(--muted); font-size: 17px; }
#clearSearch[hidden] { display: none; }

/* --------------------------------------------------------------- Ergebnisse */

#results {
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  max-height: 62vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#results[hidden] { display: none; }

.res {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-height: var(--row-h);
  padding: 5px var(--pad);
  text-align: left;
  border-bottom: 1px solid var(--line);
}
.res:last-child { border-bottom: 0; }
.res:active { background: var(--surface-2); }
.res .rname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res .rqty { flex: none; font-size: 12px; font-weight: 600; color: var(--accent); }
.res .ron { flex: none; font-size: 11px; color: var(--accent); }
.res.new { color: var(--accent); font-weight: 600; }

.res-create {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px var(--pad);
  background: var(--surface-2);
}
.res-create .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.res-create select {
  height: 28px;
  max-width: 42%;
  padding: 0 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.res-create button {
  height: 28px;
  padding: 0 10px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: 5px;
}

/* -------------------------------------------------------------------- Liste */

main { padding-bottom: calc(24px + var(--safe-b)); }

/* Einzige verbliebene Zwischenüberschrift: die Trennung zu den erledigten
   Einträgen. Kategorien tragen ihr Emoji stattdessen in der Zeile selbst. */
.done-head {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px var(--pad);
  margin-top: 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.done-head .count { margin-left: auto; font-weight: 600; opacity: .8; }

ul.items { margin: 0; padding: 0; list-style: none; }

.item {
  display: flex;
  align-items: center;
  min-height: var(--row-h);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

/* Kategoriewechsel: eine kräftigere Linie statt einer Überschrift.
   Kostet 1 px Höhe statt ~21 px. Zum Abschalten diese Regel löschen. */
.item.cat-start { border-top: 1px solid var(--line); }
.items .item.cat-start:first-child { border-top: 0; }

.check {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  align-self: stretch;
}

/* Kategorie-Emoji als Zeilen-Icon */
.cat-icon {
  flex: none;
  width: 21px;
  padding-right: 3px;
  font-size: 14px;
  line-height: 1;
  text-align: center;
  user-select: none;
}
.item.done .cat-icon { opacity: .35; }
.check i {
  display: block;
  width: 17px;
  height: 17px;
  border: 1.5px solid var(--muted);
  border-radius: 4px;
}
.item.done .check i {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 2px var(--surface);
}

.label {
  flex: 1;
  min-width: 0;
  align-self: stretch;
  padding: 6px 4px 6px 0;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.item.done .label { color: var(--muted); text-decoration: line-through; }

.qty {
  flex: none;
  padding: 3px 6px;
  margin-right: 2px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 5px;
  white-space: nowrap;
}
.qty.empty { color: var(--muted); background: none; font-weight: 400; }
.item.done .qty { color: var(--muted); background: none; }

.del { flex: none; width: 30px; align-self: stretch; color: var(--muted); font-size: 16px; }

/* Inline-Mengeneditor */
.qty-edit {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px var(--pad) 4px 30px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
}
.qty-edit input {
  width: 66px;
  height: 30px;
  padding: 0 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.qty-edit select {
  height: 30px;
  padding: 0 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.qty-edit .step {
  width: 30px; height: 30px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 5px;
  font-size: 16px;
}
.qty-edit .ok {
  margin-left: auto;
  height: 30px;
  padding: 0 12px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: 5px;
}

/* ------------------------------------------------------- Schnellzugriff */

#frequent { padding: 10px var(--pad) 0; }
#frequent[hidden] { display: none; }
#frequent h2 {
  margin: 0 0 5px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
}
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  padding: 5px 9px;
  font-size: 13.5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  white-space: nowrap;
}
.chip:active { background: var(--accent-soft); border-color: var(--accent); }
.chip.on { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

.empty-hint {
  padding: 28px var(--pad);
  text-align: center;
  color: var(--muted);
  font-size: 13.5px;
}

/* ------------------------------------------------------------------ Dialoge */

dialog {
  width: 100%;
  max-width: 520px;
  margin: auto auto 0;
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border: 0;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .38); }

.sheet-head {
  display: flex;
  align-items: center;
  padding: 10px var(--pad);
  font-weight: 700;
  border-bottom: 1px solid var(--line);
}
.sheet-head button { margin-left: auto; color: var(--muted); font-size: 17px; }
.sheet-body { padding: var(--gap) 0 calc(var(--gap) + var(--safe-b)); max-height: 70vh; overflow-y: auto; }

.sheet-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  text-align: left;
}
.sheet-item:active { background: var(--surface-2); }
.sheet-item.danger { color: var(--danger); }
.sheet-sep { height: 1px; margin: var(--gap) 0; background: var(--line); }

.form { padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.form input, .form select, .form textarea {
  width: 100%;
  height: 38px;
  padding: 0 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.form textarea { height: 120px; padding: 8px; font-family: ui-monospace, monospace; font-size: 12px; resize: vertical; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; }
.form.tight { padding-bottom: 6px; }
.hint-text { margin: 0; color: var(--muted); font-size: 13px; }
.dot { color: var(--accent); }
.btn {
  height: 36px;
  padding: 0 14px;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-weight: 600;
}
.btn.primary { color: #fff; background: var(--accent); }
.btn.danger { color: #fff; background: var(--danger); }

/* Produktverwaltung */
.pm-row {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--line);
}
.pm-row .pm-name {
  flex: 1;
  min-width: 0;
  padding: 4px 0;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pm-row select {
  flex: none;
  max-width: 44%;
  height: 28px;
  padding: 0 4px;
  font-size: 13px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.pm-row .del { width: 26px; }

/* ------------------------------------------------------------------- Toast */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(14px + var(--safe-b));
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 92vw;
  padding: 9px 14px;
  font-size: 13.5px;
  color: #fff;
  background: #24262b;
  border-radius: 18px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { color: #7fd6a0; font-weight: 700; }

@media (min-width: 620px) {
  :root { --pad: 12px; }
  #header > *, #results, main, #frequent { max-width: 620px; margin-inline: auto; }
  dialog { margin-bottom: auto; border-radius: 12px; }
}
