/* ==========================================================================
   POPUP COMPONENT
   Map popup styles for Leaflet integration.
   ========================================================================== */

/* -------------------------------------------------------------------------
   Matches Container
   ------------------------------------------------------------------------- */
/* A stop with several matches stacks its cards at the popup's fixed width. */
.matches-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  margin-top: 4px;
  padding: 4px;
  max-height: 60vh;
  overflow-y: auto;
}

/* -------------------------------------------------------------------------
   Popup Table
   ------------------------------------------------------------------------- */
.popup-table {
  width: 100%;
  margin-bottom: var(--space-1);
  border-collapse: collapse;
  table-layout: fixed;
  font-size: var(--text-xs);
}

.popup-table td {
  padding: 2px 3px;
  vertical-align: top;
  border-bottom: 1px solid var(--color-border);
  line-height: 1.28;
}

.popup-table td:first-child {
  font-weight: var(--font-bold);
  white-space: normal;
  width: 44%;
  padding-right: 10px;
  color: var(--color-fg-muted);
}

.popup-table td:last-child {
  padding-left: 6px;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.popup-table a {
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* -------------------------------------------------------------------------
   Matches View (See Matches): bar, match picker, comparison table, details
   It follows the generic .popup-table rules so that its table rules win.
   ------------------------------------------------------------------------- */
.popup-view-bar {
  display: flex;
  align-items: center;
  margin-bottom: var(--space-2);
  padding-bottom: var(--space-1);
  border-bottom: 1px solid var(--color-border);
}

.leaflet-popup-content .popup-view-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 3px 8px 3px 4px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-primary);
  font-size: var(--text-xs);
  font-weight: var(--font-bold);
  line-height: 1.2;
}

.leaflet-popup-content .popup-view-back i {
  font-size: 0.8em;
}

.leaflet-popup-content .popup-view-back:hover {
  background: var(--color-bg-muted);
  border-color: var(--color-border);
  color: var(--color-primary);
}

/* Several matches: one tab per match, coloured by the matched source. */
.popup-match-picker {
  --picker-accent: var(--color-success-fg);
  --picker-bg: var(--color-success-subtle);
  --picker-border: var(--color-success-border);
  display: flex;
  gap: 4px;
  margin-bottom: var(--space-2);
}

.popup-match-picker[data-source="atlas"] {
  --picker-accent: var(--color-primary);
  --picker-bg: var(--color-primary-subtle);
  --picker-border: var(--color-primary-border);
}

.leaflet-popup-content .popup-match-tab {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  margin: 0;
  padding: 4px 6px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-fg-muted);
  font-size: var(--text-xs);
  line-height: 1.25;
  text-align: left;
}

.leaflet-popup-content .popup-match-tab:hover {
  border-color: var(--picker-border);
  background: var(--color-bg-subtle);
  color: var(--color-fg);
}

.leaflet-popup-content .popup-match-tab[aria-selected="true"] {
  border-color: var(--picker-accent);
  background: var(--picker-bg);
  color: var(--color-fg);
  box-shadow: inset 0 -2px 0 var(--picker-accent);
}

.popup-match-tab-title {
  font-weight: var(--font-bold);
  overflow-wrap: anywhere;
}

.popup-compare[hidden],
.popup-details-panel[hidden] {
  display: none;
}

.popup-compare-table .popup-compare-label {
  width: 24%;
}

.popup-table.popup-compare-table {
  margin-bottom: 0;
}

.popup-table.popup-compare-table th,
.popup-table.popup-compare-table td {
  width: auto;
  padding: 3px 4px;
  vertical-align: top;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  line-height: 1.28;
  overflow-wrap: anywhere;
  color: var(--color-fg);
  font-weight: normal;
}

.popup-table.popup-compare-table tbody th {
  padding-left: 0;
  font-weight: var(--font-bold);
  color: var(--color-fg-muted);
  overflow-wrap: normal;
}

.popup-table.popup-compare-table thead th,
.popup-table.popup-compare-table thead td {
  padding-bottom: 4px;
  border-bottom: 2px solid var(--color-border);
  font-weight: var(--font-bold);
}

.popup-table.popup-compare-table .popup-compare-atlas,
.popup-table.popup-compare-table .popup-compare-atlas a {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
}

