:root {
  color-scheme: light dark;

  /* Helle Palette (iOS/macOS "Insets Grouped" Look) */
  --bg: #f2f2f7;
  --elevated: #ffffff;
  --text: #000000;
  --text-secondary: #6e6e73;
  --separator: rgba(60, 60, 67, 0.29);
  --fill-secondary: rgba(120, 120, 128, 0.16);
  --tint: #000000;
  --tint-text: #ffffff;
  --danger: #ff3b30;
  --success: #34c759;
  --marker: #ffcc00;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --elevated: #1c1c1e;
    --text: #ffffff;
    --text-secondary: #98989d;
    --separator: rgba(84, 84, 88, 0.6);
    --fill-secondary: rgba(120, 120, 128, 0.28);
    --tint: #ffffff;
    --tint-text: #000000;
    --danger: #ff453a;
    --success: #30d158;
    --marker: #ffd60a;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  /* Die Seite selbst scrollt nicht mehr — #app ist der alleinige Scroll-Container (siehe dort).
     Zwei verschachtelte Scroll-Bereiche (Fenster UND #app) würden sich sonst gegenseitig
     Touch-Gesten streitig machen. */
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input { font-family: inherit; }

/* #app ist bewusst der Scroll-Container (statt der Seite/`window`): Wird während eines aktiven
   Touches manuell gescrollt (siehe attachRowGestures in app.js — nötig, damit Long-Press-zum-
   Umsortieren nicht mit Scrollen kollidiert), verhält sich `element.scrollTop`-Zuweisung auf
   einem gewöhnlichen `overflow-y: auto`-Element auf iOS deutlich zuverlässiger/ruckelfreier als
   `window.scrollBy` auf dem Dokument selbst (das dort mit iOS' eigener Viewport-Scroll-Physik
   kollidiert und zu sichtbarem Vor-und-Zurück-Wippen führt). */
#app {
  max-width: 600px;
  margin: 0 auto;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding-bottom: env(safe-area-inset-bottom);
}

.screen { padding: 0 18px calc(28px + env(safe-area-inset-bottom)); }

/* Auf breiten Fenstern (macOS) braucht es keinen iOS-Safe-Area-Abstand oben und
   wirkt ein etwas schmalerer, mittiger Inhalt eher wie ein natives Mac-Fenster. */
@media (min-width: 700px) {
  #app { max-width: 520px; }
  .top-bar { padding-top: 32px !important; }
  .auth-wrap { margin-top: 10vh !important; }
}

h1 { font-size: 1.2rem; margin: 0; font-weight: 600; }
.subtitle { color: var(--text-secondary); margin: 6px 0 0; font-size: 0.95rem; }

/* ---------- Auth ---------- */

.auth-wrap {
  margin-top: 16vh;
  text-align: center;
}

.auth-wrap h1 {
  font-size: 2.1rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 6px;
}

.auth-wrap .subtitle {
  margin-bottom: 36px;
  font-size: 1rem;
}

#auth-choice, #username-form { text-align: left; }

label { display: block; font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 6px; font-weight: 500; }

input[type="text"] {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: none;
  background: var(--elevated);
  box-shadow: inset 0 0 0 1px var(--separator);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 4px;
}

input[type="text"]:focus { outline: 2px solid var(--tint); outline-offset: 1px; }

.button-row { margin-top: 18px; display: flex; gap: 10px; }
.button-row button { flex: 1; width: auto; }

button { cursor: pointer; border: none; }

.primary, .secondary {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
}

.primary { background: var(--tint); color: var(--tint-text); }
.primary:active { opacity: 0.75; }

.secondary {
  background: transparent;
  color: var(--tint);
  box-shadow: inset 0 0 0 1.5px var(--separator);
}
.secondary:active { background: var(--fill-secondary); }

.divider {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin: 16px 0;
}

.hint { font-size: 0.8rem; color: var(--text-secondary); min-height: 1em; margin: 6px 0 0; }
.error { color: var(--danger); font-size: 0.88rem; margin-top: 16px; text-align: left; }

/* ---------- "Einkauf erledigt"-Feier (letztes Item abgehakt) ---------- */

.celebration {
  padding: 32px 12px 8px;
  text-align: center;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 400ms ease, transform 400ms ease;
}
.celebration.visible { opacity: 1; transform: translateY(0); }
.celebration-text { font-size: 1.25rem; font-weight: 700; margin: 0; }

.confetti-container {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 600;
}
.confetti-piece {
  position: absolute;
  top: -24px;
  width: 8px;
  height: 14px;
  border-radius: 2px;
  opacity: 0.95;
  animation-name: confetti-fall;
  animation-timing-function: cubic-bezier(0.35, 0, 0.65, 1);
  animation-fill-mode: forwards;
}
@keyframes confetti-fall {
  0% { transform: translate(0, -20px) rotate(var(--rotate-start, 0deg)); opacity: 1; }
  100% { transform: translate(var(--drift, 0px), 105vh) rotate(calc(var(--rotate-start, 0deg) + 540deg)); opacity: 0.85; }
}

.text-link {
  display: block;
  width: 100%;
  margin-top: 28px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.82rem;
  padding: 8px;
  text-align: center;
}
.text-link:active { color: var(--text); }

.settings-username-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.settings-username-row .subtitle { margin: 0; }
.change-username-button {
  flex-shrink: 0;
  background: transparent;
  color: var(--tint);
  font-size: 0.85rem;
  font-weight: 500;
  padding: 6px 2px;
}
.change-username-button:active { opacity: 0.6; }

/* ---------- Top bar / Große Titel ---------- */

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: calc(env(safe-area-inset-top) + 14px);
  padding-bottom: 6px;
}

