// ФриТаун — sticky scroll scene: isometric 3D-style house that builds itself
function ScrollScene() {
  const ref = React.useRef(null);
  const [progress, setProgress] = React.useState(0);

  React.useEffect(() => {
    const onScroll = () => {
      if (!ref.current) return;
      const rect = ref.current.getBoundingClientRect();
      const total = Math.max(1, rect.height - window.innerHeight);
      const p = -rect.top / total;
      setProgress(Math.max(0, Math.min(1, p)));
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  // Current step (0, 1, 2)
  const step = progress < 0.34 ? 0 : progress < 0.67 ? 1 : 2;
  const stepNums = ["01", "02", "03"];
  const stepEyes = ["Шаг первый", "Шаг второй", "Шаг третий"];

  // Subtle parallax — slight rotateZ + translateY for camera feel
  // (the SVG is isometric so it already looks 3D; we don't rotateY a flat plane)
  const rotZ = -2 + progress * 4;
  const translateY = -progress * 24;
  const scale = 0.94 + Math.sin(progress * Math.PI) * 0.08;

  // Construction milestones
  const plotMarks  = true;
  const foundation = progress > 0.04;
  const wallsRise  = progress > 0.14;
  const wallsDone  = progress > 0.24;
  const roofFront  = progress > 0.34;
  const roofSide   = progress > 0.42;
  const chimney    = progress > 0.52;
  const door       = progress > 0.60;
  const winFront   = progress > 0.66;
  const winSide    = progress > 0.72;
  const winAttic   = progress > 0.78;
  const lights     = progress > 0.84;
  const smoke      = progress > 0.90;

  return (
    <section className="scroll-scene" ref={ref} aria-label="Процесс строительства">
      <div className="scroll-scene-sticky">
        {/* LEFT TEXT */}
        <div className="scroll-scene-text">
          <div className="scene-step-num" aria-hidden="true">
            {stepNums.map((n, i) => (
              <span key={n} className={step === i ? "on" : ""}>{n}</span>
            ))}
          </div>
          <div className="scene-step-eyebrow">{stepEyes[step]}</div>
          <div className="scene-step-titles">
            <h2 className={`scene-step-title ${step === 0 ? "on" : ""}`}>
              От участка <em>к проекту.</em>
            </h2>
            <h2 className={`scene-step-title ${step === 1 ? "on" : ""}`}>
              От проекта <em>к стройке.</em>
            </h2>
            <h2 className={`scene-step-title ${step === 2 ? "on" : ""}`}>
              От стройки <em>под ключ.</em>
            </h2>
          </div>
        </div>

        {/* RIGHT STAGE */}
        <div className="scroll-scene-stage">
          <div className="scene-house-bg" style={{ transform: `scale(${1 + progress * 0.35})` }}></div>

          <span className={`scene-chip scene-chip-1 ${wallsDone ? "on" : ""}`}>14.2 × 9.8 м</span>
          <span className={`scene-chip scene-chip-2 ${roofFront ? "on" : ""}`}>↕ 6.4 м</span>
          <span className={`scene-chip scene-chip-3 ${door ? "on" : ""}`}>4 спальни</span>
          <span className={`scene-chip scene-chip-4 ${lights ? "on" : ""}`}>Сдан · 142-й</span>

          <div
            className="scene-house"
            style={{
              transform: `rotateZ(${rotZ}deg) translateY(${translateY}px) scale(${scale})`,
            }}
          >
            <svg viewBox="0 0 400 400" className="iso-house">
              <defs>
                <linearGradient id="wallFront" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="0%" stopColor="var(--bg)" stopOpacity="1" />
                  <stop offset="100%" stopColor="var(--bg-2)" stopOpacity="1" />
                </linearGradient>
                <linearGradient id="wallSide" x1="0" y1="0" x2="1" y2="1">
                  <stop offset="0%" stopColor="var(--bg-2)" stopOpacity="1" />
                  <stop offset="100%" stopColor="var(--ink)" stopOpacity="0.18" />
                </linearGradient>
                <linearGradient id="roofFrontG" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="0%" stopColor="var(--accent)" stopOpacity="1" />
                  <stop offset="100%" stopColor="var(--accent)" stopOpacity="0.78" />
                </linearGradient>
                <linearGradient id="roofSideG" x1="0" y1="0" x2="1" y2="0">
                  <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.78" />
                  <stop offset="100%" stopColor="var(--ink)" stopOpacity="0.38" />
                </linearGradient>
                <linearGradient id="foundationFront" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="0%" stopColor="var(--ink)" stopOpacity="0.92" />
                  <stop offset="100%" stopColor="var(--ink)" stopOpacity="0.7" />
                </linearGradient>
                <filter id="softShadow" x="-30%" y="-30%" width="160%" height="160%">
                  <feGaussianBlur in="SourceAlpha" stdDeviation="6" />
                  <feOffset dx="0" dy="6" />
                  <feComponentTransfer><feFuncA type="linear" slope="0.18" /></feComponentTransfer>
                  <feMerge>
                    <feMergeNode/>
                    <feMergeNode in="SourceGraphic"/>
                  </feMerge>
                </filter>
              </defs>

              {/* Ambient ground shadow */}
              <ellipse
                cx="220" cy="345" rx="160" ry="18"
                fill="var(--ink)"
                opacity={foundation ? 0.14 : 0}
                style={{ transition: "opacity .8s" }}
              />

              {/* Ground line + plot stakes */}
              <line x1="30" y1="335" x2="370" y2="335" stroke="currentColor" strokeWidth="1" opacity="0.28" />
              <line x1="70" y1="328" x2="70" y2="344" stroke="currentColor" strokeWidth="1" opacity="0.35" />
              <line x1="250" y1="328" x2="250" y2="344" stroke="currentColor" strokeWidth="1" opacity="0.35" />
              <line x1="70" y1="335" x2="250" y2="335" stroke="currentColor" strokeDasharray="4 6" strokeWidth="1" opacity="0.3" />
              <line x1="250" y1="335" x2="345" y2="280" stroke="currentColor" strokeDasharray="4 6" strokeWidth="1" opacity="0.3" />

              {/* === FOUNDATION === */}
              <g className={`iso-grp ${foundation ? "on" : ""}`} style={{ transformOrigin: "220px 318px" }}>
                {/* front strip */}
                <polygon points="70,310 250,310 250,322 70,322" fill="url(#foundationFront)" />
                {/* right strip (parallelogram) */}
                <polygon points="250,310 345,255 345,267 250,322" fill="var(--ink)" opacity="0.78" />
                {/* foundation top edges (visible "ledge") */}
                <line x1="70" y1="310" x2="250" y2="310" stroke="var(--ink)" strokeWidth="1" opacity="0.5" />
                <line x1="250" y1="310" x2="345" y2="255" stroke="var(--ink)" strokeWidth="1" opacity="0.5" />
              </g>

              {/* === RIGHT WALL (parallelogram) === */}
              <g className={`iso-grp ${wallsRise ? "on" : ""}`} style={{ transformOrigin: "297px 270px" }}>
                <polygon
                  points="250,310 345,255 345,125 250,180"
                  fill="url(#wallSide)"
                  stroke="var(--ink)"
                  strokeWidth="1.2"
                  strokeOpacity="0.5"
                />
                {/* subtle horizontal cladding lines */}
                <line x1="250" y1="245" x2="345" y2="190" stroke="var(--ink)" strokeWidth="0.5" opacity="0.18"/>
                <line x1="250" y1="215" x2="345" y2="160" stroke="var(--ink)" strokeWidth="0.5" opacity="0.18"/>
              </g>

              {/* === FRONT WALL === */}
              <g className={`iso-grp ${wallsRise ? "on" : ""}`} style={{ transformOrigin: "160px 245px" }}>
                <rect
                  x="70" y="180" width="180" height="130"
                  fill="url(#wallFront)"
                  stroke="var(--ink)"
                  strokeWidth="1.4"
                  strokeOpacity="0.55"
                />
                {/* subtle vertical seam */}
                <line x1="160" y1="180" x2="160" y2="310" stroke="var(--ink)" strokeWidth="0.5" opacity="0.1"/>
              </g>

              {/* Wall corner edge — sharp vertical seam where front meets side */}
              <line
                className={`iso-line ${wallsDone ? "on" : ""}`}
                x1="250" y1="180" x2="250" y2="310"
                stroke="var(--ink)" strokeWidth="1.4" opacity="0.7"
              />

              {/* === ROOF FRONT GABLE (triangle) === */}
              <g className={`iso-grp ${roofFront ? "on" : ""}`} style={{ transformOrigin: "160px 140px" }}>
                <polygon points="64,182 256,182 160,108" fill="url(#roofFrontG)" stroke="var(--ink)" strokeWidth="1.2" strokeOpacity="0.5"/>
                {/* roof edge highlight (eaves) */}
                <line x1="64" y1="182" x2="256" y2="182" stroke="var(--ink)" strokeWidth="2" opacity="0.6"/>
              </g>

              {/* === ROOF RIGHT SLOPE (parallelogram) === */}
              <g className={`iso-grp ${roofSide ? "on" : ""}`} style={{ transformOrigin: "255px 90px" }}>
                <polygon
                  points="256,182 351,127 255,53 160,108"
                  fill="url(#roofSideG)"
                  stroke="var(--ink)"
                  strokeWidth="1.2"
                  strokeOpacity="0.5"
                />
                {/* ridge line */}
                <line x1="160" y1="108" x2="255" y2="53" stroke="var(--ink)" strokeWidth="1.4" opacity="0.7"/>
                {/* eave line on side */}
                <line x1="256" y1="182" x2="351" y2="127" stroke="var(--ink)" strokeWidth="1.8" opacity="0.55"/>
              </g>

              {/* === CHIMNEY === */}
              <g className={`iso-grp ${chimney ? "on" : ""}`} style={{ transformOrigin: "210px 70px" }}>
                {/* front face */}
                <rect x="198" y="42" width="20" height="42" fill="var(--ink)" opacity="0.88"/>
                {/* right face (parallelogram, follows depth axis) */}
                <polygon points="218,42 230,35 230,77 218,84" fill="var(--ink)" opacity="0.65"/>
                {/* cap */}
                <rect x="194" y="38" width="28" height="6" fill="var(--ink)"/>
                <polygon points="222,38 234,31 234,37 222,44" fill="var(--ink)" opacity="0.78"/>
              </g>

              {/* === DOOR === */}
              <g className={`iso-grp ${door ? "on" : ""}`} style={{ transformOrigin: "160px 280px" }}>
                {/* door frame */}
                <rect x="146" y="232" width="32" height="78" fill="var(--ink)" opacity="0.92"/>
                {/* light reveal on left side */}
                <rect x="146" y="232" width="2" height="78" fill="var(--bg)" opacity="0.25"/>
                {/* handle */}
                <circle cx="172" cy="272" r="2" fill="var(--bg)" opacity="0.8"/>
                {/* step */}
                <rect x="138" y="310" width="48" height="6" fill="var(--ink)" opacity="0.7"/>
                <polygon points="186,310 192,306 192,312 186,316" fill="var(--ink)" opacity="0.55"/>
              </g>

              {/* === FRONT WINDOWS === */}
              <g className={`iso-grp ${winFront ? "on" : ""}`} style={{ transformOrigin: "160px 215px" }}>
                {/* Left window */}
                <rect x="92" y="208" width="42" height="44" fill="var(--bg)" stroke="var(--ink)" strokeWidth="1.4"/>
                <line x1="113" y1="208" x2="113" y2="252" stroke="var(--ink)" strokeWidth="0.8"/>
                <line x1="92" y1="230" x2="134" y2="230" stroke="var(--ink)" strokeWidth="0.8"/>
                <rect x="86" y="204" width="54" height="6" fill="var(--ink)" opacity="0.55"/>
                <rect x="86" y="252" width="54" height="3" fill="var(--ink)" opacity="0.7"/>

                {/* Right window */}
                <rect x="190" y="208" width="42" height="44" fill="var(--bg)" stroke="var(--ink)" strokeWidth="1.4"/>
                <line x1="211" y1="208" x2="211" y2="252" stroke="var(--ink)" strokeWidth="0.8"/>
                <line x1="190" y1="230" x2="232" y2="230" stroke="var(--ink)" strokeWidth="0.8"/>
                <rect x="184" y="204" width="54" height="6" fill="var(--ink)" opacity="0.55"/>
                <rect x="184" y="252" width="54" height="3" fill="var(--ink)" opacity="0.7"/>
              </g>

              {/* === SIDE WINDOWS (parallelograms on right face) === */}
              <g className={`iso-grp ${winSide ? "on" : ""}`} style={{ transformOrigin: "300px 220px" }}>
                {/* window 1 */}
                <polygon points="271,265 309,243 309,205 271,227" fill="var(--bg)" stroke="var(--ink)" strokeWidth="1.2"/>
                <line x1="290" y1="234" x2="290" y2="254" stroke="var(--ink)" strokeWidth="0.7"/>
                <line x1="271" y1="246" x2="309" y2="224" stroke="var(--ink)" strokeWidth="0.7"/>

                {/* window 2 */}
                <polygon points="313,240 339,225 339,187 313,202" fill="var(--bg)" stroke="var(--ink)" strokeWidth="1.2"/>
                <line x1="326" y1="194" x2="326" y2="232" stroke="var(--ink)" strokeWidth="0.6"/>
                <line x1="313" y1="221" x2="339" y2="206" stroke="var(--ink)" strokeWidth="0.6"/>
              </g>

              {/* === ATTIC WINDOW IN GABLE === */}
              <g className={`iso-grp ${winAttic ? "on" : ""}`} style={{ transformOrigin: "160px 150px" }}>
                <rect x="146" y="138" width="28" height="34" fill="var(--bg)" stroke="var(--ink)" strokeWidth="1.2"/>
                <line x1="160" y1="138" x2="160" y2="172" stroke="var(--ink)" strokeWidth="0.7"/>
                <line x1="146" y1="155" x2="174" y2="155" stroke="var(--ink)" strokeWidth="0.7"/>
                <rect x="142" y="135" width="36" height="4" fill="var(--ink)" opacity="0.55"/>
              </g>

              {/* === LIGHT GLOWS (front windows + attic + side windows) === */}
              <g className={`iso-lights ${lights ? "on" : ""}`}>
                <rect x="93" y="209" width="40" height="42" fill="var(--accent)" opacity="0.92"/>
                <rect x="191" y="209" width="40" height="42" fill="var(--accent)" opacity="0.92" style={{ animationDelay: "0.5s" }}/>
                <rect x="147" y="139" width="26" height="32" fill="var(--accent)" opacity="0.92" style={{ animationDelay: "1s" }}/>
                <polygon points="272,264 308,243 308,206 272,228" fill="var(--accent)" opacity="0.75" style={{ animationDelay: "0.3s" }}/>
                <polygon points="314,239 338,225 338,188 314,202" fill="var(--accent)" opacity="0.75" style={{ animationDelay: "0.8s" }}/>
                {/* door window glow strip */}
                <rect x="148" y="238" width="28" height="14" fill="var(--accent)" opacity="0.6"/>
              </g>

              {/* === SMOKE === */}
              <g className={`iso-smoke ${smoke ? "on" : ""}`}>
                <path d="M208 40 C 200 24, 220 16, 210 0 C 200 -16, 220 -22, 210 -38"
                  fill="none" stroke="color-mix(in oklab, var(--muted) 80%, transparent)"
                  strokeWidth="2.4" strokeLinecap="round" opacity="0.7" />
                <path d="M214 38 C 222 24, 204 16, 214 2"
                  fill="none" stroke="color-mix(in oklab, var(--muted) 60%, transparent)"
                  strokeWidth="1.8" strokeLinecap="round" opacity="0.5" />
              </g>

              {/* Sun marker */}
              <circle
                cx="355" cy="65" r="14"
                fill="var(--accent)"
                opacity={progress > 0.92 ? 0.85 : 0}
                style={{ transition: "opacity .8s" }}
              />

              {/* Measurement arrow under house */}
              <g opacity={wallsDone ? 0.55 : 0} style={{ transition: "opacity .6s" }}>
                <line x1="70" y1="358" x2="250" y2="358" stroke="var(--ink)" strokeWidth="1" />
                <line x1="70" y1="354" x2="70" y2="362" stroke="var(--ink)" strokeWidth="1" />
                <line x1="250" y1="354" x2="250" y2="362" stroke="var(--ink)" strokeWidth="1" />
                <rect x="142" y="350" width="36" height="16" fill="var(--bg-2)" />
                <text x="160" y="362" textAnchor="middle" fontSize="11" fontFamily="ui-monospace, monospace" fill="var(--ink)">
                  14.2 м
                </text>
              </g>
            </svg>
          </div>
        </div>

        {/* SCROLL PROGRESS TRACK */}
        <div className="scroll-scene-track">
          <div className="scroll-scene-track-fill" style={{ width: `${Math.round(progress * 100)}%` }}></div>
          <div className="scroll-scene-track-label">
            <span>Прокрутите вниз</span>
            <span>{Math.round(progress * 100).toString().padStart(2, "0")} / 100</span>
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { ScrollScene });
