:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1d2420;
  --muted: #5f6b64;
  --line: #e2e0d8;
  --accent: #2f6b4f;
  --accent-soft: #e3efe8;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --radius: 12px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141816;
    --surface: #1e2421;
    --text: #e7ece9;
    --muted: #9aa6a0;
    --line: #2f3833;
    --accent: #6fbf95;
    --accent-soft: #24362d;
    --accent-text: #0f1a14;
    --danger: #f2a39d;
    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 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
}
button, input, textarea { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 12px) 16px 8px;
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 1.15rem; letter-spacing: -0.01em; }
.status { margin: 4px 0 0; font-size: 0.85rem; color: var(--danger); }

main { max-width: 720px; margin: 0 auto; padding: 16px; }

/* Buttons */
button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); cursor: pointer; text-decoration: none; color: var(--text);
}
button:disabled { opacity: 0.5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.danger { color: var(--danger); }
button.link { min-height: 0; padding: 0; border: 0; background: none; color: var(--accent); text-decoration: underline; }
button.icon { width: 44px; padding: 0; border: 0; background: none; font-size: 1.4rem; color: var(--muted); }
button.icon svg { width: 20px; height: 20px; fill: currentColor; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

input[type=url], input[type=search], input[type=password], input[type=number], input:not([type]), textarea {
  width: 100%; min-height: 44px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
}
textarea { resize: vertical; }

.add-form { display: flex; gap: 8px; }
.add-form input { flex: 1; min-width: 0; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 8px 0 12px; }
.error { color: var(--danger); font-size: 0.9rem; margin: 8px 0; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; list-style: none; }
.empty-state { text-align: center; padding: 48px 16px; color: var(--muted); }
#search { margin-bottom: 12px; }

/* Recipe cards */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.card {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px; padding-right: 10px;
}
.card.selected { border-color: var(--accent); box-shadow: inset 4px 0 0 var(--accent); }
.card-main {
  flex: 1; min-width: 0; justify-content: flex-start; gap: 12px;
  min-height: 64px; padding: 0; border: 0; border-radius: 8px; background: none; text-align: left;
}
.card img, .thumb-blank {
  width: 64px; height: 64px; flex: none; border-radius: 8px; object-fit: cover; background: var(--accent-soft);
}
.thumb-blank { display: grid; place-items: center; font-weight: 700; color: var(--accent); font-size: 1.4rem; }
.card-title { font-weight: 600; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.toggle { flex: none; padding: 0 12px; font-size: 0.9rem; white-space: nowrap; }
.toggle.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.stepper { display: flex; align-items: center; gap: 2px; flex: none; }
.stepper button { width: 36px; min-height: 36px; padding: 0; font-size: 1.1rem; }
.stepper span { min-width: 44px; text-align: center; font-weight: 600; }
.stepper small { font-weight: 400; color: var(--muted); }
.week-card .remove { width: 32px; }

.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; }
.actions.left { justify-content: flex-start; margin-top: 0; }

/* Shopping list */
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.list-head p { margin: 0; }
.list-head div { display: flex; gap: 8px; }
.aisle { margin-top: 16px; }
.aisle h2, .aisle summary {
  font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted);
  margin: 0 0 6px; font-weight: 600;
}
.aisle summary { cursor: pointer; padding: 8px 0; }
.items { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.item { display: flex; align-items: center; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item label { flex: 1; display: flex; align-items: flex-start; gap: 12px; padding: 12px 0 12px 14px; cursor: pointer; min-width: 0; }
.item input[type=checkbox] { width: 22px; height: 22px; margin: 1px 0 0; flex: none; accent-color: var(--accent); }
.item-text { min-width: 0; }
.item .name { font-weight: 500; }
.amount { color: var(--accent); font-weight: 600; white-space: nowrap; }
.from { display: block; color: var(--muted); font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item.done .name { text-decoration: line-through; color: var(--muted); }
.item.done .amount { color: var(--muted); }
.item.staple { padding: 12px 14px; justify-content: space-between; gap: 12px; }
.pin { flex: none; }

/* Settings */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.panel h2 { margin: 0 0 4px; font-size: 1rem; }
.segmented { display: flex; gap: 6px; }
.segmented button { flex: 1; padding: 0; }
.segmented [aria-pressed=true] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.chips { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li { display: inline-flex; align-items: center; gap: 2px; padding-left: 12px; border-radius: 999px; background: var(--accent-soft); }
.chips button { min-height: 32px; width: 32px; padding: 0; border: 0; background: none; color: var(--muted); }

/* Bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  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: var(--surface); border-top: 1px solid var(--line);
}
.tabs a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: 0.75rem; }
.tabs a svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs a.active { color: var(--accent); font-weight: 600; }
.tabs b { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: var(--accent-text); font-size: 0.7rem; line-height: 18px; }

/* Dialog */
dialog {
  width: min(560px, 100%); max-height: 90vh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 16px 16px 0 0; background: var(--surface); color: var(--text);
}
@media (min-width: 640px) { dialog { margin: auto; border-radius: 16px; } }
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet { padding: 16px; display: grid; gap: 12px; }
.sheet header { display: grid; grid-template-columns: 1fr auto; align-items: start; gap: 8px; }
.sheet header img { grid-column: 1 / -1; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 10px; }
.sheet h2 { margin: 0; font-size: 1.2rem; }
.sheet label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--muted); }
.sheet label input, .sheet label textarea { color: var(--text); }
.sheet .hint { margin: 0; }
.plain { margin: 0; padding-left: 18px; }
.plain li { margin: 2px 0; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 20;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 12px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 0.9rem;
}
.week-card img, .week-card .thumb-blank { width: 48px; height: 48px; font-size: 1.1rem; }
.week-card .card-main { min-height: 48px; }
