/* ===== Reset & Variables ===== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0a0e1a;
  --panel-bg: rgba(12, 16, 30, 0.92);
  --text: #eaeaf0;
  --text-dim: #8888aa;
  --accent: #FFD700;
  --glow-cyan: rgba(0, 200, 255, 0.08);
  --glow-cyan-strong: rgba(0, 200, 255, 0.15);
  --border: rgba(100, 180, 255, 0.1);
  --radius: 16px;
  --transition: 0.35s cubic-bezier(0.25, 1, 0.5, 1);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: rgba(0, 200, 255, 0.25);
  color: #fff;
}

/* ===== Map ===== */
#map {
  position: fixed;
  inset: 0;
  z-index: 0;
}

.mapboxgl-ctrl-bottom-right { right: 8px; bottom: 8px; }
.mapboxgl-ctrl-group {
  background: var(--panel-bg) !important;
  border: 1px solid var(--border) !important;
  box-shadow: 0 0 12px var(--glow-cyan) !important;
}
.mapboxgl-ctrl-group button { color: var(--text) !important; }

/* ===== Search Bar ===== */
.search-bar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 10;
  max-width: 400px;
}

.search-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 0 20px var(--glow-cyan), inset 0 1px 0 rgba(255,255,255,0.04);
  transition: box-shadow 0.3s, border-color 0.3s;
}
.search-inner:focus-within {
  border-color: rgba(0, 200, 255, 0.25);
  box-shadow: 0 0 30px var(--glow-cyan-strong), inset 0 1px 0 rgba(255,255,255,0.06);
}

.search-icon { color: var(--text-dim); flex-shrink: 0; }

#search-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  outline: none;
}
#search-input::placeholder { color: var(--text-dim); }

.search-results {
  display: none;
  margin-top: 6px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-height: 300px;
  overflow-y: auto;
  backdrop-filter: blur(20px);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4), 0 0 16px var(--glow-cyan);
}
.search-results.visible { display: block; }

.search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}
.search-item:last-child { border-bottom: none; }
.search-item:hover, .search-item:active { background: rgba(0, 200, 255, 0.05); }

.search-item-color {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.search-item-name { flex: 1; font-size: 14px; }
.search-item-name small { color: var(--text-dim); font-size: 12px; margin-left: 6px; }
.search-item-mbti { font-size: 13px; font-weight: 600; color: var(--accent); }

/* ===== Tooltip ===== */
.map-tooltip {
  position: fixed;
  z-index: 20;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  backdrop-filter: blur(16px);
  max-width: 240px;
  font-size: 13px;
  box-shadow: 0 0 16px var(--glow-cyan);
}
.map-tooltip.visible { opacity: 1; }

/* Country flags */
.country-flag {
  width: 24px; height: 18px;
  border-radius: 2px;
  vertical-align: middle;
  object-fit: cover;
  flex-shrink: 0;
}
.country-flag.flag-lg {
  width: 48px; height: 36px;
  border-radius: 4px;
}

.tooltip-name { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.tooltip-mbti {
  display: flex; align-items: center; gap: 6px;
  margin-top: 4px; font-weight: 600;
}

/* ===== Legend ===== */
.legend {
  position: fixed;
  bottom: 12px;
  left: 12px;
  z-index: 10;
  background: linear-gradient(180deg, rgba(14, 20, 36, 0.95) 0%, rgba(10, 14, 26, 0.95) 100%);
  border: 1px solid var(--border);
  border-radius: 12px;
  backdrop-filter: blur(20px);
  overflow: hidden;
  transition: max-height var(--transition);
  max-height: 44px;
  box-shadow: 0 0 16px var(--glow-cyan);
}
.legend.expanded { max-height: 80vh; overflow-y: auto; }

.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  cursor: pointer;
  user-select: none;
}
.legend-title { font-size: 13px; font-weight: 600; color: var(--text-dim); }
.legend-arrow { color: var(--text-dim); transition: transform 0.3s; }
.legend.expanded .legend-arrow { transform: rotate(180deg); }

.legend-body { padding: 0 14px 14px; }

.legend-group { margin-bottom: 10px; }
.legend-group-name { font-size: 11px; color: var(--text-dim); margin-bottom: 4px; font-weight: 500; }

.legend-item {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 6px; border-radius: 6px;
  cursor: pointer; transition: background 0.15s, box-shadow 0.2s;
  font-size: 12px;
}
.legend-item:hover {
  background: rgba(0, 200, 255, 0.06);
  box-shadow: 0 0 8px var(--glow-cyan);
}
.legend-item.active {
  background: rgba(255,215,0,0.12);
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.15);
}

