/* ───────────── Transición oficina → ciudad ───────────── */
#office {
  transition:
    transform 640ms cubic-bezier(0.22, 1, 0.36, 1),
    opacity 420ms ease;
  will-change: transform, opacity;
}

html.to-city #office {
  transform: scale(var(--zoom, 4));
  opacity: 0;
  pointer-events: none;
}

#city {
  --frame: clamp(10px, 1.6vw, 22px);
  --pad: clamp(8px, 3vw, 40px);
  --edge: max(var(--pad), calc(var(--frame) + 10px));
  --title-room: 72px;
  position: fixed;
  inset: 0;
  z-index: 300;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  place-items: center;
  justify-content: center;
  align-content: center;
  padding-inline: var(--edge);
  padding-block: calc(var(--edge) + var(--title-room) / 2);
  background: radial-gradient(ellipse at center, rgba(11, 15, 23, 0.25), rgba(11, 15, 23, 0.85));
  opacity: 0;
  transform: scale(0.9);
  transition:
    opacity 420ms ease,
    transform 640ms cubic-bezier(0.22, 1, 0.36, 1);
  will-change: transform, opacity;
}

#city[hidden] { display: none; }

html.to-city #city {
  opacity: 1;
  transform: none;
}

html.city-leaving #city {
  transition:
    opacity 320ms ease,
    transform 520ms cubic-bezier(0.4, 0, 1, 1);
}

/* ───────────── Ventana + marco ───────────── */
.city-window {
  position: relative;
  justify-self: center;
  width: min(100%, calc((100dvh - 2 * var(--edge) - var(--title-room)) * 996 / 571));
  max-height: calc(100dvh - 2 * var(--edge) - var(--title-room));
  aspect-ratio: 996 / 571;
}

.city-frame {
  position: absolute;
  inset: calc(var(--frame) * -1);
  pointer-events: none;
  z-index: 20;
  border: var(--frame) solid #3a2418;
  border-image: linear-gradient(135deg, #6b4128, #3a2418 40%, #24150d) 1;
  box-shadow:
    0 0 0 3px #0b0f17,
    0 0 0 6px #1b2738,
    0 18px 60px rgba(0, 0, 0, 0.7);
}

/* Parteluz central de la ventana + reflejo del vidrio */
.city-frame::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(115deg, transparent 18%, rgba(255, 255, 255, 0.06) 22%, transparent 30%),
    linear-gradient(115deg, transparent 58%, rgba(255, 255, 255, 0.04) 61%, transparent 66%);
}

.city-frame::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(var(--frame) * -1);
  height: var(--frame);
  background: linear-gradient(#5a3620, #2a180e);
}

.city-title {
  position: absolute;
  z-index: 30;
  top: calc(var(--frame) * -1);
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  padding: 6px 16px 7px;
  color: #fff4d6;
  background: #1b2738;
  font-family: "VT323", ui-monospace, monospace;
  font-size: clamp(18px, 2.4vw, 28px);
  line-height: 1;
  letter-spacing: 0.5px;
  white-space: nowrap;
  text-shadow: 2px 2px 0 #000;
  pointer-events: none;
  box-shadow:
    inset -3px -3px 0 #0b0f17,
    inset 3px 3px 0 #3d5677,
    0 0 0 3px #0b0f17,
    0 0 0 5px #ffb060,
    0 0 0 8px #0b0f17;
}

.city-scroll {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #0b0f17;
}

.city-stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  contain: paint;
  container-type: size;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* El recorte vive dentro de la escena. Si el dibujo se saliera del stage,
   el scroll del celular llegaría hasta el borde oscuro de la derecha. */
.city-scene {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.city-zoom {
  position: absolute;
  inset: 0;
  transform: translateY(-2%) scale(1.018, 1.05);
  transform-origin: right top;
}

.city-stage img,
#city .sprite {
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
  -webkit-user-drag: none;
}

