:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d1f1e;
  --muted: #6d716e;
  --line: #e3dfd6;
  --accent: #2f7d5b;
  --accent-soft: #e2f0e8;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --today: #fff6dc;
  --radius: 14px;
  --tabbar-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #202326;
    --surface-2: #2a2e31;
    --text: #eceeed;
    --muted: #9aa19d;
    --line: #33383b;
    --accent: #5cc195;
    --accent-soft: #1f3a2e;
    --accent-text: #0d1f17;
    --danger: #ff7a6b;
    --today: #33301f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
/* 16px+ inputs stop iOS Safari from zooming in on focus */
input, select, textarea { font-size: 16px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: calc(var(--safe-t) + 10px) 12px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 650; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.topbar-left, .topbar-right { display: flex; gap: 4px; align-items: center; min-width: 0; }
.topbar-right { justify-content: flex-end; }

/* ---------- Main ---------- */
main {
  max-width: 640px; margin: 0 auto;
  padding: 12px 16px calc(var(--tabbar-h) + var(--safe-b) + 96px);
}

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 550;
}
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 15px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  min-width: 40px; height: 40px; padding: 0 8px;
  border: 0; border-radius: 10px; background: transparent; color: var(--accent);
  font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active, .btn:active, .row:active { opacity: .7; }
.fab {
  position: fixed; z-index: 11;
  right: max(16px, calc((100vw - 640px) / 2 + 16px));
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text);
  box-shadow: 0 6px 18px rgb(0 0 0 / .22);
  display: grid; place-items: center; cursor: pointer;
}
.fab svg { width: 28px; height: 28px; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; fill: none; }

/* ---------- Inputs ---------- */
.field {
  width: 100%; min-height: 46px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); outline: none;
}
.field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.field { min-height: 120px; resize: vertical; line-height: 1.45; }
select.field { appearance: none; -webkit-appearance: none; padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 14px) 50%; background-size: 6px 6px; background-repeat: no-repeat; }
.search { position: relative; margin-bottom: 12px; }
.search .field { padding-left: 38px; background: var(--surface-2); border-color: transparent; }
.search svg { position: absolute; left: 12px; top: 50%; translate: 0 -50%; width: 18px; height: 18px; stroke: var(--muted); fill: none; stroke-width: 2; stroke-linecap: round; }
.label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 6px; }

/* ---------- Lists ---------- */
.group-title { font-size: 13px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 18px 4px 6px; }
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list > li + li { border-top: 1px solid var(--line); }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px; padding: 10px 14px;
  background: none; border: 0; text-align: left; cursor: pointer; color: inherit; text-decoration: none;
}
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 560; overflow-wrap: anywhere; }
.row-sub { font-size: 14px; color: var(--muted); margin-top: 1px; overflow-wrap: anywhere; }
.chev { width: 9px; height: 9px; border-right: 2px solid var(--muted); border-top: 2px solid var(--muted); rotate: 45deg; opacity: .6; flex: none; }
.badge { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 48px 24px; }
.empty strong { display: block; color: var(--text); font-size: 18px; margin-bottom: 6px; }