.legend-character {
  width: 22px; height: 22px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.legend-color { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.legend-type { font-weight: 600; font-size: 11px; min-width: 35px; }
.legend-name { color: var(--text-dim); font-size: 11px; }

/* ===== Info Panel ===== */
.info-panel {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 15;
  background: linear-gradient(180deg, rgba(14, 20, 36, 0.96) 0%, rgba(10, 14, 26, 0.98) 100%);
  border-radius: var(--radius) var(--radius) 0 0;
  border-top: 1px solid var(--border);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  transform: translateY(100%);
  transition: transform var(--transition);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.4), 0 0 20px var(--glow-cyan);
}
.info-panel.visible { transform: translateY(0); }
.info-panel.peek { transform: translateY(calc(100% - 80px)); }
.info-panel.half { transform: translateY(50%); }

.panel-handle {
  display: flex;
  justify-content: center;
  padding: 10px 0 6px;
  cursor: grab;
  touch-action: none;
}
.panel-handle span {
  width: 36px; height: 4px;
  background: rgba(255,255,255,0.2);
  border-radius: 2px;
}

.panel-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 20px 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
}

/* Panel content */
.panel-back {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: 8px; color: var(--text);
  padding: 8px 14px; cursor: pointer;
  font-size: 13px; margin-bottom: 16px;
  transition: all 0.15s;
}
.panel-back:hover { background: rgba(0, 200, 255, 0.08); border-color: rgba(0, 200, 255, 0.3); }

/* Country header with character */
.panel-country-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
}

.panel-character {
  width: 80px;
  height: 80px;
  border-radius: 12px;
  object-fit: cover;
  flex-shrink: 0;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
}

.panel-country-info { flex: 1; }

.panel-country-name { font-size: 22px; font-weight: 700; display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }

.panel-mbti-badge {
  display: inline-flex; align-items: center; gap: 10px;
  padding: 8px 16px; border-radius: 20px;
  color: #fff; font-weight: 600;
  box-shadow: 0 0 16px rgba(255,255,255,0.1);
}
.panel-mbti-badge .mbti-avatar svg { width: 32px; height: 32px; }
.panel-mbti-type { font-size: 18px; }
.panel-mbti-name { font-size: 13px; opacity: 0.9; }

/* Type header with large character */
.panel-type-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.panel-character-large {
  width: 120px;
  height: 120px;
  border-radius: 16px;
  object-fit: cover;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  box-shadow: 0 0 24px var(--glow-cyan);
}

.panel-mbti-badge-large {
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 16px;
}

/* Country list in type view */
.panel-country-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.country-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.2s;
  border: 1px solid transparent;
}
.country-list-item:hover {
  background: rgba(0, 200, 255, 0.05);
  border-color: rgba(0, 200, 255, 0.1);
}
.country-list-item.active {
  background: rgba(255, 215, 0, 0.08);
  border-color: rgba(255, 215, 0, 0.2);
  box-shadow: 0 0 12px rgba(255, 215, 0, 0.1);
}

.country-list-name {
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}

.country-list-arrow {
  color: var(--text-dim);
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s;
}
.country-list-item:hover .country-list-arrow,
.country-list-item.active .country-list-arrow { opacity: 1; }

.panel-section { margin-bottom: 16px; }
.panel-section-title {
  font-size: 12px; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 1px;
  margin-bottom: 8px; font-weight: 600;
}
.panel-section p { font-size: 14px; line-height: 1.65; }

.panel-confidence {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-dim); margin-bottom: 16px;
}
.confidence-stars { color: var(--accent); font-size: 14px; letter-spacing: 1px; }

