:root {
  --bg: #f6f8f3;
  --surface: #ffffff;
  --surface-2: #eef2ea;
  --text: #1b1f1a;
  --muted: #5f6a5c;
  --line: #dfe5da;
  --primary: #2e7d32;
  --primary-soft: #dcefd8;
  --on-primary: #ffffff;
  --good: #2e7d32;
  --bad: #c62828;
  --warn: #e65100;
  --radius: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111410;
    --surface: #1b1f1a;
    --surface-2: #242a22;
    --text: #e4e8e0;
    --muted: #9aa596;
    --line: #2f362d;
    --primary: #8fd18a;
    --primary-soft: #23391f;
    --on-primary: #0d2a0c;
    --good: #8fd18a;
    --bad: #ff8a80;
    --warn: #ffb74d;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
/* App-like touch: no double-tap zoom delay, no text selection or callouts on controls. */
button, a, label, .row, .card, .chip, #tabs { touch-action: manipulation; }
button, .chip, #tabs, .slot, .row[data-action], .row[data-pick], .row[data-preview], .row[data-to] {
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
h1 { font-size: 1.6rem; margin: 0; }
h2 { font-size: 1.15rem; margin: 20px 0 8px; }
h3 { margin: 0; }
p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.strong { font-weight: 600; }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn { color: var(--warn); }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; }
.sync { font-size: .75rem; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.sync.ok::before { content: "● "; color: var(--good); }
.sync.busy::before { content: "● "; color: var(--warn); }
.sync.off::before { content: "● "; color: var(--bad); }

main {
  max-width: 720px; margin: 0 auto;
  padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}

#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}
#tabs button {
  border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: .75rem; color: var(--muted);
}
#tabs .ti { font-size: 1.25rem; line-height: 1; padding: 4px 18px; border-radius: 99px; }
#tabs button.on { color: var(--text); font-weight: 600; }
#tabs button.on .ti { background: var(--primary-soft); }
body.logged-out #tabs, body.logged-out .topbar { display: none; }

/* ---------- page parts ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 4px 0 12px; }
.section { font-size: .9rem; color: var(--primary); margin: 18px 0 6px; }
.empty { text-align: center; padding: 40px 16px; }
.empty h3 { margin-bottom: 6px; }

.add-row { display: flex; gap: 8px; margin-bottom: 10px; }
input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); outline: none;
  font-size: 16px; /* stops iOS zooming in */
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea { resize: vertical; }
.search { margin-bottom: 10px; }
input[type=range] { width: 100%; accent-color: var(--primary); }
input[type=checkbox] { accent-color: var(--primary); width: 20px; height: 20px; }

