/* ==========================================================================
   CDN Performance Map — interactive tool
   --------------------------------------------------------------------------
   Markup: .performance-map block in the page, behaviour: /assets/js/tools/performance-map.js
   Prefix: every class starts with .pm- so the tool can drop into any page.

   0. Section wrapper (card + heading around the tool)
   1. Layout & loading states
   2. Toolbar (dropdowns, thresholds, view switch)
   3. Map panel (stage, drawing, scale, prompt, table)
   4. Timeline
   5. Legend
   6. Tooltip
   7. Responsive
   ========================================================================== */

/* 0. Section wrapper — the card that holds the heading, the tool and the source note
   ========================================================================== */
.map-section {
  padding: var(--card-pad-y) var(--card-pad-x);
}

.map-module {
  display: flex;
  flex-direction: column;
  gap: 40px;
  max-width: var(--content-max);
  margin: 0 auto;
}

.map-module__source {
  color: var(--color-neutral-5);
  font-size: 14px;
  line-height: 1.4;
  letter-spacing: -0.02em;
}

@media (max-width: 560px) {
  .map-module {
    gap: 28px;
  }
}

/* 1. Layout & loading states
   ========================================================================== */
.performance-map {
  --pm-legend-w: 240px;
  --pm-blue: #0065ff;
  --pm-line: #dde6f0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--pm-legend-w);
  gap: 20px;
  align-items: stretch;
  transition: grid-template-columns 400ms var(--ease-out);
}

.performance-map.is-legend-collapsed {
  --pm-legend-w: 72px;
}

.performance-map__main {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

/* Status line (loading / error) sits centred over the empty stage */
.pm-status {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--color-neutral-5);
  font-size: 14px;
  text-align: center;
}

.pm-status[hidden] {
  display: none;
}

.pm-retry {
  margin-left: 6px;
  color: var(--color-ink);
  font-weight: 600;
  text-decoration: underline;
}

/* Skeleton shimmer while the data loads */
.performance-map.is-loading .pm-drawing {
  background: linear-gradient(100deg, #f4f1f2 30%, #faf8f8 50%, #f4f1f2 70%) 0 0 / 300% 100%;
  border-radius: var(--radius-md);
  animation: pm-shimmer 1.6s linear infinite;
}

@keyframes pm-shimmer {
  to {
    background-position: -150% 0;
  }
}

/* Controls are inert until the data is in */
.performance-map:not(.is-ready) :is(.pm-toolbar, .pm-legend) {
  pointer-events: none;
  opacity: 0.6;
}

.pm-noscript {
  padding: 24px;
  color: var(--color-neutral-5);
  text-align: center;
}

/* Shared small icon button (timeline arrows, play) */
.pm-icon-button {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: var(--radius-xs);
  color: var(--pm-blue);
  transition:
    background-color 150ms,
    opacity 150ms;
}

.pm-icon-button svg {
  width: 20px;
  height: 20px;
}

.pm-icon-button:hover:not(:disabled) {
  background: #eef4ff;
}

.pm-icon-button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* 2. Toolbar
   ========================================================================== */
.pm-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.pm-toolbar__filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.pm-select {
  position: relative;
}

.pm-select__button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 42px;
  padding: 1px 11px 1px 17px;
  border: 1px solid var(--color-border-input);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-slate);
  font-weight: 500;
  font-size: 12px;
  line-height: 24px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  white-space: nowrap;
  transition:
    border-color 150ms,
    opacity 150ms;
}

.pm-select__button:hover:not(:disabled),
.pm-select__button[aria-expanded="true"] {
  border-color: var(--color-ink);
}

.pm-select__button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pm-select__button img {
  transition: transform 200ms var(--ease-out);
}

.pm-select__button[aria-expanded="true"] img {
  transform: rotate(180deg);
}

.pm-select__menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  min-width: 100%;
  padding: 4px;
  border: 1px solid var(--color-neutral-2);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  box-shadow: 0 16px 32px -16px rgba(42, 10, 16, 0.3);
  animation: pm-menu-in 180ms var(--ease-out);
}