/* ---------- Calendar ---------- */
.period { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.period-label { font-weight: 650; text-align: center; flex: 1; }
.day { display: flex; align-items: center; gap: 14px; }
.day-date { width: 46px; flex: none; text-align: center; line-height: 1.1; }
.day-date .dow { font-size: 12px; font-weight: 650; color: var(--muted); text-transform: uppercase; }
.day-date .num { font-size: 22px; font-weight: 650; }
li.today { background: var(--today); }
li.today .num { color: var(--accent); }
li.past { opacity: .55; }
.day .planned { font-weight: 560; }
.day .unplanned { color: var(--muted); }
.month-sep { font-size: 13px; font-weight: 650; color: var(--muted); padding: 8px 14px; background: var(--surface-2); text-transform: uppercase; letter-spacing: .05em; }
.summary { display: flex; gap: 8px; margin: 12px 0 0; }
.summary .btn { flex: 1; }

/* ---------- Meal detail ---------- */
.title-input { font-size: 24px; font-weight: 700; border: 0; background: transparent; width: 100%; padding: 6px 4px; outline: none; border-bottom: 2px solid transparent; }
.title-input:focus { border-bottom-color: var(--accent); }
.ing-row { display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 14px; }
.ing-row .row-title { flex: 1; min-width: 0; }
.qty { width: 110px; min-height: 38px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); outline: none; text-align: right; }
.qty:focus { border-color: var(--accent); background: var(--surface); }
.remove { width: 36px; height: 36px; border: 0; background: none; color: var(--muted); border-radius: 50%; cursor: pointer; font-size: 22px; line-height: 1; flex: none; }
.add-row { color: var(--accent); font-weight: 600; }
.save-state { font-size: 13px; color: var(--muted); padding-right: 6px; }
.inline { display: flex; gap: 8px; }
.inline .field { flex: 1; }

/* ---------- Shopping ---------- */
.check-row { display: flex; align-items: flex-start; gap: 12px; padding: 12px 14px; cursor: pointer; }
.check-row input { appearance: none; -webkit-appearance: none; flex: none; width: 24px; height: 24px; margin: 0; border: 2px solid var(--line); border-radius: 7px; display: grid; place-items: center; cursor: pointer; background: var(--surface); }
.check-row input:checked { background: var(--accent); border-color: var(--accent); }
.check-row input:checked::after { content: ""; width: 6px; height: 11px; border: solid var(--accent-text); border-width: 0 2.5px 2.5px 0; rotate: 45deg; translate: 0 -1px; }
.check-row.done .row-title { text-decoration: line-through; color: var(--muted); }
.check-row.done .row-sub { opacity: .6; }
.range { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.range label { font-size: 13px; color: var(--muted); font-weight: 600; min-width: 0; }
.range input { min-width: 0; max-width: 100%; -webkit-appearance: none; appearance: none; }
.range input { margin-top: 4px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.toolbar .btn { flex: 1; }

/* ---------- Bottom sheet ---------- */
.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px;
  max-height: calc(100dvh - var(--safe-t) - 24px);
  background: var(--surface); color: var(--text);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -10px 40px rgb(0 0 0 / .25);
}
.sheet::backdrop { background: rgb(0 0 0 / .38); }
.sheet[open] { animation: sheet-up .22s ease-out; }
@keyframes sheet-up { from { transform: translateY(60px); } }
.sheet-inner { padding: 8px 16px calc(var(--safe-b) + 20px); }
.grabber { width: 38px; height: 5px; border-radius: 99px; background: var(--line); margin: 0 auto 8px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 19px; }
.sheet .list { border-radius: 12px; }
.sheet .scroll { max-height: 42dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; border-radius: 12px; }
.sheet p { margin: 8px 2px; }
.sheet .stack > * + * { margin-top: 10px; }
.check { color: var(--accent); font-weight: 700; }
.hint { font-size: 14px; color: var(--muted); min-height: 1.3em; margin: 6px 4px 0; }
.hint.warn { color: var(--danger); }
.url-box { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; user-select: all; -webkit-user-select: all; }
.steps { padding-left: 20px; margin: 8px 0; }
.steps li { margin: 4px 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; translate: -50% 0; z-index: 100;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 90px);
  max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 15px; font-weight: 550;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; transform: translateY(8px);
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; text-align: center; display: grid; gap: 12px; }
.login-card h1 { margin: 4px 0 0; }
.login-card p { margin: 0; }
.login-card img { margin: 0 auto; border-radius: 16px; }
.login-card .field { text-align: center; }

/* ---------- Small utilities ---------- */
.center { text-align: center; }
p.center { margin: 12px 0 0; }
.danger-zone { margin-top: 32px; }
select.field.compact { width: auto; min-height: 38px; padding: 4px 32px 4px 10px; flex: none; max-width: 42%; }

