:root {
  --bg-sky-top: #cfeefb;
  --bg-sky-bot: #3a7fb8;
  --paper: #fdf8ea;
  --paper-edge: #e6d6a3;
  --ink: #1f2c46;
  --ink-soft: #4e5d7d;
  --ink-faint: #8893ad;
  --primary: #ff7a59;
  --primary-dark: #d94d2a;
  --gold: #ffce4a;
  --gold-deep: #d49a16;
  --accent: #ffd84d;
  --tint-card: #fff8e3;
  --card-shadow: 0 18px 60px rgba(10, 30, 70, 0.28);
  --safe-top: env(safe-area-inset-top);
  --safe-bot: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg-sky-bot);
  font-family: "Nunito", "Quicksand", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

input, button { font-family: inherit; }

#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: linear-gradient(180deg, #fdf8ea 0%, #f3e9c6 100%);
}

/* ───────── App bar ───────── */
.app-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 16px;
  padding-top: calc(6px + var(--safe-top));
  background: linear-gradient(180deg, #fff8e3 0%, #f8eecc 100%);
  border-bottom: 2px solid rgba(214, 197, 151, 0.7);
  box-shadow: 0 4px 16px rgba(20, 50, 80, 0.10);
  z-index: 3;
}
.app-bar-compass {
  display: inline-flex;
  flex-shrink: 0;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.18));
  animation: compassFloat 6s ease-in-out infinite;
}
@keyframes compassFloat {
  0%, 100% { transform: translateY(0) rotate(0); }
  50%      { transform: translateY(-3px) rotate(-4deg); }
}
.app-bar-text { display: flex; flex-direction: column; line-height: 1.1; }
.app-bar-title {
  margin: 0;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.2px;
  color: var(--ink);
  font-family: "Fredoka", "Nunito", sans-serif;
  text-shadow: 0 1px 0 #fff;
}
.app-bar-subtitle {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-soft);
}

/* ───────── Map stage ───────── */
.map-stage {
  position: relative;
  overflow: hidden;
  background: linear-gradient(180deg, #cfeefb 0%, #8fcdec 60%, #4f9bcc 100%);
}

#map {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: pan-x pan-y;
}

#oceanWavesLayer {
  animation: waveDrift 26s linear infinite;
  opacity: 0.85;
}
@keyframes waveDrift {
  0%   { transform: translate(0, 0); }
  100% { transform: translate(-160px, 0); }
}

/* Country interactions */
#countryLayer .country {
  cursor: pointer;
  transition: filter 0.2s ease;
}
#countryLayer .country .country-outline {
  stroke: rgba(20, 35, 60, 0.55);
  stroke-width: 1.4;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
  transition: stroke 0.2s ease, stroke-width 0.2s ease;
}
#countryLayer .country .flag-wrap {
  pointer-events: none;
  filter: url(#countryShadow);
}
#countryLayer .country:hover .country-outline {
  stroke: #ffce4a;
  stroke-width: 2.4;
}
#countryLayer .country.selected .country-outline {
  stroke: #fff;
  stroke-width: 3.2;
}
#countryLayer .country.selected {
  filter: url(#selectionGlow);
}
#countryLayer .country.selected .flag-wrap {
  animation: countryPulse 1.2s ease-in-out;
}
@keyframes countryPulse {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.015); }
  100% { transform: scale(1); }
}

#bannerLayer text.country-label {
  font-weight: 900;
  fill: #1c2842;
  paint-order: stroke;
  stroke: rgba(255, 255, 255, 0.95);
  stroke-width: 4px;
  stroke-linejoin: round;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
  letter-spacing: 0.2px;
  font-family: "Fredoka", "Nunito", sans-serif;
}
#bannerLayer text.empire-label {
  fill: #6b1a0a;
  stroke: rgba(255, 246, 200, 0.95);
  stroke-width: 5px;
}

