﻿<?php
require_once '../config/db.php';
require_once '../config/auth.php';
require_once '../config/functions.php';

$page_title = 'Browse Items';
$q    = sanitize($conn, $_GET['q'] ?? '');
$type = $_GET['type'] ?? 'all';
$cat  = sanitize($conn, $_GET['category'] ?? '');

$where_lost  = "WHERE l.status NOT IN ('closed','archived')";
$where_found = "WHERE f.status NOT IN ('closed','archived')";
if ($q) {
    $where_lost  .= " AND (l.item_name LIKE '%$q%' OR l.description LIKE '%$q%' OR l.location LIKE '%$q%')";
    $where_found .= " AND (f.item_name LIKE '%$q%' OR f.description LIKE '%$q%' OR f.location LIKE '%$q%')";
}
if ($cat) {
    $where_lost  .= " AND l.category='$cat'";
    $where_found .= " AND f.category='$cat'";
}

$lost_items  = ($type === 'all' || $type === 'lost')  ? $conn->query("SELECT l.*, u.name as reporter, u.id as poster_id, u.role as poster_role, u.phone as reporter_phone, u.avatar as reporter_avatar, COALESCE(NULLIF(l.contact_number,''), u.phone) as contact FROM lost_items l JOIN users u ON l.user_id=u.id $where_lost ORDER BY l.created_at DESC") : null;
$found_items = ($type === 'all' || $type === 'found') ? $conn->query("SELECT f.*, u.name as reporter, u.id as poster_id, u.role as poster_role, u.phone as reporter_phone, u.avatar as reporter_avatar FROM found_items f JOIN users u ON f.user_id=u.id $where_found ORDER BY f.created_at DESC") : null;
$current_user_id = isLoggedIn() ? (int)$_SESSION['user_id'] : 0;
$categories  = $conn->query("SELECT name FROM categories ORDER BY name");
$total_lost  = $conn->query("SELECT COUNT(*) c FROM lost_items WHERE status NOT IN ('closed','archived')")->fetch_assoc()['c'];
$total_found = $conn->query("SELECT COUNT(*) c FROM found_items WHERE status NOT IN ('closed','archived')")->fetch_assoc()['c'];

$lost_data = []; $found_data = [];
if ($lost_items)  { while($r=$lost_items->fetch_assoc())  $lost_data[]=$r;  }
if ($found_items) { while($r=$found_items->fetch_assoc()) $found_data[]=$r; }

