:root{
  --bg: #05070d;
  --panel-bg: rgba(10, 14, 24, 0.72);
  --panel-border: rgba(212, 162, 78, 0.25);
  --gold: #e0a544;
  --blue: #5b8fd1;
  --purple: #9b6bcc;
  --text-main: #eae3d3;
  --text-dim: #9aa3b5;
  color-scheme: dark;
}

*{ box-sizing: border-box; }
[hidden]{ display: none !important; }

.sr-only{
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

button:focus-visible, input:focus-visible, #info-panel:focus-visible{
  outline: 2px solid var(--gold);
  outline-offset: 4px;
}

html, body{
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text-main);
  font-family: 'Inter', sans-serif;
  overflow: hidden;
}

#scene-container{
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}
#scene-container canvas{ display:block; }

#label-layer{
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  overflow: hidden;
}

.city-label{
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: rgba(180, 186, 200, 0.65);
  /* satellite imagery runs from snow to dark sea, so labels carry their
     own dark outline rather than relying on the terrain behind them */
  text-shadow:
    0 0 3px rgba(0,0,0,0.95), 0 0 6px rgba(0,0,0,0.85),
    1px 1px 2px rgba(0,0,0,0.95), -1px -1px 2px rgba(0,0,0,0.95);
  white-space: nowrap;
  transform: translate(9px, -5px);
  opacity: 0;
  transition: opacity 0.6s ease, color 0.6s ease, text-shadow 0.6s ease;
  pointer-events: none;
}
.city-label.reached{
  opacity: 1;
  color: #d9c294;
}
.city-label.current{
  opacity: 1;
  color: #ffffff;
  font-size: 13px;
  text-shadow:
    0 0 4px rgba(0,0,0,0.98), 0 0 9px rgba(0,0,0,0.9),
    0 0 14px rgba(224, 165, 68, 0.7),
    1px 1px 2px rgba(0,0,0,0.95), -1px -1px 2px rgba(0,0,0,0.95);
}

/* region names sit under the city labels in the visual hierarchy: wider,
   lighter, uppercase, the way a printed map sets a country against a town */
.region-label{
  font-family: 'Inter', sans-serif;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  white-space: nowrap;
  pointer-events: none;
  color: rgba(244, 231, 203, 0.82);
  text-shadow:
    0 0 3px rgba(0,0,0,0.95), 0 0 7px rgba(0,0,0,0.85),
    1px 1px 2px rgba(0,0,0,0.9), -1px -1px 2px rgba(0,0,0,0.9);
  transition: opacity 0.35s ease;
}
.region-label.modern{
  color: rgba(214, 226, 240, 0.8);
  letter-spacing: 0.16em;
}
.region-label.minor{
  font-size: 9.5px;
  color: rgba(238, 226, 200, 0.62);
}
.region-label.modern.minor{ color: rgba(206, 219, 234, 0.6); }

/* `visibility` rather than `display`: CSS2DRenderer rewrites display itself
   every frame, so anything set there is lost */
.region-label.off{ opacity: 0; visibility: hidden; }
.city-label.off, .population-map-label.off{ visibility: hidden; }
.region-label.behind,
.city-label.behind{ visibility: hidden; }

.legend-sub{
  margin: 12px 0 6px;
  font-family: 'Cinzel', serif;
  font-size: 12px;
  color: var(--gold);
  letter-spacing: 0.04em;
}
#era-switch{
  display: flex;
  gap: 4px;
}
#era-switch button{
  flex: 1;
  min-height: 36px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--panel-border);
  color: var(--text-dim);
  font-family: 'Inter', sans-serif;
  font-size: 10.5px;
  letter-spacing: 0.02em;
  padding: 5px 2px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
#era-switch button:hover{ background: rgba(224,165,68,0.16); color: var(--text-main); }
#era-switch button.active{
  background: rgba(224,165,68,0.26);
  color: #f6e9cd;
  border-color: rgba(224,165,68,0.5);
}

#app-header{
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 10;
  text-align: center;
  padding: 18px 16px 22px;
  pointer-events: none;
  background: linear-gradient(to bottom,
    rgba(5,7,13,0.92) 0%, rgba(5,7,13,0.78) 45%, rgba(5,7,13,0) 100%);
}
#app-header h1{
  font-family: 'Cinzel', serif;
  font-weight: 700;
  font-size: clamp(20px, 3.2vw, 32px);
  margin: 0;
  letter-spacing: 0.03em;
  color: var(--gold);
  text-shadow: 0 0 18px rgba(224,165,68,0.35);
}
#app-header p{
  margin: 4px 0 0;
  font-size: clamp(11px, 1.4vw, 14px);
  color: #c3ccdb;
  letter-spacing: 0.02em;
  text-shadow: 0 1px 4px rgba(0,0,0,0.95), 0 0 10px rgba(0,0,0,0.8);
}