/* ───────── Info card ───────── */
.info-card {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  right: 18px;
  width: min(380px, calc(100vw - 36px));
  background: var(--paper);
  border: 2.5px solid var(--paper-edge);
  border-radius: 26px;
  box-shadow: var(--card-shadow);
  padding: 18px 18px 20px;
  z-index: 6;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(100% - 36px);
  overflow-y: auto;
  transition: transform 0.3s cubic-bezier(0.34, 1.4, 0.64, 1), opacity 0.25s ease;
}
.info-card.hidden { display: none; }
.info-card.just-shown {
  animation: cardPop 360ms cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes cardPop {
  0%   { transform: translateY(-12px) scale(0.94); opacity: 0; }
  100% { transform: translateY(0)     scale(1);    opacity: 1; }
}
.info-close {
  position: absolute;
  top: 10px; right: 12px;
  width: 32px; height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(31, 44, 70, 0.08);
  color: var(--ink);
  font-size: 22px;
  font-weight: 800;
  cursor: pointer;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s ease, transform 0.1s ease;
}
.info-close:hover { background: rgba(31, 44, 70, 0.16); }
.info-close:active { transform: scale(0.92); }

.info-flag-wrap {
  display: flex;
  justify-content: center;
  padding: 12px;
  background: linear-gradient(180deg, #fff9e8, #ffe9b8);
  border-radius: 18px;
  border: 1.5px dashed var(--paper-edge);
}
.info-flag {
  width: 180px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.info-flag svg,
.info-flag img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 6px 16px rgba(0,0,0,0.22), 0 0 0 1px rgba(0,0,0,0.05);
}
.info-body { display: flex; flex-direction: column; gap: 8px; }
.info-row { display: flex; align-items: center; gap: 8px; }
.info-modern {
  font-size: 14px;
  font-weight: 800;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: flex; align-items: center; gap: 6px;
}
.info-modern-arrow { color: var(--gold-deep); font-weight: 900; }
.info-period-tag {
  background: var(--gold);
  color: var(--ink);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.2px;
  text-transform: none;
}
.info-period-name {
  margin: 0;
  font-size: 28px;
  font-weight: 900;
  color: var(--ink);
  line-height: 1.05;
  font-family: "Fredoka", "Nunito", sans-serif;
}
.info-year {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-soft);
}
.info-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 6px;
}
.info-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: #fff5d8;
  border: 1.5px solid #ecd58a;
  border-radius: 14px;
  padding: 10px 12px;
}
.info-item .info-icon {
  font-size: 22px;
  line-height: 1;
  margin-top: 1px;
}
.info-label {
  font-size: 11px;
  font-weight: 800;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.info-value {
  font-size: 15px;
  font-weight: 800;
  color: var(--ink);
  line-height: 1.25;
}
.info-description {
  margin: 4px 0 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.45;
}
.info-fact, .info-note {
  background: linear-gradient(180deg, #fff7d6, #ffe8a0);
  border: 1.5px solid #f0c95a;
  border-radius: 16px;
  padding: 12px 14px;
  position: relative;
}
.info-fact-bubble, .info-note-bubble {
  display: inline-block;
  background: var(--gold-deep);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.4px;
  padding: 3px 10px;
  border-radius: 999px;
  margin-bottom: 4px;
}
.info-note {
  background: linear-gradient(180deg, #e9efff, #cfdbff);
  border-color: #93a9e6;
}
.info-note-bubble { background: #3a5fbf; }
.info-fact p, .info-note p {
  margin: 4px 0 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.4;
}
.info-note.hidden { display: none; }

/* ───────── Companion ───────── */
.companion {
  position: absolute;
  left: 18px;
  bottom: 18px;
  z-index: 4;
  pointer-events: none;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.companion.arrived {
  opacity: 1;
  transform: translateY(0);
}
.companion-bubble {
  display: inline-block;
  background: rgba(253, 248, 234, 0.96);
  border: 2px solid rgba(214, 197, 151, 0.7);
  border-radius: 18px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 800;
  color: var(--ink);
  box-shadow: 0 8px 22px rgba(20, 50, 80, 0.18);
  max-width: 280px;
  line-height: 1.3;
  animation: bubbleFloat 4s ease-in-out infinite;
}
@keyframes bubbleFloat {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-4px); }
}

/* ───────── Time Dock (slider) — compact, more room for the map ───────── */
.time-dock {
  position: relative;
  background: linear-gradient(180deg, rgba(253, 248, 234, 0.94), rgba(253, 248, 234, 1) 40%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 2px solid rgba(214, 197, 151, 0.7);
  box-shadow: 0 -8px 30px rgba(20, 50, 80, 0.18);
  padding: 6px 16px calc(6px + var(--safe-bot));
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  column-gap: 14px;
  z-index: 4;
}
.time-dock-top {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.time-year-pill {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  background: linear-gradient(180deg, #fff5d8, #ffd45a);
  border: 2px solid var(--gold-deep);
  color: var(--ink);
  padding: 3px 12px;
  border-radius: 999px;
  box-shadow: 0 3px 10px rgba(214, 154, 22, 0.3);
  min-width: 0;
  justify-content: center;
}
.time-year-label {
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--ink-soft);
}
.time-year-value {
  font-size: 20px;
  font-weight: 900;
  font-family: "Fredoka", "Nunito", sans-serif;
  line-height: 1;
  color: var(--primary-dark);
  letter-spacing: 0.3px;
}
.time-era {
  font-size: 10px;
  font-weight: 800;
  color: var(--ink-soft);
}
.time-era-tag {
  font-size: 12px;
  font-weight: 800;
  color: var(--ink);
  background: rgba(255, 206, 74, 0.45);
  border: 1.5px dashed rgba(214, 154, 22, 0.5);
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
}

.time-slider-wrap {
  position: relative;
  padding: 2px 6px 12px;
  min-width: 0;
}
.time-track {
  position: absolute;
  left: 6px;
  right: 6px;
  top: 14px;
  height: 0;
  pointer-events: none;
}
.time-mark {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  pointer-events: none;
  text-align: center;
}
.time-mark::before {
  content: "";
  display: block;
  width: 2px;
  height: 5px;
  background: rgba(31, 44, 70, 0.4);
  margin: 0 auto;
}
.time-mark .mark-label {
  font-size: 9px;
  font-weight: 800;
  color: var(--ink-soft);
  margin-top: 7px;
  display: inline-block;
}

input[type="range"]#timeSlider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 14px;
  background: transparent;
  margin: 0;
}
input[type="range"]#timeSlider::-webkit-slider-runnable-track {
  height: 10px;
  background: linear-gradient(90deg, #b890f1 0%, #f1a460 35%, #ffd84d 70%, #7cd0a0 100%);
  border-radius: 999px;
  border: 1.5px solid rgba(31, 44, 70, 0.4);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.15);
}
input[type="range"]#timeSlider::-moz-range-track {
  height: 10px;
  background: linear-gradient(90deg, #b890f1 0%, #f1a460 35%, #ffd84d 70%, #7cd0a0 100%);
  border-radius: 999px;
  border: 1.5px solid rgba(31, 44, 70, 0.4);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.15);
}
input[type="range"]#timeSlider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff7d6 0%, #ffce4a 60%, #d49a16 100%);
  border: 2.5px solid #fff;
  box-shadow: 0 3px 8px rgba(214, 154, 22, 0.5), 0 0 0 1.5px rgba(31, 44, 70, 0.55);
  cursor: grab;
  margin-top: -10px;
  transition: transform 0.1s ease;
}
input[type="range"]#timeSlider::-webkit-slider-thumb:active {
  cursor: grabbing;
  transform: scale(1.1);
}
input[type="range"]#timeSlider::-moz-range-thumb {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff7d6 0%, #ffce4a 60%, #d49a16 100%);
  border: 2.5px solid #fff;
  box-shadow: 0 3px 8px rgba(214, 154, 22, 0.5), 0 0 0 1.5px rgba(31, 44, 70, 0.55);
  cursor: grab;
}

