:root {
  --bg: #f5f6f2;
  --card: #ffffff;
  --text: #1d2420;
  --muted: #66706a;
  --line: #e2e5df;
  --brand: #2f7d4f;
  --brand-ink: #ffffff;
  --accent: #e9f3ec;
  --ok: #2f7d4f;
  --bad: #c0392b;
  --warn: #d58a1d;
  --bar-a: #2f7d4f;
  --bar-b: #c9a227;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --card: #1b211e;
    --text: #e7ece9;
    --muted: #9aa59f;
    --line: #2c3530;
    --brand: #4fb27a;
    --brand-ink: #0b130e;
    --accent: #20302a;
    --ok: #5cc48a;
    --bad: #ef7466;
    --warn: #e7a84b;
    --bar-a: #4fb27a;
    --bar-b: #d9b84a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
a { color: var(--brand); }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; justify-content: space-between; align-items: center;
  padding: .7rem 1rem; background: var(--brand); color: var(--brand-ink);
}
.top a { color: inherit; text-decoration: none; }
.brand { font-weight: 700; font-size: 1.1rem; }
.gear { font-size: 1.4rem; line-height: 1; padding: .2rem .4rem; border-radius: 8px; }
.top-right { display: flex; align-items: center; gap: .5rem; }
.sync { font-size: .75rem; opacity: .9; white-space: nowrap; }
.sync.offline, .sync.error { background: rgba(0,0,0,.25); padding: .15rem .5rem; border-radius: 99px; }
code.invite { font-size: 1.3rem; letter-spacing: .15em; font-weight: 700; padding: .35rem .7rem; background: var(--accent); border-radius: 8px; }
.gear.active { background: rgba(255,255,255,.2); }

main { max-width: 860px; margin: 0 auto; padding: 1rem; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(6, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: .45rem 0 .5rem; font-size: .7rem; color: var(--muted); text-decoration: none;
}
.tabbar a span { font-size: 1.25rem; }
.tabbar a.active { color: var(--brand); font-weight: 600; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem; margin-bottom: 1rem;
}
.hero { background: var(--accent); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

.muted { color: var(--muted); }
.small { font-size: .82rem; }
.ok { color: var(--ok); }
.bad { color: var(--bad); }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }

.row { display: flex; align-items: center; }
.between { justify-content: space-between; gap: .5rem; }
.gap { gap: .5rem; flex-wrap: wrap; margin: .5rem 0; }

button, .button, .upload {
  font: inherit; border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: .55rem .9rem; border-radius: 10px; cursor: pointer; display: inline-block;
}
button:hover, .button:hover { border-color: var(--brand); }
button.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
button.icon { padding: .25rem .55rem; background: transparent; border-color: transparent; font-size: 1.05rem; }
button.icon:hover { border-color: var(--line); }
.upload {
  display: block; text-align: center; padding: 1.1rem; margin: .75rem 0;
  border: 2px dashed var(--brand); background: var(--accent); font-weight: 600;
}

input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: .5rem .6rem; width: 100%; min-width: 0;
}
input[type="checkbox"] { width: 1.15rem; height: 1.15rem; accent-color: var(--brand); }
input.num { width: 6.5rem; }
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; }
label { display: block; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: .2rem; color: var(--text); }
label.check { display: flex; align-items: center; gap: .5rem; color: var(--text); font-size: 1rem; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .75rem; align-items: end; }
.grid .wide { grid-column: 1 / -1; }

.list { list-style: none; margin: .5rem 0 0; padding: 0; }
.list li {
  display: flex; justify-content: space-between; align-items: center; gap: .75rem;
  padding: .6rem 0; border-top: 1px solid var(--line);
}
.list li:first-child { border-top: 0; }
.list .right { display: flex; align-items: center; gap: .25rem; white-space: nowrap; }
.plain { padding-left: 1.1rem; margin: .4rem 0; }

.plan-row { align-items: flex-start !important; }
.plan-row .day { width: 3.2rem; display: flex; flex-direction: column; }
.plan-row .pick { flex: 1; min-width: 0; }
.plan-row .pick .small { margin-top: .2rem; }
.plan-row.today { background: var(--accent); border-radius: 8px; padding-left: .4rem; padding-right: .4rem; }
.lock { display: flex; align-items: center; gap: .2rem; color: var(--text); padding-top: .5rem; }
.total { margin-top: .75rem; padding-top: .75rem; border-top: 2px solid var(--line); text-align: right; }
.total.bad strong { color: var(--bad); }

.shop li label { display: flex; align-items: flex-start; gap: .6rem; color: var(--text); font-size: 1rem; flex: 1; }
.shop li.done strong { text-decoration: line-through; color: var(--muted); }
.add input { flex: 1; }

.ing-line { display: grid; grid-template-columns: 5rem 5rem 1fr auto; gap: .4rem; margin: .4rem 0; }

