:root {
  /* Warm & playful palette: terracotta/sunset accents over a cream base,
     instead of a flat corporate green. */
  --color-bg: #fbf4ec;
  --color-surface: #ffffff;
  --color-border: #ecdfce;
  --color-text: #2e2620;
  --color-text-muted: #8a7c6c;

  --color-primary: #d9702e;      /* sunset terracotta */
  --color-primary-hover: #bb5c20;
  --color-visited: #3f8b62;      /* evergreen — visited parks pop against unvisited sand */
  --color-visited-glow: rgba(63, 139, 98, 0.28);
  --color-unvisited: #e3b98a;    /* warm sand */
  --color-accent: #e0567c;       /* berry pink for small celebratory accents */
  --color-error: #c1432b;

  --shadow-sm: 0 1px 3px rgba(90, 62, 30, 0.12);
  --shadow-md: 0 10px 30px rgba(90, 62, 30, 0.22);
  --radius: 14px;
  --radius-sm: 10px;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  display: flex;
  flex-direction: column;
  /* Respect iOS safe areas (notch / home indicator) end to end. */
  padding-top: env(safe-area-inset-top);
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1.25rem;
  background: linear-gradient(135deg, #fff8ef 0%, #fdeee0 100%);
  border-bottom: 1px solid var(--color-border);
  z-index: 1000;
}

.header-title,
.header-title * {
  min-width: 0; /* allow the title to shrink/ellipsize inside the flex row */
  /* Hidden unlock control: press-and-hold the title for 5s. Suppress the
     text-selection, iOS callout menu, and tap highlight that a long press
     would otherwise trigger — applied to the whole subtree since the finger
     actually lands on the <h1>/subtitle, not the wrapper. touch-action:none
     stops iOS from interpreting the stationary press as the start of a pan. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

.header-title {
  cursor: default;
}

.header-title h1 {
  font-size: 1.15rem;
  margin: 0;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--color-text);
}

/* Subtle "edit mode is on" marker — a small dot after the title. Only
   meaningful to someone who knows to look for it. */
.header-title.is-unlocked h1::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 0.45rem;
  border-radius: 50%;
  background: var(--color-visited);
  flex-shrink: 0;
}

.tree {
  font-size: 1.15rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
}

/* The visited-count pill and the Map/List toggle share one height so they
   line up. --pill-height is the outer box height for both. */
.header-right {
  --pill-height: 2.1rem;
}

.progress-summary {
  height: var(--pill-height);
  display: flex;
  align-items: center;
  font-size: 0.88rem;
  color: var(--color-primary-hover);
  font-weight: 600;
  white-space: nowrap;
  background: #fff2e4;
  border: 1px solid #f3d9bc;
  padding: 0 0.75rem;
  border-radius: 999px;
}

/* ---------- View toggle (Map / List) ---------- */
.view-toggle {
  display: flex;
  align-items: stretch;
  height: var(--pill-height);
  background: #fff2e4;
  border: 1px solid #f3d9bc;
  border-radius: 999px;
  padding: 0.2rem;
  flex-shrink: 0;
}

.view-toggle-btn {
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 0.85rem;
  border-radius: 999px;
  cursor: pointer;
}

.view-toggle-btn.active {
  background: var(--color-primary);
  color: #fff;
}

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 999px;
  padding: 0.55rem 1.1rem;
  font-size: 0.88rem;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s ease, transform 0.1s ease;
  /* Comfortable touch target on mobile. */
  min-height: 40px;
}
.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--color-primary-hover);
}
.btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}
.btn-secondary:hover {
  background: #fdf3e7;
}

/* ---------- Main layout ---------- */
.app-main {
  position: relative;
  flex: 1;
  min-height: 0;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
}

/* ---------- List view ---------- */
.list-view {
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow-y: auto;
  background: var(--color-bg);
  padding: 1rem 1.25rem calc(1.5rem + env(safe-area-inset-bottom));
}