.popup-table.popup-compare-table .popup-compare-osm,
.popup-table.popup-compare-table .popup-compare-osm a {
  color: var(--color-success-fg);
  border-bottom-color: var(--color-success);
}

/* An identifier whose ATLAS and OSM values differ. */
.popup-table.popup-compare-table tr.is-different th {
  color: var(--color-warning-fg);
}

.popup-table.popup-compare-table tr.is-different td {
  background: var(--color-warning-subtle);
}

/* Distance and match type describe the pair, so they span both sources. */
.popup-table.popup-compare-table .popup-compare-pair td {
  background: var(--color-bg-subtle);
}

.popup-table.popup-compare-table .popup-compare-links td {
  padding-top: 6px;
  border-bottom: none;
}

.popup-compare-link + .popup-compare-link {
  margin-top: 3px;
}

.popup-compare-missing {
  color: var(--color-fg-subtle);
}

/* Routes and tags of the pair: one panel at a time, named and coloured by source. */
.popup-details {
  margin-top: var(--space-1);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
}

.popup-details-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.popup-details-tab,
.popup-details-panel {
  --detail-accent: var(--color-success-fg);
  --detail-bg: var(--color-success-subtle);
  --detail-border: var(--color-success-border);
}

.popup-details-tab[data-source="atlas"],
.popup-details-panel[data-source="atlas"] {
  --detail-accent: var(--color-primary);
  --detail-bg: var(--color-primary-subtle);
  --detail-border: var(--color-primary-border);
}

.leaflet-popup-content .popup-details-tab {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 2px 6px;
  border: 1px solid var(--color-border-emphasis);
  border-radius: var(--radius-full);
  background: var(--color-bg);
  color: var(--color-fg);
  font-size: var(--text-xs);
  font-weight: var(--font-medium, 500);
  white-space: nowrap;
}

.leaflet-popup-content .popup-details-tab:hover:not(:disabled),
.leaflet-popup-content .popup-details-tab[aria-expanded="true"] {
  border-color: var(--detail-accent);
  background: var(--detail-bg);
  color: var(--detail-accent);
}

.leaflet-popup-content .popup-details-tab:disabled {
  border-style: dashed;
  background: transparent;
  color: var(--color-fg-subtle);
  cursor: default;
}

.popup-details-panel {
  margin-top: 6px;
  max-height: 180px;
  overflow-y: auto;
  padding: 6px 8px;
  border: 1px solid var(--detail-border);
  border-radius: var(--radius-md);
  background: var(--detail-bg);
  font-size: var(--text-xs);
}

.popup-details-panel .route-list {
  margin: 0 !important;
}

/* -------------------------------------------------------------------------
   Match Cards
   ------------------------------------------------------------------------- */
.atlas-match, .osm-match, .gtfs-match {
  margin-bottom: 0;
  width: 100%;
  max-width: none;
  min-width: 0;
  position: relative;
  transition: transform var(--transition-fast);
  background-color: transparent !important;
  border: none !important;
  box-shadow: none !important;
}

.osm-match {
  --bubble-bg: var(--color-success-subtle);
  --bubble-border: var(--color-success-border);
  --bubble-fg: var(--color-success-fg);
}

.atlas-match {
  --bubble-bg: var(--color-primary-subtle);
  --bubble-border: var(--color-primary-border);
  --bubble-fg: var(--color-primary);
}

.gtfs-match {
  --bubble-bg: var(--color-warning-subtle);
  --bubble-border: var(--color-warning-border);
  --bubble-fg: var(--color-warning-fg);
}

.atlas-match.unmatched {
  --bubble-bg: var(--color-danger-subtle);
  --bubble-border: var(--color-danger-border);
  --bubble-fg: var(--color-danger);
}

.osm-match.unmatched {
  --bubble-bg: var(--color-secondary-subtle);
  --bubble-border: var(--color-secondary-border);
  --bubble-fg: var(--color-secondary);
}

.gtfs-match.unmatched {
  --bubble-bg: var(--color-secondary-subtle);
  --bubble-border: var(--color-secondary-border);
  --bubble-fg: var(--color-secondary);
}

