{% extends "base.html" %}
{% block title %}Bonny Selects - Premium Jersey Collections{% endblock %}
{% block content %}

<style>
    @import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,400;0,600;0,700;1,400;1,600&family=DM+Sans:wght@300;400;500;600&display=swap');

    :root {
        --black: #080808;
        --white: #fafaf8;
        --gold: #c9a84c;
        --gold-light: #e8d5a3;
        --gold-dark: #8a6d28;
        --gray-100: #f5f4f0;
        --gray-200: #e8e6e0;
        --gray-400: #9e9b92;
        --gray-600: #5c5a54;
        --green: #2d6a4f;
        --red: #c0392b;
        --font-display: 'Cormorant Garamond', serif;
        --font-body: 'DM Sans', sans-serif;
    }

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        -webkit-font-smoothing: antialiased;
    }

    html {
        width: 100vw;
        height: 100vh;
        position: fixed;
        overflow: hidden;
    }

    body {
        width: 100%;
        height: 100%;
        font-family: var(--font-body);
        background: var(--white);
        color: var(--black);
        overflow-y: scroll;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding-top: max(0px, env(safe-area-inset-top));
        padding-bottom: max(72px, calc(72px + env(safe-area-inset-bottom)));
    }

    /* ── HEADER ── */
    .site-header {
        position: sticky;
        top: max(0px, env(safe-area-inset-top));
        z-index: 100;
        background: var(--white);
        border-bottom: 1px solid var(--gray-200);
        padding: 14px 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .wordmark {
        display: flex;
        flex-direction: column;
        line-height: 1;
    }

    .wordmark-main {
        font-family: var(--font-display);
        font-size: 22px;
        font-weight: 700;
        letter-spacing: 0.02em;
        color: var(--black);
    }

    .wordmark-sub {
        font-size: 9px;
        font-weight: 500;
        letter-spacing: 0.25em;
        text-transform: uppercase;
        color: var(--gold);
        margin-top: 2px;
    }

    .header-actions {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .icon-btn {
        width: 40px;
        height: 40px;
        border: none;
        background: none;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        position: relative;
        transition: background 0.2s;
        -webkit-appearance: none;
    }

    .icon-btn:active { background: var(--gray-100); }

    .icon-btn svg {
        width: 22px;
        height: 22px;
        stroke: var(--black);
        fill: none;
        stroke-width: 1.8;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    #cartBadge {
        position: absolute;
        top: 4px;
        right: 4px;
        background: var(--gold);
        color: var(--black);
        width: 17px;
        height: 17px;
        border-radius: 50%;
        display: none;
        align-items: center;
        justify-content: center;
        font-size: 9px;
        font-weight: 700;
        font-family: var(--font-body);
    }

    /* ── BOTTOM NAV ── */
    .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: max(64px, calc(64px + env(safe-area-inset-bottom)));
        background: var(--white);
        border-top: 1px solid var(--gray-200);
        display: flex;
        z-index: 200;
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }

    .nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 5px;
        border: none;
        background: none;
        cursor: pointer;
        color: var(--gray-400);
        font-family: var(--font-body);
        font-size: 10px;
        font-weight: 500;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        transition: color 0.2s;
        -webkit-appearance: none;
        border-top: 2px solid transparent;
        padding-top: 10px;
    }

    .nav-item svg {
        width: 20px;
        height: 20px;
        stroke: currentColor;
        fill: none;
        stroke-width: 1.7;
        stroke-linecap: round;
        stroke-linejoin: round;
        transition: stroke 0.2s;
    }

    .nav-item.active {
        color: var(--black);
        border-top-color: var(--gold);
    }

    .nav-item:active { opacity: 0.6; }

    /* ── TAB PANELS ── */
    .tab-panel { display: none; }
    .tab-panel.active { display: block; animation: fadeUp 0.3s ease; }

    @keyframes fadeUp {
        from { opacity: 0; transform: translateY(6px); }
        to   { opacity: 1; transform: translateY(0); }
    }

    /* ── STORY / ABOUT TAB ── */
    .story-hero {
        padding: 40px 24px 32px;
        background: var(--black);
        color: var(--white);
        position: relative;
        overflow: hidden;
    }

    .story-hero::before {
        content: 'BS';
        position: absolute;
        right: -10px;
        top: -20px;
        font-family: var(--font-display);
        font-size: 160px;
        font-weight: 700;
        color: rgba(201, 168, 76, 0.06);
        line-height: 1;
        pointer-events: none;
        user-select: none;
    }

    .story-label {
        font-size: 10px;
        font-weight: 600;
        letter-spacing: 0.3em;
        text-transform: uppercase;
        color: var(--gold);
        margin-bottom: 16px;
    }

    .story-headline {
        font-family: var(--font-display);
        font-size: 34px;
        font-weight: 600;
        line-height: 1.2;
        color: var(--white);
        font-style: italic;
        margin-bottom: 20px;
    }

    .story-body {
        font-size: 14px;
        line-height: 1.8;
        color: rgba(250, 250, 248, 0.7);
        margin-bottom: 12px;
    }

    .story-body em {
        color: var(--gold-light);
        font-style: normal;
        font-weight: 500;
    }

    .story-cta {
        margin-top: 28px;
        display: inline-flex;
        align-items: center;
        gap: 10px;
        padding: 13px 24px;
        background: var(--gold);
        color: var(--black);
        font-family: var(--font-body);
        font-size: 12px;
        font-weight: 600;
        letter-spacing: 0.15em;
        text-transform: uppercase;
        border: none;
        cursor: pointer;
        transition: all 0.2s;
        -webkit-appearance: none;
    }

    .story-cta svg {
        width: 14px;
        height: 14px;
        stroke: var(--black);
        fill: none;
        stroke-width: 2.5;
        stroke-linecap: round;
        stroke-linejoin: round;
        transition: transform 0.2s;
    }

    .story-cta:active {
        background: var(--white);
        transform: scale(0.98);
    }

    .story-cta:active svg { transform: translateX(3px); }

    /* PILLARS */
    .pillars {
        padding: 32px 20px;
        background: var(--white);
    }

    .section-label {
        font-size: 9px;
        font-weight: 600;
        letter-spacing: 0.3em;
        text-transform: uppercase;
        color: var(--gray-400);
        margin-bottom: 20px;
    }

    .pillar-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }

    .pillar {
        padding: 18px 16px;
        background: var(--gray-100);
        border-left: 3px solid var(--gold);
        transition: background 0.2s;
    }

    .pillar:active { background: var(--gray-200); }

    .pillar-icon {
        width: 32px;
        height: 32px;
        margin-bottom: 10px;
        color: var(--gold-dark);
    }

    .pillar-icon svg {
        width: 100%;
        height: 100%;
        stroke: currentColor;
        fill: none;
        stroke-width: 1.6;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .pillar-title {
        font-size: 13px;
        font-weight: 600;
        color: var(--black);
        margin-bottom: 4px;
    }

    .pillar-desc {
        font-size: 11px;
        color: var(--gray-600);
        line-height: 1.5;
    }

    /* QUOTE BAND */
    .quote-band {
        padding: 28px 24px;
        background: var(--black);
        text-align: center;
    }

    .quote-text {
        font-family: var(--font-display);
        font-size: 22px;
        font-weight: 400;
        font-style: italic;
        color: var(--white);
        line-height: 1.5;
    }

    .quote-text span {
        color: var(--gold);
    }

    /* ── SHOP TAB ── */
    .shop-header {
        padding: 20px 20px 12px;
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        border-bottom: 1px solid var(--gray-200);
    }

    .shop-title {
        font-family: var(--font-display);
        font-size: 26px;
        font-weight: 600;
    }

    .shop-count {
        font-size: 12px;
        color: var(--gray-400);
        font-weight: 400;
    }

    .products-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1px;
        background: var(--gray-200);
        padding: 0;
    }

    /* PRODUCT CARD */
    .product-card {
        background: var(--white);
        display: flex;
        flex-direction: column;
        text-decoration: none;
        color: inherit;
        position: relative;
        transition: background 0.15s;
        cursor: pointer;
    }

    .product-card:active { background: var(--gray-100); }

    .product-img-wrap {
        position: relative;
        aspect-ratio: 4/5;
        overflow: hidden;
        background: var(--gray-100);
    }

    .product-img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
        transition: transform 0.4s ease;
    }

    .product-card:active .product-img { transform: scale(1.03); }

    .status-pill {
        position: absolute;
        top: 10px;
        left: 10px;
        padding: 4px 10px;
        font-size: 9px;
        font-weight: 600;
        letter-spacing: 0.15em;
        text-transform: uppercase;
    }

    .status-pill.available {
        background: var(--gold);
        color: var(--black);
    }

    .status-pill.sold {
        background: rgba(8,8,8,0.75);
        color: var(--white);
    }

    .product-body {
        padding: 14px 14px 12px;
        display: flex;
        flex-direction: column;
        flex: 1;
    }

    .product-name {
        font-family: var(--font-display);
        font-size: 16px;
        font-weight: 600;
        line-height: 1.2;
        color: var(--black);
        margin-bottom: 4px;
    }

    .product-price {
        font-size: 13px;
        font-weight: 600;
        color: var(--gold-dark);
        margin-bottom: 12px;
    }

    .add-btn {
        margin-top: auto;
        padding: 10px 0;
        background: var(--black);
        color: var(--white);
        border: none;
        font-family: var(--font-body);
        font-size: 10px;
        font-weight: 600;
        letter-spacing: 0.18em;
        text-transform: uppercase;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        transition: all 0.2s;
        -webkit-appearance: none;
        min-height: 36px;
    }

    .add-btn svg {
        width: 14px;
        height: 14px;
        stroke: currentColor;
        fill: none;
        stroke-width: 2;
        stroke-linecap: round;
        stroke-linejoin: round;
        flex-shrink: 0;
    }

    .add-btn:active { background: var(--gold); color: var(--black); }
    .add-btn:active svg { stroke: var(--black); }

    .add-btn.success {
        background: var(--green);
        pointer-events: none;
    }

    .add-btn.loading {
        opacity: 0.6;
        pointer-events: none;
    }

    .add-btn[disabled] {
        background: var(--gray-200);
        color: var(--gray-400);
        cursor: not-allowed;
    }

    /* SPINNER */
    .spin {
        width: 14px;
        height: 14px;
        border: 2px solid rgba(255,255,255,0.3);
        border-top-color: #fff;
        border-radius: 50%;
        animation: rotate 0.6s linear infinite;
    }

    @keyframes rotate { to { transform: rotate(360deg); } }

    /* EMPTY STATE */
    .empty-state {
        padding: 60px 24px;
        text-align: center;
    }

    .empty-state-icon {
        width: 56px;
        height: 56px;
        margin: 0 auto 16px;
        color: var(--gray-400);
    }

    .empty-state-icon svg {
        width: 100%;
        height: 100%;
        stroke: currentColor;
        fill: none;
        stroke-width: 1.4;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .empty-title {
        font-family: var(--font-display);
        font-size: 24px;
        font-weight: 600;
        color: var(--black);
        margin-bottom: 8px;
    }

    .empty-sub {
        font-size: 13px;
        color: var(--gray-400);
        line-height: 1.6;
    }

    /* ── CART DRAWER ── */
    .cart-overlay {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 500;
    }

    .cart-overlay.open { display: block; }

    .cart-backdrop {
        position: absolute;
        inset: 0;
        background: rgba(8,8,8,0.4);
        animation: fadeIn 0.25s ease;
    }

    @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

    .cart-drawer {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--white);
        border-radius: 16px 16px 0 0;
        max-height: 88vh;
        display: flex;
        flex-direction: column;
        animation: slideUp 0.3s cubic-bezier(0.34, 1.1, 0.64, 1);
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }

    @keyframes slideUp {
        from { transform: translateY(100%); }
        to   { transform: translateY(0); }
    }

    .cart-handle {
        width: 36px;
        height: 4px;
        background: var(--gray-200);
        border-radius: 2px;
        margin: 14px auto 0;
        flex-shrink: 0;
    }

    .cart-head {
        padding: 16px 20px 14px;
        border-bottom: 1px solid var(--gray-200);
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-shrink: 0;
    }

    .cart-title {
        font-family: var(--font-display);
        font-size: 22px;
        font-weight: 600;
    }

    .cart-close {
        width: 32px;
        height: 32px;
        border: 1px solid var(--gray-200);
        background: none;
        border-radius: 50%;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        -webkit-appearance: none;
    }

    .cart-close svg {
        width: 14px;
        height: 14px;
        stroke: var(--black);
        fill: none;
        stroke-width: 2.5;
        stroke-linecap: round;
    }

    .cart-body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 16px 20px;
    }

    .cart-empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 40px 0;
        gap: 12px;
    }

    .cart-empty svg {
        width: 48px;
        height: 48px;
        stroke: var(--gray-400);
        fill: none;
        stroke-width: 1.4;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .cart-empty p {
        font-size: 14px;
        color: var(--gray-400);
        margin: 0;
    }

    .cart-item {
        display: flex;
        gap: 14px;
        padding: 14px 0;
        border-bottom: 1px solid var(--gray-200);
    }

    .cart-item-img {
        width: 64px;
        height: 64px;
        object-fit: cover;
        flex-shrink: 0;
        background: var(--gray-100);
    }

    .cart-item-info { flex: 1; }

    .cart-item-name {
        font-size: 14px;
        font-weight: 500;
        color: var(--black);
        margin-bottom: 4px;
    }

    .cart-item-price {
        font-size: 12px;
        color: var(--gold-dark);
        font-weight: 600;
        margin-bottom: 10px;
    }

    .qty-row {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .qty-btn {
        width: 28px;
        height: 28px;
        border: 1px solid var(--gray-200);
        background: none;
        cursor: pointer;
        font-size: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--black);
        font-family: var(--font-body);
        -webkit-appearance: none;
        transition: all 0.15s;
    }

    .qty-btn:active { background: var(--gray-100); }

    .qty-val {
        font-size: 14px;
        font-weight: 600;
        min-width: 20px;
        text-align: center;
    }

    .remove-btn {
        margin-left: auto;
        background: none;
        border: none;
        cursor: pointer;
        padding: 4px;
        color: var(--gray-400);
        -webkit-appearance: none;
        display: flex;
        align-items: center;
    }

    .remove-btn svg {
        width: 16px;
        height: 16px;
        stroke: currentColor;
        fill: none;
        stroke-width: 2;
        stroke-linecap: round;
    }

    .remove-btn:active { color: var(--red); }

    .cart-foot {
        padding: 16px 20px 0;
        border-top: 1px solid var(--gray-200);
        flex-shrink: 0;
    }

    .cart-total-row {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        margin-bottom: 16px;
    }

    .cart-total-label {
        font-size: 13px;
        color: var(--gray-600);
        font-weight: 500;
    }

    .cart-total-val {
        font-family: var(--font-display);
        font-size: 22px;
        font-weight: 700;
        color: var(--black);
    }

    .checkout-btn {
        width: 100%;
        padding: 15px;
        background: var(--black);
        color: var(--white);
        border: none;
        font-family: var(--font-body);
        font-size: 11px;
        font-weight: 600;
        letter-spacing: 0.2em;
        text-transform: uppercase;
        cursor: pointer;
        text-decoration: none;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        -webkit-appearance: none;
        transition: background 0.2s;
    }

    .checkout-btn svg {
        width: 14px;
        height: 14px;
        stroke: currentColor;
        fill: none;
        stroke-width: 2.2;
        stroke-linecap: round;
        stroke-linejoin: round;
    }

    .checkout-btn:active { background: var(--gold); color: var(--black); }

    .continue-btn {
        width: 100%;
        padding: 12px;
        margin-top: 10px;
        background: none;
        border: none;
        font-family: var(--font-body);
        font-size: 12px;
        font-weight: 500;
        color: var(--gray-400);
        cursor: pointer;
        -webkit-appearance: none;
    }

    /* CONNECTIVITY */
    .conn-banner {
        position: fixed;
        top: max(0px, env(safe-area-inset-top));
        left: 0; right: 0;
        padding: 10px 20px;
        text-align: center;
        font-size: 12px;
        font-weight: 600;
        letter-spacing: 0.05em;
        z-index: 1000;
        transform: translateY(-100%);
        transition: transform 0.3s ease;
    }

    .conn-banner.show { transform: translateY(0); }
    .conn-banner.offline { background: var(--red); color: #fff; }
    .conn-banner.online  { background: var(--green); color: #fff; }
</style>

<!-- Connectivity Banner -->
<div class="conn-banner" id="connBanner"></div>

<!-- HEADER -->
<header class="site-header">
    <div class="wordmark">
        <span class="wordmark-main">Bonny Selects</span>
        <span class="wordmark-sub">Premium Jerseys</span>
    </div>
    <div class="header-actions">
        <button class="icon-btn" onclick="openCart()" aria-label="Cart">
            <svg viewBox="0 0 24 24"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
            <div id="cartBadge"></div>
        </button>
    </div>
</header>

<!-- MAIN CONTENT -->
<main>
    <!-- STORY PANEL -->
    <div id="panel-story" class="tab-panel active">

        <div class="story-hero">
            <p class="story-label">Our Philosophy</p>
            <h1 class="story-headline">You're not just buying a jersey. You're choosing how you show up.</h1>
            <p class="story-body">This isn't what fades after a few washes. Every thread carries <em>style, confidence, and presence</em> — that feeling when you step out and people notice without you saying a word.</p>
            <p class="story-body">Every design tells a story. Every detail is intentional. It doesn't blend in — <em>it stands out.</em></p>
            <button class="story-cta" onclick="switchTab('shop', document.querySelector('.nav-item:nth-child(2)'))">
                Browse Collection
                <svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>
            </button>
        </div>

        <div class="pillars">
            <p class="section-label">Why Bonny Selects</p>
            <div class="pillar-grid">
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>
                    </div>
                    <p class="pillar-title">Premium Quality</p>
                    <p class="pillar-desc">Authentic jerseys that outlast the season</p>
                </div>
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/></svg>
                    </div>
                    <p class="pillar-title">Curated Picks</p>
                    <p class="pillar-desc">Hand-selected designs that actually stand out</p>
                </div>
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
                    </div>
                    <p class="pillar-title">Saturday Delivery</p>
                    <p class="pillar-desc">Order now, receive this Saturday</p>
                </div>
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                    </div>
                    <p class="pillar-title">WhatsApp Orders</p>
                    <p class="pillar-desc">Direct line — simple, fast, personal</p>
                </div>
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" y1="21" x2="16" y2="21"/><line x1="12" y1="17" x2="12" y2="21"/></svg>
                    </div>
                    <p class="pillar-title">Weekly Drops</p>
                    <p class="pillar-desc">New collections Monday & Friday</p>
                </div>
                <div class="pillar">
                    <div class="pillar-icon">
                        <svg viewBox="0 0 24 24"><path d="M20 7H4a2 2 0 00-2 2v6a2 2 0 002 2h16a2 2 0 002-2V9a2 2 0 00-2-2z"/><path d="M16 21V5a2 2 0 00-2-2h-4a2 2 0 00-2 2v16"/></svg>
                    </div>
                    <p class="pillar-title">Pay on Delivery</p>
                    <p class="pillar-desc">No upfront payment. See it, love it, pay it</p>
                </div>
            </div>
        </div>

        <div class="quote-band">
            <p class="quote-text">"Don't wear what everyone else is wearing.<br><span>Be the one they look at twice.</span>"</p>
        </div>

    </div>

    <!-- SHOP PANEL -->
    <div id="panel-shop" class="tab-panel">

        {% if products and products|length > 0 %}
        <div class="shop-header">
            <h2 class="shop-title">Collection</h2>
            <span class="shop-count">{{ products|length }} piece{% if products|length != 1 %}s{% endif %}</span>
        </div>

        <div class="products-grid">
            {% for product in products %}
            <a href="{{ url_for('public.item_detail', product_id=product._id) }}"
               class="product-card"
               data-product-id="{{ product._id }}"
               style="text-decoration:none;">

                <div class="product-img-wrap">
                    <img src="{{ url_for('static', filename=product.image) }}"
                         alt="{{ product.title }}"
                         class="product-img"
                         loading="lazy">
                    <span class="status-pill {{ product.status }}" data-status="{{ product.status }}">
                        {{ product.status }}
                    </span>
                </div>

                <div class="product-body">
                    <p class="product-name">{{ product.title }}</p>
                    <p class="product-price">₦{{ product.price }}</p>

                    {% if product.status == "available" %}
                    <button type="button" class="add-btn"
                        onclick="event.preventDefault(); event.stopPropagation(); handleAddToCart(event, '{{ product._id }}', '{{ product.title }}', '{{ product.price }}', '{{ product.image }}')">
                        <svg viewBox="0 0 24 24"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
                        Add to Cart
                    </button>
                    {% else %}
                    <button type="button" class="add-btn" disabled>Sold Out</button>
                    {% endif %}
                </div>
            </a>
            {% endfor %}
        </div>

        {% else %}
        <div class="empty-state">
            <div class="empty-state-icon">
                <svg viewBox="0 0 24 24"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
            </div>
            <p class="empty-title">Coming Soon</p>
            <p class="empty-sub">New drops every Monday & Friday.<br>Stay tuned.</p>
        </div>
        {% endif %}

    </div>
</main>

<!-- BOTTOM NAV -->
<nav class="bottom-nav">
    <button class="nav-item active" onclick="switchTab('story', this)">
        <svg viewBox="0 0 24 24"><path d="M2 3h6a4 4 0 014 4v14a3 3 0 00-3-3H2z"/><path d="M22 3h-6a4 4 0 00-4 4v14a3 3 0 013-3h7z"/></svg>
        Story
    </button>
    <button class="nav-item" onclick="switchTab('shop', this)">
        <svg viewBox="0 0 24 24"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
        Shop
    </button>
</nav>

<!-- CART DRAWER -->
<div class="cart-overlay" id="cartOverlay">
    <div class="cart-backdrop" onclick="closeCart()"></div>
    <div class="cart-drawer">
        <div class="cart-handle"></div>
        <div class="cart-head">
            <h2 class="cart-title">Your Cart</h2>
            <button class="cart-close" onclick="closeCart()">
                <svg viewBox="0 0 24 24"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
            </button>
        </div>
        <div class="cart-body" id="cartBody"></div>
        <div class="cart-foot" id="cartFoot" style="display:none;">
            <div class="cart-total-row">
                <span class="cart-total-label">Total</span>
                <span class="cart-total-val" id="cartTotalVal">₦0</span>
            </div>
            <a href="{{ url_for('public.checkout') }}" class="checkout-btn">
                Proceed to Checkout
                <svg viewBox="0 0 24 24"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>
            </a>
            <button class="continue-btn" onclick="closeCart()">Continue Shopping</button>
        </div>
    </div>
</div>

<script src="{{ url_for('static', filename='js/shopping_cart.js') }}"></script>
<script src="{{ url_for('static', filename='js/real_time_products.js') }}"></script>

<script>
    // ── TAB SWITCHING ──
    function switchTab(id, btn) {
        document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
        document.querySelectorAll('.nav-item').forEach(b => b.classList.remove('active'));
        document.getElementById('panel-' + id).classList.add('active');
        btn.classList.add('active');
        window.scrollTo(0, 0);
        localStorage.setItem('bs_tab', id);
    }

    window.addEventListener('load', () => {
        const last = localStorage.getItem('bs_tab') || 'shop';
        const btn = last === 'shop'
            ? document.querySelector('.nav-item:nth-child(2)')
            : document.querySelector('.nav-item:nth-child(1)');
        switchTab(last, btn);

        if (typeof cart !== 'undefined') cart.updateCartUI();
    });

    // ── CART ──
    function openCart() {
        document.getElementById('cartOverlay').classList.add('open');
        document.body.style.overflow = 'hidden';
        renderCartDrawer();
    }

    function closeCart() {
        document.getElementById('cartOverlay').classList.remove('open');
        document.body.style.overflow = '';
    }

    function renderCartDrawer() {
        const items = cart.getItems();
        const body = document.getElementById('cartBody');
        const foot = document.getElementById('cartFoot');
        const totalVal = document.getElementById('cartTotalVal');

        if (!items.length) {
            foot.style.display = 'none';
            body.innerHTML = `
                <div class="cart-empty">
                    <svg viewBox="0 0 24 24"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
                    <p>Your cart is empty</p>
                </div>`;
            return;
        }

        foot.style.display = 'block';
        totalVal.textContent = '₦' + cart.getTotal().toLocaleString();

        body.innerHTML = items.map(item => `
            <div class="cart-item">
                <img class="cart-item-img" src="{{ url_for('static', filename='') }}${item.image}" alt="${item.title}">
                <div class="cart-item-info">
                    <p class="cart-item-name">${item.title}</p>
                    <p class="cart-item-price">₦${item.price}</p>
                    <div class="qty-row">
                        <button class="qty-btn" onclick="changeQty('${item._id}', ${item.quantity - 1})">−</button>
                        <span class="qty-val">${item.quantity}</span>
                        <button class="qty-btn" onclick="changeQty('${item._id}', ${item.quantity + 1})">+</button>
                        <button class="remove-btn" onclick="removeItem('${item._id}')">
                            <svg viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14H6L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/><path d="M9 6V4h6v2"/></svg>
                        </button>
                    </div>
                </div>
            </div>
        `).join('');
    }

    function changeQty(id, qty) {
        if (qty < 1) { cart.removeItem(id); }
        else { cart.updateQuantity(id, qty); }
        renderCartDrawer();
    }

    function removeItem(id) {
        cart.removeItem(id);
        renderCartDrawer();
    }

    // ── ADD TO CART ──
    function handleAddToCart(e, id, title, price, image) {
        const btn = e.target.closest('.add-btn');
        btn.classList.add('loading');
        btn.innerHTML = '<div class="spin"></div>';

        setTimeout(() => {
            cart.addItem({ _id: id, title, price, image });
            btn.classList.remove('loading');
            btn.classList.add('success');
            btn.innerHTML = `
                <svg viewBox="0 0 24 24" width="14" height="14" stroke="currentColor" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
                Added`;

            setTimeout(() => {
                btn.classList.remove('success');
                btn.innerHTML = `
                    <svg viewBox="0 0 24 24" width="14" height="14" stroke="currentColor" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2L3 6v14a2 2 0 002 2h14a2 2 0 002-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 01-8 0"/></svg>
                    Add to Cart`;
            }, 2000);
        }, 250);
    }

    // ── CONNECTIVITY ──
    const connBanner = document.getElementById('connBanner');
    let connTimer;

    function updateConn() {
        clearTimeout(connTimer);
        if (navigator.onLine) {
            connBanner.className = 'conn-banner online show';
            connBanner.textContent = 'Back online';
            connTimer = setTimeout(() => connBanner.classList.remove('show'), 3000);
        } else {
            connBanner.className = 'conn-banner offline show';
            connBanner.textContent = 'You are offline';
        }
    }

    window.addEventListener('online', updateConn);
    window.addEventListener('offline', updateConn);

    // ── CART BADGE ──
    setInterval(() => {
        if (typeof cart !== 'undefined') cart.updateCartUI();
    }, 1000);

    // ── AUTO-REFRESH PRODUCTS ──
    setInterval(() => {
        fetch('{{ url_for("public.index") }}')
            .then(r => r.text())
            .then(html => {
                const doc = new DOMParser().parseFromString(html, 'text/html');
                doc.querySelectorAll('[data-product-id]').forEach(newCard => {
                    const id = newCard.dataset.productId;
                    const cur = document.querySelector(`[data-product-id="${id}"]`);
                    if (!cur) return;
                    const newPill = newCard.querySelector('[data-status]');
                    const curPill = cur.querySelector('[data-status]');
                    if (newPill && curPill && newPill.dataset.status !== curPill.dataset.status) {
                        curPill.dataset.status = newPill.dataset.status;
                        curPill.textContent = newPill.dataset.status;
                        curPill.className = 'status-pill ' + newPill.dataset.status;
                    }
                });
            }).catch(() => {});
    }, 30000);

    // Touch optimizations
    document.addEventListener('touchstart', e => {
        if (e.touches.length > 1) e.preventDefault();
    }, { passive: false });
</script>

{% endblock %}
