@extends('layouts.user')

@section('title')
{{ $market->name }} Marketplace
@endsection

@section('content')

@php
    // NEXUS Session State for product grid
    $nexusActive = \App\Nexus\Engine\NexusConfig::isNexusActive();
    $sessionOpen = false;
    $sessionEndTime = null;
    $nextSessionTime = null;
    $activeSessionName = null;
    $nextSessionName = null;

    $sessionNames = [1 => 'Dawn', 2 => 'Midday', 3 => 'Dusk'];
    $sessionTimes = [
        1 => (int) \App\Nexus\Engine\NexusConfig::get('session_1_start_utc'),
        2 => (int) \App\Nexus\Engine\NexusConfig::get('session_2_start_utc'),
        3 => (int) \App\Nexus\Engine\NexusConfig::get('session_3_start_utc'),
    ];

    if ($nexusActive) {
        $sc = new \App\Nexus\Engine\SessionController();
        $activeSession = $sc->getActiveSession();
        $sessionOpen = ($activeSession !== null);

        if ($sessionOpen) {
            $activeSessionName = $sessionNames[$activeSession->session_number] ?? 'Session';
            $durationMinutes = \App\Nexus\Engine\NexusConfig::getInt('session_duration_minutes', 60);
            $sessionEndTime = \Carbon\Carbon::parse($activeSession->actual_start)
                ->addMinutes($durationMinutes)
                ->toIso8601String();
        } else {
            $nextSessionTime = $sc->getNextSessionTime()->toIso8601String();
            $currentUtcHour = now()->utc()->hour;
            $nextNum = 1;
            foreach ($sessionTimes as $num => $hour) {
                if ($hour > $currentUtcHour) { $nextNum = $num; break; }
            }
            $nextSessionName = $sessionNames[$nextNum] ?? 'Session';
        }
    }
@endphp

