:root {
  --bg: #fbf7f0;
  --surface: #ffffff;
  --surface-2: #f4eee4;
  --surface-3: #ebe2d4;
  --text: #2b2420;
  --muted: #776a60;
  --line: #e5dccf;
  --accent: #c4502f;
  --accent-ink: #ffffff;
  --accent-soft: #f8e1d8;
  --green: #4f7f4e;
  --green-soft: #e2eedd;
  --gold: #e0a526;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgb(60 40 20 / 6%), 0 4px 16px rgb(60 40 20 / 7%);
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Songti SC", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1613;
    --surface: #25201c;
    --surface-2: #2e2823;
    --surface-3: #3a322b;
    --text: #f3ece4;
    --muted: #a8998d;
    --line: #3b332c;
    --accent: #e57b5c;
    --accent-ink: #1a1613;
    --accent-soft: #4a2c22;
    --green: #8fc08c;
    --green-soft: #26331f;
    --gold: #f0bc4c;
    --danger: #ff8a80;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
img { display: block; max-width: 100%; }
h1, h2, h3 { font-family: var(--serif); line-height: 1.15; margin: 0; font-weight: 700; }
h1 { font-size: 30px; }
h2 { font-size: 22px; }
h3 { font-size: 18px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.hidden { display: none !important; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.topbar .brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-weight: 700; font-size: 19px; text-decoration: none; color: var(--text); margin-right: auto; }
.topbar .brand img { width: 30px; height: 30px; border-radius: 8px; }
.icon-btn {
  display: inline-grid; place-items: center; min-width: 44px; height: 44px; padding: 0 10px;
  border: 0; border-radius: 22px; background: transparent; color: var(--text); font-size: 15px; font-weight: 600;
}
.icon-btn:hover { background: var(--surface-2); }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: 15px; }

main { max-width: 980px; margin: 0 auto; padding: 16px 16px calc(96px + env(safe-area-inset-bottom)); }
.narrow { max-width: 680px; margin-left: auto; margin-right: auto; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0;
  color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabbar a .ico { font-size: 22px; line-height: 28px; filter: grayscale(0.6); opacity: 0.8; }
.tabbar a.active { color: var(--accent); }
.tabbar a.active .ico { filter: none; opacity: 1; }
.tabbar a.add .ico {
  width: 48px; height: 48px; margin-top: -18px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 28px; filter: none; opacity: 1; box-shadow: var(--shadow);
}

.page-head { display: flex; align-items: center; gap: 12px; margin: 4px 0 16px; flex-wrap: wrap; }
.page-head h1 { margin-right: auto; }
.section { margin: 28px 0 12px; }
.section-title { display: flex; align-items: baseline; gap: 8px; margin: 28px 0 12px; }
.section-title h2 { margin-right: auto; }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px; border-radius: 24px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-weight: 600; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.green { background: var(--green); border-color: var(--green); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 38px; padding: 0 14px; font-size: 15px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.stack > * + * { margin-top: 12px; }

.field { display: block; margin: 0 0 16px; }
.field > span { display: block; font-size: 14px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.input, select.input, textarea.input {
  width: 100%; min-height: 48px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); font-size: 17px;
}
textarea.input { min-height: 120px; resize: vertical; line-height: 1.5; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
@media (max-width: 480px) { .grid3 { grid-template-columns: 1fr 1fr; } }

.search { position: relative; }
.search input { padding-left: 44px; border-radius: 24px; }
.search::before { content: "🔍"; position: absolute; left: 16px; top: 50%; transform: translateY(-50%); font-size: 16px; opacity: 0.6; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.chips::-webkit-scrollbar { display: none; }
.chips.wrap { flex-wrap: wrap; overflow: visible; }
.chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 38px; padding: 0 14px;
  border-radius: 19px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-size: 15px; font-weight: 500; white-space: nowrap;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip.soft { background: var(--surface-2); border-color: transparent; }
.chip.tiny { min-height: 28px; padding: 0 10px; font-size: 13px; border-radius: 14px; }
button.chip:hover:not(.on) { background: var(--surface-2); }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 22px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; min-height: 38px; padding: 0 14px; border-radius: 19px; font-weight: 600; font-size: 15px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.stepper { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-2); border-radius: 26px; padding: 4px; }
.stepper button { width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--surface); font-size: 22px; font-weight: 600; box-shadow: var(--shadow); }
.stepper .val { min-width: 96px; text-align: center; font-weight: 700; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.pad { padding: 16px; }
.banner { padding: 12px 16px; border-radius: 12px; background: var(--accent-soft); margin: 0 0 16px; }
.banner.green { background: var(--green-soft); }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .big { font-size: 48px; margin-bottom: 8px; }

/* ---------- Recipe cards ---------- */
.recipe-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.recipe-grid > * { min-width: 0; }
@media (max-width: 520px) { .recipe-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; } }
.rcard { display: flex; flex-direction: column; overflow: hidden; text-decoration: none; color: var(--text); background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.rcard .ph { aspect-ratio: 4 / 3; }
.rcard .body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.rcard .t { font-weight: 700; font-size: 16px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.rcard .m { font-size: 13px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; }
.ph { width: 100%; background: var(--surface-2); display: grid; place-items: center; font-size: 44px; overflow: hidden; }
.ph img { width: 100%; height: 100%; object-fit: cover; }
.stars { color: var(--gold); letter-spacing: 1px; }

/* ---------- Recipe page ---------- */
.hero { position: relative; margin: -16px -16px 16px; }
.hero .ph { aspect-ratio: 16 / 10; max-height: 420px; font-size: 80px; }
@media (min-width: 700px) { .hero { margin: 0 0 20px; border-radius: var(--radius); overflow: hidden; } }
.hero .ph.noimg { aspect-ratio: auto; height: 150px; font-size: 64px; }
.hero .back { position: absolute; top: 12px; left: 12px; background: color-mix(in srgb, var(--surface) 90%, transparent); box-shadow: var(--shadow); }
.source-bar { display: flex; align-items: center; gap: 8px 12px; flex-wrap: wrap; margin: 0 0 10px; color: var(--muted); font-weight: 600; font-size: 15px; }
.source-bar .btn { color: var(--accent); }
.meta-pills { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 14px; background: var(--surface-2); font-size: 14px; font-weight: 500; }
.two-col { display: grid; gap: 24px; }
@media (min-width: 800px) { .two-col { grid-template-columns: 340px 1fr; align-items: start; } .two-col .sticky { position: sticky; top: 76px; } }
.ing-list { list-style: none; padding: 0; margin: 0; }
.ing-list li { display: flex; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.ing-list li .amt { min-width: 84px; font-weight: 700; text-align: right; }
.ing-list li.done { opacity: 0.4; text-decoration: line-through; }
.ing-list .grp { font-weight: 700; font-family: var(--serif); padding: 16px 4px 4px; border: 0; cursor: default; }
.step-list { list-style: none; padding: 0; margin: 0; counter-reset: step; }
.step-list li { position: relative; padding: 14px 14px 14px 58px; margin-bottom: 10px; border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); cursor: pointer; font-size: 18px; line-height: 1.5; }
.step-list li::before { counter-increment: step; content: counter(step); position: absolute; left: 14px; top: 13px; width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 16px; }
.step-list li.done { opacity: 0.45; }
.step-list li.done::before { content: "✓"; background: var(--green-soft); color: var(--green); }

/* ---------- Week ---------- */
.week-nav { display: flex; align-items: center; gap: 4px; }
.week-nav .icon-btn { font-size: 28px; font-weight: 400; text-decoration: none; color: var(--text); }
.week-nav h1 { min-width: 0; }
.days { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) { .days { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.day { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px; }
.day.today { outline: 2px solid var(--accent); }
.day.past { opacity: 0.85; }
.day-head { display: flex; align-items: baseline; gap: 4px 8px; margin: 0 0 8px; flex-wrap: wrap; }
.day-head .dn { font-weight: 700; font-size: 17px; }
.day-head .dd { color: var(--muted); font-size: 14px; margin-right: auto; white-space: nowrap; }
.day-head .extra { min-height: 30px; padding: 0 8px; border: 0; background: transparent; color: var(--muted); font-size: 13px; font-weight: 600; border-radius: 15px; }
.day-head .extra:hover { background: var(--surface-2); }
.slot { display: flex; align-items: center; gap: 12px; min-height: 64px; padding: 8px; border-radius: 12px; background: var(--surface-2); width: 100%; border: 0; text-align: left; }
.slot + .slot { margin-top: 8px; }
.ph.thumb { width: 44px; height: 44px; border-radius: 10px; flex: none; font-size: 22px; }
.slot .thumb { width: 52px; height: 52px; border-radius: 10px; flex: none; font-size: 26px; }
.slot .info { flex: 1; min-width: 0; }
.slot .info .st { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.slot .info .tt { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.slot .info .mm { font-size: 13px; color: var(--muted); }
.slot.empty { justify-content: center; color: var(--muted); border: 2px dashed var(--line); background: transparent; font-weight: 600; }
.slot.suggested { outline: 2px dashed var(--accent); background: var(--accent-soft); }
.slot .status { font-size: 22px; }
.slot.cooked .tt::after { content: " ✓"; color: var(--green); }
.slot.skipped .tt { text-decoration: line-through; opacity: 0.6; }
.ask-eat { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 14px; color: var(--muted); }
.wish-row { margin-top: 8px; }
.toolbar { display: flex; gap: 8px; margin: 0 0 16px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.toolbar::-webkit-scrollbar { display: none; }
.toolbar .btn { flex: none; min-height: 42px; padding: 0 16px; font-size: 15px; }

/* ---------- Shopping ---------- */
.aisle { margin: 20px 0 6px; font-weight: 700; font-family: var(--serif); font-size: 18px; }
.shop-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--surface); border-radius: 12px; margin-bottom: 6px; box-shadow: var(--shadow); }
.shop-item input[type="checkbox"] { width: 26px; height: 26px; accent-color: var(--green); flex: none; }
.shop-item .nm { flex: 1; min-width: 0; }
.shop-item .nm b { font-weight: 700; }
.shop-item .for { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shop-item.checked .nm { opacity: 0.45; text-decoration: line-through; }

/* ---------- Stats ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 640px) { .kpis { grid-template-columns: 1fr 1fr; } }
.kpi { padding: 14px 16px; }
.kpi .v { font-size: 30px; font-weight: 700; font-family: var(--serif); }
.kpi .l { font-size: 13px; color: var(--muted); font-weight: 600; }
.chart-card { padding: 16px; margin-top: 16px; }
.chart-card h3 { margin-bottom: 12px; }
.hbars { display: grid; gap: 8px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; gap: 10px; align-items: center; font-size: 15px; }
.hbar .lab { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hbar .track { display: block;  height: 14px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.hbar .fill { display: block; height: 100%; background: var(--accent); border-radius: 0 4px 4px 0; }
.hbar .num { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 2ch; text-align: right; }
.vbars { display: flex; align-items: flex-end; gap: 4px; height: 140px; padding-top: 8px; }
.vbars .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.vbars .bar { width: 100%; max-width: 28px; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
.vbars .bar.zero { background: var(--surface-3); }
.vbars .x { font-size: 11px; color: var(--muted); }
.vbars .n { font-size: 11px; font-weight: 700; }
.list-rows a, .list-rows div.r { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); color: var(--text); text-decoration: none; }
.list-rows .thumb { width: 44px; height: 44px; border-radius: 10px; flex: none; font-size: 22px; }

/* ---------- Sheet, toast, spinner ---------- */
.backdrop { position: fixed; inset: 0; z-index: 50; background: rgb(20 12 6 / 45%); display: flex; align-items: flex-end; justify-content: center; animation: fade 0.15s ease-out; }
.sheet {
  width: 100%; max-width: 640px; max-height: 88vh; overflow: auto; background: var(--bg);
  border-radius: 22px 22px 0 0; padding: 8px 16px calc(20px + env(safe-area-inset-bottom)); animation: up 0.2s ease-out;
}
@media (min-width: 700px) { .backdrop { align-items: center; } .sheet { border-radius: 22px; } }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { margin-bottom: 14px; }
.sheet .menu button { display: flex; align-items: center; gap: 14px; width: 100%; min-height: 54px; padding: 0 12px; border: 0; background: transparent; border-radius: 12px; font-size: 17px; text-align: left; }
.sheet .menu button:hover { background: var(--surface-2); }
.sheet .menu .ico { width: 28px; text-align: center; font-size: 20px; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.toast { position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60; background: var(--text); color: var(--bg); padding: 12px 20px; border-radius: 24px; font-weight: 600; box-shadow: var(--shadow); max-width: calc(100% - 32px); animation: up 0.2s ease-out; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid var(--surface-3); border-top-color: var(--accent); animation: spin 0.9s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.rate { display: flex; justify-content: center; gap: 6px; margin: 8px 0 16px; }
.rate button { border: 0; background: transparent; font-size: 40px; line-height: 1; color: var(--surface-3); padding: 4px; }
.rate button.on { color: var(--gold); }

/* ---------- Add / scan ---------- */
.tiles { display: grid; gap: 12px; }
@media (min-width: 640px) { .tiles { grid-template-columns: 1fr 1fr; } }
.tile { display: flex; gap: 14px; align-items: flex-start; padding: 18px; border: 0; text-align: left; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.tile .ico { font-size: 34px; line-height: 1; }
.tile b { display: block; font-size: 18px; margin-bottom: 2px; }
.tile span { color: var(--muted); font-size: 15px; }
.tile.main { background: var(--accent); color: var(--accent-ink); }
.tile.main span { color: inherit; opacity: 0.85; }
.thumbs { display: flex; gap: 10px; flex-wrap: wrap; }
.thumbs .th { position: relative; width: 96px; height: 120px; border-radius: 12px; overflow: hidden; background: var(--surface-2); border: 3px solid transparent; padding: 0; }
.thumbs .th img { width: 100%; height: 100%; object-fit: cover; }
.thumbs .th.sel { border-color: var(--accent); }
.thumbs .th .x { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border-radius: 50%; border: 0; background: rgb(0 0 0 / 60%); color: #fff; font-size: 16px; }
.thumbs .th.add { display: grid; place-items: center; font-size: 30px; color: var(--muted); border: 2px dashed var(--line); background: transparent; }
.tag-picker .grp { margin: 12px 0 6px; font-size: 14px; font-weight: 700; color: var(--muted); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login .box { width: 100%; max-width: 420px; text-align: center; }
.login .logo { width: 96px; height: 96px; margin: 0 auto 16px; border-radius: 24px; box-shadow: var(--shadow); }
.login h1 { margin-bottom: 8px; }
.login .input { text-align: center; font-size: 22px; min-height: 58px; margin: 20px 0 12px; }
.login .btn { min-height: 56px; font-size: 19px; }
.people { display: grid; gap: 12px; margin-top: 20px; }
.people button { min-height: 64px; font-size: 21px; }
.lang-switch { position: fixed; top: calc(env(safe-area-inset-top) + 12px); right: 12px; }
.error { color: var(--danger); font-weight: 600; min-height: 1.4em; }
