:root {
  --bg: #fbf6ef;
  --card: #ffffff;
  --ink: #2b2420;
  --muted: #8a7f76;
  --line: #ece2d6;
  --accent: #c65d2e;      /* terracota */
  --accent-2: #1f7a6d;    /* mar */
  --accent-soft: #f6e3d6;
  --green: #2e9e5b;
  --green-bg: #e3f4e8;
  --red: #d24b45;
  --red-bg: #fbe4e2;
  --yellow: #d9a520;
  --yellow-bg: #fbf1d3;
  --gray: #9a9089;
  --gray-bg: #efeae3;
  --radius: 16px;
  --shadow: 0 2px 10px rgba(60, 40, 20, .08);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; width: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2 { margin: 0; }

/* ---------- Topbar ---------- */
.sticky-top { position: sticky; top: 0; z-index: 20; }
.topbar {
  background: linear-gradient(135deg, var(--accent), #a9481f);
  color: #fff;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  box-shadow: var(--shadow);
}
.topbar-inner { display: flex; align-items: center; gap: 10px; justify-content: space-between; max-width: 900px; margin: 0 auto; }
.brand { font-size: 1.25rem; font-weight: 800; letter-spacing: .3px; }
.brand span { font-weight: 400; opacity: .85; }
.me-picker { display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,.16); padding: 6px 10px; border-radius: 999px; }
.me-label { font-size: .8rem; opacity: .9; }
.me-picker select { background: #fff; color: var(--ink); border: none; border-radius: 999px; padding: 5px 10px; font-weight: 600; max-width: 42vw; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex; gap: 4px; padding: 8px; background: var(--bg);
  border-bottom: 1px solid var(--line); overflow-x: auto;
  max-width: 900px; margin: 0 auto;
}
.tab {
  flex: 1 0 auto; border: none; background: transparent; color: var(--muted);
  padding: 8px 12px; border-radius: 999px; font-weight: 700; font-size: .92rem;
  white-space: nowrap; cursor: pointer;
}
.tab.is-active { background: var(--accent-soft); color: var(--accent); }

/* ---------- Views ---------- */
main { max-width: 900px; margin: 0 auto; padding: 12px 12px calc(90px + var(--safe-b)); }
.view { display: none; }
.view.is-active { display: block; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.hint { color: var(--muted); font-size: .9rem; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }

/* ---------- Toolbar / filtres ---------- */
.toolbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.search { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--card); }
.search:focus { outline: 2px solid var(--accent); }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--muted);
  padding: 6px 12px; border-radius: 999px; font-size: .85rem; font-weight: 600; cursor: pointer;
}
.chip.is-active { background: var(--accent); color: #fff; border-color: var(--accent); }
.mine-toggle { font-size: .9rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.mine-toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

/* Filtres + ordenació */
.filter-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.fsel {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--ink); font-size: .85rem; font-weight: 600;
}
.fsel:focus { outline: 2px solid var(--accent); }
.toggles { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; }
.tg { font-size: .88rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.tg input { width: 18px; height: 18px; accent-color: var(--accent); }
.link-btn { margin-left: auto; background: none; border: none; color: var(--accent); font-weight: 700; font-size: .85rem; cursor: pointer; text-decoration: underline; padding: 4px; }
.results-count { font-size: .8rem; color: var(--muted); }

/* ---------- Cards ---------- */
.cards { display: grid; gap: 12px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); position: relative;
}
.card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card-title { font-size: 1.08rem; font-weight: 800; margin: 0 0 2px; overflow-wrap: anywhere; }
.card-desc { color: var(--muted); font-size: .92rem; margin: 6px 0 10px; overflow-wrap: anywhere; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.badge { font-size: .74rem; font-weight: 700; padding: 3px 9px; border-radius: 999px; background: var(--gray-bg); color: #6b625b; white-space: nowrap; }
.badge.tipo-Ciutat { background: #e7eef9; color: #345; }
.badge.tipo-Excursio { background: #e4f3ec; color: #1f6b4e; }
.badge.tipo-Menjar { background: #fbe9dc; color: #a5572a; }
.badge.tipo-Festa { background: #f3e4f5; color: #7d3d86; }
.badge.tipo-Altres { background: var(--gray-bg); color: #6b625b; }
.badge.badge-date { background: var(--accent-soft); color: var(--accent); }
.badge.badge-ticket { background: #fdecc8; color: #9a6b00; }

.card-links { display: flex; gap: 12px; flex-wrap: wrap; margin: 6px 0; }
.card-links { flex-wrap: wrap; }
.map-link { color: var(--accent-2); font-weight: 700; font-size: .9rem; text-decoration: none; overflow-wrap: anywhere; }
.map-link::before { content: "📍 "; }
.comment { font-size: .85rem; color: var(--muted); font-style: italic; overflow-wrap: anywhere; }
.comment a { color: var(--accent-2); font-weight: 700; }
.ticket-link { color: var(--accent-2); font-weight: 700; text-decoration: none; }
.ticket-link::before { content: "🎟 "; }

.going { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  font-size: .72rem; font-weight: 800; color: #fff; background: var(--accent-2);
  border: 2px solid var(--card); margin-left: -8px;
}
.avatar:first-child { margin-left: 0; }
.going-count { font-size: .85rem; color: var(--muted); margin-left: 4px; }
.going-none { font-size: .85rem; color: var(--muted); }
.going-label { font-size: .82rem; color: var(--muted); font-weight: 700; margin-right: 2px; width: 100%; }
.pchip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--gray-bg); color: var(--ink);
  font-size: .82rem; font-weight: 600; padding: 4px 10px 4px 7px; border-radius: 999px;
}
.pdot { width: 12px; height: 12px; border-radius: 50%; flex: none; }

.card-actions { display: flex; gap: 8px; margin-top: 12px; }
.join-btn {
  flex: 1; border: 2px solid var(--accent); background: transparent; color: var(--accent);
  padding: 10px; border-radius: 12px; font-weight: 800; cursor: pointer;
}
.join-btn.joined { background: var(--green); border-color: var(--green); color: #fff; }
.join-btn:disabled { opacity: .5; cursor: default; }
.edit-btn { border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 10px 14px; cursor: pointer; }

/* ---------- Gent ---------- */
.people { display: grid; gap: 10px; margin: 12px 0; }
.person {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px;
  display: grid; grid-template-columns: 1fr auto; gap: 8px 10px; align-items: center; box-shadow: var(--shadow);
}
.person.is-me { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.person-name { font-weight: 800; display: flex; align-items: center; gap: 8px; }
.tag-me { font-size: .68rem; background: var(--accent); color: #fff; padding: 2px 8px; border-radius: 999px; }
.person-del { border: none; background: transparent; color: var(--muted); font-size: 1.1rem; cursor: pointer; }
.person-dates { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.person-dates label { font-size: .74rem; color: var(--muted); display: grid; gap: 3px; }
.person-dates input {
  padding: 9px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.person-dates input.missing { background: var(--yellow-bg); border-color: var(--yellow); }
.add-person { display: flex; gap: 8px; margin-top: 6px; }
.add-person input { flex: 1; padding: 11px 14px; border: 1px solid var(--line); border-radius: 999px; background: var(--card); }
.add-person button { border: none; background: var(--accent); color: #fff; font-weight: 800; padding: 0 18px; border-radius: 999px; cursor: pointer; }

/* ---------- Disponibilitat ---------- */
.legend { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.lg { font-size: .76rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; }
.lg-green { background: var(--green-bg); color: var(--green); }
.lg-red { background: var(--red-bg); color: var(--red); }
.lg-yellow { background: var(--yellow-bg); color: #9a7100; }
.lg-gray { background: var(--gray-bg); color: #6b625b; }

.disp-card { }
.disp-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.disp-date { font-size: .82rem; font-weight: 700; color: var(--accent); }
.disp-date.none { color: var(--muted); font-weight: 600; }
.disp-counts { display: flex; gap: 8px; margin: 8px 0; }
.count-pill { font-size: .78rem; font-weight: 800; padding: 3px 10px; border-radius: 999px; }
.count-pill.go { background: var(--green-bg); color: var(--green); }
.count-pill.no { background: var(--red-bg); color: var(--red); }
.disp-group { margin-top: 8px; }
.disp-group h4 { margin: 0 0 5px; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.name-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.name-chip { font-size: .8rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; }
.name-chip.go { background: var(--green-bg); color: var(--green); }
.name-chip.no { background: var(--red-bg); color: var(--red); }
.name-chip.free { background: var(--yellow-bg); color: #9a7100; }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-b)); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; border: none; background: var(--accent); color: #fff;
  font-size: 2rem; line-height: 1; box-shadow: 0 6px 18px rgba(198,93,46,.45); cursor: pointer;
}

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(30,20,10,.5); display: grid; align-items: end; }
.modal-backdrop[hidden] { display: none; }
.modal { background: var(--card); width: 100%; max-width: 560px; margin: 0 auto; border-radius: 20px 20px 0 0; max-height: 92vh; display: flex; flex-direction: column; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px; overflow-y: auto; display: grid; gap: 12px; }
.modal-body label { display: grid; gap: 5px; font-size: .82rem; font-weight: 700; color: var(--muted); }
.modal-body input, .modal-body textarea, .modal-body select {
  padding: 11px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-weight: 400; color: var(--ink);
}
.modal-body .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-body label.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.modal-body label.checkbox input { width: 20px; height: 20px; accent-color: var(--accent); }
.icon-btn { border: none; background: transparent; font-size: 1.2rem; cursor: pointer; color: var(--muted); }
.modal-actions { display: flex; align-items: center; gap: 8px; padding-top: 6px; }
.modal-actions .spacer { flex: 1; }
.btn-primary { background: var(--accent); color: #fff; border: none; padding: 11px 18px; border-radius: 12px; font-weight: 800; cursor: pointer; }
.btn-ghost { background: transparent; border: 1px solid var(--line); padding: 11px 16px; border-radius: 12px; cursor: pointer; }
.btn-danger { background: var(--red-bg); color: var(--red); border: none; padding: 11px 16px; border-radius: 12px; font-weight: 700; cursor: pointer; }

@media (min-width: 620px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 20px; }
  .cards { grid-template-columns: 1fr 1fr; }
  .toolbar { flex-wrap: wrap; align-items: center; }
  .search { max-width: 280px; }
  .filters { flex: 1; }
  .filter-grid { grid-template-columns: repeat(4, 1fr); flex: 1 1 100%; }
}

/* ---------- Mapa ---------- */
.map-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; }
.map-toolbar .fsel { flex: 1; min-width: 120px; }
#map {
  width: 100%; height: 65vh; min-height: 360px;
  border-radius: 16px; border: 1px solid var(--line); box-shadow: var(--shadow);
  z-index: 0;
}
.map-note { font-size: .82rem; color: var(--muted); margin: 10px 2px; }
.map-pin {
  width: 20px; height: 20px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  border: 2px solid #fff; box-shadow: 0 2px 4px rgba(0,0,0,.3);
}
.leaflet-popup-content { margin: 12px 14px; font: inherit; }
.map-pop-title { font-weight: 800; font-size: 1rem; margin-bottom: 4px; }
.map-pop-meta { color: var(--muted); font-size: .82rem; margin-bottom: 6px; }
.map-pop-people { font-size: .82rem; margin-bottom: 6px; }
.map-pop a { color: var(--accent-2); font-weight: 700; text-decoration: none; }

/* ---------- Pantalla d'identificació ---------- */
.identify-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: linear-gradient(160deg, var(--accent), #7c3416);
  display: grid; place-items: center; padding: 20px;
}
.identify-backdrop[hidden] { display: none; }
.identify-card {
  background: var(--card); border-radius: 22px; padding: 30px 24px;
  max-width: 400px; width: 100%; text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .35);
}
.identify-emoji { font-size: 3.2rem; line-height: 1; }
.identify-card h2 { font-size: 1.5rem; font-weight: 800; margin: 10px 0 4px; color: var(--accent); }
.identify-card > p { color: var(--muted); font-size: .95rem; margin: 4px 0 18px; }
.identify-card .fsel { width: 100%; padding: 13px; font-size: 1rem; margin-bottom: 14px; }
.identify-card .btn-primary { width: 100%; padding: 14px; font-size: 1.05rem; border-radius: 12px; }
.identify-card .btn-primary:disabled { opacity: .45; cursor: default; }
.identify-hint { font-size: .82rem; color: var(--muted); margin: 16px 0 10px; }
.identify-card .link-btn { margin: 0 auto; display: inline-block; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%);
  background: #2b2420; color: #fff; padding: 10px 16px; border-radius: 999px; z-index: 60;
  font-size: .88rem; box-shadow: var(--shadow); max-width: 90vw;
}
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
