/* Screens 1-4 + helpers, i18n-aware */

// ═════════════════════════════════════════════════════════
// SCREEN 1 — Language Selector
// ═════════════════════════════════════════════════════════
function ScreenLanguage({ navigate, state, setState, t, setLang }) {
  const langs = [
    { code: 'en', label: 'English',  flag: '🇬🇧' },
    { code: 'es', label: 'Español',  flag: '🇪🇸' },
    { code: 'it', label: 'Italiano', flag: '🇮🇹' },
    { code: 'fr', label: 'Français', flag: '🇫🇷' },
  ];
  const pick = (c) => {
    setLang(c);
    setState(s => ({ ...s, lang: c }));
    setTimeout(() => navigate('activity'), 180);
  };
  return (
    <Phone statusDark={true}>
      <div className="screen" style={{ background: 'linear-gradient(180deg, #1A6BB5 0%, #0F4D86 65%, #082C4F 100%)', overflow: 'hidden' }}>
        <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }}>
          {[...Array(14)].map((_, i) => (
            <div key={i} style={{
              position: 'absolute',
              width: 4 + (i % 5) * 3, height: 4 + (i % 5) * 3,
              borderRadius: '50%', background: 'rgba(255,255,255,0.4)',
              left: `${(i * 37) % 100}%`, bottom: -30,
              animation: `bubble ${4 + (i % 4)}s ease-in ${i * 0.5}s infinite`,
            }}/>
          ))}
        </div>
        <div style={{ position: 'absolute', top: 300, left: -10, right: -10, height: 60 }}>
          <svg viewBox="0 0 400 60" style={{ width: '100%' }}>
            <path d="M0 30 Q 50 10 100 30 T 200 30 T 300 30 T 400 30 V60 H0 Z" fill="rgba(0,174,239,0.2)"/>
            <path d="M0 40 Q 50 22 100 40 T 200 40 T 300 40 T 400 40 V60 H0 Z" fill="rgba(0,174,239,0.35)"/>
          </svg>
        </div>

        <div style={{ position: 'relative', zIndex: 2, paddingTop: 60, paddingBottom: 24, paddingLeft: 24, paddingRight: 24, flex: 1, display: 'flex', flexDirection: 'column' }}>
          <div style={{ textAlign: 'center', marginTop: 4 }}>
            <img src="assets/atlas-clean.png" alt="Atlas" style={{
              width: 220, height: 'auto', display: 'block', margin: '0 auto', background: 'transparent',
              filter: 'drop-shadow(0 18px 22px rgba(0,0,0,0.35)) drop-shadow(0 2px 4px rgba(0,0,0,0.2))',
              animation: 'floatMascot 4s ease-in-out infinite',
            }}/>
          </div>

          <div style={{ textAlign: 'center', marginTop: -2 }}>
            <div className="display" style={{ color: '#fff', fontSize: 26, lineHeight: 1, letterSpacing: '-0.01em' }}>ATLANTIS</div>
            <div style={{ display: 'inline-flex', gap: 6, alignItems: 'baseline', marginTop: 4 }}>
              <span className="display" style={{ color: '#fff', fontSize: 14 }}>DIVING</span>
              <span className="display" style={{ color: BRAND.yellow, fontSize: 14 }}>LANZAROTE</span>
            </div>
          </div>

          <div style={{ margin: '14px 0 10px', color: '#fff', textAlign: 'center' }}>
            <div className="eyebrow" style={{ color: BRAND.yellow, fontSize: 10 }}>{t('welcome')}</div>
            <div style={{ fontSize: 15, fontWeight: 600, marginTop: 4 }}>{t('chooseLang')}</div>
            <div style={{ fontSize: 12, opacity: 0.75, marginTop: 2, fontStyle: 'italic' }}>{t('chooseLangSub')}</div>
          </div>

          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10, marginTop: 4 }}>
            {langs.map(l => (
              <button key={l.code} onClick={() => pick(l.code)} style={{
                border: 'none', borderRadius: 14, padding: '12px 10px 10px',
                background: 'rgba(255,255,255,0.95)', color: BRAND.ink,
                cursor: 'pointer', textAlign: 'center',
                display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4,
                fontFamily: 'Archivo', boxShadow: '0 8px 18px -6px rgba(0,0,0,0.4)',
                position: 'relative',
              }}>
                <span style={{ fontSize: 28 }}>{l.flag}</span>
                <span style={{ fontSize: 13, fontWeight: 700 }}>{l.label}</span>
              </button>
            ))}
          </div>

          <div style={{ flex: 1 }}/>
          <div style={{ textAlign: 'center', color: 'rgba(255,255,255,0.55)', fontSize: 10, letterSpacing: '0.1em', textTransform: 'uppercase', marginTop: 14 }}>atlantislanzarote.com</div>
        </div>
      </div>
    </Phone>
  );
}

