:root {
  color-scheme: light dark;
  --green: #2f9e64;
  --green-dark: #24824f;
  --green-tint: rgba(47, 158, 100, 0.12);
  --bg: #f5f4f0;
  --card-bg: #ffffff;
  --text: #1c1c1c;
  --text-muted: #6b6b6b;
  --border: #e5e3dc;
  --danger: #d64545;
  --high: #d64545;
  --low: #8a8a8a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --card-bg: #232427;
    --text: #f0f0ef;
    --text-muted: #9a9a9a;
    --border: #34363a;
    --green-tint: rgba(47, 158, 100, 0.22);
    --shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select {
  font: inherit;
  color: inherit;
}

.hidden { display: none !important; }

/* --- Login --- */

#loginView {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
  text-align: center;
}

.login-logo img {
  width: 72px;
  height: 72px;
  border-radius: 18px;
  margin-bottom: 4px;
}

.login-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
}

.login-card input {
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 16px;
}

.login-card button {
  padding: 14px;
  border-radius: 12px;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.error {
  color: var(--danger);
  font-size: 14px;
  margin: 0;
}

.hint {
  color: var(--text-muted);
  font-size: 13px;
  margin: 4px 0 12px;
}

.hint.success { color: var(--green); }

/* --- App shell --- */

#appShell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 12px) 12px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  flex: 1;
  font-size: 19px;
  margin: 0;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  font-size: 22px;
  line-height: 1;
}

.icon-btn:active { background: var(--border); }

#backBtn { font-size: 28px; font-weight: 600; }

main#content {
  flex: 1;
  padding: 12px 12px calc(env(safe-area-inset-bottom) + 84px);
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 0 8px;
  border: none;
  background: none;
  color: var(--text-muted);
}

.tab-btn.active { color: var(--green); }
.tab-btn span { font-size: 11px; }

/* --- Shopping list groups/items --- */

.store-group { margin-bottom: 20px; }

.store-group-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--green-dark);
  margin: 0 0 8px 4px;
}

.dept-group { margin-bottom: 14px; }
.dept-group:last-child { margin-bottom: 0; }

.dept-group-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 0 0 6px 16px;
}

.item-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 12px 12px;
  margin-bottom: 8px;
  border-left: 3px solid transparent;
}

.item-card.prio-Hoch { border-left-color: var(--high); }
.item-card.prio-Niedrig { border-left-color: var(--low); }

.item-card.checked { opacity: 0.5; }
.item-card.checked .item-name { text-decoration: line-through; }

.item-check {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--card-bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.item-check.checked {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  font-size: 15px;
}

.item-main { flex: 1; min-width: 0; }

.item-name { font-size: 16px; }

.item-qty { color: var(--text-muted); font-weight: 400; font-size: 14px; }

.item-meta {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 48px 16px;
  font-size: 15px;
}

.clear-checked-bar {
  display: flex;
  justify-content: center;
  margin: 4px 0 18px;
}

.clear-checked-bar button {
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text-muted);
  font-size: 13px;
}

/* --- Recipes grid --- */

.recipes-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.recipe-card {
  background: var(--card-bg);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.recipe-card-photo {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  background: var(--green-tint);
  display: block;
}

.recipe-card-photo.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 34px;
}

.recipe-card-body { padding: 10px 12px 12px; }

.recipe-card-name {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 2px;
}

.recipe-card-servings {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
}

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(env(safe-area-inset-bottom) + 78px);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 28px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  z-index: 5;
}

/* --- Recipe detail --- */

.recipe-detail-photo {
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  border-radius: 14px;
  margin-bottom: 14px;
  background: var(--green-tint);
}

.recipe-detail-photo.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 48px;
}

.recipe-detail h2 { margin: 0 0 12px; font-size: 22px; }

.servings-stepper {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px 16px;
  margin-bottom: 16px;
  width: fit-content;
}

.servings-stepper button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--green-tint);
  color: var(--green-dark);
  font-size: 18px;
  font-weight: 700;
}

.servings-stepper span { font-size: 15px; min-width: 90px; text-align: center; }

.section-title {
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 20px 0 8px;
}

.ingredient-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.ingredient-list li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
}

.ingredient-list li:last-child { border-bottom: none; }