.tabs { display: inline-flex; gap: .25rem; background: var(--bg); padding: .2rem; border-radius: 10px; margin-bottom: .75rem; }
.tabs button { border: 0; background: transparent; padding: .4rem .75rem; }
.tabs button.active { background: var(--card); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
.tabs.small { margin: 0; }
.tabs.small button { padding: .25rem .55rem; font-size: .85rem; }

.table-wrap { overflow-x: auto; margin: .5rem -1rem; padding: 0 1rem; }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { text-align: left; padding: .4rem .35rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: .78rem; }
.rlines { list-style: none; padding: 0; margin: .5rem 0; }
.rlines li { display: grid; grid-template-columns: 6rem 1fr 7rem; gap: .5rem; align-items: end; padding: .7rem 0; border-top: 1px solid var(--line); }
.rlines .rl-name { grid-column: 1 / -1; display: flex; align-items: center; gap: .5rem; }
.rlines .rl-name input:not([type="checkbox"]) { margin: 0; font-weight: 600; }
.rlines .rl-prev { grid-column: 1 / -1; }
.rlines .rl-prev:empty { display: none; }
@media (max-width: 420px) { .rlines li { grid-template-columns: 5.5rem 1fr; } .rlines li > label:nth-of-type(4) { grid-column: 1 / -1; } }

.progress { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; margin-top: .5rem; }
.progress > div { height: 100%; background: var(--brand); transition: width .2s; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; margin-top: .5rem; }
.kpi { background: var(--bg); border-radius: 10px; padding: .7rem; font-size: .8rem; color: var(--muted); }
.kpi span { display: block; font-size: 1.25rem; font-weight: 700; color: var(--text); }

.insights { margin: 0; padding-left: 1.1rem; }
.insights li { margin: .3rem 0; }

.meter { margin: .7rem 0; }
.meter-label { display: flex; justify-content: space-between; font-weight: 600; font-size: .9rem; }
.meter-track { position: relative; height: 12px; background: var(--line); border-radius: 99px; margin: .25rem 0; }
.meter-fill { height: 100%; border-radius: 99px; }
.okbg { background: var(--ok); }
.warnbg { background: var(--warn); }
.meter-mark { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); opacity: .6; }

.macro { display: flex; height: 28px; border-radius: 8px; overflow: hidden; margin: 1rem 0 .4rem; font-size: .72rem; font-weight: 600; }
.macro > div { display: flex; align-items: center; justify-content: center; white-space: nowrap; overflow: hidden; color: #fff; min-width: 0; }
.m-prot { background: #2f7d4f; }
.m-carb { background: #c9a227; color: #1d2420 !important; }
.m-fat { background: #b5523b; }

.legend { display: flex; align-items: center; gap: .4rem; font-size: .8rem; color: var(--muted); margin-bottom: .5rem; }
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; margin-left: .5rem; }
.sw-a { background: var(--bar-a); }
.sw-b { background: var(--bar-b); }
.bars { display: grid; grid-template-columns: repeat(8, 1fr); gap: .35rem; align-items: end; height: 170px; }
.bar-group { display: flex; flex-direction: column; align-items: center; height: 100%; gap: .25rem; }
.bar-pair { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 2px; border-bottom: 1px solid var(--line); }
.bar { width: 40%; max-width: 18px; border-radius: 3px 3px 0 0; min-height: 1px; }
.bar.a { background: var(--bar-a); }
.bar.b { background: var(--bar-b); }

details { margin-top: .75rem; }
summary { cursor: pointer; color: var(--brand); font-weight: 600; }

#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 99px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 10; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 560px) {
  main { padding: .75rem; }
  .two { grid-template-columns: 1fr; }
  .ing-line { grid-template-columns: 4.2rem 4.6rem 1fr auto; }
}

.recipe-link { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--muted); }
.recipe-link:hover { color: var(--brand); }
a.button { text-decoration: none; color: var(--text); }
a.button.primary-link { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.recipe h3 { margin-top: 1.1rem; }
.ingredients { list-style: none; padding: 0; margin: .4rem 0; columns: 2; column-gap: 1.5rem; }
.ingredients li { padding: .25rem 0; break-inside: avoid; border-bottom: 1px solid var(--line); }
.ingredients .amt { display: inline-block; min-width: 4.5rem; font-weight: 600; }
.steps { padding-left: 1.4rem; margin: .4rem 0; }
.steps li { margin: .5rem 0; padding-left: .2rem; }
.note { background: var(--accent); padding: .6rem .8rem; border-radius: 8px; }
@media (max-width: 560px) { .ingredients { columns: 1; } }

.login-page { padding-bottom: 0; }
.login-page main { max-width: 420px; padding-top: 12vh; }
.login h1 { margin: 0 0 .25rem; font-size: 1.6rem; }
.login details { margin-top: 1rem; }
.login form { margin-top: .75rem; }
.grid.single { grid-template-columns: 1fr; }
a.button.block { display: block; text-align: center; padding: .75rem; margin-top: 1rem; }
.center { text-align: center; }
.alert { padding: .6rem .8rem; border-radius: 8px; background: var(--accent); }
.alert.bad { color: var(--bad); background: transparent; border: 1px solid var(--bad); }
.divider { display: flex; align-items: center; gap: .75rem; color: var(--muted); font-size: .85rem; margin-top: 1rem; }
.divider::before, .divider::after { content: ''; flex: 1; border-top: 1px solid var(--line); }

.ai-card { margin-top: 1rem; padding: 1rem; border-radius: 12px; background: var(--accent); }
.ai-card h3 { margin-bottom: .6rem; }
.ai-list > li { flex-direction: column; align-items: stretch !important; }
.ai-pick { display: flex; gap: .6rem; align-items: flex-start; color: var(--text); font-size: 1rem; }
.ai-pick input { margin-top: .2rem; flex: none; }
.spinner { display: inline-block; width: 1rem; height: 1rem; border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
button[disabled] { opacity: .5; cursor: not-allowed; }
.ai-list details li { display: list-item; border-top: 0; padding: .25rem 0; }
.ai-list details .ingredients li { border-bottom: 1px solid var(--line); }
