:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece4;
  --text: #262420;
  --muted: #6f6a5f;
  --line: #e0dcd1;
  --accent: #2f7d4f;
  --accent-soft: #e2f0e7;
  --accent-text: #ffffff;
  --danger: #b3402e;
  --warn-bg: #f7ead2;
  --warn-text: #7a5316;
  --chip-kcal: #efe4d2;
  --chip-protein: #dcebe0;
  --chip-carbs: #e4e6f2;
  --chip-fat: #f2e2dc;
  --shadow: 0 1px 3px rgba(40, 36, 28, .08), 0 4px 16px rgba(40, 36, 28, .06);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191813;
    --surface: #23211b;
    --surface-2: #2c2a22;
    --text: #ece8dd;
    --muted: #a49e8f;
    --line: #38352b;
    --accent: #58b07f;
    --accent-soft: #274332;
    --accent-text: #10130f;
    --danger: #e0705c;
    --warn-bg: #40331a;
    --warn-text: #e5c078;
    --chip-kcal: #3c3527;
    --chip-protein: #27392d;
    --chip-carbs: #2b2e3f;
    --chip-fat: #3e2d27;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; }
/* the hidden attribute must always win, even against author display rules */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-x: clip; /* nothing may ever scroll the page sideways */
}
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* ---------- header ---------- */
header {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.logo { font-size: 17px; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; }
.logo span { color: var(--accent); }
nav { display: flex; gap: 4px; }
nav button {
  border: 0; background: none; color: var(--muted);
  padding: 7px 14px; border-radius: 999px; font-weight: 600;
}
nav button:hover { background: var(--surface-2); color: var(--text); }
nav button.active { background: var(--accent); color: var(--accent-text); }
.header-spacer { flex: 1; }
.sel-summary { font-size: 13px; color: var(--muted); white-space: nowrap; }

/* small screens: logo + summary on row one, nav gets its own full-width row */
@media (max-width: 640px) {
  header { flex-wrap: wrap; gap: 6px 10px; padding: 8px 12px; }
  .logo { font-size: 15px; }
  nav { order: 3; width: 100%; }
  nav button { flex: 1; padding: 7px 4px; font-size: 13.5px; text-align: center; }
  .sel-summary { font-size: 12px; }
  .layout { padding: 12px; gap: 14px; }
}
.sel-summary b { color: var(--text); }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 20px;
  max-width: 1400px; margin: 0 auto; padding: 20px; align-items: start; }
.layout.no-cart { grid-template-columns: minmax(0, 1fr); }
.layout.no-cart #cart { display: none; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

/* ---------- meal cards ---------- */
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; align-items: center; }
.toolbar input[type=search] {
  flex: 1; min-width: 180px; padding: 8px 14px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface);
}
.toolbar .hint { font-size: 13px; color: var(--muted); }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(255px, 1fr)); gap: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 14px 12px; display: flex; flex-direction: column; gap: 9px;
  transition: border-color .15s, transform .1s;
}
.card.selected { border-color: var(--accent); outline: 1px solid var(--accent); }
.card h3 { margin: 0; font-size: 15.5px; line-height: 1.3; cursor: pointer; }
.card h3:hover { color: var(--accent); }
.badges { display: flex; gap: 5px; flex-wrap: wrap; }
.badge { font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge.ok { background: var(--accent-soft); color: var(--accent); }

.chips { display: flex; gap: 5px; flex-wrap: wrap; }
.chip { font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 7px; }
.chip.kcal { background: var(--chip-kcal); }
.chip.protein { background: var(--chip-protein); }
.chip.carbs { background: var(--chip-carbs); }
.chip.fat { background: var(--chip-fat); }
.chip.na { background: var(--surface-2); color: var(--muted); font-weight: 500; }

.card-foot { display: flex; align-items: center; gap: 8px; margin-top: auto; padding-top: 6px; }
.meals-note { font-size: 12.5px; color: var(--muted); flex: 1; }
.btn {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 9px; padding: 6px 13px; font-weight: 600; color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border: 0; background: none; color: var(--muted); padding: 6px 8px; }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: 3px 10px; font-size: 13px; }

.stepper { display: flex; align-items: center; gap: 2px; background: var(--surface-2);
  border-radius: 9px; padding: 2px; }
.stepper button { border: 0; background: none; width: 26px; height: 26px; border-radius: 7px;
  font-weight: 700; color: var(--text); }
.stepper button:hover { background: var(--surface); }
.stepper .scale { min-width: 34px; text-align: center; font-weight: 700; font-size: 13px; }