.pm-select__menu--wide {
  width: 340px;
  max-width: calc(100vw - 48px);
}

@keyframes pm-menu-in {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
}

.pm-select__option {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border-radius: var(--radius-xs);
  color: var(--color-neutral-7);
  font-size: 14px;
  text-align: left;
  white-space: nowrap;
}

.pm-select__option:hover,
.pm-select__option:focus-visible {
  background: var(--color-surface-muted);
}

.pm-select__option[aria-checked="true"] {
  font-weight: 600;
}

/* Absolute-latency thresholds: three range inputs stacked on one track */
.pm-thresholds {
  margin-top: 4px;
  padding: 12px 12px 8px;
  border-top: 1px solid var(--color-neutral-2);
}

.pm-thresholds__values {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--color-neutral-5);
  font-size: 12px;
}

.pm-thresholds__values label {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pm-thresholds__values b {
  color: var(--color-ink);
  font-weight: 600;
}

.pm-thresholds__track {
  position: relative;
  height: 40px;
  margin-top: 8px;
}

.pm-thresholds__band {
  position: absolute;
  top: 16px;
  left: 10px;
  right: 10px;
  height: 8px;
  border-radius: 4px;
}

.pm-thresholds__track input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 40px;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  pointer-events: none; /* only the thumbs are grabbable, so all three stay reachable */
}

.pm-thresholds__track input::-webkit-slider-runnable-track {
  height: 8px;
  background: transparent;
}

.pm-thresholds__track input::-moz-range-track {
  height: 8px;
  background: transparent;
}

.pm-thresholds__track input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  margin-top: -6px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--thumb);
  box-shadow: 0 0 0 1px #697586;
  pointer-events: auto;
  cursor: ew-resize;
}

.pm-thresholds__track input::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--thumb);
  box-shadow: 0 0 0 1px #697586;
  pointer-events: auto;
  cursor: ew-resize;
}

.pm-thresholds__ends {
  display: flex;
  justify-content: space-between;
  color: var(--color-neutral-5);
  font-size: 11px;
}

/* Map / table switch */
.pm-view-switch {
  display: flex;
  flex: none;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--color-border-soft);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

.pm-view-switch__button {
  border-radius: 3px;
  transition: background-color 150ms;
}

.pm-view-switch__button[aria-pressed="true"],
.pm-view-switch__button:hover {
  background: var(--color-surface-muted);
}

/* 3. Map panel
   ========================================================================== */
.pm-panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: 1px solid var(--pm-line);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  overflow: hidden;
}

.pm-stage {
  position: relative;
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  /* On large screens the map gets wider, so grow its height too (~2:1 world map).
     100vw - 520px ≈ stage width (page gutters + card padding + legend). */
  min-height: clamp(420px, 50vw - 260px, 1100px);
  padding: 16px 16px 0;
}

/* With a latency scale, it gets its own column on the left */
.performance-map.has-scale .pm-stage {
  grid-template-columns: auto minmax(0, 1fr);
  gap: 16px;
}

.pm-drawing {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.pm-svg {
  display: block;
  width: 100%;
  height: 100%;
  animation: pm-fade-in 600ms var(--ease-out);
}

@keyframes pm-fade-in {
  from {
    opacity: 0;
  }
}

/* Countries / regions that carry data */
.pm-geo {
  stroke: #fff;
  stroke-width: 0.7;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition: fill 260ms ease;
}

.pm-geo:hover {
  stroke: var(--color-ink);
  stroke-width: 1.6;
}

/* Background land without data */
.pm-land {
  fill: #eef1f5;
  stroke: #fff;
  stroke-width: 0.4;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

/* Multi-CDN, nothing picked yet */
.pm-prompt {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  pointer-events: none;
}

.pm-prompt[hidden] {
  display: none;
}

.pm-prompt p {
  max-width: 340px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.94);
  color: var(--color-ink);
  font-weight: 500;
  font-size: 15px;
  line-height: 1.45;
  text-align: center;
}

/* Latency colour scale */
.pm-scale {
  align-self: center;
  pointer-events: none;
}

.pm-scale[hidden] {
  display: none;
}

.pm-scale__title {
  margin-bottom: 8px;
  color: var(--color-neutral-5);
  font-weight: 600;
  font-size: 12px;
}

.pm-scale__bands {
  display: flex;
  flex-direction: column;
}

.pm-scale__bands li {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--color-neutral-5);
  font-size: 11px;
  line-height: 1;
}

