:root {
  --bg: #fbf6ef;
  --surface: #ffffff;
  --surface-2: #f4ede4;
  --text: #2b2622;
  --muted: #7b716a;
  --line: #e9e0d5;
  --accent: #e07a5f;
  --accent-ink: #ffffff;
  --accent-soft: #fbe3dc;
  --green: #4f8a6d;
  --green-soft: #dcefe5;
  --warn: #c7771a;
  --warn-soft: #fcebd3;
  --bad: #c0392b;
  --bad-soft: #f8dcd8;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(60, 40, 20, 0.06), 0 4px 16px rgba(60, 40, 20, 0.06);
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1b19;
    --surface: #282522;
    --surface-2: #332f2b;
    --text: #f3ede6;
    --muted: #a99f96;
    --line: #3d3834;
    --accent-soft: #4a2e26;
    --green-soft: #22382d;
    --warn-soft: #43321c;
    --bad-soft: #4a2420;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    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.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; font-weight: 750; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; font-weight: 700; }
h3 { font-size: 1rem; font-weight: 650; }
p { margin: 0.4em 0; }
a { color: var(--accent); text-decoration: none; }
img { max-width: 100%; display: block; }
input, select, textarea, button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---- Layout ---------------------------------------------------------------- */

.view {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 12px) 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
}
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; }
.row > .field, .row > input { flex: 1; min-width: 0; }
.row.end { justify-content: flex-end; }
.row.wrap { flex-wrap: wrap; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 0.85rem; }
.pre { white-space: pre-wrap; }
.warn-text { color: var(--warn); }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 0 16px;
}
.page-head h1 { overflow: hidden; text-overflow: ellipsis; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card > h2 { margin-bottom: 10px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.card-head a { font-weight: 600; font-size: 0.9rem; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.boot, .loading { display: grid; place-items: center; min-height: 40vh; }

/* ---- Tab bar -------------------------------------------------------------- */

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--line);
  z-index: 10;
}
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 600;
}
.tabbar .tab-icon { font-size: 1.35rem; filter: grayscale(1); opacity: 0.7; }
.tabbar a.active { color: var(--accent); }
.tabbar a.active .tab-icon { filter: none; opacity: 1; }

/* ---- Buttons & inputs ----------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 650;
  cursor: pointer;
  text-align: center;
}
.btn:disabled { opacity: 0.55; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.ghost { background: transparent; }
.btn.light { background: rgba(255, 255, 255, 0.9); color: #2b2622; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 0.9rem; border-radius: 10px; }
.btn.block { width: 100%; }
.danger-text { color: var(--bad) !important; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--surface-2);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  flex: none;
}
.icon-btn.small { width: 32px; height: 32px; font-size: 1rem; }
.icon-btn.accent { background: var(--accent); color: var(--accent-ink); font-size: 1.6rem; }

input:not([type=checkbox]):not([type=radio]), select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 16px; /* prevents iOS zoom on focus */
}
textarea { resize: vertical; }
input:focus, select, textarea:focus { outline: none; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }

.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-size: 0.85rem; font-weight: 650; color: var(--muted); }
.form-error { color: var(--bad); min-height: 1em; margin: 0; }
.check { display: flex; gap: 10px; align-items: center; min-height: 36px; }
details summary { cursor: pointer; color: var(--accent); font-weight: 600; padding: 6px 0; }
details[open] { display: flex; flex-direction: column; gap: 10px; }