/* ---------- cart / grocery panel ---------- */
#cart {
  position: sticky; top: 66px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: calc(100vh - 90px); overflow-y: auto;
  padding: 16px;
}
@media (max-width: 980px) { #cart { position: static; max-height: none; } }
#cart h2 { margin: 0 0 4px; font-size: 16px; }
#cart .sub { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.totals { display: flex; gap: 5px; flex-wrap: wrap; margin: 8px 0 12px; }
.cart-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.cross { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.cross b { color: var(--text); }

.gsection { margin-bottom: 12px; }
.gsection h4 { margin: 10px 0 6px; font-size: 12px; text-transform: uppercase;
  letter-spacing: .07em; color: var(--muted); }
.gitem { padding: 4px 0; border-bottom: 1px dashed var(--line); font-size: 13.5px; }
.gitem:last-child { border-bottom: 0; }
.gitem label { display: flex; gap: 8px; align-items: baseline; cursor: pointer; }
.gitem input[type=checkbox] { accent-color: var(--accent); transform: translateY(2px); }
.gitem.checked .gname, .gitem.checked .gqty { text-decoration: line-through; opacity: .45; }
.gname { font-weight: 600; }
.gqty { color: var(--muted); }
.guse { font-size: 11.5px; font-weight: 700; color: var(--accent); background: var(--accent-soft);
  padding: 0 7px; border-radius: 999px; margin-left: auto; white-space: nowrap; cursor: pointer; }
.gbreak { margin: 3px 0 4px 26px; font-size: 12px; color: var(--muted); display: none; }
.gitem.open .gbreak { display: block; }

/* ---------- modal ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text);
  max-width: 640px; width: calc(100vw - 40px); max-height: 86vh;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(20, 18, 12, .5); backdrop-filter: blur(2px); }
.modal-body { padding: 22px 24px; overflow-y: auto; max-height: 86vh; }
.modal-body h2 { margin: 0 0 6px; font-size: 20px; }
.modal-meta { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.modal-meta a { color: var(--accent); }
.modal-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--muted); margin: 18px 0 6px; }
.modal-body ul, .modal-body ol { margin: 0; padding-left: 22px; }
.modal-body li { margin: 3px 0; }
.note-box { background: var(--warn-bg); color: var(--warn-text); border-radius: 9px;
  padding: 9px 12px; font-size: 13.5px; margin-top: 12px; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; flex-wrap: wrap; }

/* ---------- add tab ---------- */
.panel { background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 18px; }
.panel h2 { margin: 0 0 4px; font-size: 17px; }
.panel .sub { color: var(--muted); font-size: 13.5px; margin: 0 0 12px; }
.panel textarea, .panel input[type=text], .panel input[type=number], .panel input[type=url], .panel select {
  width: 100%; padding: 8px 12px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--bg); resize: vertical;
}
.row { display: flex; gap: 10px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.row label.toggle { display: flex; gap: 7px; align-items: center; font-size: 14px; color: var(--muted); }
.row label.toggle input { accent-color: var(--accent); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
@media (max-width: 640px) { .grid-4 { grid-template-columns: 1fr 1fr; } .grid-2 { grid-template-columns: 1fr; } }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 10px 0 4px; }

.log { font: 12.5px/1.6 ui-monospace, monospace; background: var(--bg);
  border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px;
  margin-top: 12px; max-height: 260px; overflow-y: auto; white-space: pre-wrap; display: none; }
.log.visible { display: block; }
.log .ok { color: var(--accent); }
.log .err { color: var(--danger); }

/* ---------- plans tab ---------- */
.plan-card { display: flex; align-items: center; gap: 14px; }
.plan-card .info { flex: 1; min-width: 0; }
.plan-card h3 { margin: 0 0 2px; font-size: 15.5px; }
.plan-card .sub { font-size: 13px; color: var(--muted); }
.period-tag { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  background: var(--accent-soft); color: var(--accent); padding: 2px 9px; border-radius: 999px; }

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.empty .big { font-size: 34px; margin-bottom: 8px; }

/* ---------- login ---------- */
#loginOverlay {
  position: fixed; inset: 0; z-index: 100; display: grid; place-items: center;
  background: var(--bg); padding: 20px;
}
.confirm-card.login-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: var(--shadow); padding: 34px 36px; max-width: 400px; width: 100%;
  text-align: center;
}
.login-card .big { font-size: 40px; margin-bottom: 8px; }
.login-card h1 { font-size: 22px; margin: 0 0 8px; }
.login-card p { color: var(--muted); font-size: 14.5px; margin: 0 0 18px; }
.login-card input[type=email] {
  width: 100%; padding: 11px 14px; margin-bottom: 10px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.login-card .btn { width: 100%; padding: 11px; font-size: 15px; }
.login-msg { margin-top: 14px; font-size: 14px; color: var(--accent); font-weight: 600; }
.login-msg.error { color: var(--danger); }

.user-menu { display: flex; align-items: center; gap: 6px; }
.user-email { font-size: 12.5px; color: var(--muted); max-width: 140px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 640px) { .user-email { display: none; } }

.filter-chips { display: flex; gap: 3px; background: var(--surface-2);
  border-radius: 999px; padding: 3px; }
.filter-chips button { border: 0; background: none; color: var(--muted);
  padding: 5px 12px; border-radius: 999px; font-weight: 600; font-size: 13px; }
.filter-chips button.active { background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); }

/* mobile: floating jump-to-grocery-list button */
#cartJump {
  display: none; position: fixed; bottom: 18px; right: 18px; z-index: 40;
  background: var(--accent); color: var(--accent-text); border: 0;
  border-radius: 999px; padding: 12px 18px; font-weight: 700; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
@media (max-width: 980px) { #cartJump.visible { display: block; } }

/* toast */
#toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--text); color: var(--bg); padding: 10px 20px; border-radius: 999px;
  font-weight: 600; font-size: 14px; opacity: 0; transition: all .25s; z-index: 99; pointer-events: none; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
