:root {
  --green: #1b5e20;
  --green-dark: #124116;
  --accent: #2e7d32;
  --bg: #f6f7f4;
  --card: #ffffff;
  --text: #1b1f1b;
  --muted: #5f6a60;
  --line: #d9ded9;
  --danger: #b3261e;
  --warn-bg: #fff4d6;
  --warn-text: #6b4d00;
  --hl: #e3f3e4;
  --radius: 14px;
  --tap: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1410;
    --card: #1a211b;
    --text: #eef2ee;
    --muted: #a3ada4;
    --line: #2c352d;
    --hl: #1f3a22;
    --warn-bg: #3a2f00;
    --warn-text: #ffe28a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0 0; font-size: 0.95em; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: var(--tap) 1fr auto; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  background: var(--green); color: #fff;
}
.top h1 { margin: 0; font-size: 1.15rem; font-weight: 600; grid-column: 2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 4px; }
.icon-btn {
  min-width: var(--tap); min-height: var(--tap); border: 0; border-radius: 12px;
  background: transparent; color: inherit; font-size: 1.35rem; display: inline-grid; place-items: center;
}
.icon-btn:active { background: rgba(255,255,255,0.15); }
.top .icon-btn { color: #fff; }

main { padding: 12px 12px calc(96px + var(--safe-bottom)); max-width: 640px; margin: 0 auto; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.card h2 { margin: 0 0 12px; font-size: 1.1rem; }
label { display: block; font-size: 0.9rem; color: var(--muted); margin: 10px 0 4px; }
input[type="text"], input[type="password"], input[type="tel"], select {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text);
}
input:focus-visible, button:focus-visible { outline: 3px solid #7cb342; outline-offset: 2px; }

.btn {
  min-height: var(--tap); padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font-weight: 600;
}
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.link { background: none; border: 0; color: var(--accent); padding: 0 6px; font-weight: 500; }
.btn:disabled { opacity: 0.6; }
.icon { width: 22px; height: 22px; fill: currentColor; vertical-align: -5px; margin-right: 6px; flex: none; }
.icon-btn .icon { margin: 0; width: 24px; height: 24px; }
.row { display: flex; gap: 10px; margin-top: 14px; justify-content: flex-end; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.row.tight { margin: 0; }
.stack > * + * { margin-top: 10px; }

.banner {
  background: var(--warn-bg); color: var(--warn-text); padding: 10px 14px; font-size: 0.95rem;
  display: flex; align-items: center; gap: 12px; justify-content: space-between;
}
.banner .btn { min-height: 36px; padding: 0 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 999px; opacity: 0; transition: opacity .2s, transform .2s;
  pointer-events: none; z-index: 20; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%); }

/* List */
.items { list-style: none; margin: 0; padding: 0; }
.item {
  display: grid; grid-template-columns: var(--tap) 1fr auto var(--tap); align-items: center; gap: 6px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; padding: 4px;
  transition: background .6s;
}
.item.hl { background: var(--hl); }
.item.checked .name { text-decoration: line-through; color: var(--muted); }
.item .check { min-width: var(--tap); min-height: var(--tap); border: 0; background: none; font-size: 1.4rem; color: var(--accent); }
.item .check .box { display: inline-grid; place-items: center; width: 26px; height: 26px; border: 2px solid var(--accent); border-radius: 8px; font-size: 1rem; }
.item.checked .check .box { background: var(--accent); color: #fff; }
.item .body { min-width: 0; padding: 0; }
.item .name { font-weight: 600; word-break: break-word; background: none; border: 0; text-align: left; padding: 0; width: 100%; min-height: var(--tap); display: flex; align-items: center; font-size: 1rem; }
.item .meta { font-size: 0.82rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; padding-bottom: 6px; }
.item .meta:empty { display: none; }
.item .badge { font-size: 0.75rem; padding: 1px 8px; border-radius: 999px; background: var(--line); }
.item .qty { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 999px; }
.item .qty button { min-width: var(--tap); min-height: var(--tap); border: 0; background: none; font-size: 1.3rem; }
.item .qty span { min-width: 24px; text-align: center; font-weight: 600; }
.item .rm { min-width: var(--tap); min-height: var(--tap); border: 0; background: none; color: var(--muted); font-size: 1.3rem; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: 1.6fr 1fr 1fr 1fr; gap: 6px;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  background: var(--card); border-top: 1px solid var(--line);
}
.bottom .btn { padding: 0 6px; font-size: 0.9rem; }
.bottom .btn.primary { font-size: 1.05rem; }

/* Scan */
.scan { position: fixed; inset: 0; background: #000; z-index: 10; display: flex; flex-direction: column; }
.scan video { flex: 1; width: 100%; object-fit: cover; background: #000; }
.scan video.rotated { transform: rotate(90deg); }
.scan .finder {
  position: absolute; left: 8%; right: 8%; top: 38%; height: 22%; border: 3px solid rgba(255,255,255,0.85); border-radius: 16px;
  box-shadow: 0 0 0 100vmax rgba(0,0,0,0.35); pointer-events: none;
}
.scan .flash { position: absolute; inset: 0; background: #4caf50; opacity: 0; pointer-events: none; transition: opacity .15s; }
.scan .flash.on { opacity: 0.45; }
.scan .status { position: absolute; top: calc(12px + var(--safe-top)); left: 12px; right: 12px; color: #fff; text-align: center; text-shadow: 0 1px 3px #000; }
.scan .controls {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 10px 12px calc(10px + var(--safe-bottom)); background: #111;
}
.scan .controls .btn { background: #222; border-color: #333; color: #fff; }
.scan .controls .btn.primary { background: var(--danger); border-color: var(--danger); }
.scan .msg { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; color: #fff; text-align: center; background: rgba(0,0,0,0.7); }

/* Family */
.members { list-style: none; margin: 0; padding: 0; }
.members li { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: var(--tap); border-top: 1px solid var(--line); padding: 4px 0; }
.members li:first-child { border-top: 0; }
.role { font-size: 0.8rem; color: var(--muted); margin-left: 6px; }
.code { font: 600 1.5rem/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.08em; }

.settings-list .btn { width: 100%; text-align: left; margin-top: 8px; }
.link-row { display: flex; justify-content: space-between; margin-top: 12px; flex-wrap: wrap; gap: 8px; }

dialog.sheet { border: 0; border-radius: 18px; padding: 20px; width: min(92vw, 420px); background: var(--card); color: var(--text); }
dialog.sheet::backdrop { background: rgba(0,0,0,0.45); }
dialog.sheet h2 { margin: 0 0 6px; font-size: 1.1rem; }

@media (prefers-reduced-motion: reduce) {
  .toast, .item, .scan .flash { transition: none; }
}