.list-filters {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.filter-chip {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
}

.filter-chip.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}

.park-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.park-list-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.9rem;
  cursor: pointer;
}

.park-list-row:hover,
.park-list-row:focus-visible {
  border-color: var(--color-primary);
  outline: none;
}

.park-list-thumb {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--color-bg);
}

.park-list-thumb-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  border: 1px solid var(--color-border);
}

.park-marker-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid #fff;
  box-shadow: var(--shadow-sm);
}

.park-marker-dot.marker-visited {
  background: var(--color-visited);
  box-shadow: 0 0 0 3px var(--color-visited-glow), var(--shadow-sm);
}

.park-marker-dot.marker-unvisited {
  background: var(--color-unvisited);
}

.park-list-name {
  flex: 1;
  font-weight: 600;
  font-size: 0.95rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.park-list-date {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.list-empty {
  text-align: center;
  padding: 2rem 1rem;
}

/* ---------- Leaflet layers control (base map picker) ---------- */
.leaflet-control-layers {
  border-radius: var(--radius-sm) !important;
  border: 1px solid var(--color-border) !important;
  box-shadow: var(--shadow-sm) !important;
  font-family: inherit;
}
.leaflet-control-layers-toggle {
  border-radius: var(--radius-sm) !important;
}
.leaflet-control-layers-expanded {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  padding: 0.6rem 0.8rem !important;
}
.leaflet-control-layers label {
  font-size: 0.85rem;
  margin-bottom: 0.3rem;
}
.leaflet-control-layers-selector {
  accent-color: var(--color-primary);
}

/* ---------- Markers ---------- */
.park-marker .marker-dot {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease;
}

.marker-visited .marker-dot {
  background: var(--color-visited);
  box-shadow: 0 0 0 4px var(--color-visited-glow), var(--shadow-sm);
}

.marker-unvisited .marker-dot {
  background: var(--color-unvisited);
}

/* Larger invisible tap target around each marker so they're easy to hit on touch
   screens without visually enlarging the dot. Leaflet already sets this element
   (.leaflet-marker-icon) to position:absolute and repositions it via left/top on
   every pan/zoom — do NOT override position here, or that positioning breaks and
   markers drift from their real lat/lng as the map reflows. absolute is itself a
   valid positioning context for the ::before below, so no extra rule is needed. */
.park-marker::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44px;
  height: 44px;
  transform: translate(-50%, -50%);
}

/* ---------- Detail panel ----------
   Desktop/tablet: slide-in panel from the right, map stays visible beside it.
   Mobile (see media query below): becomes a bottom sheet instead, so the map
   stays visible above it and it feels native to a phone. */
.detail-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(400px, 100%);
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
  transform: translateX(100%);
  transition: transform 0.24s ease;
  z-index: 1200;
  overflow-y: auto;
  padding: 1.6rem;
  border-left: 1px solid var(--color-border);
}

.detail-panel.open {
  transform: translateX(0);
}

/* Drag handle is a mobile-only affordance; hidden in the desktop side panel. */
.detail-drag-handle {
  display: none;
}

.close-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.9rem;
  background: var(--color-bg);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
}
.close-btn:hover {
  color: var(--color-text);
  background: #f3e8d8;
}

.detail-title {
  font-size: 1.3rem;
  margin: 0 2.2rem 1rem 0;
  font-weight: 700;
}

.detail-row {
  margin-bottom: 1.1rem;
}

.detail-row label {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.4rem;
}

.visited-toggle {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  cursor: pointer;
  font-size: 1rem;
}
.visited-toggle input {
  width: 20px;
  height: 20px;
  accent-color: var(--color-visited);
}

input[type="date"],
textarea,
input[type="file"] {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.95rem;
  background: var(--color-bg);
  color: var(--color-text);
  /* 16px-equivalent minimum on iOS prevents the page auto-zooming on focus. */
  font-size: max(0.95rem, 16px);
}

