// ─────────────────────────────────────────────────────────────
// THE DEFINITIVE CONCEPT — recreating the actual WXG customer
// satisfaction survey, pixel-faithful to live collateral:
//
//   • Full-bleed animated skyline at top (uses the real WXG
//     city-line path data). Amber overlay reveals left-to-right
//     in lockstep with the stepper.
//   • Stacked WXG / WAXMAN GROUP logo centered, breaks the
//     skyline visually.
//   • White card, sharp corners, soft shadow.
//   • Pastel squares corner motif (red / gold / green / blue
//     from the brand book's secondary palette).
//   • Project + milestone meta as key-value rows.
//   • Numbered circular stepper, amber active + amber-fill done.
//   • Rating as thin-bordered numbered circles 6—10.
//   • Free text with amber send icon.
//   • Outlined "המשך" button during journey, amber-filled
//     "אישור ושליחה" on the final step.
//
// Everything else (typography hierarchy, Calibri Light weight,
// amber-square punctuation, mono micro-labels) follows the
// brand book.
// ─────────────────────────────────────────────────────────────

const RL_TYPE = WXG_FONT;
const RL_MONO = WXG_MONO;

// Palette — locked to the WXG brand book primaries + secondary pastels.
const RL = {
  amber:    '#FCAF17',
  amberHover: '#E89E0F',
  amberSoft: 'rgba(252, 175, 23, 0.10)',
  ink:      '#1F1F1F',
  grey:     '#4D4D4D',
  greyMid:  '#8A8A8A',
  greyLight: '#CDCDCD',
  rule:     '#E8E8E8',
  paper:    '#F5F5F4',
  paperWarm:'#FAF8F4',
  card:     '#FFFFFF',
  red:      '#E5B2B0',
  // Required-marker red — matches the small asterisk used in the
  // real survey screenshots (a deep wine red, not the pink).
  required: '#B0303B',
};

// ─────────────────────────────────────────────────────────────
// Full-bleed animated skyline. Uses the real WXG city-line path,
// with an amber overlay revealed left-to-right by clipping.
// ─────────────────────────────────────────────────────────────
const RL_CITY_PATH = (typeof window !== 'undefined' && window.WXG_CITY_PATH) || '';
const RL_CITY_DETAILS = (typeof window !== 'undefined' && window.WXG_CITY_DETAILS) || [];
const RL_CITY_VB   = (typeof window !== 'undefined' && window.WXG_CITY_VIEWBOX) || '0 0 3341 366';
const RL_CITY_W = parseFloat(RL_CITY_VB.split(/\s+/)[2]) || 3341;
const RL_CITY_H = parseFloat(RL_CITY_VB.split(/\s+/)[3]) || 366;

// Small black square markers at architectural high points along
// the skyline — purely decorative, matches the WXG website SVG.
const RL_CITY_MARKERS = [
  { x: 81,   y: 276, w: 10, h: 10 },
  { x: 300,  y: 40,  w: 10, h: 10 },
  { x: 410,  y: 125, w: 10, h: 10 },
  { x: 592,  y: 183, w: 10, h: 10 },
  { x: 762,  y: 333, w: 10, h: 10 },
  { x: 1120, y: 152, w: 10, h: 10 },
  { x: 1233, y: 326, w: 10, h: 10 },
  { x: 1504, y: 148, w: 10, h: 10 },
  { x: 1708, y: 267, w: 10, h: 10 },
  { x: 1938, y: 254, w: 10, h: 10 },
  { x: 2068, y: 123, w: 10, h: 10 },
  { x: 2346, y: 92,  w: 10, h: 10 },
  { x: 2658, y: 111, w: 10, h: 10 },
  { x: 2871, y: 290, w: 10, h: 10 },
  { x: 3169, y: 302, w: 10, h: 10 },
];

