:root {
  --sauce: #b93a1c;
  --sauce-dark: #8f2a12;
  --crust: #d98a2e;
  --cream: #f6e7bf;
  --paper: #fbf5e6;
  --basil: #5e7a2e;
  --ink: #2b2b22;
  --muted: #7a6f5a;
  --line: #e6d8b4;
  --card: #fffdf6;
  --shadow: 0 8px 28px rgba(60, 35, 10, .18);
  --radius: 16px;
  --top: 64px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1f1c16; --card: #2a2620; --ink: #f3ead3; --muted: #b3a688; --line: #403929; --cream: #3a3322;
    --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'DM Sans', system-ui, sans-serif; color: var(--ink); background: var(--paper);
  -webkit-font-smoothing: antialiased; overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--sauce); }
:focus-visible { outline: 3px solid var(--crust); outline-offset: 2px; }
[hidden] { display: none !important; }

.app {
  position: relative;
  height: 100dvh; display: grid;
  grid-template-columns: 300px 1fr; grid-template-rows: var(--top) 1fr;
  grid-template-areas: "top top" "filters stage";
}

/* ---------- Header ---------- */
.top {
  grid-area: top; display: flex; align-items: center; gap: 20px; padding: 0 20px;
  background: var(--sauce); color: #fff7e2; position: relative; z-index: 1200;
  border-bottom: 4px solid var(--crust);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit;
  font-family: 'Fraunces', serif; font-size: 24px; font-weight: 600; letter-spacing: -.01em; white-space: nowrap; }
.brand img { border-radius: 50%; background: var(--paper); box-shadow: 0 0 0 2px var(--crust); }
.brand b { font-weight: 800; }
.stats { margin-left: auto; display: flex; gap: 18px; font-size: 14px; }
.stats span b { font-family: 'Fraunces', serif; font-size: 20px; margin-right: 4px; color: #fff; }
.btn.filter-toggle { display: none; margin-left: auto; }

.btn { border: 0; border-radius: 999px; padding: 10px 18px; font-weight: 700; background: var(--cream); color: var(--ink);
  display: inline-flex; align-items: center; gap: 8px; transition: transform .1s, background .15s; }
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--sauce); color: #fff; }
.btn.primary:hover { background: var(--sauce-dark); }
.btn.ghost { background: transparent; border: 2px solid var(--line); }
.pill { background: var(--crust); color: #fff; border-radius: 999px; font-size: 12px; padding: 1px 7px; }

/* ---------- Filters ---------- */
.filters {
  grid-area: filters; padding: 18px; overflow-y: auto; background: var(--paper);
  border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 16px;
}
.search { position: relative; display: block; }
.search svg { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search input, .field select {
  width: 100%; padding: 12px 14px; border: 2px solid var(--line); border-radius: 12px; background: var(--card); min-height: 46px;
}
.search input { padding-left: 42px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.field select { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--ink); }
.seg { display: flex; background: var(--cream); border-radius: 999px; padding: 4px; gap: 2px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px 12px; border-radius: 999px; font-weight: 700; font-size: 14px; color: var(--muted); }
.seg button[aria-checked="true"] { background: var(--sauce); color: #fff; box-shadow: 0 2px 6px rgba(143, 42, 18, .35); }
.checks { display: flex; flex-direction: column; gap: 10px; }
.checks label { display: flex; align-items: center; gap: 10px; font-weight: 500; min-height: 32px; cursor: pointer; }
.checks input { width: 20px; height: 20px; accent-color: var(--sauce); }
.filter-actions { display: flex; gap: 10px; margin-top: auto; }
.filter-actions .primary { display: none; flex: 1; justify-content: center; }
.filter-actions .ghost { flex: 1; justify-content: center; }

/* ---------- Stage / map ---------- */
.stage { grid-area: stage; position: relative; min-width: 0; overflow: hidden; }
#map { position: absolute; inset: 0; z-index: 0; background: #e8dfc8; }
.leaflet-container { font-family: inherit; }
.view-switch { position: absolute; z-index: 950; top: 14px; left: 50%; transform: translateX(-50%); width: 270px; box-shadow: var(--shadow); background: var(--card); }
.loading { position: absolute; inset: 0; z-index: 900; background: var(--paper); display: grid; place-content: center; justify-items: center; gap: 8px; color: var(--muted); transition: opacity .4s; }
.loading img { animation: spin 2.4s linear infinite; border-radius: 50%; }
.loading.done { opacity: 0; pointer-events: none; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loading img { animation: none; } * { scroll-behavior: auto !important; } }

/* markers */
.pz { display: block; border-radius: 50%; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .45)); transition: transform .12s; }
.pz.todo { opacity: .8; filter: saturate(.5) drop-shadow(0 1px 2px rgba(0, 0, 0, .35)); }
.pz.fav { box-shadow: 0 0 0 3px #f2c230; }
.pz.closed { filter: grayscale(1) drop-shadow(0 1px 2px rgba(0, 0, 0, .35)); opacity: .55; }
.pz.active { transform: scale(1.35); }
.cl { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff;
  background: var(--crust); border: 4px solid rgba(255, 247, 226, .85); box-shadow: 0 2px 8px rgba(0, 0, 0, .35); font-size: 14px; }
.cl.has-visited { background: var(--sauce); border-color: var(--crust); }
.cl.big { width: 54px; height: 54px; font-size: 16px; }
.leaflet-tooltip { font-weight: 700; border-radius: 8px; border: 0; box-shadow: var(--shadow); background: var(--card); color: var(--ink); }
.leaflet-tooltip-top:before { border-top-color: var(--card); }

/* ---------- Gallery ---------- */
.gallery, .stats-page { position: absolute; inset: 0; z-index: 850; background: var(--paper); overflow-y: auto; padding: 70px 20px 24px; }
.g-head { font-weight: 700; color: var(--muted); margin: 0 0 12px; }
.g-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; align-content: start; }
.g-more { height: 60px; display: grid; place-items: center; color: var(--muted); font-size: 14px; }
.tile { position: relative; border: 0; padding: 0; border-radius: 14px; overflow: hidden; aspect-ratio: 1; background: var(--cream); }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .3s; }
.tile:hover img { transform: scale(1.06); }
.tile span { position: absolute; inset: auto 0 0 0; padding: 22px 10px 8px; text-align: left; color: #fff; font-size: 13px; font-weight: 700;
  background: linear-gradient(transparent, rgba(0, 0, 0, .75)); }
