*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
input, textarea, input[type="search"], input[type="text"], input[type="password"] { direction: ltr; }

:root {
  --bg: #f8f6f2;
  --surface: #fff;
  --text: #2c2c2c;
  --muted: #7a7a7a;
  --accent: #3a7d44;
  --accent-hover: #2e6336;
  --border: #e0ddd8;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  max-width: 960px;
  margin: 0 auto;
  padding: 1rem;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

header h1 { font-size: 1.5rem; }

.header-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

input[type="search"] {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  width: 240px;
}

.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.15s;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover { background: #d0cdc8; }
.btn-danger { background: #c0392b; color: #fff; }
.btn-danger:hover { background: #a93226; }
.btn-small { padding: 0.25rem 0.6rem; font-size: 0.8rem; }

nav#tag-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 1.25rem;
}

.tag {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  background: var(--border);
  border-radius: 999px;
  font-size: 0.78rem;
  cursor: pointer;
  transition: background 0.15s;
}
.tag:hover, .tag.active { background: var(--accent); color: #fff; }

.recipe-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.recipe-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  cursor: pointer;
  transition: box-shadow 0.15s;
}
.recipe-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
.recipe-card h3 { margin-bottom: 0.3rem; }
.recipe-card .meta { font-size: 0.82rem; color: var(--muted); }
.recipe-card .tags { margin-top: 0.5rem; display: flex; flex-wrap: wrap; gap: 0.3rem; }

.recipe-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}
.recipe-card-header h3 { flex: 1; }

.btn-card-basket {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.15rem 0.4rem;
  font-size: 0.85rem;
  cursor: pointer;
  line-height: 1.4;
  transition: background 0.1s, border-color 0.1s;
}
.btn-card-basket:hover { background: var(--bg); border-color: var(--accent); }

.hidden { display: none !important; }

.recipe-stores {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 1rem;
}
.recipe-stores-label {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}
.recipe-store-badge {
  font-size: 0.78rem;
  padding: 0.15rem 0.55rem;
  border-radius: 20px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
}

/* Detail view */
#recipe-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

#recipe-detail .detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

#recipe-detail h2 { font-size: 1.4rem; }
#recipe-detail .meta { color: var(--muted); font-size: 0.85rem; margin-bottom: 1rem; }

.detail-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

.scaling-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}
.scaling-controls input {
  width: 60px;
  padding: 0.3rem;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.two-col {
  display: grid;
  grid-template-columns: 1fr 1.5fr;
  gap: 2rem;
  margin-top: 1rem;
}

@media (max-width: 600px) {
  .two-col { grid-template-columns: 1fr; }
  input[type="search"] { width: 100%; }
  .header-actions { width: 100%; }
  .form-row { flex-direction: column; gap: 0; }
  #recipe-detail .detail-header { flex-direction: column; }
  .detail-actions { align-self: flex-start; }
  .basket-item { flex-wrap: wrap; }
  .basket-item .title { min-width: 100%; margin-bottom: 0.3rem; }
}

.two-col h3 { margin-bottom: 0.5rem; font-size: 1rem; }
.two-col ul { list-style: disc inside; }
.two-col li { margin-bottom: 0.3rem; font-size: 0.9rem; }
.two-col ol li { margin-bottom: 0.6rem; font-size: 0.9rem; }

/* Form */
#recipe-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

#recipe-form h2 { margin-bottom: 1rem; }

.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 600; margin-bottom: 0.3rem; font-size: 0.9rem; }
.form-group input, .form-group textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
}
.form-group textarea { min-height: 80px; resize: vertical; }

.tag-input-wrapper { display: flex; flex-wrap: wrap; gap: 0.3rem; align-items: center; border: 1px solid var(--border); border-radius: var(--radius); padding: 0.4rem 0.5rem; background: #fff; cursor: text; }
.tag-input-wrapper:focus-within { border-color: var(--accent); }
.tag-chips { display: contents; }
.tag-chip { display: inline-flex; align-items: center; gap: 0.2rem; background: var(--accent); color: #fff; border-radius: 20px; padding: 0.1rem 0.5rem 0.1rem 0.7rem; font-size: 0.8rem; }
.tag-chip-remove { background: none; border: none; color: #fff; cursor: pointer; font-size: 0.95rem; padding: 0; line-height: 1; opacity: 0.8; }
.tag-chip-remove:hover { opacity: 1; }
.tag-input-wrapper input { border: none; outline: none; font-size: 0.9rem; flex: 1; min-width: 120px; padding: 0.1rem 0; background: transparent; }
.tag-suggestions { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: none; max-height: 160px; overflow-y: auto; margin-top: 0.2rem; z-index: 100; position: relative; }
.tag-suggestion-item { padding: 0.4rem 0.75rem; cursor: pointer; font-size: 0.9rem; }
.tag-suggestion-item:hover { background: var(--bg); color: var(--accent); }

.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }

.dynamic-list { margin-bottom: 0.5rem; }
.dynamic-list .row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  align-items: center;
}
.dynamic-list .row input { flex: 1; }
.dynamic-list .row .btn-remove {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0 0.3rem;
}

.form-actions { display: flex; gap: 0.5rem; margin-top: 1.5rem; }

a.source-link { color: var(--accent); font-size: 0.85rem; }

/* Basket */
#basket-count {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  min-width: 1.3em;
  height: 1.3em;
  line-height: 1.3em;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0 0.3em;
}

#basket-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.basket-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--border);
  gap: 0.75rem;
}
.basket-item:last-child { border-bottom: none; }
.basket-item .title { flex: 1; font-weight: 500; }
.basket-item input[type="number"] {
  width: 60px;
  padding: 0.3rem;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.shopping-list { margin-top: 1.5rem; }
.shopping-list h3 { margin-bottom: 0.75rem; }
.shopping-list ul { list-style: none; }
.shopping-list li {
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  display: flex;
  gap: 0.5rem;
}
.shopping-list li:last-child { border-bottom: none; }
.shopping-list .qty { font-weight: 600; min-width: 60px; text-align: right; }
.shopping-list .unit { color: var(--muted); min-width: 40px; }

.basket-actions { display: flex; gap: 0.5rem; margin-bottom: 1rem; }

/* Suggest view */
.suggest-selected { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin: 1rem 0; }
.suggest-chips { display: flex; flex-wrap: wrap; gap: 0.4rem; min-height: 1.5rem; }
.suggest-chip { display: inline-flex; align-items: center; gap: 0.3rem; background: var(--accent); color: #fff; border-radius: 20px; padding: 0.2rem 0.6rem 0.2rem 0.8rem; font-size: 0.85rem; }
.chip-remove { background: none; border: none; color: #fff; cursor: pointer; font-size: 1rem; line-height: 1; padding: 0; opacity: 0.8; }
.chip-remove:hover { opacity: 1; }
.suggest-picker { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.suggest-ingredient-grid { display: flex; flex-wrap: wrap; gap: 0.4rem; max-height: 260px; overflow-y: auto; }
.suggest-ing-btn { background: var(--bg); border: 1px solid var(--border); border-radius: 20px; padding: 0.25rem 0.75rem; font-size: 0.85rem; cursor: pointer; color: var(--text); transition: background 0.15s, border-color 0.15s; }
.suggest-ing-btn:hover { border-color: var(--accent); color: var(--accent); }
.suggest-ing-btn.selected { background: var(--accent); border-color: var(--accent); color: #fff; }
.suggest-results { margin-top: 0.5rem; }
.suggest-result-card { cursor: pointer; }
.match-badge { font-size: 0.8rem; font-weight: 600; padding: 0.2rem 0.6rem; border-radius: 20px; white-space: nowrap; flex-shrink: 0; }
.match-high { background: #d4edda; color: #1a5c2a; }
.match-mid  { background: #fff3cd; color: #7a5c00; }
.match-low  { background: #f8e0e0; color: #7a1a1a; }

/* Ingredients catalog view */
#ingredients-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.ing-accordion {
  margin-top: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.ing-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.85rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
  user-select: none;
}
.ing-row:last-of-type { border-bottom: none; }
.ing-row:hover { background: var(--bg); }
.ing-row.expanded { background: var(--bg); }
.ing-row.dirty { border-left: 3px solid var(--accent); padding-left: calc(0.85rem - 3px); }

.ing-name {
  font-size: 0.9rem;
}
.ing-cat-badge {
  margin-left: 0.5rem;
  font-size: 0.72rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.05rem 0.45rem;
  vertical-align: middle;
}

.ing-expand {
  padding: 0.85rem;
  background: #f0ede8;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.ing-expand.hidden { display: none; }

.ing-field-label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.ing-slider-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.ing-slider-row input[type=range] { flex: 1; accent-color: var(--accent); }
.ing-slider-label { font-size: 0.78rem; color: var(--muted); }
.ing-score-val { min-width: 2.5rem; text-align: right; font-size: 0.85rem; font-weight: 600; color: var(--accent); }

.avail-select {
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  background: var(--surface);
  cursor: pointer;
}
.avail-select:focus { outline: none; border-color: var(--accent); }

.ing-row-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  min-width: 7rem;
  flex-shrink: 0;
}

.ing-avail-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: transparent;
  flex-shrink: 0;
}

.ing-store-label {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  text-align: right;
}

.ingredients-catalog-filter {
  width: 100%;
  max-width: 300px;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
  margin-top: 0.75rem;
}

/* Meal Plan */
#mealplan-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.mealplan-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.mealplan-cards {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.mealplan-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.mealplan-day {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 0.3rem;
}

.mealplan-recipe-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 0.2rem;
}

.mealplan-meta {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.6rem;
}

.mealplan-card-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
}

.mealplan-import {
  margin-top: 1.25rem;
  display: flex;
  justify-content: flex-end;
}

/* Swap modal */
.swap-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.swap-modal.hidden { display: none !important; }

.swap-modal-inner {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 90%;
  max-width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 24px rgba(0,0,0,0.2);
}

#swap-results {
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.swap-recipe-card {
  cursor: pointer;
  padding: 0.75rem 1rem;
}

.swap-recipe-card:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  border-color: var(--accent);
}

@media (max-width: 600px) {
  .mealplan-card-actions { flex-direction: column; }
  .mealplan-import { justify-content: stretch; }
  .mealplan-import .btn { width: 100%; }
}