.fab-small {
  flex: none; width: 48px; border-radius: 12px; border: 0;
  background: var(--primary); color: var(--on-primary); font-size: 1.4rem;
}
.btn {
  border: 0; border-radius: 99px; padding: 10px 18px; font-weight: 600;
  background: var(--primary); color: var(--on-primary);
}
.btn.outline { background: transparent; color: var(--primary); border: 1px solid var(--line); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.danger.outline { background: transparent; color: var(--bad); }
.btn.wide { width: 100%; padding: 13px; margin: 4px 0; }
.btn:disabled { opacity: .6; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.btn-row.end { justify-content: flex-end; }
.text-btn { border: 0; background: none; color: var(--primary); font-weight: 600; padding: 8px 4px; white-space: nowrap; }
.icon-btn {
  flex: none; border: 0; background: none; width: 44px; height: 44px; border-radius: 99px;
  font-size: 1.1rem; color: var(--muted); display: inline-grid; place-items: center;
}
.icon-btn:active, .text-btn:active { background: var(--surface-2); }
.icon-btn.on { color: var(--text); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips.tight { padding: 0; flex-wrap: wrap; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 6px 12px; font-size: .875rem; white-space: nowrap;
}
.chip.on { background: var(--primary-soft); border-color: transparent; font-weight: 600; }

.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.row { display: flex; align-items: center; gap: 10px; padding: 10px 6px 10px 14px; border-bottom: 1px solid var(--line); min-height: 56px; }
.row:last-child { border-bottom: 0; }
.row[data-action], .row[data-pick] { cursor: pointer; }
.row-main { flex: 1; min-width: 0; cursor: pointer; }
.row-title { font-weight: 500; }
.row-sub { font-size: .85rem; color: var(--muted); }
.row.done .row-title { text-decoration: line-through; color: var(--muted); }
.check { display: inline-grid; place-items: center; }

.cards { display: grid; gap: 10px; }
.card {
  display: block; width: 100%; text-align: left; border: 0; background: var(--surface);
  border-radius: var(--radius); padding: 14px; box-shadow: 0 1px 2px rgb(0 0 0 / .04);
}
.card-title { font-weight: 600; font-size: 1.02rem; }
.bar { height: 6px; background: var(--surface-2); border-radius: 99px; margin: 10px 0 6px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--good); border-radius: 99px; }
.hero { background: var(--primary-soft); }

.slot { display: flex; align-items: center; gap: 6px; padding: 10px 4px 10px 14px; margin-bottom: 8px; }
.slot.empty { color: var(--muted); background: var(--surface-2); gap: 12px; padding: 14px; }
.slot-main { flex: 1; min-width: 0; }
.slot-type { font-size: .7rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); font-weight: 600; min-width: 72px; }
.slot-actions { display: flex; }
.slot.cooked .card-title { text-decoration: line-through; color: var(--muted); }

/* recipe detail */
.detail-head { display: flex; align-items: center; gap: 4px; margin: 0 -8px 4px; }
.detail-title { flex: 1; font-size: 1.35rem; }
.ings { list-style: none; padding: 0; margin: 0; }
.ing { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--line); }
.ing-name { flex: 1; }
.mark { width: 18px; text-align: center; }
.steps { padding-left: 22px; }
.steps li { margin: 8px 0; }
.steps li::marker { color: var(--primary); font-weight: 700; }

