:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e3e5ea;
  --accent: #16a34a;
  --accent-dark: #128040;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #191c22;
    --surface-2: #22262e;
    --text: #e8eaed;
    --muted: #9aa1ad;
    --border: #2c313a;
    --accent: #22c55e;
    --accent-dark: #16a34a;
    --danger: #f87171;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 17px;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
}

.muted { color: var(--muted); }

/* ---------- login ---------- */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 20px; }
.login-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 32px 26px; width: 100%; max-width: 380px; text-align: center;
}
.login-logo { font-size: 48px; line-height: 1; }
.login-card h1 { font-size: 24px; margin: 12px 0 4px; }
.login-card p { margin: 0 0 22px; font-size: 15px; }
.login-card label {
  display: block; text-align: left; font-size: 13px; font-weight: 600;
  color: var(--muted); margin: 14px 0 6px;
}
.login-card input {
  width: 100%; padding: 13px 14px; font-size: 17px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.login-card button { margin-top: 22px; width: 100%; }
.alert {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  color: var(--danger); border-radius: 10px; padding: 11px 14px;
  font-size: 15px; margin-bottom: 6px;
}

/* ---------- struttura ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: var(--accent); color: #fff; box-shadow: var(--shadow);
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 650; }
.topbar-right { display: flex; align-items: center; gap: 6px; }
.topbar form { display: contents; }
.who { font-size: 13px; opacity: .9; max-width: 90px; overflow: hidden;
       text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  background: rgba(255,255,255,.16); border: 0; color: #fff; cursor: pointer;
  width: 36px; height: 36px; border-radius: 9px; font-size: 16px;
  display: grid; place-items: center;
}
.icon-btn:active { background: rgba(255,255,255,.3); }
.badge { font-size: 14px; opacity: .9; }

main { max-width: 640px; margin: 0 auto; padding: 14px 12px 130px; }

/* ---------- aggiunta ---------- */
.add-row { position: relative; display: flex; gap: 8px; }
#add-input {
  flex: 1; padding: 14px 16px; font-size: 17px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
}
#add-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn-add {
  width: 52px; border: 0; border-radius: 12px; background: var(--accent);
  color: #fff; font-size: 28px; line-height: 1; cursor: pointer; box-shadow: var(--shadow);
}
.btn-add:active { background: var(--accent-dark); }

.suggestions {
  position: absolute; top: calc(100% + 6px); left: 0; right: 60px; z-index: 15;
  margin: 0; padding: 6px; list-style: none; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  max-height: 250px; overflow-y: auto;
}
.suggestions li { padding: 11px 12px; border-radius: 8px; cursor: pointer; }
.suggestions li:hover, .suggestions li.active { background: var(--surface-2); }

/* ---------- stato ---------- */
.status-line {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; color: var(--muted); padding: 16px 4px 8px;
}
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); transition: background .3s; }
.sync-dot.stale { background: var(--muted); }
.sync-dot.error { background: var(--danger); }

/* ---------- lista ---------- */
.items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.item.checked { opacity: .55; }
.item.checked .item-name { text-decoration: line-through; }

.check {
  flex-shrink: 0; width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
  border: 2px solid var(--border); background: transparent; color: transparent;
  display: grid; place-items: center; font-size: 15px; padding: 0;
}
.item.checked .check { background: var(--accent); border-color: var(--accent); color: #fff; }

.item-main { flex: 1; min-width: 0; cursor: pointer; }
.item-name { font-weight: 550; overflow-wrap: anywhere; }
.item-meta { font-size: 12px; color: var(--muted); }

.qty { display: flex; align-items: center; gap: 2px; }
.qty button {
  width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 17px; cursor: pointer; padding: 0;
}
.qty span { min-width: 26px; text-align: center; font-variant-numeric: tabular-nums; font-size: 15px; }

.del {
  flex-shrink: 0; width: 34px; height: 34px; border: 0; border-radius: 9px;
  background: transparent; color: var(--muted); font-size: 17px; cursor: pointer;
}
.del:active { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }

.empty { text-align: center; color: var(--text); padding: 46px 20px; font-size: 16px; }

/* ---------- barra inferiore ---------- */
.bottombar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; gap: 10px; padding: 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-top: 1px solid var(--border);
}
.bottombar > * { flex: 1; }
.bottombar > .btn-secondary { flex: 0 1 auto; }

.btn-primary, .btn-secondary {
  padding: 14px 16px; font-size: 16px; font-weight: 600; border-radius: 12px;
  cursor: pointer; border: 1px solid transparent; font-family: inherit;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: var(--accent-dark); }
.btn-primary:disabled { background: var(--muted); cursor: not-allowed; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:active { background: var(--surface-2); }

/* ---------- storico ---------- */
dialog {
  border: 0; border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 22px; max-width: 520px; width: calc(100% - 32px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h2 { margin: 0 0 14px; font-size: 19px; }
.history-body { max-height: 60vh; overflow-y: auto; margin-bottom: 16px; }
.trip { border-top: 1px solid var(--border); padding: 12px 0; }
.trip:first-child { border-top: 0; }
.trip-head { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
dialog .btn-secondary { width: 100%; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 40;
  background: var(--text); color: var(--bg); padding: 11px 18px;
  border-radius: 999px; font-size: 15px; box-shadow: var(--shadow);
  animation: rise .18s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } }
