<x-guest-layout>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">

<style>
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Plus Jakarta Sans', sans-serif;
    min-height: 100vh;
    background: #fff8f0;
    overflow-x: hidden;
}

body::before {
    content: '';
    position: fixed; top: -120px; right: -120px;
    width: 480px; height: 480px; border-radius: 50%;
    background: radial-gradient(circle, rgba(245,158,11,0.18) 0%, transparent 70%);
    pointer-events: none;
}

body::after {
    content: '';
    position: fixed; bottom: -100px; left: -100px;
    width: 380px; height: 380px; border-radius: 50%;
    background: radial-gradient(circle, rgba(217,119,6,0.12) 0%, transparent 70%);
    pointer-events: none;
}

.page-wrapper {
    max-width: 520px;
    margin: 0 auto;
    padding: 52px 24px 72px;
}

/* Brand */
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 44px; }

.brand-icon {
    width: 52px; height: 52px;
    background: linear-gradient(135deg, #f59e0b, #d97706);
    border-radius: 16px;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 8px 24px rgba(245,158,11,0.35);
}

.brand-icon i { font-size: 22px; color: white; }
.brand-text h1 { font-size: 20px; font-weight: 800; color: #1a0e00; line-height: 1; }
.brand-text p  { font-size: 12px; color: #b47c44; font-weight: 500; margin-top: 3px; }

/* Page title */
.page-title { margin-bottom: 40px; }
.page-title h2 { font-size: 30px; font-weight: 800; color: #1a0e00; line-height: 1.2; }
.page-title h2 span { color: #d97706; }
.page-title p { font-size: 14px; color: #7a5a3a; margin-top: 8px; line-height: 1.6; }

/* Section heading */
.section-heading {
    font-size: 11px; font-weight: 800; color: #d97706;
    text-transform: uppercase; letter-spacing: 1.5px;
    margin-bottom: 16px; margin-top: 36px;
    display: flex; align-items: center; gap: 8px;
}
.section-heading::after {
    content: ''; flex: 1; height: 1px;
    background: linear-gradient(to right, #fde68a, transparent);
}

/* Form group */
.form-group { margin-bottom: 18px; }
.form-group label {
    display: block; font-size: 12px; font-weight: 700;
    color: #4a2e0e; margin-bottom: 7px;
    text-transform: uppercase; letter-spacing: 0.4px;
}

/* Input */
.input-wrapper { position: relative; }

.input-wrapper i.icon-left {
    position: absolute; left: 15px; top: 50%;
    transform: translateY(-50%);
    color: #c49050; font-size: 14px;
    pointer-events: none; z-index: 1;
}

.input-wrapper input {
    width: 100%;
    padding: 13px 14px 13px 44px;
    border: 1.5px solid #f0dcc8; border-radius: 14px;
    font-size: 14px; font-family: 'Plus Jakarta Sans', sans-serif;
    background: white; color: #2a1600; transition: all 0.2s;
}

.input-wrapper input:focus {
    outline: none; border-color: #f59e0b;
    background: #fffdf9;
    box-shadow: 0 0 0 4px rgba(245,158,11,0.1);
}

.input-wrapper input::placeholder { color: #d4b896; }
.input-wrapper input:disabled { background: #faf5ee; color: #c0a882; cursor: not-allowed; }

/* OTP Row */
.otp-row { display: flex; gap: 10px; }
.otp-row .input-wrapper { flex: 1; }

.btn-otp {
    padding: 0 18px;
    background: #fef3c7; border: 1.5px solid #fde68a;
    border-radius: 14px; font-size: 12px; font-weight: 700;
    color: #b45309; cursor: pointer; white-space: nowrap;
    transition: all 0.2s; font-family: 'Plus Jakarta Sans', sans-serif;
}
.btn-otp:hover { background: #f59e0b; color: white; border-color: #f59e0b; }
.btn-otp:disabled { opacity: 0.5; cursor: not-allowed; }

.otp-status { font-size: 12px; margin-top: 6px; font-weight: 500; }
.otp-status.success { color: #16a34a; }
.otp-status.error   { color: #dc2626; }

/* OTP Boxes */
.otp-section {
    display: none;
    background: white; border: 1.5px solid #fde68a;
    border-radius: 16px; padding: 22px; margin-top: 12px;
}
.otp-section.visible { display: block; }
.otp-section > label {
    font-size: 12px; font-weight: 700; color: #4a2e0e;
    text-transform: uppercase; letter-spacing: 0.4px;
    margin-bottom: 14px; display: block;
}

.otp-boxes { display: flex; gap: 10px; justify-content: center; }

.otp-boxes input {
    width: 48px !important; height: 58px;
    padding: 0 !important; text-align: center;
    font-size: 26px !important; font-weight: 800 !important;
    border: 2px solid #f0dcc8 !important; border-radius: 14px !important;
    color: #d97706 !important; background: #fffdf9;
    font-family: 'Plus Jakarta Sans', sans-serif; transition: all 0.2s;
}
.otp-boxes input:focus {
    border-color: #f59e0b !important;
    box-shadow: 0 0 0 3px rgba(245,158,11,0.15) !important; outline: none;
}
.otp-boxes input.filled { background: #fffbeb; border-color: #f59e0b !important; }

#otpVerifyStatus { text-align: center; margin-top: 10px; font-size: 12px; font-weight: 600; }

.btn-verify-otp {
    width: 100%; padding: 12px;
    background: linear-gradient(135deg, #f59e0b, #d97706);
    border: none; border-radius: 12px; color: white;
    font-size: 13px; font-weight: 700;
    font-family: 'Plus Jakarta Sans', sans-serif;
    cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px;
    margin-top: 16px; transition: all 0.2s;
}
.btn-verify-otp:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-verify-otp:not(:disabled):hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(245,158,11,0.3);
}

/* Verified badge */
.verified-badge {
    display: none; align-items: center; gap: 10px;
    background: #f0fdf4; border: 1.5px solid #bbf7d0;
    border-radius: 14px; padding: 12px 16px;
    font-size: 13px; font-weight: 600; color: #15803d;
    margin-top: 12px;
}
.verified-badge.show { display: flex; }
.verified-badge i { font-size: 16px; flex-shrink: 0; }

/* Password toggle */
.password-toggle {
    position: absolute; right: 14px; top: 50%;
    transform: translateY(-50%);
    background: none; border: none; cursor: pointer;
    color: #c49050; font-size: 14px; padding: 0;
}

/* Password strength */
.pw-strength { margin-top: 8px; display: none; }
.pw-bars { display: flex; gap: 4px; margin-bottom: 4px; }
.pw-bar { flex: 1; height: 4px; border-radius: 4px; background: #f0dcc8; transition: background 0.3s; }
.pw-label { font-size: 11px; font-weight: 700; }

/* Error */
.error-message {
    color: #dc2626; font-size: 11px; margin-top: 5px;
    display: flex; align-items: center; gap: 4px;
}

/* Password grid */
.password-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* Submit */
.btn-submit {
    width: 100%; padding: 15px;
    background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    border: none; border-radius: 16px; color: white;
    font-size: 15px; font-weight: 800;
    font-family: 'Plus Jakarta Sans', sans-serif;
    cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px;
    margin-top: 32px; transition: all 0.25s;
    box-shadow: 0 8px 24px rgba(245,158,11,0.3);
    position: relative; overflow: hidden;
}
.btn-submit::before {
    content: ''; position: absolute; top: 0; left: -100%;
    width: 100%; height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.15), transparent);
    transition: left 0.4s;
}
.btn-submit:hover::before { left: 100%; }
.btn-submit:hover { transform: translateY(-2px); box-shadow: 0 12px 32px rgba(245,158,11,0.4); }

/* Sections locked */
.form-section { transition: opacity 0.3s; }
.form-section.locked { opacity: 0.35; pointer-events: none; user-select: none; }

/* Login link */
.login-link { text-align: center; margin-top: 28px; font-size: 13px; color: #7a5a3a; }
.login-link a { color: #d97706; font-weight: 700; text-decoration: none; }
.login-link a:hover { text-decoration: underline; }

@media (max-width: 480px) {
    .page-wrapper { padding: 32px 18px 52px; }
    .page-title h2 { font-size: 24px; }
    .password-grid { grid-template-columns: 1fr; gap: 18px; }
    .otp-boxes input { width: 40px !important; height: 48px; font-size: 20px !important; }
}
</style>

<div class="page-wrapper">

    <div class="brand">
        <div class="brand-icon"><i class="fas fa-couch"></i></div>
        <div class="brand-text">
            <h1>LokaFurniture</h1>
            <p>Furnitur lokal terbaik</p>
        </div>
    </div>

    <div class="page-title">
        <h2>Buat akun <span>baru</span> ✦</h2>
        <p>Daftarkan diri Anda dan mulai temukan furnitur impian.</p>
    </div>

    <form method="POST" action="{{ route('register') }}" id="registerForm">
        @csrf
        <input type="hidden" name="otp" id="otpHidden">
        <input type="hidden" name="email" id="emailHidden">

        {{-- BAGIAN 1: VERIFIKASI EMAIL --}}
        <div class="form-section" id="sectionEmail">
            <div class="section-heading"><i class="fas fa-envelope"></i> Verifikasi Email</div>

            <div class="form-group">
                <label for="email_input">Alamat Email</label>
                <div class="otp-row">
                    <div class="input-wrapper">
                        <i class="icon-left fas fa-envelope"></i>
                        <input type="email" id="email_input" placeholder="nama@email.com">
                    </div>
                    <button type="button" class="btn-otp" id="btnOtp" onclick="sendOtp(this)">
                        Kirim OTP
                    </button>
                </div>
                <div id="otpStatus" class="otp-status"></div>
                @error('email')
                    <div class="error-message"><i class="fas fa-exclamation-circle"></i> {{ $message }}</div>
                @enderror
            </div>

            <div class="otp-section" id="otpSection">
                <label>Masukkan Kode OTP</label>
                <div class="otp-boxes">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                    <input type="text" maxlength="1" inputmode="numeric" class="otp-digit">
                </div>
                <div id="otpVerifyStatus" class="otp-status"></div>
                <button type="button" class="btn-verify-otp" id="btnVerifyOtp"
                        onclick="verifyOtp()" disabled>
                    <i class="fas fa-shield-halved"></i> Verifikasi OTP
                </button>
            </div>

            @error('otp')
                <div class="error-message" style="margin-top:6px;"><i class="fas fa-exclamation-circle"></i> {{ $message }}</div>
            @enderror

            <div class="verified-badge" id="verifiedBadge">
                <i class="fas fa-circle-check"></i>
                <span>Email <strong id="verifiedEmailText"></strong> telah terverifikasi</span>
            </div>
        </div>

        {{-- BAGIAN 2: DATA DIRI & PASSWORD --}}
        <div class="form-section locked" id="sectionData">
            <div class="section-heading"><i class="fas fa-user"></i> Data Diri</div>

            <div class="form-group">
                <label for="name">Nama Lengkap</label>
                <div class="input-wrapper">
                    <i class="icon-left fas fa-user"></i>
                    <input type="text" id="name" name="name"
                           value="{{ old('name') }}" placeholder="Nama lengkap Anda">
                </div>
                @error('name')
                    <div class="error-message"><i class="fas fa-exclamation-circle"></i> {{ $message }}</div>
                @enderror
            </div>

            <div class="section-heading"><i class="fas fa-lock"></i> Password</div>

            <div class="password-grid">
                <div class="form-group">
                    <label for="password">Password</label>
                    <div class="input-wrapper">
                        <i class="icon-left fas fa-lock"></i>
                        <input type="password" id="password" name="password" placeholder="Min. 8 karakter">
                        <button type="button" class="password-toggle" onclick="togglePw('password','icon1')">
                            <i class="fas fa-eye" id="icon1"></i>
                        </button>
                    </div>
                    <div class="pw-strength" id="pwStrength">
                        <div class="pw-bars">
                            <div class="pw-bar" id="b1"></div>
                            <div class="pw-bar" id="b2"></div>
                            <div class="pw-bar" id="b3"></div>
                            <div class="pw-bar" id="b4"></div>
                        </div>
                        <div class="pw-label" id="pwLabel"></div>
                    </div>
                    @error('password')
                        <div class="error-message"><i class="fas fa-exclamation-circle"></i> {{ $message }}</div>
                    @enderror
                </div>

                <div class="form-group">
                    <label for="password_confirmation">Konfirmasi Password</label>
                    <div class="input-wrapper">
                        <i class="icon-left fas fa-lock"></i>
                        <input type="password" id="password_confirmation"
                               name="password_confirmation" placeholder="Ulangi password">
                        <button type="button" class="password-toggle" onclick="togglePw('password_confirmation','icon2')">
                            <i class="fas fa-eye" id="icon2"></i>
                        </button>
                    </div>
                </div>
            </div>

            <button type="submit" class="btn-submit">
                <i class="fas fa-user-plus"></i> DAFTAR SEKARANG
            </button>
        </div>

    </form>

    <div class="login-link">
        Sudah punya akun? <a href="{{ route('login') }}">Masuk di sini</a>
    </div>
</div>

<script>
let countdownTimer = null;

function sendOtp(btn) {
    const email = document.getElementById('email_input').value.trim();
    const statusDiv = document.getElementById('otpStatus');
    if (!email) {
        statusDiv.textContent = '✗ Masukkan email terlebih dahulu';
        statusDiv.className = 'otp-status error'; return;
    }
    btn.disabled = true; btn.textContent = 'Mengirim...';
    statusDiv.textContent = ''; statusDiv.className = 'otp-status';
    fetch("{{ route('send.otp') }}", {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
        body: JSON.stringify({ email })
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) {
            statusDiv.textContent = '✓ OTP dikirim! Cek inbox atau folder spam Anda';
            statusDiv.className = 'otp-status success';
            document.getElementById('otpSection').classList.add('visible');
            document.querySelectorAll('.otp-digit')[0].focus();
            startCountdown(btn, 60);
        } else {
            statusDiv.textContent = '✗ ' + (data.message || 'Gagal mengirim OTP');
            statusDiv.className = 'otp-status error';
            btn.disabled = false; btn.textContent = 'Kirim OTP';
        }
    })
    .catch(() => {
        statusDiv.textContent = '✗ Terjadi kesalahan, coba lagi';
        statusDiv.className = 'otp-status error';
        btn.disabled = false; btn.textContent = 'Kirim OTP';
    });
}

function startCountdown(btn, sec) {
    if (countdownTimer) clearInterval(countdownTimer);
    let rem = sec;
    countdownTimer = setInterval(() => {
        rem--;
        if (rem <= 0) { clearInterval(countdownTimer); btn.disabled = false; btn.textContent = 'Kirim Ulang'; }
        else btn.textContent = `Ulang (${rem}s)`;
    }, 1000);
}

const digits = document.querySelectorAll('.otp-digit');
digits.forEach((inp, idx) => {
    inp.addEventListener('input', e => {
        const v = e.target.value.replace(/\D/g, '');
        e.target.value = v; e.target.classList.toggle('filled', !!v);
        if (v && idx < 5) digits[idx + 1].focus();
        checkFull();
    });
    inp.addEventListener('keydown', e => {
        if (e.key === 'Backspace' && !inp.value && idx > 0) {
            digits[idx-1].value = ''; digits[idx-1].classList.remove('filled');
            digits[idx-1].focus(); checkFull();
        }
    });
    inp.addEventListener('paste', e => {
        e.preventDefault();
        const text = (e.clipboardData || window.clipboardData).getData('text').replace(/\D/g, '');
        [...text.slice(0, 6)].forEach((ch, i) => {
            if (digits[i]) { digits[i].value = ch; digits[i].classList.add('filled'); }
        });
        checkFull();
        if (text.length >= 6) digits[5].focus();
    });
});

function checkFull() {
    document.getElementById('btnVerifyOtp').disabled = ![...digits].every(d => d.value.length === 1);
}

function verifyOtp() {
    const otp = [...digits].map(d => d.value).join('');
    const email = document.getElementById('email_input').value.trim();
    const statusDiv = document.getElementById('otpVerifyStatus');
    statusDiv.textContent = 'Memverifikasi...'; statusDiv.className = 'otp-status';
    fetch("{{ route('verify.otp') }}", {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
        body: JSON.stringify({ email, otp })
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) {
            document.getElementById('otpHidden').value = otp;
            document.getElementById('emailHidden').value = email;
            document.getElementById('verifiedEmailText').textContent = email;
            document.getElementById('verifiedBadge').classList.add('show');
            document.getElementById('otpSection').style.display = 'none';
            document.getElementById('email_input').disabled = true;
            document.getElementById('btnOtp').style.display = 'none';
            statusDiv.textContent = '';
            document.getElementById('sectionData').classList.remove('locked');
            setTimeout(() => {
                document.getElementById('sectionData').scrollIntoView({ behavior: 'smooth', block: 'start' });
            }, 200);
        } else {
            statusDiv.textContent = '✗ ' + (data.message || 'OTP salah atau kadaluarsa');
            statusDiv.className = 'otp-status error';
            digits.forEach(d => { d.value = ''; d.classList.remove('filled'); });
            digits[0].focus();
            document.getElementById('btnVerifyOtp').disabled = true;
        }
    })
    .catch(() => {
        statusDiv.textContent = '✗ Terjadi kesalahan, coba lagi';
        statusDiv.className = 'otp-status error';
    });
}

function togglePw(id, iconId) {
    const inp = document.getElementById(id), ic = document.getElementById(iconId);
    if (inp.type === 'password') { inp.type = 'text'; ic.classList.replace('fa-eye','fa-eye-slash'); }
    else { inp.type = 'password'; ic.classList.replace('fa-eye-slash','fa-eye'); }
}

document.getElementById('password').addEventListener('input', function () {
    const pw = this.value;
    const wrap = document.getElementById('pwStrength'), lbl = document.getElementById('pwLabel');
    const bars = ['b1','b2','b3','b4'].map(id => document.getElementById(id));
    if (!pw) { wrap.style.display = 'none'; return; }
    wrap.style.display = 'block';
    let score = 0;
    if (pw.length >= 8) score++;
    if (/[A-Z]/.test(pw)) score++;
    if (/[0-9]/.test(pw)) score++;
    if (/[^A-Za-z0-9]/.test(pw)) score++;
    const colors = ['#ef4444','#f97316','#eab308','#22c55e'];
    const labels = ['Lemah','Sedang','Kuat','Sangat Kuat'];
    bars.forEach((bar, i) => bar.style.background = i < score ? colors[score-1] : '#f0dcc8');
    lbl.textContent = labels[score-1] || ''; lbl.style.color = colors[score-1] || '#7a5a3a';
});

@if($errors->any())
document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('sectionData').classList.remove('locked');
});
@endif
</script>
</x-guest-layout>