:root {
  color-scheme: light dark;
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1a2029;
  --text-muted: #6b7280;
  --border: #e2e6ec;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(20, 25, 35, 0.08), 0 1px 2px rgba(20, 25, 35, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --surface: #1a2029;
    --surface-2: #222a35;
    --text: #f1f3f6;
    --text-muted: #9aa4b2;
    --border: #2b3543;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, input {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- Header ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px calc(14px + env(safe-area-inset-top)) 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  flex: 1;
  font-weight: 600;
}

.icon-btn {
  border: none;
  background: var(--surface-2);
  color: var(--text);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 20px; height: 20px; }

.back-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  border: none;
  background: var(--surface-2);
  color: var(--text);
  padding: 10px 14px 10px 10px;
  min-height: 44px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}
.back-btn:active { transform: scale(0.96); }
.back-btn svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ---------- Current user / presence badges ---------- */
.user-chip {
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 999px;
  flex-shrink: 0;
  white-space: nowrap;
  margin-right: 8px;
}

.header-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
  margin-left: auto;
}
.header-meta .me {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  white-space: nowrap;
}
.presence-pill {
  background: #f59e0b22;
  color: #b45309;
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}
@media (prefers-color-scheme: dark) {
  .presence-pill { color: #fbbf24; }
}

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 8px;
  border-radius: 10px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
}
.tab.active {
  background: var(--surface-2);
  color: var(--text);
}

.content {
  flex: 1;
  padding: 16px;
  padding-bottom: 100px;
}

/* ---------- Store chips ---------- */
.section-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 4px 0 10px;
}

.store-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 24px;
}

.store-chip {
  border: none;
  border-radius: var(--radius);
  padding: 18px 14px;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 64px;
  text-align: left;
}
.store-chip:active { transform: scale(0.97); }

.store-logo {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  flex-shrink: 0;
  object-fit: contain;
  background: #ffffff;
  padding: 4px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06);
}

/* ---------- List cards ---------- */
.list-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.list-card:active { transform: scale(0.98); }

.list-card .store-logo { width: 44px; height: 44px; font-size: 15px; }

.list-card-info { flex: 1; min-width: 0; }
.list-card-info .name { font-weight: 700; font-size: 15px; }
.list-card-info .meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px;
  font-size: 14px;
}

/* ---------- Shopping list detail ---------- */
.list-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.list-header .store-logo { width: 36px; height: 36px; font-size: 13px; }
.list-header .title { flex: 1; min-width: 0; font-weight: 700; font-size: 16px; }
.list-header .subtitle { font-size: 12px; color: var(--text-muted); font-weight: 400; }

#items-list-unchecked {
  margin-top: 10px;
}

.section-divider {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 16px 2px 8px;
}

.item-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}

.item-check {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
}
.item-check.checked {
  background: var(--success);
  border-color: var(--success);
  color: white;
}

.item-name {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  min-width: 0;
  overflow-wrap: anywhere;
}
.item-row.checked .item-name {
  text-decoration: line-through;
  color: var(--text-muted);
}

.stepper {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface-2);
  border-radius: 10px;
  flex-shrink: 0;
}
.stepper button {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  color: var(--text);
}
.stepper button:active { opacity: 0.6; }
.stepper .qty {
  min-width: 26px;
  text-align: center;
  font-weight: 700;
  font-size: 14px;
}

.remove-btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.remove-btn:active { color: var(--danger); }

/* ---------- Bottom panel (undo + add item + complete) ---------- */
.bottom-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)) 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
}
.bottom-panel-inner {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.autocomplete-wrap { position: relative; }

.add-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.add-input-row input[type="text"] {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 15px;
  min-width: 0;
}
.add-input-row input:focus { outline: 2px solid var(--accent); }

.qty-picker {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 4px;
}
.qty-picker button {
  width: 30px;
  height: 30px;
  border: none;
  background: var(--surface);
  border-radius: 8px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
}
.qty-picker .qty { min-width: 20px; text-align: center; font-weight: 700; }

.add-btn {
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 10px;
  padding: 12px 16px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}
.add-btn:active { opacity: 0.85; }
.add-btn:disabled { opacity: 0.5; cursor: default; }

.suggestions {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  margin-bottom: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-height: 220px;
  overflow-y: auto;
  z-index: 20;
}
.suggestion-item {
  padding: 12px 14px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.suggestion-item:last-child { border-bottom: none; }
.suggestion-item:active { background: var(--surface-2); }

/* ---------- Undo chip & complete button (inside bottom panel) ---------- */
.undo-fab {
  align-self: flex-start;
  border: none;
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 9px 14px;
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
}

.complete-btn {
  width: 100%;
  border: none;
  background: var(--success);
  color: white;
  padding: 13px;
  border-radius: 12px;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
}

.confirm-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
}
.confirm-row span { flex: 1; }
.confirm-yes, .confirm-no {
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  font-weight: 700;
  cursor: pointer;
  font-size: 13px;
}
.confirm-yes { background: var(--success); color: white; }
.confirm-no { background: var(--surface); color: var(--text); }

/* ---------- Drag & drop ---------- */
.drag-handle {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin: -4px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  text-align: center;
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.drag-handle:active { background: var(--surface-2); }
.item-row.sortable-ghost { opacity: 0.4; }
.item-row.sortable-chosen { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); }

.list-header { cursor: pointer; }

/* ---------- Loyalty cards modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}
@media (min-width: 560px) {
  .modal-overlay { align-items: center; }
}
.modal-card {
  width: 100%;
  max-width: 560px;
  max-height: 85vh;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
@media (min-width: 560px) {
  .modal-card { border-radius: 18px; max-height: 80vh; }
}
.modal-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-title { flex: 1; font-weight: 700; font-size: 15px; }
.modal-body {
  padding: 16px;
  overflow-y: auto;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.card-thumb {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: var(--shadow);
}
.card-thumb img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  display: block;
  cursor: pointer;
}
.card-thumb-name {
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card-thumb-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: white;
  cursor: pointer;
  font-size: 12px;
}

.card-upload {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.card-upload input[type="text"] {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 14px;
}
.card-upload input[type="file"] { font-size: 13px; }

.fullscreen-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.fullscreen-overlay img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 20px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card h1 { font-size: 20px; margin: 0 0 6px; text-align: center; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card input {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 15px;
}
.login-card button {
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  padding: 13px;
  border-radius: 10px;
  font-weight: 700;
  cursor: pointer;
  margin-top: 4px;
}
.login-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  min-height: 18px;
}

/* ---------- History detail (readonly) ---------- */
.history-item-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}
.history-item-row .item-name { font-size: 14px; }
.history-item-row .qty-badge {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 4px 8px;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}

.spinner-wrap { display: flex; justify-content: center; padding: 40px; color: var(--text-muted); }
