:root {
  --bg:        #1e1726;
  --surface:   #2a2233;
  --surface-2: #362b40;
  --border:    #4a3c56;
  --text:      #f1eaf5;
  --muted:     #ab9bb5;
  --primary:   #c15fd1;
  --danger:    #e0577a;
  --warning:   #e0a33e;
  --success:   #56a860;
  --radius:    8px;
  --tap:       44px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow-x: hidden;
}

.wrap {
  max-width: 540px;
  margin: 0 auto;
  padding: 18px 12px 48px;
}

h2 { font-size: 1.05rem; font-weight: 600; margin: 0 0 12px; }
h3 { font-size: .95rem; font-weight: 600; margin: 0 0 8px; }
.section-head { margin: 20px 0 10px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }

/* ── Tabs ─────────────────────────────────────────────────────────────── */
.tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tab {
  flex: 1;
  min-height: var(--tap);
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.tab.is-active { color: var(--text); border-bottom-color: var(--primary); }

/* ── Cards & forms ────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.col-3  { flex: 1 1 22%; min-width: 0; }
.col-6  { flex: 1 1 46%; min-width: 0; }
.col-12 { flex: 1 1 100%; min-width: 0; }

label.col-6, label.col-12, .field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: .8rem;
  color: var(--muted);
}

input, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 1rem; /* keeps iOS from zooming on focus */
}
input:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
input::placeholder { color: #8a7a94; }
input[type="date"] { min-height: var(--tap); }

.error { color: #f0839e; font-size: .85rem; margin: 8px 0 0; min-height: 0; }
.error:empty { margin: 0; }

/* ── Buttons ──────────────────────────────────────────────────────────── */
.btn {
  min-height: var(--tap);
  padding: 8px 14px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { filter: brightness(1.2); }
.btn-block { display: block; width: 100%; margin-top: 10px; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: .85rem; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-ghost   { background: none; border-color: var(--border); color: var(--muted); }
.btn-danger  { background: none; border-color: var(--danger);  color: #f0839e; }
.btn-outline { background: none; border-color: var(--primary); color: #dba6e8; }
.btn-warn    { background: none; border-color: var(--warning); color: var(--warning); }

.icon-btn {
  min-width: var(--tap); min-height: var(--tap);
  background: none; border: 0; color: var(--muted);
  font-size: 1.5rem; line-height: 1; cursor: pointer;
}
.link-btn {
  background: none; border: 0; padding: 8px 0;
  min-height: var(--tap);
  color: #dba6e8; font: inherit; cursor: pointer;
}

/* ── Inventory items ──────────────────────────────────────────────────── */
.item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--muted);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 8px;
}
.item.expired { border-left-color: var(--danger); }
.item.soon    { border-left-color: var(--warning); }
.item.good    { border-left-color: var(--success); }

.item-row { display: flex; align-items: center; gap: 8px; }
.item-row + .item-row { margin-top: 6px; }
.item-name { font-weight: 600; flex: 1; word-break: break-word; }
.meta  { color: var(--muted); font-size: .85rem; flex: 1; }
.notes { color: var(--muted); font-size: .85rem; font-style: italic; margin-top: 4px; }

.badge {
  font-size: .72rem; font-weight: 600;
  padding: 3px 8px; border-radius: 20px;
  border: 1px solid currentColor;
  white-space: nowrap;
}
.badge.expired { color: #f0839e; }
.badge.soon    { color: var(--warning); }
.badge.good    { color: #7cc286; }
.badge.neutral { color: var(--muted); }

.empty {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: center;
  color: var(--muted);
  font-size: .9rem;
}

/* ── Recipes ──────────────────────────────────────────────────────────── */
.list-head { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.list-head h2 { flex: 1; margin: 0; }

.recipe-detail h2 { font-size: 1.3rem; margin: 4px 0 16px; }
.recipe-detail h3 { color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-size: .78rem; }
.recipe-detail ul, .recipe-detail ol { margin: 0 0 20px; padding-left: 22px; }
.recipe-detail li { margin-bottom: 6px; }
.detail-actions { display: flex; gap: 8px; }

/* ── Recipe table ──────────────────────────────────────────────────────── */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.recipe-table { width: 100%; border-collapse: collapse; font-size: .85rem; white-space: nowrap; }
.recipe-table th, .recipe-table td { padding: 9px 12px; text-align: left; }
.recipe-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--muted);
  font-size: .72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.recipe-table thead th:hover { color: var(--text); }
.recipe-table thead th.sorted { color: var(--primary); }
.recipe-table thead th .arrow { margin-left: 3px; }
.recipe-table tbody tr {
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.recipe-table tbody tr:last-child { border-bottom: none; }
.recipe-table tbody tr:hover { background: var(--surface); }
.recipe-table tbody tr:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
.recipe-table .recipe-name { font-weight: 600; }
.recipe-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.recipe-table .dash { color: var(--muted); }
.recipe-table .status-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}
.recipe-table .status-dot.good    { background: var(--success); }
.recipe-table .status-dot.soon    { background: var(--warning); }
.recipe-table .status-dot.expired { background: var(--danger); }

.recipe-table .checkbox-cell {
  cursor: default;
  width: 1%;
}
.recipe-table input[type="checkbox"] {
  width: 20px; height: 20px; min-height: 20px;
  accent-color: var(--primary);
}

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 12px;
}
.bulk-bar .meta { flex: 0 0 auto; white-space: nowrap; }
.bulk-bar input { flex: 1 1 140px; min-width: 0; }

/* ── Editor rows ──────────────────────────────────────────────────────── */
.step-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }

/* Each ingredient is two lines: name alone (it's the one field that needs
   room to read while typing), then the short fields + buttons together. */
.ing-line { margin-bottom: 8px; }
.ing-row { display: flex; gap: 6px; align-items: center; }
.ing-row + .ing-row { margin-top: 6px; }

/* A set of interchangeable ingredients — every member styled identically
   (no "main" row + subordinate extras), separated by an "OR" divider. */
.alt-group {
  border: 1px solid var(--warning);
  border-radius: var(--radius);
  padding: 8px;
  margin-bottom: 6px;
}
.alt-group .ing-line { margin-bottom: 0; }
.or-divider { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.or-divider::before, .or-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.or-divider span { color: var(--warning); font-size: .7rem; font-weight: 700; }

.ing-optional {
  display: flex; align-items: center; gap: 4px;
  flex: 0 0 auto;
  color: var(--muted); font-size: .78rem; white-space: nowrap;
}
.ing-optional input { width: 18px; height: 18px; min-height: 18px; accent-color: var(--primary); }

.ing-name { flex: 1 1 auto; min-width: 0; }
.ing-qty  { flex: 1 1 0; min-width: 0; }
.ing-unit { flex: 1 1 0; min-width: 0; }
.step-num { color: var(--muted); font-size: .85rem; min-width: 18px; }
.step-text { flex: 1; min-width: 0; }
.row-btn {
  flex: 0 0 auto;
  min-width: 40px; min-height: 40px;
  padding: 4px 8px;
  font-size: .8rem;
}

/* ── Modal ────────────────────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .6);
  display: flex; align-items: flex-end; justify-content: center;
}
/* An author `display` beats the UA's [hidden] rule, so restate it. */
.modal[hidden] { display: none; }
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 540px;
  max-height: 92vh;
  display: flex; flex-direction: column;
}
.modal-head, .modal-foot {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { flex: 1; margin: 0; }
.modal-foot { border-bottom: 0; border-top: 1px solid var(--border); padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.modal-foot .btn-block { margin-top: 0; }
.modal-body { padding: 14px; overflow-y: auto; -webkit-overflow-scrolling: touch; }

@media (min-width: 560px) {
  .modal { align-items: center; }
  .modal-box { border-radius: var(--radius); }
}

/* ── Added: matching, planning and inline editing ──────────────────────── */

.col-4 { flex: 1 1 30%; min-width: 0; }
.col-8 { flex: 1 1 62%; min-width: 0; }
label.col-4, label.col-8 { display: flex; flex-direction: column; gap: 4px; font-size: .8rem; color: var(--muted); }

.count { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.summary { margin: 0 0 12px; }
.hint { color: var(--muted); font-size: .78rem; margin: 2px 0 6px; }
.hint:empty { margin: 0; }

.search { margin-bottom: 10px; }

/* Collapsible "Add Item" card — the list matters more than the form. */
.collapsible { padding: 0; }
.collapsible > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  min-height: var(--tap);
  display: flex; align-items: center;
}
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary h2 { margin: 0; flex: 1; }
.collapsible > summary::after { content: "+"; color: var(--muted); font-size: 1.3rem; line-height: 1; }
.collapsible[open] > summary::after { content: "−"; }
.collapsible[open] > summary { border-bottom: 1px solid var(--border); }
.collapsible > form { padding: 14px; }

/* ── Expiring banner ──────────────────────────────────────────────────── */
.banner {
  border: 1px solid var(--border);
  border-left: 4px solid var(--warning);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 12px 14px;
  margin-bottom: 12px;
}
.banner.expired { border-left-color: var(--danger); }
.banner-head { font-weight: 600; font-size: .92rem; margin-bottom: 8px; }
.banner-items { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: .74rem;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 3px 9px;
}

/* ── Inline item editing ──────────────────────────────────────────────── */
.item-edit .grid { margin-bottom: 8px; }
.item-edit .detail-actions { margin-top: 4px; }

/* Recipe cards reuse the inventory .item colours. */
.item.good { border-left-color: var(--success); }
.item.soon { border-left-color: var(--warning); }
.item.expired { border-left-color: var(--danger); }

/* ── Recipe ingredient checklist ──────────────────────────────────────── */
.ing-list { list-style: none; margin: 0 0 20px; padding: 0; }
.ing {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
}
.ing:last-child { border-bottom: 0; }
.ing-mark { flex: 0 0 auto; width: 1.1em; text-align: center; font-weight: 700; }
.ing.have    .ing-mark { color: var(--success); }
.ing.low     .ing-mark { color: var(--warning); }
.ing.expired .ing-mark { color: var(--danger); }
.ing.missing .ing-mark { color: var(--muted); }
.ing.missing > span:last-child { color: var(--muted); }
.ing-note { color: var(--muted); font-size: .82rem; }

.similar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 6px;
  padding: 8px 10px;
  background: var(--surface-2);
  border-radius: 6px;
}

/* ── Servings scaler ──────────────────────────────────────────────────── */
.scaler { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.scaler-value { font-weight: 600; min-width: 1.5em; text-align: center; }

.recipe-detail .item-row h2 { flex: 1; margin: 4px 0 12px; }

/* ── Shopping planner ─────────────────────────────────────────────────── */
.plan-row { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.plan-check { width: 22px; height: 22px; min-height: 22px; flex: 0 0 auto; accent-color: var(--primary); }
.plan-servings { width: 68px; min-width: 0; flex: 0 0 auto; }
.is-bought { opacity: .5; }

/* ── Nutrition table ───────────────────────────────────────────────────── */
.nutri-table { width: 100%; border-collapse: collapse; font-size: .82rem; white-space: nowrap; }
.nutri-table th, .nutri-table td { padding: 6px 8px; text-align: left; }
.nutri-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--muted);
  font-size: .7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.nutri-table thead th:hover { color: var(--text); }
.nutri-table thead th.sorted { color: var(--primary); }
.nutri-table thead th .arrow { margin-left: 3px; }
.nutri-table tbody tr:not(.nutri-edit-row) { border-bottom: 1px solid var(--border); }
.nutri-table tbody tr:not(.nutri-edit-row):hover { background: var(--surface); }
.nutri-table .nutri-name { font-weight: 600; }
.nutri-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.nutri-table .dash { color: var(--muted); }
.nutri-table .checkbox-cell { width: 1%; }

.nutri-category {
  width: 88px;
  min-height: 30px;
  padding: 3px 6px;
  font-size: .78rem;
}

.nutri-edit-row td { background: var(--surface-2); white-space: normal; padding: 12px; }
.nutri-edit-row .grid { margin-bottom: 8px; }

.toast {
  position: fixed;
  left: 12px; right: 12px; bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 100;
  max-width: 516px;
  margin: 0 auto;
  background: var(--surface-2);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: #f0839e;
  padding: 12px 14px;
  font-size: .88rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