.dim-row {
  display: flex; gap: 8px; padding: 8px 10px;
  border-radius: 8px; margin-bottom: 4px;
  font-size: 13px; line-height: 1.5;
  background: rgba(255,255,255,0.02);
}
.dim-row.active {
  background: rgba(255,215,0,0.06);
  border-left: 3px solid var(--accent);
}
.dim-label { font-weight: 600; color: var(--accent); min-width: 56px; flex-shrink: 0; }
.dim-text { color: var(--text-dim); font-size: 12px; }

.panel-sources ul { list-style: none; }
.panel-sources li {
  font-size: 12px; color: var(--text-dim);
  padding: 4px 0 4px 16px; position: relative; line-height: 1.5;
}
.panel-sources li::before {
  content: ''; position: absolute; left: 4px; top: 11px;
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--accent);
}

/* ===== Chat Window ===== */
.chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 25;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6C1D80, #2EBE6E);
  border: 1px solid rgba(255,255,255,0.15);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4), 0 0 20px rgba(0, 200, 255, 0.15);
  transition: transform 0.2s, box-shadow 0.2s;
}
.chat-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5), 0 0 30px rgba(0, 200, 255, 0.25);
}
.chat-fab svg { width: 24px; height: 24px; }

.chat-window {
  position: fixed;
  bottom: 88px;
  right: 24px;
  z-index: 25;
  width: 380px;
  max-height: 520px;
  background: linear-gradient(180deg, rgba(14, 20, 36, 0.98) 0%, rgba(10, 14, 26, 0.99) 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  backdrop-filter: blur(24px);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5), 0 0 24px var(--glow-cyan);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.chat-window.open { display: flex; }

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(108, 29, 128, 0.15), rgba(0, 200, 255, 0.08));
}
.chat-header-title {
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.chat-header-title .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #2EBE6E;
  box-shadow: 0 0 6px rgba(46, 190, 110, 0.5);
}
.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.chat-settings-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  transition: color 0.2s, transform 0.2s;
  opacity: 0.5;
}
.chat-settings-btn:hover { color: var(--text); opacity: 1; transform: rotate(45deg); }
.chat-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  transition: color 0.15s;
}
.chat-close:hover { color: var(--text); }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 300px;
  max-height: 380px;
}

.chat-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.55;
  word-break: break-word;
}
.chat-msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, rgba(0, 200, 255, 0.15), rgba(0, 150, 255, 0.1));
  border: 1px solid rgba(0, 200, 255, 0.2);
  color: var(--text);
}
.chat-msg.ai {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  color: var(--text);
}
.chat-msg.error {
  align-self: center;
  background: rgba(255, 80, 80, 0.1);
  border: 1px solid rgba(255, 80, 80, 0.2);
  color: #ff8888;
  font-size: 12px;
  text-align: center;
}

.chat-typing {
  align-self: flex-start;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  gap: 4px;
  align-items: center;
}
.chat-typing span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: chatBounce 1.2s ease-in-out infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes chatBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

.chat-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.15);
}
.chat-input-area input {
  flex: 1;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--text);
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s;
}
.chat-input-area input::placeholder { color: var(--text-dim); }
.chat-input-area input:focus { border-color: rgba(0, 200, 255, 0.3); }

.chat-send {
  background: linear-gradient(135deg, rgba(0, 200, 255, 0.2), rgba(0, 150, 255, 0.15));
  border: 1px solid rgba(0, 200, 255, 0.2);
  border-radius: 8px;
  color: #fff;
  padding: 8px 12px;
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}
.chat-send:hover {
  background: linear-gradient(135deg, rgba(0, 200, 255, 0.3), rgba(0, 150, 255, 0.25));
  box-shadow: 0 0 10px rgba(0, 200, 255, 0.15);
}

/* -- Provider Setup -- */
.chat-setup {
  padding: 4px 0;
}
.chat-setup-title {
  font-size: 15px;
  font-weight: 700;
  text-align: center;
  margin-bottom: 4px;
  color: var(--text);
}
.chat-setup-subtitle {
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 12px;
  line-height: 1.5;
}
.chat-setup-subtitle strong { color: var(--text); }

