:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e3e6ea;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --danger: #b91c1c;
  --ok: #15803d;
  --warn: #b45309;
  --soon: #c2410c;
  --shadow: 0 6px 24px rgba(16, 24, 40, .10);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #191d23;
    --surface-2: #222730;
    --text: #e9edf2;
    --muted: #9aa4b2;
    --border: #2b313a;
    --primary: #2dd4bf;
    --primary-ink: #06201d;
    --danger: #f87171;
    --ok: #4ade80;
    --warn: #fbbf24;
    --soon: #fb923c;
    --shadow: 0 8px 28px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
  max-width: 760px;
  margin: 0 auto;
  min-height: 100dvh;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 8px; }
.brand h1 { font-size: 1.1rem; margin: 0; letter-spacing: -.01em; }
.brand p { margin: 0; font-size: .78rem; color: var(--muted); }
.header-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: inline-grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 6px;
  padding: 4px 16px 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
}
.tab {
  flex: 1;
  padding: 10px 8px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
}
.tab.is-active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-count {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  margin-left: 4px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: .74rem;
  line-height: 18px;
}
.tab.is-active .tab-count { background: var(--primary); color: var(--primary-ink); }

main { padding: 14px 16px 96px; }
.panel[hidden] { display: none; }

/* ---------- Stats ---------- */
.stats {
  display: grid;
  /* Immer vier nebeneinander, auch auf dem Handy */
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  min-width: 0;
}
.stat:hover { background: var(--surface-2); }
.stat:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.stat[aria-pressed="true"] { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); background: var(--surface-2); }
.stat b { display: block; font-size: 1.25rem; line-height: 1.2; }
.stat span { display: block; font-size: .76rem; color: var(--muted); line-height: 1.25; }
@media (max-width: 420px) {
  .stats { gap: 6px; }
  .stat { padding: 8px 8px; border-radius: 12px; }
  .stat b { font-size: 1.1rem; }
  .stat span { font-size: .7rem; }
}
.stat.alert b { color: var(--danger); }
.stat.warn b { color: var(--soon); }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
#search {
  width: 100%;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}
#search:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
}
.filters select,
.inline-form select,
.field select {
  width: 100%;
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: .88rem;
}

/* ---------- List ---------- */
.list { display: flex; flex-direction: column; gap: 8px; }
.card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.card.is-expired { border-left-color: var(--danger); }
.card.is-soon { border-left-color: var(--soon); }
.card.is-ok { border-left-color: var(--ok); }
.card.is-nodate { border-left-color: var(--muted); }

.card-main { flex: 1; min-width: 0; cursor: pointer; }
.card-main:focus-visible,
.tab:focus-visible,
.step:focus-visible,
.icon-btn:focus-visible,
.btn:focus-visible,
.fab:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
.card-main:focus-visible { border-radius: 8px; }
.card-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.card-title strong { font-size: .98rem; }
.card-title .qty { font-size: .82rem; color: var(--muted); }
.card-meta {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 4px;
  font-size: .74rem;
  color: var(--muted);
}
.pill {
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 600;
}
.badge.expired { background: rgba(185, 28, 28, .14); color: var(--danger); }
.badge.today { background: rgba(194, 65, 12, .16); color: var(--soon); }
.badge.soon { background: rgba(180, 83, 9, .16); color: var(--warn); }
.badge.ok { background: rgba(21, 128, 61, .14); color: var(--ok); }
.badge.none { background: var(--surface-2); color: var(--muted); }

.card-actions { display: flex; gap: 6px; flex-shrink: 0; }
.step {
  width: 34px; height: 34px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
  line-height: 1;
}
.step:hover { background: var(--border); }
.step.danger:hover { background: rgba(185, 28, 28, .15); color: var(--danger); border-color: var(--danger); }
.step.go:hover { background: rgba(15, 118, 110, .15); color: var(--primary); border-color: var(--primary); }

