:root {
  --bg: #f2f1ec;
  --surface: #ffffff;
  --surface-2: #fbfaf7;
  --text: #1c1c1a;
  --text-2: #6b6a64;
  --text-3: #9b9a92;
  --border: rgba(0,0,0,.08);
  --border-strong: rgba(0,0,0,.18);
  --accent: #007aff;
  --accent-bg: #e6f1fb;
  --danger: #e24b4a;
  --success: #1d9e75;
  --success-bg: #e1f5ee;
  --success-fg: #0f6e56;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514;
    --surface: #232220;
    --surface-2: #2b2a27;
    --text: #f2f1ec;
    --text-2: #a8a79f;
    --text-3: #78776f;
    --border: rgba(255,255,255,.10);
    --border-strong: rgba(255,255,255,.22);
    --accent: #0a84ff;
    --accent-bg: #12314e;
    --danger: #ff5a58;
    --success: #34c79a;
    --success-bg: #123a30;
    --success-fg: #6fe0c0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior-y: none;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg);
}

/* Kopfzeile */
.topbar {
  padding: calc(var(--safe-top) + 12px) 20px 8px;
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
}
.titles { flex: 1; min-width: 0; }
.avatar {
  flex: 0 0 auto; width: 38px; height: 38px; margin-top: 4px;
  border-radius: 50%; border: none; cursor: pointer; font-family: inherit;
  background: var(--accent-bg); color: var(--accent); font-size: 16px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.avatar:active { transform: scale(.95); }
.titles h1 { margin: 0; font-size: 28px; font-weight: 600; letter-spacing: -.5px; }
.status { margin: 4px 0 0; font-size: 13px; color: var(--text-3); display: flex; align-items: center; gap: 6px; }
.live { display: inline-flex; align-items: center; gap: 5px; color: var(--text-2); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); transition: background .3s; }
.live.online .dot { background: var(--success); animation: pulse 1.8s infinite; }
.live.offline .dot { background: var(--danger); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(29,158,117,.45); }
  70% { box-shadow: 0 0 0 6px rgba(29,158,117,0); }
  100% { box-shadow: 0 0 0 0 rgba(29,158,117,0); }
}
.sep { color: var(--text-3); }

/* Liste */
.list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 14px 24px;
}

.sechead { display: flex; align-items: center; gap: 10px; padding: 12px 4px 8px; }
.sechead .tile {
  width: 28px; height: 28px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center; font-size: 16px; flex: 0 0 auto;
}
.sechead h2 { margin: 0; font-size: 15px; font-weight: 600; color: var(--text); }
.sechead .n { margin-left: auto; font-size: 14px; color: var(--text-3); padding-right: 4px; }

.sec { background: var(--surface); border-radius: 14px; overflow: hidden; border: 0.5px solid var(--border); }

.row {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 4px 4px 14px; background: var(--surface);
  border-top: 0.5px solid var(--border); cursor: pointer;
  -webkit-user-select: none; user-select: none;
}
.sec .row:first-child { border-top: none; }
.row:active { background: var(--surface-2); }
.row .label { font-size: 16px; color: var(--text); word-break: break-word; padding: 9px 0; }
.row .qty { margin-left: auto; font-size: 14px; color: var(--text-3); flex: 0 0 auto; }
.row.done .label { color: var(--text-3); text-decoration: line-through; }
.row.flash { animation: flash 1.6s ease; }
@keyframes flash { 0%, 45% { background: var(--accent-bg); } 100% { background: var(--surface); } }
.more {
  margin-left: auto; flex: 0 0 auto; width: 44px; height: 44px; padding: 0;
  border: none; background: transparent; color: var(--text-3); font-size: 20px;
  display: flex; align-items: center; justify-content: center; cursor: pointer; border-radius: 8px;
}
.row .qty + .more { margin-left: 4px; }
.more:hover { color: var(--text-2); }
.more:active { background: var(--surface-2); }

