/* Einkaufsliste – ein Stylesheet, keine Abhängigkeiten.
   Farben als Tokens; Hell/Dunkel nach System, per data-theme überschreibbar. */

:root {
  --bg: #f5f6f4;
  --surface: #ffffff;
  --surface-2: #eef0ed;
  --text: #1b1f1d;
  --muted: #5f6964;
  --line: #e1e5e1;
  --accent: #2e7d5b;
  --accent-soft: #e3f1ea;
  --on-accent: #ffffff;
  --future: #2a65c4;
  --soon: #a85a00;
  --danger: #c62828;
  --best: #1f7a4d;
  --best-soft: #dff3e8;
  --soon-soft: #fbecd9;
  --shadow: 0 8px 28px rgba(20, 30, 25, .16);
  --radius: 14px;
  --tap: 52px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121413;
    --surface: #1b1e1c;
    --surface-2: #252926;
    --text: #e7ebe8;
    --muted: #9ba59f;
    --line: #2d322f;
    --accent: #4fb487;
    --accent-soft: #1d3329;
    --on-accent: #08130d;
    --future: #79aaff;
    --soon: #f0a24a;
    --danger: #ef6461;
    --best: #5fd39a;
    --best-soft: #173325;
    --soon-soft: #3a2a14;
    --shadow: 0 8px 28px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121413;
  --surface: #1b1e1c;
  --surface-2: #252926;
  --text: #e7ebe8;
  --muted: #9ba59f;
  --line: #2d322f;
  --accent: #4fb487;
  --accent-soft: #1d3329;
  --on-accent: #08130d;
  --future: #79aaff;
  --soon: #f0a24a;
  --danger: #ef6461;
  --best: #5fd39a;
  --best-soft: #173325;
  --soon-soft: #3a2a14;
  --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; overscroll-behavior-x: none; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
  -webkit-tap-highlight-color: transparent;
  overflow-x: clip; /* nie seitlich verschiebbar (clip statt hidden: sticky Kopfzeile bleibt) */
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Kopfzeile und Eingabe (oben fixiert) ---------- */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px 16px;
  background: var(--bg);
}
.topbar h1 {
  flex: 1 0 auto; margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar .icon-btn { flex: none; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg circle { fill: currentColor; stroke: none; }

.sync {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 10px; border: 0; border-radius: 16px;
  background: var(--surface-2); color: var(--muted); font-size: 13px; white-space: nowrap;
  flex: 0 1 auto; min-width: 0; max-width: 62%;
}
.sync .dot { flex: none; }
.sync .label { overflow: hidden; text-overflow: ellipsis; }
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync[data-state="ok"] .dot { background: var(--accent); }
.sync[data-state="syncing"] .dot { background: var(--future); animation: pulse 1s ease-in-out infinite; }
.sync[data-state="offline"] .dot { background: var(--soon); }
.sync[data-state="error"] { color: var(--danger); }
.sync[data-state="error"] .dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.add {
  position: sticky; top: calc(env(safe-area-inset-top) + 60px); z-index: 19;
  padding: 0 12px 10px; background: var(--bg);
}
.add-field {
  display: flex; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.add-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.add input {
  flex: 1; min-width: 0; height: var(--tap); padding: 0 14px;
  border: 0; background: transparent; outline: none; font-size: 17px;
}
.add-btn {
  display: grid; place-items: center; flex: none;
  width: 44px; height: 44px; margin-right: 4px; border: 0; border-radius: 11px;
  background: var(--accent); color: var(--on-accent);
}
.add-btn:active { filter: brightness(.92); }

.suggest {
  position: absolute; left: 12px; right: 12px; top: calc(100% - 6px);
  margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: 50vh; overflow-y: auto;
}
.suggest li {
  display: flex; flex-direction: column; justify-content: center;
  min-height: 48px; padding: 6px 12px; border-radius: 10px; cursor: pointer;
}
.suggest li[aria-selected="true"], .suggest li:active { background: var(--surface-2); }
.suggest .s-title { font-weight: 500; }
.suggest .s-detail { font-size: 13px; color: var(--muted); }
.suggest mark { background: none; color: var(--accent); font-weight: 700; }

.market-bar { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.market-label { margin: 0 2px; font-size: 13px; color: var(--muted); }
.parsed { margin: 2px 2px 0; font-size: 15px; }
.parsed strong { color: var(--accent); }
.parsed-actions { display: flex; justify-content: flex-end; gap: 8px; }
.market-select, .field select {
  width: 100%; min-height: 48px; padding: 0 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text);
}
.market-select { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); font-weight: 600; }
.field select { min-height: 46px; border-radius: 10px; background: var(--bg); }

/* Marktliste bearbeiten */
.mk-cat { margin-bottom: 14px; padding: 10px; border: 1px solid var(--line); border-radius: var(--radius); }
.mk-cat-head { display: flex; gap: 4px; align-items: center; margin-bottom: 6px; }
.mk-cat-name {
  flex: 1; min-width: 0; min-height: 44px; padding: 0 10px; font-weight: 700; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}
.mk-list { margin: 0; padding: 0; list-style: none; }
.mk-row { display: flex; align-items: center; gap: 4px; min-height: 44px; border-top: 1px solid var(--line); }
.mk-name { flex: 1; min-width: 0; padding-left: 4px; }
.mk-btn {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 10px; background: none;
  color: var(--muted); font-size: 18px;
}
.mk-btn:disabled { opacity: .25; }
.mk-btn[data-action$="del"]:not(:disabled) { color: var(--danger); }
.mk-add { display: flex; gap: 8px; margin-top: 8px; }

/* Artikelkatalog */
.art-form { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr); gap: 6px; margin: 8px 0; }
.art-form .actions { grid-column: 1 / -1; margin-top: 0; }
.art-form input, .art-search, .merge-list + .field input {
  min-width: 0; min-height: 44px; padding: 0 10px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}
.art-search { width: 100%; margin: 8px 0 0; }
.art-tools { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 6px; }
.art-tools .btn { min-height: 40px; font-size: 14px; }
.art-group { margin: 10px 0; }
.art-head { display: flex; align-items: center; gap: 8px; margin: 0 0 2px; font-size: 16px; }
.art-uses { color: var(--muted); font-size: 13px; }
.art-edit { padding: 6px 0; border-top: 1px solid var(--line); }
.brand-owner {
  flex: 0 1 46%; min-width: 0; min-height: 40px; padding: 0 6px; font-size: 14px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text);
}
#brand-add select {
  min-width: 0; min-height: 44px; padding: 0 8px; font-size: 16px; grid-column: span 2;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}