/* shopping list */
.shop-item .card-title strong.checked { text-decoration: line-through; color: var(--muted); }
.shop-item.is-done { opacity: .65; }

/* ---------- Empty / misc ---------- */
.empty {
  text-align: center;
  color: var(--muted);
  padding: 36px 16px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.list-actions { margin-top: 10px; text-align: right; }

.inline-form {
  display: grid;
  grid-template-columns: 1fr 80px;
  gap: 8px;
  margin-bottom: 12px;
}
.inline-form .ta-shopping { grid-column: 1 / -1; }
.inline-form #shopping-unit { grid-column: 1; }
.inline-form .btn { grid-column: 2; }
.inline-form input {
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}

/* ---------- Buttons ---------- */
.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: .92rem;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-ghost { background: transparent; }

.fab {
  position: fixed;
  right: max(18px, calc(50vw - 380px + 18px));
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 58px; height: 58px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--primary-ink);
  display: grid;
  place-items: center;
  padding: 0;
  box-shadow: var(--shadow);
  cursor: pointer;
  z-index: 30;
}
.fab:active { transform: scale(.95); }
.fab[hidden] { display: none; }

/* ---------- Dialog ---------- */
dialog {
  border: none;
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  width: min(560px, calc(100vw - 24px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); backdrop-filter: blur(2px); }
.modal-form { padding: 18px; }
.modal-form h2 { margin: 0 0 14px; font-size: 1.1rem; }

.grid-2 {
  display: grid;
  align-items: end;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field span { font-size: .78rem; font-weight: 600; color: var(--muted); }
.field input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
}
.field input:focus, .field select:focus, #search:focus, .filters select:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
.field .hint { font-weight: 500; color: var(--warn); margin-left: 4px; }
.field select { font-size: 1rem; }
.field input, .field select { box-sizing: border-box; height: 44px; }
.date-field { position: relative; display: flex; gap: 6px; }
.date-field input[type="text"] { flex: 1; min-width: 0; }
.date-field .icon-btn { height: 44px; width: 44px; flex: none; }
/* Unsichtbar, aber vorhanden, damit showPicker() den Kalender am Feld öffnet */
.date-picker { position: absolute; right: 0; bottom: 0; width: 44px; height: 44px; opacity: 0; pointer-events: none; }
.more { margin-bottom: 12px; }
.more summary { cursor: pointer; font-size: .85rem; color: var(--muted); padding: 4px 0 10px; }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 6px; }
.modal-actions .spacer { flex: 1; }
/* Beim Erfassen ist „Speichern & weiter“ der Hauptweg – eigene Zeile, volle Breite */
#btn-save-next { flex: 1 1 100%; padding: 12px 16px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: .88rem;
  opacity: 0;
  pointer-events: none;
  transition: .22s ease;
  z-index: 50;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (min-width: 600px) {
  .inline-form { grid-template-columns: 1fr 90px 130px auto; }
  .inline-form .ta-shopping { grid-column: 1; }
  .inline-form #shopping-unit { grid-column: 3; }
  .inline-form .btn { grid-column: 4; }
}

/* ---------- Notvorrat ---------- */
.field[hidden] { display: none; }

/* ---------- Anmeldung & Sync ---------- */
#login-dialog .muted { margin: 0 0 14px; font-size: .88rem; }
.login-error { margin: 10px 0 0; color: var(--danger); font-size: .88rem; }
.btn-block { display: block; width: 100%; }
#subtitle.is-offline { color: var(--warn); }

.supply-settings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 12px;
  align-items: end;
  margin-bottom: 4px;
}
.supply-settings .check { grid-column: 1 / -1; }
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: .9rem;
  cursor: pointer;
}
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.check small { color: var(--muted); }

.supply-hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
}
.hero-figure { display: flex; align-items: baseline; gap: 8px; }
.hero-figure b { font-size: 3rem; line-height: 1; letter-spacing: -.02em; }
.hero-figure span { color: var(--muted); font-size: .95rem; }
.supply-hero p { margin: 8px 0 0; font-size: .9rem; }

