:root {
  --bg: #f5f6f3;
  --card: #ffffff;
  --text: #1d2420;
  --muted: #6b746f;
  --line: #e2e5e0;
  --accent: #2f7d4f;
  --accent-ink: #ffffff;
  --warn: #b7791f;
  --danger: #c0392b;
  --radius: 14px;
  --tabbar-h: 62px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121513;
    --card: #1b201d;
    --text: #e7ece8;
    --muted: #96a19a;
    --line: #2c332e;
    --accent: #4caf78;
    --accent-ink: #0d1510;
  }
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.wrap {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom));
}
h1 { font-size: 1.5rem; margin: 0.25rem 0 0.75rem; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: 1rem; margin: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small, small { font-size: 0.82rem; }
.error { color: var(--danger); }
.warn { color: var(--warn); font-weight: 600; }
.brand { text-align: center; margin: 15vh 0 1.5rem; color: var(--accent); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card > header, .pagehead, .daybar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.card > header h2, .pagehead h1 { flex: 1; }
.card > header { margin-bottom: 8px; }
.daybar { justify-content: space-between; }
.daybar h1 { margin: 0; }
.note {
  background: color-mix(in srgb, var(--accent) 10%, var(--card));
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.9rem;
}

.stack { display: flex; flex-direction: column; gap: 10px; }
.row2, .row3, .row4, .row5 { display: grid; gap: 8px; }
.row2 { grid-template-columns: repeat(2, 1fr); }
.row3 { grid-template-columns: repeat(3, 1fr); }
.row4 { grid-template-columns: repeat(4, 1fr); }
.row5 { grid-template-columns: 1.2fr 1.2fr 1fr 1fr 1fr; gap: 4px; }
.row5 input { padding: 8px 5px; }
.row-form { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.row-form label { flex: 1; min-width: 120px; }

label { display: flex; flex-direction: column; gap: 4px; font-size: 0.88rem; color: var(--muted); min-width: 0; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 10px;
  width: 100%;
  min-width: 0;
}
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 14px;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:disabled { opacity: 0.6; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 5px 10px; font-size: 0.85rem; }
.btn.danger { color: var(--danger); background: transparent; border-color: var(--danger); margin-top: 8px; }
.btn.done { color: var(--accent); border-color: var(--accent); }
.btn.on { background: color-mix(in srgb, var(--accent) 18%, transparent); }

.budget-row { display: flex; justify-content: space-around; text-align: center; }
.budget-row div { display: flex; flex-direction: column; }
.budget-row strong { font-size: 1.5rem; }
.budget-row small { color: var(--muted); }
.budget-row .over strong { color: var(--danger); }
.bar { height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; margin: 12px 0 8px; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.bar span.over { background: var(--danger); }
.macros { display: flex; justify-content: space-around; font-size: 0.85rem; color: var(--muted); }

.entries, .list, .ingredients, .checklist { list-style: none; margin: 0; padding: 0; }
.entries li, .list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.entries li:first-child, .list li:first-child { border-top: 0; }
.entries li > div { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.entries .name, .list li > a, .list li > span:first-child { overflow-wrap: anywhere; }
.list li > a, .list li > span:first-child { flex: 1; }
.entries .kcal { font-weight: 600; }
.list form { margin: 0; }

.actions-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 6px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.9rem;
}
.tile span { font-size: 1.5rem; }

.nutri { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; }
.nutri div { display: flex; flex-direction: column; }
.nutri strong { font-size: 1.25rem; }
.nutri small { color: var(--muted); }

.photo-pick {
  align-items: center;
  justify-content: center;
  min-height: 160px;
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  overflow: hidden;
  color: var(--text);
  font-weight: 600;
}
.photo-pick input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.photo-pick img { width: 100%; max-height: 320px; object-fit: cover; }
.thumb { width: 100%; border-radius: 10px; margin-top: 10px; }
.review-row { border: 0; border-top: 1px solid var(--line); padding: 10px 0 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.review-row.off { opacity: 0.45; }
.review-row label.check input[name="name"] { flex: 1; }
.badge { font-size: 0.72rem; padding: 2px 8px; border-radius: 99px; background: var(--line); color: var(--muted); }
.badge.low { background: color-mix(in srgb, var(--warn) 25%, transparent); color: var(--warn); }
.total { text-align: right; margin: 0; }

.viewfinder { position: relative; aspect-ratio: 4 / 3; background: #000; border-radius: var(--radius); overflow: hidden; }
.viewfinder video { width: 100%; height: 100%; object-fit: cover; }
.reticle { position: absolute; inset: 35% 12%; border: 2px solid rgb(255 255 255 / 0.85); border-radius: 8px; }

.chips { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips legend { font-size: 0.88rem; color: var(--muted); margin-bottom: 4px; }
.chips > div { display: contents; }
.chip { flex-direction: row; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 99px; padding: 6px 12px; color: var(--text); }
.chips ul { display: contents; list-style: none; }

.planned { border-top: 1px solid var(--line); padding: 10px 0; }
.planned:first-of-type { border-top: 0; }
.planned header, .planned footer { display: flex; align-items: center; gap: 8px; }
.planned header > div { flex: 1; }
.planned footer small { flex: 1; }
.planned p { margin: 6px 0; font-size: 0.92rem; }
.ingredients { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.ingredients li { font-size: 0.8rem; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); }
.ingredients li.have { border-color: var(--accent); color: var(--accent); }
.checklist li { padding: 4px 0; }
.spark { width: 100%; height: auto; color: var(--accent); }

details > summary { cursor: pointer; font-weight: 600; }
details[open] > summary { margin-bottom: 10px; }

.messages { position: sticky; top: 8px; z-index: 5; }
.msg { background: var(--text); color: var(--bg); border-radius: 10px; padding: 10px 14px; margin-bottom: 8px; }
.msg-error { background: var(--danger); color: #fff; }

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  justify-content: space-around;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.tabbar a {
  flex: 1;
  max-width: 128px;
  height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  color: var(--muted);
  text-decoration: none;
}
.tabbar a span { font-size: 1.3rem; line-height: 1.2; }
.tabbar a.on { color: var(--accent); font-weight: 600; }

@media (max-width: 420px) {
  .row4 { grid-template-columns: repeat(2, 1fr); }
}
