@font-face {
  font-family: "Rainy Hearts";
  src: url("fonts/rainyhearts.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Panter";
  src: url("fonts/Panter.otf") format("opentype"),
       url("fonts/Panter.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --cream: #fffff3;
  --green: #016630;
  --green-soft: #3c7a3b;
  --ink: #1e2939;
  --muted: #4a5565;
  --dashed: #1e2939;
  --font-ui: "VT323", "Rainy Hearts", monospace;
  --font-display: "Pixelify Sans", "VT323", monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  min-height: 100dvh;
}

body {
  font-family: var(--font-ui);
  font-size: 1.25rem;
  color: var(--ink);
  background: var(--cream);
  -webkit-font-smoothing: none;
  -moz-osx-font-smoothing: unset;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: inherit;
  color: inherit;
  background: transparent;
  border: none;
  cursor: pointer;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.page {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  overflow-x: hidden;
}

/* ---------- Header ---------- */
.title {
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 4vw, 2.35rem);
  font-weight: 700;
  color: var(--green);
  text-align: center;
  padding: 1.25rem 1rem 0.35rem;
  letter-spacing: 0.04em;
  text-transform: lowercase;
  -webkit-font-smoothing: none;
  -moz-osx-font-smoothing: unset;
}

.room-meta {
  font-family: var(--font-ui);
  font-size: 1.05rem;
  color: var(--muted);
  text-align: center;
  padding: 0 1rem 0.35rem;
  min-height: 1.35em;
  opacity: 0.9;
}

@media (min-width: 768px) {
  .title { padding: 2.5rem 1rem 0.75rem; }
}

.status {
  font-family: var(--font-ui);
  color: var(--green);
  font-size: 1.15rem;
  text-align: center;
  opacity: 0.85;
  padding: 0 1rem 0.5rem;
  min-height: 1.5em;
}

.status.is-winning {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.35rem, 4vw, 1.85rem);
  color: #F0D020;
  opacity: 1;
  letter-spacing: 0.03em;
  -webkit-font-smoothing: none;
  -moz-osx-font-smoothing: unset;
}

.status .sub {
  opacity: 0.7;
  margin-left: 0.4rem;
}

.status .sub::before {
  content: "· ";
}

.counts {
  display: flex;
  gap: 1rem;
  justify-content: center;
  align-items: center;
  font-size: 1.05rem;
  color: var(--muted);
  padding-bottom: 0.75rem;
  line-height: 1;
}

.counts > span {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  line-height: 1;
}

@media (max-width: 767px) {
  .counts #totalCount { display: none; }

  .main {
    flex: 0 0 auto;
    gap: 0;
    padding-bottom: 0;
    justify-content: flex-start;
  }

  .scene-wrap {
    width: min(100%, 420px);
    animation: none;
  }

  .scene {
    aspect-ratio: 1.7 / 1;
    overflow: hidden;
  }

  .scene-island {
    filter: drop-shadow(10px 10px 0 rgba(60, 90, 40, 0.12));
  }

  .placement {
    width: 56px;
    height: 56px;
  }

  .mobile-cta {
    flex-direction: row;
    width: min(100%, 340px);
    gap: 0.5rem;
    padding-bottom: 0.5rem;
    margin-top: -3.5rem;
  }

  .mobile-cta .pill {
    flex: 1;
    padding: 0.65rem 0.5rem;
    font-size: 0.95rem;
  }

  .gallery-link {
    margin-top: 0.35rem;
    margin-bottom: 1.25rem;
  }
}

/* ---------- Main layout ---------- */
.main {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2.5rem;
  padding: 0 1rem 2.5rem;
}

@media (min-width: 768px) {
  .main {
    flex-direction: row;
    gap: 5rem;
    align-items: center;
    padding-bottom: 3rem;
  }
}

/* ---------- Floating scene ---------- */
.scene-wrap {
  position: relative;
  width: min(100%, 560px);
  animation: bob 3s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.scene {
  position: relative;
  width: 100%;
  aspect-ratio: 1.05 / 1;
}

.scene-svg {
  width: 100%;
  height: auto;
  display: block;
}

.scene-clouds {
  filter: drop-shadow(12px 18px 0 rgba(60, 90, 40, 0.12));
}

.scene-cloud-small {
  /* Same shadow as the other clouds, shifted up ~5px */
  filter: drop-shadow(12px 13px 0 rgba(60, 90, 40, 0.12));
}

.scene-island {
  filter: drop-shadow(12px 18px 0 rgba(60, 90, 40, 0.12));
}

.placements {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.placement {
  position: absolute;
  width: 72px;
  height: 72px;
  transform: translateX(-50%);
  transform-origin: center bottom;
  pointer-events: auto;
  cursor: pointer;
}

.placement.is-cow {
  /* left/top mark where the hooves meet the grass */
  transform: translate(-50%, -100%);
}

@media (min-width: 768px) {
  .placement { width: 92px; height: 92px; }
}

.placement .wander {
  width: 100%;
  height: 100%;
  transform-origin: center bottom;
  --s: 1;
  --amp: 14px;
  will-change: transform;
}

.placement.is-alien .wander {
  --amp: 22px;
  animation: wanderX var(--wander-dur, 5s) ease-in-out infinite alternate;
  animation-delay: var(--wander-delay, 0s);
}

.placement.is-cow .wander {
  animation: none;
  transform: scale(var(--s));
}

@keyframes wanderX {
  from { transform: scale(var(--s)) translateX(calc(-1 * var(--amp))); }
  to   { transform: scale(var(--s)) translateX(var(--amp)); }
}

.placement .grow {
  animation: growIn 0.55s ease-out both;
  transform-origin: center bottom;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.placement canvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  transition: transform 0.3s ease-out;
}

.placement:hover canvas {
  transform: scale(1.2) translateY(-4px);
}

@keyframes growIn {
  0% { transform: scale(0.45); opacity: 0; }
  55% { transform: scale(1.05); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------- Beam-up abduction ---------- */
.beam-stage {
  position: absolute;
  inset: 0;
  z-index: 80;
  pointer-events: none;
  overflow: hidden;
}

.beam-stage[hidden] {
  display: none !important;
}

.beam-column {
  position: absolute;
  width: 36px;
  border-radius: 999px;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 180, 0) 0%,
    rgba(255, 240, 120, 0.55) 18%,
    rgba(180, 255, 200, 0.65) 50%,
    rgba(255, 240, 120, 0.5) 82%,
    rgba(255, 255, 180, 0) 100%
  );
  box-shadow:
    0 0 18px rgba(255, 240, 120, 0.55),
    0 0 40px rgba(140, 220, 255, 0.35);
  opacity: 0;
  transform-origin: top center;
  filter: blur(0.2px);
}

.beam-actor {
  position: absolute;
  width: 88px;
  height: 88px;
  transform-origin: center center;
  will-change: left, top, transform, opacity;
}

@media (min-width: 768px) {
  .beam-actor { width: 108px; height: 108px; }
  .beam-column { width: 44px; }
}

.beam-actor canvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.18));
}