// ═════════════════════════════════════════════════════════
// SCREEN 2 — Activity Type
// ═════════════════════════════════════════════════════════
function ScreenActivity({ navigate, state, setState, t }) {
  const pick = (type) => {
    setState(s => ({ ...s, activity: type }));
    setTimeout(() => navigate('personal'), 150);
  };
  return (
    <Phone>
      <div className="screen">
        <Chrome onBack={() => navigate('language')} />
        <div className="screen-scroll" style={{ padding: '6px 20px 24px' }}>
          <div style={{ textAlign: 'center', marginTop: 4 }}>
            <div style={{ display: 'inline-flex', alignItems: 'baseline', gap: 6, color: BRAND.blue }}>
              <span className="display" style={{ fontSize: 14 }}>ATLANTIS</span>
              <span className="display" style={{ fontSize: 10, color: BRAND.ink }}>DIVING</span>
              <span className="display" style={{ fontSize: 10, color: BRAND.yellow, textShadow: '0 1px 0 rgba(0,0,0,0.15)' }}>LANZAROTE</span>
            </div>
          </div>

          <div style={{ marginTop: 24 }}>
            <div className="eyebrow">{t('step01')}</div>
            <h1 className="h1" style={{ marginTop: 6 }}>{t('whatToday')}</h1>
          </div>

          <p className="sub" style={{ marginTop: 14 }}>{t('whatTodaySub')}</p>

          <div style={{ display: 'flex', flexDirection: 'column', gap: 12, marginTop: 18 }}>
            <ActivityCard
              accent={BRAND.cyan}
              bg="linear-gradient(135deg, #00AEEF 0%, #1A6BB5 100%)"
              onClick={() => pick('beginner')}
              label={t('beginnerLabel')} title={t('beginnerTitle')} desc={t('beginnerDesc')}
              tag={t('beginnerTag')}
              icon={<MaskIcon/>}
            />
            <ActivityCard
              accent={BRAND.yellow}
              bg="linear-gradient(135deg, #0F4D86 0%, #082C4F 100%)"
              onClick={() => pick('advanced')}
              label={t('advancedLabel')} title={t('advancedTitle')} desc={t('advancedDesc')}
              tag={t('advancedTag')}
              icon={<FinIcon/>}
            />
          </div>

          <div style={{
            marginTop: 18, padding: 14,
            background: BRAND.yellow + '22', border: '1px solid ' + BRAND.yellow + '88',
            borderRadius: 12, display: 'flex', gap: 10, alignItems: 'flex-start',
          }}>
            <div style={{ fontSize: 18 }}>💡</div>
            <div style={{ fontSize: 12, lineHeight: 1.45, color: '#6B5600' }}>
              <b>{t('notSure')}</b> {t('notSureBody')}
            </div>
          </div>
        </div>
      </div>
    </Phone>
  );
}

function ActivityCard({ bg, onClick, label, title, desc, icon, accent, tag }) {
  return (
    <button onClick={onClick} style={{
      border: 'none', background: bg, color: '#fff',
      borderRadius: 20, padding: '18px 18px 16px',
      textAlign: 'left', cursor: 'pointer',
      position: 'relative', overflow: 'hidden',
      boxShadow: '0 14px 30px -14px rgba(8, 44, 79, 0.55)',
      fontFamily: 'Archivo',
    }}>
      <div style={{ position: 'absolute', right: -10, bottom: -10, opacity: 0.18, color: accent }}>{icon}</div>
      <div style={{ fontSize: 10, letterSpacing: '0.12em', textTransform: 'uppercase', color: accent, fontWeight: 700 }}>{label}</div>
      <div className="display" style={{ fontSize: 18, lineHeight: 1.1, marginTop: 6, maxWidth: '85%' }}>{title}</div>
      <div style={{ fontSize: 12, opacity: 0.85, lineHeight: 1.45, marginTop: 8, maxWidth: '90%' }}>{desc}</div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 14 }}>
        <span style={{ fontSize: 11, fontWeight: 700, background: 'rgba(255,255,255,0.15)', padding: '5px 10px', borderRadius: 999 }}>{tag}</span>
        <div style={{ width: 34, height: 34, borderRadius: '50%', background: '#fff', color: BRAND.blue, display: 'grid', placeItems: 'center' }}>
          <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
            <path d="M2 7h10M7 2l5 5-5 5" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </div>
      </div>
    </button>
  );
}

function MaskIcon() {
  return (
    <svg width="100" height="100" viewBox="0 0 100 100" fill="currentColor">
      <ellipse cx="35" cy="45" rx="18" ry="15"/>
      <ellipse cx="70" cy="45" rx="18" ry="15"/>
      <rect x="45" y="38" width="15" height="14" rx="3"/>
      <path d="M20 60 Q 50 75 85 60" stroke="currentColor" strokeWidth="4" fill="none"/>
    </svg>
  );
}
function FinIcon() {
  return (
    <svg width="100" height="100" viewBox="0 0 100 100" fill="currentColor">
      <path d="M30 20 L70 20 L80 80 Q 50 95 20 80 Z"/>
      <rect x="35" y="10" width="30" height="14" rx="4"/>
    </svg>
  );
}

// ═════════════════════════════════════════════════════════
// SCREEN 3 — Personal
// ═════════════════════════════════════════════════════════
function ScreenPersonal({ navigate, state, setState, t }) {
  const [focused, setFocused] = React.useState(null);
  const p = state.personal || {};
  const set = (k, v) => setState(s => ({ ...s, personal: { ...s.personal, [k]: v } }));
  const filled = p.firstName && p.lastName && p.email && p.dob && p.emName && p.emPhone;
  const isAdv = state.activity === 'advanced';
  const totalSteps = isAdv ? 6 : 5;

  return (
    <Phone>
      <div className="screen">
        <Chrome onBack={() => navigate('activity')} step={1} totalSteps={totalSteps} label={`${t('continue')} · 1/5`} />
        <div className="screen-scroll" style={{ padding: '4px 20px 160px' }} onClick={() => setFocused(null)}>
          <div className="eyebrow">{t('personal')}</div>
          <h1 className="h1" style={{ marginTop: 4 }}>{t('personalTitle')}</h1>
          <p className="sub">{t('personalSub')}</p>

          <div style={{ display: 'flex', flexDirection: 'column', gap: 14, marginTop: 22 }}>
            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10 }}>
              <Field label={t('firstName')} required>
                <Input value={p.firstName} onChange={v => set('firstName', v)} placeholder="Maya" filled={!!p.firstName} onFocus={e => { e.stopPropagation(); setFocused('firstName'); }}/>
              </Field>
              <Field label={t('lastName')} required>
                <Input value={p.lastName} onChange={v => set('lastName', v)} placeholder="Ruiz" filled={!!p.lastName} onFocus={e => { e.stopPropagation(); setFocused('lastName'); }}/>
              </Field>
            </div>

            <Field label={t('email')} required hint={t('emailHint')}>
              <Input type="email" value={p.email} onChange={v => set('email', v)} placeholder="maya@mail.com" filled={!!p.email}
                icon={<svg width="16" height="16" viewBox="0 0 16 16" fill="none"><rect x="1" y="3" width="14" height="10" rx="2" stroke="currentColor" strokeWidth="1.4"/><path d="M2 4l6 5 6-5" stroke="currentColor" strokeWidth="1.4" fill="none"/></svg>}
                onFocus={e => { e.stopPropagation(); setFocused('email'); }}/>
            </Field>

            <Field label={t('dob')} required>
              <Input type="date" value={p.dob || '1994-06-12'} onChange={v => set('dob', v)}/>
            </Field>

            <Field label={t('phone')}>
              <div style={{ display: 'flex', gap: 8 }}>
                <select className="select" style={{ width: 88, flexShrink: 0 }} value={p.phoneCc || '+34'} onChange={e => set('phoneCc', e.target.value)}>
                  <option>+34</option><option>+44</option><option>+33</option><option>+49</option><option>+1</option><option>+39</option>
                </select>
                <Input value={p.phone} onChange={v => set('phone', v)} placeholder="612 345 678" filled={!!p.phone}/>
              </div>
            </Field>

            <Field label={t('nationality')}>
              <Select value={p.nationality} onChange={v => set('nationality', v)} placeholder={t('selectCountry')}
                options={['Spanish','British','German','French','Italian','Dutch','Irish','American','Canadian','Australian','Other']}/>
            </Field>

            <div style={{ marginTop: 6, paddingTop: 14, borderTop: '1px dashed var(--line)' }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 }}>
                <div style={{ width: 28, height: 28, borderRadius: 8, background: '#FEE7E6', color: 'var(--err)', display: 'grid', placeItems: 'center', fontSize: 14 }}>⚠</div>
                <div>
                  <div style={{ fontSize: 13, fontWeight: 700 }}>{t('emergency')}</div>
                  <div style={{ fontSize: 11, color: 'var(--muted)' }}>{t('emergencySub')}</div>
                </div>
              </div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
                <Field label={t('contactName')} required>
                  <Input value={p.emName} onChange={v => set('emName', v)} placeholder={t('partnerPlaceholder')} filled={!!p.emName}/>
                </Field>
                <Field label={t('contactPhone')} required>
                  <Input value={p.emPhone} onChange={v => set('emPhone', v)} placeholder="+34 612 345 678" filled={!!p.emPhone}/>
                </Field>
              </div>
            </div>
          </div>
        </div>

        <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, padding: '14px 20px 28px', background: 'linear-gradient(180deg, rgba(255,255,255,0) 0%, #fff 40%)', zIndex: 15 }}>
          <button className="btn btn-primary" disabled={!filled} onClick={() => navigate('physical')}>
            {t('continue')}
            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 7h10M7 2l5 5-5 5" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </button>
        </div>

        {focused && <Keyboard/>}
      </div>
    </Phone>
  );
}