function timeAgo($datetime) {
    $diff = time() - strtotime($datetime);
    if ($diff < 3600)   return max(1, round($diff/60)) . 'm ago';
    if ($diff < 86400)  return round($diff/3600) . 'h ago';
    if ($diff < 604800) return round($diff/86400) . 'd ago';
    return date('M d', strtotime($datetime));
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browse Items - RELINK</title>
<link rel="stylesheet" href="/lostfound_relink/assets/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVJkEZSMUkrQ6usKlVzEqLv7KqAzNmtzjK8+3+47C4YzgyGgrMR7DcxtxOGtY5CBcYM0wlt+sKg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
/* TRUST BANNER */
.trust-banner {
    background: linear-gradient(90deg, #1a3a00 0%, #2d6200 50%, #1a3a00 100%);
    padding: 10px 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 32px;
    flex-wrap: wrap;
    border-bottom: 1px solid rgba(200,240,96,.15);
}
.trust-item {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    font-weight: 600;
    color: rgba(200,240,96,.85);
    letter-spacing: .3px;
}
.trust-dot {
    width: 6px; height: 6px; border-radius: 50%;
    background: #c8f060;
    box-shadow: 0 0 6px #c8f060;
    animation: trustPulse 2s infinite;
}
@keyframes trustPulse {
    0%,100% { opacity:1; transform:scale(1); }
    50% { opacity:.5; transform:scale(1.4); }
}

/* NAV */
.pub-nav {
    background: linear-gradient(135deg, #0a1f00 0%, #1e4d00 50%, #0a1f00 100%);
    padding:16px 28px; display:flex; align-items:center; justify-content:space-between;
    position:sticky; top:0; z-index:200;
    box-shadow: 0 8px 32px rgba(0,0,0,.25);
    border-bottom:2px solid rgba(200,240,96,.15);
    backdrop-filter: blur(10px);
}
.pub-nav .logo { display:flex; align-items:center; gap:12px; }
.pub-nav .logo img { width:40px; height:40px; object-fit:contain; filter: drop-shadow(0 2px 8px rgba(0,0,0,.2)); }
.pub-nav .logo-text { font-size:20px; font-weight:900; color:#c8f060; letter-spacing:2px; }
.pub-nav .logo-sub { font-size:11px; color:rgba(255,255,255,.55); display:block; line-height:1; letter-spacing:.6px; margin-top:2px; }
.pub-nav .nav-actions { display:flex; gap:10px; align-items:center; }

/* HERO */
.browse-hero {
    position: relative;
    background: linear-gradient(135deg, #0a1f00 0%, #1e4d00 35%, #2d6e00 65%, #3a8a00 100%);
    color: #fff;
    padding: 80px 24px 60px;
    text-align: center;
    overflow: hidden;
}
.browse-hero::before {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(ellipse at 20% 50%, rgba(100,200,0,.2) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 20%, rgba(200,240,96,.15) 0%, transparent 50%),
        url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.04'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    animation: heroBgShift 12s ease-in-out infinite alternate;
}
@keyframes heroBgShift {
    from { opacity: .8; transform: scale(1); }
    to   { opacity: 1; transform: scale(1.02); }
}
.browse-hero::after {
    content: '';
    position: absolute;
    bottom: -2px; left: 0; right: 0;
    height: 60px;
    background: var(--bg);
    clip-path: ellipse(65% 100% at 50% 100%);
}
.browse-hero-inner { position: relative; z-index: 1; max-width: 850px; margin: 0 auto; }
.browse-hero-logo { display:flex; align-items:center; justify-content:center; gap:20px; margin-bottom:20px; }
.browse-hero-logo img {
    width:90px; height:90px; object-fit:contain;
    filter: drop-shadow(0 8px 20px rgba(0,0,0,.3));
    animation: logoPulse 3s ease-in-out infinite;
}
@keyframes logoPulse {
    0%,100% { transform: scale(1); }
    50%      { transform: scale(1.08); }
}
.browse-hero-logo-text { text-align:left; }
.browse-hero-logo-text h1 {
    font-size:42px; font-weight:950; color:#fff; letter-spacing:3px; line-height:1; margin:0;
    text-shadow: 0 3px 15px rgba(0,0,0,.4);
}
.browse-hero-logo-text h1 span { color:#c8f060; }
.browse-hero-logo-text .sub { font-size:12px; color:rgba(255,255,255,.65); letter-spacing:1.2px; text-transform:uppercase; margin-top:6px; display:block; font-weight:600; }
.browse-hero-tagline {
    font-size:16px; color:rgba(255,255,255,.8); margin-bottom:36px; margin-top:16px;
    display:inline-flex; align-items:center; gap:10px;
    background:rgba(255,255,255,.1); border:1px solid rgba(255,255,255,.2);
    padding:10px 24px; border-radius:40px; backdrop-filter:blur(8px);
    font-weight:500;
}
.browse-stats { display:flex; gap:16px; justify-content:center; flex-wrap:wrap; margin-top:14px; }
.browse-stat {
    padding:20px 32px; text-align:center;
    background:rgba(255,255,255,.08);
    border:1px solid rgba(255,255,255,.15);
    border-radius:20px; backdrop-filter:blur(10px);
    transition: transform .3s cubic-bezier(.4,0,.2,1), background .3s, box-shadow .3s;
    min-width:130px;
    cursor: default;
}
.browse-stat:hover { 
    transform: translateY(-6px);
    background:rgba(255,255,255,.15);
    box-shadow: 0 12px 28px rgba(0,0,0,.15);
    border-color: rgba(255,255,255,.25);
}
.browse-stat .num { font-size:42px; font-weight:950; color:#c8f060; line-height:1; text-shadow: 0 3px 10px rgba(0,0,0,.25); }
.browse-stat .lbl { font-size:11px; color:rgba(255,255,255,.65); margin-top:6px; text-transform:uppercase; letter-spacing:1.5px; font-weight:600; }

/* BODY */
.pub-body { max-width:1400px; margin:0 auto; padding:40px 20px; }

/* FILTER */
.filter-bar { 
    background: linear-gradient(135deg, #fff 0%, #f8fafc 100%);
    border:1px solid var(--border); 
    border-radius: 18px; 
    padding: 20px 24px; 
    margin-bottom: 28px; 
    display:flex; 
    gap:12px; 
    flex-wrap:wrap; 
    align-items:center; 
    box-shadow: 0 4px 16px rgba(0,0,0,.06);
    backdrop-filter: blur(8px);
}
.filter-bar .form-control { 
    flex:1; 
    min-width:140px;
    border-radius: 12px;
    border: 1px solid var(--border);
    font-size: 14px;
    font-weight: 500;
}
.filter-search-wrap { 
    flex:2; 
    min-width:240px; 
    position:relative; 
}
.filter-search-wrap .search-icon { 
    position:absolute; 
    left:14px; 
    top:50%; 
    transform:translateY(-50%); 
    font-size:16px; 
    pointer-events:none;
    opacity: .6;
}
.filter-search-wrap input { 
    padding-left:42px; 
    width:100%;
}

/* TABS */
.tabs { 
    display:flex; 
    gap:8px; 
    margin-bottom:32px; 
    flex-wrap:wrap; 
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg) 100%);
    border:1px solid var(--border); 
    border-radius:50px; 
    padding:6px; 
    width:fit-content;
    box-shadow: 0 2px 8px rgba(0,0,0,.04);
}
.tab-btn { 
    padding:10px 24px; 
    border-radius:45px; 
    font-size:14px; 
    font-weight:600; 
    cursor:pointer; 
    border:none; 
    background:transparent; 
    color:var(--text-muted); 
    transition: all .3s cubic-bezier(.4,0,.2,1);
    text-decoration:none; 
    display:inline-block; 
    white-space:nowrap;
}
.tab-btn.active { 
    background: linear-gradient(135deg, var(--primary) 0%, #5db82f 100%);
    color:#fff;
    box-shadow: 0 4px 12px rgba(79,157,28,.35);
    font-weight:700;
}

/* CARDS */
.items-grid { 
    display:grid; 
    grid-template-columns:repeat(auto-fill,minmax(270px,1fr)); 
    gap: 24px; 
    margin-bottom:28px;
}
.item-card { 
    background:#fff; 
    border-radius:20px; 
    border:1px solid var(--border); 
    box-shadow: 0 4px 16px rgba(0,0,0,.06);
    overflow:hidden; 
    transition: box-shadow .35s cubic-bezier(.4,0,.2,1), transform .35s, border-color .3s;
    display:flex; 
    flex-direction:column; 
    cursor:pointer; 
    height:100%;
    position: relative;
}
.item-card:hover { 
    box-shadow: 0 20px 48px rgba(0,0,0,.14);
    transform: translateY(-10px);
    border-color: rgba(79,157,28,.3);
}
.item-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 4px;
    background: linear-gradient(90deg, #4f9d1c 0%, #c8f060 100%);
    opacity: 0;
    transition: opacity .3s;
    z-index: 1;
}
.item-card:hover::before {
    opacity: 1;
}
/* New badge */
.item-new-badge {
    position: absolute;
    top: 12px; left: 12px;
    background: linear-gradient(135deg, #4f9d1c, #c8f060);
    color: #0a1f00;
    font-size: 10px;
    font-weight: 800;
    padding: 3px 10px;
    border-radius: 20px;
    z-index: 2;
    letter-spacing: .5px;
    text-transform: uppercase;
    box-shadow: 0 2px 8px rgba(79,157,28,.4);
}
.item-card-img { 
    width:100%; 
    height:210px; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    display:flex; 
    align-items:center; 
    justify-content:center; 
    font-size:56px; 
    color:var(--text-muted); 
    overflow:hidden; 
    position:relative; 
    flex-shrink:0;
}
.item-card-img img { 
    width:100%; 
    height:100%; 
    object-fit:cover; 
    display:block; 
    transition: transform .4s cubic-bezier(.4,0,.2,1); 
}
.item-card:hover .item-card-img img { 
    transform: scale(1.08);
}
.item-card-img .view-hint { 
    position:absolute; 
    inset:0; 
    background: rgba(0,0,0,.45);
    display:flex; 
    align-items:center; 
    justify-content:center; 
    opacity:0; 
    transition: opacity .3s;
    color:#fff; 
    font-size:14px; 
    font-weight:700; 
    gap:6px;
}
.item-card:hover .view-hint { 
    opacity:1;
}
.item-card-img .date-badge { 
    position:absolute; 
    top:12px; 
    right:12px; 
    background: linear-gradient(135deg, rgba(0,0,0,.6) 0%, rgba(0,0,0,.8) 100%);
    color:#fff; 
    font-size:11px; 
    font-weight:800; 
    padding: 5px 12px; 
    border-radius:24px; 
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255,255,255,.2);
}
.item-card-body { 
    padding: 18px 18px 12px; 
    flex:1; 
    display:flex; 
    flex-direction:column; 
    gap: 7px;
}
.item-card-title { 
    font-size:16px; 
    font-weight:750; 
    color:var(--text); 
    line-height:1.3; 
    margin-bottom:4px;
}
.item-card-meta { 
    font-size:13px; 
    color:var(--text-secondary); 
    display:flex; 
    align-items:center; 
    gap:6px; 
    white-space:nowrap; 
    overflow:hidden; 
    text-overflow:ellipsis;
}
.item-card-footer { 
    padding: 12px 18px 14px; 
    border-top:1px solid var(--border); 
    display:flex; 
    align-items:center; 
    justify-content:space-between; 
    gap:8px; 
    flex-shrink:0;
    background: linear-gradient(135deg, transparent 0%, rgba(79,157,28,.03) 100%);
}
.item-card-ago {
    font-size: 11px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 4px;
}
.type-tag { 
    font-size:12px; 
    font-weight:750; 
    padding: 5px 14px; 
    border-radius:24px;
}
.type-lost  { 
    background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
    color:#991b1b;
}
.type-found { 
    background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
    color:#065f46;
}

/* ===== SHARE DROPDOWN ===== */
.share-button-wrapper {
    position: relative;
    margin-left: auto;
}

.btn-share-trigger {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all .2s ease;
    white-space: nowrap;
}

.btn-share-trigger:hover {
    background: var(--border);
    color: var(--text);
    border-color: var(--primary);
}

.share-dropdown {
    display: none;
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 8px 28px rgba(0,0,0,.14);
    min-width: 180px;
    z-index: 99;
    overflow: hidden;
    animation: dropUp .18s cubic-bezier(.4,0,.2,1);
}

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

.share-dropdown.open {
    display: block;
}

.share-dropdown-label {
    font-size: 10px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    padding: 10px 14px 6px;
}

.share-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    transition: background .15s;
    text-decoration: none;
}

.share-option:hover {
    background: var(--bg-secondary);
}

.share-option-icon {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}

.share-option-icon.fb   { background: #e7f0fd; color: #1877f2; }
.share-option-icon.copy { background: #f0fdf4; color: #16a34a; }

.share-option .copied-text { display: none; color: #16a34a; }

body.dark-mode .share-dropdown {
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 8px 28px rgba(0,0,0,.4);
}

body.dark-mode .share-option {
    color: var(--text);
}

body.dark-mode .share-option:hover {
    background: #263348;
}

body.dark-mode .share-option-icon.copy { background: #0d2d1a; color: #4ade80; }
body.dark-mode .share-option-icon.fb   { background: #0c2340; color: #60a5fa; }

/* SECTION HEADING */
.section-heading { 
    font-size:18px; 
    font-weight:800; 
    margin: 36px 0 20px; 
    display:flex; 
    align-items:center; 
    gap:12px; 
    color:var(--text); 
    padding-bottom:14px; 
    border-bottom: 2px solid var(--primary);
}
/* URGENCY STRIP */
.urgency-strip {
    background: linear-gradient(135deg, #fff7ed 0%, #ffedd5 100%);
    border: 1px solid #fed7aa;
    border-radius: 14px;
    padding: 14px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 24px;
    font-size: 13px;
    color: #7c2d12;
    font-weight: 600;
}
.urgency-strip .urgency-icon { font-size: 20px; flex-shrink:0; }
body.dark-mode .urgency-strip {
    background: linear-gradient(135deg, #2d1a00 0%, #3d2200 100%);
    border-color: #7c2d12;
    color: #fdba74;
}
.empty-state { 
    text-align:center; 
    padding: 72px 28px; 
    color:var(--text-muted); 
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg) 100%);
    border-radius: 18px; 
    border: 2px dashed var(--border);
}
.empty-state .icon { 
    font-size:64px; 
    margin-bottom:20px; 
    opacity:.5;
}
.empty-state p { 
    font-size:15px;
    font-weight: 500;
}

/* COUNTER ANIMATION */
@keyframes countUp {
    from { opacity:0; transform:translateY(8px); }
    to   { opacity:1; transform:translateY(0); }
}
.browse-stat .num { animation: countUp .6s ease both; }

/* CTA */
.cta-bar { 
    background: linear-gradient(135deg, #1a2e0a 0%, #4a7c00 50%, #2d5a00 100%);
    color:#fff; 
    border-radius: 18px; 
    padding: 28px 32px; 
    display:flex; 
    align-items:center; 
    justify-content:space-between; 
    gap:20px; 
    flex-wrap:wrap; 
    margin-bottom:36px; 
    box-shadow: 0 8px 28px rgba(0,0,0,.15), 0 0 1px rgba(200,240,96,.3) inset;
    border: 1px solid rgba(200,240,96,.2);
}
.cta-bar strong { 
    font-size:17px; 
    display:block; 
    margin-bottom:4px;
    font-weight: 800;
}
.cta-bar p { 
    font-size:14px; 
    opacity:.9; 
    margin:0;
    font-weight: 500;
}
.cta-bar-actions { display:flex; gap:10px; flex-wrap:wrap; align-items:center; }

/* ===== DETAIL MODAL ===== */
.detail-overlay { 
    display:none; 
    position:fixed; 
    inset:0; 
    background: rgba(0,0,0,.75);
    z-index:500; 
    align-items:center; 
    justify-content:center; 
    padding:16px;
    backdrop-filter: blur(4px);
}
.detail-overlay.open { 
    display:flex;
}
.detail-modal { 
    background:#fff; 
    border-radius: 20px; 
    width:100%; 
    max-width:760px; 
    max-height:92vh; 
    overflow-y:auto; 
    position:relative; 
    box-shadow: 0 25px 50px rgba(0,0,0,.35);
}
.detail-modal-close { 
    position:absolute; 
    top:16px; 
    right:16px; 
    background: rgba(0,0,0,.6);
    color:#fff; 
    border:none; 
    border-radius:50%; 
    width:36px; 
    height:36px; 
    font-size:18px; 
    cursor:pointer; 
    display:flex; 
    align-items:center; 
    justify-content:center; 
    z-index:10; 
    transition: all .3s;
    backdrop-filter: blur(8px);
}
.detail-modal-close:hover { 
    background: rgba(0,0,0,.85);
    transform: scale(1.1);
}
.detail-img-wrap { 
    width:100%; 
    height:500px; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    overflow:hidden; 
    border-radius: 20px 20px 0 0;
    display:flex; 
    align-items:center; 
    justify-content:center; 
    font-size:72px; 
    color:var(--text-muted); 
    position:relative; 
    cursor:zoom-in;
}

.detail-type-badge { 
    position:absolute; 
    top:16px; 
    left:16px; 
    font-size:13px; 
    font-weight:800; 
    padding: 6px 16px; 
    border-radius:24px; 
    z-index:2; 
    pointer-events:none;
    backdrop-filter: blur(8px);
}
.detail-img-wrap .zoom-tap { 
    position:absolute; 
    bottom:14px; 
    right:14px; 
    background: rgba(0,0,0,.6);
    color:#fff; 
    font-size:12px; 
    font-weight:700; 
    padding: 6px 12px; 
    border-radius:24px; 
    pointer-events:none; 
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255,255,255,.2);
}
.detail-img-wrap img { 
    width:100%; 
    height:500px; 
    object-fit:contain; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    cursor:zoom-in; 
    display:block;
}
.detail-title { 
    font-size:22px; 
    font-weight:850; 
    color:var(--text); 
    margin-bottom:8px;
}
.detail-status { 
    margin-bottom:20px;
}
.detail-grid { 
    display:grid; 
    grid-template-columns:1fr 1fr; 
    gap: 16px 24px; 
    margin-bottom:28px;
}
.detail-field label { 
    font-size:12px; 
    font-weight:800; 
    text-transform:uppercase; 
    letter-spacing: .8px;
    color:var(--text-muted); 
    display:block; 
    margin-bottom:4px;
}
.detail-field span { 
    font-size:15px; 
    color:var(--text); 
    font-weight:600;
}
.detail-desc { 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-radius: 14px; 
    padding: 18px 20px; 
    font-size:15px; 
    color:var(--text); 
    line-height:1.8; 
    margin-bottom:28px;
    border: 1px solid var(--border);
}
.detail-desc label { 
    font-size:12px; 
    font-weight:800; 
    text-transform:uppercase; 
    letter-spacing: .8px;
    color:var(--text-muted); 
    display:block; 
    margin-bottom:10px;
}

/* SIDE BY SIDE PHOTOS */
.photos-row { 
    display:grid; 
    grid-template-columns:1fr 1fr; 
    gap: 16px; 
    margin-bottom:28px;
}
.photo-box { 
    border-radius:14px; 
    overflow:hidden; 
    border: 1px solid var(--border);
    cursor:zoom-in; 
    position:relative; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    box-shadow: 0 2px 8px rgba(0,0,0,.04);
    transition: all .3s;
}
.photo-box img { 
    width:100%; 
    height:240px; 
    object-fit:contain; 
    display:block; 
    transition: transform .3s;
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
}
.photo-box:hover { 
    box-shadow: 0 8px 20px rgba(0,0,0,.1);
    transform: translateY(-3px);
}
.photo-box:hover img { 
    transform: scale(1.05);
}
.photo-box .photo-label { 
    position:absolute; 
    bottom:0; 
    left:0; 
    right:0; 
    background: linear-gradient(180deg, transparent 0%, rgba(0,0,0,.7) 100%);
    color:#fff; 
    font-size:12px; 
    font-weight:750; 
    padding: 8px 12px;
    text-align:center;
}
.photo-box .zoom-hint { 
    position:absolute; 
    top:8px; 
    right:8px; 
    background: rgba(0,0,0,.6);
    color:#fff; 
    font-size:11px; 
    padding: 4px 10px; 
    border-radius:12px; 
    opacity:0; 
    transition: opacity .3s;
    backdrop-filter: blur(6px);
}
.photo-box:hover .zoom-hint { 
    opacity:1;
}
.photo-box-empty { 
    border-radius:14px; 
    border:2px dashed var(--border); 
    height:240px; 
    display:flex; 
    flex-direction:column; 
    align-items:center; 
    justify-content:center; 
    color:var(--text-muted); 
    font-size:13px; 
    gap:8px; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
}
.photo-box-empty .icon { 
    font-size:40px;
}

/* FINDER SECTION */
.finder-section { 
    border-top:1px solid var(--border); 
    padding-top: 24px; 
    margin-top: 12px;
}
.finder-section h4 { 
    font-size:14px; 
    font-weight:800; 
    color:var(--text); 
    margin-bottom:14px; 
    display:flex; 
    align-items:center; 
    gap:8px;
    text-transform: uppercase;
    letter-spacing: .5px;
}
.finder-profile { 
    display:flex; 
    align-items:center; 
    gap:18px; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-radius: 14px; 
    padding: 16px;
    border: 1px solid var(--border);
}
.finder-photo { 
    width:72px; 
    height:72px; 
    border-radius:50%; 
    object-fit:cover; 
    border: 3px solid var(--primary); 
    flex-shrink:0;
    cursor: zoom-in;
    box-shadow: 0 4px 12px rgba(79,157,28,.25);
}
.finder-avatar { 
    width:72px; 
    height:72px; 
    border-radius:50%; 
    background: linear-gradient(135deg,#6aaa00,#a8d040); 
    display:flex; 
    align-items:center; 
    justify-content:center; 
    font-size:28px; 
    font-weight:850; 
    color:#1a2e0a; 
    flex-shrink:0;
    box-shadow: 0 4px 12px rgba(79,157,28,.25);
}
.finder-info .name { 
    font-size:16px; 
    font-weight:800; 
    color:var(--text);
}
.finder-info .role-tag { 
    font-size:12px; 
    color:var(--text-secondary); 
    margin-top:4px;
    font-weight: 500;
}

/* REPORTER SECTION (lost) */
.reporter-section { 
    border-top:1px solid var(--border); 
    padding-top: 24px; 
    margin-top: 12px;
}
.reporter-section h4 { 
    font-size:14px; 
    font-weight:800; 
    color:var(--text); 
    margin-bottom:14px; 
    display:flex; 
    align-items:center; 
    gap:8px;
    text-transform: uppercase;
    letter-spacing: .5px;
}
.reporter-profile { 
    display:flex; 
    align-items:center; 
    gap:18px; 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-radius: 14px; 
    padding: 16px;
    border: 1px solid var(--border);
}
.reporter-avatar { 
    width:64px; 
    height:64px; 
    border-radius:50%; 
    background: linear-gradient(135deg,#3b82f6,#6366f1); 
    display:flex; 
    align-items:center; 
    justify-content:center; 
    font-size:24px; 
    font-weight:850; 
    color:#fff; 
    flex-shrink:0;
    box-shadow: 0 4px 12px rgba(59,130,246,.25);
}
.reporter-info .name { 
    font-size:16px; 
    font-weight:800; 
    color:var(--text);
}
.reporter-info .sub { 
    font-size:12px; 
    color:var(--text-secondary); 
    margin-top:4px;
    font-weight: 500;
}
.finder-photo:hover, .reporter-profile img:hover { 
    opacity:.85; 
    transition: opacity .2s;
}

/* ZOOM LIGHTBOX */
.zoom-overlay { 
    display:none; 
    position:fixed; 
    inset:0; 
    background: rgba(0,0,0,.98);
    z-index:900; 
    align-items:center; 
    justify-content:center; 
    cursor:zoom-out;
    backdrop-filter: blur(12px);
    padding: 0;
    overflow: hidden;
}
.zoom-overlay.open { 
    display:flex;
}
.zoom-overlay img { 
    width: 90vw;
    height: 90vh;
    object-fit: contain; 
    border-radius: 20px; 
    box-shadow: 0 40px 80px rgba(0,0,0,.95);
    animation: zoomIn .3s cubic-bezier(.4,0,.2,1);
    max-width: none;
    max-height: none;
}
@keyframes zoomIn {
    from { transform: scale(.92); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}
.zoom-close { 
    position:absolute; 
    top:24px; 
    right:28px; 
    color:#fff; 
    font-size:36px; 
    cursor:pointer; 
    background: rgba(0,0,0,.6);
    border: 2px solid rgba(255,255,255,.3); 
    line-height:1; 
    opacity:.9;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .3s;
    backdrop-filter: blur(12px);
}
.zoom-close:hover { 
    opacity: 1;
    background: rgba(0,0,0,.8);
    transform: scale(1.15);
    border-color: rgba(255,255,255,.6);
}
.zoom-label { 
    position:absolute; 
    bottom:24px; 
    left:50%; 
    transform: translateX(-50%); 
    color:rgba(255,255,255,.8); 
    font-size:13px; 
    font-weight:600; 
    background: rgba(0,0,0,.6);
    padding: 6px 18px; 
    border-radius:24px;
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255,255,255,.2);
}

@media(max-width:768px) {
    .items-grid { grid-template-columns:repeat(auto-fill,minmax(200px,1fr)); }
    .browse-hero { padding: 56px 20px 48px; }
    .browse-hero-logo { gap: 14px; }
    .browse-hero-logo img { width: 70px; height: 70px; }
    .browse-hero-logo-text h1 { font-size: 32px; }
    .pub-body { padding: 32px 16px; }
    .detail-grid { grid-template-columns:1fr; }
    .detail-img-wrap { height: 320px; }
    .detail-img-wrap img { height: 320px; }
    .photos-row { grid-template-columns:1fr; }
    .photo-box img, .photo-box-empty { height: 200px; }
}

@media(max-width:600px) {
    .items-grid { grid-template-columns: 1fr; }
    .browse-hero-logo { flex-direction: column; gap: 10px; }
    .browse-hero-logo-text { text-align: center; }
    .browse-hero-logo-text h1 { font-size: 28px; }
    .pub-nav { padding: 12px 16px; flex-direction: column; gap: 12px; }
    .detail-grid { grid-template-columns:1fr; }
    .detail-img-wrap, .detail-img-wrap img { height: 260px; }
    .photos-row { grid-template-columns:1fr; }
    .photo-box img, .photo-box-empty { height: 160px; }
    .filter-bar { flex-direction: column; }
    .filter-bar .form-control { width: 100%; }
    .cta-bar { flex-direction: column; text-align: center; }
    .browse-stats { flex-direction: column; gap: 10px; width: 100%; }
    .browse-stat { min-width: 100%; }

    /* Mobile responsive for share button */
    .item-card-footer {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        padding: 12px 14px;
    }

    .share-button-wrapper {
        width: 100%;
        margin: 0;
        justify-content: space-between;
    }

    .btn-share-facebook {
        padding: 7px 12px;
        font-size: 11px;
    }

    .type-tag {
        order: 1;
    }

    .share-button-wrapper {
        order: 3;
    }

    .item-card-ago {
        order: 2;
        width: 100%;
    }
}

@media(max-width:380px) {
    .items-grid { grid-template-columns: 1fr; }
    .photos-row { grid-template-columns: 1fr; }
    .filter-bar { flex-direction: column; }
    .filter-bar .form-control { width: 100%; }
    .browse-hero-logo img { width: 60px; height: 60px; }
    .browse-hero-logo-text h1 { font-size: 24px; }
    .tab-btn { padding: 8px 16px; font-size: 13px; }
}

/* ===== DARK MODE OVERRIDES ===== */
body.dark-mode .filter-bar { 
    background: linear-gradient(135deg, var(--white) 0%, #0f172a 100%);
    border-color:var(--border);
}
body.dark-mode .filter-bar select.form-control {
    background-color: #0f172a;
    border-color: #334155;
    color: #f1f5f9;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}
body.dark-mode .tabs { background: var(--bg-secondary); border-color:var(--border); }
body.dark-mode .tab-btn { color:var(--text-muted); }
body.dark-mode .tab-btn.active { background: linear-gradient(135deg, #6aaa00 0%, #a8d040 100%); color:#1a2e0a; box-shadow: 0 4px 12px rgba(106,170,0,.4); }
body.dark-mode .item-card { background:var(--white); border-color:var(--border); }
body.dark-mode .item-card-title { color:var(--text); }
body.dark-mode .item-card-meta { color:var(--text-secondary); }
body.dark-mode .item-card-footer { background: #263348; border-top-color:var(--border); }
body.dark-mode .item-card-img { background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg) 100%); border-bottom-color:var(--border); }
body.dark-mode .section-heading { color:var(--text); border-bottom-color: var(--primary); }
body.dark-mode .detail-modal { background:var(--white); }
body.dark-mode .detail-title { color:var(--text); }
body.dark-mode .detail-field span { color:var(--text); }
body.dark-mode .detail-field label { color:var(--text-muted); }
body.dark-mode .detail-desc { 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    color:var(--text);
    border-color: var(--border);
}
body.dark-mode .detail-desc label { color:var(--text-muted); }
body.dark-mode .detail-grid { color:var(--text); }
body.dark-mode .finder-profile,
body.dark-mode .reporter-profile { 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-color: var(--border);
}
body.dark-mode .finder-info .name,
body.dark-mode .reporter-info .name { color:var(--text); }
body.dark-mode .finder-info .role-tag,
body.dark-mode .reporter-info .sub { color:var(--text-secondary); }
body.dark-mode .finder-section,
body.dark-mode .reporter-section { border-top-color:var(--border); }
body.dark-mode .finder-section h4,
body.dark-mode .reporter-section h4 { color:var(--text); }
body.dark-mode .photo-box { 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-color: var(--border);
}
body.dark-mode .photo-box img { background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%); }
body.dark-mode .photo-box-empty { 
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
    border-color:var(--border); 
    color:var(--text-muted);
}
body.dark-mode .empty-state { 
    color:var(--text-muted);
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg) 100%);
    border-color: var(--border);
}
body.dark-mode .badge-pending  { background:#78350f; color:#fcd34d; }
body.dark-mode .badge-matched  { background:#1e3a5f; color:#93c5fd; }
body.dark-mode .badge-returned { background:#064e3b; color:#6ee7b7; }
body.dark-mode .badge-closed   { background:#1f2937; color:#9ca3af; }
body.dark-mode .type-lost      { background:#7f1d1d; color:#fca5a5; }
body.dark-mode .type-found     { background:#064e3b; color:#6ee7b7; }

/* Detail body container */
.detail-body {
    padding: 28px;
}

/* Button styling enhancements */
.btn-block {
    width: 100%;
}

.btn-primary,
.btn {
    font-weight: 700;
    letter-spacing: .3px;
    transition: all .3s cubic-bezier(.4,0,.2,1);
}
</style>
</head>
<body style="background:var(--bg)">

<?php if(isLoggedIn()): ?>
<!-- LOGGED IN: full layout with sidebar -->
<div class="layout">
    <?php if(isAdmin()): ?>
        <?php include '../includes/admin_sidebar.php'; ?>
    <?php else: ?>
        <?php include '../includes/user_sidebar.php'; ?>
    <?php endif; ?>
    <div class="main-content">
        <?php include '../includes/topbar.php'; ?>
        <div class="page-content" style="padding:0">

<!-- HERO -->
<div class="browse-hero" style="margin:0">
    <div class="browse-hero-inner">
        <div class="browse-hero-logo">
            <img src="/lostfound_relink/cyn_logo.png" alt="Cauayan City Hall">
            <div class="browse-hero-logo-text">
                <h1>🔗 <span>RELINK</span></h1>
                <span class="sub">Lost &amp; Found Management System</span>
            </div>
        </div>
        <p class="browse-hero-tagline">📍 Cauayan City Hall &mdash; Public Lost &amp; Found Board</p>
        <div class="browse-stats">
            <div class="browse-stat"><div class="num"><?= $total_lost ?></div><div class="lbl">🔍 Lost Items</div></div>
            <div class="browse-stat"><div class="num"><?= $total_found ?></div><div class="lbl">📦 Found Items</div></div>
        </div>
    </div>
</div>

<div class="pub-body">
<?php else: ?>
<!-- GUEST: public nav -->
<nav class="pub-nav">
    <div class="logo">
        <img src="/lostfound_relink/cyn_logo.png" alt="Logo">
        <div>
            <span class="logo-text">🔗 RELINK</span>
            <span class="logo-sub">Cauayan City Hall</span>
        </div>
    </div>
    <div class="nav-actions">
        <a href="/lostfound_relink/auth/login.php" class="btn btn-sm btn-outline" style="color:#c8f060;border-color:rgba(200,240,96,.4)">Sign In</a>
        <a href="/lostfound_relink/auth/register.php" class="btn btn-sm btn-primary">Register</a>
    </div>
</nav>


<!-- HERO -->
<div class="browse-hero">
    <div class="browse-hero-inner">
        <div class="browse-hero-logo">
            <img src="/lostfound_relink/cyn_logo.png" alt="Cauayan City Hall">
            <div class="browse-hero-logo-text">
                <h1>🔗 <span>RELINK</span></h1>
                <span class="sub">Lost &amp; Found Management System</span>
            </div>
        </div>
        <p class="browse-hero-tagline">📍 Cauayan City Hall &mdash; Public Lost &amp; Found Board</p>
        <div class="browse-stats">
            <div class="browse-stat"><div class="num"><?= $total_lost ?></div><div class="lbl">🔍 Lost Items</div></div>
            <div class="browse-stat"><div class="num"><?= $total_found ?></div><div class="lbl">📦 Found Items</div></div>
        </div>
    </div>
</div>

<div class="pub-body">
<?php endif; ?>

    <!-- FILTER -->
    <form method="GET" class="filter-bar">
        <div class="filter-search-wrap">
            <span class="search-icon">🔎</span>
            <input type="text" name="q" class="form-control" placeholder="Search by name, description, location..." value="<?= htmlspecialchars($q) ?>">
        </div>
        <select name="category" class="form-control" style="max-width:160px">
            <option value="">All Categories</option>
            <?php while($c=$categories->fetch_assoc()): ?>
            <option value="<?= htmlspecialchars($c['name']) ?>" <?= $cat===$c['name']?'selected':'' ?>><?= htmlspecialchars($c['name']) ?></option>
            <?php endwhile; ?>
        </select>
        <input type="hidden" name="type" value="<?= htmlspecialchars($type) ?>">
        <button type="submit" class="btn btn-primary">Search</button>
        <a href="browse.php?type=<?= $type ?>" class="btn btn-light">✕ Clear</a>
    </form>

    <!-- URGENCY STRIP -->
    <?php if(!isLoggedIn()): ?>
    <div class="urgency-strip">
        <span class="urgency-icon">⚡</span>
        <span>Someone may be looking for their lost item right now — <strong>register for free</strong> to report and get notified instantly via SMS when a match is found.</span>
        <a href="/lostfound_relink/auth/register.php" class="btn btn-sm btn-primary" style="margin-left:auto;white-space:nowrap">Join Free →</a>
    </div>
    <?php endif; ?>

    <!-- TABS -->
    <div class="tabs">
        <a href="?q=<?= urlencode($q) ?>&category=<?= urlencode($cat) ?>&type=all"   class="tab-btn <?= $type==='all'  ?'active':'' ?>">🗂 All Items</a>
        <a href="?q=<?= urlencode($q) ?>&category=<?= urlencode($cat) ?>&type=lost"  class="tab-btn <?= $type==='lost' ?'active':'' ?>">🔍 Lost Items</a>
        <a href="?q=<?= urlencode($q) ?>&category=<?= urlencode($cat) ?>&type=found" class="tab-btn <?= $type==='found'?'active':'' ?>">📦 Found Items</a>
    </div>

    <!-- LOST ITEMS -->
    <?php if($lost_data !== null && ($type==='all'||$type==='lost')): ?>
    <div class="section-heading">🔍 Lost Items <span class="badge badge-pending"><?= count($lost_data) ?></span></div>
    <?php if(empty($lost_data)): ?>
    <div class="empty-state"><div class="icon">🔍</div><p>No lost items found.</p></div>
    <?php else: ?>
    <div class="items-grid">
        <?php foreach($lost_data as $item):
            $isNew = (strtotime($item['created_at']) > strtotime('-3 days'));
        ?>
        <div class="item-card" onclick="openDetail('lost','<?= $item['id'] ?>')">
            <div class="item-card-img">
                <?php if($item['image_path']): ?>
                <img src="/lostfound_relink/<?= htmlspecialchars($item['image_path']) ?>" alt="">
                <?php else: ?>🔍<?php endif; ?>
                <div class="view-hint">👁 View Details</div>
                <?php if($isNew): ?><div class="item-new-badge">🆕 New</div><?php endif; ?>
                <?php if($item['date_lost']): ?>
                <div class="date-badge"><?= date('M d', strtotime($item['date_lost'])) ?></div>
                <?php endif; ?>
            </div>
            <div class="item-card-body">
                <div class="item-card-title"><?= htmlspecialchars($item['item_name']) ?></div>
                <div class="item-card-meta"><span>📂</span><?= htmlspecialchars($item['category']) ?></div>
                <div class="item-card-meta"><span>📍</span><?= htmlspecialchars($item['location']) ?></div>
                <?php if(!empty($item['contact'])): ?>
                <div class="item-card-meta" style="color:var(--primary);font-weight:600"><span>📞</span><?= htmlspecialchars($item['contact']) ?></div>
                <?php endif; ?>
            </div>
            <div class="item-card-footer">
                <span class="type-tag type-lost">Lost</span>
                <span class="item-card-ago">🕐 <?= timeAgo($item['created_at']) ?></span>
                <div class="share-button-wrapper">
                    <button class="btn-share-trigger" onclick="toggleShare(event, 'lost', <?= $item['id'] ?>, '<?= htmlspecialchars($item['item_name'], ENT_QUOTES) ?>')">
                        <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>
                        Share
                    </button>
                    <div class="share-dropdown" id="share-lost-<?= $item['id'] ?>">
                        <div class="share-dropdown-label">Share this item</div>
                        <button class="share-option" onclick="copyItemLink('lost', <?= $item['id'] ?>, this)">
                            <span class="share-option-icon copy">🔗</span>
                            <span class="default-text">Copy Link</span>
                            <span class="copied-text">✅ Copied!</span>
                        </button>
                        <button class="share-option" onclick="shareToFacebook('lost', <?= $item['id'] ?>, '<?= htmlspecialchars($item['item_name'], ENT_QUOTES) ?>')">  
                            <span class="share-option-icon fb"><i class="fab fa-facebook-f"></i></span>
                            Share to Facebook
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>

    <!-- FOUND ITEMS -->
    <?php if($found_data !== null && ($type==='all'||$type==='found')): ?>
    <div class="section-heading" style="margin-top:32px">📦 Found Items <span class="badge badge-returned"><?= count($found_data) ?></span></div>
    <?php if(empty($found_data)): ?>
    <div class="empty-state"><div class="icon">📦</div><p>No found items found.</p></div>
    <?php else: ?>
    <div class="items-grid">
        <?php foreach($found_data as $item):
            $isNew = (strtotime($item['created_at']) > strtotime('-3 days'));
        ?>
        <div class="item-card" onclick="openDetail('found','<?= $item['id'] ?>')">
            <div class="item-card-img">
                <?php if($item['image_path']): ?>
                <img src="/lostfound_relink/<?= htmlspecialchars($item['image_path']) ?>" alt="">
                <?php else: ?>📦<?php endif; ?>
                <div class="view-hint">👁 View Details</div>
                <?php if($isNew): ?><div class="item-new-badge">🆕 New</div><?php endif; ?>
                <?php if($item['date_found']): ?>
                <div class="date-badge"><?= date('M d', strtotime($item['date_found'])) ?></div>
                <?php endif; ?>
            </div>
            <div class="item-card-body">
                <div class="item-card-title"><?= htmlspecialchars($item['item_name']) ?></div>
                <div class="item-card-meta"><span>📂</span><?= htmlspecialchars($item['category']) ?></div>
                <div class="item-card-meta"><span>📍</span><?= htmlspecialchars($item['location']) ?></div>
                <?php if(!empty($item['reporter_phone'])): ?>
                <div class="item-card-meta" style="color:var(--primary);font-weight:600"><span>📞</span><?= htmlspecialchars($item['reporter_phone']) ?></div>
                <?php endif; ?>
            </div>
            <div class="item-card-footer">
                <span class="type-tag type-found">Found</span>
                <span class="item-card-ago">🕐 <?= timeAgo($item['created_at']) ?></span>
                <div class="share-button-wrapper">
                    <button class="btn-share-trigger" onclick="toggleShare(event, 'found', <?= $item['id'] ?>, '<?= htmlspecialchars($item['item_name'], ENT_QUOTES) ?>')">
                        <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" y1="13.51" x2="15.42" y2="17.49"/><line x1="15.41" y1="6.51" x2="8.59" y2="10.49"/></svg>
                        Share
                    </button>
                    <div class="share-dropdown" id="share-found-<?= $item['id'] ?>">
                        <div class="share-dropdown-label">Share this item</div>
                        <button class="share-option" onclick="copyItemLink('found', <?= $item['id'] ?>, this)">
                            <span class="share-option-icon copy">🔗</span>
                            <span class="default-text">Copy Link</span>
                            <span class="copied-text">✅ Copied!</span>
                        </button>
                        <button class="share-option" onclick="shareToFacebook('found', <?= $item['id'] ?>, '<?= htmlspecialchars($item['item_name'], ENT_QUOTES) ?>')">
                            <span class="share-option-icon fb"><i class="fab fa-facebook-f"></i></span>
                            Share to Facebook
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>

</div><!-- /pub-body -->

<footer style="background: linear-gradient(135deg, #1a2e0a 0%, #0a1f00 100%); color:rgba(200,240,96,.6); text-align:center; padding:32px 20px; font-size:13px; margin-top:60px; border-top: 1px solid rgba(200,240,96,.15);">
    <div style="display:flex; align-items:center; justify-content:center; gap:10px; margin-bottom:8px; max-width:1400px; margin-left:auto; margin-right:auto;">
        <img src="/lostfound_relink/cyn_logo.png" style="width:24px; height:24px; object-fit:contain; opacity:.7;">
        <span style="color:rgba(255,255,255,.6); font-weight:700;">🔗 RELINK Lost & Found System</span>
    </div>
    <div style="display:flex; align-items:center; justify-content:center; gap:24px; flex-wrap:wrap; margin:12px 0; max-width:1400px; margin-left:auto; margin-right:auto;">
        <span style="font-size:12px; color:rgba(200,240,96,.5);">🏛️ Cauayan City Hall</span>
        <span style="font-size:12px; color:rgba(200,240,96,.5);">🔒 Secure System</span>
        <span style="font-size:12px; color:rgba(200,240,96,.5);">📲 SMS Notifications</span>
        <span style="font-size:12px; color:rgba(200,240,96,.5);">🤖 Smart Matching</span>
    </div>
    <p style="margin:8px 0 0; font-weight: 500; opacity: .8;">&copy; <?= date('Y') ?> Cauayan City Hall. Helping reunite lost items with their owners.</p>
</footer>

<?php if(isLoggedIn()): ?>
        </div><!-- /page-content -->
    </div><!-- /main-content -->
</div><!-- /layout -->
<?php endif; ?>

<!-- DETAIL MODAL -->
<div class="detail-overlay" id="detailOverlay" onclick="closeDetailOnBg(event)">
    <div class="detail-modal" id="detailModal">
        <button class="detail-modal-close" onclick="closeDetail()">✕</button>
        <div id="detailContent"></div>
    </div>
</div>

<!-- ZOOM LIGHTBOX -->
<div class="zoom-overlay" id="zoomOverlay" onclick="closeZoom()">
    <button class="zoom-close" onclick="closeZoom()">✕</button>
    <img id="zoomImg" src="" alt="">
    <div class="zoom-label" id="zoomLabel"></div>
</div>

<!-- EMBED ALL ITEM DATA AS JSON -->
<script>
const lostData  = <?= json_encode($lost_data,  JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT) ?>;
const foundData = <?= json_encode($found_data, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT) ?>;

function openDetail(type, id) {
    const items = type === 'lost' ? lostData : foundData;
    const item  = items.find(i => String(i.id) === String(id));
    if (!item) return;

    const isLost    = type === 'lost';
    const dateKey   = isLost ? item.date_lost : item.date_found;
    const dateLabel = isLost ? 'Date Lost' : 'Date Found';
    const dateVal   = dateKey ? new Date(dateKey).toLocaleDateString('en-US',{year:'numeric',month:'long',day:'numeric'}) : '—';
    const imgSrc    = item.image_path ? '/lostfound_relink/' + item.image_path : null;
    const typeClass = isLost ? 'type-lost' : 'type-found';
    const typeLabel = isLost ? 'Lost' : 'Found';
    const badgeClass= 'badge-' + item.status;

    // Header image - full width for both lost and found
    const headerEmoji = isLost ? "🔍" : "📦";
    const headerImg = `
        <div class="detail-img-wrap" onclick="${imgSrc ? `zoomImg('${imgSrc}','Item Photo')` : ``}" title="${imgSrc ? `Click to zoom` : ``}">
            ${imgSrc ? `<img src="${imgSrc}" alt="">` : `<span style="font-size:64px">${headerEmoji}</span>`}
            <span class="detail-type-badge ${typeClass}">${typeLabel}</span>
            ${imgSrc ? `<span class="zoom-tap">🔍 Tap to zoom</span>` : ``}
        </div>`;

    // Finder photo (found items only, shown below header)
    let photosRow = "";
    if (!isLost) {
        const finderSrc = item.finder_photo ? "/lostfound_relink/" + item.finder_photo : null;
        if (finderSrc) {
            photosRow = `
            <div class="detail-body" style="padding-bottom:0;padding-top:14px">
                <div style="font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.5px;color:var(--text-muted);margin-bottom:8px">🤳 Finder Photo â€” Click to zoom</div>
                <div class="photo-box" onclick="zoomImg('${finderSrc}','Finder Photo')" title="Click to zoom">
                    <img src="${finderSrc}" alt="Finder photo">
                    <div class="photo-label">🤳 Finder Photo</div>
                    <div class="zoom-hint">🔍 Zoom</div>
                </div>
            </div>`;
        }
    }

    const descHtml = item.description
        ? `<div class="detail-desc"><label>Description</label>${escHtml(item.description)}</div>`
        : '';

    const contactNum = isLost ? (item.contact || item.reporter_phone || '') : (item.reporter_phone || '');
    const contactHtml = contactNum
        ? `<div class="detail-field" style="grid-column:span 2"><label>Contact Number</label><span style="font-size:15px;font-weight:800;color:var(--primary)">📞 ${escHtml(contactNum)}</span></div>`
        : '';

    // Finder info (found items)
    let finderHtml = '';
    if (!isLost) {
        const initial = item.reporter ? item.reporter.charAt(0).toUpperCase() : '?';
        const finderSrc = item.finder_photo ? '/lostfound_relink/' + item.finder_photo : null;
        const reporterAvatarSrc = item.reporter_avatar ? '/lostfound_relink/' + item.reporter_avatar : null;
        // Priority: finder_photo > reporter avatar > initial letter
        const photoEl = finderSrc
            ? `<img src="${finderSrc}" class="finder-photo" alt="Finder" style="cursor:zoom-in" onclick="zoomImg('${finderSrc}','Finder Photo')">`
            : reporterAvatarSrc
                ? `<img src="${reporterAvatarSrc}" class="finder-photo" alt="Profile" style="cursor:zoom-in" onclick="zoomImg('${reporterAvatarSrc}','${escHtml(item.reporter)}')">`
                : `<div class="finder-avatar">${initial}</div>`;
        finderHtml = `
        <div class="finder-section">
            <h4>🤳 Person Who Found This Item</h4>
            <div class="finder-profile">
                ${photoEl}
                <div class="finder-info">
                    <div class="name">${escHtml(item.reporter)}</div>
                    <div class="role-tag">📍 Found at: ${escHtml(item.location)}</div>
                    <div class="role-tag">📅 Found on: ${dateVal}</div>
                    ${item.reporter_phone ? `<div style="margin-top:6px;font-size:13px;font-weight:700;color:var(--primary)">📞 ${escHtml(item.reporter_phone)}</div>` : ''}
                </div>
            </div>
        </div>`;
    }

    // Reporter info (lost items)
    let reporterHtml = '';
    if (isLost) {
        const initial = item.reporter ? item.reporter.charAt(0).toUpperCase() : '?';
        const reporterAvatarSrc = item.reporter_avatar ? '/lostfound_relink/' + item.reporter_avatar : null;
        const reporterPhotoEl = reporterAvatarSrc
            ? `<img src="${reporterAvatarSrc}" style="width:52px;height:52px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);flex-shrink:0;cursor:zoom-in" alt="Profile" onclick="zoomImg('${reporterAvatarSrc}','${escHtml(item.reporter)}')">` 
            : `<div class="reporter-avatar">${initial}</div>`;
        reporterHtml = `
        <div class="reporter-section">
            <h4>👤 Reported By</h4>
            <div class="reporter-profile">
                ${reporterPhotoEl}
                <div class="reporter-info">
                    <div class="name">${escHtml(item.reporter)}</div>
                    <div class="sub">Lost on ${dateVal} at ${escHtml(item.location)}</div>
                    ${(item.contact || item.reporter_phone) ? `<div style="margin-top:6px;font-size:13px;font-weight:700;color:var(--primary)">📞 ${escHtml(item.contact || item.reporter_phone)}</div>` : ''}
                </div>
            </div>
        </div>`;
    }

    // Message button
    const LOGGED_IN = <?= isLoggedIn() ? 'true' : 'false' ?>;
    const ME_ID = <?= $current_user_id ?>;
    let msgBtnHtml = '';
    if (LOGGED_IN && item.poster_id && parseInt(item.poster_id) !== ME_ID) {
        const convType = item.poster_role === 'admin' ? 'user_admin' : 'user_user';
        const btnLabel = item.poster_role === 'admin' ? '💬 Message Admin' : '💬 Message Reporter';
        msgBtnHtml = `<div style="padding:16px 0 4px;border-top:1px solid var(--border);margin-top:8px;">
            <button class="btn btn-primary btn-block" style="font-size:14px;" onclick="startMsgFromBrowse(${item.poster_id},'${escHtml(item.reporter)}','${convType}','${escHtml(item.item_name)}')">${btnLabel} about "${escHtml(item.item_name)}"</button>
        </div>`;
    } else if (!LOGGED_IN) {
        msgBtnHtml = `<div style="padding:16px 0 4px;border-top:1px solid var(--border);margin-top:8px;">
            <a href="/lostfound_relink/auth/login.php" class="btn btn-outline btn-block" style="font-size:14px;">🔐 Login to Message Reporter</a>
        </div>`;
    }

    document.getElementById('detailContent').innerHTML = `
        ${headerImg}
        ${photosRow}
        <div class="detail-body">
            <div class="detail-title">${escHtml(item.item_name)}</div>
            <div style="margin-bottom:14px"><span class="badge ${badgeClass}">${item.status}</span></div>
            <div class="detail-grid">
                <div class="detail-field"><label>Category</label><span>📂 ${escHtml(item.category||'—')}</span></div>
                <div class="detail-field"><label>Location</label><span>📍 ${escHtml(item.location||'—')}</span></div>
                <div class="detail-field"><label>${dateLabel}</label><span>📅 ${dateVal}</span></div>
                <div class="detail-field"><label>Status</label><span class="badge ${badgeClass}">${item.status}</span></div>
                ${contactHtml}
            </div>
            ${descHtml}
            ${finderHtml}
            ${reporterHtml}
            ${msgBtnHtml}
        </div>`;

    document.getElementById('detailOverlay').classList.add('open');
    document.body.style.overflow = 'hidden';
}

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

function closeDetailOnBg(e) {
    if (e.target === document.getElementById('detailOverlay')) closeDetail();
}

// Zoom lightbox
function zoomImg(src, label) {
    document.getElementById('zoomImg').src = src;
    document.getElementById('zoomLabel').textContent = label;
    document.getElementById('zoomOverlay').classList.add('open');
}

function closeZoom() {
    document.getElementById('zoomOverlay').classList.remove('open');
    document.getElementById('zoomImg').src = '';
}

function startMsgFromBrowse(posterId, posterName, convType, itemName) {
    fetch('/lostfound_relink/messaging_ajax.php', {
        method: 'POST',
        body: new URLSearchParams({ action: 'start_conversation', other_id: posterId, type: convType })
    })
    .then(r => r.json())
    .then(data => {
        if (!data.conversation_id) return;
        const openingMsg = 'Hi! I saw your post about "' + itemName + '" on the browse page. I would like to discuss it with you.';
        fetch('/lostfound_relink/messaging_ajax.php', {
            method: 'POST',
            body: new URLSearchParams({ action: 'send', conversation_id: data.conversation_id, message: openingMsg })
        }).then(() => {
            closeDetail();
            const dest = <?= isAdmin() ? "'/lostfound_relink/admin/messages.php'" : "'/lostfound_relink/user/messages.php'" ?>;
            window.location.href = dest + '?conv=' + data.conversation_id;
        });
    });
}

function escHtml(str) {
    if (!str) return '—';
    return String(str).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
}

// ========== SHARE FUNCTIONS ==========

function toggleShare(e, type, id, name) {
    e.stopPropagation();
    const dropdown = document.getElementById('share-' + type + '-' + id);
    // close all other open dropdowns first
    document.querySelectorAll('.share-dropdown.open').forEach(d => {
        if (d !== dropdown) d.classList.remove('open');
    });
    dropdown.classList.toggle('open');
}

function copyItemLink(type, id, btn) {
    const url = window.location.origin + '/lostfound_relink/public/browse.php?view=' + type + '&id=' + id;
    navigator.clipboard.writeText(url).then(() => {
        btn.querySelector('.default-text').style.display = 'none';
        btn.querySelector('.copied-text').style.display = 'inline';
        setTimeout(() => {
            btn.querySelector('.default-text').style.display = 'inline';
            btn.querySelector('.copied-text').style.display = 'none';
            btn.closest('.share-dropdown').classList.remove('open');
        }, 1800);
    });
}

function shareToFacebook(itemType, itemId, itemName) {
    const url = window.location.origin + '/lostfound_relink/public/browse.php?view=' + encodeURIComponent(itemType) + '&id=' + encodeURIComponent(itemId);
    const fbUrl = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(url);
    window.open(fbUrl, 'facebook-share', 'width=600,height=400,scrollbars=no,resizable=yes');
    document.querySelectorAll('.share-dropdown.open').forEach(d => d.classList.remove('open'));
}

// close dropdown when clicking outside
document.addEventListener('click', () => {
    document.querySelectorAll('.share-dropdown.open').forEach(d => d.classList.remove('open'));
});

document.addEventListener('keydown', e => {
    if (e.key === 'Escape') {
        if (document.getElementById('zoomOverlay').classList.contains('open')) closeZoom();
        else closeDetail();
    }
});
</script>
<?php if(isLoggedIn()): ?>
<script src="/lostfound_relink/assets/js/main.js"></script>
<?php endif; ?>
</body>
</html>
