:root {
  --bg: #f6f6f4;
  --card: #ffffff;
  --text: #1f1f1f;
  --muted: #6b6b6b;
  --border: #ddd;
  --accent: #2f7a4f;
  --accent-text: #ffffff;
  --danger: #c62828;
  --error-bg: #fdecec;
  --warn: #9a5b00;
  --warn-bg: #fff5e0;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616;
    --card: #222;
    --text: #eee;
    --muted: #a0a0a0;
    --border: #363636;
    --accent: #4caf7a;
    --accent-text: #0d1f14;
    --danger: #f36b6b;
    --error-bg: #3a1d1d;
    --warn: #f0b457;
    --warn-bg: #33270f;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, sans-serif;
}

a { color: var(--accent); }

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

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.muted, .meta { color: var(--muted); }
.meta { font-size: 0.9rem; }

/* Header */
.site-header {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; color: var(--text); text-decoration: none; }

.site-header nav a { color: var(--muted); text-decoration: none; }
.site-header nav { display: flex; flex-wrap: wrap; gap: 0.2rem 1rem; }
.site-header nav a { white-space: nowrap; }
.site-header nav a:hover { color: var(--text); }
.site-header nav a[aria-current="page"] { color: var(--text); font-weight: 600; }

main {
  max-width: 44rem;
  margin: 2rem auto 4rem;
  padding: 0 1rem;
}

h1 { margin: 0 0 1rem; line-height: 1.2; }

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.page-header h1 { margin: 0; }

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

.back { display: inline-block; margin-bottom: 0.75rem; text-decoration: none; }

/* Buttons (also used on links that look like buttons) */
.button {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}

.button:hover { border-color: var(--muted); }

.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.button.danger { color: var(--danger); }
.button.danger:hover { border-color: var(--danger); }

/* Recipe list */
.empty {
  padding: 2rem 1rem;
  text-align: center;
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: 12px;
}

.card-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.75rem; }

.card {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.9rem 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
}

.card:hover { border-color: var(--accent); }

.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Recipe view */
.description { white-space: pre-wrap; margin: 0 0 0.5rem; }

table.ingredients {
  width: 100%;
  margin-top: 1rem;
  border-collapse: collapse;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.ingredients th, .ingredients td {
  padding: 0.55rem 0.8rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.ingredients tr:last-child td { border-bottom: none; }
.ingredients th { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
.ingredients .num { text-align: right; font-variant-numeric: tabular-nums; }

/* Portion scaler */
.scaler { margin-top: 1.25rem; }

.scaler-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.scaler-controls > label:first-child { font-weight: 600; }

.stepper { display: flex; gap: 0.25rem; }
.stepper input { width: 4.5rem; text-align: center; }
.button.step { padding: 0.4rem 0.8rem; }

.toggle { display: flex; align-items: center; gap: 0.4rem; margin-left: auto; font-size: 0.9rem; }
.toggle input { width: auto; }

button.link {
  padding: 0;
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
}

button.link:hover { text-decoration: underline; }

.scaler .meta { margin: 0.75rem 0 0; }

.maths {
  margin-top: 0.15rem;
  font-size: 0.8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.rounded-note { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }

/* Meal plan */
.week-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.week-nav .button { white-space: nowrap; padding: 0.45rem 0.75rem; }

.week-label { display: flex; flex-direction: column; align-items: center; text-align: center; font-size: 0.95rem; }
.week-label a { font-size: 0.85rem; }

.plan-summary { margin-bottom: 1rem; }
.plan-summary ul { margin: 0.25rem 0 0; padding-left: 1.2rem; }

.days { display: grid; gap: 0.75rem; }

.day { gap: 0.6rem; }
.day.today { border-color: var(--accent); }

.day header { display: flex; align-items: baseline; justify-content: space-between; gap: 0.5rem; }
.day h2 { margin: 0; font-size: 1.05rem; }

.plan-items { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; }

.plan-item, .plan-add {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 3.5rem auto;
  gap: 0.4rem;
  align-items: center;
}

.plan-item .remove { grid-row: auto; grid-column: auto; width: 2.25rem; }

.plan-add select { color: var(--muted); }
.plan-add { padding-top: 0.6rem; border-top: 1px dashed var(--border); }
.plan-items:empty + .plan-add, .day header + .plan-add { border-top: none; padding-top: 0; }

input.portions { text-align: center; padding-left: 0.3rem; padding-right: 0.3rem; }

/* Grocery list */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin: 1rem 0 0.5rem;
}

.grocery-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; }

.grocery-item { display: flex; align-items: flex-start; gap: 0.6rem; }
.grocery-item > input[type="checkbox"] { width: 1.2rem; height: 1.2rem; margin-top: 0.85rem; accent-color: var(--accent); flex-shrink: 0; }

.grocery-item details {
  flex: 1;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.grocery-item summary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.9rem;
  cursor: pointer;
  list-style: none;
}

.grocery-item summary::-webkit-details-marker { display: none; }
.grocery-item summary::before { content: "▸"; color: var(--muted); }
.grocery-item details[open] summary::before { content: "▾"; }

.item-name { flex: 1; overflow-wrap: anywhere; }
.amount { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }

.badge {
  display: inline-block;
  margin-left: 0.4rem;
  padding: 0 0.45rem;
  font-size: 0.72rem;
  line-height: 1.5;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  vertical-align: 0.1em;
}

.badge.warn { color: var(--warn); border-color: var(--warn); }

.item-details { padding: 0 0.9rem 0.8rem 1.9rem; font-size: 0.92rem; }
.item-details .label { margin: 0; font-size: 0.8rem; color: var(--muted); }
.sources { margin: 0.2rem 0 0.5rem; padding-left: 1.1rem; }
.item-details .maths { margin: 0 0 0.6rem; font-size: 0.85rem; }

.edit-amount, .equivalence-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.edit-amount select, .equivalence-form select { width: auto; }
.edit-amount input, .equivalence-form input { width: 5.5rem; }

.notice { background: var(--warn-bg); border-color: var(--warn); margin: 0.75rem 0; }
.notice ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.75rem; }
.notice p { margin: 0 0 0.5rem; }

.merge-bar { gap: 0.5rem; margin: 1rem 0 0.75rem; }
.merge-bar p { margin: 0; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }

.error-text { margin: 0.4rem 0 0; color: var(--danger); font-size: 0.9rem; }
.warn-text { color: var(--warn); }

.conversions { margin-top: 1.5rem; font-size: 0.92rem; }
.conversions h2 { font-size: 1rem; margin: 0 0 0.3rem; }
.conversions ul { margin: 0; padding-left: 1.2rem; }

/* Nutrition */
.nutrition { margin-top: 2rem; }
.nutrition h2 { font-size: 1.15rem; margin: 0 0 0.5rem; }
.nutrition-summary { gap: 0.4rem; margin-bottom: 0.75rem; }
.nutrition-summary p { margin: 0; }
.label { font-size: 0.8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }

.macro-tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem; }
.macro-tile { display: flex; flex-direction: column; padding: 0.5rem 0.6rem; border: 1px solid var(--border); border-radius: 8px; }
.macro-value { font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.nutrition-lines { list-style: none; margin: 0 0 0.75rem; padding: 0; display: grid; gap: 0.4rem; }
.nutrition-line {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding: 0.55rem 0.8rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.nutrition-line.unlinked, .nutrition-line.needsGrams { border-color: var(--warn); }
.nutrition-line summary { cursor: pointer; }
.nutrition-line .maths { margin: 0.2rem 0; font-size: 0.85rem; }
.nutrition-line > .button { align-self: flex-start; }

.food-search { display: grid; gap: 0.5rem; }
.food-results { margin: 0; padding-left: 1.1rem; display: grid; gap: 0.3rem; }
.food-results .link { text-align: left; }

.day-nutrition { margin: -0.3rem 0 0; font-size: 0.82rem; }
.day-nutrition:empty { display: none; }

/* Sync */
.sync-banner { max-width: 44rem; margin: 1rem auto 0; padding: 0 1rem; }
.sync-banner:empty { display: none; }
.sync-notice p { margin: 0 0 0.4rem; }
.sync-form { display: grid; gap: 0.4rem; max-width: 22rem; }
.sync-form label { margin: 0.25rem 0 0; }
.sync-form .button { justify-self: start; }
.sync-form .actions { margin-top: 0.3rem; }
.password-form { margin-top: 1rem; padding-top: 0.75rem; border-top: 1px solid var(--border); max-width: none; }
.password-form h3 { margin: 0; font-size: 1rem; }
.password-form p { margin: 0; }

/* Footer + backup */
.site-footer { max-width: 44rem; margin: -2rem auto 2rem; padding: 0 1rem; font-size: 0.9rem; }
.site-footer a { color: var(--muted); }
.backup-load { margin-top: 1rem; }

/* Batch add + goals */
.intro { margin: -0.5rem 0 1rem; }
.batch-form { margin-bottom: 0.75rem; gap: 0.6rem; }
.batch-form summary { cursor: pointer; font-weight: 600; }
.batch-form form { display: grid; gap: 0.6rem; margin-top: 0.6rem; }
.batch-form .plan-add { border-top: none; padding-top: 0; }
.batch-form form > .button { justify-self: start; }
.day-toggles { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.day-toggle {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.9rem;
}
.day-toggle input { width: auto; accent-color: var(--accent); }
.day-toggle:has(input:checked) { border-color: var(--accent); }

.goal-flag { font-weight: 600; text-decoration: none; }
.goal-flag.ok { color: var(--accent); }
.goal-flag.off { color: var(--warn); }

.goals-form { gap: 0.5rem; margin-bottom: 1.5rem; }
.goals-form h2, .goals-review h2 { font-size: 1.15rem; margin: 0; }
.goals-form p { margin: 0; }
.goals-grid { display: grid; grid-template-columns: 1fr 6rem 6rem; gap: 0.35rem 0.5rem; align-items: center; }
.goals-grid input { text-align: right; }

.goals-review { display: grid; gap: 0.75rem; margin-top: 1rem; }
.review-headline { font-weight: 600; margin: 0; }
.day-review h3 { margin: 0 0 0.3rem; font-size: 1rem; }
.day-review .warn-text { margin: 0 0 0.3rem; font-size: 0.88rem; }
.goal-checks { list-style: none; margin: 0.2rem 0 0; padding: 0; display: grid; gap: 0.3rem; }
.goal-check { display: flex; justify-content: space-between; gap: 0.75rem; font-size: 0.92rem; }
.goal-status { white-space: nowrap; font-weight: 600; }
.goal-check.ok .goal-status { color: var(--accent); }
.goal-check.under .goal-status, .goal-check.over .goal-status { color: var(--warn); }

/* Shopping list */
.progress { font-weight: 600; }
.progress.done { color: var(--accent); }

.shop-section { margin-top: 1.25rem; }
.shop-section h2 { font-size: 1.05rem; margin: 0 0 0.4rem; }

.shop-items { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; }

.shop-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.35rem 0.75rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.shop-item input[type="checkbox"] {
  width: 1.4rem;
  height: 1.4rem;
  flex-shrink: 0;
  accent-color: var(--accent);
}

/* The whole row text is the label, so tapping anywhere on it ticks the box */
.shop-item label {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0.35rem 0;
  cursor: pointer;
}

.shop-line { display: flex; justify-content: space-between; gap: 0.75rem; }

.shop-item.checked { opacity: 0.55; }
.shop-item.checked .item-name, .shop-item.checked .amount { text-decoration: line-through; }

.section-select { width: auto; max-width: 8.5rem; font-size: 0.85rem; padding: 0.3rem 0.4rem; }
.section-select.guessed { border-style: dashed; }

/* Flyers */
.section-title { font-size: 1.15rem; margin: 2rem 0 0.75rem; }

.import-box { gap: 0.5rem; }
.import-box h2 { margin: 0; font-size: 1.15rem; }
.import-box label { display: block; margin: 0.5rem 0 0.25rem; font-weight: 600; }
.import-box details summary { cursor: pointer; color: var(--accent); margin: 0.25rem 0; }
.import-box details textarea { margin: 0.4rem 0; font-family: ui-monospace, monospace; font-size: 0.85rem; }
.import-box .help p { margin: 0.4rem 0; font-size: 0.92rem; }

.steps { margin: 0; padding-left: 1.3rem; }

.import-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.import-fields .meta { display: block; margin-top: 0.2rem; }

.flyer-preview:not(:empty) { margin-top: 0.75rem; border-top: 1px solid var(--border); padding-top: 0.75rem; }
.flyer-preview .actions { margin-top: 0.75rem; }
.problem-list { margin: 0; padding-left: 1.2rem; font-size: 0.9rem; }

/* Wide tables scroll sideways inside their box instead of widening the page */
.table-scroll { overflow-x: auto; margin-top: 1rem; }
.table-scroll table.ingredients { margin-top: 0; }
.products td { vertical-align: top; }
.saving { font-size: 0.75rem; color: var(--accent); font-weight: 600; }

/* Form */
.recipe-form { display: flex; flex-direction: column; }

.recipe-form label { margin: 1rem 0 0.3rem; font-weight: 600; }
.recipe-form > label:first-of-type { margin-top: 0; }

input, select, textarea {
  width: 100%;
  padding: 0.5rem 0.7rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
}

#f-portions { max-width: 8rem; }

fieldset {
  margin: 1.5rem 0 0;
  padding: 0;
  border: none;
}

legend { font-weight: 600; margin-bottom: 0.5rem; }

.ingredient-rows { list-style: none; margin: 0 0 0.75rem; padding: 0; display: grid; gap: 0.6rem; }

/* Each ingredient line: name | qty | unit | remove, with the note underneath */
.ingredient-row, .row-headings {
  display: grid;
  grid-template-columns: 1fr 5.5rem 7rem 2.25rem;
  gap: 0.4rem;
}

.ingredient-row .note { grid-column: 1 / 4; font-size: 0.9rem; padding: 0.35rem 0.7rem; }

.row-headings { font-size: 0.8rem; color: var(--muted); margin-bottom: 0.25rem; }

.remove {
  grid-row: 1;
  grid-column: 4;
  height: 100%;
  font-size: 1.3rem;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.remove:hover { color: var(--danger); }

.recipe-form > .actions { margin-top: 1.5rem; }

.errors {
  margin-bottom: 1rem;
  padding: 0.75rem 1rem;
  background: var(--error-bg);
  border: 1px solid var(--danger);
  border-radius: 8px;
}

.errors p { margin: 0 0 0.25rem; font-weight: 600; }
.errors ul { margin: 0; padding-left: 1.2rem; }

/* Recipe PDF import */
.row-source { grid-column: 1 / -1; font-size: 0.78rem; color: var(--muted); overflow-wrap: anywhere; }
.ingredient-row.needs-review { padding: 0.5rem; margin: 0 -0.5rem; background: var(--warn-bg); border-radius: 8px; }
.ingredient-row.needs-review .row-source { color: var(--warn); }
.notice ul.notice-points { display: block; list-style: disc; margin: 0.25rem 0 0.5rem; padding-left: 1.2rem; }
.pdf-text {
  max-height: 16rem;
  overflow: auto;
  margin: 0.5rem 0 0;
  padding: 0.6rem;
  font-size: 0.8rem;
  white-space: pre-wrap;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* Phones: stack the ingredient line into two rows so nothing is cramped */
@media (max-width: 520px) {
  .site-header { flex-direction: column; align-items: flex-start; gap: 0.3rem; padding-bottom: 0.5rem; }
  .site-header nav { width: 100%; gap: 1.1rem; }
  .import-fields { grid-template-columns: 1fr; }
  .macro-tiles { grid-template-columns: repeat(2, 1fr); }
  .goals-grid { grid-template-columns: 1fr 4.5rem 4.5rem; }
  .goal-check { flex-direction: column; gap: 0; }
  .hide-narrow { display: none; }
  .ingredients th, .ingredients td { padding: 0.5rem 0.45rem; }
  .ingredients .num { white-space: nowrap; }
  .rounded-note { white-space: normal; }
  .toggle { margin-left: 0; }
  .row-headings { display: none; }
  .ingredient-row {
    grid-template-columns: 1fr 1fr 2.25rem;
    padding: 0.6rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
  }
  .ingredient-row .name { grid-column: 1 / 3; }
  .ingredient-row .remove { grid-column: 3; }
  .ingredient-row .note { grid-column: 1 / 3; }
}