.pm-scale__swatch {
  width: 12px;
  height: 28px;
}

.pm-scale__bands li:first-child .pm-scale__swatch {
  border-radius: 6px 6px 0 0;
}

.pm-scale__bands li:last-child .pm-scale__swatch {
  border-radius: 0 0 6px 6px;
}

/* Table view replaces the drawing inside the same frame */
.performance-map.is-table-view :is(.pm-drawing, .pm-scale) {
  display: none;
}

.performance-map.is-table-view .pm-stage {
  grid-template-columns: minmax(0, 1fr);
  padding: 0;
}

.pm-table {
  max-height: 560px;
  overflow: auto;
}

.pm-table[hidden] {
  display: none;
}

.pm-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.pm-table th,
.pm-table td {
  padding: 10px 16px;
  border-bottom: 1px solid #f0edee;
  text-align: left;
  white-space: nowrap;
}

.pm-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--color-surface-muted);
  color: var(--color-slate);
  font-weight: 500;
  font-size: 12px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.pm-table tbody th {
  font-weight: 500;
}

.pm-table tbody tr:hover {
  background: #fbfafa;
}

.pm-table__num {
  text-align: right !important;
  font-variant-numeric: tabular-nums;
}

.pm-table__dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 10px;
  border-radius: 50%;
  vertical-align: 0;
}

/* 4. Timeline
   ========================================================================== */
.pm-timeline {
  padding: 12px 0 0;
}

.pm-timeline:empty {
  min-height: 92px;
}

.pm-timeline__head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  margin-bottom: 8px;
}

.pm-timeline__range,
.pm-timeline__window {
  color: var(--color-slate);
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

.pm-timeline__date {
  color: var(--pm-blue);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.pm-timeline__controls {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.pm-timeline__window {
  margin-right: 4px;
}

/* Play turns into a stop square while playing */
.pm-play__stop {
  display: none;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: var(--color-ink);
}

.pm-play.is-playing .pm-play__icon {
  display: none;
}

.pm-play.is-playing .pm-play__stop {
  display: block;
}

.pm-timeline__scroll {
  overflow-x: auto;
  padding: 0 16px 16px;
  scrollbar-width: thin;
}

.pm-ruler__months {
  position: relative;
  height: 15px;
  margin-bottom: 2px;
}

.pm-ruler__month {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  color: var(--color-ink);
  font-weight: 600;
  font-size: 11.5px;
  line-height: 1;
  white-space: nowrap;
}

.pm-ruler__ticks {
  display: flex;
  align-items: flex-end;
  border-bottom: 1px solid #dfe5ec;
  border-radius: 2px;
  cursor: grab;
  touch-action: pan-y; /* horizontal drags scrub, vertical still scrolls the page */
  user-select: none;
}

.pm-ruler__ticks:active {
  cursor: grabbing;
}

.pm-ruler__tick {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding-bottom: 7px;
}

.pm-ruler__label {
  color: #45535f;
  font-size: 11.5px;
  line-height: 1;
  white-space: nowrap;
  transition: color 120ms;
}

.pm-ruler__mark {
  width: 1px;
  height: 9px;
  background: #c9d2dc;
}

.pm-ruler__tick:hover .pm-ruler__label {
  color: var(--pm-blue);
}

/* Day not downloaded yet (history still loading) */
.pm-ruler__tick.is-pending .pm-ruler__label {
  color: #b9c2cc;
}

.pm-ruler__tick.is-active .pm-ruler__label {
  color: var(--pm-blue);
  font-weight: 600;
}

.pm-ruler__tick.is-active .pm-ruler__mark {
  width: 2px;
  height: 13px;
  background: var(--pm-blue);
}

/* Caret under the baseline marks the day the map shows */
.pm-ruler__tick.is-active::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  background: var(--pm-blue);
  transform: translateX(-50%) rotate(45deg);
}

/* 5. Legend
   ========================================================================== */
.pm-legend {
  position: relative;
  min-width: 0;
}

.pm-legend__panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: 100%;
  padding: 8px 12px;
  border: 1px solid var(--color-neutral-2);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  overflow: hidden;
}

.pm-legend__head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 0 4px 8px;
  white-space: nowrap;
}

