:root {
  --bg-color: #0b0f19;
  --panel-bg: rgba(17, 24, 39, 0.85);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --accent-color: #3b82f6;
  --accent-hover: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.3);
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  
  /* Continent colors */
  --c-afrique: #f59e0b;
  --c-ameriques: #ef4444;
  --c-asie: #10b981;
  --c-europe: #38bdf8;
  --c-oceanie: #a855f7;
  --c-autre: #94a3b8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background-color: var(--bg-color);
  color: var(--text-main);
  font-family: var(--font-family);
  user-select: none;
  display: flex;
  flex-direction: column;
}

/* Glassmorphism panel base */
.glass-panel {
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--panel-border);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.45);
}

/* Top Controls Panel (2 Columns) */
.top-controls-panel {
  width: 100%;
  flex-shrink: 0;
  z-index: 10;
  padding: 10px 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  align-items: start;
  border-radius: 0;
  border-top: none;
  border-left: none;
  border-right: none;
  border-bottom: 1px solid var(--panel-border);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.panel-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.app-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

h1.app-title {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 8px;
}

p.app-subtitle {
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-top: 1px;
  line-height: 1.3;
}

.btn-icon {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text-main);
  border-radius: 8px;
  min-width: 30px;
  height: 30px;
  padding: 0 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 0.9rem;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.18);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
}

.btn-lang {
  font-weight: 700;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  color: #93c5fd;
  width: 30px;
  padding: 0;
}

/* Three.js Canvas Container - placed below */
#canvas-container {
  width: 100%;
  flex: 1;
  min-height: 0;
  position: relative;
  z-index: 1;
  touch-action: none;
  overflow: hidden;
}

#canvas-container canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
  touch-action: none;
  cursor: grab;
}

#canvas-container canvas:active {
  cursor: grabbing;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.control-group label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Custom Select & Inputs */
select.custom-select, input.custom-input {
  width: 100%;
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: var(--text-main);
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.2s;
}

select.custom-select:focus, input.custom-input:focus {
  border-color: var(--accent-color);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Selected Countries Tags */
.selected-tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
  margin-top: 2px;
}

.country-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(59, 130, 246, 0.2);
  border: 1px solid rgba(59, 130, 246, 0.45);
  color: #fff;
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 0.72rem;
  font-weight: 500;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}

.tag-remove {
  background: none;
  border: none;
  color: #93c5fd;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 0 1px;
  transition: color 0.15s;
}

.tag-remove:hover {
  color: #ef4444;
}

.btn-clear-tags {
  background: transparent;
  border: 1px dashed rgba(255, 255, 255, 0.2);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.68rem;
  padding: 3px 6px;
  border-radius: 6px;
  transition: all 0.15s;
}

.btn-clear-tags:hover {
  border-color: rgba(239, 68, 68, 0.5);
  color: #fca5a5;
}

/* Segmented Control Buttons */
.segmented-control {
  display: flex;
  background: rgba(0, 0, 0, 0.35);
  padding: 3px;
  border-radius: 8px;
  gap: 3px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.segmented-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 6px 4px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
}

.segmented-btn:hover {
  color: #fff;
}

.segmented-btn.active {
  background: var(--accent-color);
  color: #fff;
  font-weight: 600;
  box-shadow: 0 2px 8px var(--accent-glow);
}

/* Year Timeline Slider */
.timeline-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-play {
  background: var(--accent-color);
  border: none;
  color: #fff;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s;
}

.btn-play:hover {
  background: var(--accent-hover);
  transform: scale(1.06);
}

.year-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.2);
  outline: none;
}

.year-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-color);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.5);
}

