:root {
  --bg: #ffffff;
  --fg: #111418;
  --muted: #545c69;
  --border: #d3d8df;
  --surface: #f4f6f8;
  --accent: #0a7039;
  --accent-fg: #ffffff;
  --accent-soft: #e3f3ea;
  --warn: #9a3a00;
  --danger: #b3261e;
  --space: 1rem;
  --radius: 10px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --fg: #e8ebef;
    --muted: #a0a9b6;
    --border: #2d343e;
    --surface: #181c22;
    --accent: #5fd08f;
    --accent-fg: #06210f;
    --accent-soft: #15301f;
    --warn: #ffb37a;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0 auto;
  max-width: 44rem;
  padding: var(--space);
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1 { font-size: 1.6rem; line-height: 1.2; margin: calc(var(--space) / 2) 0; }
h2, h3 { line-height: 1.3; }
.h-small { font-size: 1.05rem; margin: 0 0 calc(var(--space) / 2); }
.lede { margin: 0 0 var(--space); }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.warn { color: var(--warn); }
.big { font-size: 1.1rem; margin: 0 0 0.5rem; }
.big strong { font-size: 1.6rem; }
p { margin: 0.4rem 0; }
a { color: var(--accent); }

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

/* Controls */
button, input, select, textarea { font: inherit; color: inherit; }
button {
  min-height: 44px;
  padding: 0 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.danger { color: var(--danger); border-color: currentColor; background: transparent; }
button.link {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0;
  border: 0;
  background: none;
  text-align: left;
  font-weight: 600;
  color: var(--fg);
  text-decoration: underline;
  text-decoration-color: var(--border);
  text-underline-offset: 3px;
}
.icon-btn { width: 44px; padding: 0; display: inline-grid; place-items: center; }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

input:not([type=checkbox]), select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  font-size: 16px; /* stops iOS zooming on focus */
}
textarea { resize: vertical; }
input[type=checkbox] { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); flex: none; }
[aria-invalid="true"] { border-color: var(--danger) !important; }
.error { color: var(--danger); font-size: 0.875rem; margin: 0.25rem 0 0; }
.error:empty { display: none; }

.field { margin-bottom: 0.75rem; min-width: 0; }
.field label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 0.25rem; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(9.5rem, 1fr)); gap: 0 0.75rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 0.5rem; }
.grid-3 .grow { grid-column: 1 / -1; }
@media (min-width: 34rem) { .grid-3 { grid-template-columns: 1fr 5rem 7rem; } .grid-3 .grow { grid-column: auto; } }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.row.between { justify-content: space-between; }
.toolbar { margin: var(--space) 0 0.5rem; }
.check { display: inline-flex; align-items: center; gap: 0.5rem; min-height: 44px; }

.stepper { display: grid; grid-template-columns: 44px 1fr 44px; gap: 0.25rem; }
.stepper input { text-align: center; }
.stepper button { padding: 0; font-size: 1.25rem; }

fieldset { border: 0; margin: 0 0 var(--space); min-width: 0; }
legend { font-weight: 700; padding: 0; margin-bottom: 0.5rem; float: left; width: 100%; }
legend + * { clear: both; }
.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 0.75rem; }
.chip {
  display: inline-flex; align-items: center; gap: 0.5rem;
  min-height: 44px; padding: 0 0.85rem;
  border: 1px solid var(--border); border-radius: 999px; background: var(--bg); cursor: pointer;
}
.chip:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

/* Layout pieces */
.tabs {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  margin: 0 calc(var(--space) * -1) var(--space);
  padding: 0 var(--space);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.tabs button {
  min-height: 48px; padding: 0 0.25rem;
  border: 0; border-bottom: 3px solid transparent; border-radius: 0;
  background: none; color: var(--muted); font-weight: 600; font-size: 0.95rem;
}
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space);
  margin-bottom: 0.75rem;
}
.banner { border: 2px solid var(--accent); background: var(--accent-soft); border-radius: var(--radius); padding: var(--space); margin-bottom: var(--space); }
.empty { color: var(--muted); }
.plain { list-style: none; margin: 0; padding: 0; }