.pm-legend__collapse {
  flex: none;
  width: 24px;
  height: 24px;
  margin-left: auto;
}

.pm-legend__collapse img {
  transform: rotate(180deg);
  transition: transform 250ms var(--ease-out);
}

.performance-map.is-legend-collapsed .pm-legend__collapse img {
  transform: none;
}

/* Info popover for "Build Multi CDN" */
.pm-info {
  position: relative;
  display: inline-flex;
}

.pm-info__pop {
  position: absolute;
  top: calc(100% + 10px);
  right: -60px;
  z-index: 40;
  width: 240px;
  padding: 10px 12px;
  border: 1px solid var(--color-neutral-2);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  box-shadow: 0 12px 30px -12px rgba(42, 10, 16, 0.3);
  color: var(--color-neutral-5);
  font-size: 12px;
  line-height: 1.5;
  white-space: normal;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-4px);
  transition:
    opacity 150ms,
    transform 150ms,
    visibility 0s 150ms;
}

.pm-info:hover .pm-info__pop,
.pm-info:focus-within .pm-info__pop {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition:
    opacity 150ms,
    transform 150ms;
}

.pm-legend__body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
}

.pm-legend__list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pm-legend__best,
.pm-legend__item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px;
  border-radius: var(--radius-xs);
  text-align: left;
  transition:
    background-color 150ms,
    transform 200ms var(--ease-out);
}

.pm-legend__best {
  gap: 8px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
}

.pm-legend__best[hidden] {
  display: none;
}

.pm-legend__best:hover,
.pm-legend__item:hover {
  background: #faf8f8;
}

.pm-legend__best.is-selected,
.pm-legend__item.is-selected {
  background: var(--color-surface-muted);
}

.pm-legend__item.is-selected .pm-legend__name {
  font-weight: 600;
}