#lists-screen > .top-bar {
  align-items: flex-end;
  padding-bottom: 14px;
}

#lists-screen h1 {
  font-size: 2.1rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

/* Detail-Ansicht: Kopfzeile (inkl. ＋-Button) bleibt beim Scrollen sichtbar */
#list-detail-screen > .top-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg);
  margin: 0 -18px;
  padding-left: 18px;
  padding-right: 18px;
}

.top-bar-actions { display: flex; gap: 4px; align-items: center; }

.icon-button {
  background: transparent;
  border: none;
  color: var(--tint);
  width: 40px;
  height: 40px;
  font-size: 1.35rem;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.icon-button:active { background: var(--fill-secondary); }

/* ---------- Listenübersicht (Insets Grouped) ---------- */

#lists-container {
  background: var(--elevated);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 0 0.5px var(--separator);
}

.list-summary-row {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: transparent;
  border: none;
  border-bottom: 0.5px solid var(--separator);
  padding: 14px 16px;
  text-align: left;
  font-size: 1.05rem;
  color: var(--text);
}

.list-summary-row:last-child { border-bottom: none; }
.list-summary-row:active { background: var(--fill-secondary); }

.list-summary-name { font-weight: 400; }
.list-summary-meta { font-size: 0.85rem; color: var(--text-secondary); white-space: nowrap; }

/* ---------- Detailansicht ---------- */

.detail-title { display: flex; align-items: center; gap: 4px; }
.detail-title h1 { font-size: 1.1rem; font-weight: 600; }
.detail-title .icon-button { width: 36px; height: 36px; font-size: 1.3rem; margin-right: 2px; }

.section-label {
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin: 22px 4px 6px;
}

.member-list, .item-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--elevated);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 0 0.5px var(--separator);
}

.member-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  border-bottom: 0.5px solid var(--separator);
  font-size: 1rem;
}

.member-row:last-child { border-bottom: none; }

/* Items werden per Grid-Höhe rein/raus animiert (An-/Abhaken, Realtime-Updates),
   ohne dass die Nachbar-Items hart "springen". */
.item-row {
  position: relative;
  display: grid;
  grid-template-rows: 1fr;
  border-bottom: 0.5px solid var(--separator);
  opacity: 1;
  overflow: hidden;
  transition: grid-template-rows 280ms cubic-bezier(0.4, 0, 0.2, 1),
    opacity 220ms ease,
    border-color 280ms ease;
}

.item-row:last-child { border-bottom: none; }

.item-row.collapsed {
  grid-template-rows: 0fr;
  opacity: 0;
  border-color: transparent;
}

.item-row-content {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  min-height: 0;
  overflow: hidden;
  background: var(--elevated);
}

.item-edit-button {
  flex: none;
  width: 30px;
  height: 30px;
  margin: -4px -6px -4px 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
}
.item-edit-button:active { background: var(--fill-secondary); }

/* Liegt hinter `.item-row-content` und wird sichtbar, sobald man das Item beim Swipe nach
   links zieht — zeigt an, dass Loslassen das Item als Standard-Item markiert/demarkiert. */
.item-swipe-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 24px;
  color: var(--marker);
  font-size: 1.15rem;
  pointer-events: none;
}

.member-name { flex: 1; }

.admin-badge, .creator-badge {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--fill-secondary);
  border-radius: 6px;
  padding: 3px 7px;
  white-space: nowrap;
}

