:root {
  --bg: #f5efe4;
  --panel-bg: rgba(255, 250, 243, 0.88);
  --panel-border: rgba(60, 40, 20, 0.09);
  --card-bg: #fffaf3;
  --text: #241f19;
  --muted: #8a7c6c;
  --accent: #c2410c;
  --accent-text: #fff8f0;
  --accent-2: #b45309;
  --accent-2-bg: #fde8d2;
  --accent-2-text: #7c3a05;
  --chip-bg: #efe6d8;
  color-scheme: light;
}

* { box-sizing: border-box; }

/* Browsers give form controls their own default font that doesn't inherit from body/html —
   without this, every button/input silently falls back to the system font regardless of what's
   set above. */
button, input, select, textarea { font-family: inherit; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Fredoka", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-weight: 500;
  -webkit-font-smoothing: antialiased;
}

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

#topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 500;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: max(14px, env(safe-area-inset-top)) 14px 24px;
  background: linear-gradient(to bottom, rgba(245,239,228,0.95) 0%, rgba(245,239,228,0.85) 55%, rgba(245,239,228,0) 100%);
  pointer-events: none;
}

#topbar > * { pointer-events: auto; }

#brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 21px;
  color: var(--accent);
}

.nav-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: block;
}

.segmented {
  display: inline-flex;
  background: var(--card-bg);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
  box-shadow: 0 1px 2px rgba(60, 40, 20, 0.05);
}

.segmented button {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.2px;
  cursor: pointer;
}

.segmented button.active {
  background: var(--accent);
  color: var(--accent-text);
}

/* Mobile-first: compact stacked icon + small label — narrow viewports have little room to
   share between the logo and this toggle. */
#activity-toggle button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
}

#activity-toggle .icon {
  font-size: 18px;
  line-height: 1;
}

#activity-toggle .btn-label {
  font-size: 10px;
  font-weight: 600;
  line-height: 1;
}

/* Wider viewports have room to spare — go back to a single inline row, bigger label text. */
@media (min-width: 640px) {
  #activity-toggle button {
    flex-direction: row;
    gap: 6px;
    padding: 8px 14px;
  }

  #activity-toggle .icon {
    font-size: 16px;
  }

  #activity-toggle .btn-label {
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 0.2px;
  }
}

#message {
  position: fixed;
  top: 64px;
  left: 12px;
  right: 12px;
  z-index: 600;
  background: #fef9e7;
  border: 1px solid #fde68a;
  color: #854d0e;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(60, 40, 20, 0.08);
}

#message.error {
  background: #fef2f2;
  border-color: #fca5a5;
  color: #991b1b;
}

.hidden { display: none !important; }

#tracking-bar {
  position: fixed;
  top: max(64px, calc(env(safe-area-inset-top) + 54px));
  left: 12px;
  right: 12px;
  z-index: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--card-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: 0 4px 16px rgba(60, 40, 20, 0.12);
}

/* Off-route reuses the same red as #message.error / #result.error for a consistent "something's
   wrong" signal across the app, rather than inventing another color for the same meaning. */
#tracking-bar.off-route {
  background: #fef2f2;
  border-color: #fca5a5;
  color: #991b1b;
}

#tracking-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
  min-width: 0;
}

#tracking-status {
  font-weight: 700;
}

#tracking-stop-btn {
  flex-shrink: 0;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

#panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 500;
  background: var(--panel-bg);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-top: 1px solid var(--panel-border);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 32px rgba(60, 40, 20, 0.10);
  max-height: 70vh;
  display: flex;
  flex-direction: column;
}

/* The whole strip is one tap target (not just the handle bar) — easier to hit than a 36px bar,
   and matches how native bottom sheets treat their whole header as the toggle/drag affordance. */
#panel-toggle {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
  flex-shrink: 0;
  border: none;
  background: none;
  padding: 10px 16px 8px;
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.panel-handle-bar {
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--panel-border);
}

.panel-toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

