:root {
  --bg-top: #183f2b;
  --bg-bottom: #0e2b1d;
  --panel: rgba(0, 0, 0, 0.45);
  --panel-border: rgba(255,255,255,0.12);
  --text: #ffffff;
  --subtle: rgba(255,255,255,0.78);
  --button: #2f5f41;
  --button-hover: #3a7652;
  --popup: #244a33;
  --popup-soft: rgba(255,255,255,0.08);
  --firefly: rgba(255, 240, 140, 0.95);
}

body.night-mode {
  --bg-top: #0f2030;
  --bg-bottom: #09131b;
  --panel: rgba(5, 10, 18, 0.55);
  --panel-border: rgba(255,255,255,0.10);
  --text: #eef7ff;
  --subtle: rgba(238,247,255,0.78);
  --button: #284866;
  --button-hover: #35618a;
  --popup: #162a3e;
  --popup-soft: rgba(255,255,255,0.10);
  --firefly: rgba(255, 243, 166, 1);
}

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

#ui {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  padding: 12px 14px;
  border-radius: 12px;
  max-width: min(390px, 92vw);
  line-height: 1.4;
  backdrop-filter: blur(6px);
  box-shadow: 0 10px 30px rgba(0,0,0,0.28);
}

#ui h1 {
  margin: 0 0 8px 0;
  font-size: 18px;
}

#ui p {
  margin: 0 0 8px 0;
  font-size: 13px;
  color: var(--subtle);
}

#ui .small {
  font-size: 12px;
  opacity: 0.9;
}

.ui-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.ui-row input,
.ui-row select,
.modal-body textarea,
.modal-body input {
  flex: 1;
  min-width: 140px;
  padding: 8px 10px;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  color: var(--text);
  outline: none;
  font: inherit;
  box-sizing: border-box;
}

.ui-row input::placeholder,
.modal-body textarea::placeholder,
.modal-body input::placeholder {
  color: rgba(255,255,255,0.58);
}

button {
  padding: 8px 10px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: var(--button);
  color: var(--text);
  font-weight: bold;
  transition: background 0.18s ease, transform 0.18s ease;
}

button:hover {
  background: var(--button-hover);
  transform: translateY(-1px);
}

.secondary {
  background: rgba(255,255,255,0.12);
}

.secondary:hover {
  background: rgba(255,255,255,0.18);
}

#world {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  cursor: grab;
  background: linear-gradient(180deg, #143726 0%, #102d1f 100%);
  touch-action: none;
}

#forest {
  position: absolute;
  width: 3000px;
  height: 3000px;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
  backface-visibility: hidden;
  background:
    radial-gradient(circle at 20% 30%, rgba(70,120,80,0.08), transparent 40%),
    radial-gradient(circle at 75% 65%, rgba(30,60,40,0.08), transparent 45%),
    linear-gradient(180deg, #143726 0%, #102d1f 100%);
}

body.night-mode #forest {
  background:
    radial-gradient(rgba(255,255,255,0.012) 1px, transparent 1px),
    radial-gradient(rgba(0,0,0,0.05) 2px, transparent 2px),
    radial-gradient(circle at 20% 30%, rgba(41,68,95,0.14), transparent 35%),
    radial-gradient(circle at 75% 65%, rgba(17,30,42,0.18), transparent 40%),
    linear-gradient(180deg, rgba(17,39,53,0.96) 0%, rgba(11,22,31,0.98) 100%);
  background-size:
    36px 36px,
    110px 110px,
    900px 900px,
    1200px 1200px,
    100% 100%;
}

.fog-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.18;
  background:
    radial-gradient(circle at 30% 40%, rgba(255,255,255,0.06), transparent 60%),
    radial-gradient(circle at 70% 60%, rgba(255,255,255,0.04), transparent 70%);
  animation: fogDrift 95s linear infinite;
}

body.night-mode .fog-layer {
  opacity: 0.24;
}

@keyframes fogDrift {
  0% { transform: translateX(0); }
  50% { transform: translateX(-100px); }
  100% { transform: translateX(0); }
}

#decorLayer,
#treeLayer,
#ambientLayer,
#effectLayer {
  position: absolute;
  inset: 0;
}

#decorLayer,
#ambientLayer,
#effectLayer {
  pointer-events: none;
}

#treeLayer {
  pointer-events: auto;
}

.tree {
  touch-action: none;  
  position: absolute;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  cursor: pointer;
  transform: translate(-50%, -50%);
  transition: transform 0.14s ease;
}

.tree:hover {
  transform: translate(-50%, -50%) scale(1.08);
}

.tree span {
  font-size: 24px;
  line-height: 1;
  transform-origin: center;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.3));
  will-change: transform;
  backface-visibility: hidden;
  transform: translateZ(0);
}

.tree-ground-halo,
.tree-leaves {
  position: absolute;
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.tree-ground-halo {
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0,0,0,0.15) 0%, rgba(0,0,0,0.06) 45%, transparent 75%);
  opacity: 0.7;
}

.tree-leaves {
  font-size: 9px;
  opacity: 0.5;
  letter-spacing: 3px;
}

