:root {
  --sky: #FFF4DE;
  --grass: #DCEFB4;
  --fence: #A9744F;
  --ink: #3D2B1F;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Comic Sans MS', 'Baloo 2', 'Trebuchet MS', sans-serif;
  color: var(--ink);
}

body {
  background: var(--sky);
  background-image:
    radial-gradient(circle at 15% 20%, #FFE9A8 0%, transparent 40%),
    radial-gradient(circle at 85% 15%, #FFE9A8 0%, transparent 35%);
}

/* ---------- VIEWS ---------- */
.view {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
}
.view.active { display: flex; }

/* ---------- GRID VIEW ---------- */
#gridView,
#allZoosView {
  justify-content: flex-start;
  padding: 20px;
  padding-bottom: 40px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

h1 {
  font-size: clamp(2rem, 6vw, 3.2rem);
  margin: 0 0 4px 0;
  text-shadow: 2px 2px 0 #fff;
}

.subtitle {
  margin: 0 0 14px 0;
  font-size: 1.1rem;
  opacity: 0.75;
}

.grid-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.saved-indicator {
  background: #fff;
  border: 3px solid var(--fence);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 1rem;
  font-weight: bold;
}
.saved-indicator[hidden] { display: none; }

.info-btn {
  background: #fff;
  border: 3px solid var(--fence);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 1.1rem;
  font-weight: bold;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.info-btn:active { transform: scale(0.9); }

.allzoos-grid {
  margin-top: 20px;
}

.pen-header-spacer {
  width: 1px;
}

.zoo-tile {
  cursor: pointer;
}

.zoo-tile .label {
  max-width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.zoo-tile.new-zoo-tile {
  background: repeating-linear-gradient(45deg, #FFF8E8, #FFF8E8 10px, #FFF0CC 10px, #FFF0CC 20px);
}

.zoo-tile-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--fence);
  background: #fff;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.85rem;
  line-height: 1;
  color: var(--ink);
  cursor: pointer;
  z-index: 3;
  padding: 0;
}
.zoo-tile-delete:active { transform: scale(0.9); }

.zoo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 18px;
  width: min(94vw, 960px);
}

.pen {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 20px;
  border: 6px solid var(--fence);
  background: var(--grass);
  background-image: repeating-linear-gradient(90deg, rgba(169,116,79,0.15) 0 4px, transparent 4px 32px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 6px 0 rgba(0,0,0,0.12);
  overflow: hidden;
}
.pen:hover { transform: translateY(-3px); }
.pen:active { transform: translateY(2px) scale(0.97); box-shadow: 0 2px 0 rgba(0,0,0,0.12); }
.pen.bounce { animation: bounce 0.5s ease; }

@keyframes bounce {
  0% { transform: scale(1); }
  30% { transform: scale(1.12) rotate(-3deg); }
  60% { transform: scale(0.96) rotate(2deg); }
  100% { transform: scale(1); }
}

.emoji { font-size: clamp(3.2rem, 12vw, 5rem); line-height: 1; filter: drop-shadow(2px 3px 1px rgba(0,0,0,0.15)); }

.label {
  margin-top: 6px;
  font-size: 1.1rem;
  font-weight: bold;
  background: #fff;
  padding: 2px 12px;
  border-radius: 999px;
  border: 3px solid var(--fence);
}

.enter-hint {
  position: absolute;
  bottom: 6px;
  font-size: 0.7rem;
  background: #fff;
  padding: 2px 8px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.2s ease;
  font-weight: bold;
}
.pen.greeted .enter-hint { opacity: 0.85; }

.speech {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%) scale(0);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 6px 10px;
  font-size: 0.95rem;
  font-weight: bold;
  white-space: nowrap;
  pointer-events: none;
  transition: transform 0.25s ease;
  z-index: 2;
}
.speech.show { transform: translateX(-50%) scale(1); }

.pen[data-animal="cat"]      { background-color: #FDEBD3; }
.pen[data-animal="elephant"] { background-color: #E6E9F0; }
.pen[data-animal="lion"]     { background-color: #FFEFC2; }
.pen[data-animal="giraffe"]  { background-color: #FFF3D6; }

/* ---------- PEN VIEW (feeding) ---------- */
#penView {
  background: var(--grass);
  background-image: repeating-linear-gradient(90deg, rgba(169,116,79,0.12) 0 4px, transparent 4px 40px);
}

.pen-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  box-sizing: border-box;
}

.back-btn {
  background: #fff;
  border: 3px solid var(--fence);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 1rem;
  font-weight: bold;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
}
.back-btn:active { transform: scale(0.95); }

.pen-title {
  font-size: 1.3rem;
  font-weight: bold;
  background: #fff;
  padding: 4px 16px;
  border-radius: 999px;
  border: 3px solid var(--fence);
}

.hunger-meter {
  display: flex;
  gap: 3px;
  font-size: 1.2rem;
}

.habitat {
  position: relative;
  flex: 1;
  width: 100%;
  overflow: hidden;
}

.animal-sprite {
  position: absolute;
  font-size: clamp(3.5rem, 14vw, 6rem);
  filter: drop-shadow(2px 4px 2px rgba(0,0,0,0.2));
  user-select: none;
  z-index: 3;
  will-change: left, top, transform;
}
/* walking bob, applied while moving */
.animal-sprite.walking { animation: walkBob 0.45s ease-in-out infinite; }
@keyframes walkBob {
  0%, 100% { margin-top: 0px; }
  50% { margin-top: -5px; }
}
.animal-sprite.eating { animation: nomnom 0.3s ease infinite; }
@keyframes nomnom {
  0%, 100% { margin-top: 0px; }
  50% { margin-top: -6px; }
}

.pen-speech {
  position: absolute;
  transform: translate(-50%, -100%) scale(0);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 6px 12px;
  font-size: 1rem;
  font-weight: bold;
  white-space: nowrap;
  pointer-events: none;
  transition: transform 0.25s ease;
  z-index: 5;
}
.pen-speech.show { transform: translate(-50%, -110%) scale(1); }

.food-tray {
  width: 100%;
  padding: 16px 12px 22px;
  display: flex;
  justify-content: center;
  gap: 22px;
  background: linear-gradient(to top, #fff8e8, transparent);
  box-sizing: border-box;
}

.food-item {
  font-size: 2.6rem;
  cursor: grab;
  touch-action: none;
  filter: drop-shadow(2px 3px 1px rgba(0,0,0,0.2));
  user-select: none;
  transition: transform 0.15s ease;
}
.food-item:active { cursor: grabbing; }
.food-item.dragging {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  transform: scale(1.3);
}

.full-msg {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  border: 3px solid var(--fence);
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 0.9rem;
  font-weight: bold;
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: 6;
}
.full-msg.show { opacity: 1; }

/* ---------- COINS ---------- */
.coin-counter {
  position: fixed;
  top: 14px;
  right: 16px;
  background: #fff;
  border: 3px solid var(--fence);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 1.1rem;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 20;
  box-shadow: 0 4px 0 rgba(0,0,0,0.1);
}
.coin-counter .coin-emoji { font-size: 1.2rem; }
.coin-counter.pulse { animation: coinPulse 0.4s ease; }
@keyframes coinPulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

.coin-popup {
  position: absolute;
  font-size: 1.3rem;
  font-weight: bold;
  color: #B8860B;
  text-shadow: 1px 1px 0 #fff;
  pointer-events: none;
  z-index: 25;
  animation: coinFloat 1s ease-out forwards;
}
@keyframes coinFloat {
  0%   { transform: translate(-50%, 0) scale(0.6); opacity: 0; }
  15%  { transform: translate(-50%, -10px) scale(1.1); opacity: 1; }
  100% { transform: translate(-50%, -70px) scale(1); opacity: 0; }
}

/* ---------- LOCKED PEN ---------- */
.pen.locked {
  background: repeating-linear-gradient(45deg, #E4DACB, #E4DACB 10px, #D9CBB6 10px, #D9CBB6 20px);
  cursor: default;
}
.pen.locked:hover { transform: none; }
.pen.locked .emoji { filter: grayscale(1) opacity(0.6); }
.lock-progress {
  margin-top: 6px;
  font-size: 0.85rem;
  font-weight: bold;
  background: #fff;
  padding: 2px 10px;
  border-radius: 999px;
  border: 3px solid var(--fence);
}

.pen.locked.ready {
  background: repeating-linear-gradient(45deg, #FFEFC2, #FFEFC2 10px, #FFE097 10px, #FFE097 20px);
  cursor: pointer;
  border-color: #E8A33D;
  animation: readyGlow 1.4s ease-in-out infinite;
}
.pen.locked.ready:hover { transform: translateY(-3px); }
.pen.locked.ready:active { transform: translateY(2px) scale(0.97); }
.pen.locked.ready .emoji { filter: none; }
.pen.locked.ready .lock-progress {
  background: #FFD873;
  border-color: #E8A33D;
}
@keyframes readyGlow {
  0%, 100% { box-shadow: 0 6px 0 rgba(0,0,0,0.12); }
  50% { box-shadow: 0 6px 0 rgba(0,0,0,0.12), 0 0 0 6px rgba(255, 216, 115, 0.55); }
}
.pen.locked.ready::after {
  content: "Tap to unlock!";
  position: absolute;
  bottom: 6px;
  font-size: 0.7rem;
  font-weight: bold;
  background: #fff;
  padding: 2px 8px;
  border-radius: 999px;
}

/* ---------- UNLOCK MODAL ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(61, 43, 31, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-overlay.show { display: flex; }

.modal-card {
  position: relative;
  background: #fff8e8;
  border: 6px solid var(--fence);
  border-radius: 26px;
  padding: 34px 28px 28px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: 0 10px 0 rgba(0,0,0,0.15);
  overflow: visible;
  transform: scale(0.7);
  opacity: 0;
  transition: transform 0.35s cubic-bezier(.34,1.56,.64,1), opacity 0.25s ease;
}
.modal-overlay.show .modal-card {
  transform: scale(1);
  opacity: 1;
}

.modal-star {
  position: absolute;
  font-size: 1.6rem;
  opacity: 0;
  z-index: 2;
}
.modal-overlay.show .modal-star {
  animation: starPop 0.6s ease forwards;
}
@keyframes starPop {
  0%   { opacity: 0; transform: scale(0) rotate(0deg); }
  60%  { opacity: 1; transform: scale(1.3) rotate(20deg); }
  100% { opacity: 1; transform: scale(1) rotate(0deg); }
}

.modal-title {
  font-size: 1.4rem;
  font-weight: bold;
  margin: 6px 0 2px;
}
.modal-newanimal {
  font-size: 5rem;
  margin: 10px 0;
  filter: drop-shadow(2px 4px 2px rgba(0,0,0,0.2));
  display: inline-block;
  animation: bounce 0.6s ease 0.3s;
}
.modal-sub {
  font-size: 1.05rem;
  margin: 0 0 20px;
  opacity: 0.85;
}
.modal-btn {
  background: #FFD873;
  border: 3px solid var(--fence);
  border-radius: 999px;
  padding: 10px 26px;
  font-size: 1.1rem;
  font-weight: bold;
  font-family: inherit;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0,0,0,0.15);
}
.modal-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.15); }

.pen.new-pen {
  animation: penPopIn 0.5s cubic-bezier(.34,1.56,.64,1);
}
@keyframes penPopIn {
  0% { transform: scale(0); opacity: 0; }
  70% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); }
}