#legend-toggle{
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 20;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  color: var(--text-main);
  font-family: 'Inter', sans-serif;
  font-size: 12px;
  padding: 8px 14px;
  border-radius: 20px;
  cursor: pointer;
  min-height: 44px;
  backdrop-filter: blur(8px);
  display: none;
}

#population-toggle{
  position: fixed;
  top: 20px;
  left: 18px;
  z-index: 20;
  min-height: 44px;
  padding: 9px 15px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 22px;
  color: var(--gold);
  font: 500 12px 'Inter', sans-serif;
  cursor: pointer;
  backdrop-filter: blur(10px);
}
#population-toggle:hover, #population-toggle[aria-pressed="true"]{ background: #3b2d19; }
#population-toggle:disabled{ opacity: 0.5; cursor: default; }

#population-panel{
  position: fixed;
  top: 90px;
  bottom: 24px;
  left: 18px;
  z-index: 15;
  width: 340px;
  max-width: calc(100vw - 36px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: rgba(10, 14, 24, 0.9);
  backdrop-filter: blur(12px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}
.population-eyebrow{ color: var(--gold); font-size: 11px; line-height: 1.5; }
#population-title{ font: 700 19px 'Cinzel', serif; margin: 7px 0; }
.population-intro{ color: var(--text-dim); font-size: 12px; line-height: 1.5; margin: 0 0 14px; }
#population-detail{ border-block: 1px solid var(--panel-border); padding: 12px 0; margin-bottom: 16px; }
#population-place{ font-size: 15px; margin: 0; }
#population-count{ font-size: 26px; font-weight: 600; color: var(--gold); margin: 5px 0; font-variant-numeric: tabular-nums; }
#population-share{ font-size: 12px; color: var(--text-dim); margin: 0; line-height: 1.5; }
#population-panel label{ display: block; font-size: 12px; margin-bottom: 7px; }
#population-search{
  width: 100%; min-height: 44px; border: 1px solid var(--panel-border);
  border-radius: 7px; background: #0a101b; color: var(--text-main); padding: 10px; font: inherit; font-size: 13px;
}
#population-results{ font-size: 10.5px; color: var(--text-dim); line-height: 1.5; margin: 8px 0; }
#population-list{ max-height: 260px; overflow-y: auto; overscroll-behavior: contain; padding: 4px; }
.population-row{
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  width: 100%; min-height: 46px; padding: 9px 7px; border: 0; border-bottom: 1px solid rgba(255,255,255,0.07);
  background: transparent; color: var(--text-main); font: 12px 'Inter', sans-serif; text-align: left; cursor: pointer;
}
.population-row:hover, .population-row.selected{ background: rgba(224,165,68,0.14); }
.population-row-value{ flex: none; color: var(--gold); font-size: 10.5px; font-variant-numeric: tabular-nums; }
.population-note, .population-source{ font-size: 10.5px; line-height: 1.6; color: var(--text-dim); margin: 12px 0 0; }
.population-source a{ color: var(--gold); text-underline-offset: 3px; }
.population-map-label{
  max-width: 210px; padding: 6px 9px; border-radius: 6px;
  border: 1px solid var(--panel-border); background: rgba(5,7,13,0.88);
  color: #fff1d3; font: 600 11px 'Inter', sans-serif; text-align: center;
  pointer-events: none;
}

#legend{
  position: fixed;
  top: 70px;
  right: 18px;
  z-index: 15;
  width: 230px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px 16px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
}
#legend h2{
  margin: 0 0 8px;
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: var(--gold);
  letter-spacing: 0.04em;
}
.legend-row{
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-main);
  margin: 6px 0;
}
.legend-row .dot{
  width: 10px; height: 10px;
  border-radius: 50%;
  flex: none;
  box-shadow: 0 0 6px currentColor;
}
.legend-note{
  margin: 10px 0 0;
  font-size: 11px;
  line-height: 1.5;
  color: var(--text-dim);
}

#info-panel{
  position: fixed;
  left: 18px;
  top: 90px;
  bottom: 100px;
  z-index: 15;
  width: 320px;
  max-width: calc(100vw - 36px);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 18px 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  overflow-y: auto;
}
.info-badge{
  display: inline-block;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 3px 10px;
  border-radius: 20px;
  margin-bottom: 10px;
}
.info-step{
  float: right;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
}
#info-place{
  font-family: 'Cinzel', serif;
  margin: 4px 0 0;
  font-size: 22px;
  color: var(--text-main);
  clear: both;
}
#info-subtitle{
  margin: 2px 0 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--gold);
}
.info-verse{
  margin: 0 0 10px;
  font-size: 12px;
  font-style: italic;
  color: var(--text-dim);
}
.info-text{
  margin: 0 0 14px;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text-main);
}
.info-believers{
  font-size: 12.5px;
  color: var(--text-dim);
  border-top: 1px solid var(--panel-border);
  padding-top: 10px;
}
.info-believers .label{ color: var(--text-main); font-weight: 600; }