.scene-empty {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
  opacity: 0.7;
  pointer-events: none;
  max-width: 12rem;
}

/* ---------- Draw panel ---------- */
.draw-panel {
  display: none;
  grid-template-columns: auto min(92vw, 320px);
  grid-template-areas:
    ". toggle"
    ". hint"
    "palette grid"
    ". actions"
    ". error";
  column-gap: 0.75rem;
  row-gap: 0.85rem;
  align-items: center;
  justify-content: center;
}

@media (min-width: 768px) {
  .draw-panel { display: grid; }
}

.kind-toggle {
  display: flex;
  gap: 0.5rem;
}

.draw-panel > .kind-toggle {
  grid-area: toggle;
  justify-self: center;
}

.kind-btn {
  border: 1px solid var(--green);
  border-radius: 999px;
  padding: 0.35rem 0.9rem;
  font-size: 1rem;
  color: var(--green);
  box-shadow: 0 2px 6px rgba(0,0,0,0.06);
  transition: transform 0.2s ease, background 0.2s ease, color 0.2s ease;
}

.kind-btn:hover { transform: scale(1.05); }

.kind-btn.active {
  background: var(--green);
  color: #fff;
}

.draw-panel > .hint {
  grid-area: hint;
  justify-self: center;
}

.draw-panel > .palette {
  grid-area: palette;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  align-self: center;
}