/* Bearbeiten-Overlay (Bottom-Sheet) */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: flex-end;
  background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .2s ease;
}
.sheet-backdrop.show { opacity: 1; pointer-events: auto; }
.sheet {
  width: 100%; max-width: 640px; margin: 0 auto;
  background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 8px 20px calc(20px + var(--safe-bottom));
  transform: translateY(100%); transition: transform .25s ease;
}
.sheet-backdrop.show .sheet { transform: translateY(0); }
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: var(--border-strong); margin: 6px auto 12px; }
.sheet-title { margin: 0 0 16px; font-size: 18px; font-weight: 600; text-align: center; word-break: break-word; }
.sheet-meta { margin: -12px 0 16px; text-align: center; font-size: 13px; color: var(--text-3); }
.sheet-label { display: block; font-size: 13px; color: var(--text-2); margin: 0 0 6px; }
.sheet-qty, .sheet-name {
  width: 100%; height: 44px; font-size: 16px; font-family: inherit;
  color: var(--text); background: var(--surface-2);
  border: 0.5px solid var(--border); border-radius: 10px; padding: 0 12px; margin: 0 0 16px;
}
.sheet-qty:focus, .sheet-name:focus { outline: none; border-color: var(--accent); }
/* Namensfeld nur im Hinzufügen-Modus, Löschen nur im Bearbeiten-Modus */
.sheet:not(.add-mode) .sheet-name,
.sheet:not(.add-mode) .sheet-name-label { display: none; }
.sheet.add-mode .sheet-btn.danger { display: none; }
.sheet-cat {
  border: 0.5px solid var(--border); border-radius: 12px; overflow: hidden;
  max-height: 250px; overflow-y: auto; -webkit-overflow-scrolling: touch; margin: 0 0 18px;
}
.catrow {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  border-top: 0.5px solid var(--border); cursor: pointer; background: var(--surface-2);
}
.catrow:first-child { border-top: none; }
.catrow:active { background: var(--surface-1); }
.catrow .tile {
  width: 30px; height: 30px; border-radius: 8px; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-size: 17px;
}
.catrow .catname { font-size: 16px; color: var(--text); }
.catrow .catcheck { margin-left: auto; color: var(--accent); font-size: 19px; visibility: hidden; }
.catrow.sel .catcheck { visibility: visible; }
.catrow.sel { background: var(--accent-bg); }
.sheet-actions { display: flex; gap: 10px; margin-top: 4px; }
.sheet-btn {
  flex: 1; height: 46px; border-radius: 12px; font-size: 16px; font-weight: 500;
  font-family: inherit; cursor: pointer; border: 0.5px solid var(--border);
}
.sheet-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.sheet-btn.danger { background: transparent; color: var(--danger); border-color: var(--border); }
.sheet-btn:active { transform: scale(.98); }

/* Gekauft-Bereich */
.boughthead { display: flex; align-items: center; gap: 8px; padding: 12px 4px 8px 6px; cursor: pointer; }
.boughthead h2 { margin: 0; font-size: 15px; font-weight: 600; color: var(--text-2); }
.boughthead .chev { color: var(--text-3); font-size: 17px; transition: transform .2s; flex: 0 0 auto; }
.boughthead.closed .chev { transform: rotate(-90deg); }
.bought-more {
  margin-left: auto; flex: 0 0 auto; width: 40px; height: 40px; padding: 0;
  border: none; background: transparent; color: var(--text-3); font-size: 20px;
  display: flex; align-items: center; justify-content: center; cursor: pointer; border-radius: 8px;
}
.bought-more:active { background: var(--surface); color: var(--text-2); }

