:root {
  --bg: #f7f5f0;
  --panel: #ffffff;
  --text: #1c2024;
  --muted: #6b7280;
  --border: #e5e1d8;
  --accent: #0f766e;
  --accent-soft: #d7eee9;
  --shadow: 0 6px 24px rgba(20, 30, 40, 0.12);
  --sidebar-w: 350px;
}

[data-theme="dark"] {
  --bg: #0f1417;
  --panel: #161c21;
  --text: #e7eaed;
  --muted: #93a1ad;
  --border: #232b32;
  --accent: #2dd4bf;
  --accent-soft: #14302d;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent; /* no grey flash on tap */
}

html, body {
  margin: 0;
  height: 100%;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none; /* kill pull-to-refresh / scroll chaining */
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Sidebar ---------- */
#sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
  min-height: 0;
}

.sidebar-head {
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.title-row { display: flex; align-items: center; justify-content: space-between; }
.sidebar-head h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.02em; }
.subtitle { margin: 2px 0 10px; color: var(--muted); font-size: 0.85rem; }

.user-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}
.user-row select {
  flex: 1;
  font: inherit;
  font-size: 0.85rem;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.icon-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 8px; cursor: pointer; font-size: 1rem;
}
.icon-btn:hover { background: var(--accent-soft); }

.toolbar {
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  border-bottom: 1px solid var(--border);
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-primary[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--accent-soft); }
.add-icon { font-weight: 700; }

.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  --chip: var(--muted);
  border: 1.5px solid var(--chip);
  background: transparent;
  color: var(--chip);
  border-radius: 999px;
  padding: 4px 11px;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  text-transform: capitalize;
  transition: background 0.15s, color 0.15s;
}
.chip[aria-pressed="true"] { background: var(--chip); color: #fff; border-color: transparent; }

.hint {
  margin: 0;
  padding: 10px 18px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 500;
}

/* ---------- Places list ---------- */
.places-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  overscroll-behavior: contain; /* sheet scroll doesn't leak to the map/page */
  -webkit-overflow-scrolling: touch;
}
.place-item {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  gap: 10px;
  align-items: start;
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
}
.place-item:hover { background: var(--accent-soft); }
.place-dot { width: 11px; height: 11px; border-radius: 50%; margin-top: 4px; }
.place-body h3 { margin: 0; font-size: 0.92rem; font-weight: 600; }
.place-body .meta { margin: 2px 0 0; color: var(--muted); font-size: 0.76rem; text-transform: capitalize; }
.place-body .note { margin: 4px 0 0; color: var(--muted); font-size: 0.8rem; line-height: 1.35; }
.place-actions { display: flex; gap: 4px; opacity: 0; }
.place-item:hover .place-actions { opacity: 1; }
.mini-btn {
  border: none; background: transparent; color: var(--muted);
  cursor: pointer; font-size: 0.85rem; padding: 2px 4px; border-radius: 6px;
}
.mini-btn:hover { background: var(--border); color: var(--text); }

.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; border-top: 1px solid var(--border);
  font-size: 0.78rem; color: var(--muted);
}

/* ---------- Map ---------- */
#map { width: 100%; height: 100%; }
#map.adding { cursor: crosshair; }

.pin {
  width: 27px; height: 27px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.35);
  cursor: pointer;
  display: grid; place-items: center;
}
.pin-emoji {
  transform: rotate(45deg);
  font-size: 13px;
  line-height: 1;
  filter: drop-shadow(0 0 1px rgba(255,255,255,0.6));
}