#panel-summary {
  flex: 1;
  min-width: 0;
  text-align: left;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  /* One line, always — this is the whole point of the collapsed state, so a long start-status or
     result string gets truncated rather than wrapping and defeating "one line summary". */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-chevron {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--muted);
  transition: transform 0.2s ease;
}

/* Points up when expanded (content below), down when collapsed (content is "under" the fold) —
   inverted from what "▾ means expand" might suggest, but matches the direction content would
   physically unfold if you dragged the sheet instead of tapping. */
#panel:not(.collapsed) .panel-chevron {
  transform: rotate(180deg);
}

/* The 1fr/0fr grid-template-rows trick: animates a smooth height collapse for content whose height
   isn't known upfront (the panel body's row count varies with stops/result), which plain
   max-height or display:none transitions can't do without either guessing a max-height or losing
   the animation entirely. Requires the single grid row's content to clip rather than push the row
   taller than its track — .panel-body-inner's overflow-y (below) does that clipping *and* doubles
   as the scrollbar for a tall expanded panel (many stop chips), since auto/scroll clip to bounds
   exactly like hidden does, just with a scrollbar when there's more to see.
*/
#panel-body {
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows 0.25s ease;
  /* Lets this shrink below its content height instead of pushing #panel past its max-height —
     a flex child's default min-height is auto (= content size), which would defeat the cap. */
  flex: 1 1 auto;
  min-height: 0;
}

#panel.collapsed #panel-body {
  grid-template-rows: 0fr;
}

.panel-body-inner {
  overflow-y: auto;
  min-height: 0;
  padding: 0 16px max(14px, env(safe-area-inset-bottom));
}

.panel-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.panel-row:last-child { margin-bottom: 0; }

.label {
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
  min-width: 52px;
}

.muted { color: var(--muted); font-size: 13px; }

#start-status {
  font-size: 13px;
  color: var(--muted);
}

#start-status.set { color: var(--text); font-weight: 500; }

.chip-list {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--chip-bg);
  border: 1px solid transparent;
  color: var(--text);
  padding: 7px 11px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.2px;
}

.chip button {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
}

.preset-chip {
  background: var(--chip-bg);
  border: 1px solid transparent;
  color: var(--text);
  padding: 7px 13px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.2px;
  cursor: pointer;
}

.preset-chip.active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

#distance-input {
  width: 90px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: var(--card-bg);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
}

.unit { color: var(--muted); font-size: 13px; }

.actions { gap: 10px; }

.primary-btn {
  flex: 1;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.3px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(194, 65, 12, 0.30);
}

.primary-btn:disabled {
  opacity: 0.5;
  box-shadow: none;
}

#use-location {
  margin-left: auto;
}

.ghost-btn {
  background: var(--card-bg);
  color: var(--text);
  border: 1px solid var(--panel-border);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
}

#result {
  font-size: 13px;
  line-height: 1.5;
  background: var(--card-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px 12px;
  box-shadow: 0 1px 3px rgba(60, 40, 20, 0.06);
}

#result strong {
  font-size: 16px;
  font-weight: 700;
}

#result.warn { border-color: #fde68a; background: #fef9e7; color: #854d0e; }
#result.error { border-color: #fca5a5; background: #fef2f2; color: #991b1b; }

.maps-link {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin-left: 8px;
  vertical-align: middle;
  color: var(--accent-2-text);
  background: var(--accent-2-bg);
  font-weight: 700;
  font-size: 11px;
  text-decoration: none;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
  /* Also used on a <button> (the GPX share/save action) alongside the <a> map links — reset the
     button-specific defaults so it matches those pills exactly instead of picking up UA styling. */
  border: none;
  font-family: inherit;
  cursor: pointer;
}

.leaflet-container { background: #efe7da; }

.loopster-marker { text-align: center; font-size: 20px; line-height: 26px; filter: drop-shadow(0 1px 2px rgba(0,0,0,0.25)); }
.stop-pin {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(60, 40, 20, 0.35);
}
