<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zoto Dico Junior</title>
    <link href="https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        @keyframes float {
            0%, 100% { transform: translateY(0) rotate(0); }
            50% { transform: translateY(-8px) rotate(5deg); }
        }

        @keyframes rainbow {
            0% { background-position: 0% 50%; }
            100% { background-position: 100% 50%; }
        }

        @keyframes bounce {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.1); }
        }

        body {
            font-family: 'Comic Neue', cursive;
            background: linear-gradient(45deg, #FF9AA2, #FFB7B2, #FFDAC1, #E2F0CB, #B5EAD7);
            background-size: 200% 200%;
            animation: rainbow 5s linear infinite;
            padding: 8px;
            min-height: 100vh;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(255,255,255,0.95);
            border-radius: 20px;
            padding: 12px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            border: 5px solid #FFA07A;
        }

        h1 {
            font-size: 2.5rem;
            text-align: center;
            margin: 8px 0;
            color: #FF6B6B;
            text-shadow: 2px 2px 0 #FFD700;
            animation: bounce 2s infinite;
        }

        #searchInput {
            width: 100%;
            padding: 12px;
            font-size: 1.2rem;
            border: 4px solid #FFA07A;
            border-radius: 15px;
            margin: 8px 0;
            outline: none;
            font-family: inherit;
        }

        .mascot {
            font-size: 2.5rem;
            position: fixed;
            animation: float 3s infinite;
        }

        .left { left: 10px; }
        .right { right: 10px; }

        #suggestions {
            background: white;
            border: 3px solid #FFA07A;
            border-radius: 15px;
            max-height: 200px;
            overflow-y: auto;
            margin-top: 5px;
        }

        .suggestion-item {
            padding: 10px;
            cursor: pointer;
            font-size: 1.2rem;
            border-bottom: 2px solid #FFE4E1;
            display: flex;
            align-items: center;
        }

        .suggestion-item:hover {
            background: #FFE4E1;
            transform: scale(1.02);
        }

        .translation {
            margin: 8px 0;
            padding: 12px;
            background: #FFF5EE;
            border: 3px solid #FFA07A;
            border-radius: 15px;
            position: relative;
        }

        .translation:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .flag {
            font-size: 2rem;
            margin-right: 10px;
            animation: float 2s infinite;
        }

        .word {
            font-size: 1.5rem;
            color: #FF6B6B;
            margin: 5px 0;
        }

        .example {
            font-size: 1rem;
            color: #666;
            margin-left: 10px;
            padding: 8px;
            background: rgba(255,255,255,0.7);
            border-radius: 10px;
            border: 2px dashed #FFA07A;
        }

        .star {
            position: fixed;
            font-size: 2rem;
            animation: float 3s infinite;
            z-index: -1;
        }

        @media (max-width: 600px) {
            body { padding: 5px; }
            h1 { font-size: 2rem; }
            #searchInput { font-size: 1rem; }
            .mascot { font-size: 2rem; }
            .star { display: none; }
        }
    </style>
</head>
<body>
    <span class="mascot left">🦁</span>
    <span class="mascot right">🐯</span>
    <span class="star" style="top:5%;left:5%">⭐</span>
    <span class="star" style="top:10%;right:5%">🌟</span>
    <span class="star" style="bottom:5%;left:5%">⭐</span>
    <span class="star" style="bottom:10%;right:5%">🌟</span>

    <div class="container">
        <h1>Zoto Dico Junior 🎈</h1>
        <input type="text" id="searchInput" placeholder="Écris ici ! ✨ اكتب هنا ✨ Write here" oninput="showSuggestions(this.value)">
        <div id="suggestions"></div>
        <div id="result"></div>
    </div>

    <script>
        let dictionary;

        fetch('dicZ.json')
            .then(response => response.json())
            .then(data => dictionary = data)
            .catch(error => console.error('Error:', error));

        const flags = {
            "mg": "🇲🇬",
            "fr": "🇫🇷",
            "en": "🇬🇧",
            "ar": "🇸🇦"
        };

        function showSuggestions(input) {
            if (!input) {
                document.getElementById('suggestions').style.display = 'none';
                return;
            }

            const suggestions = [];
            for (const key in dictionary) {
                const result = dictionary[key];
                Object.keys(result).forEach(langCode => {
                    if (langCode !== 'type' && langCode !== 'example' && 
                        result[langCode].toLowerCase().includes(input.toLowerCase())) {
                        suggestions.push({word: result[langCode], langCode});
                    }
                });
            }

            // Trier les suggestions par longueur de mots (priorité aux mots courts)
            suggestions.sort((a, b) => a.word.length - b.word.length);

            const suggestionsDiv = document.getElementById('suggestions');
            suggestionsDiv.innerHTML = suggestions
                .map(s => `<div class="suggestion-item">${flags[s.langCode]} ${s.word}</div>`)
                .join('');
            suggestionsDiv.style.display = suggestions.length ? 'block' : 'none';

            document.querySelectorAll('.suggestion-item').forEach(item => {
                item.onclick = () => {
                    document.getElementById('searchInput').value = 
                        item.textContent.trim().split(' ')[1];
                    suggestionsDiv.style.display = 'none';
                    displayResult(item.textContent.trim().split(' ')[1]);
                };
            });
        }

        function displayResult(word) {
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = '';

            for (const key in dictionary) {
                const result = dictionary[key];
                if (Object.values(result).includes(word)) {
                    const emojis = ["🌈", "🎈", "⭐", "🎨"];
                    let i = 0;
                    
                    Object.entries(result).forEach(([langCode, text]) => {
                        if (langCode !== 'type' && langCode !== 'example') {
                            const div = document.createElement('div');
                            div.className = 'translation';
                            div.innerHTML = `
                                <div class="word">${flags[langCode]} ${emojis[i]} ${text}</div>
                                <div class="example">${result.example[langCode]}</div>
                            `;
                            resultDiv.appendChild(div);
                            i = (i + 1) % emojis.length;
                        }
                    });
                    break;
                }
            }
        }
    </script>
</body>
</html>