.bubble-body {
  padding: 8px 9px;
  background-color: var(--bubble-bg);
  border: 1px solid var(--bubble-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  position: relative;
  z-index: 2;
}

.osm-match:hover, .atlas-match:hover, .gtfs-match:hover {
  transform: translateY(-2px);
}

.atlas-match h5, .osm-match h5, .gtfs-match h5 {
  color: var(--bubble-fg);
  margin-top: 0;
  margin-bottom: var(--space-1);
  font-weight: var(--font-bold);
  font-size: var(--text-xs);
}

.popup-mono {
  font-family: var(--font-family-monospace, monospace);
}

/* -------------------------------------------------------------------------
   Bubble Footer (button row + collapsible panel)
   ------------------------------------------------------------------------- */
.bubble-footer {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--color-border);
}

/* Popups have a fixed width, so buttons that do not fit move to a second line. */
.bubble-btn-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* -------------------------------------------------------------------------
   Premium Collapsible Button
   ------------------------------------------------------------------------- */

.btn-collapsible-arrow {
  font-size: 10px;
  opacity: 0.7;
  transition: transform var(--transition-base);
}

.collapsible-content {
  margin-top: var(--space-2);
  max-height: 180px;
  overflow-y: auto;
  padding: var(--space-2);
  background: var(--color-surface-veil);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--text-xs);
}

/* -------------------------------------------------------------------------
   Leaflet Popup Overrides
   ------------------------------------------------------------------------- */
.leaflet-popup-content {
  margin: 0;
  overflow: auto !important;
  min-width: 0;
  min-height: 0;
  padding: 0;
  position: relative;
  max-width: none;
}

.popup-outer-wrapper {
  max-width: 100%;
}

.popup-content-container {
  max-width: 100%;
}

.leaflet-popup-content::-webkit-resizer {
  display: none;
}

.leaflet-popup-content-wrapper {
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 4px;
  overflow: hidden;
  background: var(--color-bg);
  max-width: min(96vw, 900px);
}

.customPopup .leaflet-popup-content {
  max-width: min(96vw, 900px);
}

.leaflet-popup {
  margin-bottom: 30px;
}

.leaflet-popup-tip-container {
  pointer-events: none;
  z-index: 1;
}

.leaflet-popup-tip {
  width: 18px;
  height: 18px;
}

/* -------------------------------------------------------------------------
   Popup Headings
   ------------------------------------------------------------------------- */
.leaflet-popup-content h4 {
  margin-top: 0;
  margin-bottom: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-fg);
  border-bottom: 1px solid var(--color-border);
  padding-bottom: var(--space-1);
}

.leaflet-popup-content h5 {
  margin-top: var(--space-1);
  margin-bottom: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-fg);
}

/* -------------------------------------------------------------------------
   Popup Buttons
   ------------------------------------------------------------------------- */
.leaflet-popup-content button {
  margin-top: var(--space-1);
  margin-right: var(--space-1);
  padding: 3px var(--space-2);
  font-size: var(--text-xs);
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border-emphasis);
  background: var(--color-bg-subtle);
  color: var(--color-fg);
  cursor: pointer;
  transition: all var(--transition-base);
}

.leaflet-popup-content button:hover {
  background: var(--color-bg-muted);
  border-color: var(--color-fg-subtle);
  color: var(--color-fg);
}

/* -------------------------------------------------------------------------
   Popup Actions Container
   ------------------------------------------------------------------------- */
.bubble-btn-row .popup-action-btn {
  color: var(--color-on-accent) !important;
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-hover));
  border: none;
  border-radius: var(--radius-full);
  font-weight: var(--font-bold);
  padding: 5px 12px;
  box-shadow: var(--shadow-primary);
  font-size: var(--text-xs);
  cursor: pointer;
  transition: all var(--transition-base);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none !important;
  margin-top: 0;
  white-space: nowrap;
}

.bubble-btn-row .popup-action-btn:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-primary);
  background: linear-gradient(135deg, var(--color-primary-hover), var(--color-primary));
}

/* Routes button – same pill shape but neutral gray/white */
.bubble-btn-row .popup-routes-btn {
  color: var(--color-fg) !important;
  background: var(--color-bg) !important;
  border: 1px solid var(--color-border-emphasis) !important;
  box-shadow: var(--shadow-md) !important;
}

.bubble-btn-row .popup-routes-btn:hover {
  background: var(--color-bg-muted) !important;
  border-color: var(--color-fg-subtle) !important;
  box-shadow: var(--shadow-lg) !important;
  transform: translateY(-1px);
}