function RL_Skyline({ progress, mobile }) {
  // Stable clip-path id so multiple instances don't collide.
  const clipId = React.useMemo(
    () => 'rl-skyline-clip-' + Math.random().toString(36).slice(2, 8), []);

  const padBottom = 8;

  return (
    <div style={{
      position: 'absolute', inset: 0, pointerEvents: 'none',
    }}>
      <svg width="100%" height="100%"
        viewBox={`0 0 ${RL_CITY_W} ${RL_CITY_H + padBottom}`}
        preserveAspectRatio="xMidYMax meet"
        style={{ display: 'block', overflow: 'visible' }}>
        <defs>
          <clipPath id={clipId}>
            <rect x="0" y="0"
              width={progress * RL_CITY_W} height={RL_CITY_H + 50}
              style={{ transition: 'width 900ms cubic-bezier(.2,.8,.2,1)' }} />
          </clipPath>
        </defs>

        {/* base grey outline — main skyline silhouette */}
        <path d={RL_CITY_PATH} fill="none" stroke={RL.ink}
          strokeWidth="3.5" strokeLinejoin="miter" strokeLinecap="square"
          opacity="0.85" />

        {/* base grey — inner architectural details (spires, antennas,
            internal floor lines, slants, rooftop fixtures) */}
        {RL_CITY_DETAILS.map((d, i) => (
          <path key={'b' + i} d={d} fill="none" stroke={RL.ink}
            strokeWidth="2.4" strokeLinejoin="miter" strokeLinecap="round"
            opacity="0.75" />
        ))}

        {/* amber-lit overlay, revealed by progress clip */}
        <g clipPath={`url(#${clipId})`}>
          <path d={RL_CITY_PATH} fill="none" stroke={RL.amber}
            strokeWidth="4" strokeLinejoin="miter" strokeLinecap="square" />
          {RL_CITY_DETAILS.map((d, i) => (
            <path key={'a' + i} d={d} fill="none" stroke={RL.amber}
              strokeWidth="2.8" strokeLinejoin="miter" strokeLinecap="round" />
          ))}
        </g>

        {/* small black markers along the line */}
        {RL_CITY_MARKERS.map((m, i) => (
          <rect key={i} x={m.x} y={m.y} width={m.w} height={m.h} fill={RL.ink} />
        ))}
      </svg>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Numbered circular stepper — the signature WXG survey progress.
// ─────────────────────────────────────────────────────────────
function RL_Stepper({ current, total, mobile }) {
  // Steps render right-to-left in RTL so step 1 lives at the right.
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      width: '100%', gap: 0,
    }}>
      {Array.from({ length: total }, (_, i) => {
        const n = i + 1;
        const done = current > n;
        const active = current === n;
        const upcoming = current < n;
        const isLast = i === total - 1;

        // Connector color — between this step and the next, painted on the LEFT
        // (which is "after" this step in RTL). Amber once this step is done.
        const connectorActive = current > n;

        const size = mobile ? 28 : 34;

        return (
          <React.Fragment key={n}>
            <div style={{
              display: 'flex', flexDirection: 'column', alignItems: 'center',
              flexShrink: 0,
            }}>
              <div style={{
                width: size, height: size, borderRadius: '50%',
                background: done ? RL.amber : active ? RL.card : RL.greyLight,
                border: active ? `2px solid ${RL.amber}` : 'none',
                color: done ? RL.ink : active ? RL.grey : '#FFFFFF',
                display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
                fontFamily: RL_TYPE, fontWeight: 400, fontSize: mobile ? 13 : 14,
                transition: 'all 300ms cubic-bezier(.2,.8,.2,1)',
                position: 'relative',
              }}>
                {n}
              </div>
            </div>
            {!isLast && (
              <div style={{
                flex: 1, height: 1, margin: '0 4px',
                background: connectorActive ? RL.amber : RL.greyLight,
                transition: 'background 400ms ease',
              }} />
            )}
          </React.Fragment>
        );
      })}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Card header — project + milestone meta, key/value pairs.
// ─────────────────────────────────────────────────────────────
function RL_CardHeader({ mobile }) {
  return (
    <div style={{
      paddingBottom: mobile ? 18 : 22,
      borderBottom: `1px solid ${RL.rule}`,
      display: 'flex', justifyContent: 'center',
    }}>
      <div style={{
        width: '100%', maxWidth: mobile ? 300 : 520,
        display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start',
        gap: mobile ? 18 : 48,
      }}>
      {[
        ['פרויקט',       'מגדלי יבנה צפון'],
        ['אבן דרך',      'אחת לשנה'],
      ].map(([k, v], i) => (
          <div key={i}>
            <div style={{
              fontFamily: RL_TYPE, fontWeight: 700,
              fontSize: mobile ? 14 : 16, color: RL.ink, marginBottom: 4,
            }}>{k}</div>
            <div style={{
              fontFamily: RL_TYPE, fontWeight: 300,
              fontSize: mobile ? 13 : 15, color: RL.grey,
            }}>{v}</div>
          </div>
      ))}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Question controls — rating circles, single-choice, free text
// ─────────────────────────────────────────────────────────────
function RL_Rating({ q, value, onChange, mobile }) {
  // Render order with dir="rtl": first DOM child sits visually on the RIGHT.
  // Numbers go [6,7,8,9,10] so 6 ends up next to the min/negative label
  // (which is on the right), 10 next to the max/positive label (left).
  const numbers = (
    <div style={{
      display: 'flex', alignItems: 'center',
      gap: mobile ? 6 : 14,
      justifyContent: mobile ? 'space-between' : 'center',
    }}>
      {Array.from({ length: 5 }, (_, i) => {
        const n = i + 6; // 6, 7, 8, 9, 10
        const active = value === n;
        const sz = mobile ? 44 : 50;
        return (
          <button key={n} onClick={() => onChange(n)} style={{
            width: sz, height: sz, borderRadius: '50%',
            background: active ? RL.amberSoft : RL.card,
            border: `1px solid ${active ? RL.amber : RL.greyLight}`,
            color: active ? RL.ink : RL.grey,
            fontFamily: RL_TYPE, fontWeight: active ? 400 : 300,
            fontSize: mobile ? 17 : 20, cursor: 'pointer',
            transition: 'all 180ms cubic-bezier(.2,.8,.2,1)',
            outline: 'none', padding: 0, flexShrink: 0,
          }}
          onMouseOver={e => { if (!active) { e.currentTarget.style.borderColor = RL.greyMid; e.currentTarget.style.color = RL.ink; } }}
          onMouseOut={e => { if (!active) { e.currentTarget.style.borderColor = RL.greyLight; e.currentTarget.style.color = RL.grey; } }}>
            {n}
          </button>
        );
      })}
    </div>
  );

  if (mobile) {
    // Stacked: circles row, then labels row beneath (negative right, positive left)
    return (
      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        {numbers}
        <div style={{
          display: 'flex', justifyContent: 'space-between',
          fontFamily: RL_TYPE, fontWeight: 300, fontSize: 12,
          color: RL.greyMid, paddingInline: 4,
        }}>
          <span>{q.minLabel}</span>
          <span>{q.maxLabel}</span>
        </div>
      </div>
    );
  }

  // Desktop: inline — negative label on the right, circles, positive label on the left.
  return (
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      gap: 18, flexWrap: 'nowrap',
    }}>
      <span style={{
        fontFamily: RL_TYPE, fontWeight: 300, fontSize: 14,
        color: RL.greyMid, whiteSpace: 'nowrap', flexShrink: 0,
      }}>{q.minLabel}</span>

      {numbers}

      <span style={{
        fontFamily: RL_TYPE, fontWeight: 300, fontSize: 14,
        color: RL.greyMid, whiteSpace: 'nowrap', flexShrink: 0,
      }}>{q.maxLabel}</span>
    </div>
  );
}

function RL_Choice({ q, value, onChange, mobile }) {
  return (
    <div style={{ display: 'grid', gap: 8 }}>
      {q.options.map((o, i) => {
        const active = value === o;
        return (
          <button key={o} onClick={() => onChange(o)} style={{
            display: 'flex', alignItems: 'center', gap: mobile ? 14 : 16,
            padding: mobile ? '12px 14px' : '14px 18px',
            background: active ? RL.amberSoft : RL.card,
            color: active ? RL.ink : RL.grey,
            border: `1px solid ${active ? RL.amber : RL.greyLight}`,
            fontFamily: RL_TYPE, fontWeight: active ? 400 : 300,
            fontSize: mobile ? 15 : 16, textAlign: 'right',
            cursor: 'pointer', borderRadius: 0,
            transition: 'all 180ms cubic-bezier(.2,.8,.2,1)',
          }}
          onMouseOver={e => { if (!active) e.currentTarget.style.borderColor = RL.greyMid; }}
          onMouseOut={e => { if (!active) e.currentTarget.style.borderColor = RL.greyLight; }}>
            <span style={{
              width: 18, height: 18, borderRadius: '50%',
              background: active ? RL.amber : RL.card,
              border: `1px solid ${active ? RL.amber : RL.greyLight}`,
              flexShrink: 0,
              position: 'relative',
            }}>
              {active && <span style={{
                position: 'absolute', inset: 4, borderRadius: '50%', background: RL.ink,
              }} />}
            </span>
            <span style={{ flex: 1 }}>{o}</span>
          </button>
        );
      })}
    </div>
  );
}

function RL_Text({ q, value, onChange, onSubmit, canSubmit, mobile }) {
  return (
    <div>
      <p style={{
        fontFamily: RL_TYPE, fontWeight: 300, fontSize: mobile ? 12 : 13,
        color: RL.greyMid, margin: '0 0 10px',
      }}>
        המשוב שלך חשוב לנו - ספר/י לנו כיצד נוכל להשתפר
      </p>
      <div style={{
        position: 'relative', background: '#FAFAF9',
        border: `1.5px solid ${RL.amber}`,
        padding: mobile ? '14px 16px 28px' : '16px 20px 34px',
        transition: 'border .15s',
      }}>
        <textarea
          value={value || ''} onChange={e => onChange(e.target.value)}
          dir="rtl" rows={mobile ? 3 : 4}
          maxLength={500}
          style={{
            width: '100%', resize: 'none',
            background: 'transparent', border: 'none',
            padding: 0, fontFamily: RL_TYPE, fontWeight: 300,
            fontSize: mobile ? 14 : 16, color: RL.ink,
            lineHeight: 1.55, outline: 'none', boxSizing: 'border-box',
            display: 'block',
          }} />
        <span style={{
          position: 'absolute', bottom: 8, insetInlineEnd: 12,
          fontFamily: RL_MONO, fontSize: 10, letterSpacing: 1.2,
          color: RL.greyMid, textTransform: 'uppercase',
        }}>
          {(value || '').length}/500
        </span>
        {/* amber send icon */}
        <button onClick={onSubmit} disabled={!canSubmit} style={{
          position: 'absolute', bottom: 8, insetInlineStart: 12,
          width: 32, height: 32, borderRadius: '50%',
          background: canSubmit ? '#2A6DBA' : RL.greyLight,
          border: 'none', cursor: canSubmit ? 'pointer' : 'not-allowed',
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          padding: 0, transition: 'all .2s',
        }} aria-label="שלח">
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none">
            <path d="M12 4 L12 20 M5 11 L12 4 L19 11"
              stroke="#FFFFFF" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </button>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Question screen (everything inside the white card)
// ─────────────────────────────────────────────────────────────
function RL_Question({ s, mobile }) {
  const q = s.currentQ;
  if (!q) return null;

  return (
    <div style={{ display: 'flex', flexDirection: 'column',
      gap: mobile ? 22 : 32 }}>
      {/* Stepper */}
      <RL_Stepper current={s.page} total={s.total} mobile={mobile} />

      {/* Question text */}
      <div>
        <h2 style={{
          fontFamily: RL_TYPE, fontWeight: 300,
          fontSize: mobile ? 17 : 22, lineHeight: 1.4, letterSpacing: '-0.005em',
          margin: 0, color: RL.ink, textWrap: 'pretty',
        }}>
          {q.text}
          {q.required && (
            <span style={{
              color: RL.required, marginInlineStart: 4, fontWeight: 400,
            }} aria-label="חובה">*</span>
          )}
        </h2>
      </div>

      {/* Answer control */}
      <div>
        {q.kind === 'rating' && <RL_Rating q={q} value={s.answers[q.id]} onChange={v => s.setAnswer(q.id, v)} mobile={mobile} />}
        {q.kind === 'choice' && <RL_Choice q={q} value={s.answers[q.id]} onChange={v => s.setAnswer(q.id, v)} mobile={mobile} />}
        {q.kind === 'text'   && <RL_Text   q={q} value={s.answers[q.id]} onChange={v => s.setAnswer(q.id, v)} mobile={mobile}
                                  canSubmit={s.currentAnswered || !q.required} onSubmit={s.goNext} />}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Intro & Thanks (custom screens; intro starts the journey,
// thanks shows post-submit confirmation)
// ─────────────────────────────────────────────────────────────
function RL_Intro({ mobile, onStart }) {
  return (
    <div style={{
      display: 'flex', flexDirection: 'column', gap: mobile ? 22 : 30,
      alignItems: mobile ? 'stretch' : 'flex-start',
    }}>
      <RL_Stepper current={0} total={5} mobile={mobile} />

      <h1 style={{
        fontFamily: RL_TYPE, fontWeight: 300,
        fontSize: mobile ? 26 : 36, lineHeight: 1.18, letterSpacing: '-0.012em',
        margin: 0, color: RL.ink, textWrap: 'balance',
      }}>
        ברוך/ה הבא/ה לסקר שביעות הרצון
        <span aria-hidden="true" style={{
          display: 'inline-block', width: mobile ? 11 : 14, height: mobile ? 11 : 14,
          background: RL.amber, marginInlineStart: mobile ? 8 : 10,
          transform: 'translateY(-2px)',
        }} />
      </h1>

      <p style={{
        fontFamily: RL_TYPE, fontWeight: 300,
        fontSize: mobile ? 14 : 16, lineHeight: 1.6, color: RL.grey,
        margin: 0, maxWidth: 560,
      }}>
        חמש שאלות קצרות. שתי דקות. אנונימי לחלוטין.<br />
        המשובים מסוכמים מדי רבעון בכרטיס הפרויקט ומשמשים את הנהלת
        הקבוצה לתעדוף שיפורים במחזור הבא.
      </p>

      {/* CTA row — sits one comfortable step below the copy, like the
          paragraph spacing in the header. */}
      <div style={{
        marginTop: mobile ? 8 : 14,
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        gap: mobile ? 14 : 20, flexWrap: 'wrap',
        alignSelf: 'stretch',
      }}>
        <span style={{
          fontFamily: RL_MONO, fontSize: mobile ? 10 : 11, letterSpacing: 1.4,
          color: RL.greyMid, textTransform: 'uppercase',
        }}>
          05 שאלות · 02 דקות · אנונימי
        </span>

        <button onClick={onStart} style={{
          background: 'transparent', color: RL.ink,
          border: `1px solid ${RL.grey}`,
          cursor: 'pointer',
          fontFamily: RL_TYPE, fontWeight: 400, fontSize: mobile ? 13 : 14,
          padding: mobile ? '8px 16px' : '9px 20px',
          borderRadius: 0, transition: 'all .2s',
          display: 'inline-flex', alignItems: 'center', gap: 10,
          letterSpacing: '-0.005em',
        }}
        onMouseOver={e => { e.currentTarget.style.background = RL.ink; e.currentTarget.style.color = RL.amber; e.currentTarget.style.borderColor = RL.ink; }}
        onMouseOut={e => { e.currentTarget.style.background = 'transparent'; e.currentTarget.style.color = RL.ink; e.currentTarget.style.borderColor = RL.grey; }}>
          <span>התחלת הסקר</span>
          <span aria-hidden="true" style={{
            width: 8, height: 8, background: RL.amber, display: 'inline-block',
            flexShrink: 0,
          }} />
        </button>
      </div>
    </div>
  );
}

function RL_Thanks({ s, mobile }) {
  return (
    <div style={{
      display: 'flex', flexDirection: 'column', gap: mobile ? 22 : 30,
      alignItems: 'center', textAlign: 'center',
    }}>
      <div style={{ textAlign: 'center' }}>
        <h1 style={{
          fontFamily: RL_TYPE, fontWeight: 300,
          fontSize: mobile ? 26 : 36, lineHeight: 1.18, letterSpacing: '-0.012em',
          margin: 0, color: RL.ink, textWrap: 'balance',
        }}>
          תודה רבה
          <span aria-hidden="true" style={{
            display: 'inline-block', width: mobile ? 11 : 14, height: mobile ? 11 : 14,
            background: RL.amber, marginInlineStart: mobile ? 8 : 10,
            transform: 'translateY(-2px)',
          }} />
        </h1>
        <p style={{
          fontFamily: RL_TYPE, fontWeight: 300,
          fontSize: mobile ? 14 : 16, lineHeight: 1.55, color: RL.grey,
          marginTop: mobile ? 10 : 14, marginBottom: 0, maxWidth: 540,
          marginInline: 'auto', textAlign: 'center',
        }}>
          הסקר הבא יישלח בהגעה של הפרויקט אל אבן הדרך הבאה.
          <br />
          מודים לכן על שיתוף הפעולה.
        </p>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Card-footer nav: outlined "המשך" or amber-filled "אישור ושליחה"
// ─────────────────────────────────────────────────────────────
function RL_CardNav({ s, mobile }) {
  // Don't show nav on the intro (intro renders its own start button)
  // or thanks (thanks renders its own restart button), or on the
  // free-text question which sends via its inline amber icon.
  if (s.page === 0 || s.page > s.total) return null;
  if (s.currentQ?.kind === 'text') {
    // On text page, show a back-only link.
    return (
      <div style={{
        marginTop: mobile ? 22 : 28, paddingTop: mobile ? 16 : 20,
        borderTop: `1px solid ${RL.rule}`,
        display: 'flex', justifyContent: 'flex-end',
      }}>
        <button onClick={s.goPrev} style={{
          background: 'transparent', color: s.page <= 1 ? RL.greyLight : RL.grey,
          border: 'none', cursor: s.page <= 1 ? 'default' : 'pointer',
          fontFamily: RL_TYPE, fontWeight: 400, fontSize: mobile ? 13 : 14,
          padding: 0, display: 'inline-flex', alignItems: 'center', gap: 6,
        }}>← חזרה</button>
      </div>
    );
  }

  const isFinal = s.page === s.total;
  return (
    <div style={{
      marginTop: mobile ? 22 : 28, paddingTop: mobile ? 16 : 20,
      borderTop: `1px solid ${RL.rule}`,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12,
    }}>
      <button onClick={s.goPrev} disabled={s.page <= 1} style={{
        background: 'transparent', color: s.page <= 1 ? RL.greyLight : RL.grey,
        border: 'none', cursor: s.page <= 1 ? 'default' : 'pointer',
        fontFamily: RL_TYPE, fontWeight: 400, fontSize: mobile ? 13 : 14,
        padding: 0, display: 'inline-flex', alignItems: 'center', gap: 6,
      }}>← חזרה</button>

      <button onClick={s.goNext} disabled={!s.currentAnswered} style={{
        background: isFinal ? (s.currentAnswered ? RL.amber : RL.greyLight) : RL.card,
        color: isFinal ? RL.ink : (s.currentAnswered ? RL.grey : RL.greyLight),
        border: isFinal ? 'none' : `1px solid ${s.currentAnswered ? RL.greyMid : RL.rule}`,
        cursor: s.currentAnswered ? 'pointer' : 'not-allowed',
        fontFamily: RL_TYPE, fontWeight: 400, fontSize: mobile ? 14 : 15,
        padding: mobile ? '11px 26px' : '12px 36px',
        borderRadius: 0, transition: 'all .2s',
        opacity: s.currentAnswered ? 1 : 0.7,
      }}
      onMouseOver={e => {
        if (!s.currentAnswered) return;
        if (isFinal) e.currentTarget.style.background = RL.amberHover;
        else { e.currentTarget.style.background = RL.amber; e.currentTarget.style.color = RL.ink; e.currentTarget.style.borderColor = RL.amber; }
      }}
      onMouseOut={e => {
        if (!s.currentAnswered) return;
        if (isFinal) e.currentTarget.style.background = RL.amber;
        else { e.currentTarget.style.background = RL.card; e.currentTarget.style.color = RL.grey; e.currentTarget.style.borderColor = RL.greyMid; }
      }}>
        {isFinal ? 'אישור ושליחה' : 'המשך'}
      </button>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// The Frame — full-bleed paper with skyline, centered logo,
// centered white card, footer.
// ─────────────────────────────────────────────────────────────
function DirectionReal({ mobile = false, initialPage = 0, initialAnswers = null }) {
  const s = useSurvey({ initialPage, initialAnswers });

  // Continuous progress 0..1 across intro→Q1..Q5→thanks for the skyline.
  const progress = s.page === 0 ? 0.04
                 : s.page > s.total ? 1
                 : (s.page / (s.total + 0.4));

  return (
    <div dir="rtl" style={{
      width: '100%', height: '100%', position: 'relative',
      background: '#F1EFEC', color: RL.ink,
      fontFamily: RL_TYPE, overflow: 'hidden',
      display: 'flex', flexDirection: 'column',
    }}>
      {/* Skyline region — sits at the top, in normal flow, so the
          form card lives in clean paper space below. The logo
          overlaps the skyline's bottom edge. */}
      <div style={{
        position: 'relative',
        height: mobile ? 110 : 170,
        flexShrink: 0,
      }}>
        <RL_Skyline progress={progress} mobile={mobile} />

        {/* Centered stacked logo, breaks the skyline at its bottom */}
        <div style={{
          position: 'absolute', insetInline: 0,
          bottom: mobile ? -28 : -36,
          display: 'flex', justifyContent: 'center',
          zIndex: 3,
        }}>
          <div style={{
            padding: mobile ? '6px 14px' : '8px 18px',
            background: '#F1EFEC',
          }}>
            <WXGStackedLogo height={mobile ? 56 : 72} color={RL.grey} accent={RL.amber} />
          </div>
        </div>
      </div>

      {/* Centered form card */}
      <div style={{
        flex: 1, position: 'relative', zIndex: 2,
        display: 'flex', justifyContent: 'center',
        padding: mobile ? '52px 18px 0'
                       : '74px 36px 0',
        minHeight: 0,
      }}>
        <div style={{
          width: '100%', maxWidth: mobile ? 360 : 760,
          background: RL.card,
          padding: mobile ? '22px 22px 26px' : '32px 40px 34px',
          boxShadow: '0 1px 0 rgba(0,0,0,0.04), 0 12px 32px -16px rgba(0,0,0,0.10), 0 28px 56px -32px rgba(0,0,0,0.10)',
          position: 'relative', overflow: 'auto', maxHeight: '100%',
          boxSizing: 'border-box',
          display: 'flex', flexDirection: 'column',
        }}>
          {/* Pastel squares motif — top-left corner */}
          <div style={{
            position: 'absolute', top: mobile ? -10 : -14,
            insetInlineStart: mobile ? -10 : -14,
            zIndex: 3, pointerEvents: 'none',
          }}>
            <WXGSquaresMotif scale={mobile ? 0.7 : 0.85} />
          </div>

          {/* Card header */}
          <RL_CardHeader mobile={mobile} />

          {/* Body */}
          <div key={s.page} style={{
            marginTop: mobile ? 22 : 28,
            animation: `wxg-fade-slide-${s.direction > 0 ? 'next' : 'prev'} 380ms cubic-bezier(.2,.8,.2,1)`,
            ...(s.page > s.total && {
              flex: 1, display: 'flex', flexDirection: 'column',
              justifyContent: 'center',
            }),
          }}>
            {s.page === 0 && <RL_Intro mobile={mobile} onStart={s.goNext} />}
            {s.page > 0 && s.page <= s.total && <RL_Question s={s} mobile={mobile} />}
            {s.page > s.total && <RL_Thanks s={s} mobile={mobile} />}
          </div>

          {/* Card nav (only on Q1..Q5 except the text Q5) */}
          <RL_CardNav s={s} mobile={mobile} />
        </div>
      </div>

      {/* Footer */}
      <footer style={{
        padding: mobile ? '14px 16px 14px' : '20px 36px 18px',
        textAlign: 'center', position: 'relative', zIndex: 2,
        fontFamily: RL_TYPE, fontWeight: 300, fontSize: mobile ? 12 : 13,
        color: RL.greyMid,
      }}>
        © 2026 WXG · Waxman Group
      </footer>
    </div>
  );
}

window.DirectionReal = DirectionReal;

// Export shared pieces so sibling concepts (e.g. "ONE GROUP. ONE POWER.")
// can reuse the form internals without duplicating logic.
Object.assign(window, {
  RL, RL_Skyline, RL_Stepper, RL_CardHeader,
  RL_Rating, RL_Choice, RL_Text,
  RL_Question, RL_Intro, RL_Thanks, RL_CardNav,
});