.pm-legend__name {
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  color: var(--color-neutral-7);
  font-size: 14px;
  line-height: 1.4;
  letter-spacing: -0.02em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pm-legend__best .pm-legend__name {
  color: var(--color-ink);
}

/* Map colour key — hidden once the legend is used for picking */
.pm-legend__swatch {
  flex: none;
  width: 20px;
  height: 6px;
  border-radius: 3px;
  background: var(--swatch);
  transition:
    opacity 200ms,
    transform 200ms;
}

.pm-legend.is-picking .pm-legend__swatch {
  opacity: 0;
  transform: scaleX(0.3);
}

/* Rows cascade in once the data has arrived */
.performance-map.is-ready .pm-legend__list li {
  animation: pm-row-in 500ms var(--ease-out) both;
  animation-delay: calc(var(--i, 0) * 40ms); /* --i set per row in performance-map.js */
}

@keyframes pm-row-in {
  from {
    opacity: 0;
    transform: translateX(-8px);
  }
}

/* Desktop collapsed legend: a slim column of logos (names via title tooltips) */
@media (min-width: 1025px) {
  .performance-map.is-legend-collapsed .pm-legend__panel {
    align-items: center;
    padding: 12px 4px;
  }

  .performance-map.is-legend-collapsed .pm-legend__head {
    flex-direction: column;
    gap: 10px;
    padding: 0;
  }

  .performance-map.is-legend-collapsed .pm-legend__collapse {
    order: -1;
    margin: 0;
  }

  .performance-map.is-legend-collapsed :is(.switch__label, .pm-info, .pm-legend__name, .pm-legend__swatch) {
    display: none;
  }

  .performance-map.is-legend-collapsed :is(.pm-legend__best, .pm-legend__item) {
    justify-content: center;
    width: 48px;
    padding: 8px 0;
  }

  .performance-map.is-legend-collapsed .pm-legend__best {
    padding: 5px 0;
  }
}

/* 6. Tooltip (appended to <body>)
   ========================================================================== */
.pm-tooltip {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 300;
  width: max-content;
  min-width: 212px;
  max-width: 320px;
  padding: 10px;
  border: 1px solid var(--color-neutral-2);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  box-shadow: 0 8px 24px -8px rgba(42, 10, 16, 0.3);
  font-family: var(--font-sans);
  pointer-events: none;
}

.pm-tooltip[hidden] {
  display: none;
}

.pm-tooltip__title {
  margin-bottom: 8px;
  color: var(--color-ink);
  font-weight: 600;
  font-size: 14px;
  line-height: 1.4;
}

.pm-tooltip__rows,
.pm-tooltip__context {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.pm-tooltip__context {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #f0edee;
}

.pm-tooltip__row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.pm-tooltip__key {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--color-neutral-5);
  font-size: 12px;
  white-space: nowrap;
}

.pm-tooltip__dot {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.pm-tooltip__value {
  color: var(--color-neutral-7);
  font-weight: 500;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.pm-tooltip__value--worse {
  color: #d13333;
}

.pm-tooltip__value--par {
  color: #2f9e44;
}

/* 7. Responsive
   ========================================================================== */

/* Tablet & mobile: legend moves under the map, map keeps its own proportions */
@media (max-width: 1024px) {
  .performance-map {
    grid-template-columns: minmax(0, 1fr);
  }

  .pm-stage {
    min-height: 0;
  }

  .pm-drawing {
    aspect-ratio: var(--pm-map-aspect, 2 / 1);
  }

  .pm-legend__list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
    gap: 4px 12px;
  }

  .performance-map.is-legend-collapsed .pm-legend__body {
    display: none;
  }
}

@media (max-width: 600px) {
  .pm-toolbar {
    align-items: flex-start;
  }

  .pm-toolbar__filters {
    gap: 8px;
  }

  .pm-select__button {
    height: 38px;
    padding: 1px 6px 1px 10px;
    font-size: 11px;
  }

  .pm-stage {
    padding: 12px 8px 0;
  }

  /* Scale goes horizontal under the map */
  .performance-map.has-scale .pm-stage {
    grid-template-columns: minmax(0, 1fr);
  }

  .pm-scale {
    order: 2;
    align-self: stretch;
  }

  .pm-scale__bands {
    flex-direction: row;
  }

  .pm-scale__bands li {
    flex: 1 1 0;
    flex-direction: column;
    gap: 4px;
    font-size: 10px;
  }

  .pm-scale__swatch {
    width: 100%;
    height: 10px;
  }

  .pm-scale__bands li:first-child .pm-scale__swatch {
    border-radius: 5px 0 0 5px;
  }

  .pm-scale__bands li:last-child .pm-scale__swatch {
    border-radius: 0 5px 5px 0;
  }

  .pm-timeline__range {
    display: none;
  }

  .pm-legend__list {
    grid-template-columns: 1fr 1fr;
    gap: 2px 4px;
  }

  .pm-legend__item {
    gap: 8px;
    padding: 6px 4px;
  }

  .pm-legend__name {
    font-size: 13px;
  }

  .pm-legend__swatch {
    width: 14px;
  }

  .pm-table th,
  .pm-table td {
    padding: 8px 10px;
    font-size: 13px;
  }

  .pm-tooltip {
    min-width: 0;
    max-width: calc(100vw - 16px);
  }
}
