:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1c2733;
  --muted: #6b7a8c;
  --accent: #16a34a;
  --accent-soft: #e3f6ea;
  --danger: #dc2626;
  --border: #e3e8ee;
  --shadow: 0 1px 3px rgba(20, 35, 50, .08);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --card: #1a222b;
    --text: #e8edf2;
    --muted: #8b9aab;
    --accent: #22c55e;
    --accent-soft: #15301f;
    --border: #2a3542;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}

body {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: calc(86px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

/* ----- Cabecera ----- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 6px;
}
.topbar h1 { margin: 0; font-size: 1.35rem; }
.icon-btn {
  background: none;
  border: none;
  font-size: 1.35rem;
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
}
.icon-btn:active { background: var(--border); }

/* ----- Pestañas de supermercados (sublistas) ----- */
.tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 16px 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: .9rem;
  cursor: pointer;
  white-space: nowrap;
}
.tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.tab .dot { width: 10px; height: 10px; border-radius: 50%; }
.tab .count {
  background: rgba(127, 127, 127, .18);
  border-radius: 999px;
  font-size: .75rem;
  padding: 1px 7px;
}
.tab.active .count { background: rgba(255, 255, 255, .25); }

/* ----- Lista ----- */
.list { padding: 0 16px; }

.group-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 18px 4px 8px;
  font-size: .85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}
.group-title .dot { width: 10px; height: 10px; border-radius: 50%; }

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 8px;
  cursor: pointer;
}
.item .check {
  flex: 0 0 26px;
  width: 26px;
  height: 26px;
  border: 2px solid var(--muted);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: transparent;
  transition: all .15s ease;
}
.item.bought .check {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.item .body { flex: 1; min-width: 0; }
.item .name { display: block; overflow: hidden; text-overflow: ellipsis; }
.item.bought .name { text-decoration: line-through; color: var(--muted); }
.item .meta { display: block; font-size: .78rem; color: var(--muted); margin-top: 2px; }
.item .qty {
  flex: 0 0 auto;
  font-size: .82rem;
  color: var(--muted);
  background: var(--bg);
  border-radius: 8px;
  padding: 3px 8px;
}

.bought-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 4px 8px;
}
.bought-header button {
  background: none;
  border: none;
  color: var(--danger);
  font-size: .85rem;
  cursor: pointer;
}

.empty {
  text-align: center;
  color: var(--muted);
  margin: 60px 20px;
  line-height: 1.6;
}
.empty .big { font-size: 3rem; display: block; margin-bottom: 8px; }

/* ----- Barra de añadir ----- */
.addbar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 640px;
  display: flex;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--border);
}
.addbar input, .addbar select {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: .95rem;
  min-width: 0;
}
#addName { flex: 1; }
#addQty { flex: 0 0 64px; }
#addSuper { flex: 0 0 110px; }
.btn-add {
  flex: 0 0 48px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1.5rem;
  border-radius: 10px;
  cursor: pointer;
  line-height: 1;
}

/* ----- Aviso sin conexión ----- */
.offline {
  position: fixed;
  bottom: calc(92px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  font-size: .85rem;
  padding: 8px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

/* ----- Modales ----- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 15, 20, .55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
@media (min-width: 480px) {
  .overlay { align-items: center; }
}
.modal {
  background: var(--card);
  width: 100%;
  max-width: 440px;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
}
@media (min-width: 480px) {
  .modal { border-radius: 18px; }
}
.modal h2 { margin: 0 0 10px; font-size: 1.15rem; }
.modal h3 { margin: 18px 0 8px; font-size: .95rem; }
.modal section { border-top: 1px solid var(--border); }
.modal section:first-of-type { border-top: none; }
.modal label { display: block; margin: 10px 0; font-size: .88rem; color: var(--muted); }
.modal input[type="text"], .modal select {
  display: block;
  width: 100%;
  margin-top: 4px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: .95rem;
}
.modal .row { display: flex; gap: 8px; margin: 8px 0; }
.modal .row input { flex: 1; margin-top: 0; }
.modal-actions { display: flex; gap: 8px; margin-top: 18px; align-items: center; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: .85rem; }

.btn {
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: .92rem;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: none; border-color: var(--danger); color: var(--danger); }

/* ----- Gestión de supermercados ----- */
.super-list { list-style: none; margin: 0 0 10px; padding: 0; }
.super-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
}
.super-list .dot { width: 14px; height: 14px; border-radius: 50%; flex: 0 0 auto; }
.super-list .nm { flex: 1; }
.super-list button { background: none; border: none; font-size: 1rem; cursor: pointer; padding: 4px; }

.colors { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.color-swatch {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  padding: 0;
}
.color-swatch.selected { border-color: var(--text); }

/* ----- Pantalla de acceso ----- */
.modal.auth { max-width: 380px; }
.auth-logo {
  font-size: 3rem;
  text-align: center;
  background: var(--accent-soft);
  width: 84px;
  height: 84px;
  line-height: 84px;
  border-radius: 24px;
  margin: 4px auto 10px;
}
.auth-title { text-align: center; margin: 0; }
.center { text-align: center; }
.full { width: 100%; margin-top: 12px; }
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: .9rem;
  cursor: pointer;
  padding: 6px;
  text-decoration: underline;
}
.form-error {
  background: rgba(220, 38, 38, .12);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 14px;
  margin: 10px 0;
  font-size: .88rem;
}
.modal label small { color: var(--muted); font-weight: 400; }

.hidden { display: none !important; }