.bar { height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; margin: 0.5rem 0; }
.bar span { display: block; height: 100%; background: var(--accent); }
.bar.over span { background: var(--warn); }

.day h2 { margin-bottom: 0; }
.meal {
  display: grid; grid-template-columns: 1fr auto; gap: 0.5rem; align-items: center;
  padding: 0.6rem 0; border-top: 1px solid var(--border);
}
.day h2 + .meal { border-top: 0; }
.meal-info { display: flex; flex-direction: column; min-width: 0; }
.meal-type { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.meal-actions { display: flex; gap: 0.25rem; }
.meal.locked .meal-type::after { content: " · locked"; color: var(--accent); }

.aisle { margin-bottom: var(--space); }
.aisle h2 { margin: var(--space) 0 0.25rem; }
.item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 0.75rem; align-items: center;
  padding: 0 0.25rem 0 0.75rem; border-bottom: 1px solid var(--border);
}
.item label { display: flex; flex-direction: column; justify-content: center; min-height: 52px; padding: 0.35rem 0; cursor: pointer; }
.item.done .item-name { text-decoration: line-through; color: var(--muted); }

.recipe-card {
  display: flex; flex-direction: column; gap: 0.2rem; align-items: flex-start;
  width: 100%; padding: 0.75rem var(--space); margin-bottom: 0.5rem; text-align: left;
}
.recipe-name { font-weight: 600; }
.tags { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.badge {
  display: inline-block; font-size: 0.75rem; font-weight: 600; line-height: 1.6;
  padding: 0 0.5rem; margin-left: 0.35rem; border: 1px solid var(--border); border-radius: 999px; color: var(--muted);
}
.tags .badge { margin-left: 0; }
.badge.accent { color: var(--accent); border-color: var(--accent); }

details > summary { cursor: pointer; font-weight: 700; min-height: 44px; display: flex; align-items: center; }
details[open] > summary { margin-bottom: 0.5rem; }

/* Dialogs */
dialog {
  width: min(34rem, calc(100% - 1.5rem)); max-height: 88vh;
  padding: 0; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--fg);
}
dialog::backdrop { background: rgb(0 0 0 / 0.55); }
.dialog-body { padding: var(--space) calc(var(--space) * 1.25); }
.dialog-body h2 { margin-top: 0; font-size: 1.25rem; }
.dialog-body h3 { font-size: 1rem; margin: var(--space) 0 0.25rem; }
.dialog-body ul, .dialog-body ol { padding-left: 1.25rem; margin: 0; }
.dialog-body li { margin: 0.2rem 0; }
.use-row {
  display: grid; grid-template-columns: 1fr auto; gap: 0.5rem; align-items: end;
  margin: var(--space) 0; padding: 0.75rem; border-radius: var(--radius); background: var(--accent-soft);
}
.use-row label { grid-column: 1 / -1; font-weight: 600; }

.toast {
  position: fixed; left: 50%; bottom: max(1rem, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  display: flex; gap: 0.75rem; align-items: center;
  width: max-content; max-width: calc(100% - 2rem);
  padding: 0.5rem 0.5rem 0.5rem 1rem; border-radius: var(--radius);
  background: var(--fg); color: var(--bg); box-shadow: 0 4px 16px rgb(0 0 0 / 0.25);
}
.toast span { padding: 0.35rem 0; }
.toast button { background: transparent; color: inherit; border-color: currentColor; font-weight: 700; flex: none; }

footer { margin-top: calc(var(--space) * 2); padding-top: var(--space); border-top: 1px solid var(--border); }
footer time { color: var(--muted); font-variant-numeric: tabular-nums; }
footer li { margin: 0.25rem 0; }