/* ---------- Popup ---------- */
.maplibregl-popup-content {
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  font-family: inherit;
}
.map-popup h3 { margin: 0 0 5px; font-size: 0.98rem; }
.map-popup .cat {
  display: inline-block; font-size: 0.66rem; text-transform: uppercase;
  letter-spacing: 0.04em; color: #fff; border-radius: 5px; padding: 1px 7px; margin-bottom: 6px;
}
.map-popup > p { margin: 4px 0 0; font-size: 0.84rem; color: #444; line-height: 1.4; }

.popup-links { display: flex; flex-wrap: wrap; gap: 10px; margin: 8px 0 2px; }
.popup-links a { font-size: 0.8rem; color: var(--accent); text-decoration: none; font-weight: 500; }
.popup-links a:hover { text-decoration: underline; }

.comments {
  margin: 8px 0 4px;
  display: flex; flex-direction: column; gap: 5px;
  max-height: 150px; overflow-y: auto;
}
.comment {
  display: flex; gap: 6px; align-items: baseline;
  font-size: 0.82rem; line-height: 1.35;
  background: rgba(127,127,127,0.08);
  border-radius: 8px; padding: 4px 8px;
}
.comment-author { font-weight: 600; color: var(--accent); flex-shrink: 0; }
.comment-text { color: #333; flex: 1; }
[data-theme="dark"] .comment-text { color: #cdd3d8; }
[data-theme="dark"] .map-popup > p { color: #cdd3d8; }
.cmt-del { border: none; background: none; cursor: pointer; color: #aaa; font-size: 1rem; line-height: 1; padding: 0 2px; }
.cmt-del:hover { color: #dc2626; }

.add-comment { display: flex; gap: 6px; margin-top: 6px; }
.add-comment input {
  flex: 1; font: inherit; font-size: 0.82rem;
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
  background: var(--bg); color: var(--text); min-width: 0;
}
.add-comment .btn { padding: 6px 12px; font-size: 0.82rem; }

/* ---------- Editor dialog ---------- */
#editor {
  border: none; border-radius: 14px; padding: 0;
  box-shadow: var(--shadow); background: var(--panel); color: var(--text);
  width: min(92vw, 400px);
}
#editor::backdrop { background: rgba(10, 15, 20, 0.45); }
#editor-form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
#editor-form h2 { margin: 0; font-size: 1.15rem; }
#editor-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.8rem; color: var(--muted); }
#editor-form input, #editor-form select, #editor-form textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; resize: vertical;
}
#editor-form .coords { margin: 0; font-size: 0.72rem; color: var(--muted); }
.hint-inline { font-weight: 400; opacity: 0.7; }
#editor-form menu { display: flex; justify-content: flex-end; gap: 8px; margin: 4px 0 0; padding: 0; }

/* ---------- Mobile sheet handle (hidden on desktop) ---------- */
.sheet-toggle { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr; position: relative; }

  #map { position: absolute; inset: 0; height: 100%; }

  /* Sidebar becomes a draggable-feeling bottom sheet */
  #sidebar {
    position: absolute;
    left: 0; right: 0; bottom: 0;
    height: 50vh;
    border-right: none;
    border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    box-shadow: var(--shadow);
    transition: height 0.25s ease;
    z-index: 5;
    padding-top: 14px;
    /* respect the home-indicator / notch when viewport-fit=cover */
    padding-bottom: env(safe-area-inset-bottom);
  }
  #sidebar.expanded { height: 88dvh; }

  /* Compact the header + toolbar so the list keeps real estate */
  .sidebar-head { padding: 6px 16px 8px; }
  .subtitle { margin-bottom: 6px; }
  .toolbar { padding: 10px 14px; gap: 8px; }

  /* Filters scroll horizontally in ONE row instead of wrapping (was eating
     vertical space and squeezing the list). */
  .filters {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    scrollbar-width: none;
    padding-bottom: 2px;
    margin: 0 -2px;
  }
  .filters::-webkit-scrollbar { display: none; }
  .chip { flex: 0 0 auto; }

  /* The list is the part that should grow + scroll */
  .places-list { flex: 1 1 auto; min-height: 0; }

  /* keep map controls clear of the notch */
  .maplibregl-ctrl-top-right { margin-right: env(safe-area-inset-right); }

  .sheet-toggle {
    display: block;
    position: absolute;
    top: 6px; left: 50%;
    transform: translateX(-50%);
    width: 44px; height: 5px;
    border: none; border-radius: 3px;
    background: var(--border);
    z-index: 6;
    cursor: pointer;
  }

  .sidebar-head h1 { font-size: 1.3rem; }

  /* Touch targets: always show actions on mobile */
  .place-actions { opacity: 1; }
  .mini-btn { font-size: 1rem; padding: 4px 6px; }

  .maplibregl-ctrl-top-right { margin-top: 6px; }
}

@media (max-width: 760px) and (orientation: landscape) {
  #sidebar { height: 60vh; }
}