/* -------------------------------------------------------------------------
   Popup Links
   ------------------------------------------------------------------------- */
.leaflet-popup-content a {
  color: var(--color-primary);
  text-decoration: none;
  transition: color var(--transition-base);
}

.leaflet-popup-content a:hover {
  color: var(--color-primary-hover);
  text-decoration: underline;
}

/* -------------------------------------------------------------------------
   OSM Editor Link
   ------------------------------------------------------------------------- */
.osm-editor-link-container {
  margin-top: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
}

.osm-editor-link {
  font-size: var(--text-xs);
  color: var(--color-primary);
  text-decoration: none;
}

.osm-editor-link:hover {
  text-decoration: underline;
}

/* Styled like the iD link beside it, overriding the generic popup button. */
.leaflet-popup-content .popup-copy-link,
.leaflet-popup-content .popup-copy-link:hover {
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font-size: var(--text-xs);
  color: var(--color-primary);
  cursor: pointer;
}

.leaflet-popup-content .osm-editor-link + .popup-copy-link,
.leaflet-popup-content .osm-editor-link + .popup-main-map-link {
  margin-left: var(--space-3);
}

/* Route maps link a stop to the main map, styled like the iD link. */
.popup-main-map-link {
  font-size: var(--text-xs);
  white-space: nowrap;
  color: var(--color-primary);
  text-decoration: none;
}

.popup-main-map-link:hover {
  text-decoration: underline;
}

.leaflet-popup-content .popup-copy-link:hover {
  text-decoration: underline;
}

.leaflet-popup-content .popup-copy-link.is-copied,
.leaflet-popup-content .popup-copy-link.is-copied:hover {
  color: var(--color-success);
}

/* -------------------------------------------------------------------------
   Popup Drag Handle
   ------------------------------------------------------------------------- */
.popup-drag-handle {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  height: 28px;
  background-color: var(--color-bg-subtle);
  border-bottom: 1px solid var(--color-border);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xs);
  color: var(--color-fg-muted);
  user-select: none;
  z-index: var(--z-dropdown);
  box-shadow: var(--shadow-xs);
  margin-bottom: var(--space-2);
}

.popup-drag-handle svg {
  margin-right: var(--space-2);
  vertical-align: middle;
}

.leaflet-popup-dragging .popup-drag-handle {
  cursor: grabbing;
}

/* -------------------------------------------------------------------------
   Popup Connection Line
   ------------------------------------------------------------------------- */
.popup-connection-line {
  stroke: var(--color-fg-muted);
  stroke-width: 1.5;
  stroke-dasharray: 4, 3;
  pointer-events: none;
}

.popup-connection-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 690;
  overflow: visible;
}

/* -------------------------------------------------------------------------
   Popup States
   ------------------------------------------------------------------------- */
.leaflet-popup-dragging {
  opacity: 0.9;
  filter: drop-shadow(var(--shadow-lg));
}

.leaflet-popup-resizing {
  opacity: 0.9;
}

/* -------------------------------------------------------------------------
   Custom Popup Close Button
   ------------------------------------------------------------------------- */
.customPopup .leaflet-popup-close-button {
  position: absolute;
  top: 0;
  left: 0;
  padding: var(--space-1) var(--space-2) 0 var(--space-2);
  border: none;
  text-align: center;
  width: 24px;
  height: 24px;
  font: 18px/20px var(--font-sans);
  color: var(--color-fg-muted);
  text-decoration: none;
  font-weight: var(--font-bold);
  border-radius: var(--radius-lg) 0 var(--radius-lg) 0;
  background-color: var(--color-surface-glass);
  z-index: 10001;
  transition: all var(--transition-fast);
}

.customPopup .leaflet-popup-close-button:hover {
  color: var(--color-fg);
  background-color: var(--color-bg);
}

/* -------------------------------------------------------------------------
   OSM Tag List (collapsible, single-stop popups)
   ------------------------------------------------------------------------- */
.popup-osm-tags {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: var(--text-xs);
}

.popup-osm-tags td {
  padding: 1px 3px;
  vertical-align: top;
  border-bottom: 1px solid var(--color-border);
  overflow-wrap: anywhere;
}

.popup-osm-tags td:first-child {
  color: var(--color-fg-muted);
  padding-right: var(--space-2);
}