#brand-add input { grid-column: span 2; }
.merge-list { max-height: 45dvh; overflow-y: auto; margin: 8px 0; border: 1px solid var(--line); border-radius: 10px; }
.merge-row { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 12px; border-top: 1px solid var(--line); }
.merge-row:first-child { border-top: 0; }
.merge-row input { width: 20px; height: 20px; accent-color: var(--accent); }
.merge-row span:first-of-type { flex: 1; }
.field { position: relative; }
.field-suggest { left: 0; right: 0; top: 100%; z-index: 5; }
@media (max-width: 420px) { .art-form { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.mk-add input {
  flex: 1; min-width: 0; min-height: 44px; padding: 0 10px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}

.field input::placeholder, .field textarea::placeholder { color: var(--muted); opacity: .7; }

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

main { padding: 0 12px; }

.list { margin: 0; padding: 0; list-style: none; }
.group-head {
  margin: 18px 4px 6px; font-size: 12px; font-weight: 700; letter-spacing: .08em;
  text-transform: uppercase; color: var(--muted);
}
.group-head:first-child { margin-top: 6px; }

.item {
  position: relative; margin-bottom: 6px; border-radius: var(--radius);
  overflow: hidden; background: var(--surface-2);
}
.swipe-bg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px; font-weight: 600; font-size: 15px;
}
.swipe-bg .bg-edit { color: var(--accent); }
.swipe-bg .bg-del { color: var(--danger); }
.item[data-swipe="left"] .swipe-bg { background: color-mix(in srgb, var(--danger) 16%, var(--surface)); }
.item[data-swipe="right"] .swipe-bg { background: var(--accent-soft); }

.row {
  position: relative; display: flex; align-items: flex-start; gap: 12px;
  min-height: var(--tap); padding: 13px 4px 13px 14px;
  background: var(--surface); border-radius: var(--radius);
  touch-action: pan-y; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  transition: transform .18s ease, opacity .18s ease;
  outline: none;
}
.row:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.row.dragging { transition: none; }

/* Trefferfläche 44 × 44 px um den 24-px-Kreis, ohne die Zeilenhöhe zu verändern */
.check-btn {
  flex: none; display: grid; place-items: center;
  width: 44px; height: 44px; margin: -10px -10px -10px -10px; padding: 0;
  /* bewusst eckig: abgerundete Ecken verkleinern die Trefferfläche (Hit-Test folgt border-radius) */
  border: 0; border-radius: 0; background: none; cursor: pointer;
}
.check-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -4px; border-radius: 10px; }
.check-btn:active .check { transform: scale(.9); }
.check {
  flex: none; width: 24px; height: 24px;
  border: 2px solid var(--muted); border-radius: 50%;
  display: grid; place-items: center; transition: background .15s, border-color .15s;
}
.check svg { width: 16px; height: 16px; stroke: var(--on-accent); stroke-width: 3; opacity: 0; }
.item.is-done .check { background: var(--accent); border-color: var(--accent); }
.item.is-done .check svg { opacity: 1; }
.item.is-done .title { text-decoration: line-through; color: var(--muted); }
.item.leaving .row { opacity: .55; }