.city-bg,
.city-lights {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

/* ───────────── Cielo según la hora (html[data-sky], mismo que la ventana de la oficina) ───────────── */
.city-sky {
  position: absolute;
  inset: 0;
  pointer-events: none;
  -webkit-mask: url("assets/city/city-sky-mask.png") 0 0 / 100% 100%;
  mask: url("assets/city/city-sky-mask.png") 0 0 / 100% 100%;
  opacity: 0;
  transition: opacity 1.2s ease;
}

html[data-sky="day"] .city-sky {
  opacity: 0.94;
  background: linear-gradient(to bottom, #4fa3e3 0%, #8fcdf2 55%, #e9f1dc 100%);
}

/* Nubes pixeladas: cada sombra es un "pixel" de 1em */
.city-cloud {
  position: absolute;
  left: 0;
  top: var(--top);
  width: 1em;
  height: 1em;
  font-size: var(--px, 1.1cqh);
  display: none;
  color: #fff;
  box-shadow:
    3em 0, 4em 0, 5em 0, 6em 0,
    1em 1em, 2em 1em, 3em 1em, 4em 1em, 5em 1em, 6em 1em, 7em 1em, 8em 1em,
    0 2em, 1em 2em, 2em 2em, 3em 2em, 4em 2em, 5em 2em, 6em 2em, 7em 2em, 8em 2em, 9em 2em, 10em 2em,
    1em 3em #d8ecf7, 2em 3em #d8ecf7, 3em 3em #d8ecf7, 4em 3em #d8ecf7, 5em 3em #d8ecf7, 6em 3em #d8ecf7, 7em 3em #d8ecf7, 8em 3em #d8ecf7, 9em 3em #d8ecf7;
  animation: cloud-drift var(--dur) linear var(--delay) infinite;
}

html[data-sky="day"] .city-cloud { display: block; }

@keyframes cloud-drift {
  from { transform: translateX(-12em); }
  to { transform: translateX(calc(100cqw + 2em)); }
}

html[data-sky="evening"] .city-sky {
  opacity: 0.35;
  background: linear-gradient(to bottom, #4b2d6e 0%, #c65a5a 55%, #ffb36b 100%);
  mix-blend-mode: soft-light;
}

html[data-sky="night"] .city-sky {
  opacity: 0.96;
  background:
    radial-gradient(circle at 84% 15%, #fff7d6 0 2.2%, rgba(255, 247, 214, 0.25) 2.4% 5%, transparent 5.2%),
    linear-gradient(to bottom, #050813 0%, #0e1630 55%, #22284a 100%);
}

.city-sun,
.city-stars {
  position: absolute;
  display: none;
}

html[data-sky="day"] .city-sun {
  display: block;
  right: 12%;
  top: 9%;
  width: 6cqh;
  height: 6cqh;
  border-radius: 50%;
  background: #ffe58a;
  box-shadow: 0 0 0 0.8cqh #ffd25e, 0 0 4cqh 1cqh rgba(255, 230, 140, 0.7);
  animation: sun-pulse 4s steps(4) infinite;
}

html[data-sky="night"] .city-stars {
  display: block;
  inset: 0;
  background-image:
    radial-gradient(1px 1px at 8% 12%, #fff 99%, transparent),
    radial-gradient(1px 1px at 17% 30%, #cfe3ff 99%, transparent),
    radial-gradient(2px 2px at 29% 8%, #fff 99%, transparent),
    radial-gradient(1px 1px at 41% 22%, #fff 99%, transparent),
    radial-gradient(1px 1px at 55% 6%, #cfe3ff 99%, transparent),
    radial-gradient(2px 2px at 63% 27%, #fff 99%, transparent),
    radial-gradient(1px 1px at 72% 10%, #fff 99%, transparent),
    radial-gradient(1px 1px at 93% 30%, #cfe3ff 99%, transparent),
    radial-gradient(1px 1px at 4% 34%, #fff 99%, transparent),
    radial-gradient(2px 2px at 48% 34%, #fff 99%, transparent);
  animation: twinkle 3s steps(2) infinite;
}

@keyframes sun-pulse {
  50% { box-shadow: 0 0 0 1cqh #ffd25e, 0 0 5cqh 1.4cqh rgba(255, 230, 140, 0.8); }
}

@keyframes twinkle {
  50% { opacity: 0.55; }
}

/* Edificios: de día más brillantes, de noche oscuros con ventanas/neones encendidos */
.city-bg { transition: filter 1.2s ease; }
html[data-sky="day"] .city-bg { filter: brightness(1.12) saturate(1.05) hue-rotate(-6deg); }
html[data-sky="night"] .city-bg { filter: none; }

.city-lights {
  opacity: 0;
  z-index: 6;
  -webkit-mask: url("assets/city/city-lights-mask.png?v=3") 0 0 / 100% 100%;
  mask: url("assets/city/city-lights-mask.png?v=3") 0 0 / 100% 100%;
  transition: opacity 1.2s ease;
}

html[data-sky="night"] .city-lights {
  opacity: 1;
  animation: neon-flicker 7s steps(1) infinite;
}

@keyframes neon-flicker {
  0%, 100% { opacity: 1; }
  41% { opacity: 0.82; }
  42% { opacity: 1; }
  77% { opacity: 0.9; }
  78% { opacity: 1; }
}

/* Filtro global de iluminación: une fondo + sprites bajo la misma luz */
.city-glow {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  mix-blend-mode: soft-light;
  transition: background 1.2s ease;
  background: linear-gradient(to bottom, rgba(120, 60, 160, 0.35), rgba(255, 140, 70, 0.55) 60%, rgba(255, 190, 110, 0.45));
}

.city-glow::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse 120% 90% at 50% 40%, transparent 55%, rgba(10, 5, 20, 0.45));
  mix-blend-mode: multiply;
}

html[data-sky="day"] .city-glow {
  background: linear-gradient(to bottom, rgba(150, 210, 255, 0.35), rgba(255, 245, 210, 0.3));
}

html[data-sky="night"] .city-glow {
  background: linear-gradient(to bottom, rgba(6, 10, 28, 0.62), rgba(8, 12, 32, 0.45));
  mix-blend-mode: multiply;
}

html[data-sky="night"] .city-glow::after {
  background: none;
}

/* ───────────── Carriles de sprites (overflow: hidden en los bordes) ───────────── */
.city-lane {
  position: absolute;
  left: 1.6%;
  right: 1.6%;
  overflow: hidden;
  pointer-events: none;
}

.city-air { top: 3%; height: 38%; z-index: 4; overflow: visible; }
.city-walk { bottom: 16%; height: 10%; z-index: 6; }
.city-road.far { bottom: 7.4%; height: 11.5%; z-index: 7; }
.city-road.near { bottom: 3%; height: 13%; z-index: 8; }

.city-road {
  left: 0;
  right: 0;
}

.city-lane > * {
  position: absolute;
  left: 0;
  bottom: 0;
  backface-visibility: hidden;
  will-change: transform;
}

.city-car,
.city-person {
  width: 100%;
}

/* Avión: un único elemento; city.js cambia el sprite en cada vuelta → nunca hay dos a la vez */
/* Un solo avión. Cruza en ~42s y espera fuera de pantalla antes del siguiente diseño. */
.city-plane {
  z-index: 5;
  top: var(--top, 18%);
  bottom: auto;
  height: var(--h, 16%);
  animation: plane-fly 50s linear infinite;
}

@keyframes plane-fly {
  0% { transform: translate3d(-140%, 0, 0); }
  55% { transform: translate3d(calc(100cqw + 30%), 0, 0); }
  100% { transform: translate3d(calc(100cqw + 30%), 0, 0); }
}

/* La avioneta amarilla entra por la derecha, con la nariz hacia la izquierda. */
.city-plane.west {
  animation-name: plane-fly-west;
}

@keyframes plane-fly-west {
  0% { transform: translate3d(calc(100cqw + 30%), 0, 0); }
  55% { transform: translate3d(-140%, 0, 0); }
  100% { transform: translate3d(-140%, 0, 0); }
}

/* Vehículos: siempre de izquierda a derecha. El % es el ancho del carril. */
@keyframes drive-right {
  from { transform: translate3d(-100%, 0, 0); }
  to { transform: translate3d(100%, 0, 0); }
}

@keyframes walk-left {
  from { transform: translate3d(100%, 0, 0); }
  to { transform: translate3d(-100%, 0, 0); }
}

.city-car {
  height: var(--h, 78%);
  animation: drive-right var(--dur) linear var(--delay) infinite;
}

.city-car img {
  height: 100%;
  width: auto;
  display: block;
  animation: car-bump 0.45s ease-in-out infinite;
}

@keyframes car-bump {
  0%, 100% { transform: translate3d(0, 0, 0); }
  50% { transform: translate3d(0, -1px, 0); }
}

.city-person {
  height: var(--h, 92%);
  animation: drive-right var(--dur) linear var(--delay) infinite;
}

.city-person.left { animation-name: walk-left; }
.city-person.left img { animation-name: step-bob-left; }

@keyframes step-bob-left {
  0%, 100% { transform: translate3d(0, 0, 0) scaleX(-1); }
  50% { transform: translate3d(0, -1px, 0) scaleX(-1); }
}

.city-person img {
  height: 100%;
  width: auto;
  display: block;
  animation: step-bob 0.55s ease-in-out infinite;
}

@keyframes step-bob {
  0%, 100% { transform: translate3d(0, 0, 0); }
  50% { transform: translate3d(0, -1px, 0); }
}

html[data-sky="night"] .city-car,
html[data-sky="night"] .city-person.day-only {
  display: none;
}

html[data-sky="night"] .city-car img {
  filter: none;
}

/* ───────────── Edificios interactivos ───────────── */
.city-buildings {
  position: absolute;
  inset: 0;
  z-index: 9;
}

.city-building {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--w);
  height: calc(82% - var(--y));
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  outline: none;
}

.city-building::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0;
  background: rgba(255, 220, 150, 0.08);
  box-shadow: inset 0 0 0 2px rgba(255, 230, 170, 0.55);
  transition: opacity 200ms steps(3);
}

.city-building:focus-visible::before,
.city-building.active::before {
  opacity: 1;
}

.city-sign {
  position: absolute;
  left: 0;
  bottom: calc(100% + 1.2cqh);
  box-sizing: border-box;
  display: grid;
  justify-items: center;
  gap: 0.6cqh;
  width: 100%;
  max-width: none;
  min-width: 0;
  padding: 8px 10px 6px;
  color: #1b2738;
  background: #fff;
  font-family: "VT323", ui-monospace, monospace;
  font-size: clamp(12px, 1.8cqh, 16px);
  line-height: 1;
  text-align: center;
  text-shadow: none;
  white-space: normal;
  pointer-events: none;
  opacity: 0;
  transform: translate(0, 10px);
  transition: opacity 220ms ease, transform 220ms steps(4);
  box-shadow:
    inset 0 0 0 3px var(--accent, #ffb060),
    inset 0 0 0 6px #0b0f17,
    4px 4px 0 0 rgba(0, 0, 0, 0.45);
}

.city-stage .city-sign img {
  display: block;
  width: auto;
  height: auto;
  max-width: 90%;
  max-height: 32px;
  object-fit: contain;
  object-position: center;
  image-rendering: auto;
}

.city-building[data-id="institucional"] .city-sign img,
.city-building[data-id="electro"] .city-sign img {
  max-height: 42px;
}

.city-sign small {
  font-size: 0.72em;
  color: #1b2738;
}

.city-building[data-id="boutique"] .city-sign {
  bottom: calc(100% + 8cqh);
}

.city-building.active .city-sign {
  opacity: 1;
  transform: translate(0, 0);
  animation: sign-float 2.4s ease-in-out 220ms infinite;
}

@keyframes sign-float {
  50% { margin-bottom: 0.8cqh; }
}

@media (hover: hover) and (pointer: fine) {
  .city-building:hover::before { opacity: 1; }
  .city-building:hover .city-sign {
    opacity: 1;
    transform: translate(0, 0);
    animation: sign-float 2.4s ease-in-out 220ms infinite;
  }

  .city-buildings:hover .city-building.active:not(:hover) .city-sign {
    opacity: 0;
    animation: none;
  }
}

/* ───────────── Botón retro "Regresar a la oficina" ───────────── */
.city-back {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 1000;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px 9px;
  color: #fff4d6;
  background: #1b2738;
  border: 0;
  border-radius: 0;
  font-family: "VT323", ui-monospace, monospace;
  font-size: clamp(18px, 2.2vw, 24px);
  line-height: 1;
  text-shadow: 2px 2px 0 #000;
  cursor: pointer;
  box-shadow:
    inset -3px -3px 0 #0b0f17,
    inset 3px 3px 0 #3d5677,
    0 0 0 3px #0b0f17,
    0 0 0 5px #ffb060,
    0 0 0 8px #0b0f17,
    6px 6px 0 8px rgba(0, 0, 0, 0.45);
}

.city-back span { color: #ffb060; animation: back-blink 1s steps(1) infinite; }

.city-back:hover,
.city-back:focus-visible {
  background: #ffb060;
  color: #0b0f17;
  text-shadow: none;
  outline: none;
}

.city-back:hover span,
.city-back:focus-visible span { color: #0b0f17; }

.city-back:active {
  transform: translate(2px, 2px);
  box-shadow:
    inset 3px 3px 0 #0b0f17,
    0 0 0 3px #0b0f17,
    0 0 0 5px #ffb060,
    0 0 0 8px #0b0f17;
}

@keyframes back-blink {
  50% { opacity: 0.25; }
}

.city-hint {
  display: none;
  margin: 0;
}

/* ───────────── Móvil: scroll horizontal con snap por edificio ───────────── */
@media (max-aspect-ratio: 4/3) {
  #city {
    padding: calc(150px + env(safe-area-inset-top)) 8px calc(18px + env(safe-area-inset-bottom));
    align-content: stretch;
    justify-items: stretch;
    gap: 8px;
  }

  .city-back {
    top: calc(max(12px, env(safe-area-inset-top)) + 36px);
  }

  .city-title {
    top: calc(var(--frame) * -1 - 10px);
    transform: translate(-50%, -100%);
    font-size: clamp(16px, 4.8vw, 22px);
    letter-spacing: 0;
    padding: 5px 12px 6px;
  }

  .city-window {
    justify-self: stretch;
    align-self: stretch;
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: none;
    aspect-ratio: auto;
  }

  .city-scroll {
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    overscroll-behavior-x: none;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .city-scroll::-webkit-scrollbar { display: none; }

  .city-stage {
    width: auto;
    height: 100%;
    aspect-ratio: 996 / 571;
  }

  .city-building { scroll-snap-align: center; }
  .city-building.edge-right { scroll-snap-align: end; }

  .city-hint {
    display: block;
    position: relative;
    z-index: 40;
    margin: 12px 0 0;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    color: #fff4d6;
    font-size: 18px;
    line-height: 1.35;
    text-align: center;
    text-shadow: 2px 2px 0 #000;
    animation: back-blink 1.6s steps(1) infinite;
  }
}

@media (max-aspect-ratio: 4/3) and (max-width: 420px) {
  #city {
    padding-top: calc(150px + env(safe-area-inset-top));
  }
}

@media (prefers-reduced-motion: reduce) {
  #office,
  #city { transition-duration: 1ms !important; transition-delay: 0ms !important; }
  .city-lane > *,
  .city-lane img,
  .city-sign { animation: none !important; }
}
