File size: 24,310 Bytes
57da3ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import { useState } from 'react';
import { useStore } from '../store';
import { useNavigate } from 'react-router-dom';
import { Leaf, Store, User, Camera, Loader2, X, MapPin, Check } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
import { CameraCapture } from './CameraCapture';
import { INDIA_DATA } from '../data/india-data';

const INDIAN_STATES = Object.keys(INDIA_DATA);

export function Auth() {
  const { login, deviceId, initDevice, deviceProfiles, deviceProfilesLoaded } = useStore();
  const navigate = useNavigate();
  const [name, setName] = useState('');
  const [contact, setContact] = useState('');
  const [nearestCity, setNearestCity] = useState('');
  const [district, setDistrict] = useState('');
  const [state, setState] = useState('');
  const [pincode, setPincode] = useState('');
  const [role, setRole] = useState('buyer');

  const [showStateList, setShowStateList] = useState(false);
  const [filteredStates, setFilteredStates] = useState(INDIAN_STATES);

  const [showDistrictList, setShowDistrictList] = useState(false);
  const [filteredDistricts, setFilteredDistricts] = useState([]);

  const [location, setLocation] = useState(null);
  const [locating, setLocating] = useState(false);
  
  const [selfieFile, setSelfieFile] = useState(null);
  const [selfiePreview, setSelfiePreview] = useState(null);
  const [showCamera, setShowCamera] = useState(false);
  const [isCreating, setIsCreating] = useState(false);
  const [error, setError] = useState('');
  const [isSubmitting, setIsSubmitting] = useState(false);

  // Auto-init device id just in case they hit auth directly
  if (!deviceId) initDevice();

  const removeImage = () => {
    setSelfieFile(null);
    setSelfiePreview(null);
  };

  const getLocation = () => {
    setLocating(true);
    setError('');
    
    if (!('geolocation' in navigator)) {
      setError('Geolocation is not supported by your browser.');
      setLocating(false);
      return;
    }

    const options = {
      enableHighAccuracy: true,
      timeout: 15000,
      maximumAge: 0
    };

    navigator.geolocation.getCurrentPosition(
      (position) => {
        setLocation({
          lat: position.coords.latitude,
          lng: position.coords.longitude,
          address: `Precise GPS: ${position.coords.latitude.toFixed(4)}, ${position.coords.longitude.toFixed(4)}`
        });
        setLocating(false);
      },
      (err) => {
        console.error("Auth Location Error:", err);
        let msg = 'Failed to get precise GPS location.';
        if (err.code === 1) msg = 'Location permission denied. Please allow location access in your browser settings.';
        else if (err.code === 3) msg = 'Location request timed out. Please try again in an open area.';
        
        setError(msg);
        setLocating(false);
      },
      options
    );
  };

  const handleLogin = async (e) => {
    e.preventDefault();
    if (!name.trim()) return;
    
    // Validate Indian phone number format
    const phoneRegex = /^(?:\+?91[-\s]?)?[0]?(?:91)?[6789]\d{9}$/;
    if (!phoneRegex.test(contact)) {
      setError('Please enter a valid 10-digit Indian phone number.');
      return;
    }

    if (!selfieFile) {
      setError('A live selfie snapshot is strictly required to ensure network trust.');
      return;
    }

    if (!location) {
      setError('Please capture your primary GPS location.');
      return;
    }

    if (!nearestCity || !district || !state || !pincode) {
      setError('Please fill in all location details.');
      return;
    }

    setIsSubmitting(true);
    setError('');

    try {
      const { deviceId: currentDeviceId, deviceToken } = deviceId ? { deviceId, deviceToken: useStore.getState().deviceToken } : initDevice();

      const formData = new FormData();
      formData.append('id', currentDeviceId);
      formData.append('deviceToken', deviceToken);
      formData.append('name', name);
      formData.append('contact', contact);
      formData.append('role', role);
      formData.append('nearestCity', nearestCity);
      formData.append('district', district);
      formData.append('state', state);
      formData.append('pincode', pincode);
      formData.append('location', JSON.stringify(location));
      formData.append('selfie', selfieFile);

      const res = await fetch('/api/users', {
        method: 'POST',
        body: formData,
      });

      const data = await res.json();

      if (!res.ok) {
        throw new Error(data.error || 'Failed to register identity.');
      }
      
      console.log('REGISTRATION SUCCESS:', data);
      console.log('NAVIGATING TO:', (role === 'buyer' ? '/buyer' : '/seller'));
      
      // Zustand handles our local user binding
      login(data);
      
      navigate(role === 'buyer' ? '/buyer' : '/seller');
    } catch (err) {
      console.error(err);
      setError(err.message || 'Registration failed. Please try again.');
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleProfileSelect = (profile) => {
    login(profile);
    navigate(profile.role === 'buyer' ? '/buyer' : '/seller');
  };

  const hasProfiles = deviceProfilesLoaded && deviceProfiles.length > 0;
  const isCreatingNew = !hasProfiles || isCreating;

  const hasBuyer = deviceProfiles.some(p => p.role === 'buyer');
  const hasSeller = deviceProfiles.some(p => p.role === 'seller');

  if (!deviceProfilesLoaded) {
    return (
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '100%' }}>
        <Loader2 className="animate-spin" size={32} color="var(--primary-color)" />
      </div>
    );
  }

  return (
    <motion.div 
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.5 }}
      style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '100%', paddingBottom: '40px' }}
    >
      {showCamera && (
        <CameraCapture 
          facingMode="user"
          onClose={() => setShowCamera(false)}
          onCapture={(file, dataUrl) => {
            setSelfieFile(file);
            setSelfiePreview(dataUrl);
            setShowCamera(false);
            setError('');
          }}
        />
      )}
      
      <div style={{ textAlign: 'center', marginBottom: '30px', marginTop: '30px' }}>
        <div style={{
          width: '70px', height: '70px', borderRadius: '24px',
          background: 'linear-gradient(135deg, #10b981, #059669)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          margin: '0 auto 16px',
          boxShadow: '0 10px 30px rgba(16, 185, 129, 0.3)'
        }}>
          <Leaf size={32} color="white" />
        </div>
        <h1 style={{ fontSize: '28px', marginBottom: '8px' }}>Welcome to Meri Mandi</h1>
        <p style={{ color: 'var(--text-muted)' }}>The smart way to buy and sell crops directly.</p>
      </div>

      {error && (
        <div style={{ width: '100%', background: 'var(--danger-glass)', color: 'var(--danger-color)', padding: '16px', borderRadius: '14px', marginBottom: '20px', textAlign: 'center' }}>
          {error}
        </div>
      )}

      {isCreatingNew ? (
        <form onSubmit={handleLogin} className="glass-panel" style={{ width: '100%', padding: '24px' }}>
          
          {hasProfiles && (
             <div style={{ marginBottom: '24px' }}>
               <button 
                type="button" 
                onClick={() => setIsCreating(false)} 
                className="btn-secondary" 
                style={{ width: '100%', padding: '12px' }}
               >
                 Cancel Creation
               </button>
             </div>
          )}

          {/* Selfie Upload UI */}
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', marginBottom: '24px' }}>
            <p style={{ display: 'block', marginBottom: '12px', fontWeight: 500 }}>Take a Live Selfie Profile</p>
            
            {selfiePreview ? (
              <div style={{ position: 'relative', width: '120px', height: '120px', borderRadius: '50%', overflow: 'hidden', boxShadow: '0 0 20px rgba(0,0,0,0.5)' }}>
                <img src={selfiePreview} alt="Selfie" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
                <button 
                  type="button" 
                  onClick={removeImage}
                  style={{
                    position: 'absolute', top: 8, right: 8, background: 'rgba(239, 68, 68, 0.9)', 
                    border: 'none', color: 'white', borderRadius: '50%', width: '24px', height: '24px',
                    display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer'
                  }}
                >
                  <X size={14} />
                </button>
              </div>
            ) : (
              <div 
                onClick={() => setShowCamera(true)}
                style={{
                  width: '120px', height: '120px', borderRadius: '50%', 
                  border: '2px dashed var(--primary-color)',
                  display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
                  cursor: 'pointer', background: 'var(--primary-glow)', gap: '8px'
                }}
              >
                <Camera size={28} color="white" />
                <span style={{ fontSize: '12px', fontWeight: 600 }}>Snap Live</span>
              </div>
            )}
            <p style={{ marginTop: '12px', fontSize: '12px', color: 'var(--text-muted)' }}>We strictly ask for you to take a live photo over the network.</p>
          </div>

          <div style={{ marginBottom: '20px' }}>
            <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500 }}>Your Name</label>
            <input 
              type="text" 
              className="input-base" 
              placeholder="Enter your name"
              value={name}
              onChange={(e) => setName(e.target.value)}
              disabled={isSubmitting}
              required
            />
          </div>

          <div style={{ marginBottom: '20px' }}>
            <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500 }}>Contact Number</label>
            <input 
              type="tel" 
              className="input-base" 
              placeholder="Enter your phone number"
              value={contact}
              onChange={(e) => setContact(e.target.value)}
              disabled={isSubmitting}
              required
            />
          </div>

          {/* New Location Fields */}
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '16px', marginBottom: '20px' }}>
            <div style={{ position: 'relative' }}>
              <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500, fontSize: '14px' }}>State</label>
              <input 
                type="text" 
                className="input-base" 
                placeholder="State Name"
                value={state}
                onChange={(e) => {
                  const val = e.target.value;
                  setState(val);
                  setFilteredStates(INDIAN_STATES.filter(s => s.toLowerCase().includes(val.toLowerCase())));
                  setShowStateList(true);
                  setDistrict(''); // Reset district on state change
                }}
                onFocus={() => setShowStateList(true)}
                disabled={isSubmitting}
                required
              />
              <AnimatePresence>
                {showStateList && filteredStates.length > 0 && (
                  <motion.div 
                    initial={{ opacity: 0, y: -10 }}
                    animate={{ opacity: 1, y: 0 }}
                    exit={{ opacity: 0, y: -10 }}
                    style={{
                      position: 'absolute', top: '100%', left: 0, right: 0, zIndex: 110,
                      background: 'var(--surface-light)', border: '1px solid var(--primary-color)',
                      borderRadius: '12px', marginTop: '4px', maxHeight: '200px', overflowY: 'auto',
                      boxShadow: '0 10px 25px rgba(0,0,0,0.8)',
                      backdropFilter: 'blur(20px)'
                    }}
                  >
                    {filteredStates.map(s => (
                      <div 
                        key={s}
                        onClick={() => {
                          setState(s);
                          setShowStateList(false);
                          setError('');
                          setDistrict('');
                          setFilteredDistricts(INDIA_DATA[s] || []);
                        }}
                        style={{
                          padding: '12px 16px', cursor: 'pointer', borderBottom: '1px solid var(--border-color)',
                          display: 'flex', alignItems: 'center', justifyContent: 'space-between'
                        }}
                        onMouseEnter={e => e.currentTarget.style.background = 'var(--primary-glow)'}
                        onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
                      >
                        <span style={{ fontSize: '14px' }}>{s}</span>
                        {state === s && <Check size={14} color="var(--primary-color)" />}
                      </div>
                    ))}
                  </motion.div>
                )}
              </AnimatePresence>
              {showStateList && <div onClick={() => setShowStateList(false)} style={{ position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, zIndex: 105 }} />}
            </div>

            <div style={{ position: 'relative' }}>
              <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500, fontSize: '14px' }}>District</label>
              <input 
                type="text" 
                className="input-base" 
                placeholder={state ? "District Name" : "Select State First"}
                value={district}
                onChange={(e) => {
                  const val = e.target.value;
                  setDistrict(val);
                  const ds = INDIA_DATA[state] || [];
                  setFilteredDistricts(ds.filter(d => d.toLowerCase().includes(val.toLowerCase())));
                  setShowDistrictList(true);
                }}
                onFocus={() => {
                  if (state) {
                    const ds = INDIA_DATA[state] || [];
                    setFilteredDistricts(ds.filter(d => d.toLowerCase().includes(district.toLowerCase())));
                    setShowDistrictList(true);
                  }
                }}
                disabled={isSubmitting || !state}
                required
              />
              <AnimatePresence>
                {showDistrictList && filteredDistricts.length > 0 && (
                  <motion.div 
                    initial={{ opacity: 0, y: -10 }}
                    animate={{ opacity: 1, y: 0 }}
                    exit={{ opacity: 0, y: -10 }}
                    style={{
                      position: 'absolute', top: '100%', left: 0, right: 0, zIndex: 110,
                      background: 'var(--surface-light)', border: '1px solid var(--primary-color)',
                      borderRadius: '12px', marginTop: '4px', maxHeight: '200px', overflowY: 'auto',
                      boxShadow: '0 10px 25px rgba(0,0,0,0.8)',
                      backdropFilter: 'blur(20px)'
                    }}
                  >
                    {filteredDistricts.map(d => (
                      <div 
                        key={d}
                        onClick={() => {
                          setDistrict(d);
                          setShowDistrictList(false);
                          setError('');
                        }}
                        style={{
                          padding: '12px 16px', cursor: 'pointer', borderBottom: '1px solid var(--border-color)',
                          display: 'flex', alignItems: 'center', justifyContent: 'space-between'
                        }}
                        onMouseEnter={e => e.currentTarget.style.background = 'var(--primary-glow)'}
                        onMouseLeave={e => e.currentTarget.style.background = 'transparent'}
                      >
                        <span style={{ fontSize: '14px' }}>{d}</span>
                        {district === d && <Check size={14} color="var(--primary-color)" />}
                      </div>
                    ))}
                  </motion.div>
                )}
              </AnimatePresence>
              {showDistrictList && <div onClick={() => setShowDistrictList(false)} style={{ position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, zIndex: 105 }} />}
            </div>
          </div>

          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '16px', marginBottom: '20px' }}>
            <div>
              <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500, fontSize: '14px' }}>Nearest City</label>
              <input 
                type="text" 
                className="input-base" 
                placeholder="Nearest City"
                value={nearestCity}
                onChange={(e) => setNearestCity(e.target.value)}
                disabled={isSubmitting}
                required
              />
            </div>
            <div>
              <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500, fontSize: '14px' }}>Pincode</label>
              <input 
                type="text" 
                className="input-base" 
                placeholder="Pincode"
                value={pincode}
                onChange={(e) => setPincode(e.target.value)}
                disabled={isSubmitting}
                required
              />
            </div>
          </div>

          <div style={{ marginBottom: '24px' }}>
            <label style={{ display: 'block', marginBottom: '8px', fontWeight: 500 }}>Primary Location (GPS)</label>
            <div 
              onClick={isSubmitting ? null : getLocation}
              style={{
                display: 'flex', alignItems: 'center', justifyContent: 'space-between',
                padding: '16px', background: 'var(--surface-light)', borderRadius: '14px',
                border: `1px solid ${location ? 'var(--primary-color)' : 'var(--border-color)'}`,
                cursor: isSubmitting ? 'not-allowed' : 'pointer', transition: 'all 0.2s',
                opacity: isSubmitting ? 0.5 : 1
              }}
            >
              <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
                <MapPin size={24} color={location ? '#10b981' : '#94a3b8'} />
                <div style={{ display: 'flex', flexDirection: 'column' }}>
                  <span style={{ fontWeight: 600, color: location ? '#10b981' : '#f8fafc' }}>
                    {location ? 'Precise Location Captured' : 'Get Primary Location (GPS)'}
                  </span>
                  {location && <span style={{ fontSize: '12px', color: 'var(--text-muted)' }}>{location.address}</span>}
                </div>
              </div>
              {locating && <Loader2 className="animate-spin" size={18} color="var(--text-muted)" />}
            </div>
          </div>

          <div style={{ marginBottom: '24px' }}>
            <label style={{ display: 'block', marginBottom: '12px', fontWeight: 500 }}>Select Role</label>
            <div style={{ display: 'flex', gap: '12px' }}>
              <div 
                onClick={() => !isSubmitting && !hasBuyer && setRole('buyer')}
                style={{
                  flex: 1, padding: '16px', borderRadius: '16px',
                  border: `2px solid ${role === 'buyer' ? 'var(--primary-color)' : 'var(--border-color)'}`,
                  background: role === 'buyer' ? 'var(--primary-glow)' : 'transparent',
                  cursor: isSubmitting || hasBuyer ? 'not-allowed' : 'pointer', textAlign: 'center', transition: 'all 0.2s',
                  display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '8px',
                  opacity: isSubmitting || hasBuyer ? 0.5 : 1
                }}
              >
                <User size={24} color={role === 'buyer' ? '#10b981' : '#f8fafc'} />
                <span style={{ fontWeight: 600, color: role === 'buyer' ? '#10b981' : '#f8fafc' }}>{hasBuyer ? 'Buyer (Owned)' : 'Buyer'}</span>
              </div>
              
              <div 
                onClick={() => !isSubmitting && !hasSeller && setRole('seller')}
                style={{
                  flex: 1, padding: '16px', borderRadius: '16px',
                  border: `2px solid ${role === 'seller' ? 'var(--primary-color)' : 'var(--border-color)'}`,
                  background: role === 'seller' ? 'var(--primary-glow)' : 'transparent',
                  cursor: isSubmitting || hasSeller ? 'not-allowed' : 'pointer', textAlign: 'center', transition: 'all 0.2s',
                  display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '8px',
                  opacity: isSubmitting || hasSeller ? 0.5 : 1
                }}
              >
                <Store size={24} color={role === 'seller' ? '#10b981' : '#f8fafc'} />
                <span style={{ fontWeight: 600, color: role === 'seller' ? '#10b981' : '#f8fafc' }}>{hasSeller ? 'Seller (Owned)' : 'Seller'}</span>
              </div>
            </div>
          </div>

          <button type="submit" disabled={isSubmitting || (role === 'buyer' && hasBuyer) || (role === 'seller' && hasSeller)} className="btn-primary" style={{ width: '100%', padding: '16px' }}>
            {isSubmitting ? <><Loader2 className="animate-spin" size={20} /> Registering...</> : `Create ${role === 'buyer' ? 'Buyer' : 'Seller'} Profile`}
          </button>
        </form>
      ) : (
        <div style={{ width: '100%' }}>
          <h3 style={{ fontSize: '20px', marginBottom: '16px', textAlign: 'center' }}>Select Profile</h3>
          <div style={{ display: 'grid', gap: '16px', marginBottom: '24px' }}>
            {deviceProfiles.map(profile => (
              <div 
                key={profile.id}
                onClick={() => handleProfileSelect(profile)}
                className="glass-panel"
                style={{
                  padding: '20px', display: 'flex', alignItems: 'center', gap: '16px',
                  cursor: 'pointer', transition: 'transform 0.2s, box-shadow 0.2s'
                }}
                onMouseOver={(e) => e.currentTarget.style.transform = 'translateY(-2px)'}
                onMouseOut={(e) => e.currentTarget.style.transform = 'translateY(0)'}
              >
                <div style={{ width: '60px', height: '60px', borderRadius: '50%', overflow: 'hidden', border: '2px solid var(--primary-color)' }}>
                  <img src={profile.selfiePath} alt="Selfie" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
                </div>
                <div style={{ flex: 1 }}>
                  <h4 style={{ fontSize: '18px', marginBottom: '4px' }}>{profile.name}</h4>
                  <div style={{ display: 'flex', alignItems: 'center', gap: '6px' }}>
                    {profile.role === 'buyer' ? <User size={14} color="var(--primary-color)" /> : <Store size={14} color="var(--primary-color)" />}
                    <span style={{ fontSize: '14px', color: 'var(--text-muted)', textTransform: 'capitalize' }}>{profile.role}</span>
                  </div>
                </div>
              </div>
            ))}
          </div>

          {deviceProfiles.length < 2 && (
             <button 
               className="btn-secondary" 
               style={{ width: '100%', padding: '16px', borderStyle: 'dashed' }}
               onClick={() => {
                 setRole(hasBuyer ? 'seller' : 'buyer');
                 setIsCreating(true);
               }}
             >
               + Create {hasBuyer ? 'Seller' : 'Buyer'} Profile
             </button>
          )}
        </div>
      )}
    </motion.div>
  );
}