.tile small { display: block; font-weight: 400; opacity: .85; }
.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- Detail panel ---------- */
.detail { position: absolute; z-index: 1100; top: var(--top); right: 0; bottom: 0; width: 400px; max-width: 100%;
  background: var(--paper); box-shadow: var(--shadow); overflow-y: auto; padding: 0 0 24px;
  animation: slide .25s ease-out; border-left: 1px solid var(--line); }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } }
.d-head { padding: 18px 20px 6px; position: relative; }
.d-close { position: absolute; top: 14px; right: 14px; width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--cream); font-size: 20px; line-height: 1; }
.d-head h2 { font-family: 'Fraunces', serif; font-size: 26px; line-height: 1.1; margin: 0 44px 6px 0; }
.d-sub { color: var(--muted); font-size: 14px; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.badge { font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--cream); }
.badge.eaten { background: var(--basil); color: #fff; }
.badge.todo { background: var(--cream); color: var(--crust); border: 1px solid var(--crust); }
.badge.fav { background: #f6d968; color: #5b4300; }
.badge.closed { background: #ddd; color: #555; }

.links { display: flex; gap: 10px; padding: 14px 20px; flex-wrap: wrap; }
.links a { flex: 1 1 140px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 12px 14px; min-height: 46px;
  border-radius: 12px; font-weight: 700; text-decoration: none; font-size: 14px; border: 2px solid var(--line); background: var(--card); color: var(--ink); transition: border-color .15s, transform .1s; }
.links a:hover { border-color: var(--sauce); }
.links a:active { transform: scale(.97); }
.links a.ig svg { color: #d62976; }
.links a.gm svg { color: var(--basil); }

/* Instagram post frame */
.ig-frame { margin: 4px 20px 0; background: #fff; color: #262626; border: 1px solid #dbdbdb; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 14px rgba(0, 0, 0, .1); font-family: -apple-system, 'Segoe UI', Roboto, sans-serif; }
.ig-top { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.ig-av { width: 34px; height: 34px; border-radius: 50%; padding: 2px; background: linear-gradient(45deg, #f9ce34, #ee2a7b, #6228d7); flex: none; }
.ig-av img { width: 100%; height: 100%; border-radius: 50%; border: 2px solid #fff; background: #fff; display: block; }
.ig-who { flex: 1; min-width: 0; line-height: 1.2; }
.ig-who b { font-size: 14px; display: block; }
.ig-who small { color: #737373; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.ig-dots { font-weight: 700; letter-spacing: 1px; color: #262626; }
.ig-img { position: relative; aspect-ratio: 1; background: #efefef; }
.ig-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ig-img iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }
.ig-actions { display: flex; align-items: center; gap: 14px; padding: 10px 12px 4px; }
.ig-actions svg { display: block; }
.ig-actions .sp { margin-left: auto; }
.ig-cap { padding: 2px 12px 4px; font-size: 14px; line-height: 1.4; }
.ig-cap b { margin-right: 4px; }
.ig-date { padding: 0 12px 10px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: #737373; }
.ig-foot { display: flex; border-top: 1px solid #efefef; }
.ig-foot a, .ig-foot button { flex: 1; text-align: center; padding: 12px; font-size: 14px; font-weight: 600; color: #0095f6; text-decoration: none; background: none; border: 0; min-height: 44px; }
.ig-foot button { border-left: 1px solid #efefef; }
.ig-foot > :first-child:only-child { border: 0; }
.ig-foot a:hover, .ig-foot button:hover { background: #fafafa; }

.todo-card { margin: 4px 20px 0; padding: 22px; border: 2px dashed var(--line); border-radius: 14px; text-align: center; color: var(--muted); }
.todo-card img { width: 64px; opacity: .55; filter: grayscale(.4); border-radius: 50%; }
.todo-card p { margin: 8px 0 0; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  :root { --top: 56px; }
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "stage"; }
  .brand { font-size: 20px; }
  .brand img { width: 34px; height: 34px; }
  .stats { display: none; }
  .btn.filter-toggle { display: inline-flex; }
  .filters {
    position: fixed; z-index: 1300; top: var(--top); left: 0; right: 0; max-height: calc(100dvh - var(--top)); border-right: 0;
    border-bottom: 4px solid var(--crust); box-shadow: var(--shadow);
    transform: translateY(-110%); visibility: hidden; transition: transform .28s ease, visibility .28s; padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
  .filters.open { transform: none; visibility: visible; }
  .filter-actions .primary { display: inline-flex; }
  .detail {
    top: auto; left: 0; right: 0; bottom: 0; width: 100%; max-height: 78dvh; border-radius: 22px 22px 0 0; border-left: 0;
    padding-bottom: calc(24px + env(safe-area-inset-bottom)); animation: sheet .28s ease-out; z-index: 1250;
  }
  .detail::before { content: ""; display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; position: sticky; top: 8px; }
  @keyframes sheet { from { transform: translateY(40%); opacity: 0; } }
  .gallery, .stats-page { padding: 66px 12px 24px; }
  .g-grid { grid-template-columns: repeat(2, 1fr); }
  .view-switch { width: min(270px, calc(100% - 24px)); }
  .view-switch { top: 10px; }
  .leaflet-control-zoom { display: none; }
}
@media (min-width: 861px) {
  .stage.has-detail .view-switch { left: calc(50% - 200px); }
  .detail { top: var(--top); }
}
.base-tiles { filter: sepia(.22) saturate(.85) brightness(1.02); }

/* multi-select */
.ms-btn { width: 100%; text-align: left; padding: 12px 14px; min-height: 46px; border: 2px solid var(--line); border-radius: 12px; background: var(--card); font-weight: 500; text-transform: none; letter-spacing: 0; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.ms-btn::after { content: "▾"; color: var(--muted); }
.ms-panel { margin-top: 6px; border: 2px solid var(--line); border-radius: 12px; background: var(--card); padding: 8px; text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--ink); }
.ms-panel input[type=search] { width: 100%; padding: 9px 12px; border: 2px solid var(--line); border-radius: 10px; background: var(--paper); margin-bottom: 6px; }
.ms-list { max-height: 220px; overflow-y: auto; }
.ms-list label { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.ms-list label:hover { background: var(--cream); }
.ms-list input { width: 18px; height: 18px; accent-color: var(--sauce); flex: none; }
.ms-list em { margin-left: auto; font-style: normal; color: var(--muted); font-size: 12px; }
.ms-foot { display: flex; justify-content: space-between; padding: 6px 4px 0; }
.ms-foot button { border: 0; background: none; color: var(--sauce); font-weight: 700; padding: 6px; }

/* stats page */
.sp-inner { max-width: 1100px; margin: 0 auto; }
.sp-inner h2 { font-family: 'Fraunces', serif; font-size: 30px; margin: 4px 0 16px; }
.sp-filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 16px; align-items: start; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; text-transform: none; letter-spacing: 0; }
.chip { border: 2px solid var(--line); background: var(--paper); border-radius: 999px; padding: 6px 12px; font-weight: 700; font-size: 13px; color: var(--ink); }
.chip[aria-pressed="true"] { background: var(--sauce); border-color: var(--sauce); color: #fff; }
.range { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.range .field { flex: 1 1 130px; }
.range input { padding: 10px; border: 2px solid var(--line); border-radius: 12px; background: var(--paper); min-height: 46px; text-transform: none; font-weight: 500; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin: 16px 0; }
.kpi { background: var(--sauce); color: #fff7e2; border-radius: var(--radius); padding: 14px 16px; }
.kpi b { display: block; font-family: 'Fraunces', serif; font-size: 32px; line-height: 1.1; }
.kpi span { font-size: 13px; opacity: .85; }
.kpi:nth-child(even) { background: var(--basil); }
.cards { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card h3 { margin: 0 0 10px; font-family: 'Fraunces', serif; font-size: 18px; }
.chart-box { position: relative; height: 340px; }
.note { color: var(--muted); font-size: 13px; }
@media (max-width: 860px) { .cards { grid-template-columns: 1fr; } .chart-box { height: 280px; } .chart-box.pie { height: 340px; } }