/* modal sheet */
#modal {
  /* Sized to the visible area, so sheets sit above the keyboard (see setupIosFeel). */
  position: fixed; left: 0; right: 0; top: var(--vvtop, 0px); height: var(--vvh, 100%); z-index: 20; display: none;
  overscroll-behavior: contain;
  background: rgb(0 0 0 / .4); align-items: flex-end; justify-content: center;
}
#modal.open { display: flex; }
.sheet {
  width: 100%; max-width: 560px; max-height: calc(var(--vvh, 100vh) - max(12px, env(safe-area-inset-top)));
  overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  transition: transform .18s ease-out;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
  animation: up .18s ease-out;
}
@media (min-width: 600px) { #modal { align-items: center; } .sheet { border-radius: 20px; } }
@keyframes up { from { transform: translateY(30px); opacity: .5; } }
.sheet h2 { margin-top: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
.stack label input, .stack label select, .stack label textarea { color: var(--text); }
.stack hr { width: 100%; border: 0; border-top: 1px solid var(--line); margin: 8px 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg label { flex-direction: row; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 10px; color: var(--text); }
.seg input:checked + span { background: var(--primary-soft); border-color: transparent; font-weight: 600; }
.check-line { flex-direction: row !important; align-items: center; gap: 10px !important; color: var(--text) !important; font-size: 1rem !important; padding: 4px 0; }
.checks { display: flex; flex-direction: column; }
.pick-list { max-height: 50vh; overflow-y: auto; }

/* login */
.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login .logo { font-size: 3.5rem; }
.login form { margin-top: 20px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 30;
  background: #263238; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: .9rem;
  max-width: calc(100% - 32px); transition: .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* online recipes */
.head-actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.source-link { color: var(--primary); text-decoration: none; font-weight: 600; }
.row .fa-chevron-right { padding-right: 8px; }

/* choose meals */
.stepper { display: flex; align-items: center; gap: 2px; }
.stepper output { min-width: 1.6em; text-align: center; font-weight: 600; font-size: 1.1rem; }
.stepper .icon-btn:disabled { opacity: .35; }
.stepper-row.disabled .row-title { color: var(--muted); }

/* ---------- search box with clear button ---------- */
.search-box { position: relative; margin-bottom: 10px; flex: 1; }
.search-box > .fa-magnifying-glass { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; font-size: .9rem; }
.search-box input { padding-left: 36px !important; padding-right: 44px !important; }
.add-row .search-box { margin-bottom: 0; }
.search-clear {
  position: absolute; right: 2px; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; border: 0; background: none; color: var(--muted); font-size: 1.05rem;
}
.search-clear[hidden] { display: none; }
input[type=search]::-webkit-search-cancel-button, input[type=search]::-webkit-search-decoration { -webkit-appearance: none; appearance: none; }

/* ---------- online preview ---------- */
.preview-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 0 -4px; }
.preview-step { display: flex; align-items: center; gap: 2px; }
.preview-step .icon-btn:disabled { opacity: .3; }
.preview-steps summary { cursor: pointer; font-weight: 600; padding: 10px 0; }
.sheet-actions { position: sticky; bottom: calc(-20px - env(safe-area-inset-bottom)); background: var(--bg); padding: 10px 0 4px; margin-bottom: -4px; }

/* ---------- sheets: grabber, keyboard, scroll lock ---------- */
.grabber { position: relative; width: 36px; height: 5px; border-radius: 99px; background: var(--line); margin: 2px auto 12px; }
.grabber::before { content: ""; position: absolute; inset: -14px -60px; } /* bigger touch area */
body.modal-open { overflow: hidden; }
/* The tab bar always stays put; sheets (z-index 20) slide over it. */

/* ---------- press feedback ---------- */
.card, .row, .chip, .btn, .text-btn, .icon-btn, .fab-small, #tabs button { transition: transform .08s, opacity .08s, background-color .15s; }
.card:active, .btn:active, .fab-small:active, .chip:active { transform: scale(.98); }
.row[data-action]:active, .row[data-pick]:active, .row[data-preview]:active, .row[data-to]:active { background: var(--surface-2); }
#tabs button:active .ti { transform: scale(.9); }

/* ---------- meal plan drag and drop ---------- */
.slot[data-meal-id] { cursor: grab; }
.drag-ghost {
  position: fixed; z-index: 40; margin: 0; pointer-events: none;
  transform: scale(1.03) rotate(-1deg); box-shadow: 0 12px 28px rgb(0 0 0 / .22); opacity: .96;
}
.drag-origin { opacity: .35; }
.drop-target { outline: 2px dashed var(--primary); outline-offset: 2px; background: var(--primary-soft) !important; }
body.dragging, body.dragging * { cursor: grabbing !important; -webkit-user-select: none; user-select: none; }

/* ---------- plan: week / month ---------- */
.seg-toggle { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 10px; padding: 2px; margin-bottom: 10px; }
.seg-toggle button { border: 0; background: none; padding: 7px 0; border-radius: 8px; font-size: .9rem; font-weight: 600; color: var(--muted); }
.seg-toggle button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.week-nav { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin: 2px -6px 6px; }
.week-nav .icon-btn:disabled { opacity: .3; }
.week-title { display: flex; flex-direction: column; align-items: center; text-align: center; line-height: 1.25; }
.today-btn { display: block; margin: -4px auto 8px; }
.section.past { color: var(--muted); }
.slot.past { opacity: .7; }

.cal { padding: 8px 6px 10px; margin-bottom: 4px; }
.cal-head, .cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); }
.cal-head span { text-align: center; font-size: .75rem; font-weight: 600; color: var(--muted); padding: 4px 0 6px; }
.cal-day {
  border: 0; background: none; padding: 4px 0 6px; min-height: 48px; border-radius: 10px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.cal-day .num { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 99px; font-size: .95rem; }
.cal-day.other .num { color: var(--muted); opacity: .5; }
.cal-day.past .num { color: var(--muted); }
.cal-day.today .num { color: var(--primary); font-weight: 700; }
.cal-day.sel .num { background: var(--primary); color: var(--on-primary); font-weight: 700; }
.cal-day .dots { display: flex; gap: 3px; height: 6px; }
.cal-day .dot { width: 6px; height: 6px; border-radius: 99px; background: var(--primary); }
.cal-day .dot.done { background: none; box-shadow: inset 0 0 0 1.5px var(--muted); }

/* ---------- counts ---------- */
.count-field { display: flex; align-items: center; gap: 4px; }
.count-field input { text-align: center; width: 5.5em !important; flex: none; }
.count-line { justify-content: space-between; }
.small-field input { width: 3.6em !important; padding: 8px 4px !important; }

/* ---------- name links ---------- */
.ing { align-items: center; }
.ing .link-btn { width: 36px; height: 36px; margin: -6px -8px -6px 0; color: var(--muted); font-size: .9rem; }