/* iOS Safari gives <input type="date"> an intrinsic min width and an internal
   value element that ignores the input's width, so it can render wider than
   its container and get clipped off-screen. Strip the native appearance and
   pin the inner value box so it respects width: 100%. */
input[type="date"] {
  -webkit-appearance: none;
  appearance: none;
}
input[type="date"]::-webkit-date-and-time-value {
  text-align: left;
  margin: 0;
}
input[type="date"]::-webkit-datetime-edit {
  padding: 0;
}

input[type="date"]:disabled,
textarea:disabled {
  /* Locked mode: this is real saved content, not a placeholder — keep it
     readable. Only the background flattens to signal "not editable". */
  color: var(--color-text);
  opacity: 1; /* iOS dims disabled fields ~0.4 by default */
  -webkit-text-fill-color: var(--color-text); /* iOS uses this, not `color` */
  background: #f4ece0;
}

textarea {
  resize: vertical;
}

.detail-save-btn {
  width: 100%;
  margin-bottom: 1.3rem;
}

/* ---------- Photo carousel (Instagram-style) ---------- */
.carousel {
  position: relative;
  margin-bottom: 0.6rem;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
}

.carousel-track {
  display: flex;
  transition: transform 0.25s ease;
  touch-action: pan-y;
}

.carousel-slide {
  position: relative;
  flex: 0 0 100%;
  aspect-ratio: 1 / 1;
  display: block;
}

.carousel-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  -webkit-user-drag: none;
}

/* Per-photo delete (edit mode only). */
.carousel-delete {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 50%;
  background: rgba(193, 67, 43, 0.92); /* --color-error */
  color: #fff;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
}
.carousel-delete:hover {
  background: var(--color-error);
}

.carousel-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(46, 38, 32, 0.55);
  color: #fff;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.carousel-arrow-prev {
  left: 0.5rem;
}

.carousel-arrow-next {
  right: 0.5rem;
}

.carousel-arrow-hidden {
  opacity: 0;
  pointer-events: none;
}

.carousel-dots {
  position: absolute;
  bottom: 0.6rem;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 0.35rem;
}

.carousel-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.6);
  padding: 0;
  cursor: pointer;
}

.carousel-dot.active {
  background: #fff;
  transform: scale(1.3);
}

.empty-note,
.hint-text {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  margin: 0.3rem 0;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.5rem + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  background: var(--color-text);
  color: #fff;
  padding: 0.6rem 1.1rem;
  border-radius: 999px;
  font-size: 0.88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 2000;
  max-width: 90vw;
  text-align: center;
}

.toast-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast-error {
  background: var(--color-error);
}

/* ---------- View gate (password screen before the map renders) ---------- */
/* :not([hidden]) so the `hidden` attribute JS sets on unlock actually hides
   it — a bare `.view-gate { display:flex }` overrides the [hidden] UA rule and
   leaves this fixed overlay covering the working map. */
.view-gate:not([hidden]) {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #fff8ef 0%, #fdeee0 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 4000;
  padding: 1.25rem;
}
.view-gate[hidden] {
  display: none;
}

/* Belt-and-suspenders: keep the app hidden until JS removes .view-locked, so a
   slow script load never flashes the map. */
body.view-locked .app-header,
body.view-locked .app-main,
body.view-locked .toast {
  visibility: hidden;
}

.view-gate-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.75rem 1.5rem;
  width: min(360px, 100%);
  box-shadow: var(--shadow-md);
  text-align: center;
}

.view-gate-card h1 {
  margin: 0 0 0.3rem;
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}

.view-gate-sub {
  color: var(--color-text-muted);
  font-size: 0.88rem;
  margin: 0 0 1.1rem;
}

.view-gate input {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: max(0.95rem, 16px);
  margin-bottom: 0.6rem;
}

.view-gate .btn {
  width: 100%;
}

.view-gate .error-text {
  text-align: left;
}