.firefly {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--firefly);
  box-shadow:
    0 0 6px color-mix(in srgb, var(--firefly) 95%, transparent),
    0 0 12px color-mix(in srgb, var(--firefly) 60%, transparent),
    0 0 20px color-mix(in srgb, var(--firefly) 35%, transparent);
  pointer-events: none;
  transform: translate(-50%, -50%);
  opacity: 0.8;
}

.plant-ripple,
.plant-glow {
  position: absolute;
  pointer-events: none;
  transform: translate(-50%, -50%);
  border-radius: 50%;
}

.plant-ripple {
  width: 24px;
  height: 24px;
  border: 2px solid rgba(255,255,255,0.7);
  animation: plantRipple 1.8s ease-out forwards;
}

.plant-glow {
  width: 30px;
  height: 30px;
  background: radial-gradient(circle, rgba(255,234,150,0.65) 0%, rgba(255,234,150,0.22) 45%, transparent 75%);
  animation: plantGlow 1.8s ease-out forwards;
}

@keyframes plantRipple {
  0% { opacity: 0.95; width: 18px; height: 18px; }
  100% { opacity: 0; width: 145px; height: 145px; }
}

@keyframes plantGlow {
  0% { opacity: 0.9; transform: translate(-50%, -50%) scale(0.8); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(2.4); }
}

#popup {
  position: fixed;
  display: none;
  z-index: 40;
  width: min(330px, calc(100vw - 24px));
  background: var(--popup);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 16px 36px rgba(0,0,0,0.35);
  backdrop-filter: blur(8px);
  -webkit-overflow-scrolling: touch;
}

.popup-stage-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--popup-soft);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  margin-bottom: 10px;
}

.popup-stage-badge .stage-icon {
  font-size: 18px;
}

#popup .coord {
  display: inline-block;
  font-size: 12px;
  opacity: 0.9;
  margin-bottom: 10px;
  background: rgba(255,255,255,0.08);
  padding: 5px 8px;
  border-radius: 999px;
}

#popup .message {
  font-size: 15px;
  line-height: 1.48;
  margin-bottom: 12px;
  color: var(--text);
}

#popup .divider {
  height: 1px;
  background: rgba(255,255,255,0.10);
  margin: 12px 0;
}

#popup .meta {
  font-size: 12px;
  opacity: 0.82;
  margin-top: 4px;
}

#popup .actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

#overlay,
#welcomeOverlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: none;
  z-index: 50;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#welcomeOverlay {
  z-index: 60;
  display: flex;
}

.modal {
  width: 560px;
  max-width: 90vw;
  background: #173726;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.4);
  overflow: hidden;
}

.modal-body {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-header {
  padding: 16px 18px 10px 18px;
  font-size: 20px;
  font-weight: bold;
}

.modal-body p,
.modal-body li {
  color: var(--subtle);
  font-size: 14px;
  line-height: 1.45;
}

.modal-body ul {
  padding-left: 18px;
  margin-top: 8px;
  margin-bottom: 12px;
}

.modal-body label {
  display: block;
  font-size: 13px;
  margin-bottom: 6px;
  opacity: 0.9;
}

.modal-body textarea {
  resize: vertical;
  min-height: 110px;
  margin-bottom: 12px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 4px;
}

#toast {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(21, 52, 36, 0.95);
  border: 1px solid rgba(255,255,255,0.1);
  padding: 12px 16px;
  border-radius: 999px;
  z-index: 80;
  display: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  font-size: 14px;
  max-width: min(92vw, 520px);
  text-align: center;
}

.highlight-ring {
  position: absolute;
  width: 70px;
  height: 70px;
  border: 3px solid rgba(255,255,255,0.9);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  animation: pulse 1.4s ease-out 3;
}

@keyframes pulse {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(0.8); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.6); }
}

.tree {
  position: absolute;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  cursor: pointer;
  transform: translate(-50%, -50%);
  transition: transform 0.14s ease;
  backface-visibility: hidden;
  will-change: transform;
}

.tree:hover {
  transform: translate(-50%, -50%) scale(1.08);
}

.tree span,
.firefly,
.tree-leaves,
.tree-ground-halo {
  transform: translateZ(0);
  backface-visibility: hidden;
  will-change: transform;
}

.popup-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  background: rgba(255,255,255,0.9);
  color: #234;
  box-shadow: 0 2px 8px rgba(0,0,0,0.18);
  z-index: 5;
}

.popup-close:hover {
  transform: scale(1.05);
}

@media (max-width: 768px) {
  #popup {
    position: fixed !important;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%) !important;
    width: min(92vw, 340px);
    max-width: 92vw;
    max-height: 75vh;
    overflow-y: auto;
    border-radius: 16px;
    padding: 16px;
    box-sizing: border-box;
  }

  .tree {
    width: 38px;
    height: 38px;
  }

  .popup-close {
    width: 38px;
    height: 38px;
    font-size: 22px;
  }
}

@media (hover: none) and (pointer: coarse) {
  .tree:hover {
    transform: translate(-50%, -50%);
  }

  .popup-close:hover {
    transform: none;
  }
}