:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1f2421;
  --muted: #6b716d;
  --border: #e3e0d8;
  --primary: #2e7d4f;
  --primary-text: #ffffff;
  --needed: #e0544e;
  --needed-soft: #fdecea;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .05);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151816;
    --surface: #1f2421;
    --text: #eef1ee;
    --muted: #9aa29d;
    --border: #2f3531;
    --primary: #4caf7a;
    --primary-text: #0d1a12;
    --needed: #ff7a73;
    --needed-soft: #3a2220;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

h1, h2 { margin: 0; }
h2 { font-size: 1.25rem; }
.muted { color: var(--muted); }
.error { color: var(--needed); margin: 12px 0 0; }

/* ---------- Buttons & inputs ---------- */

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.ghost { background: transparent; }
.btn.small { padding: 8px 14px; font-size: .9rem; }
.btn.danger { color: var(--needed); border-color: var(--needed); }
.btn[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 1.4rem;
  padding: 6px;
  cursor: pointer;
}

input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  width: 100%;
}
input:focus, select:focus, .btn:focus-visible, .tile:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--primary) 45%, transparent);
  outline-offset: 1px;
}

.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 10px; justify-content: flex-end; }
.row.wrap { flex-wrap: wrap; justify-content: flex-start; }
label { font-weight: 600; font-size: .9rem; }
label.check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
label.check input { width: auto; }

/* ---------- Welcome ---------- */

.welcome {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.welcome-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  text-align: center;
}
.welcome-card form { text-align: left; margin-top: 18px; }
.welcome-logo { font-size: 3.2rem; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 18px 0 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* ---------- App shell ---------- */

#app { max-width: 960px; margin: 0 auto; padding-bottom: 80px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 6px;
}
.brand { font-size: 1.3rem; }

.tabs {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 6px;
  padding: 8px 16px;
  background: var(--bg);
}
.tab {
  flex: 1;
  border: 0;
  border-radius: 999px;
  padding: 11px 10px;
  font: inherit;
  font-weight: 600;
  background: var(--surface);
  color: var(--muted);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.tab.active { background: var(--primary); color: var(--primary-text); }
.count {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--needed);
  color: #fff;
  font-size: .8rem;
  line-height: 1.6em;
}
.count.zero { background: var(--border); color: var(--muted); }

.view { padding: 8px 16px; }

.toolbar { display: flex; gap: 8px; align-items: center; }
.toolbar input { flex: 1; min-width: 0; }
.toolbar .btn { white-space: nowrap; }
.hint { font-size: .85rem; margin: 10px 2px 4px; }

/* ---------- Categories & tiles ---------- */

.category { margin-top: 18px; }
.category-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.05rem;
  margin: 0 0 10px;
  padding-left: 10px;
  border-left: 5px solid var(--cat, var(--primary));
}
.category-title .badge {
  font-size: .75rem;
  font-weight: 700;
  color: var(--needed);
  background: var(--needed-soft);
  border-radius: 999px;
  padding: 2px 8px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 10px;
}

.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 96px;
  padding: 10px 6px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  user-select: none;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
.tile:active { transform: scale(.95); }
.tile .emoji { font-size: 2.3rem; line-height: 1.1; }
.tile .name { font-size: .8rem; font-weight: 600; text-align: center; overflow-wrap: break-word; hyphens: auto; max-width: 100%; }
.tile .name.long { font-size: .7rem; }
.tile.needed { background: var(--needed-soft); border-color: var(--needed); }
.tile.needed::after {
  content: "On list";
  position: absolute;
  top: 6px;
  right: 6px;
  font-size: .65rem;
  font-weight: 700;
  color: #fff;
  background: var(--needed);
  border-radius: 999px;
  padding: 1px 6px;
}
.tile.pop { animation: pop .25s ease; }
@keyframes pop { 50% { transform: scale(1.08); } }

.editing .tile { animation: wiggle .3s ease-in-out infinite alternate; }
.editing .tile::before {
  content: "✕";
  position: absolute;
  top: -6px;
  left: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  font-size: .75rem;
  display: grid;
  place-items: center;
}
.editing .tile.needed::after { display: none; }
@keyframes wiggle { from { transform: rotate(-1deg); } to { transform: rotate(1deg); } }

/* ---------- Shopping list ---------- */

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.list-item .emoji { font-size: 1.8rem; }
.list-item .name { flex: 1; font-weight: 600; }
.list-item .tick {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
}
.list-item.done { opacity: .5; }
.list-item.done .name { text-decoration: line-through; }
.list-item.done .tick { background: var(--primary); border-color: var(--primary); }

.list-actions { display: flex; justify-content: center; margin-top: 24px; }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty-emoji { font-size: 3rem; }

/* ---------- Dialogs ---------- */

dialog {
  width: min(440px, calc(100vw - 32px));
  border: 0;
  border-radius: 20px;
  padding: 22px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog hr { border: 0; border-top: 1px solid var(--border); width: 100%; }

.key {
  display: block;
  font: 600 1.05rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .04em;
  text-align: center;
  padding: 14px 10px;
  background: var(--bg);
  border-radius: 10px;
  user-select: all;
  overflow-wrap: anywhere;
}

.emoji-choices { display: flex; flex-wrap: wrap; gap: 6px; }
.emoji-choices button {
  font-size: 1.5rem;
  border: 2px solid transparent;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px 6px;
  cursor: pointer;
}
.emoji-choices button.selected { border-color: var(--primary); }

/* ---------- Toast & sync status ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 12px;
  background: #222;
  color: #fff;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
  z-index: 20;
}
.toast-undo {
  border: 0;
  background: transparent;
  color: #8fe3b0;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.sync-status {
  position: fixed;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  font-size: .8rem;
  color: var(--muted);
  pointer-events: none;
}
.sync-status.offline { color: var(--needed); }

/* ---------- Accounts ---------- */

.small { font-size: .85rem; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
.link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--primary);
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}
.google-section { margin-top: 18px; }
.google-section .divider { margin-top: 14px; }
.google-btn { display: flex; justify-content: center; min-height: 44px; }
.signed-in-banner {
  margin-top: 16px;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg);
}
.signed-in-banner p { margin: 0 0 4px; }
.back-btn { margin-top: 14px; }
.row.inline { justify-content: stretch; align-items: center; }
.row.inline input { flex: 1; min-width: 0; }
dialog .stack > p { margin: 0; }
dialog h2 { margin-top: 4px; }

.household-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.household-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--bg);
}
.household-list .hh-name { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
.household-list .current { color: var(--primary); font-size: .8rem; font-weight: 700; }

/* ---------- Legal pages & links ---------- */

.legal {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 48px;
  line-height: 1.6;
}
.legal h1 { font-size: 1.8rem; margin: 18px 0 4px; }
.legal h2 { font-size: 1.15rem; margin: 28px 0 8px; }
.legal h3 { font-size: 1rem; margin: 18px 0 6px; }
.legal p, .legal li { overflow-wrap: break-word; }
.legal ul { padding-left: 22px; }
.legal a { color: var(--primary); }
.legal code { font-size: .9em; }
.legal-back { font-weight: 600; text-decoration: none; }
.legal-summary {
  margin-top: 18px;
  padding: 4px 18px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.legal-footer { margin-top: 36px; color: var(--muted); }

.legal-links { margin: 16px 0 0; font-size: .8rem; color: var(--muted); }
.legal-links a { color: inherit; }
