:root {
  --bg-ocean-top: #bfe6f7;
  --bg-ocean-bottom: #3a7fb8;
  --paper: #fdf6e3;
  --paper-edge: #d6c597;
  --ink: #2b3a55;
  --ink-soft: #5a6b88;
  --primary: #ff7a59;
  --primary-dark: #e6603f;
  --accent: #ffd84d;
  --soft: #f0eadb;
  --soft-2: #e6deca;
  --tool-active: #ffd84d;
  --tool-active-shadow: 0 6px 18px rgba(255, 184, 60, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg-ocean-bottom);
  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: none;
  overscroll-behavior: none;
}

input[type="text"] {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- App bar ---------- */
.app-bar {
  position: absolute;
  top: max(8px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 18px 8px 14px;
  background: rgba(253, 246, 227, 0.92);
  border: 2px solid rgba(214, 197, 151, 0.7);
  border-radius: 999px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 6px 18px rgba(20, 50, 80, 0.18);
  z-index: 3;
}
.app-bar-compass {
  display: inline-flex;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.15));
}
.app-bar-title {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.2px;
  color: var(--ink);
}

/* ---------- Map ---------- */
#map {
  flex: 1;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#oceanWavesLayer {
  animation: waveDrift 22s linear infinite;
  transform-origin: center;
  opacity: 0.9;
}
@keyframes waveDrift {
  0%   { transform: translate(0, 0); }
  100% { transform: translate(-120px, 0); }
}

#oceanDecorLayer .ocean-cloud {
  animation: cloudDrift 60s linear infinite;
  opacity: 0.9;
}
#oceanDecorLayer .ocean-cloud.slow { animation-duration: 90s; opacity: 0.78; }
#oceanDecorLayer .ocean-cloud.fast { animation-duration: 45s; }
@keyframes cloudDrift {
  0%   { transform: translateX(0); }
  100% { transform: translateX(40px); }
}
#oceanDecorLayer .ocean-boat {
  animation: boatBob 4s ease-in-out infinite;
  transform-origin: center;
}
@keyframes boatBob {
  0%, 100% { transform: translate(0, 0) rotate(-2deg); }
  50%      { transform: translate(0, -4px) rotate(2deg); }
}

#continentLayer .continent {
  cursor: grab;
  transition: filter 0.18s ease;
}
#continentLayer .continent.selected { filter: url(#selectionGlow); }
#continentLayer .continent.dragging {
  cursor: grabbing;
  filter: url(#continentShadowLifted);
}
#continentLayer .continent.just-born { animation: continentPop 600ms cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes continentPop {
  0%   { opacity: 0; transform: scale(0.55); }
  60%  { opacity: 1; transform: scale(1.06); }
  100% { opacity: 1; transform: scale(1); }
}
#continentLayer .continent.erasing { animation: continentErase 420ms ease-out forwards; pointer-events: none; }
@keyframes continentErase {
  0%   { opacity: 1; transform: scale(1); }
  60%  { opacity: 0.6; transform: scale(0.96); }
  100% { opacity: 0; transform: scale(0.7); }
}

#continentLayer .deco {
  pointer-events: none;
  opacity: 0.92;
}

#continentLayer .continent.is-country path[data-role="shape"] {
  stroke: rgba(255, 255, 255, 0.9);
  stroke-width: 2;
  stroke-dasharray: 3 3;
}

#labelLayer text.continent-label {
  font-family: inherit;
  font-weight: 800;
  font-size: 22px;
  fill: var(--ink);
  paint-order: stroke;
  stroke: rgba(255, 255, 255, 0.95);
  stroke-width: 5px;
  stroke-linejoin: round;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
  letter-spacing: 0.3px;
}

#sparkleLayer .sparkle {
  animation: sparklePop 900ms ease-out forwards;
  transform-origin: center;
  transform-box: fill-box;
}
@keyframes sparklePop {
  0%   { opacity: 0; transform: scale(0.2) rotate(0deg); }
  35%  { opacity: 1; }
  100% { opacity: 0; transform: scale(1.4) rotate(45deg); }
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 8px;
  padding: 10px 10px max(12px, env(safe-area-inset-bottom)) 10px;
  background: linear-gradient(180deg, rgba(253, 246, 227, 0.6), rgba(253, 246, 227, 0.96) 50%);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 2px solid rgba(214, 197, 151, 0.55);
  box-shadow: 0 -6px 22px rgba(20, 50, 80, 0.16);
  overflow-x: auto;
  scrollbar-width: none;
  z-index: 4;
}
.toolbar::-webkit-scrollbar { display: none; }