// ═════════════════════════════════════════════════════════
// SCREEN 4 — Physical
// ═════════════════════════════════════════════════════════
function ScreenPhysical({ navigate, state, setState, t }) {
  const ph = state.physical || { height: 170, weight: 70, shoe: '42' };
  const set = (k, v) => setState(s => ({ ...s, physical: { ...s.physical, [k]: v } }));
  const isAdv = state.activity === 'advanced';
  const totalSteps = isAdv ? 6 : 5;
  const shoeSizes = [];
  for (let i = 35; i <= 47; i++) shoeSizes.push(String(i));

  return (
    <Phone>
      <div className="screen">
        <Chrome onBack={() => navigate('personal')} step={2} totalSteps={totalSteps} label={`${t('physical')} · 2/5`} />
        <div className="screen-scroll" style={{ padding: '4px 20px 120px' }}>
          <div className="eyebrow">{t('physical')}</div>
          <h1 className="h1" style={{ marginTop: 4 }}>{t('physicalTitle')}</h1>
          <p className="sub">{t('physicalSub')}</p>

          <div style={{ display: 'flex', alignItems: 'center', gap: 12, background: 'var(--bg-cream)', border: '1px solid ' + BRAND.yellow + 'aa', borderRadius: 14, padding: 12, marginTop: 16 }}>
            <img src="assets/atlas-clean.png" alt="" style={{ width: 46, height: 46, objectFit: 'contain', flexShrink: 0 }}/>
            <div style={{ fontSize: 12, lineHeight: 1.45, color: '#5C4600' }}>
              <b>{t('atlasSays')}</b> {t('atlasSaysBody')}
            </div>
          </div>

          <div style={{ display: 'flex', flexDirection: 'column', gap: 18, marginTop: 20 }}>
            <Field label={t('height')} required>
              <div style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
                <div className="display" style={{ fontSize: 28, color: BRAND.blue }}>{ph.height}</div>
                <div style={{ fontSize: 12, color: 'var(--muted)' }}>cm</div>
              </div>
              <HeightScale value={ph.height} onChange={v => set('height', v)}/>
            </Field>
            <Field label={t('weight')} required>
              <div style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
                <div className="display" style={{ fontSize: 28, color: BRAND.blue }}>{ph.weight}</div>
                <div style={{ fontSize: 12, color: 'var(--muted)' }}>kg</div>
              </div>
              <WeightScale value={ph.weight} onChange={v => set('weight', v)}/>
            </Field>
            <Field label={t('bootSize')} required hint={t('bootHint')}>
              <div style={{ display: 'flex', gap: 6, overflowX: 'auto', paddingBottom: 6, scrollbarWidth: 'none' }}>
                {shoeSizes.map(s => (
                  <button key={s} onClick={() => set('shoe', s)} style={{
                    minWidth: 48, height: 56, borderRadius: 10,
                    border: '1.5px solid ' + (ph.shoe === s ? BRAND.blue : 'var(--line)'),
                    background: ph.shoe === s ? BRAND.blue : '#fff',
                    color: ph.shoe === s ? '#fff' : 'var(--ink)',
                    fontFamily: 'Archivo Black', fontSize: 16, flexShrink: 0, cursor: 'pointer',
                  }}>{s}</button>
                ))}
              </div>
            </Field>

            <div style={{ background: 'var(--bg-soft)', borderRadius: 14, padding: 14, display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
              <div>
                <div style={{ fontSize: 11, color: 'var(--muted)', letterSpacing: '0.06em', textTransform: 'uppercase' }}>{t('suggestedSuit')}</div>
                <div className="display" style={{ fontSize: 20, marginTop: 2, color: BRAND.blue }}>{suggestSuit(ph.height, ph.weight)}</div>
              </div>
              <div style={{ fontSize: 11, color: 'var(--muted)', textAlign: 'right' }}>{t('canaryWater')}<br/>20–23 °C</div>
            </div>
          </div>
        </div>

        <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, padding: '14px 20px 28px', background: 'linear-gradient(180deg, rgba(255,255,255,0) 0%, #fff 40%)' }}>
          <button className="btn btn-primary" onClick={() => navigate('medical')}>
            {t('continueMedical')}
            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M2 7h10M7 2l5 5-5 5" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </button>
        </div>
      </div>
    </Phone>
  );
}