.member-row-actions { display: flex; gap: 14px; }

.promote-button,
.kick-button,
.leave-button {
  background: transparent;
  padding: 0;
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
}

.promote-button { color: var(--tint); }
.kick-button, .leave-button { color: var(--danger); }
.promote-button:active, .kick-button:active, .leave-button:active { opacity: 0.6; }

.danger-link {
  display: block;
  margin: 28px auto 0;
  background: transparent;
  color: var(--danger);
  font-size: 0.95rem;
  font-weight: 500;
  padding: 8px;
}
.danger-link:active { opacity: 0.6; }

.item-check {
  width: 22px;
  height: 22px;
  min-width: 22px;
  border-radius: 50%;
  border: 1.5px solid var(--text-secondary);
  background: transparent;
  position: relative;
  transition: background 200ms ease, border-color 200ms ease;
}

.item-check::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 9px;
  height: 5px;
  border-left: 2px solid var(--tint-text);
  border-bottom: 2px solid var(--tint-text);
  transform: translate(-50%, -65%) rotate(-45deg) scale(0);
  transition: transform 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Grüner Haken beim Abhaken: kurze "pop"-Animation, danach 2s stehen bleiben,
   bevor das Item per .collapsed rausanimiert wird (siehe app.js). */
.item-row.is-checked .item-check {
  background: var(--success);
  border-color: var(--success);
}

.item-row.is-checked .item-check::after {
  transform: translate(-50%, -65%) rotate(-45deg) scale(1);
}

.item-row.is-checked .item-name {
  color: var(--text-secondary);
  text-decoration: line-through;
  transition: color 200ms ease;
}

.item-name { flex: 1; font-size: 1rem; }
.item-added-by { font-size: 0.78rem; color: var(--text-secondary); }

/* Standard-Item-Marker: gelber Punkt am rechten Rand der Zeile, gesetzt per Gedrückthalten. */
.item-default-marker {
  display: inline-block;
  width: 9px;
  height: 9px;
  min-width: 9px;
  border-radius: 50%;
  background: var(--marker);
}

/* ---------- Abteilungen ---------- */

.section-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 0 6px;
}
.section-label-row .section-label { margin: 0; }
.section-label-row .icon-button { width: 30px; height: 30px; font-size: 1rem; }

/* Analog zum Höhe-0-zu-auto-Trick bei .item-row: eine neu hinzugekommene Abteilung (z. B. per
   Sprachassistent) klappt sanft auf statt einfach zu erscheinen. `.department-section-inner`
   trägt das Overflow-Clipping, `.department-section` selbst die animierte Grid-Zeile. */
.department-section {
  display: grid;
  grid-template-rows: 1fr;
  opacity: 1;
  margin-bottom: 16px;
  transition: grid-template-rows 320ms cubic-bezier(0.4, 0, 0.2, 1),
    opacity 260ms ease,
    margin-bottom 320ms cubic-bezier(0.4, 0, 0.2, 1);
}
.department-section-inner { overflow: hidden; min-height: 0; }
.department-section.section-collapsed {
  grid-template-rows: 0fr;
  opacity: 0;
  margin-bottom: 0;
}
.department-section-title { margin: 22px 4px 6px; }
.department-section:first-child .department-section-title { margin-top: 0; }

/* Kurzes, sanftes Aufleuchten (kein hartes Blitzen) als Hinweis, dass die KI gerade z. B. den
   Namen eines Items geändert hat. */
.item-row.flash-update .item-row-content {
  animation: flash-update-pulse 900ms ease-out;
}
@keyframes flash-update-pulse {
  0% { background: color-mix(in srgb, var(--tint) 16%, var(--elevated)); }
  100% { background: var(--elevated); }
}

/* ---------- Drag & Drop (Items & Abteilungen) ---------- */

/* Ganze Zeile ist der Drag-Griff (nicht nur ein Handle-Icon). `touch-action: pan-y` lässt den
   Browser vertikal nativ scrollen (mit iOS-Momentum); Long-Press-zum-Draggen und der horizontale
   Swipe werden in attachRowGestures (app.js) erkannt, solange der Finger ruhig liegt bzw. sich
   horizontal bewegt. Nur Maus-Drags scrollen dort noch manuell. `user-select`/
   `-webkit-touch-callout` verhindern, dass iOS/Android beim Halten stattdessen den Text
   markieren bzw. das Auswahl-/Kopieren-Menü einblenden. */