.tool {
  flex: 1 0 auto;
  min-width: 72px;
  border: 2px solid transparent;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 20px;
  padding: 10px 8px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.1s ease, background 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.06);
}
.tool .icon {
  font-size: 30px;
  line-height: 1;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.12));
}
.tool:active { transform: translateY(1px) scale(0.97); box-shadow: 0 1px 0 rgba(0,0,0,0.06); }
.tool:hover { background: #fff; }
.tool.active {
  background: var(--tool-active);
  color: var(--ink);
  border-color: #f0b424;
  box-shadow: var(--tool-active-shadow);
}

/* ---------- Toast ---------- */
.toast {
  position: absolute;
  top: max(72px, calc(env(safe-area-inset-top) + 64px));
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  background: rgba(253, 246, 227, 0.97);
  color: var(--ink);
  padding: 12px 22px;
  border-radius: 22px;
  border: 2px solid rgba(214, 197, 151, 0.7);
  box-shadow: 0 8px 26px rgba(20, 50, 80, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.3s ease;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 86vw;
  text-align: center;
}
.toast-title {
  font-size: 18px;
  font-weight: 800;
}
.toast-subtitle {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-top: 2px;
}
.toast-subtitle:empty { display: none; }
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.hidden { display: none; }

.confetti {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 6;
}

/* ---------- Companion ---------- */
.companion {
  position: absolute;
  left: max(10px, env(safe-area-inset-left));
  bottom: calc(max(12px, env(safe-area-inset-bottom)) + 96px);
  display: flex;
  align-items: flex-end;
  gap: 8px;
  z-index: 4;
  pointer-events: none;
}
.companion-avatar {
  pointer-events: auto;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  filter: drop-shadow(0 4px 10px rgba(20, 50, 80, 0.25));
  animation: companionFloat 3.4s ease-in-out infinite;
  transform-origin: center;
}
.companion-avatar:active { transform: scale(0.94); }
@keyframes companionFloat {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-4px); }
}

.companion-bubble {
  pointer-events: auto;
  background: rgba(253, 246, 227, 0.96);
  border: 2px solid rgba(214, 197, 151, 0.7);
  border-radius: 18px;
  padding: 8px 14px;
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
  box-shadow: 0 4px 14px rgba(20, 50, 80, 0.18);
  max-width: 56vw;
  position: relative;
  margin-bottom: 6px;
  line-height: 1.3;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.companion-bubble::before {
  content: "";
  position: absolute;
  left: -10px;
  bottom: 14px;
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-right: 12px solid rgba(214, 197, 151, 0.7);
}
.companion-bubble::after {
  content: "";
  position: absolute;
  left: -7px;
  bottom: 14px;
  width: 0;
  height: 0;
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  border-right: 11px solid rgba(253, 246, 227, 0.96);
}
.companion-bubble.hidden {
  opacity: 0;
  transform: translateY(6px);
  pointer-events: none;
}

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 30, 60, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 16px;
}
.overlay.hidden { display: none; }

.overlay-card {
  background: var(--paper);
  border: 2px solid var(--paper-edge);
  border-radius: 22px;
  padding: 22px;
  width: min(440px, 100%);
  box-shadow: 0 20px 50px rgba(0, 20, 50, 0.3);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.overlay-card h2 {
  margin: 0;
  font-size: 22px;
  color: var(--ink);
}
.overlay-card label {
  font-size: 16px;
  font-weight: 700;
  color: var(--ink-soft);
}
.overlay-body {
  margin: 0;
  font-size: 17px;
  color: var(--ink-soft);
  line-height: 1.4;
}
.overlay-card input[type="text"] {
  border: 2px solid var(--soft-2);
  background: #fffdf6;
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 18px;
  font-family: inherit;
  font-weight: 700;
  color: var(--ink);
  outline: none;
  width: 100%;
}
.overlay-card input[type="text"]:focus { border-color: var(--primary); }

.overlay-buttons {
  display: flex;
  gap: 10px;
  align-items: center;
}
.overlay-buttons .btn { flex: 1; }
.btn {
  border: none;
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 3px 0 var(--primary-dark);
}
.btn-primary:active { transform: translateY(1px); box-shadow: 0 1px 0 var(--primary-dark); }
.btn-soft {
  background: var(--soft);
  color: var(--ink);
  box-shadow: 0 3px 0 var(--soft-2);
}
.btn-soft:active { transform: translateY(1px); box-shadow: 0 1px 0 var(--soft-2); }

#worldsList {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 40vh;
  overflow-y: auto;
}
#worldsList li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--soft);
  border-radius: 12px;
  font-size: 16px;
  color: var(--ink);
}
#worldsList li .world-name { flex: 1; font-weight: 700; }
#worldsList li button {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 18px;
  padding: 4px 8px;
  border-radius: 8px;
}
#worldsList li button:hover { background: rgba(0,0,0,0.05); }
#worldsList li button.delete { color: #c44; }

@media (max-width: 480px) {
  .tool { min-width: 60px; font-size: 12px; padding: 8px 6px 6px; }
  .tool .icon { font-size: 26px; }
  .app-bar-title { font-size: 16px; }
  .companion-bubble { font-size: 14px; padding: 6px 12px; max-width: 60vw; }
  #labelLayer text.continent-label { font-size: 19px; }
}

@media (max-height: 560px) {
  .companion { bottom: calc(max(8px, env(safe-area-inset-bottom)) + 88px); }
  .companion-avatar svg { width: 42px; height: 42px; }
}