/* ---------- Frozen ---------- */
.snow { color: #3a8fd6; font-size: .9em; }
.thaw { color: #3a8fd6; }
@media (prefers-color-scheme: dark) { .snow, .thaw { color: #7cbcf0; } }
.toggle-row { display: flex; align-items: flex-start; gap: 12px; padding: 10px 4px; cursor: pointer; font-size: 15px; line-height: 1.35; }
.toggle-row .muted { font-size: 14px; }
.checkbox { appearance: none; -webkit-appearance: none; flex: none; width: 24px; height: 24px; margin: 0; border: 2px solid var(--line); border-radius: 7px; display: grid; place-items: center; cursor: pointer; background: var(--surface); }
.checkbox:checked { background: var(--accent); border-color: var(--accent); }
.checkbox:checked::after { content: ""; width: 6px; height: 11px; border: solid var(--accent-text); border-width: 0 2.5px 2.5px 0; rotate: 45deg; translate: 0 -1px; }

/* ---------- At home / to buy ---------- */
.item-row { display: flex; align-items: center; padding-right: 10px; }
.item-row > .row, .item-row > .check-row { flex: 1; min-width: 0; }
.have {
  flex: none; min-height: 34px; padding: 0 12px; border-radius: 99px; cursor: pointer;
  border: 1.5px solid var(--line); background: transparent; color: var(--muted);
  font-size: 14px; font-weight: 600; white-space: nowrap;
}
.have.on { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.have:disabled { opacity: .5; }
.segmented { display: flex; gap: 4px; padding: 3px; margin: -2px 0 6px; background: var(--surface-2); border-radius: 12px; }
.segmented button { flex: 1; min-height: 36px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-size: 14px; font-weight: 600; cursor: pointer; padding: 0 4px; }
.segmented button span { font-weight: 500; opacity: .75; }
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.section-head { display: flex; align-items: center; gap: 8px; font-size: 20px; margin: 22px 4px 0; }
.section-head .badge { font-size: 13px; }
.muted-badge { background: var(--surface-2); color: var(--muted); }
.row-sub.stale { color: var(--danger); }
.empty.compact { padding: 20px 16px; }

/* Narrow phones (iPhone SE): the back link shrinks to just its arrow so it doesn't crowd the title. */
@media (max-width: 374px) {
  .topbar-left .icon-btn span { display: none; }
  .topbar h1 { max-width: 52vw; }
}

/* ---------- Keyboard-proof sheets ---------- */
html.sheet-open, html.sheet-open body { overflow: hidden; }
.sheet.tall {
  /* Pinned to the top of the visible area and sized to it, so the keyboard opening/closing doesn't move anything. */
  margin: calc(var(--vvtop, 0px) + var(--safe-t) + 12px) auto 0;
  height: calc(var(--vvh, 100dvh) - var(--safe-t) - 12px);
  max-height: none;
}
.sheet.tall .sheet-inner { height: 100%; display: flex; flex-direction: column; padding-bottom: max(12px, var(--safe-b)); }
.sheet.tall .sheet-inner > * { flex: none; }
.sheet.tall .sheet-inner > .scroll { flex: 1 1 auto; min-height: 80px; max-height: none; }
.sheet.tall .search { margin-bottom: 10px; }

/* ---------- Hand-added list items / Reminders ---------- */
.btn.on-list { color: var(--accent); border-color: var(--accent); }
.item-row .remove { margin-left: 2px; }
.howto { margin: 14px 4px 0; font-size: 15px; }
.howto summary { color: var(--accent); font-weight: 600; cursor: pointer; padding: 6px 0; }
.howto p { margin: 8px 0; }
#listToggle { margin-top: 10px; }
.toolbar .btn.primary[data-action=share-list] { flex-basis: 100%; }
