:root {
  --panel-bg: #ffffff;
  --panel-border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #e11d48;
  --accent-soft: #fff1f2;
  --danger: #b91c1c;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  /* Nothing on the page scrolls, so kill the rubber-band on iOS. */
  overscroll-behavior: none;
}

/*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map. dvh tracks Safari's collapsing toolbar, so the map
 * is neither cropped nor scrollable as the bar hides.
 */
#map {
  height: 100%;
  height: 100dvh;
}

/* ---------------------------------------------------------------- the panel */

.panel {
  width: 264px;
  margin: 10px;
  /* Landscape on a notched phone puts the display cutout to one side. */
  margin-top: max(10px, env(safe-area-inset-top));
  margin-left: max(10px, env(safe-area-inset-left));
  background-color: var(--panel-bg);
  border-radius: var(--radius);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.16),
    0 8px 24px rgba(15, 23, 42, 0.12);
  font-family: var(--font);
  color: var(--text);
  overflow: hidden;
}

/*
 * Touch defaults for every control in the panel: no grey tap flash, no
 * 300ms double-tap-to-zoom delay, and no text selection or callout when a
 * press turns into a drag.
 */
.panel button {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.panel__toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

/* Hover styling only where a pointer can hover — otherwise it sticks on tap. */
@media (hover: hover) {
  .panel__toggle:hover {
    background-color: #f8fafc;
  }
}

.panel__chevron {
  width: 16px;
  height: 16px;
  flex: none;
  color: var(--text-muted);
  transition: transform 150ms ease;
}

.panel.is-collapsed .panel__chevron {
  transform: rotate(-90deg);
}

.panel__title {
  flex: 1;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.panel__count {
  min-width: 20px;
  padding: 1px 7px;
  border-radius: 999px;
  background-color: #f1f5f9;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-align: center;
}

.panel__count:empty {
  display: none;
}

.panel__body {
  padding: 0 12px 12px;
  border-top: 1px solid var(--panel-border);
}

.panel.is-collapsed .panel__body {
  display: none;
}

/* --------------------------------------------------------------- date range */

.field {
  padding-top: 10px;
}

.field__label {
  display: block;
  margin-bottom: 4px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.field__input {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 8px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background-color: #fff;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
}

@media (hover: hover) {
  .field__input:hover {
    border-color: #cbd5e1;
  }
}

.field__input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ------------------------------------------------------------------ year row */

.years {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.years:empty {
  display: none;
}

.year {
  flex: none;
  padding: 3px 9px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background-color: #fff;
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  cursor: pointer;
}

@media (hover: hover) {
  .year:hover {
    border-color: #cbd5e1;
    color: var(--text);
  }
}

.year:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.year.is-active {
  background-color: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ------------------------------------------------------- selected route info */

.detail {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background-color: var(--accent-soft);
}

.detail__date {
  font-size: 12px;
  font-weight: 600;
}

.detail__stats {
  display: flex;
  gap: 12px;
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 11px;
}

.detail__stats strong {
  color: var(--text);
  font-weight: 600;
}

.detail__description {
  margin: 6px 0 0;
  font-size: 11px;
  line-height: 1.4;
  color: var(--text-muted);
}

/* ------------------------------------------------------------------ playback */

.playback {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.playback__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  flex: none;
  padding: 0;
  border: 1px solid var(--panel-border);
  border-radius: 50%;
  background-color: #fff;
  color: var(--accent);
  cursor: pointer;
}

@media (hover: hover) {
  .playback__toggle:hover {
    background-color: var(--accent-soft);
    border-color: var(--accent);
  }
}

.playback__toggle:active {
  background-color: var(--accent-soft);
}

.playback__toggle svg {
  width: 14px;
  height: 14px;
}

.playback__slider {
  flex: 1;
  min-width: 0;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.playback__clock {
  flex: none;
  min-width: 68px;
  color: var(--text-muted);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* -------------------------------------------------------------------- status */

.status {
  margin: 10px 0 0;
  font-size: 12px;
  line-height: 1.4;
  color: var(--text-muted);
}

.status[data-tone="error"] {
  color: var(--danger);
}

/* ---------------------------------------------------------------- route list */

.routes {
  max-height: min(46vh, 340px);
  margin-top: 10px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.routes:empty {
  display: none;
}

.route {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.route + .route {
  margin-top: 2px;
}

@media (hover: hover) {
  .route:hover:not(:disabled) {
    background-color: #f8fafc;
  }
}

.route:active:not(:disabled) {
  background-color: #f1f5f9;
}

.route:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.route.is-selected {
  background-color: var(--accent-soft);
  border-color: #fecdd3;
}

.route__thumb {
  display: flex;
  width: 44px;
  height: 44px;
  flex: none;
  border-radius: 6px;
  background-color: #f1f5f9;
  color: var(--text-muted);
}

.route.is-selected .route__thumb {
  background-color: #fff;
  color: var(--accent);
}

.route__thumb svg {
  width: 100%;
  height: 100%;
}

.route__meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.route__date {
  font-size: 12px;
  font-weight: 600;
}

.route.is-selected .route__date {
  color: var(--accent);
}

.route__stats {
  color: var(--text-muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.route.is-loading .route__thumb {
  animation: pulse 1.2s ease-in-out infinite;
}

.route.is-broken {
  cursor: default;
  opacity: 0.6;
}

.route.is-broken .route__stats {
  color: var(--danger);
}

@keyframes pulse {
  50% {
    opacity: 0.45;
  }
}

/* -------------------------------------------------------------- info windows */

.infowindow {
  font-family: var(--font);
  color: var(--text);
  max-width: 220px;
}

.infowindow__heading {
  font-size: 12px;
  font-weight: 600;
}

.infowindow__stats {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 11px;
}

.infowindow__description {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.4;
}

/*
 * With a pointer the info window is hover-driven, so its close button would
 * never be used. On touch the window is opened by a tap and has to be
 * closeable, so the button stays.
 */
@media (hover: hover) {
  .gm-ui-hover-effect {
    display: none !important;
  }
}

/* ------------------------------------------------------- short screens (landscape) */

/*
 * A phone on its side is ~400px tall: the list has to give way or the panel
 * runs off the bottom of the map.
 */
@media (min-width: 641px) and (max-height: 520px) {
  .routes {
    max-height: 26vh;
  }
}

/* ------------------------------------------------------------ phones: sheet */

/*
 * On a phone the map is the whole point, so the panel drops to the bottom as
 * a sheet with two states. Peek keeps the selected route's stats and its
 * playback controls in thumb reach; expanding brings up the period filters
 * and the route list. JS moves the element out of the map's control layer at
 * this same breakpoint, which is what lets it be position: fixed here.
 */
@media (max-width: 640px) {
  .panel {
    position: fixed;
    z-index: 3;
    right: 0;
    bottom: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    width: auto;
    max-width: none;
    max-height: 85vh;
    max-height: 85dvh;
    margin: 0;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -1px 3px rgba(15, 23, 42, 0.1),
      0 -10px 30px rgba(15, 23, 42, 0.16);
    transition: transform 160ms ease;
  }

  .panel__toggle {
    position: relative;
    min-height: 48px;
    padding: 16px 16px 12px;
    /* The header is the drag surface, so it owns vertical gestures. */
    touch-action: none;
  }

  /* Grab handle, the standard affordance for "this sheet moves". */
  .panel__toggle::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    width: 36px;
    height: 4px;
    margin-left: -18px;
    border-radius: 999px;
    background-color: #cbd5e1;
  }

  .panel__title {
    font-size: 15px;
  }

  .panel__count {
    padding: 2px 9px;
    font-size: 12px;
  }

  /* One scroll container per sheet: the body scrolls, the list inside it does
     not, so a drag never has to pick between two nested scrollers. */
  .panel__body {
    flex: 1;
    min-height: 0;
    padding: 0 16px calc(14px + env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .routes {
    max-height: none;
    overflow: visible;
  }

  /* Peek state: the chevron points up because tapping it opens the sheet. */
  .panel.is-collapsed .panel__chevron {
    transform: rotate(180deg);
  }

  .panel.is-collapsed .panel__body {
    display: block;
  }

  .panel.is-collapsed .field,
  .panel.is-collapsed .years,
  .panel.is-collapsed .routes {
    display: none;
  }

  .panel.is-collapsed .detail {
    margin-top: 12px;
  }

  /* ---- targets sized for a thumb (44px) and type sized for a phone ---- */

  .field__label {
    font-size: 12px;
  }

  .field__input {
    padding: 11px 12px;
    /* 16px keeps iOS from zooming the page when the field takes focus. */
    font-size: 16px;
  }

  .years {
    gap: 6px;
    margin-top: 10px;
  }

  .year {
    min-height: 40px;
    padding: 4px 16px;
    font-size: 13px;
  }

  .detail {
    padding: 10px 12px;
  }

  .detail__date {
    font-size: 14px;
  }

  .detail__stats,
  .detail__description {
    font-size: 12px;
  }

  .playback {
    gap: 12px;
    margin-top: 12px;
  }

  .playback__toggle {
    width: 44px;
    height: 44px;
  }

  .playback__toggle svg {
    width: 18px;
    height: 18px;
  }

  /* Taller than it looks: the extra height is hit area for the thumb. */
  .playback__slider {
    height: 44px;
  }

  .playback__clock {
    min-width: 76px;
    font-size: 11px;
  }

  .status {
    font-size: 13px;
  }

  .route {
    gap: 12px;
    padding: 8px 6px;
  }

  .route__thumb {
    width: 48px;
    height: 48px;
  }

  .route__date {
    font-size: 14px;
  }

  .route__stats {
    font-size: 12px;
  }

  .infowindow {
    max-width: 240px;
  }

  .infowindow__heading {
    font-size: 13px;
  }

  .infowindow__stats,
  .infowindow__description {
    font-size: 12px;
  }

  /* ------------------------------------------------- date picker as a modal */

  /*
   * The picker positions itself against the input with inline styles, which
   * on a phone puts it half off-screen behind the sheet. Pin it to the middle
   * of the viewport instead and grow the cells to tappable size.
   */
  .daterangepicker {
    position: fixed !important;
    top: 50% !important;
    right: 12px !important;
    bottom: auto !important;
    left: 12px !important;
    width: auto !important;
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
    transform: translateY(-50%);
    border-radius: 12px;
  }

  /* The callout arrow points at an input the picker is no longer anchored to. */
  .daterangepicker::before,
  .daterangepicker::after {
    display: none;
  }

  .daterangepicker .ranges {
    float: none;
    width: auto;
  }

  .daterangepicker .ranges li {
    padding: 11px 12px;
    font-size: 14px;
  }

  .daterangepicker .drp-calendar {
    float: none;
    width: auto;
    max-width: none;
    padding: 8px 12px 12px;
  }

  /* Matches the library's own selector, which is specific enough to win. */
  .daterangepicker .calendar-table td,
  .daterangepicker .calendar-table th {
    min-width: 38px;
    width: 38px;
    height: 38px;
    line-height: 38px;
    font-size: 13px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .panel {
    transition: none;
  }

  .panel__chevron {
    transition: none;
  }

  .route.is-loading .route__thumb {
    animation: none;
  }
}
