:root {
  --bg: #f6f7f5;
  --card: #ffffff;
  --text: #1d231f;
  --muted: #6b746e;
  --line: #e2e6e3;
  --accent: #2e7d32;
  --accent-soft: #e5f2e6;
  --danger: #c62828;
  --warn: #e0a100;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121513;
    --card: #1c211e;
    --text: #e8ece9;
    --muted: #97a19b;
    --line: #2c332e;
    --accent: #66bb6a;
    --accent-soft: #1f3321;
    --danger: #ef5350;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
.topbar {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
h1 { font-size: 1.3rem; margin: 0; }
h2 { font-size: .95rem; margin: 0; color: var(--muted); font-weight: 600; }
main { max-width: 720px; margin: 0 auto; padding: 12px 16px 40px; }

.sync {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 999px; padding: 6px 12px; font: inherit; font-size: .85rem; cursor: pointer;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--accent); }
.dot.busy { background: var(--warn); animation: pulse 1s infinite; }
.dot.err { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.add { display: flex; gap: 8px; margin-bottom: 14px; }
.add input {
  flex: 1; min-width: 0;
  font: inherit; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--text);
}
.add input.qty { flex: 0 0 90px; }
.add button {
  flex: 0 0 50px; font-size: 1.6rem; line-height: 1;
  border: 0; border-radius: 12px; background: var(--accent); color: #fff; cursor: pointer;
}

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.item {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 6px 6px 6px 10px; min-height: 52px;
  user-select: none;
}
.item .name { flex: 1; overflow-wrap: anywhere; cursor: pointer; }
.item .amount { color: var(--muted); font-size: .9rem; white-space: nowrap; }
.check {
  flex: 0 0 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--accent); background: transparent; cursor: pointer; padding: 0;
}
.del {
  flex: 0 0 40px; height: 40px; border: 0; background: transparent;
  color: var(--muted); font-size: 1.5rem; cursor: pointer; border-radius: 10px;
}
.del:active { background: var(--line); }
.grip {
  flex: 0 0 32px; height: 40px; display: grid; place-items: center;
  color: var(--muted); font-size: 1.3rem; cursor: grab; touch-action: none; border-radius: 10px;
}
.item.dragging { opacity: .7; box-shadow: 0 4px 14px rgba(0,0,0,.18); pointer-events: none; }
.item.dragging .grip { pointer-events: auto; cursor: grabbing; }

.done .item { opacity: .6; }
.done .name { text-decoration: line-through; }
.done .check { background: var(--accent); position: relative; }
.done .check::after {
  content: ""; position: absolute; left: 8px; top: 3px; width: 6px; height: 12px;
  border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg);
}

#doneSection { margin-top: 24px; }
.doneHead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.link { border: 0; background: none; color: var(--danger); font: inherit; font-size: .9rem; cursor: pointer; padding: 6px 0; }
.empty { color: var(--muted); text-align: center; margin: 30px 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  max-width: calc(100% - 32px); display: flex; gap: 14px; align-items: center;
}
.toast button { border: 0; background: none; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; }
.toast[hidden], [hidden] { display: none !important; }
