:root {
  color-scheme: light dark;
  --bg: #f6f5f0;
  --surface: #ffffff;
  --surface-2: #efeee7;
  --text: #1d2420;
  --muted: #5d6962;
  --border: #dedcd2;
  --accent: #1f7a4d;
  --accent-soft: #e0f1e7;
  --on-accent: #ffffff;
  --warn: #a8480c;
  --warn-soft: #fbeadb;
  --danger: #b3261e;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 14px rgb(0 0 0 / 5%);
  --tabs-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121613;
    --surface: #1b211d;
    --surface-2: #242b26;
    --text: #e8ede9;
    --muted: #9ba8a0;
    --border: #2d3630;
    --accent: #4cc38a;
    --accent-soft: #1c3a2b;
    --on-accent: #0b1a12;
    --warn: #f0a868;
    --warn-soft: #3a2a1a;
    --danger: #f2877f;
    --shadow: none;
  }
}

* { 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) + 24px);
}

h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

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

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* App bar */
.app-bar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.app-bar .wrap { display: flex; align-items: center; justify-content: space-between; height: 54px; }
.app-bar h1 { display: flex; align-items: center; gap: 10px; font-size: 1.15rem; letter-spacing: -0.01em; }
.app-bar img { border-radius: 7px; }
.app-bar-end { display: flex; align-items: center; gap: 10px; }
.sync-state { font-size: 0.8125rem; color: var(--muted); }

/* Buttons */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 10px 18px; font-weight: 600; cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn.small { padding: 6px 12px; font-size: 0.875rem; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.wide { width: 100%; padding: 13px 18px; }
.btn.danger { color: var(--danger); }

.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.icon-btn svg, .tabs svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

/* Cards and forms */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 16px; margin-top: 16px; }
.form .wide { grid-column: 1 / -1; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > span:first-child { font-size: 0.8125rem; font-weight: 600; color: var(--muted); }
.field input, .field select, .search, .price-row input {
  width: 100%; min-width: 0; height: 44px; padding: 0 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
}
.money-input { position: relative; display: block; }
.money-input > span {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--muted); pointer-events: none;
}
.money-input input { padding-left: 2.6em; }

/* Budget summary */
.summary { padding: 16px; margin-top: 12px; }
.summary-top { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.summary-total { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.summary-total small { font-size: 0.9rem; font-weight: 500; color: var(--muted); }
.summary-left { font-weight: 600; color: var(--accent); }
.summary.over .summary-left { color: var(--warn); }
.bar { height: 8px; margin: 10px 0 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar span { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.summary.over .bar span { background: var(--warn); }
.summary-meta { color: var(--muted); font-size: 0.875rem; }
.notice {
  margin-top: 12px; padding: 10px 12px; border-radius: 10px;
  background: var(--warn-soft); color: var(--warn); font-size: 0.9rem;
}
.notice.info { background: var(--accent-soft); color: var(--accent); margin: 0 0 12px; }

/* Days */
.days { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.day { display: flex; flex-wrap: wrap; align-items: center; padding-right: 6px; }
.day-main {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px;
  padding: 12px 4px 12px 14px; text-align: left;
  border: 0; background: transparent; cursor: pointer; border-radius: var(--radius);
}
.emoji {
  flex: none; display: grid; place-items: center; width: 46px; height: 46px;
  border-radius: 12px; background: var(--surface-2); font-size: 1.5rem;
}
.day-text { flex: 1; min-width: 0; display: grid; }
.day-name { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent); }
.recipe-name { font-weight: 600; overflow-wrap: anywhere; }
.meta { color: var(--muted); font-size: 0.8125rem; }
.cost { flex: none; font-weight: 700; font-variant-numeric: tabular-nums; }
.day-actions { flex: none; display: flex; }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.day.empty { padding: 18px 14px; color: var(--muted); }
/* What still has to be bought for the day, after the pantry is used up from Monday on. */
.day-buy { flex-basis: 100%; padding: 0 14px 12px 72px; font-size: 0.875rem; overflow-wrap: anywhere; }
.day-buy .label { font-weight: 600; }
.day-home { margin-top: 2px; color: var(--muted); }

.empty-state { text-align: center; color: var(--muted); padding: 40px 16px; }

/* View headings */
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin: 20px 0 8px; }
.view-head h2 { font-size: 1.35rem; letter-spacing: -0.01em; }
.view-head h3 { font-size: 1.05rem; }
.actions { display: flex; gap: 8px; }
.hint { color: var(--muted); font-size: 0.9rem; margin-bottom: 12px; }
.group-title {
  margin: 18px 0 6px; font-size: 0.75rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted);
}

/* Shopping list */
.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--border); }
.check-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; cursor: pointer; }
.check-row input { flex: none; width: 22px; height: 22px; accent-color: var(--accent); }
.check-row .name { flex: 1; min-width: 0; }
.check-row .qty { color: var(--muted); font-size: 0.875rem; }
.check-row .price { width: 4.5em; text-align: right; font-variant-numeric: tabular-nums; }
.check-row:has(input:checked) .name { text-decoration: line-through; color: var(--muted); }
.plain-row { display: flex; justify-content: space-between; gap: 12px; padding: 10px 14px; }
.plain-row span:last-child { color: var(--muted); }
.total-row { display: flex; justify-content: space-between; padding: 14px; margin-top: 12px; font-weight: 700; font-size: 1.1rem; }