/* Kontextmenü (Popover) */
.ctx-backdrop { position: fixed; inset: 0; z-index: 50; display: none; }
.ctx-backdrop.show { display: block; }
.ctx-menu {
  position: fixed; min-width: 210px; background: var(--surface);
  border: 0.5px solid var(--border); border-radius: 12px; padding: 4px;
  box-shadow: 0 10px 30px rgba(0,0,0,.20);
}
.ctx-item {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 11px 12px; background: transparent; border: none; border-radius: 8px;
  font-size: 15px; font-family: inherit; color: var(--text); cursor: pointer;
}
.ctx-item i { font-size: 18px; }
.ctx-item:active { background: var(--surface-2); }
.ctx-item.danger { color: var(--danger); }

/* Leerzustand */
.empty { text-align: center; padding: 56px 24px 40px; }
.empty .big {
  width: 74px; height: 74px; border-radius: 50%;
  background: var(--success-bg); color: var(--success-fg);
  display: flex; align-items: center; justify-content: center; font-size: 36px; margin: 0 auto 16px;
}
.empty h2 { margin: 0 0 6px; font-size: 20px; font-weight: 600; }
.empty p { margin: 0; font-size: 14px; color: var(--text-2); line-height: 1.5; }

/* Skeleton beim Laden */
.skeleton { padding: 20px 4px; color: var(--text-3); font-size: 14px; text-align: center; }

/* Hinzufügen-Leiste */
.addbar {
  padding: 10px 16px calc(10px + var(--safe-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-top: 0.5px solid var(--border);
}
.addtrigger {
  width: 100%; height: 48px; border-radius: 14px; border: none; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 16px; font-weight: 500; font-family: inherit;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.addtrigger:active { transform: scale(.99); }
.addtrigger .plus {
  width: 22px; height: 22px; border-radius: 50%; flex: 0 0 auto;
  background: rgba(255,255,255,.22); color: #fff; font-size: 15px;
  display: flex; align-items: center; justify-content: center;
}

/* Toast für Live-Updates */
.toast-host {
  position: fixed; left: 0; right: 0; bottom: calc(76px + var(--safe-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none; z-index: 20; padding: 0 16px;
}
.toast {
  width: 100%; max-width: 380px;
  background: var(--surface); border: 0.5px solid var(--border); border-radius: 12px;
  padding: 11px 14px; display: flex; align-items: center; gap: 10px;
  font-size: 14px; color: var(--text);
  box-shadow: 0 8px 28px rgba(0,0,0,.16);
  transform: translateY(16px); opacity: 0; transition: all .25s ease;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast .ic {
  width: 26px; height: 26px; border-radius: 50%; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
  background: var(--accent-bg); color: var(--accent);
}

/* Anmeldebildschirm */
.login {
  position: fixed; inset: 0; z-index: 60; display: none;
  align-items: center; justify-content: center;
  padding: calc(24px + var(--safe-top)) 24px calc(24px + var(--safe-bottom));
  background: var(--bg);
}
.login.show { display: flex; }
.login-card { width: 100%; max-width: 360px; }
.login-logo {
  width: 60px; height: 60px; border-radius: 16px; margin: 0 auto 16px;
  background: var(--accent-bg); color: var(--accent);
  display: flex; align-items: center; justify-content: center; font-size: 30px;
}
.login-card h1 { margin: 0; font-size: 26px; font-weight: 600; text-align: center; letter-spacing: -.5px; }
.login-sub { margin: 6px 0 26px; text-align: center; color: var(--text-2); font-size: 14px; }
.login-card label { display: block; font-size: 13px; color: var(--text-2); margin: 0 0 6px; }
.login-card input {
  width: 100%; height: 46px; font-size: 16px; font-family: inherit; color: var(--text);
  background: var(--surface); border: 0.5px solid var(--border); border-radius: 12px;
  padding: 0 14px; margin: 0 0 16px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-error { margin: -6px 0 14px; color: var(--danger); font-size: 13px; text-align: center; }
.login-btn {
  width: 100%; height: 48px; border: none; border-radius: 14px;
  background: var(--accent); color: #fff; font-size: 16px; font-weight: 500;
  font-family: inherit; cursor: pointer;
}
.login-btn:active { transform: scale(.99); }
