:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-dim: #6b6b6b;
  --border: #e5e5e0;
  --accent: #d97706;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --good: #16a34a;
  --caution: #ca8a04;
  --avoid: #dc2626;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --surface: #1c1c1c;
    --text: #f2f2f2;
    --text-dim: #a0a0a0;
    --border: #2e2e2e;
    --accent: #f59e0b;
    --accent-text: #1a1a1a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  padding-bottom: 76px;
}

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: env(safe-area-inset-top, 12px) 16px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.app-header h1 { font-size: 1.15rem; margin: 0; }

.whoami { display: flex; gap: 6px; }
.whoami button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-dim);
  border-radius: 999px; padding: 4px 12px; font-size: 0.8rem; cursor: pointer;
}
.whoami button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }

#app { padding: 12px 16px 24px; max-width: 720px; margin: 0 auto; }
.tab h2 { margin: 4px 0 12px; font-size: 1.3rem; }

.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }

.input, textarea {
  width: 100%; padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 1rem;
}
.input.qty { max-width: 90px; }
textarea { font-family: inherit; resize: vertical; }

.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 8px 14px; font-size: 0.9rem; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.subtle { color: var(--text-dim); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer;
}
.card h3 { margin: 0 0 4px; font-size: 1.02rem; }
.card .meta { color: var(--text-dim); font-size: 0.82rem; }
.recipe-card-row { display: flex; gap: 10px; align-items: flex-start; }
.recipe-card-body { flex: 1; min-width: 0; }
.recipe-thumb { width: 56px; height: 56px; border-radius: 8px; object-fit: cover; flex-shrink: 0; background: var(--bg); }
.recipe-hero-image, .recipe-hero-video, .draft-image-preview {
  width: 100%; border-radius: var(--radius); margin-bottom: 10px; display: block; background: #000;
}
.recipe-hero-image, .draft-image-preview { max-height: 260px; object-fit: cover; }
.recipe-hero-video { max-height: 340px; }
.tag {
  display: inline-block; background: var(--bg); border: 1px solid var(--border); border-radius: 999px;
  padding: 2px 9px; font-size: 0.72rem; color: var(--text-dim); margin-right: 4px;
}
.badge { border-radius: 999px; padding: 2px 8px; font-size: 0.72rem; font-weight: 600; }
.badge.pending { background: #fef3c7; color: #92400e; }
.badge.good { background: #dcfce7; color: #166534; }
.badge.caution { background: #fef9c3; color: #854d0e; }
.badge.avoid { background: #fee2e2; color: #991b1b; }
.star { color: var(--accent); }

.empty { color: var(--text-dim); text-align: center; padding: 24px 12px; font-size: 0.9rem; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; display: flex; background: var(--surface);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom, 0);
}
.tab-btn {
  flex: 1; border: none; background: none; color: var(--text-dim); padding: 8px 4px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 0.68rem; cursor: pointer;
}
.tab-btn span { font-weight: 500; }
.tab-btn.active { color: var(--accent); }
.tab-btn .icon { font-size: 1.25rem; }

.paste-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.paste-box textarea { margin: 8px 0; }

.week-nav { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; color: var(--text-dim); }

.day-block { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.day-block h4 { margin: 0 0 8px; font-size: 0.95rem; }
.slot-row { display: flex; justify-content: space-between; align-items: center; padding: 5px 0; border-top: 1px dashed var(--border); }
.slot-row:first-of-type { border-top: none; }
.slot-label { font-size: 0.78rem; color: var(--text-dim); width: 72px; flex-shrink: 0; text-transform: capitalize; }
.slot-recipe { flex: 1; font-size: 0.9rem; }
.slot-empty { color: var(--text-dim); font-size: 0.85rem; }

.shopping-group h4 { margin: 14px 0 6px; font-size: 0.85rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.02em; }
.section-divider {
  margin: 28px 0 4px; padding-top: 14px; border-top: 2px solid var(--border);
  font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-dim);
}
.shopping-item { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.shopping-item.checked .name { text-decoration: line-through; color: var(--text-dim); }
.shopping-item input[type=checkbox] { width: 20px; height: 20px; }
.shopping-item .name { flex: 1; }
.shopping-item .qty { color: var(--text-dim); font-size: 0.82rem; }
.shopping-item .del { background: none; border: none; color: var(--text-dim); font-size: 1.1rem; cursor: pointer; }

.person-card { display: flex; align-items: center; justify-content: space-between; gap: 10px; cursor: default; }
.person-card .person-info { display: flex; align-items: center; cursor: pointer; flex: 1; min-width: 0; }
.person-card .del { flex-shrink: 0; font-size: 1.1rem; }
.person-card .avatar {
  width: 36px; height: 36px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; margin-right: 8px; flex-shrink: 0;
}
.wizard-chip-grid { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 4px; }
.wizard-chip { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; font-size: 0.85rem; cursor: pointer; }
.wizard-chip.love { border-color: #86efac; background: #dcfce7; color: #166534; }
.wizard-chip.dislike { border-color: #fca5a5; background: #fee2e2; color: #991b1b; }
.pref-group { margin-top: 10px; }
.pref-group h5 { margin: 0 0 6px; font-size: 0.78rem; text-transform: uppercase; color: var(--text-dim); }
.pref-chip { display: inline-flex; align-items: center; gap: 6px; background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 3px 6px 3px 10px; font-size: 0.8rem; margin: 2px; }
.pref-chip.love { border-color: #86efac; }
.pref-chip.dislike, .pref-chip.allergic { border-color: #fca5a5; }
.pref-chip button { background: none; border: none; color: var(--text-dim); cursor: pointer; padding: 0 2px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end;
  justify-content: center; z-index: 50;
}
.modal-sheet {
  background: var(--surface); width: 100%; max-width: 720px; border-radius: 16px 16px 0 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom, 0)); max-height: 88vh; overflow-y: auto;
}
.modal-sheet h3 { margin-top: 0; }
.modal-close { float: right; background: none; border: none; font-size: 1.3rem; color: var(--text-dim); cursor: pointer; }
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 0.8rem; color: var(--text-dim); margin-bottom: 4px; }
.method-text { white-space: pre-wrap; line-height: 1.5; }
.ingredient-row { display: flex; gap: 6px; margin-bottom: 6px; }
.ingredient-row input { flex: 1; }
.ingredient-row input.qty { flex: 0 0 60px; }
.ingredient-row input.unit { flex: 0 0 70px; }
.source-link { display: inline-block; margin-top: 6px; font-size: 0.85rem; color: var(--accent); text-decoration: none; }
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: #1a1a1a; color: #fff;
  padding: 10px 18px; border-radius: 999px; font-size: 0.85rem; z-index: 60; opacity: 0; transition: opacity 0.2s;
}
.toast.show { opacity: 1; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.diner-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.diner-picker label { display: flex; align-items: center; gap: 4px; border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; font-size: 0.85rem; cursor: pointer; }

/* The chat tab is its own fixed pane between the header and tab bar (heights measured in JS
   into --header-h/--tabbar-h, since they vary with safe-area insets on notched phones) — only
   .chat-messages scrolls internally, like a normal chat app, instead of the whole page scrolling. */
#tab-chat {
  position: fixed; left: 0; right: 0;
  /* max() with a CSS-native floor (base content height + the real env() safe-area inset) guards
     against the JS-measured --header-h/--tabbar-h being stale on a real device — e.g. measured
     once on load before Safari has resolved the home-indicator safe-area-inset-bottom, which
     otherwise left the input bar sitting under the tab bar on notched iPhones. */
  top: max(var(--header-h, 60px), calc(44px + env(safe-area-inset-top, 0px)));
  bottom: max(var(--tabbar-h, 60px), calc(49px + env(safe-area-inset-bottom, 0px)));
  display: flex; flex-direction: column;
  max-width: 720px; margin: 0 auto; padding: 12px 16px 0;
  overflow: hidden;
}
/* #tab-chat's own `display: flex` (an ID selector) outranks the browser default [hidden]
   { display: none } (an attribute selector) — without this, switching tabs via the `hidden`
   attribute stops actually hiding the fixed chat pane, and it floats on top of every other tab. */
#tab-chat[hidden] { display: none; }
#tab-chat .row-between { flex-shrink: 0; }
.chat-messages { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; gap: 10px; padding-bottom: 8px; }
.chat-bubble { max-width: 82%; padding: 10px 13px; border-radius: 16px; background: var(--surface); border: 1px solid var(--border); }
.chat-bubble.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.chat-bubble.user .chat-who { color: rgba(0,0,0,0.55); }
.chat-bubble.assistant { align-self: flex-start; }
.chat-who { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-dim); margin-bottom: 2px; }
.chat-text { line-height: 1.45; font-size: 0.95rem; }
.chat-text div { margin: 2px 0; }
.chat-text ul, .chat-text ol { margin: 4px 0 8px; padding-left: 22px; }
.chat-text li { margin: 2px 0; }
.chat-text strong { font-weight: 700; }
.chat-recipe-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip-btn { background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; font-size: 0.8rem; cursor: pointer; color: var(--text); }
.chat-input-row { flex-shrink: 0; display: flex; gap: 8px; background: var(--bg); padding: 10px 0; }
.chat-input-row .input { flex: 1; }

/* Auth screen */
.auth-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
}
.auth-card h1 { text-align: center; margin: 0 0 20px; font-size: 1.4rem; }
.auth-card #auth-submit { width: 100%; margin-top: 4px; }
.auth-card #auth-toggle-mode { width: 100%; margin-top: 12px; }
.auth-error { color: var(--danger); font-size: 0.85rem; margin-bottom: 10px; }
.whoami-static { display: flex; align-items: center; gap: 10px; font-size: 0.85rem; color: var(--text-dim); }
.whoami-static button { background: none; border: none; color: var(--text-dim); text-decoration: underline; cursor: pointer; font-size: 0.85rem; padding: 0; }
#admin-section { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }
.text-dim { color: var(--text-dim); font-size: 0.85rem; }

/* Same [hidden]-vs-author-stylesheet specificity issue as #tab-chat[hidden] above — these three
   all set their own `display`, which otherwise wins the tie against the browser's default
   [hidden] { display: none }, leaving them visible pre-login. */
.app-header[hidden], #app[hidden], .tabbar[hidden], .auth-screen[hidden] { display: none; }
.recipe-filters { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.recipe-filters select, .recipe-filters input { flex: 1 1 140px; min-width: 0; }
.favorite-btn { background: none; border: none; font-size: 1.1rem; cursor: pointer; padding: 0 0 0 8px; line-height: 1; flex-shrink: 0; opacity: 0.55; }
.favorite-btn.active { opacity: 1; }
