:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #666666;
  --border: #e0e0e0;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --success: #16a34a;
  --danger: #dc2626;
  --protein-chicken: #f59e0b;
  --protein-beef: #b91c1c;
  --protein-pork: #ec4899;
  --protein-turkey: #a16207;
  --protein-fish: #0891b2;
  --protein-shrimp: #0d9488;
  --protein-vegetarian: #16a34a;
  --protein-other: #6b7280;
  --radius: 10px;
}

:root[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --text: #eaeaea;
  --text-muted: #9a9a9a;
  --border: #333333;
  --primary: #3b82f6;
  --primary-text: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212;
    --surface: #1e1e1e;
    --text: #eaeaea;
    --text-muted: #9a9a9a;
    --border: #333333;
    --primary: #3b82f6;
    --primary-text: #ffffff;
  }
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }

button, input, select, textarea { font-family: inherit; font-size: 16px; }

.btn {
  border: none;
  border-radius: var(--radius);
  padding: 10px 16px;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-primary { background: var(--primary); color: var(--primary-text); border: none; }
.btn-secondary { background: var(--surface); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; border: none; }
.btn-link { background: none; border: none; color: var(--primary); text-decoration: underline; padding: 6px; }
.icon-btn { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text); padding: 6px 10px; }

/* Login */
#view-login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card h1 { margin-top: 0; }
.login-users { display: flex; gap: 12px; justify-content: center; margin-top: 24px; }
.login-users button {
  flex: 1;
  padding: 20px 10px;
  font-size: 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}
.login-pin-area { margin-top: 20px; }
.login-pin-area input {
  width: 100%;
  padding: 14px;
  text-align: center;
  letter-spacing: 6px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  margin-top: 10px;
}
.login-pin-area .btn { width: 100%; margin-top: 10px; }
.error { color: var(--danger); }

/* App shell */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}
.topbar-title { font-weight: 700; font-size: 18px; margin-right: auto; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; order: 3; width: 100%; }
.tab-btn {
  border: none;
  background: var(--bg);
  color: var(--text-muted);
  padding: 10px 14px;
  border-radius: 999px;
  cursor: pointer;
  flex: 1;
  min-width: 70px;
}
.tab-btn.active { background: var(--primary); color: var(--primary-text); }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.current-user { color: var(--text-muted); font-size: 14px; }

main { padding: 14px; max-width: 900px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

/* Library */
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.filters input, .filters select {
  flex: 1;
  min-width: 120px;
  padding: 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.meal-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}
.meal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.meal-card.in-week { border-color: var(--primary); }
.meal-card h3 { margin: 0 0 6px 0; font-size: 17px; }
.meal-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.tag {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 999px;
  color: #fff;
  font-weight: 600;
}
.tag-cuisine { background: var(--text-muted); }
.ingredient-preview { font-size: 13px; color: var(--text-muted); margin: 6px 0; }
.meal-card-actions { display: flex; gap: 8px; margin-top: 10px; }
.meal-card-actions .btn { flex: 1; padding: 8px; font-size: 14px; }
.in-week-badge { font-size: 12px; color: var(--primary); font-weight: 600; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  z-index: 100; padding: 20px; overflow-y: auto;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 520px;
  width: 100%;
  position: relative;
  margin: 20px 0;
}
.modal-close {
  position: absolute; top: 10px; right: 14px;
  background: none; border: none; font-size: 26px; cursor: pointer; color: var(--text);
}
.modal h2 { margin-top: 0; }
.modal .ing-row { display: flex; gap: 6px; margin-bottom: 6px; }
.modal .ing-row input, .modal .ing-row select { padding: 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.modal label { display: block; margin-top: 12px; font-weight: 600; font-size: 14px; }
.modal input[type=text], .modal input[type=number], .modal textarea, .modal select {
  width: 100%; padding: 10px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); margin-top: 4px;
}
.modal-actions { display: flex; gap: 8px; margin-top: 20px; }
.modal-actions .btn { flex: 1; }
.ingredients-list { margin-top: 10px; }
.ingredients-list li { padding: 4px 0; border-bottom: 1px solid var(--border); font-size: 14px; }

/* This Week */
.week-header { display: flex; gap: 10px; margin-bottom: 14px; }
.week-header select { flex: 1; padding: 10px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.week-totals { color: var(--text-muted); margin-bottom: 14px; }
.week-meal-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; margin-bottom: 10px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.week-meal-card .wm-name { flex: 1; min-width: 140px; font-weight: 600; }
.wm-name.cooked { text-decoration: line-through; color: var(--text-muted); }
.servings-control { display: flex; align-items: center; gap: 6px; }
.servings-control button { width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--border); background: var(--bg); color: var(--text); cursor: pointer; }
.jump-btn { width: 100%; margin-top: 10px; }

/* Shopping */
.shopping-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.shopping-category { margin-bottom: 18px; }
.shopping-category h3 { margin-bottom: 8px; color: var(--text-muted); text-transform: uppercase; font-size: 13px; letter-spacing: 1px; }
.shopping-item {
  display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border);
}
.shopping-item input[type=checkbox] { width: 22px; height: 22px; margin-top: 2px; }
.shopping-item.checked .item-name { text-decoration: line-through; color: var(--text-muted); }
.item-name { font-weight: 600; }
.used-in { font-size: 12px; color: var(--text-muted); }

/* History */
.history-week { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; cursor: pointer; }
.history-week h3 { margin: 0 0 4px 0; }
.history-week .meta { color: var(--text-muted); font-size: 14px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  z-index: 200; font-size: 14px;
}

@media (min-width: 700px) {
  .tabs { order: 0; width: auto; }
  .tab-btn { flex: none; }
}