.time-dock-bottom {
  display: inline-flex;
  gap: 4px;
  justify-content: center;
  flex-wrap: nowrap;
}
.time-jump {
  border: 1.5px solid rgba(214, 197, 151, 0.7);
  background: #fff;
  color: var(--ink);
  font-size: 12px;
  font-weight: 800;
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(20, 50, 80, 0.08);
  white-space: nowrap;
  transition: background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
.time-jump:hover {
  background: #fff8e3;
  border-color: var(--gold-deep);
}
.time-jump:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(20, 50, 80, 0.08);
}
.time-jump-now {
  background: linear-gradient(180deg, #ff9b73, #ff7a59);
  color: #fff;
  border-color: var(--primary-dark);
  box-shadow: 0 3px 0 var(--primary-dark);
}
.time-jump-now:hover { background: #ff8260; }
.time-jump-now:active { box-shadow: 0 1px 0 var(--primary-dark); }

/* ───────── Tablet / iPad tuning ───────── */
@media (min-width: 900px) {
  .info-card { width: 420px; }
  .info-flag { width: 220px; height: 146px; }
  .info-period-name { font-size: 32px; }
  .info-grid { grid-template-columns: 1fr 1fr; }
  .info-description { font-size: 16px; }
  .app-bar-title { font-size: 30px; }
  .companion { left: 22px; bottom: 22px; }
  .companion-bubble { font-size: 16px; max-width: 320px; }
  .time-year-value { font-size: 22px; }
  .time-era-tag { font-size: 13px; padding: 4px 12px; }
  .time-jump { font-size: 13px; padding: 5px 11px; }
}

@media (min-width: 1200px) {
  .info-card { width: 460px; }
  .info-flag { width: 240px; height: 160px; }
  .info-period-name { font-size: 36px; }
}

/* Small phones — stack the dock so the slider gets its own row */
@media (max-width: 720px) {
  .time-dock {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    row-gap: 4px;
    padding: 4px 10px calc(4px + var(--safe-bot));
  }
  .time-dock-top { justify-content: center; }
  .time-dock-bottom { flex-wrap: wrap; }
}
@media (max-width: 540px) {
  .app-bar { padding: 6px 12px; padding-top: calc(6px + var(--safe-top)); gap: 8px; }
  .app-bar-title { font-size: 20px; }
  .app-bar-subtitle { font-size: 11px; }
  .info-card {
    top: auto;
    right: 8px;
    left: 8px;
    bottom: auto;
    width: auto;
    max-height: 60%;
    border-radius: 22px;
  }
  .info-flag { width: 120px; height: 80px; }
  .info-period-name { font-size: 22px; }
  .info-grid { grid-template-columns: 1fr; }
  .companion { left: 10px; bottom: 6px; }
  .companion-bubble { font-size: 12px; padding: 6px 10px; max-width: 200px; }
  .time-year-pill { padding: 2px 10px; }
  .time-year-value { font-size: 17px; }
  .time-era-tag { font-size: 11px; padding: 2px 8px; }
  .time-mark .mark-label { font-size: 9px; margin-top: 6px; }
  .time-jump { font-size: 11px; padding: 3px 8px; }
  #bannerLayer text.country-label { font-size: 22px !important; }
}

@media (max-height: 620px) {
  .info-card { max-height: calc(100% - 16px); top: 8px; right: 8px; }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  #oceanWavesLayer { animation: none; }
  .companion-bubble { animation: none; }
  .app-bar-compass { animation: none; }
}

/* Print: hide UI, show just the map */
@media print {
  .app-bar, .time-dock, .info-card, .companion { display: none; }
}