.draggable-row {
  /* pan-y: vertikales Scrollen übernimmt der Browser nativ (Momentum/Rubber-Banding wie in iOS);
     horizontale Gesten (Swipe) und Long-Press landen weiter bei unserem JS. */
  touch-action: pan-y;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.draggable-row:active { cursor: grabbing; }

/* Während eines aktiven Links-Swipes (Standard-Item markieren) soll kein "grab"-Cursor stören. */
.item-row.swiping { cursor: default; }

.item-row.dragging,
.member-row.dragging {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
  border-radius: 10px;
  background: var(--elevated);
  opacity: 0.96;
}

.drag-placeholder {
  border: 1.5px dashed var(--separator);
  border-radius: 8px;
  background: var(--fill-secondary);
  margin: 4px 8px;
  box-sizing: border-box;
}

/* ---------- Account-Bereich ---------- */

.account-section {
  margin-top: 32px;
  text-align: center;
}

/* ---------- Eingabe-Sheet (ersetzt native prompt()-Dialoge) ---------- */

.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  opacity: 0;
  transition: opacity 220ms ease;
}

.sheet-panel {
  position: relative;
  width: 100%;
  max-width: 600px;
  background: var(--elevated);
  border-radius: 0 0 16px 16px;
  padding: calc(14px + env(safe-area-inset-top)) 18px 18px;
  box-sizing: border-box;
  transform: translateY(-100%);
  transition: transform 260ms cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
}

.sheet-overlay.open .sheet-backdrop { opacity: 1; }
.sheet-overlay.open .sheet-panel { transform: translateY(0); }

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 10px;
}

.sheet-header h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
}

.sheet-header .text-link {
  margin: 0;
  width: auto;
  padding: 4px 2px;
  font-size: 0.95rem;
}

.sheet-submit { color: var(--tint); font-weight: 600; }

#sheet-input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: none;
  background: var(--bg);
  box-shadow: inset 0 0 0 1px var(--separator);
  color: var(--text);
  font-size: 1rem;
  box-sizing: border-box;
}
#sheet-input:focus { outline: 2px solid var(--tint); outline-offset: 1px; }

.sheet-suggestions {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  max-height: 45vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 12px;
  box-shadow: inset 0 0 0 1px var(--separator);
}

.sheet-suggestion-row {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  padding: 11px 14px;
  font-size: 0.98rem;
  color: var(--text);
  border-bottom: 0.5px solid var(--separator);
}
.sheet-suggestions li:last-child .sheet-suggestion-row { border-bottom: none; }
.sheet-suggestion-row:active { background: var(--fill-secondary); }

/* ---------- Toggle-Switch (z. B. Mikro-Modus) ---------- */

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 2px 4px;
  font-size: 1rem;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 30px;
  flex: none;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}

.switch-track {
  position: absolute;
  inset: 0;
  background: var(--fill-secondary);
  border-radius: 999px;
  transition: background 180ms ease;
  pointer-events: none;
}

.switch-track::before {
  content: "";
  position: absolute;
  left: 2px;
  top: 2px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: transform 180ms ease;
}

.switch input:checked + .switch-track { background: var(--success); }
.switch input:checked + .switch-track::before { transform: translateX(20px); }

/* ---------- Mikro-Modus (Push-to-Talk) ---------- */

.mic-button {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--tint);
  color: var(--tint-text);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
  z-index: 500;
  touch-action: none;
}

.mic-button.recording {
  background: var(--danger);
  animation: mic-pulse 1.2s ease-in-out infinite;
}

@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 59, 48, 0.45); }
  50% { box-shadow: 0 0 0 16px rgba(255, 59, 48, 0); }
}

.mic-status {
  position: fixed;
  bottom: calc(96px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--elevated);
  color: var(--text);
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
  z-index: 500;
  white-space: nowrap;
}

/* Während die Sprachaufnahme verarbeitet wird (Transkription + KI-Aktionsplanung), ersetzt diese
   Lade-Animation genau an der Stelle des Mikro-Buttons das Mikro-Symbol (Button wird ausgeblendet,
   Spinner erscheint, danach wieder umgekehrt) — Text erscheint nur noch im Fehlerfall (als Toast,
   siehe showToast in app.js). */
.mic-spinner {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--tint);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
  z-index: 500;
}

.mic-spinner-ring {
  width: 22px;
  height: 22px;
  border: 2.5px solid rgba(255, 255, 255, 0.35);
  border-top-color: var(--tint-text);
  border-radius: 50%;
  animation: mic-spin 0.7s linear infinite;
}

@keyframes mic-spin {
  to { transform: rotate(360deg); }
}

[hidden] { display: none !important; }
