/* ---------- base ---------- */

:root {
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --panel: #ffffff;
  --accent: #e8006f;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.18);
  --drawer-profile-height: 220px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #dfe6ec;
}

#map {
  position: absolute;
  inset: 0;
}

button { font: inherit; color: inherit; }

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}
.icon-button:hover { background: #f1f3f5; }
.icon-button:disabled { opacity: 0.3; cursor: default; background: transparent; }

/* ---------- trip title card ---------- */

.trip-card {
  position: absolute;
  top: 12px;
  left: 12px;
  max-width: min(360px, calc(100vw - 24px - 140px));
  padding: 12px 16px;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.trip-title { margin: 0; font-size: 1.15rem; line-height: 1.2; }
.trip-subtitle { margin: 4px 0 0; font-size: 0.8rem; color: var(--muted); }
.trip-summary { margin: 6px 0 0; font-size: 0.85rem; font-weight: 600; }

/* ---------- map controls ---------- */

.map-controls {
  position: absolute;
  top: 12px;
  right: 56px; /* leave room for MapLibre's zoom/compass buttons */
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.segmented, .toggle {
  display: flex;
  padding: 3px;
  background: var(--panel);
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.segmented button {
  border: 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: transparent;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--ink); color: #fff; }

.toggle {
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.toggle input { accent-color: var(--ink); margin: 0; }

/* ---------- route popup ---------- */

.stage-popup .maplibregl-popup-content {
  padding: 6px 10px;
  font-size: 0.8rem;
  border-radius: 8px;
  box-shadow: var(--shadow);
  pointer-events: none;
}
.stage-popup .maplibregl-popup-tip { display: none; }

/* ---------- photo markers ---------- */

.photo-marker {
  /* MapLibre positions markers absolutely; don't override position here */
  width: 14px;
  height: 14px;
  padding: 0;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #334155;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  transition: width 0.15s, height 0.15s;
}
.photo-marker img {
  display: none;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}
.photo-count {
  display: none;
  position: absolute;
  top: -6px;
  right: -8px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  border: 1.5px solid #fff;
}
.photos-large .photo-marker { width: 44px; height: 44px; border-width: 3px; }
.photos-large .photo-marker img { display: block; }
.photos-large .photo-count { display: block; }
.photo-marker:hover { z-index: 2; transform-origin: center; }
.photos-large .photo-marker:hover { width: 54px; height: 54px; }
.photos-hidden .photo-marker { display: none; }

/* ---------- bottom drawer ---------- */

.drawer {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(12px + var(--safe-bottom));
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.drawer-head {
  display: flex;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 12px;
}
.drawer-toggle svg { transition: transform 0.2s; transform: rotate(180deg); }
.drawer.collapsed .drawer-toggle svg { transform: rotate(0deg); }

.drawer-heading { min-width: 0; flex: 0 1 auto; }
.drawer-eyebrow { margin: 0; font-size: 0.75rem; color: var(--muted); }
.drawer-title {
  margin: 0;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.drawer-stats {
  display: flex;
  gap: 4px 20px;
  margin: 0 0 0 auto;
}
.stat dt { font-size: 0.7rem; color: var(--muted); white-space: nowrap; }
.stat dd { margin: 0; font-size: 0.95rem; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }

.drawer-nav { display: flex; }
.drawer:not(.has-stage) .drawer-close { display: none; }

.drawer-body {
  height: var(--drawer-profile-height);
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  transition: height 0.25s ease, padding 0.25s ease;
}
.drawer.collapsed .drawer-body { height: 0; padding-bottom: 0; }

.profile-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  padding: 0 8px 4px 48px;
  font-size: 0.72rem;
  color: var(--muted);
}
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.swatch {
  display: inline-block;
  width: 12px;
  height: 10px;
  border-radius: 2px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}

.profile-chart {
  position: relative;
  flex: 1;
  min-height: 0;
  touch-action: pan-y;
}
.profile-chart canvas { position: absolute; inset: 0; }
.profile-chart canvas + canvas { cursor: crosshair; }

.profile-tooltip {
  position: absolute;
  top: 0;
  left: 0;
  padding: 6px 9px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.15);
  font-size: 0.78rem;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
}
.tt-title { font-weight: 700; margin-bottom: 2px; }
.tt-grade { display: flex; align-items: center; gap: 6px; }
.tt-hint { color: var(--muted); font-size: 0.72rem; margin-top: 2px; }

/* ---------- photo modal ---------- */

body.modal-open .maplibregl-ctrl, body.modal-open .map-controls { visibility: hidden; }

.photo-modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  background: rgba(10, 12, 16, 0.92);
  color: #fff;
}
.photo-modal[hidden] { display: none; }

.modal-stage {
  position: relative;
  margin: 0;
  height: 100%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 56px 0 16px;
}
.modal-image {
  max-width: 100%;
  max-height: calc(100vh - 150px);
  max-height: calc(100dvh - 150px);
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  transition: opacity 0.2s;
}
.photo-modal.loading .modal-image { opacity: 0.25; }

.modal-spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 36px;
  height: 36px;
  margin: -18px 0 0 -18px;
  border: 3px solid rgba(255, 255, 255, 0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: none;
}
.photo-modal.loading .modal-spinner { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

.photo-modal figcaption {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 16px;
  margin-top: 12px;
  font-size: 0.9rem;
  text-align: center;
}
.modal-caption { display: flex; flex-wrap: wrap; gap: 4px 12px; justify-content: center; }
.modal-hint {
  flex-basis: 100%;
  margin: 0;
  font-weight: 700;
  color: #ffd166;
}
.modal-counter { color: rgba(255, 255, 255, 0.55); font-variant-numeric: tabular-nums; }
.link-button {
  border: 0;
  padding: 0;
  background: none;
  color: #8ecaff;
  text-decoration: underline;
  cursor: pointer;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1;
  width: 44px;
  height: 44px;
  color: #fff;
}
.modal-close:hover { background: rgba(255, 255, 255, 0.12); }

.modal-arrow {
  display: grid;
  place-items: center;
  width: 56px;
  height: 96px;
  margin: 0 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #fff;
  cursor: pointer;
}
.modal-arrow:hover { background: rgba(255, 255, 255, 0.1); }

.load-error {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 420px;
  padding: 16px 20px;
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Keep MapLibre's scale bar and attribution above the drawer (--drawer-height is set from JS) */
.maplibregl-ctrl-bottom-left, .maplibregl-ctrl-bottom-right {
  bottom: calc(var(--drawer-height, 0px) + 16px + var(--safe-bottom));
  transition: bottom 0.25s ease;
}

/* ---------- small screens ---------- */

@media (max-width: 760px) {
  .drawer-head { flex-wrap: wrap; }
  .drawer-heading { flex: 1 1 0; }
  .drawer-stats {
    order: 3;
    flex-basis: 100%;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px 8px;
  }
  .stat dt { font-size: 0.62rem; white-space: normal; line-height: 1.15; }
  .stat dd { font-size: 0.82rem; }
}

@media (max-width: 640px) {
  :root { --drawer-profile-height: 170px; }
  .trip-card { top: 8px; left: 8px; padding: 8px 12px; max-width: calc(100vw - 16px - 60px); }
  .trip-title { font-size: 0.95rem; }
  .trip-subtitle { display: none; }
  .map-controls {
    top: auto;
    right: auto;
    left: 8px;
    bottom: auto;
    margin-top: 0;
    position: absolute;
    top: 86px;
    justify-content: flex-start;
    gap: 6px;
  }
  .segmented button { padding: 5px 10px; font-size: 0.8rem; }
  .drawer { left: 6px; right: 6px; bottom: calc(6px + var(--safe-bottom)); }
  .profile-legend { padding-left: 8px; }
  .modal-arrow { position: absolute; bottom: 8px; width: 64px; height: 56px; z-index: 1; }
  .modal-prev { left: 4px; }
  .modal-next { right: 4px; }
  .photo-modal { grid-template-columns: 1fr; }
  .modal-stage { padding: 56px 8px 72px; }
  .modal-image { max-height: calc(100dvh - 210px); }
}
