:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1f2421;
  --muted: #6b716d;
  --line: #e3e1da;
  --accent: #2f7d4f;
  --accent-text: #ffffff;
  --accent-soft: #e3f1e8;
  --danger: #b3261e;
  --badge: #efe9dc;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141614;
    --surface: #1e211f;
    --text: #e8ebe8;
    --muted: #9aa29d;
    --line: #2f3431;
    --accent: #5fbf85;
    --accent-text: #0d1a12;
    --accent-soft: #1f3528;
    --danger: #ff8a80;
    --badge: #2c2a24;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}

button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}
textarea { min-height: 160px; resize: vertical; }
button {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
  cursor: pointer;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 6px 4px; min-height: 0; }
button:disabled { opacity: 0.5; }

h1 { font-size: 24px; margin: 0 0 8px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 22px 4px 8px; font-weight: 600; }
h3 { font-size: 18px; margin: 18px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.error { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; }

.screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 20px; box-shadow: var(--shadow); }
.narrow { width: 100%; max-width: 380px; display: grid; gap: 12px; }

#app { max-width: 640px; margin: 0 auto; padding: 0 16px 96px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 0 10px;
  background: var(--bg);
}
.modes { display: flex; flex: 1; background: var(--line); border-radius: 12px; padding: 3px; }
.modes button { flex: 1; border: none; background: transparent; border-radius: 9px; min-height: 38px; font-weight: 600; color: var(--muted); }
.modes button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.icon-btn { width: 44px; padding: 0; font-size: 20px; }
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.sync-dot.live { background: var(--accent); }

.add-form { position: relative; display: grid; gap: 8px; margin-bottom: 4px; }
.add-row { display: flex; gap: 8px; }
.add-row .qty { width: 76px; flex: none; }
.add-row.secondary { font-size: 15px; color: var(--muted); }
.inline { display: flex; align-items: center; gap: 8px; }
.inline select { width: auto; min-height: 36px; padding: 4px 8px; }
.suggestions {
  position: absolute; top: 52px; left: 0; right: 0; z-index: 6;
  list-style: none; margin: 0; padding: 4px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}
.suggestions button { width: 100%; text-align: left; border: none; background: none; display: flex; justify-content: space-between; gap: 8px; }
.suggestions button:hover, .suggestions button:focus { background: var(--accent-soft); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border-radius: 999px; padding: 6px 12px; min-height: 36px; font-size: 15px; }

.group { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; box-shadow: var(--shadow); }
.row { display: flex; align-items: center; gap: 4px; border-top: 1px solid var(--line); min-height: 52px; }
.row:first-child { border-top: none; }
.check {
  flex: none; width: 52px; height: 52px; border: none; background: none; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.check span { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--muted); display: block; }
.row.done .check span { background: var(--accent); border-color: var(--accent); }
.row-main { flex: 1; min-width: 0; text-align: left; border: none; background: none; padding: 8px 4px 8px 0; min-height: 52px; border-radius: 0; }
.row-title { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px; }
.row-name { font-weight: 500; overflow-wrap: anywhere; }
.row-qty { color: var(--muted); font-size: 15px; }
.row-meta { font-size: 13px; color: var(--muted); }
.row.done .row-name { text-decoration: line-through; color: var(--muted); }
.badge { font-size: 12px; background: var(--badge); border-radius: 6px; padding: 1px 6px; color: var(--muted); }
.more { flex: none; width: 44px; border: none; background: none; color: var(--muted); font-size: 20px; }

.store-mode .row { min-height: 60px; }
.store-mode .row-name { font-size: 19px; }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }
.bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 22px 4px 8px; }
.bar h2 { margin: 0; }
.store-picker { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.store-picker select { flex: 1; font-weight: 600; }
.finish { width: 100%; margin-top: 16px; }

.sheet {
  border: none; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 90vh;
  background: var(--bg); color: var(--text); border-radius: 18px 18px 0 0;
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }
#sheet-body { padding: 18px 16px calc(env(safe-area-inset-bottom) + 18px); display: grid; gap: 12px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head h1 { margin: 0; font-size: 20px; }
.field { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.field > * { font-size: 17px; color: var(--text); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions > button { flex: 1; }
.list-btn { display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; }
.toggle { display: flex; align-items: center; gap: 10px; }
.toggle input { width: 22px; min-height: 22px; height: 22px; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px); transform: translate(-50%, 40px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 15px;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; max-width: calc(100% - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