.supply-groups { display: flex; flex-direction: column; gap: 8px; }
.supply-row {
  --status: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.supply-row.is-ok { --status: var(--ok); }
.supply-row.is-warn { --status: var(--warn); }
.supply-row.is-danger { --status: var(--danger); }
.supply-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.supply-head strong { font-size: .92rem; }
.supply-val { font-size: .82rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Spur ist eine hellere Stufe derselben Statusfarbe */
.meter {
  height: 8px;
  margin: 8px 0 6px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--status) 18%, var(--surface-2));
  overflow: hidden;
}
.meter > span {
  display: block;
  height: 100%;
  min-width: 0;
  border-radius: 4px;
  background: var(--status);
}
.supply-foot {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  font-size: .76rem;
  color: var(--muted);
}
.status-tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-size: .74rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.status-tag.ok { border-color: var(--ok); }
.status-tag.warn { border-color: var(--warn); }
.status-tag.danger { border-color: var(--danger); }
.warn-text { color: var(--text); }

.supply-notes h3 { font-size: .9rem; margin: 18px 0 6px; }
.supply-notes ul { margin: 0; padding-left: 18px; font-size: .88rem; }
.supply-notes li { margin: 3px 0; }
.supply-notes p { font-size: .85rem; }
.muted { color: var(--muted); }
.link {
  padding: 0;
  border: none;
  background: none;
  color: var(--primary);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}
.source { margin-top: 20px; font-size: .74rem; color: var(--muted); }
.pill-supply { border-color: var(--primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Drei Tabs auf schmalen Displays: Zähler unter die Beschriftung */
@media (max-width: 420px) {
  .tab { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .88rem; padding: 8px 4px; }
  .tab-count { margin-left: 0; }
}

/* ---------- Vorschläge beim Tippen ---------- */
.typeahead { position: relative; }
.typeahead input { width: 100%; box-sizing: border-box; }
.ta-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 20;
  margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); max-height: 280px; overflow-y: auto;
}
.ta-list[hidden] { display: none; }
.ta-item { display: flex; align-items: center; gap: 8px; padding: 8px 6px 8px 10px; border-radius: 8px; cursor: pointer; }
.ta-item.is-active, .ta-item:hover { background: var(--surface-2); }
.ta-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ta-meta { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 45%; }
.ta-forget {
  flex: none; width: 28px; height: 28px; border: 0; border-radius: 6px;
  background: transparent; color: var(--muted); font-size: 1.1rem; line-height: 1; cursor: pointer;
}
.ta-forget:hover { background: var(--border); color: var(--danger); }

/* ---------- Lagerorte ---------- */
#locations-dialog .muted { margin: 0 0 14px; font-size: .88rem; }
.loc-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.loc-row { display: grid; grid-template-columns: 1fr 120px auto auto auto; gap: 6px; align-items: center; }
.loc-row input, .loc-row select {
  box-sizing: border-box; height: 40px; min-width: 0; padding: 6px 10px; font-size: 1rem;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.loc-row .icon-btn { width: 36px; height: 36px; }
.loc-row .icon-btn:disabled { opacity: .35; cursor: default; }
@media (max-width: 420px) {
  /* Als Karte: Name und Kühlung links untereinander, Knöpfe rechts daneben */
  .loc-row {
    grid-template-columns: 1fr auto auto auto;
    padding: 8px; border: 1px solid var(--border); border-radius: 12px; row-gap: 6px;
  }
  .loc-row select { grid-column: 1; grid-row: 2; height: 36px; font-size: .9rem; }
  .loc-row .icon-btn { grid-row: 1 / span 2; align-self: center; width: 34px; height: 34px; }
}

/* ---------- Scanner ---------- */
.name-row { display: flex; gap: 8px; align-items: stretch; }
.name-row .typeahead { flex: 1; min-width: 0; }
.scan-btn { flex: none; width: 44px; height: auto; min-height: 44px; }
.field > label { font-size: .78rem; font-weight: 600; color: var(--muted); }
.ean-tag { font-weight: 500; margin-left: 4px; color: var(--muted); font-variant-numeric: tabular-nums; }
.scan-view {
  position: relative; aspect-ratio: 4 / 3; margin-bottom: 10px;
  background: #000; border-radius: 12px; overflow: hidden;
}
.scan-view[hidden] { display: none; }
.scan-view video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame {
  position: absolute; inset: 32% 8%;
  border: 2px solid rgba(255, 255, 255, .9); border-radius: 10px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, .28);
}
#scan-dialog .muted { margin: 0 0 12px; font-size: .88rem; }
/* Die Liste liegt im .field – dessen Label-Stil (.field span) nicht erben */
.field .ta-name { font-size: 1rem; font-weight: 400; color: var(--text); }
.field .ta-meta { font-size: .78rem; font-weight: 400; color: var(--muted); }