.chat-provider-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}
.chat-provider-card {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}
.chat-provider-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 3px; height: 100%;
  background: var(--prov-color, #888);
  opacity: 0.4;
  transition: opacity 0.2s, width 0.2s;
}
.chat-provider-card:hover {
  border-color: rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.06);
}
.chat-provider-card:hover::before { opacity: 0.8; }
.chat-provider-card.selected {
  border-color: var(--prov-color, #888);
  background: rgba(255,255,255,0.06);
  box-shadow: 0 0 12px rgba(0,0,0,0.2), inset 0 0 20px rgba(255,255,255,0.02);
}
.chat-provider-card.selected::before { opacity: 1; width: 4px; }

.chat-provider-card-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}
.chat-provider-card-desc {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.chat-provider-card-link {
  font-size: 10px;
  color: rgba(0, 200, 255, 0.7);
  text-decoration: none;
  transition: color 0.15s;
}
.chat-provider-card-link:hover { color: rgba(0, 200, 255, 1); text-decoration: underline; }

.chat-setup-input {
  width: 100%;
  padding: 9px 12px;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255,255,255,0.05);
  color: var(--text);
  font-size: 12px;
  outline: none;
  box-sizing: border-box;
  transition: border-color 0.2s;
}
.chat-setup-input:focus { border-color: rgba(0, 200, 255, 0.3); }
.chat-setup-input.shake {
  animation: chatShake 0.4s ease-in-out;
  border-color: rgba(255, 100, 100, 0.5);
}

@keyframes chatShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(6px); }
  75% { transform: translateX(-4px); }
}

.chat-setup-save {
  padding: 8px 18px;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, rgba(108, 29, 128, 0.5), rgba(46, 190, 110, 0.4));
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
}
.chat-setup-save:hover {
  box-shadow: 0 0 14px rgba(0, 200, 255, 0.15);
  filter: brightness(1.15);
}

/* -- Suggestion Chips -- */
.chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 2px 0 4px;
}
.chat-chip {
  padding: 6px 12px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  line-height: 1.3;
}
.chat-chip:hover {
  background: rgba(0, 200, 255, 0.08);
  border-color: rgba(0, 200, 255, 0.25);
  color: var(--text);
}

/* -- Markdown in AI messages -- */
.chat-msg.ai strong {
  color: rgba(0, 220, 255, 0.9);
  font-weight: 600;
}
.chat-msg.ai em {
  font-style: italic;
  color: rgba(255, 255, 255, 0.85);
}
.chat-msg.ai ul {
  margin: 4px 0;
  padding-left: 18px;
}
.chat-msg.ai li {
  margin-bottom: 2px;
  line-height: 1.5;
}
.chat-msg.ai br + br {
  display: block;
  content: '';
  margin-top: 4px;
}

/* ===== Tablet (641-1024px) ===== */
@media (min-width: 641px) {
  .search-bar { left: 16px; right: auto; width: 360px; }

  .legend { max-height: none; max-width: 220px; }
  .legend-arrow { display: none; }
  .legend-body { display: block !important; }

  .info-panel {
    left: auto;
    right: 0;
    width: 380px;
    max-height: 100vh;
    border-radius: var(--radius) 0 0 var(--radius);
    border-top: none;
    border-left: 1px solid var(--border);
    transform: translateX(100%);
  }
  .info-panel.visible { transform: translateX(0); }
  .info-panel.peek, .info-panel.half { transform: translateX(0); }

  .panel-handle { display: none; }

  .chat-window { right: 400px; }
}

/* ===== Desktop (> 1024px) ===== */
@media (min-width: 1025px) {
  .search-bar { width: 400px; top: 16px; left: 16px; }
  .info-panel { width: 420px; }
  .legend { bottom: 20px; left: 20px; }
  .chat-window { right: 440px; }
}

/* ===== Mobile ===== */
@media (max-width: 640px) {
  .chat-fab { bottom: 16px; right: 16px; width: 46px; height: 46px; }
  .chat-window {
    bottom: 0; right: 0; left: 0;
    width: 100%;
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
  }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 4px; }

/* ===== Utility ===== */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); border: 0;
}