/* ---------- Password modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(46, 32, 18, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  padding: 1rem;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.6rem;
  width: min(340px, 100%);
  box-shadow: var(--shadow-md);
}

.modal h2 {
  margin: 0 0 0.4rem;
  font-size: 1.15rem;
}

.modal-sub {
  color: var(--color-text-muted);
  font-size: 0.85rem;
  margin: 0 0 1rem;
}

.modal input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: max(0.95rem, 16px);
  margin-bottom: 0.5rem;
}

.error-text {
  color: var(--color-error);
  font-size: 0.85rem;
  margin: 0 0 0.6rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 0.4rem;
}

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  .app-header {
    padding: 0.6rem 0.75rem;
    gap: 0.5rem;
  }
  .header-title {
    flex: 1 1 auto;
    min-width: 0;
  }
  .header-title h1 {
    font-size: 0.92rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .header-right {
    gap: 0.45rem;
    --pill-height: 1.9rem;
  }
  .progress-summary {
    font-size: 0.72rem;
    padding: 0 0.55rem;
  }
  .view-toggle-btn {
    font-size: 0.78rem;
    padding: 0 0.7rem;
  }

  /* Bottom sheet with two heights: "peek" (drag handle + park name only) and
     "expanded" (full detail). It is always at least peek-visible once a park
     is selected so you can see which park you're on, and it sits ABOVE iOS
     Safari's bottom toolbar so the handle is grabbable.

     Anchoring: position: fixed against the visual viewport. bottom is offset by
     safe-area-inset-bottom so the sheet's bottom edge clears the home
     indicator; the sheet is otherwise flush to the bottom of the visible area
     (fixed elements sit above the Safari toolbar when the page body isn't
     scrollable — .app-main fills the viewport and doesn't scroll). */
  :root {
    --sheet-peek-height: 4rem;
  }

  .detail-panel {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: env(safe-area-inset-bottom);
    width: 100%;
    height: 85dvh;
    max-height: 85dvh;
    display: flex;
    flex-direction: column;
    overflow: visible; /* inner .detail-content scrolls instead */
    border-left: none;
    border-top: 1px solid var(--color-border);
    border-radius: 20px 20px 0 0;
    padding: 0;
    transition: transform 0.26s ease;
    /* Fully hidden when not open: pushed entirely below the screen. */
    transform: translateY(120%);
  }

  /* Peek: only the handle bar shows above the toolbar. Translate the sheet down
     by (its height - peek height) so the handle lands at the bottom. */
  .detail-panel.open.peek {
    transform: translateY(calc(85dvh - var(--sheet-peek-height)));
  }

  /* Expanded: full sheet up to 85dvh. */
  .detail-panel.open.expanded {
    transform: translateY(0);
  }

  .detail-drag-handle {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-shrink: 0;
    height: var(--sheet-peek-height);
    padding: 0 3rem 0 1.25rem; /* right padding clears the close button */
    cursor: grab;
    touch-action: none; /* let JS own the vertical drag gesture */
    border-radius: 20px 20px 0 0;
    background: var(--color-surface);
  }
  .detail-drag-handle:active {
    cursor: grabbing;
  }

  .detail-peek-title {
    font-weight: 700;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .detail-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden; /* guard against any child (e.g. iOS date input) blowing out width */
    -webkit-overflow-scrolling: touch;
    padding: 0.25rem 1.25rem calc(1.5rem + env(safe-area-inset-bottom));
  }

  /* The full-detail <h2> is redundant with the peek title now — hide it on
     mobile to avoid showing the name twice when expanded. */
  .detail-content .detail-title {
    display: none;
  }

  .close-btn {
    top: 0.85rem;
  }
}

/* Very narrow phones: the title + Map/List toggle + visited count can't share
   one row without overlapping, so stack the header — title on top, controls
   below. */
@media (max-width: 460px) {
  .app-header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.4rem;
  }
  .header-right {
    justify-content: flex-start;
    gap: 0.5rem;
  }
  .header-title h1 {
    font-size: 1rem;
  }
}