/* ---------- Verlauf ---------- */
.history-toolbar { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-bottom: 12px; }
.history-toolbar input, .history-toolbar select {
  box-sizing: border-box; height: 44px; padding: 0 12px; font-size: 1rem; min-width: 0;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.history-note { margin: 0 0 10px; font-size: .85rem; }
.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.hist-day { margin: 10px 2px 2px; font-size: .78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.hist-day:first-child { margin-top: 0; }
.hist-entry {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.hist-sign {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; line-height: 1;
  background: var(--surface-2); color: var(--muted);
}
.hist-in .hist-sign { color: var(--ok); }
.hist-out .hist-sign { color: var(--soon); }
.hist-delete .hist-sign { color: var(--danger); }
.hist-main { min-width: 0; }
.hist-main strong { overflow-wrap: anywhere; }
.hist-main .muted { font-size: .85rem; }
.hist-meta { margin-top: 2px; font-size: .78rem; color: var(--muted); }
.hist-pending { color: var(--warn); }
@media (max-width: 420px) {
  .tabs { gap: 2px; padding: 4px 8px 0; }
  .tab { padding: 10px 2px; font-size: .84rem; }
  .tab-count { margin-left: 2px; padding: 0 5px; }
}

/* ---------- Packungen (gruppierte Einträge) ---------- */
.card.group { flex-wrap: wrap; }
.card.group > .card-main { flex: 1; min-width: 0; }
.card.group .pill-count { font-weight: 600; }
.lots { flex-basis: 100%; border-top: 1px dashed var(--border); margin-top: 8px; padding-top: 8px; }
.lots[hidden] { display: none; }
.lot-list { list-style: none; margin: 0 0 6px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lot { display: flex; align-items: center; gap: 8px; }
.lot-main {
  flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.lot-qty { font-weight: 600; }
.lot-actions { display: flex; gap: 6px; }
.lot-history { font-size: .85rem; }

/* ---------- Verlauf pro Produkt ---------- */
.history-exact {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin: -4px 0 10px; padding: 6px 6px 6px 12px;
  border: 1px solid var(--primary); border-radius: 12px; background: var(--surface);
}
.history-exact[hidden] { display: none; }
.history-exact .icon-btn { width: 32px; height: 32px; }
.history-summary { margin: 0 0 12px; font-size: .88rem; }
.hist-name { font-weight: 700; color: var(--text); text-decoration: none; padding: 0; }

.modal-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.modal-head h2 { margin: 0; }
.modal-head .link { font-size: .9rem; white-space: nowrap; }

.loc-orphans { margin: 0 0 12px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--warn); font-size: .88rem; }

/* Gesamtmenge hervorheben */
.qty-total { font-weight: 700; color: var(--text); }

/* Summenzeile über der Bestandsliste */
.inventory-summary { margin: 0 2px 10px; font-size: .88rem; color: var(--text); }
.inventory-summary[hidden] { display: none; }
.inventory-summary b { font-weight: 700; }