.ingredient-qty { color: var(--green-dark); font-weight: 600; white-space: nowrap; }
.ingredient-store { color: var(--text-muted); font-size: 12px; }

.add-to-list-btn {
  width: 100%;
  padding: 14px;
  border-radius: 12px;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  margin-top: 18px;
}

.add-to-list-confirm {
  text-align: center;
  color: var(--green-dark);
  font-size: 14px;
  margin-top: 8px;
}

.instructions-list {
  margin: 0;
  padding-left: 22px;
}

.instructions-list li {
  margin-bottom: 10px;
  line-height: 1.5;
}

.recipe-detail-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.recipe-detail-actions button {
  flex: 1;
  padding: 12px;
  border-radius: 12px;
  font-size: 15px;
}

/* --- Buttons (shared) --- */

.btn-secondary {
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
}

.btn-primary {
  border: none;
  background: var(--green);
  color: #fff;
  font-weight: 600;
}

.text-btn {
  border: none;
  background: none;
  color: var(--green-dark);
  font-size: 14px;
  padding: 8px 0;
  text-align: left;
}

.text-btn.danger { color: var(--danger); }

/* --- Modals --- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
}

.modal-sheet {
  width: 100%;
  max-height: 88vh;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-sheet-full { max-height: 94vh; }

.modal-head {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 { flex: 1; margin: 0; font-size: 18px; }

.modal-body {
  padding: 16px calc(16px) calc(env(safe-area-inset-bottom) + 24px);
  overflow-y: auto;
}

.form-section { margin-bottom: 12px; border-bottom: 1px solid var(--border); }
.form-section:last-child { border-bottom: none; }

.section-head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: none;
  background: none;
  padding: 14px 2px;
  text-align: left;
}

.section-head h3 { margin: 0; font-size: 16px; }

.section-chevron {
  color: var(--text-muted);
  font-size: 20px;
  transition: transform 0.15s ease;
}

.section-head.open .section-chevron { transform: rotate(90deg); }

.section-body { padding-bottom: 18px; }

.stacked-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 6px; }

.stacked-form input, .inline-form input {
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 16px;
}

.stacked-form button {
  padding: 12px;
  border-radius: 10px;
  border: none;
  background: var(--green);
  color: #fff;
  font-weight: 600;
}

.store-cat-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--green-dark);
  margin: 16px 0 4px;
}

.store-cat-title:first-child { margin-top: 0; }

.stores-list { list-style: none; margin: 0 0 10px; padding: 0; }

.stores-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.stores-list input {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  font-size: 15px;
  padding: 4px 0;
}

.stores-list select {
  flex: 0 0 auto;
  max-width: 116px;
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 6px;
  font-size: 12px;
  padding: 5px 4px;
}

.stores-list .remove-store {
  border: none;
  background: none;
  color: var(--danger);
  font-size: 18px;
  width: 28px;
}

.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; }
.inline-form select {
  flex: 0 0 auto;
  max-width: 118px;
  padding: 12px 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 13px;
}

.inline-form button {
  width: 44px;
  border-radius: 10px;
  border: none;
  background: var(--green);
  color: #fff;
  font-size: 20px;
}

/* --- Recipe editor form --- */

.field-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 16px 0 6px;
}

#recipeForm input[type="text"],
#recipeForm input[type="number"],
#recipeForm textarea,
#itemForm input[type="text"],
#itemForm select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 16px;
}

#recipeForm textarea { resize: vertical; }

.photo-picker {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 12px;
  overflow: hidden;
  background: var(--green-tint);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
}

.photo-picker img { width: 100%; height: 100%; object-fit: cover; }
.photo-placeholder { color: var(--green-dark); font-size: 14px; }

.ingredient-row {
  display: grid;
  grid-template-columns: 1fr 64px 72px auto;
  gap: 6px;
  margin-bottom: 8px;
  align-items: center;
}

.ingredient-row input, .ingredient-row select {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 14px;
  min-width: 0;
}

.ingredient-row .remove-ingredient {
  border: none;
  background: none;
  color: var(--danger);
  font-size: 18px;
  width: 26px;
}

.ingredient-row-store {
  grid-column: 1 / 4;
}

.editor-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.editor-actions button { flex: 1; padding: 13px; border-radius: 12px; font-size: 15px; text-align: center; }

/* --- Lightbox --- */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}