.current-year-badge {
  font-size: 0.9rem;
  font-weight: 700;
  color: #fff;
  min-width: 40px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* View presets */
.views-row {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.view-btn {
  flex: 1 1 0;
  min-width: 0;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
  font-size: 0.70rem;
  padding: 4px 2px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: center;
  white-space: nowrap;
  letter-spacing: -0.01em;
}

.view-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}

.view-btn.active {
  background: rgba(59, 130, 246, 0.25);
  border-color: var(--accent-color);
  color: #fff;
  font-weight: 600;
}

/* Continent Filter Toggles */
.continent-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.continent-pill {
  flex: 1 1 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  font-size: 0.72rem;
  padding: 4px 8px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.2s;
  white-space: nowrap;
}

.continent-pill:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.continent-pill.active {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-color: currentColor;
}

.pill-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

/* Axis Badges */
.axis-badge {
  font-weight: 700;
  font-size: 0.7rem;
  padding: 2px 7px;
  border-radius: 4px;
  min-width: 24px;
  text-align: center;
  flex-shrink: 0;
}

.axis-badge.x { background: #ef4444; color: #fff; }
.axis-badge.y { background: #10b981; color: #fff; }
.axis-badge.z { background: #1d4ed8; color: #fff; }

/* Floating Tooltip */
#tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.92);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.16);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
  min-width: 220px;
  max-width: 320px;
  opacity: 0;
  transform: translate(-50%, -115%);
  transition: opacity 0.15s ease;
}

#tooltip.visible {
  opacity: 1;
}

.tooltip-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 5px;
}

.tooltip-flag {
  font-size: 1.25rem;
}

.tooltip-title {
  font-size: 0.9rem;
  font-weight: 700;
  color: #fff;
  flex: 1;
}

.tooltip-continent {
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

.tooltip-stats {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.75rem;
}

.tooltip-stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.tooltip-stat-label {
  color: var(--text-muted);
}

.tooltip-stat-val {
  font-weight: 600;
  color: #fff;
  font-variant-numeric: tabular-nums;
}

/* Info Modal */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 20px;
}

.modal-overlay.open {
  display: flex;
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  width: 100%;
  max-width: 580px;
  padding: 24px;
  position: relative;
  max-height: 85vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-size: 1.25rem;
  font-weight: 700;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
}

.modal-close:hover {
  color: #fff;
}

.modal-body {
  font-size: 0.85rem;
  line-height: 1.6;
  color: #d1d5db;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.formula-box {
  background: rgba(0, 0, 0, 0.4);
  border-left: 4px solid var(--accent-color);
  padding: 12px 16px;
  border-radius: 4px 8px 8px 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.95rem;
  color: #93c5fd;
  text-align: center;
  margin: 6px 0;
}

.formula-components {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.formula-comp-item {
  display: flex;
  gap: 10px;
  align-items: baseline;
}

.formula-comp-lead {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 120px;
  flex-shrink: 0;
}

.formula-comp-lead b {
  color: #fff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
}

.axis-badge-empty {
  width: 24px;
  display: inline-block;
  flex-shrink: 0;
}

.modal-footer-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
  margin: 6px 0 2px 0;
}

.modal-footer-links {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  font-size: 0.85rem;
}

.modal-footer-links a,
.modal-copyright a {
  color: #60a5fa;
  text-decoration: underline;
  text-underline-offset: 3px;
  transition: color 0.15s ease;
}

.modal-footer-links a:hover,
.modal-copyright a:hover {
  color: #93c5fd;
}

.modal-copyright {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
  margin-top: 4px;
}

/* Embed Mode overrides */
body.is-embedded .top-controls-panel {
  padding: 6px 14px;
  gap: 14px;
}

body.is-embedded h1.app-title {
  font-size: 0.95rem;
}

/* Responsive / Compact layout */

/* From 850px down to 401px: keep 2 columns side-by-side, stacking buttons progressively */
@media (max-width: 850px) {
  .top-controls-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    padding: 8px 12px;
    max-height: 50vh;
    overflow-y: auto;
    scrollbar-width: thin;
  }

  .top-controls-panel::-webkit-scrollbar {
    width: 4px;
  }

  .top-controls-panel::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
  }

  h1.app-title {
    font-size: 1rem;
  }

  p.app-subtitle {
    display: none;
  }

  .panel-col {
    gap: 6px;
  }

  /* Projection buttons remain on 1 row */
  .views-row {
    gap: 3px;
  }

  .views-row .view-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 4px 2px;
    font-size: 0.68rem;
  }

  /* Stack continent buttons 3 + 2 */
  .continent-pills .continent-pill {
    flex: 1 1 calc(33.333% - 4px);
  }
}

/* Down to ~400px: stack buttons maximally (2x2) and optimize compact spacing so both columns stay side-by-side */
@media (max-width: 600px) {
  .top-controls-panel {
    gap: 8px;
    padding: 6px 8px;
  }

  h1.app-title {
    font-size: 0.85rem;
    line-height: 1.15;
  }

  .btn-icon {
    min-width: 25px;
    width: 25px;
    height: 25px;
    font-size: 0.75rem;
    border-radius: 6px;
  }

  .btn-lang {
    width: 25px;
    font-size: 0.68rem;
  }

  .control-group label {
    font-size: 0.66rem;
    margin-bottom: 1px;
  }

  select.custom-select, input.custom-input {
    font-size: 0.72rem;
    padding: 4px 6px;
    border-radius: 6px;
  }

  .timeline-container {
    gap: 6px;
  }

  .btn-play {
    width: 26px;
    height: 26px;
    font-size: 0.7rem;
  }

  .current-year-badge {
    font-size: 0.78rem;
  }

  /* Continent pills stacked 2 per row */
  .continent-pills .continent-pill {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    padding: 3px 5px;
    font-size: 0.68rem;
    gap: 4px;
  }

  .continent-pill .pill-dot {
    width: 6px;
    height: 6px;
  }

  .continent-pill span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Views buttons compact on a single line */
  .views-row {
    gap: 2px;
  }

  .views-row .view-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 3px 1px;
    font-size: 0.64rem;
    letter-spacing: -0.02em;
  }
}

/* Below ~400px: only now stack one column under the other */
@media (max-width: 400px) {
  .top-controls-panel {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 8px 10px;
    max-height: 55vh;
  }

  .continent-pills .continent-pill {
    flex: 1 1 calc(50% - 4px);
  }

  .views-row {
    gap: 3px;
  }

  .views-row .view-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 4px 2px;
    font-size: 0.68rem;
  }
}
