/* ============================================================
   Section 2 — 所在國家分布圖
   ============================================================ */

#map {
  background: var(--color-surface);
  border-top: 1px solid var(--color-border-light);
  border-bottom: 1px solid var(--color-border-light);
  overflow-x: hidden; /* 讓左側地圖可被裁切 */
}

/* 讓 container 變 flex column，方便 map-container 靠右對齊 */
#map .container {
  display: flex;
  flex-direction: column;
}

/* --- 地圖容器 --- */
.map-container {
  position: relative;
  background: var(--color-surface-alt);
  border-radius: var(--radius-lg);
  overflow: visible; /* popup 卡片不被裁切 */
  width: calc(var(--container-max) - var(--container-padding) * 2); /* 固定 1136px，與校準寬度一致 */
  align-self: flex-end; /* 靠右對齊，窄視窗往左裁切 */
}

#world-map-svg {
  width: 100%;
  height: 480px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: block;
}

.jvm-tooltip {
  display: none !important;
}

/* ============================================================
   HTML overlay（頭像 + popup）
   ============================================================ */

.map-markers-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* ── Callout 群組容器 ── */
.map-callout {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: all;
  z-index: 10;
}

/* 有 popup 開啟時，把整個 callout 提到最上層，避免被鄰近群組擋住 */
.map-callout:has(.map-avatar-item.is-open) {
  z-index: 30;
}

.map-callout__grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

.map-callout__row {
  display: flex;
  gap: 5px;
}

/* ── 共用：單顆頭像 item ── */
.map-avatar-item {
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
}

.map-avatar-item > img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  object-position: top;
  border: 2.5px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
  display: block;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.map-avatar-item:hover > img,
.map-avatar-item.is-open > img {
  transform: translateY(-3px);
  box-shadow: 0 5px 14px rgba(0, 0, 0, 0.22);
}

.map-avatar-item.is-country-hover > img {
  transform: translateY(-2px);
}

/* ============================================================
   Popup 卡片
   ============================================================ */

.map-popup {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%) scale(0.88);
  transform-origin: bottom center;
  width: 140px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card-hover);
  padding: var(--space-3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 100;
  text-align: center;
}

/* 向下展開（callout 靠近頂端時） */
.map-callout.popup-below .map-popup {
  bottom: auto;
  top: calc(100% + 10px);
  transform-origin: top center;
}

.map-avatar-item.is-open .map-popup {
  opacity: 1;
  transform: translateX(-50%) scale(1);
  pointer-events: all;
}

/* 小箭頭 */
.map-popup::before {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 8px;
  height: 8px;
  background: var(--color-bg);
  border-right: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
}

.map-callout.popup-below .map-popup::before {
  bottom: auto;
  top: -5px;
  transform: translateX(-50%) rotate(225deg);
}

/* --- Popup 內容 --- */
.map-popup__head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-border-light);
}

.map-popup__head img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  object-position: top;
  border: 1.5px solid var(--color-border-light);
  flex-shrink: 0;
}

.map-popup__name {
  font-size: var(--text-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  white-space: nowrap;
}

.map-popup__note {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}

.map-popup__meta {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.map-popup__location {
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
}

.map-popup__tz-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.map-popup__tz-diff {
  font-size: var(--text-xs);
  font-weight: var(--font-weight-medium);
  padding: 1px var(--space-2);
  border-radius: var(--radius-pill);
}

.map-popup__tz-diff.is-same {
  background: color-mix(in srgb, var(--color-text-muted) 12%, transparent);
  color: var(--color-text-muted);
}

.map-popup__tz-diff.is-ahead {
  background: color-mix(in srgb, var(--color-coach) 15%, transparent);
  color: var(--color-coach);
}

.map-popup__tz-diff.is-behind {
  background: color-mix(in srgb, var(--color-lecturer) 15%, transparent);
  color: var(--color-lecturer);
}

/* ============================================================
   RWD
   ============================================================ */

@media (max-width: 768px) {
  #world-map-svg {
    height: 340px;
  }

  .map-avatar-item > img {
    width: 34px;
    height: 34px;
  }

  .map-popup {
    width: 170px;
  }
}