function HeightScale({ value, onChange }) {
  const min = 120, max = 220;
  const pct = ((value - min) / (max - min)) * 100;
  return (
    <div style={{ marginTop: 8, position: 'relative', height: 40 }}>
      <div style={{ position: 'absolute', top: 14, left: 0, right: 0, height: 4, background: 'var(--line)', borderRadius: 2 }}/>
      <div style={{ position: 'absolute', top: 14, left: 0, width: `${pct}%`, height: 4, background: 'linear-gradient(90deg, ' + BRAND.blue + ', ' + BRAND.cyan + ')', borderRadius: 2 }}/>
      <div style={{ position: 'absolute', top: 6, left: `calc(${pct}% - 10px)`, width: 20, height: 20, background: '#fff', border: '3px solid ' + BRAND.blue, borderRadius: '50%', boxShadow: '0 2px 6px rgba(26,107,181,0.3)', pointerEvents: 'none' }}/>
      {[140, 170, 200].map(m => (
        <div key={m} style={{ position: 'absolute', top: 28, left: `${((m - min)/(max-min))*100}%`, transform: 'translateX(-50%)', fontSize: 9, color: 'var(--muted)' }}>{m}</div>
      ))}
      <input type="range" min={min} max={max} value={value} onChange={e => onChange(Number(e.target.value))}
        style={{ position: 'absolute', top: 6, left: -4, right: -4, width: 'calc(100% + 8px)', height: 20, opacity: 0, cursor: 'pointer', margin: 0 }}/>
    </div>
  );
}

function WeightScale({ value, onChange }) {
  const min = 35, max = 180;
  const pct = ((value - min) / (max - min)) * 100;
  return (
    <div style={{ marginTop: 8, position: 'relative', height: 40 }}>
      <div style={{ position: 'absolute', top: 14, left: 0, right: 0, height: 4, background: 'var(--line)', borderRadius: 2 }}/>
      <div style={{ position: 'absolute', top: 14, left: 0, width: `${pct}%`, height: 4, background: 'linear-gradient(90deg, ' + BRAND.blue + ', ' + BRAND.cyan + ')', borderRadius: 2 }}/>
      <div style={{ position: 'absolute', top: 6, left: `calc(${pct}% - 10px)`, width: 20, height: 20, background: '#fff', border: '3px solid ' + BRAND.blue, borderRadius: '50%', boxShadow: '0 2px 6px rgba(26,107,181,0.3)', pointerEvents: 'none' }}/>
      {[50, 80, 110, 140].map(m => (
        <div key={m} style={{ position: 'absolute', top: 28, left: `${((m - min)/(max-min))*100}%`, transform: 'translateX(-50%)', fontSize: 9, color: 'var(--muted)' }}>{m}</div>
      ))}
      <input type="range" min={min} max={max} value={value} onChange={e => onChange(Number(e.target.value))}
        style={{ position: 'absolute', top: 6, left: -4, right: -4, width: 'calc(100% + 8px)', height: 20, opacity: 0, cursor: 'pointer', margin: 0 }}/>
    </div>
  );
}

function suggestSuit(h, w) {
  if (h < 160 && w < 65) return 'XS / S';
  if (h < 170 && w < 75) return 'S / M';
  if (h < 180 && w < 85) return 'M / L';
  if (h < 190 && w < 95) return 'L / XL';
  return 'XL / XXL';
}

Object.assign(window, {
  ScreenLanguage, ScreenActivity, ScreenPersonal, ScreenPhysical,
  suggestSuit,
});