#timeline-bar{
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 30px;
  padding: 10px 18px;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  width: min(720px, calc(100vw - 24px));
}
#timeline-bar button{
  background: rgba(224,165,68,0.12);
  border: 1px solid var(--panel-border);
  color: var(--gold);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 12px;
  flex: none;
  transition: background 0.2s ease;
}
#timeline-bar button:hover{ background: rgba(224,165,68,0.28); }
#timeline-bar button:disabled{ opacity: 0.35; cursor: default; }
#timeline-bar #btn-play{ font-size: 14px; }

#timeline-slider{
  flex: 1;
  min-width: 0;
  height: 44px;
  margin: 0;
  accent-color: var(--gold);
  cursor: pointer;
}
#timeline-label{
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  min-width: 90px;
  text-align: center;
}

#loading{
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 24px;
  text-align: center;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--text-dim);
  font-size: 14px;
  letter-spacing: 0.03em;
  transition: opacity 0.6s ease;
}
#loading p{ max-width: 36em; margin: 0; line-height: 1.6; }
#btn-retry{
  min-height: 44px;
  padding: 10px 20px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: rgba(224,165,68,0.12);
  color: var(--gold);
  font: inherit;
  cursor: pointer;
}
#loading.hidden{ opacity: 0; pointer-events: none; }

@media (min-width: 761px) and (max-width: 1100px){
  #population-toggle{ top: auto; bottom: 98px; left: auto; right: 18px; }
}

@media (max-width: 760px){
  /* the account reads first, so it sits directly under the title and the
     globe takes the room that is left */
  #app-header{
    padding: 10px 98px 12px 12px;   /* room for the key button on the right */
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    text-align: left;
  }
  #app-header h1{ font-size: 16px; letter-spacing: 0.02em; }
  #app-header p{ display: none; }

  #legend-toggle{
    display: block;
    top: calc(10px + env(safe-area-inset-top, 0px));
    right: 10px;
    padding: 7px 12px;
  }

  #population-toggle{
    top: auto; left: 8px;
    bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    min-height: 44px;
  }
  #population-toggle[aria-pressed="true"]{ bottom: calc(14px + env(safe-area-inset-bottom, 0px)); }
  #population-panel{
    top: calc(62px + env(safe-area-inset-top, 0px)); bottom: auto;
    left: 8px; right: 8px; width: auto; max-width: none;
    max-height: 46vh; padding: 14px;
  }
  #population-count{ font-size: 23px; }
  #population-list{ max-height: 170px; }

  #info-panel{
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;   /* the desktop rule clamps this to 100vw - 36px */
    top: calc(62px + env(safe-area-inset-top, 0px));
    bottom: auto;
    max-height: 33vh;
    padding: 12px 14px;
    border-radius: 12px;
    overscroll-behavior: contain;
  }
  .info-badge{ font-size: 9.5px; padding: 2px 8px; margin-bottom: 6px; }
  .info-step{ font-size: 10.5px; }
  #info-place{ font-size: 18px; }
  #info-subtitle{ font-size: 12px; margin-bottom: 8px; }
  .info-verse{ font-size: 11px; margin-bottom: 7px; }
  .info-text{ font-size: 12.5px; line-height: 1.55; margin-bottom: 10px; }
  .info-believers{ font-size: 11.5px; padding-top: 8px; }

  #legend{
    display: none;
    top: auto;
    /* clears the population button below it, which is 44px tall at 88px up */
    bottom: calc(142px + env(safe-area-inset-bottom, 0px));
    right: 8px;
    left: 8px;
    width: auto;
    max-height: calc(100vh - 212px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  #legend.open{ display: block; }
  #era-switch button{ min-height: 44px; padding: 8px 2px; font-size: 11px; }

  #timeline-bar{
    padding: 8px 10px;
    gap: 6px;
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    width: calc(100vw - 16px);
  }
  #timeline-label{ display: none; }
  .region-label{ font-size: 9.5px; letter-spacing: 0.16em; }
  .region-label.minor{ font-size: 8.5px; }
}

/* a phone held sideways has almost no vertical room to give away */
@media (max-height: 520px){
  #app-header{ padding-top: 8px; padding-bottom: 8px; }
  #app-header h1{ font-size: 15px; }
  #app-header p{ display: none; }
  #info-panel{
    top: calc(62px + env(safe-area-inset-top, 0px));
    bottom: auto;
    max-height: calc(100vh - 156px - env(safe-area-inset-top, 0px));
    padding: 12px 14px;
  }
  /* stop short of the population button, which sits bottom-right on a screen
     this shape */
  #legend{
    top: calc(62px + env(safe-area-inset-top, 0px)); bottom: auto;
    max-height: calc(100vh - 214px - env(safe-area-inset-top, 0px));
    overflow-y: auto;
  }
  #timeline-bar{ bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  #population-panel{ top: 45px; bottom: 70px; max-height: none; width: 300px; right: auto; }
  #population-toggle{ top: auto; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); }
  #population-toggle[aria-pressed="true"]{ bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
}

@media (prefers-reduced-motion: reduce){
  *, *::before, *::after{ transition: none !important; }
}