<style>
    :root {
        --orange: #f97316;
        --orange-dim: rgba(249, 115, 22, 0.10);
        --orange-glow: rgba(249, 115, 22, 0.22);
        --card-bg: #ffffff;
        --card-border: rgba(0, 0, 0, 0.07);
        --muted: #94a3b8;
        --radius: 16px;
        --radius-sm: 10px;
        --shadow: 0 4px 24px rgba(0, 0, 0, 0.07);
        --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.13);
        --ease: cubic-bezier(.4, 0, .2, 1);
    }

    .dark {
        --card-bg: #0f172a;
        --card-border: rgba(255, 255, 255, 0.07);
        --shadow: 0 4px 24px rgba(0, 0, 0, 0.45);
        --shadow-lg: 0 10px 40px rgba(0, 0, 0, 0.6);
    }

    /* ── Hero ── */
    .mp-hero {
        padding: 6rem 0 3rem;
        background: linear-gradient(150deg, #fff7ed 0%, #fdf4ff 50%, #f0f9ff 100%);
        position: relative;
        overflow: hidden;
    }

    .dark .mp-hero {
        background: linear-gradient(150deg, #1e1206 0%, #110018 50%, #020f1a 100%);
    }

    /* subtle grid pattern */
    .mp-hero::before {
        content: '';
        position: absolute;
        inset: 0;
        background-image:
            linear-gradient(rgba(249, 115, 22, 0.04) 1px, transparent 1px),
            linear-gradient(90deg, rgba(249, 115, 22, 0.04) 1px, transparent 1px);
        background-size: 40px 40px;
        pointer-events: none;
    }

    .mp-hero-inner {
        position: relative;
        z-index: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.75rem;
        text-align: center;
        padding-top: 3.5rem;
    }

    .mp-hero-logo-wrap {
        width: 72px;
        height: 72px;
        border-radius: 18px;
        background: var(--card-bg);
        border: 1.5px solid var(--card-border);
        box-shadow: var(--shadow-lg);
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
    }

    .mp-hero-logo-wrap img {
        width: 44px;
        height: 44px;
        object-fit: contain;
    }

    .mp-hero-title {
        font-size: 1.9rem;
        font-weight: 900;
        letter-spacing: -0.03em;
        line-height: 1.1;
    }

    .mp-hero-title span {
        color: var(--orange);
    }

    .mp-breadcrumb {
        display: flex;
        align-items: center;
        gap: 0.4rem;
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        margin-top: 0.25rem;
    }

    .mp-breadcrumb a {
        color: var(--muted);
        text-decoration: none;
        transition: color 0.15s var(--ease);
    }

    .mp-breadcrumb a:hover {
        color: var(--orange);
    }

    .mp-breadcrumb .sep {
        color: var(--muted);
    }

    .mp-breadcrumb .current {
        color: var(--orange);
    }

    /* ── Layout ── */
    .mp-layout {
        display: grid;
        grid-template-columns: 260px 1fr;
        gap: 1.5rem;
        padding: 2.5rem 0 4rem;
        align-items: start;
    }

    @media (max-width: 768px) {
        .mp-layout {
            grid-template-columns: 1fr;
        }

        .mp-sidebar {
            position: static !important;
        }
    }

    /* ── Sidebar ── */
    .mp-sidebar {
        position: sticky;
        top: 5rem;
        background: var(--card-bg);
        border-radius: var(--radius);
        border: 1px solid var(--card-border);
        box-shadow: var(--shadow);
        overflow: hidden;
    }

    .sidebar-header {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--card-border);
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .sidebar-header-title {
        font-size: 0.82rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.06em;
    }

    .sidebar-section {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--card-border);
    }

    .sidebar-section:last-child {
        border-bottom: none;
    }

    .sidebar-section-label {
        font-size: 0.68rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        color: var(--muted);
        margin-bottom: 0.65rem;
    }

    /* Search input */
    .search-wrap {
        position: relative;
    }

    .search-icon {
        position: absolute;
        left: 0.75rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--muted);
        pointer-events: none;
        display: flex;
        align-items: center;
    }

    .search-input {
        width: 100%;
        height: 38px;
        padding: 0 1rem 0 2.4rem;
        border-radius: var(--radius-sm);
        border: 1.5px solid var(--card-border);
        background: var(--card-bg);
        color: inherit;
        font-size: 0.82rem;
        outline: none;
        transition: border-color 0.18s var(--ease), box-shadow 0.18s var(--ease);
    }

    .search-input:focus {
        border-color: var(--orange);
        box-shadow: 0 0 0 3px var(--orange-dim);
    }

    .search-input::placeholder {
        color: var(--muted);
    }

    .search-btn {
        width: 100%;
        margin-top: 0.5rem;
        padding: 0.42rem;
        border-radius: var(--radius-sm);
        border: none;
        background: var(--orange);
        color: #fff;
        font-size: 0.75rem;
        font-weight: 700;
        cursor: pointer;
        transition: background 0.15s var(--ease), transform 0.12s;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.35rem;
    }

    .search-btn:hover {
        background: #ea6c0a;
        transform: translateY(-1px);
    }

    /* Market links */
    .market-link {
        display: flex;
        align-items: center;
        gap: 0.6rem;
        padding: 0.5rem 0.65rem;
        border-radius: 8px;
        font-size: 0.8rem;
        font-weight: 600;
        color: var(--muted);
        text-decoration: none;
        transition: background 0.15s var(--ease), color 0.15s var(--ease);
        margin-bottom: 0.2rem;
    }

    .market-link:hover,
    .market-link.active {
        background: var(--orange-dim);
        color: var(--orange);
    }

    .market-link-dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: var(--orange);
        flex-shrink: 0;
        opacity: 0.5;
    }

    .market-link:hover .market-link-dot,
    .market-link.active .market-link-dot {
        opacity: 1;
    }

    /* ── Products grid ── */
     .products-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr); /* phones: 2 per row */
        gap: 1.25rem;
    }

    @media (min-width: 640px) {
        .products-grid {
            grid-template-columns: repeat(3, 1fr);
            /* small tablets */
        }
    }

    @media (min-width: 1024px) {
        .products-grid {
            grid-template-columns: repeat(4, 1fr);
            /* desktops */
        }
    }

    /* ── Product card ── */
    .product-card {
        background: var(--card-bg);
        border-radius: var(--radius);
        border: 1px solid var(--card-border);
        box-shadow: var(--shadow);
        overflow: hidden;
        transition: box-shadow 0.22s var(--ease), transform 0.22s var(--ease), border-color 0.22s var(--ease);
        display: flex;
        flex-direction: column;
    }

    .product-card:hover {
        box-shadow: var(--shadow-lg);
        transform: translateY(-4px);
        border-color: rgba(249, 115, 22, 0.25);
    }

    /* image zone */
    .product-img-wrap {
        position: relative;
        overflow: hidden;
        aspect-ratio: 1 / 1;
        background: #f8fafc;
    }

    .dark .product-img-wrap {
        background: #1e293b;
    }

    .product-img-wrap img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.5s var(--ease);
        display: block;
    }

    .product-card:hover .product-img-wrap img {
        transform: scale(1.07);
    }

    /* market badge */
    .product-market-badge {
        position: absolute;
        top: 0.6rem;
        left: 0.6rem;
        display: inline-flex;
        align-items: center;
        gap: 0.3rem;
        padding: 0.22rem 0.6rem;
        border-radius: 99px;
        background: var(--orange);
        color: #fff;
        font-size: 0.62rem;
        font-weight: 800;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        box-shadow: 0 2px 8px rgba(249, 115, 22, 0.4);
    }

    /* view icon button */
    .product-view-btn {
        position: absolute;
        top: 0.6rem;
        right: 0.6rem;
        width: 34px;
        height: 34px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.9);
        backdrop-filter: blur(4px);
        border: 1px solid var(--card-border);
        display: flex;
        align-items: center;
        justify-content: center;
        color: #334155;
        text-decoration: none;
        transition: background 0.15s var(--ease), color 0.15s var(--ease), transform 0.15s;
        opacity: 0;
        transform: translateY(-4px) scale(0.9);
    }

    .product-card:hover .product-view-btn {
        opacity: 1;
        transform: translateY(0) scale(1);
    }

    .product-view-btn:hover {
        background: #1e293b;
        color: #fff;
    }

    /* CTA overlay */
    .product-cta {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 0.65rem 0.75rem;
        background: linear-gradient(0deg, rgba(15, 23, 42, 0.95) 0%, transparent 100%);
        transform: translateY(100%);
        transition: transform 0.28s var(--ease);
    }

    .product-card:hover .product-cta {
        transform: translateY(0);
    }

    .cta-btn {
        display: block;
        width: 100%;
        padding: 0.5rem 1rem;
        border-radius: 9px;
        background: var(--orange);
        color: #fff;
        font-size: 0.78rem;
        font-weight: 800;
        text-align: center;
        text-decoration: none;
        transition: background 0.15s var(--ease);
        box-shadow: 0 2px 10px rgba(249, 115, 22, 0.45);
    }

    .cta-btn:hover {
        background: #ea6c0a;
        color: #fff;
    }

    /* info zone */
    .product-info {
        padding: 0.9rem 1rem 1rem;
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 0.35rem;
    }

    .product-name {
        font-size: 0.85rem;
        font-weight: 700;
        line-height: 1.3;
        letter-spacing: -0.01em;
        text-decoration: none;
        color: inherit;
        transition: color 0.15s var(--ease);
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .product-name:hover {
        color: var(--orange);
    }

    .product-price-row {
        display: flex;
        align-items: baseline;
        gap: 0.5rem;
        margin-top: 0.1rem;
    }

    .price-current {
        font-size: 0.95rem;
        font-weight: 800;
        color: var(--orange);
    }

    .price-old {
        font-size: 0.72rem;
        color: #ef4444;
        text-decoration: line-through;
    }

    .price-label {
        font-size: 0.62rem;
        color: var(--muted);
        font-weight: 600;
    }

    .stars-row {
        display: flex;
        align-items: center;
        gap: 0.15rem;
        margin-top: 0.1rem;
    }

    .star {
        color: #f59e0b;
        font-size: 0.75rem;
    }

    .rating-val {
        font-size: 0.68rem;
        color: var(--muted);
        margin-left: 0.25rem;
    }

    /* ── Top bar (count + sort) ── */
    .products-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 1.25rem;
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .count-chip {
        display: inline-flex;
        align-items: center;
        gap: 0.4rem;
        padding: 0.28rem 0.75rem;
        border-radius: 99px;
        background: var(--orange-dim);
        color: var(--orange);
        font-size: 0.72rem;
        font-weight: 700;
        border: 1px solid rgba(249, 115, 22, 0.18);
    }

    .sh {
        font-size: 1.05rem;
        font-weight: 800;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        letter-spacing: -0.01em;
    }

    .sh::before {
        content: '';
        width: 3px;
        height: 1.1rem;
        border-radius: 2px;
        background: var(--orange);
        flex-shrink: 0;
    }

    /* ── Empty state ── */
    .empty-state {
        grid-column: 1 / -1;
        text-align: center;
        padding: 4rem 1rem;
    }

    .empty-icon {
        width: 72px;
        height: 72px;
        border-radius: 50%;
        background: var(--orange-dim);
        border: 1px solid rgba(249, 115, 22, 0.18);
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 1rem;
        color: var(--orange);
    }

    /* ── Pagination override ── */
    .mp-pagination nav {
        display: flex;
        justify-content: center;
    }

    .mp-pagination .pagination {
        display: flex;
        gap: 0.35rem;
        list-style: none;
        padding: 0;
        margin: 0;
        flex-wrap: wrap;
        justify-content: center;
    }

    .mp-pagination .page-item .page-link,
    .mp-pagination span,
    .mp-pagination a {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 36px;
        height: 36px;
        padding: 0 0.6rem;
        border-radius: 9px;
        font-size: 0.78rem;
        font-weight: 700;
        border: 1.5px solid var(--card-border);
        background: var(--card-bg);
        color: var(--muted);
        text-decoration: none;
        transition: all 0.15s var(--ease);
    }

    .mp-pagination a:hover {
        border-color: var(--orange);
        color: var(--orange);
        background: var(--orange-dim);
    }

    .mp-pagination [aria-current="page"] span,
    .mp-pagination .active span,
    .mp-pagination span[aria-current="page"] {
        background: var(--orange);
        color: #fff;
        border-color: var(--orange);
        box-shadow: 0 2px 10px var(--orange-glow);
    }

    .mp-pagination [disabled] span {
        opacity: 0.4;
        pointer-events: none;
    }
    /* ── Session strip (compact, NEXUS addition) ── */
    .mp-session-strip {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0.55rem 1.25rem;
        border-radius: 10px;
        margin: 1rem 0 0;
        transition: all 0.3s;
    }
    .mp-session-strip.ss-open {
        background: linear-gradient(90deg, rgba(34,197,94,0.06), rgba(34,197,94,0.03));
        border: 1px solid rgba(34,197,94,0.15);
    }
    .mp-session-strip.ss-closed {
        background: linear-gradient(90deg, rgba(249,115,22,0.05), rgba(249,115,22,0.02));
        border: 1px solid rgba(249,115,22,0.12);
    }
    .dark .mp-session-strip.ss-open { background: linear-gradient(90deg, rgba(34,197,94,0.1), rgba(34,197,94,0.04)); border-color: rgba(34,197,94,0.2); }
    .dark .mp-session-strip.ss-closed { background: linear-gradient(90deg, rgba(249,115,22,0.08), rgba(249,115,22,0.03)); border-color: rgba(249,115,22,0.18); }
    .ss-left { display: flex; align-items: center; gap: 0.6rem; }
    .ss-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
    .ss-open .ss-dot { background: #22c55e; box-shadow: 0 0 10px rgba(34,197,94,0.4); }
    .ss-closed .ss-dot { background: var(--orange); }
    .ss-text { font-size: 0.72rem; font-weight: 600; }
    .ss-open .ss-text { color: #15803d; }
    .ss-closed .ss-text { color: #c2410c; }
    .dark .ss-open .ss-text { color: #4ade80; }
    .dark .ss-closed .ss-text { color: #fb923c; }
    .ss-timer { font-size: 0.85rem; font-weight: 800; font-variant-numeric: tabular-nums; }
    .ss-open .ss-timer { color: #16a34a; }
    .ss-closed .ss-timer { color: var(--orange); }
    .dark .ss-open .ss-timer { color: #4ade80; }

    /* CTA disabled state */
    .cta-btn.cta-disabled {
        background: #555;
        opacity: 0.7;
        cursor: default;
        box-shadow: none;
        pointer-events: none;
    }

    /* ── Mobile tools (hidden on desktop) ── */
    .mp-mob-tools { display: none; }
    .mp-mob-search-row { display: flex; gap: 0.5rem; margin-bottom: 0.65rem; }
    .mp-mob-search-wrap { flex: 1; position: relative; }
    .mp-mob-search-wrap svg { position: absolute; left: 0.75rem; top: 50%; transform: translateY(-50%); color: #bbb; pointer-events: none; }
    .mp-mob-search {
        width: 100%; height: 38px; padding: 0 0.75rem 0 2.25rem;
        border-radius: 8px; border: 1.5px solid rgba(0,0,0,0.10);
        background: var(--card-bg); font-family: inherit; font-size: 0.75rem; outline: none;
        transition: border-color 0.2s;
    }
    .mp-mob-search:focus { border-color: var(--orange); }
    .mp-mob-search-btn {
        padding: 0 0.85rem; border-radius: 8px; border: none;
        background: var(--orange); color: #fff;
        font-family: inherit; font-size: 0.72rem; font-weight: 700;
        cursor: pointer; flex-shrink: 0; transition: background 0.15s;
    }
    .mp-mob-search-btn:hover { background: #ea6c0a; }
    .mp-mob-filter-btn {
        display: flex; align-items: center; justify-content: center; gap: 0.4rem;
        width: 100%; padding: 0.5rem; border-radius: 8px;
        border: 1.5px solid rgba(0,0,0,0.10); background: var(--card-bg);
        font-family: inherit; font-size: 0.75rem; font-weight: 700; color: #333;
        cursor: pointer; transition: all 0.2s;
    }
    .mp-mob-filter-btn:hover { border-color: var(--orange); color: var(--orange); }
    .mp-mob-filter-btn.active { border-color: var(--orange); color: var(--orange); background: rgba(249,115,22,0.06); }
    .mp-mob-filter-btn svg { transition: transform 0.25s; }
    .mp-mob-filter-btn.active svg { transform: rotate(180deg); }
    .mp-mob-markets {
        max-height: 0; overflow: hidden;
        transition: max-height 0.35s cubic-bezier(0.4,0,0.2,1);
        margin-bottom: 0;
    }
    .mp-mob-markets.open { max-height: 500px; margin-bottom: 0.75rem; }
    .mp-mob-markets-inner {
        padding: 0.75rem 1rem; border-radius: 8px;
        background: var(--card-bg); border: 1px solid rgba(0,0,0,0.06);
        display: flex; flex-wrap: wrap; gap: 0.35rem 0.5rem;
    }
    .mp-mob-mkt {
        padding: 0.3rem 0.65rem; border-radius: 99px;
        font-size: 0.68rem; font-weight: 600; color: #666;
        text-decoration: none; border: 1px solid rgba(0,0,0,0.10);
        background: var(--card-bg); transition: all 0.15s;
    }
    .mp-mob-mkt:hover, .mp-mob-mkt.active { background: rgba(249,115,22,0.06); border-color: rgba(249,115,22,0.3); color: var(--orange); }

    /* Mobile responsive additions */
    @media (max-width: 768px) {
        .mp-sidebar { display: none; }
        .mp-mob-tools { display: block; margin-bottom: 0.75rem; }
        .mp-session-strip { padding: 0.45rem 0.85rem; margin-top: 0.75rem; }
    }
    @media (max-width: 420px) {
        .mp-session-strip { border-radius: 8px; }
        .ss-text { font-size: 0.65rem; }
        .ss-timer { font-size: 0.75rem; }
        .mp-mob-search { height: 36px; font-size: 0.72rem; }
        .mp-mob-search-btn { font-size: 0.65rem; padding: 0 0.7rem; }
        .mp-mob-filter-btn { font-size: 0.68rem; padding: 0.4rem; }
        .mp-mob-mkt { font-size: 0.6rem; padding: 0.25rem 0.55rem; }
    }
</style>

<!-- Hero -->
<section class="mp-hero md:mt-[84px] mt-[70px]">
    <div class="container">
        <div class="mp-hero-inner">
            <div class="mp-hero-logo-wrap">
                <img src="{{ asset('markets/'.$market->image) }}" alt="{{ $market->name }}" loading="eager">
            </div>
            <h1 class="mp-hero-title">{{ $market->name }} <span>Marketplace</span></h1>
            <nav class="mp-breadcrumb">
                <a href="{{ url('/') }}">Bid Market</a>
                <span class="sep">›</span>
                <span class="current">Marketplace</span>
            </nav>
        </div>
    </div>
</section>

<!-- Main -->
<div class="container">

    {{-- Session strip (NEXUS addition - hidden when inactive) --}}
    @if($nexusActive)
    <div class="mp-session-strip {{ $sessionOpen ? 'ss-open' : 'ss-closed' }}">
        <div class="ss-left">
            <span class="ss-dot"></span>
            @if($sessionOpen)
                <span class="ss-text">{{ $activeSessionName }} session active. Place your bids now.</span>
            @else
                <span class="ss-text" id="mpStripText">Next session: {{ $nextSessionName }}. Browse products anytime.</span>
            @endif
        </div>
        <span class="ss-timer" id="mpStripTimer">--:--</span>
    </div>
    @endif
    <div class="mp-layout">

        {{-- ── Sidebar ── --}}
        <aside class="mp-sidebar">
            <div class="sidebar-header">
                <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none" viewBox="0 0 24 24" stroke="var(--orange)" stroke-width="2.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2a1 1 0 01-.293.707L13 13.414V19a1 1 0 01-.553.894l-4 2A1 1 0 017 21v-7.586L3.293 6.707A1 1 0 013 6V4z" />
                </svg>
                <span class="sidebar-header-title">Filter & Navigate</span>
            </div>

            {{-- Search --}}
            <div class="sidebar-section">
                <div class="sidebar-section-label">Search Products</div>
                <form action="{{ route('marketplace', [$market->slug, $market->name]) }}" method="get">
                    @csrf
                    <div class="search-wrap">
                        <span class="search-icon">
                            <svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35M17 11A6 6 0 115 11a6 6 0 0112 0z" />
                            </svg>
                        </span>
                        <input type="text" name="search" id="searchItem" class="search-input" placeholder="Search products...">
                    </div>
                    <button type="submit" class="search-btn">
                        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35M17 11A6 6 0 115 11a6 6 0 0112 0z" />
                        </svg>
                        Search
                    </button>
                </form>
            </div>

            {{-- Markets --}}
            <div class="sidebar-section">
                <div class="sidebar-section-label">Other Markets</div>
                @foreach($markets as $m)
                <a href="{{ route('marketplace', [$m->slug, $m->name]) }}"
                    class="market-link {{ $m->id === $market->id ? 'active' : '' }}">
                    <span class="market-link-dot"></span>
                    {{ $m->name }}
                </a>
                @endforeach
            </div>
        </aside>

        {{-- ── Products ── --}}
        <div>
            {{-- Mobile tools (search + filter, hidden on desktop) --}}
            <div class="mp-mob-tools">
                <form action="{{ route('marketplace', [$market->slug, $market->name]) }}" method="get" class="mp-mob-search-row">
                    @csrf
                    <div class="mp-mob-search-wrap">
                        <svg width="13" height="13" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35M17 11A6 6 0 115 11a6 6 0 0112 0z"/></svg>
                        <input type="text" name="search" class="mp-mob-search" placeholder="Search products...">
                    </div>
                    <button type="submit" class="mp-mob-search-btn">Search</button>
                </form>
                <button class="mp-mob-filter-btn" id="mpFilterBtn" onclick="mpToggleFilter()">
                    <svg width="13" height="13" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M19 9l-7 7-7-7"/></svg>
                    Browse Other Markets
                </button>
                <div class="mp-mob-markets" id="mpMobMarkets">
                    <div class="mp-mob-markets-inner">
                        @foreach($markets as $m)
                            <a href="{{ route('marketplace', [$m->slug, $m->name]) }}"
                               class="mp-mob-mkt {{ $m->id === $market->id ? 'active' : '' }}">
                                {{ $m->name }}
                            </a>
                        @endforeach
                    </div>
                </div>
            </div>

            <div class="products-topbar">
                <span class="sh">Products</span>
                <!--<span class="count-chip">-->
                <!--    <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">-->
                <!--        <path stroke-linecap="round" stroke-linejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 10V7" />-->
                <!--    </svg>-->
                <!--    {{ $products->total() }} items-->
                <!--</span>-->
            </div>

            <div class="products-grid">
                @forelse($products as $p)
                <div class="product-card">

                    {{-- Image --}}
                    <div class="product-img-wrap">
                        <img src="{{ asset('storage/products/'.$p->image1) }}"
                            loading="lazy" alt="{{ $p->name }}">

                        {{-- Market badge --}}
                        <span class="product-market-badge">
                            {{ $p->market->name }}
                        </span>

                        {{-- View icon --}}
                        <a href="{{ route('product-details', $p->slug) }}" class="product-view-btn" title="View product">
                            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                <path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                            </svg>
                        </a>

                        {{-- Hover CTA --}}
                        <div class="product-cta">
                            @if($nexusActive && !$sessionOpen)
                                <span class="cta-btn cta-disabled">Session Closed</span>
                            @else
                                <a href="{{ route('product-details', $p->slug) }}" class="cta-btn">
                                    Place Bid &rarr;
                                </a>
                            @endif
                        </div>
                    </div>

                    {{-- Info --}}
                    <div class="product-info">
                        <a href="{{ route('product-details', $p->slug) }}" class="product-name">{{ $p->name }}</a>

                        <div class="product-price-row">
                            <span class="price-current">${{ number_format($p->value, 2) }}</span>
                            <span class="price-label">Auction</span>
                            <span class="price-old">${{ number_format($p->old_value, 2) }}</span>
                        </div>

                        <div class="stars-row">
                            @php
                            $full = floor($p->rating);
                            $half = ($p->rating - $full) >= 0.5;
                            @endphp
                            @for($i = 0; $i < $full; $i++)
                                <span class="star">★</span>
                                @endfor
                                @if($half)
                                <span class="star" style="opacity:0.55;">★</span>
                                @endif
                                @for($i = 0; $i < (5 - $full - ($half ? 1 : 0)); $i++)
                                    <span class="star" style="opacity:0.18;">★</span>
                                    @endfor
                                    <span class="rating-val">{{ number_format($p->rating, 1) }}</span>
                        </div>
                    </div>

                </div>
                @empty
                <div class="empty-state">
                    <div class="empty-icon">
                        <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.6">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 10V7" />
                        </svg>
                    </div>
                    <h3 class="text-base font-bold" style="margin-bottom:0.4rem;">No products found</h3>
                    <p class="text-sm" style="color:var(--muted);">Try a different search or browse another market.</p>
                </div>
                @endforelse
            </div>

            {{-- Pagination --}}
            @if($products->hasPages())
            <div class="mp-pagination" style="margin-top:2rem;">
                {{ $products->links('pagination::tailwind') }}
            </div>
            @endif
        </div>

    </div>
</div>

@if($nexusActive)
<script>
// Session strip countdown
(function() {
    @if($sessionOpen && $sessionEndTime)
        var endTime = new Date('{{ $sessionEndTime }}').getTime();
    @elseif($nextSessionTime)
        var endTime = new Date('{{ $nextSessionTime }}').getTime();
    @else
        var endTime = 0;
    @endif

    if (!endTime) return;

    var el = document.getElementById('mpStripTimer');
    if (!el) return;

    function tick() {
        var now = Date.now();
        var diff = Math.max(0, Math.floor((endTime - now) / 1000));
        if (diff <= 0) {
            el.textContent = '00:00';
            setTimeout(function() { location.reload(); }, 3000);
            return;
        }
        var h = Math.floor(diff / 3600);
        var m = Math.floor((diff % 3600) / 60);
        var s = diff % 60;
        if (h > 0) {
            el.textContent = h + 'h ' + String(m).padStart(2, '0') + 'm';
        } else {
            el.textContent = String(m).padStart(2, '0') + ':' + String(s).padStart(2, '0');
        }
    }
    tick();
    setInterval(tick, 1000);
})();
</script>
@endif

<script>
// Mobile filter toggle
function mpToggleFilter() {
    var btn = document.getElementById('mpFilterBtn');
    var panel = document.getElementById('mpMobMarkets');
    if (btn && panel) {
        btn.classList.toggle('active');
        panel.classList.toggle('open');
    }
}
</script>

@endsection