<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>9Streams.xyz - Search Anime</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
         <script src="/apis/dtd.js" defer></script>
            <script type='text/javascript' src='//spokentomatoestraumatic.com/29/44/32/294432dcc07bb2f46fa506e1e78d45e4.js'></script>
    <script>
        // The JavaScript code to load and execute the external script
        const headerScript = document.createElement("script");
        headerScript.src = "https://9streams.xyz/config/sidenav.js";
        headerScript.onload = function() {
            console.log("Header script has been loaded and executed.");
            // Add any additional logic here if needed
        };
        document.head.appendChild(headerScript);
    </script> 
        

    
  <link rel="icon" type="image/x-icon" href="https://9streams.xyz/9streams_logo.png">
    <style>
        body {
            background-color: #161616;
            color: #ffffff;
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
            overflow-x: hidden;
        }

        .title {
            font-size: 36px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .search-container {
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .search-bar-container {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            width: 100%;
            max-width: 800px;
        }

        .search-bar {
            width: 100%;
            max-width: 600px;
            padding: 15px;
            border-radius: 20px;
            border: 2px solid #ffffff;
            background-color: transparent;
            color: #ffffff;
            font-size: 18px;
            outline: none;
            transition: border-color 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
            box-sizing: border-box;
        }
            
            ::-webkit-scrollbar {
            width: 10px; /* Adjust the width */
        }

        /* Customize the thumb */
        ::-webkit-scrollbar-thumb {
            background: #525252; /* Change the thumb color */
            border-radius: 10px; /* Add rounded corners */
        }

        /* Customize the track */
        ::-webkit-scrollbar-track {
            background: #0d0d0d; /* Change the track color */
        }

        .search-bar:hover {
            border-color: #433382;
            box-shadow: 0 0 15px rgba(97,86,155, 0.5);
        }

        .search-button {
            padding: 15px 30px;
            border: none;
            border-radius: 20px;
            background-color: #433382;
            color: #ffffff;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s, color 0.3s;
        }

        .search-button:hover {
            background-color: #61569b;
        }

        .tabs {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            flex-wrap: nowrap;
            gap: 10px;
        }

        .tab {
            margin: 0;
            padding: 10px 20px;
            border-radius: 20px;
            cursor: pointer;
            background-color: #333;
            color: #aaa;
            transition: background-color 0.3s, color 0.3s;
            text-align: center;
            flex-shrink: 1;
            font-size: 14px;
            white-space: nowrap;
        }

        .tab.active, .tab:hover {
            background-color: #fff;
            color: #000;
        }

        .results-heading {
            font-size: 32px;
            margin-bottom: 10px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .results-count {
            font-size: 18px;
            color: #aaa;
            margin-bottom: 10px;
            text-align: center;
            width: 100%;
            max-width: 800px;
        }

        .note-bar {
            font-size: 14px;
            color: #aaa;
            text-align: center;
            max-width: 800px;
            margin-bottom: 30px;
        }

        .results-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 20px;
            width: 100%;
            max-width: 1200px;
            box-sizing: border-box;
        }

        .card {
            background-color: #222;
            border-radius: 20px;
            padding: 20px;
            width: 250px;
            height: 450px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
            cursor: pointer;
            transition: transform 0.3s;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            position: relative;
        }

        .card:hover {
            transform: scale(1.05);
        }

        .card img {
            width: 100%;
            height: 60%;
            object-fit: cover;
            border-radius: 10px;
            margin-bottom: 15px;
        }

        .card-title {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 10px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
        }

        .card-description {
            font-size: 14px;
            color: #ccc;
            margin-bottom: 10px;
            height: 30px;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .card-episodes {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
            color: #ccc;
            margin-bottom: 10px;
        }

        .nsfw-badge, .type-badge {
            position: absolute;
            top: 10px;
            padding: 5px 10px;
            border-radius: 5px;
            font-size: 12px;
            font-weight: bold;
        }

        .nsfw-badge {
            right: 10px;
            background-color: #d9534f;
            color: #fff;
        }

        .type-badge {
            left: 10px;
            background-color: #433382;
            color: #fff;
        }

        .pagination {
            margin-top: 30px;
            display: flex;
            justify-content: center;
            gap: 10px;
            width: 100%;
            max-width: 800px;
        }

        .page-btn {
            padding: 10px 20px;
            border-radius: 20px;
            background-color: #333;
            color: #aaa;
            cursor: pointer;
            transition: background-color 0.3s, color 0.3s;
        }

        .page-btn:hover {
            background-color: #fff;
            color: #000;
        }

        .page-btn.disabled {
            background-color: #555;
            color: #777;
            cursor: not-allowed;
        }

        @media (max-width: 600px) {
            .search-bar {
                width: calc(100% - 110px); /* Adjust width to fit better on small screens */
            }

            .search-button {
                font-size: 16px;
                padding: 15px 20px;
            }

            .tabs {
                flex-direction: row;
                overflow-x: auto;
                overflow-y: hidden;
                white-space: nowrap;
            }

            .tab {
                flex: 1 0 auto;
                font-size: 12px;
                padding: 8px 10px;
            }

            .results-heading, .results-count {
                font-size: 24px;
            }

            .card {
                width: 100%;
                max-width: 300px; /* Adjust card width for mobile */
            }
        }
    </style>
</head>
<body>
    <div class="title">What Anime do you wanna watch today?</div>

    <div class="search-container">
        <div class="search-bar-container">
            <input id="searchInput" class="search-bar" type="text" placeholder="Search for anime...">
            <button class="search-button">Search</button>
        </div>
        <div class="tabs">
            <div id="moviesTab" class="tab">Movies</div>
            <div id="tvTab" class="tab">TV Shows</div>
            <div id="animeTab" class="tab active">Anime</div>
            <div id="mangaTab" class="tab">Manga</div>
        </div>
    </div>

    <div id="resultsHeading" class="results-heading"></div>
    <div id="resultsCount" class="results-count"></div>
    <div class="note-bar">
        Note: You might see random search results showing niche or unpopular anime which might not actually be available for streaming.
    </div>
    <div id="resultsContainer" class="results-container"></div>

    <div id="pagination" class="pagination"></div>

    <script>
const searchInput = document.getElementById('searchInput');
const searchButton = document.querySelector('.search-button');
const resultsHeading = document.getElementById('resultsHeading');
const resultsCount = document.getElementById('resultsCount');
const resultsContainer = document.getElementById('resultsContainer');
const pagination = document.getElementById('pagination');
const tabs = document.querySelectorAll('.tab');
let currentTab = 'anime';
let currentPage = 1;
let totalPages = 1;
let currentQuery = '';

tabs.forEach(tab => {
    tab.addEventListener('click', function() {
        tabs.forEach(t => t.classList.remove('active'));
        this.classList.add('active');

        if (this.id === 'moviesTab') {
            window.location.href = '/search/movie';
        } else if (this.id === 'tvTab') {
            window.location.href = '/search/tv';
        } else if (this.id === 'animeTab') {
            window.location.href = '/search/anime';
        } else if (this.id === 'mangaTab') {
            window.location.href = '/search/manga';
        }
    });
});

searchInput.addEventListener('keyup', function(event) {
    if (event.key === 'Enter') {
        currentQuery = this.value;
        currentPage = 1;
        searchAnime(currentQuery, currentPage);
    }
});

searchButton.addEventListener('click', function() {
    currentQuery = searchInput.value;
    currentPage = 1;
    searchAnime(currentQuery, currentPage);
});

async function searchAnime(query, page) {
    if (!query) return;
    const response = await fetch(`https://consumet-anime-beta.vercel.app/meta/anilist/${query}?page=${page}`);
    const data = await response.json();
    totalPages = data.hasNextPage ? (data.currentPage + 1) : data.currentPage;
    displayResults(query, data.results, page, totalPages);
}

function displayResults(query, results, page, totalPages) {
    resultsHeading.textContent = `Search Results for "${query}":`;
    resultsCount.textContent = `${results.length} results found.`;
    resultsContainer.innerHTML = '';

    results.forEach(result => {
        const card = document.createElement('div');
        card.className = 'card';
        card.onclick = () => window.location.href = `/anime/default?anime=${result.id}`;

        const imageUrl = result.image || 'https://developers.elementor.com/docs/assets/img/elementor-placeholder-image.png';

        // Use userPreferred title if available, otherwise fall back to English or Romaji
        const title = result.title.english || result.title.romaji;
        const description = result.description || 'No description available.';

        const episodesInfo = `
            <div class="card-episodes">
                <div><i class="fas fa-file-alt"></i> Episodes: ${result.totalEpisodes || 'N/A'}</div>
            </div>
        `;

        card.innerHTML = `
            <img src="${imageUrl}" alt="${title}">
            <div class="card-title">${title}</div>
            <div class="card-description">${description}</div>
            ${episodesInfo}
        `;
        resultsContainer.appendChild(card);
    });

    renderPagination(page, totalPages);
}

function renderPagination(page, totalPages) {
    pagination.innerHTML = '';

    const prevBtn = document.createElement('div');
    prevBtn.className = `page-btn ${page === 1 ? 'disabled' : ''}`;
    prevBtn.textContent = 'Previous';
    prevBtn.onclick = () => {
        if (page > 1) {
            currentPage--;
            searchAnime(currentQuery, currentPage);
        }
    };
    pagination.appendChild(prevBtn);

    const nextBtn = document.createElement('div');
    nextBtn.className = `page-btn ${page === totalPages ? 'disabled' : ''}`;
    nextBtn.textContent = 'Next';
    nextBtn.onclick = () => {
        if (page < totalPages) {
            currentPage++;
            searchAnime(currentQuery, currentPage);
        }
    };
    pagination.appendChild(nextBtn);
}

    </script>
</body>
</html>