/* Pantry */
.search { background: var(--surface); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 8px 14px; cursor: pointer; font-size: 0.9375rem;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
.chip[aria-pressed="true"]::before { content: "✓ "; }
.pantry-row { display: flex; align-items: center; gap: 10px; padding: 6px 6px 6px 14px; }
.pantry-row .name { flex: 1; min-width: 0; overflow-wrap: break-word; hyphens: auto; }
.pantry-row input {
  width: 6.5em; height: 38px; padding: 0 10px; text-align: right;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
}
.pantry-row .unit { width: 2.6em; color: var(--muted); font-size: 0.8125rem; }
#pantry-items { margin-bottom: 8px; }

/* Prices */
.price-row { display: flex; align-items: center; gap: 12px; padding: 8px 14px; }
.price-row .name { flex: 1; min-width: 0; }
.price-row input { width: 5.5em; height: 38px; text-align: right; }
.price-row .unit { width: 3.6em; color: var(--muted); font-size: 0.8125rem; }
.price-row { padding-right: 6px; }
.price-row .name { overflow-wrap: break-word; hyphens: auto; }
.price-row.changed .name { font-weight: 600; color: var(--accent); }
.badge-mine, .badge-ai, .badge-guess {
  display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 999px;
  font-size: 0.6875rem; font-weight: 700; line-height: 1.6; vertical-align: 1px;
}
.badge-mine { background: var(--accent-soft); color: var(--accent); }
.badge-ai { background: var(--surface-2); color: var(--text); }
.badge-guess { background: var(--warn-soft); color: var(--warn); }
.icon-btn.busy { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }
.filter-row { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px; margin-bottom: 4px; }
.filter-row select {
  height: 44px; padding: 0 10px; max-width: 46vw;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.more { text-align: center; margin-top: 12px; }
.code-row { display: flex; gap: 8px; }
.code-row input {
  flex: 1; min-width: 0; height: 44px; padding: 0 12px; font-family: ui-monospace, monospace; font-size: 0.8125rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
}
.hint a, .note a { color: var(--accent); }

/* Bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs .wrap { display: grid; grid-template-columns: repeat(5, 1fr); height: var(--tabs-h); padding: 0 4px; }
.tabs a {
  display: grid; place-content: center; justify-items: center; gap: 2px; min-width: 0;
  position: relative; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: var(--muted); text-decoration: none; font-size: 0.6875rem; font-weight: 600; border-radius: 12px;
}
.tabs a[aria-current="page"] { color: var(--accent); }
/* Count bubble on the tab icon's top-right corner. */
.badge {
  position: absolute; top: 5px; left: calc(50% + 5px);
  min-width: 1.6em; padding: 0 5px; border-radius: 999px;
  background: var(--accent); color: var(--on-accent); font-size: 0.6875rem; line-height: 1.6; text-align: center;
}

/* Recipe dialog */
dialog {
  width: min(600px, 100vw - 24px); max-height: min(86vh, 800px);
  padding: 0; border: 1px solid var(--border); border-radius: 18px;
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 50%); }
.recipe { padding: 18px; }
.recipe-head { display: flex; align-items: flex-start; gap: 12px; }
.recipe-head h2 { flex: 1; font-size: 1.25rem; line-height: 1.25; padding-top: 2px; }
.recipe h3 { margin: 18px 0 6px; font-size: 0.95rem; }
.recipe h3 small { font-weight: 500; color: var(--muted); }
.recipe .meta { margin-top: 10px; font-size: 0.875rem; }
.ing { list-style: none; margin: 0; padding: 0; }
.ing li { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-top: 1px solid var(--border); }
.ing li span:last-child { color: var(--muted); white-space: nowrap; }
.ing .home { color: var(--accent); font-size: 0.75rem; font-weight: 700; margin-left: 6px; }
.steps { margin: 0; padding-left: 1.3em; display: grid; gap: 8px; }
.recipe .note { margin-top: 16px; color: var(--muted); font-size: 0.8125rem; }
.recipe .source { margin-top: 18px; }
.recipe .source .btn { display: inline-block; text-decoration: none; }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px);
  transform: translate(-50%, 20px); z-index: 10;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 999px;
  background: var(--text); color: var(--bg); font-size: 0.9rem;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 380px) {
  .emoji { width: 40px; height: 40px; font-size: 1.3rem; }
  .day-main { gap: 10px; padding-left: 10px; }
  .day-actions { flex-direction: column; }
  .day-buy { padding-left: 14px; }
  .price-row { gap: 6px; padding-left: 12px; }
  .price-row input { width: 4.6em; padding: 0 8px; }
  .price-row .unit { width: 2.9em; }
  .price-row .icon-btn { width: 34px; }
  .pantry-row input { width: 5em; }
}

@media (prefers-reduced-motion: reduce) {
  #toast { transition: none; }
}