.txt { flex: 1; min-width: 0; }
.title { overflow-wrap: anywhere; }
.sub { margin-top: 2px; font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.sub.note { display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
.v-future { color: var(--future); font-weight: 600; }
.v-soon { color: var(--soon); font-weight: 600; }
.v-expired { text-decoration: line-through; }
.unit-price, .sub.price > span { white-space: nowrap; }
.badge {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 8px;
  font-size: 12px; font-weight: 700; vertical-align: 1px;
}
.badge.best { background: var(--best-soft); color: var(--best); }
.badge.dear { background: var(--soon-soft); color: var(--soon); }
.price-check.higher { color: var(--soon); font-weight: 600; }
.price-check.lowest { color: var(--best); }
.badge.all { background: var(--surface-2); color: var(--muted); font-weight: 600; }
.badge.pending { background: var(--surface-2); color: var(--muted); font-weight: 500; }

.del { flex: none; margin: -10px 0 -10px 0; width: 44px; height: 44px; }
.del svg { width: 20px; height: 20px; }

.flash .row { animation: flash .9s ease; }
@keyframes flash { 30% { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft); } }

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

.banner {
  display: flex; align-items: center; gap: 10px; width: 100%;
  margin: 2px 0 10px; padding: 12px 14px; border: 0; border-radius: var(--radius);
  background: var(--accent-soft); color: var(--text); text-align: left; font-size: 15px;
}
.banner strong { color: var(--accent); }

.ptr {
  height: 0; overflow: hidden; text-align: center; color: var(--muted); font-size: 13px;
  line-height: 32px; transition: height .15s;
}
.ptr.show { height: 32px; }

/* ---------- Erledigt ---------- */

.done { margin-top: 18px; }
.done-toggle {
  display: flex; align-items: center; gap: 6px; width: 100%; min-height: 48px; padding: 0 4px;
  border: 0; background: none; color: var(--muted); font-weight: 600; font-size: 15px;
}
.done-toggle .chev { width: 18px; height: 18px; transition: transform .15s; }
.done-toggle[aria-expanded="true"] .chev { transform: rotate(90deg); }
.count {
  min-width: 24px; padding: 0 7px; border-radius: 10px; background: var(--surface-2);
  font-size: 13px; line-height: 22px; text-align: center;
}
.done-list .row { background: transparent; }
.done-list .item { background: transparent; margin-bottom: 0; }
.hint { color: var(--muted); font-size: 14px; }

.text-btn {
  min-height: 44px; padding: 0 8px; border: 0; background: none; color: var(--accent); font-weight: 600;
}
.danger { color: var(--danger); }

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

.toast {
  position: fixed; left: 12px; right: 12px; bottom: calc(env(safe-area-inset-bottom) + 14px); z-index: 50;
  display: flex; align-items: center; gap: 12px; max-width: 560px; margin: 0 auto;
  padding: 8px 8px 8px 16px; min-height: 52px; border-radius: var(--radius);
  background: #24302a; color: #fff; box-shadow: var(--shadow); font-size: 15px;
}
.toast .msg { flex: 1; }
.toast .action { min-height: 40px; padding: 0 12px; border: 0; border-radius: 10px; background: none; color: #8fe0b5; font-weight: 700; }

/* ---------- Menü ---------- */

.menu {
  position: fixed; z-index: 40; top: calc(env(safe-area-inset-top) + 54px); right: 10px;
  min-width: 240px; padding: 6px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.menu > button {
  display: block; width: 100%; min-height: 48px; padding: 0 12px; border: 0; border-radius: 10px;
  background: none; text-align: left;
}
.menu > button:active { background: var(--surface-2); }
.menu-group { padding: 8px 12px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--muted); }
.seg { display: flex; padding: 3px; border-radius: 10px; background: var(--surface-2); }
.seg button { flex: 1; min-height: 36px; border: 0; border-radius: 8px; background: none; font-size: 14px; }
.seg button[aria-checked="true"] { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, .15); font-weight: 600; }

/* ---------- Dialoge (Bottom-Sheets) ---------- */

dialog.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; margin: auto auto 0; padding: 18px 16px calc(env(safe-area-inset-bottom) + 14px);
  border: 0; border-radius: 20px 20px 0 0; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); overflow-y: auto;
  /* seitlich fest: nur senkrecht scrollen, kein seitliches Verschieben/Zurückfedern */
  overflow-x: hidden; overscroll-behavior: contain; touch-action: pan-y pinch-zoom;
}
dialog.sheet.small { margin: auto; border-radius: 20px; max-width: 420px; }
dialog::backdrop { background: rgba(10, 15, 12, .45); }
dialog:focus { outline: none; }
dialog h2 { margin: 0 0 12px; font-size: 19px; }
@media (min-width: 600px) { dialog.sheet { margin: auto; border-radius: 20px; } }

#edit-form { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px 10px; }
#edit-form h2, #edit-form .hint, #edit-form .actions, .field.wide { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; font-size: 13px; color: var(--muted); }
.field input, .field textarea {
  width: 100%; min-width: 0; max-width: 100%; min-height: 46px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); font-size: 16px;
}
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field textarea { resize: vertical; }
/* iOS gibt Datumsfeldern eine eigene Mindestbreite und Darstellung -> auf Spaltenbreite zwingen */
.field input[type="date"] { -webkit-appearance: none; appearance: none; display: block; text-align: left; }
.field input[type="date"]::-webkit-date-and-time-value { text-align: left; }

.actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.spacer { flex: 1; }
.btn {
  min-height: 46px; padding: 0 18px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-weight: 600;
}
.btn.primary { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.btn.danger-bg { border-color: var(--danger); background: var(--danger); color: #fff; }

.legacy-card { padding: 12px; margin-bottom: 10px; border: 1px solid var(--line); border-radius: var(--radius); }
.legacy-card .orig { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.legacy-card .grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
.legacy-card .grid .wide { grid-column: 1 / -1; }
.legacy-card .actions { margin-top: 10px; }

.info { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 8px; font-size: 15px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