.segmented { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.segmented > label, .segmented > button { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span, .segmented > button {
  display: block;
  padding: 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
}
.segmented input:checked + span, .segmented > button.on { background: var(--surface); box-shadow: var(--shadow); }
.segmented.tabs { margin-bottom: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 12px 4px 4px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 650; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.pantry-usually_home { background: var(--green-soft) !important; color: var(--green) !important; }
.pantry-always_add { background: var(--accent-soft) !important; color: var(--accent) !important; }

.avatar {
  display: inline-grid; place-items: center; flex: none;
  border-radius: 50%; color: #fff; font-weight: 700;
  width: 26px; height: 26px; font-size: 0.7rem;
}
.avatar.md { width: 38px; height: 38px; font-size: 0.9rem; }
.avatar-link { display: inline-flex; }
.avatars { display: inline-flex; gap: 2px; margin-left: auto; }

/* ---- Home ------------------------------------------------------------------ */

.hero {
  position: relative;
  margin: calc(-12px - var(--safe-top)) -16px 16px;
  min-height: calc(260px + var(--safe-top));
  aspect-ratio: 4 / 3;
  max-height: 58vh;
  overflow: hidden;
  background: linear-gradient(135deg, #e07a5f, #f2cc8f);
  color: #fff;
}
.hero img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.hero-shade { position: absolute; inset: 0; background: linear-gradient(to bottom, rgba(0,0,0,0.25), transparent 30%, transparent 45%, rgba(0,0,0,0.6)); }
.hero-settings { position: absolute; top: calc(var(--safe-top) + 12px); right: 16px; }
.hero-settings .avatar { box-shadow: 0 0 0 2px rgba(255,255,255,0.9); }
.hero-text { position: absolute; left: 20px; right: 20px; bottom: 18px; text-shadow: 0 1px 8px rgba(0,0,0,0.35); }
.hero-text h1 { font-size: 2rem; }
.hero-text p { margin: 0; font-weight: 600; }
.hero-text .btn { margin-top: 10px; text-shadow: none; }
.hero-date { opacity: 0.9; font-weight: 500 !important; }
@media (min-width: 720px) { .hero { margin: 0 0 16px; border-radius: var(--radius); } }

.hint-card {
  display: flex; flex-direction: column; gap: 2px;
  padding: 14px 16px; border-radius: var(--radius);
  background: var(--accent-soft); color: var(--text);
}
.hint-card strong { color: var(--accent); }

.day-label { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 10px 0 4px; }
.event-row, .meal-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; color: var(--text); border-bottom: 1px solid var(--line);
}
.event-row:last-child, .meal-row:last-child { border-bottom: 0; }
.event-time { width: 56px; flex: none; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.9rem; }
.event-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.event-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); width: 72px; flex: none; }

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.tile { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; color: var(--text); display: flex; flex-direction: column; }
.tile strong { font-size: 2rem; line-height: 1; color: var(--accent); }
.tile span { color: var(--muted); font-size: 0.9rem; }
.tile.warn strong { color: var(--warn); }

/* ---- Calendar ------------------------------------------------------------- */

.month-card { padding: 12px; margin-bottom: 14px; }
.month-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.month { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.dow { text-align: center; font-size: 0.75rem; color: var(--muted); font-weight: 600; padding: 4px 0; }
.day {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  aspect-ratio: 1 / 1; max-height: 64px; padding: 6px 0 0;
  border: 0; border-radius: 12px; background: transparent; cursor: pointer;
}
.day .num { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; font-variant-numeric: tabular-nums; }
.day.other { opacity: 0.35; }
.day.today .num { color: var(--accent); font-weight: 800; }
.day.selected .num { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.dots { display: flex; gap: 3px; height: 6px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; }

.agenda { display: flex; flex-direction: column; gap: 10px; }
.event-card {
  display: flex; align-items: stretch; gap: 12px; text-align: left;
  width: 100%; padding: 12px; border: 0; border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); cursor: pointer;
}
.event-card.message { background: var(--accent-soft); }
.event-bar { width: 4px; border-radius: 4px; flex: none; }
.event-main { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.event-main small { color: var(--muted); }
.event-main .notes { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.event-detail-title { font-size: 1.3rem; }

/* ---- Meals ------------------------------------------------------------------ */

.week-head { display: flex; align-items: center; justify-content: space-between; }
.plan-day { padding: 12px 16px; }
.plan-day .card-head { margin-bottom: 4px; }
.plan-day.today { box-shadow: 0 0 0 2px var(--accent), var(--shadow); }
.plan-day.past { opacity: 0.7; }
.plan-meal { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.plan-meal a, .plan-meal > span:nth-child(2) { flex: 1; color: var(--text); font-weight: 600; }
.plan-meal.cooked a::after { content: ' ✓'; color: var(--green); }

.suggestions { display: flex; flex-direction: column; gap: 12px; }
.suggestion { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; padding-top: 10px; border-top: 1px solid var(--line); }
.suggestion:first-child { border-top: 0; padding-top: 0; }
.suggestion-title { font-weight: 700; color: var(--text); }
.suggestion small { grid-column: 1; color: var(--muted); }
.suggestion .btn { grid-column: 2; grid-row: 1 / span 3; }
.meter { height: 6px; background: var(--surface-2); border-radius: 6px; overflow: hidden; grid-column: 1; }
.meter i { display: block; height: 100%; background: var(--green); }

.recipe-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-top: 12px; }
.recipe-card { display: flex; flex-direction: column; gap: 2px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; color: var(--text); padding-bottom: 10px; }
.recipe-card img, .recipe-ph { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; margin-bottom: 6px; }
.recipe-ph { display: grid; place-items: center; font-size: 2.4rem; background: var(--surface-2); }
.recipe-card strong, .recipe-card small { padding: 0 12px; }
.recipe-card small { color: var(--muted); }
.recipe-photo { width: 100%; max-height: 320px; object-fit: cover; border-radius: var(--radius); cursor: pointer; }
.photo-drop { width: 100%; padding: 28px; border: 2px dashed var(--line); border-radius: var(--radius); background: transparent; color: var(--muted); font-weight: 600; cursor: pointer; }

.ingredients { list-style: none; margin: 0; padding: 0; }
.ingredients li { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.ingredients li:last-child { border-bottom: 0; }
.ingredients .qty { width: 72px; flex: none; color: var(--muted); font-variant-numeric: tabular-nums; }
.ingredients li > span:nth-child(2) { flex: 1; }

.ing-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.ing-row { display: flex; gap: 6px; align-items: center; }
.ing-row input.qty, .receipt-row input.qty { width: 64px; flex: none; }
.ing-row input.unit, .receipt-row input.unit { width: 64px; flex: none; }
.ing-row input[data-k=name] { flex: 1; min-width: 0; }

.legend { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-direction: column; gap: 6px; }
.pantry-select { width: auto !important; min-height: 34px !important; padding: 4px 8px !important; font-size: 0.85rem !important; border-radius: 999px !important; flex: none; }

.pick-list { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow: auto; }
.pick { display: flex; align-items: center; gap: 10px; padding: 6px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-align: left; cursor: pointer; }
.pick.on { border-color: var(--accent); background: var(--accent-soft); }
.pick img, .pick-ph { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; display: grid; place-items: center; background: var(--surface-2); flex: none; }

/* ---- Lists (shopping, fridge, ingredients) ---------------------------------- */

.list { padding: 4px 16px; }
.list .card-head { margin: 12px 0 4px; }
.list-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: 0; }
.list-main { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; flex: 1; min-width: 0; padding: 0; border: 0; background: transparent; text-align: left; cursor: pointer; }
.list-main.static { cursor: default; }
.list-main small { color: var(--muted); }
.urgent { box-shadow: 0 0 0 2px var(--warn-soft), var(--shadow); }

.add-bar { display: flex; gap: 8px; margin-bottom: 14px; position: sticky; top: calc(var(--safe-top) + 4px); z-index: 5; }
.add-bar input { box-shadow: var(--shadow); }
.shop-row { display: grid; grid-template-columns: 1fr auto; align-items: center; column-gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.shop-row:last-child { border-bottom: 0; }
.shop-check { display: flex; align-items: center; gap: 12px; min-height: 40px; cursor: pointer; }
.shop-name { flex: 1; font-weight: 600; }
.shop-qty { color: var(--muted); font-variant-numeric: tabular-nums; }
.shop-meta { grid-column: 1; color: var(--muted); padding-left: 34px; margin-top: -6px; }
.shop-row .icon-btn { grid-column: 2; grid-row: 1; }
.shop-row.done .shop-name { text-decoration: line-through; color: var(--muted); font-weight: 400; }

.scan-btn {
  display: flex; align-items: center; gap: 14px; width: 100%; margin-bottom: 14px;
  padding: 16px; border: 0; border-radius: var(--radius); text-align: left; cursor: pointer;
  background: linear-gradient(135deg, var(--accent), #f2a65a); color: #fff; box-shadow: var(--shadow);
}
.scan-btn strong { display: block; font-size: 1.1rem; }
.scan-btn small { opacity: 0.9; }
.scan-icon { font-size: 2rem; }

.reading { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 24px; }
.receipt-top { display: flex; gap: 12px; align-items: flex-start; }
.receipt-thumb { width: 72px; max-height: 110px; object-fit: cover; border-radius: 8px; flex: none; }
.receipt-rows { display: flex; flex-direction: column; gap: 10px; }
.receipt-row { display: flex; gap: 10px; align-items: flex-start; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.receipt-row > input[type=checkbox] { margin-top: 11px; }
.receipt-row.off .receipt-fields { opacity: 0.4; }
.receipt-fields { flex: 1; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.receipt-fields input.date { flex: 1; min-width: 0; }
.receipt-fields small { font-family: ui-monospace, monospace; font-size: 0.75rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ocr { white-space: pre-wrap; font-size: 0.75rem; max-height: 200px; overflow: auto; background: var(--surface-2); padding: 8px; border-radius: 8px; }

/* ---- Settings & auth ------------------------------------------------------- */

.settings-photo { border-radius: 12px; margin-bottom: 10px; max-height: 220px; width: 100%; object-fit: cover; }
.card > .btn + .row, .card > input + .row { margin-top: 10px; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 32px; height: 32px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.swatch.on { border-color: var(--text); }
.steps { margin: 0; padding-left: 1.3em; display: flex; flex-direction: column; gap: 4px; }

.auth-screen { min-height: 100vh; display: grid; place-items: center; padding: calc(var(--safe-top) + 24px) 16px calc(var(--safe-bottom) + 24px); }
.auth-card { width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 12px; }
.auth-logo { font-size: 3rem; text-align: center; }
.auth-card h1 { text-align: center; }
.auth-card > p { text-align: center; }

/* ---- Sheets, toast, spinner ------------------------------------------------ */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0); transition: background 0.2s;
}
.sheet-backdrop.open { background: rgba(0, 0, 0, 0.35); }
.sheet {
  width: 100%; max-width: 600px; max-height: calc(100vh - var(--safe-top) - 24px);
  display: flex; flex-direction: column;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding-bottom: var(--safe-bottom);
  transform: translateY(100%); transition: transform 0.22s ease-out;
}
.sheet-backdrop.open .sheet { transform: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.sheet-body { overflow: auto; padding: 8px 16px 20px; -webkit-overflow-scrolling: touch; }
@media (min-width: 640px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; }
}

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: #2b2622; color: #fff; font-weight: 600; box-shadow: var(--shadow);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--bad); }

.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 4px solid var(--accent-soft); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.loading::after { content: ''; width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
.list-row > .row { flex: 1; min-width: 0; }