.swatch {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 2px solid #d1d5dc;
  padding: 0;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.swatch:hover { transform: scale(1.1); }

.swatch.selected {
  border-color: var(--dashed);
  transform: scale(1.12);
}

.swatch.eraser {
  background: #fff;
  position: relative;
  overflow: hidden;
}

.swatch.eraser::after {
  content: "";
  position: absolute;
  inset: 5px;
  background: repeating-linear-gradient(45deg, #ccc, #ccc 2px, #fff 2px, #fff 5px);
  border-radius: 2px;
}

.pixel-grid {
  display: grid;
  grid-template-columns: repeat(32, 1fr);
  width: min(92vw, 320px);
  height: min(92vw, 320px);
  gap: 0;
  background: transparent;
  border: 4px dashed var(--dashed);
  border-radius: 4px;
  touch-action: none;
  user-select: none;
  cursor: crosshair;
  overflow: hidden;
}

.draw-panel > .pixel-grid {
  grid-area: grid;
}

.pixel-cell {
  background: transparent;
  aspect-ratio: 1;
}

.draw-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.draw-panel > .draw-actions {
  grid-area: actions;
  justify-self: center;
}

.pill {
  border: 1px solid var(--green);
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
  font-size: 1.05rem;
  color: var(--green);
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  transition: transform 0.2s ease, background 0.2s ease, color 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.pill:hover { transform: scale(1.08); }

.pill.solid {
  background: var(--green);
  color: #fff;
}

.pill.solid:hover { background: #008138; }

.icon-btn {
  font-size: 1.25rem;
  color: var(--muted);
  border-radius: 999px;
  width: 2rem;
  height: 2rem;
  display: grid;
  place-items: center;
  transition: transform 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  transform: scale(1.15);
  color: var(--ink);
}

.hint, .error-msg {
  font-size: 0.8rem;
  text-align: center;
  max-width: 14rem;
}

.hint {
  color: var(--muted);
  font-size: clamp(1rem, 2.5vw, 1.2rem);
  max-width: 18rem;
}
.error-msg {
  color: #bf000f;
  min-height: 1.1em;
  font-size: 1.05rem;
}

.draw-panel > .error-msg {
  grid-area: error;
  justify-self: center;
}

/* ---------- Mobile draw CTA ---------- */
.mobile-cta {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  width: min(100%, 280px);
  padding-bottom: 1rem;
}

@media (min-width: 768px) {
  .mobile-cta { display: none; }
}

.mobile-cta .pill {
  justify-content: center;
  padding: 0.75rem 1.25rem;
  font-size: 1.15rem;
  background: var(--green);
  color: #fff;
  box-shadow: 0 8px 18px rgba(1, 102, 48, 0.2);
}

.mobile-cta .pill:hover { background: #008138; }

/* ---------- Gallery link ---------- */
.gallery-link {
  margin: 0 0 2rem;
}

@media (min-width: 768px) {
  .gallery-link {
    position: fixed;
    right: 2rem;
    bottom: 2rem;
    margin: 0;
    z-index: 40;
  }
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 243, 0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: max(0.5rem, env(safe-area-inset-top, 0px))
           max(0.5rem, env(safe-area-inset-right, 0px))
           max(0.5rem, env(safe-area-inset-bottom, 0px))
           max(0.5rem, env(safe-area-inset-left, 0px));
}

@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
}

.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--cream);
  width: 100%;
  max-width: 480px;
  border: 1px solid var(--green);
  border-radius: 1.5rem;
  max-height: calc(
    100dvh
    - env(safe-area-inset-top, 0px)
    - env(safe-area-inset-bottom, 0px)
    - 1rem
  );
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem 1rem 1.15rem;
  box-shadow: 0 12px 40px rgba(1, 102, 48, 0.12);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.65rem;
  flex-shrink: 0;
}

.modal-head h2 {
  font-family: var(--font-display);
  font-size: clamp(1.15rem, 4.5vw, 1.5rem);
  font-weight: 700;
  color: var(--green);
}

.modal-head .close {
  font-size: 1.5rem;
  color: var(--muted);
  line-height: 1;
  padding: 0.25rem 0.5rem;
}

.modal .draw-panel {
  --editor-chrome: 12.5rem;
  --grid-size: min(
    320px,
    calc(100vw - 5.75rem),
    calc(
      100dvh
      - env(safe-area-inset-top, 0px)
      - env(safe-area-inset-bottom, 0px)
      - var(--editor-chrome)
    )
  );
  display: grid;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  grid-template-columns: auto var(--grid-size);
  grid-template-areas:
    ". toggle"
    ". hint"
    "palette grid"
    ". actions"
    ". error";
  column-gap: clamp(0.4rem, 2vw, 0.75rem);
  row-gap: clamp(0.45rem, 1.8vh, 0.85rem);
  justify-content: center;
  align-items: center;
}

/* Fit palette + grid + actions within the phone viewport */
.modal .pixel-grid {
  width: var(--grid-size);
  height: var(--grid-size);
  max-width: 100%;
}

.modal .swatch {
  width: clamp(22px, 6.5vw, 28px);
  height: clamp(22px, 6.5vw, 28px);
}

.modal .hint {
  font-size: clamp(0.9rem, 3.5vw, 1.1rem);
  margin: 0;
}

.modal .draw-actions,
.modal .kind-toggle {
  flex-shrink: 0;
}

@media (max-height: 700px) {
  .modal {
    padding: 0.75rem 0.85rem 0.9rem;
    border-radius: 1.15rem;
  }

  .modal-head {
    margin-bottom: 0.4rem;
  }

  .modal .draw-panel {
    --editor-chrome: 11rem;
  }

  .modal .palette {
    gap: 0.3rem;
  }
}

@media (max-height: 580px) {
  .modal .draw-panel {
    --editor-chrome: 10rem;
    grid-template-areas:
      ". toggle"
      "palette grid"
      ". actions"
      ". error";
  }

  .modal .hint {
    display: none;
  }
}

.gallery-filters {
  justify-content: center;
  margin-bottom: 1rem;
  flex-shrink: 0;
}

.modal-scrapbook {
  /* Keep chrome fixed; only the grid scrolls */
  overflow: hidden;
  min-height: min(70dvh, 520px);
}

.modal-scrapbook .gallery-grid {
  flex: 1 1 auto;
  min-height: 12rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  align-content: start;
  padding-bottom: 0.25rem;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 0.85rem;
  min-height: 4rem;
  transition: opacity 0.15s ease;
}

.gallery-grid.is-loading {
  opacity: 0.55;
  pointer-events: none;
}

.gallery-grid .gallery-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 1.5rem 0.5rem;
  font-size: 1.05rem;
}

.gallery-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

.gallery-item canvas {
  width: 72px;
  height: 72px;
  image-rendering: pixelated;
  border: 1px dashed rgba(30, 41, 57, 0.25);
  border-radius: 6px;
  background: rgba(255,255,255,0.4);
}

.load-more {
  display: inline-flex;
  align-self: center;
  flex-shrink: 0;
  margin: 1rem auto 0;
  padding: 0.45rem 1.1rem;
}

.load-more[hidden] {
  display: none !important;
}

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