<?php
ob_start();
include __DIR__ . '/../includes/header.php';
$headerHtml = ob_get_clean();
$headerHtml = preg_replace('/<\/body>\s*<\/html>\s*$/i', '', $headerHtml);

$pageAssets = <<<'HTML'
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Sora:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">

<style>
/* ── All styles scoped to #reg-page to avoid conflicts ─────── */
#reg-page {
    --gold:      #d4a017;
    --gold-lt:   #f5c842;
    --gold-pale: #fdf6e3;
    --navy:      #0d1f3c;
    --navy-md:   #1a3560;
    --navy-lt:   #2a4a7f;
    --blue:      #2563eb;
    --blue-lt:   #eff6ff;
    --green:     #059669;
    --green-lt:  #ecfdf5;
    --red:       #dc2626;
    --red-lt:    #fef2f2;
    --amber:     #d97706;
    --amber-lt:  #fffbeb;
    --gray-50:   #f9fafb;
    --gray-100:  #f3f4f6;
    --gray-200:  #e5e7eb;
    --gray-300:  #d1d5db;
    --gray-400:  #9ca3af;
    --gray-500:  #6b7280;
    --gray-600:  #4b5563;
    --gray-700:  #374151;
    --gray-800:  #1f2937;
    --gray-900:  #111827;
    --white:     #ffffff;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08), 0 2px 6px rgba(0,0,0,0.05);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.1), 0 4px 12px rgba(0,0,0,0.06);
    --r-xl: 20px;
    --r-lg: 14px;
    --r-md: 10px;
    --r-sm: 6px;
    font-family: 'Sora', sans-serif;
    background: #f0f4f9;
    color: var(--gray-800);
}

#reg-page * { box-sizing: border-box; }

/* ── Layout ─────────────────────────────────────────────────── */
.rp-wrap { max-width: 1320px; margin: 0 auto; padding: 0 20px 80px; }

/* ── Breadcrumb ─────────────────────────────────────────────── */
.rp-breadcrumb {
    display: flex; align-items: center; gap: 8px;
    padding: 24px 0 22px;
    font-size: 12px; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--gray-400);
}
.rp-breadcrumb a { color: var(--navy-md); text-decoration: none; transition: color .2s; }
.rp-breadcrumb a:hover { color: var(--gold); }
.rp-breadcrumb .sep { color: var(--gray-300); font-size: 10px; }
.rp-breadcrumb .current { color: var(--navy); font-weight: 700; }

/* ── Card Base ──────────────────────────────────────────────── */
.rp-card {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-md);
}

/* ════════════════════════════════════════════════════════════════
   HERO CARD
═══════════════════════════════════════════════════════════════ */
.rp-hero {
    padding: 0;
    margin-bottom: 24px;
    overflow: hidden;
    animation: rpFade .5s ease both;
}

/* Gold accent top bar */
.rp-hero-bar {
    height: 5px;
    background: linear-gradient(90deg, var(--navy) 0%, var(--gold-lt) 50%, var(--navy) 100%);
}

.rp-hero-inner { padding: 36px 40px; }

/* Top section: logo + info + rating */
.rp-hero-top {
    display: grid;
    grid-template-columns: 140px 1fr 220px;
    gap: 32px;
    align-items: start;
    padding-bottom: 30px;
    margin-bottom: 30px;
    border-bottom: 1px solid var(--gray-100);
}

/* Logo */
.rp-logo-wrap {
    position: relative;
}
.rp-logo-box {
    width: 130px; height: 130px;
    background: var(--navy);
    border-radius: var(--r-lg);
    display: flex; align-items: center; justify-content: center;
    padding: 18px;
    box-shadow: 0 8px 24px rgba(13,31,60,0.25);
    position: relative;
    overflow: hidden;
}
.rp-logo-box::before {
    content: '';
    position: absolute; inset: 0;
    background: linear-gradient(135deg, rgba(212,160,23,0.18) 0%, transparent 60%);
}
.rp-logo-box img {
    max-width: 100%; max-height: 100%;
    object-fit: contain;
    filter: brightness(0) invert(1);
    position: relative; z-index: 1;
}
.rp-logo-verified {
    display: flex; align-items: center; gap: 5px;
    margin-top: 10px;
    background: var(--green-lt);
    border: 1px solid rgba(5,150,105,0.2);
    border-radius: 6px;
    padding: 5px 10px;
    font-size: 11px; font-weight: 600;
    color: var(--green);
}

/* Regulator info */
.rp-reg-name {
    font-size: 28px; font-weight: 800;
    color: var(--navy);
    line-height: 1.15;
    margin-bottom: 8px;
    letter-spacing: -0.02em;
}
.rp-reg-meta {
    font-size: 13px; color: var(--gray-500);
    margin-bottom: 16px;
}
.rp-badges { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 18px; }
.rp-badge {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 5px 12px; border-radius: 7px;
    font-size: 11px; font-weight: 600;
    letter-spacing: 0.03em;
    border: 1.5px solid;
}
.rp-badge-navy  { background: var(--navy);    color: #fff;           border-color: var(--navy); }
.rp-badge-gold  { background: var(--gold-pale); color: var(--amber); border-color: rgba(212,160,23,0.35); }
.rp-badge-green { background: var(--green-lt); color: var(--green);  border-color: rgba(5,150,105,0.25); }
.rp-badge-blue  { background: var(--blue-lt);  color: var(--blue);   border-color: rgba(37,99,235,0.2); }

/* Quick stats inline */
.rp-quick-stats { display: flex; gap: 24px; }
.rp-qstat { text-align: center; }
.rp-qstat-val { font-size: 18px; font-weight: 800; color: var(--navy); font-family: 'JetBrains Mono', monospace; }
.rp-qstat-key { font-size: 10px; color: var(--gray-400); text-transform: uppercase; letter-spacing: 0.06em; margin-top: 2px; }

/* Rating column */
.rp-rating-col {
    background: var(--navy);
    border-radius: var(--r-lg);
    padding: 24px 20px;
    text-align: center;
    position: relative;
    overflow: hidden;
}
.rp-rating-col::before {
    content: '';
    position: absolute; top: -40px; right: -40px;
    width: 120px; height: 120px;
    background: radial-gradient(circle, rgba(212,160,23,0.2), transparent 70%);
}
.rp-rating-label {
    font-size: 10px; font-weight: 600;
    letter-spacing: 0.1em; text-transform: uppercase;
    color: rgba(255,255,255,0.5); margin-bottom: 4px;
}
.rp-rating-grade {
    font-size: 52px; font-weight: 800;
    color: var(--gold-lt);
    line-height: 1;
    letter-spacing: -0.04em;
    font-family: 'JetBrains Mono', monospace;
}
.rp-rating-sublabel {
    font-size: 11px; font-weight: 600;
    padding: 4px 12px; border-radius: 5px;
    display: inline-block; margin: 6px 0 12px;
}
.rp-level-high   { background: rgba(5,150,105,0.2);  color: #34d399; }
.rp-level-medium { background: rgba(245,158,11,0.2); color: #fcd34d; }
.rp-level-low    { background: rgba(220,38,38,0.2);  color: #fca5a5; }
#ratingChart { width: 100%; height: 150px; }

/* ── Stat tiles ─────────────────────────────────────────────── */
.rp-stat-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 20px;
}
.rp-stat-tile {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--r-lg);
    padding: 18px 20px;
    display: flex; align-items: center; gap: 14px;
    transition: all .25s;
    cursor: default;
}
.rp-stat-tile:hover {
    border-color: var(--gold);
    background: var(--gold-pale);
    box-shadow: var(--shadow-sm);
}
.rp-st-icon {
    width: 44px; height: 44px; border-radius: 11px;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; flex-shrink: 0;
}
.rp-st-icon.navy  { background: var(--navy);    color: var(--gold-lt); }
.rp-st-icon.gold  { background: var(--gold-pale); color: var(--amber); border: 1px solid rgba(212,160,23,0.3); }
.rp-st-icon.green { background: var(--green-lt); color: var(--green); }
.rp-st-body { flex: 1; min-width: 0; }
.rp-st-key  { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; color: var(--gray-400); margin-bottom: 3px; }
.rp-st-val  { font-size: 14px; font-weight: 700; color: var(--gray-900); }
.rp-st-sub  { font-size: 11px; color: var(--gray-400); margin-top: 1px; }
.rp-st-right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.rp-pill {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px; font-weight: 600;
    padding: 3px 8px; border-radius: 5px; white-space: nowrap;
}
.rp-pill-red   { background: var(--red-lt);   color: var(--red);   }
.rp-pill-blue  { background: var(--blue-lt);  color: var(--blue);  }
.rp-pill-amber { background: var(--amber-lt); color: var(--amber); }
.rp-pill-navy  { background: rgba(13,31,60,0.08); color: var(--navy); }

/* Flag chip */
.rp-flag { display: flex; align-items: center; gap: 5px; }
.rp-flag img { width: 17px; border-radius: 3px; box-shadow: 0 1px 3px rgba(0,0,0,0.15); }
.rp-flag span { font-size: 11px; font-weight: 500; color: var(--gray-600); }

/* ── Metric Row ─────────────────────────────────────────────── */
.rp-metrics {
    display: grid; grid-template-columns: repeat(3,1fr); gap: 16px;
    padding-top: 20px;
    border-top: 1px solid var(--gray-100);
    margin-bottom: 20px;
}
.rp-metric {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--r-lg);
    padding: 18px 20px;
    transition: all .25s;
}
.rp-metric:hover { border-color: var(--navy-lt); box-shadow: var(--shadow-sm); }
.rp-metric-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.07em; font-weight: 600; color: var(--gray-400); margin-bottom: 4px; }
.rp-metric-grade {
    font-family: 'JetBrains Mono', monospace;
    font-size: 24px; font-weight: 700;
    color: var(--navy); margin-bottom: 2px;
}
.rp-metric-rank { font-size: 11px; color: var(--gray-500); margin-bottom: 10px; }
.rp-metric-rank strong { color: var(--gold); }
.rp-score-tag {
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px; font-weight: 600;
    padding: 2px 7px; border-radius: 5px;
    background: var(--navy); color: var(--gold-lt);
}

/* Website bar */
.rp-site-bar {
    display: flex; align-items: center; justify-content: space-between;
    background: var(--navy);
    border-radius: var(--r-lg);
    padding: 14px 20px;
    text-decoration: none;
    transition: background .25s;
}
.rp-site-bar:hover { background: var(--navy-md); }
.rp-site-bar-l { display: flex; align-items: center; gap: 12px; }
.rp-site-icon {
    width: 36px; height: 36px;
    background: rgba(212,160,23,0.15);
    border: 1px solid rgba(212,160,23,0.3);
    border-radius: 9px;
    display: flex; align-items: center; justify-content: center;
    color: var(--gold-lt); font-size: 13px;
}
.rp-site-text { font-size: 13px; font-weight: 500; color: rgba(255,255,255,0.7); }
.rp-site-link { font-size: 13px; font-weight: 700; color: var(--gold-lt); display: flex; align-items: center; gap: 6px; }

/* ════════════════════════════════════════════════════════════════
   INFO CARDS
═══════════════════════════════════════════════════════════════ */
.rp-info-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 18px;
    margin-bottom: 24px;
    animation: rpFade .5s ease .15s both;
}
.rp-info-card {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: var(--r-xl);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: all .3s cubic-bezier(.4,0,.2,1);
}
.rp-info-card:hover {
    border-color: var(--navy-lt);
    box-shadow: var(--shadow-lg);
    transform: translateY(-4px);
}
.rp-info-img {
    height: 100px; overflow: hidden; position: relative;
}
.rp-info-img img {
    width: 100%; height: auto; object-fit: cover;
    transition: transform .5s ease;
}
.rp-info-card:hover .rp-info-img img { transform: scale(1.08); }
/* Navy gradient overlay on image */
.rp-info-img::after {
    content: '';
    position: absolute; bottom: 0; left: 0; right: 0; height: 50%;
    background: linear-gradient(transparent, rgba(13,31,60,0.5));
}
.rp-info-body { padding: 16px 18px 20px; }
.rp-info-title { font-size: 14px; font-weight: 700; color: var(--navy); margin-bottom: 8px; line-height: 1.3; }
.rp-info-hr { height: 1px; background: var(--gray-100); margin: 8px 0; }
.rp-info-label { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--gray-400); margin-bottom: 3px; }
.rp-info-val { font-size: 13px; font-weight: 600; color: var(--gray-700); line-height: 1.4; }
.rp-info-gold { font-size: 13px; font-weight: 700; color: var(--amber); }

/* ════════════════════════════════════════════════════════════════
   INTRO / ABOUT
═══════════════════════════════════════════════════════════════ */
.rp-intro {
    position: relative; overflow: hidden;
    padding: 44px 48px;
    margin-bottom: 24px;
    text-align: center;
    animation: rpFade .5s ease .2s both;
}
/* Watermark */
.rp-intro::before {
    content: 'ABOUT';
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 130px; font-weight: 800;
    color: rgba(13,31,60,0.025);
    white-space: nowrap;
    pointer-events: none;
    user-select: none;
    letter-spacing: 0.1em;
}
/* Gold accent line */
.rp-intro::after {
    content: '';
    position: absolute; top: 0; left: 50%; transform: translateX(-50%);
    width: 60px; height: 3px;
    background: linear-gradient(90deg, var(--gold), var(--gold-lt));
    border-radius: 0 0 4px 4px;
}
.rp-intro-badge {
    display: inline-flex; align-items: center; gap: 6px;
    background: var(--navy); color: var(--gold-lt);
    border-radius: 99px; padding: 5px 16px;
    font-size: 11px; font-weight: 600;
    letter-spacing: 0.06em; text-transform: uppercase;
    margin-bottom: 20px;
}
.rp-intro-title { font-size: 24px; font-weight: 800; color: var(--navy); margin-bottom: 14px; letter-spacing: -0.01em; }
.rp-intro-text  { font-size: 14px; line-height: 1.85; color: var(--gray-600); max-width: 740px; margin: 0 auto; }

/* ════════════════════════════════════════════════════════════════
   SECTION HEADER
═══════════════════════════════════════════════════════════════ */
.rp-sec-header { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.rp-sec-title  { font-size: 20px; font-weight: 800; color: var(--navy); white-space: nowrap; }
.rp-sec-line   { flex: 1; height: 1px; background: linear-gradient(90deg, var(--gray-300), transparent); }
.rp-sec-badge  {
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px; font-weight: 600;
    background: var(--navy); color: var(--gold-lt);
    padding: 3px 10px; border-radius: 5px;
}

/* ════════════════════════════════════════════════════════════════
   BROKER CARDS
═══════════════════════════════════════════════════════════════ */
.rp-members-grid {
    display: grid; grid-template-columns: repeat(2,1fr); gap: 16px;
    margin-bottom: 24px;
    animation: rpFade .5s ease .25s both;
}
.rp-broker {
    display: flex; gap: 18px;
    background: var(--white);
    border: 1.5px solid var(--gray-200);
    border-radius: var(--r-xl);
    padding: 22px 24px;
    text-decoration: none;
    transition: all .3s cubic-bezier(.4,0,.2,1);
    position: relative; overflow: hidden;
}
/* Left accent line */
.rp-broker::before {
    content: '';
    position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
    background: linear-gradient(180deg, var(--gold), var(--navy));
    border-radius: 0 0 0 var(--r-xl);
    transform: scaleY(0);
    transition: transform .3s;
    transform-origin: top;
}
.rp-broker:hover { border-color: var(--navy-lt); box-shadow: var(--shadow-lg); transform: translateY(-3px); }
.rp-broker:hover::before { transform: scaleY(1); }

.rp-broker-logo {
    width: 70px; height: 70px; flex-shrink: 0;
    background: var(--navy);
    border-radius: var(--r-lg);
    display: flex; align-items: center; justify-content: center; padding: 10px;
    box-shadow: 0 4px 12px rgba(13,31,60,0.2);
}
.rp-broker-logo img { max-width: 100%; max-height: 100%; object-fit: contain; filter: brightness(0) invert(1); }
.rp-broker-body { flex: 1; min-width: 0; }
.rp-broker-name { font-size: 17px; font-weight: 800; color: var(--navy); margin-bottom: 4px; transition: color .2s; }
.rp-broker:hover .rp-broker-name { color: var(--gold); }
.rp-broker-score-row { display: flex; align-items: baseline; gap: 5px; margin-bottom: 10px; }
.rp-broker-score {
    font-family: 'JetBrains Mono', monospace;
    font-size: 22px; font-weight: 700; color: var(--gold);
}
.rp-broker-score-lbl { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.07em; color: var(--gray-400); }
.rp-broker-tags { display: flex; flex-wrap: wrap; gap: 5px; }
.rp-btag {
    padding: 3px 8px;
    background: var(--gray-100);
    border: 1px solid var(--gray-200);
    border-radius: 5px;
    font-size: 10px; font-weight: 500; color: var(--gray-600);
}
.rp-broker-arrow { color: var(--gray-300); font-size: 11px; align-self: flex-start; padding-top: 4px; flex-shrink: 0; transition: color .2s, transform .2s; }
.rp-broker:hover .rp-broker-arrow { color: var(--gold); transform: translate(2px,-2px); }

/* ════════════════════════════════════════════════════════════════
   DISCLOSURES
═══════════════════════════════════════════════════════════════ */
.rp-disclosures {
    animation: rpFade .5s ease .3s both;
}
.rp-tab-nav {
    display: flex; gap: 2px;
    background: var(--gray-100);
    border-radius: 10px;
    padding: 4px;
    margin-bottom: 24px;
    width: fit-content;
}
.rp-tab {
    padding: 8px 18px;
    font-size: 12px; font-weight: 600;
    letter-spacing: 0.04em;
    color: var(--gray-500);
    cursor: pointer;
    border-radius: 7px;
    border: none; background: none;
    transition: all .2s;
}
.rp-tab.active {
    background: var(--navy);
    color: var(--gold-lt);
    box-shadow: 0 2px 8px rgba(13,31,60,0.2);
}
.rp-tab-count {
    display: inline-block; margin-left: 5px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px; padding: 1px 5px;
    border-radius: 4px;
    background: rgba(255,255,255,0.15);
}
.rp-tab:not(.active) .rp-tab-count { background: var(--gray-200); color: var(--gray-500); }

.rp-disc-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 16px; }

.rp-disc-item {
    background: var(--white);
    border: 1.5px solid var(--gray-200);
    border-radius: var(--r-xl);
    padding: 20px;
    text-decoration: none;
    display: block;
    transition: all .25s cubic-bezier(.4,0,.2,1);
}
.rp-disc-item:hover { border-color: var(--navy-lt); box-shadow: var(--shadow-lg); transform: translateY(-3px); }
.rp-disc-meta { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.rp-disc-type {
    font-size: 10px; font-weight: 700;
    letter-spacing: 0.07em; text-transform: uppercase;
    padding: 5px 11px; border-radius: 6px;
    display: inline-flex; align-items: center; gap: 5px;
}
.rp-type-warn   { background: var(--amber-lt); color: var(--amber); border: 1px solid rgba(217,119,6,0.2); }
.rp-type-punish { background: var(--red-lt);   color: var(--red);   border: 1px solid rgba(220,38,38,0.2); }
.rp-disc-date { font-family: 'JetBrains Mono', monospace; font-size: 10px; color: var(--gray-400); }
.rp-disc-title { font-size: 13px; font-weight: 600; color: var(--gray-700); line-height: 1.55; margin-bottom: 14px; }
.rp-disc-footer { display: flex; align-items: center; gap: 8px; padding-top: 12px; border-top: 1px solid var(--gray-100); }
.rp-disc-avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--gray-100); border: 1px solid var(--gray-200); display: flex; align-items: center; justify-content: center; overflow: hidden; }
.rp-disc-avatar img { width: 18px; }
.rp-disc-org { font-size: 11px; font-weight: 700; color: var(--gray-600); }

/* ── Empty State ────────────────────────────────────────────── */
.rp-empty { padding: 80px 40px; text-align: center; }
.rp-empty i { font-size: 52px; color: var(--gray-300); margin-bottom: 20px; display: block; }
.rp-empty-title { font-size: 20px; font-weight: 800; color: var(--navy); margin-bottom: 8px; }
.rp-empty-text  { font-size: 14px; color: var(--gray-400); }

/* ── Animation ──────────────────────────────────────────────── */
@keyframes rpFade { from { opacity:0; transform:translateY(16px); } to { opacity:1; transform:none; } }

/* ── Responsive ─────────────────────────────────────────────── */
@media (max-width: 1100px) {
    .rp-hero-top { grid-template-columns: auto 1fr; }
    .rp-rating-col { display: none; }
    .rp-info-grid { grid-template-columns: repeat(2, 1fr); }
    .rp-stat-grid { grid-template-columns: 1fr 1fr; }
    .rp-metrics { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 768px) {
    .rp-hero-inner { padding: 24px 20px; }
    .rp-hero-top { grid-template-columns: 1fr; }
    .rp-logo-box { width: 90px; height: 90px; }
    .rp-reg-name { font-size: 22px; }
    .rp-stat-grid, .rp-metrics { grid-template-columns: 1fr; }
    .rp-members-grid, .rp-disc-grid { grid-template-columns: 1fr; }
    .rp-intro { padding: 30px 20px; }
    .rp-quick-stats { gap: 14px; }
}
@media (max-width: 520px) {
    .rp-info-grid { grid-template-columns: 1fr; }
    .rp-wrap { padding: 0 14px 60px; }
}
</style>
HTML;

echo str_replace('</head>', $pageAssets . PHP_EOL . '</head>', $headerHtml);
include __DIR__ . '/../config.php';

$regulator = trim($_GET['regulator'] ?? '');
$results   = [];

if ($regulator !== '') {
    $stmt = $dbh->prepare("SELECT * FROM brokers WHERE regulator LIKE ?");
    $stmt->execute(["%{$regulator}%"]);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>

<script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>

<div id="reg-page">
<div class="rp-wrap">

<?php if (!empty($results)): ?>
<?php
    $row      = $results[0];
    $regLevel = strtolower(trim($row['regulation_level'] ?? ''));
    [$lvlCls, $lvlLbl] = match($regLevel) {
        'high'   => ['rp-level-high',   'High Trust'],
        'medium' => ['rp-level-medium', 'Medium Trust'],
        'low'    => ['rp-level-low',    'Low Trust'],
        default  => ['rp-level-medium', 'Unknown'],
    };
?>

<!-- ── Breadcrumb ─────────────────────────────────────────── -->
<nav class="rp-breadcrumb">
    <a href="/"><i class="fas fa-home"></i> Home</a>
    <span class="sep"><i class="fas fa-chevron-right"></i></span>
    <a href="#">Regulators</a>
    <span class="sep"><i class="fas fa-chevron-right"></i></span>
    <span class="current"><?= htmlspecialchars($regulator) ?></span>
</nav>

<!-- ════════════════════════════════════════════════════════════
     HERO CARD
═══════════════════════════════════════════════════════════════ -->
<div class="rp-card rp-hero">
    <div class="rp-hero-bar"></div>
    <div class="rp-hero-inner">

        <div class="rp-hero-top">
            <!-- Logo -->
            <div class="rp-logo-wrap">
                <div class="rp-logo-box">
                    <img src="<?= $base_url ?>/uploads/regulator/<?= htmlspecialchars($row['regulator_logo']) ?>"
                         alt="<?= htmlspecialchars($row['regulator']) ?>">
                </div>
                <div class="rp-logo-verified">
                    <i class="fas fa-circle-check"></i> Verified
                </div>
            </div>

            <!-- Info -->
            <div>
                <h1 class="rp-reg-name"><?= htmlspecialchars($row['regulator']) ?></h1>
                <p class="rp-reg-meta">Financial Regulatory Authority &middot; Est. 2001 &middot; Government Statutory Body</p>
                <div class="rp-badges">
                    <span class="rp-badge rp-badge-navy"><i class="fas fa-shield-halved"></i><?= htmlspecialchars($row['regulator']) ?></span>
                    <span class="rp-badge rp-badge-green"><i class="fas fa-circle-check"></i> Govt. Regulated</span>
                    <span class="rp-badge rp-badge-gold"><i class="fas fa-layer-group"></i><?= htmlspecialchars($row['assets'] ?? 'Gold') ?></span>
                    <span class="rp-badge rp-badge-blue"><i class="fas fa-globe"></i> IOSCO Member</span>
                </div>
                <div class="rp-quick-stats" style="margin-top:16px">
                    <div class="rp-qstat">
                        <div class="rp-qstat-val"><?= count($results) ?></div>
                        <div class="rp-qstat-key">Members</div>
                    </div>
                    <div style="width:1px;background:var(--gray-200)"></div>
                    <div class="rp-qstat">
                        <div class="rp-qstat-val">35+</div>
                        <div class="rp-qstat-key">Years</div>
                    </div>
                    <div style="width:1px;background:var(--gray-200)"></div>
                    <div class="rp-qstat">
                        <div class="rp-qstat-val">AAA</div>
                        <div class="rp-qstat-key">Rating</div>
                    </div>
                </div>
            </div>

            <!-- Rating -->
            <div class="rp-rating-col">
                <div class="rp-rating-label">Regulatory Rating</div>
                <div class="rp-rating-grade"><?= htmlspecialchars($row['regulation_rating'] ?? 'AA') ?></div>
                <div class="rp-rating-sublabel <?= $lvlCls ?>"><?= $lvlLbl ?></div>
                <div id="ratingChart"></div>
            </div>
        </div>

        <!-- Stat tiles -->
        <div class="rp-stat-grid">
            <div class="rp-stat-tile">
                <div class="rp-st-icon navy"><i class="fas fa-server"></i></div>
                <div class="rp-st-body">
                    <div class="rp-st-key">Trading Environment</div>
                    <div class="rp-st-val">AAA Class</div>
                    <div class="rp-st-sub">Avg. transaction speed</div>
                </div>
                <div class="rp-st-right">
                    <span class="rp-pill rp-pill-blue">638.4 ms</span>
                    <span class="rp-pill rp-pill-red">Below avg.</span>
                </div>
            </div>
            <div class="rp-stat-tile">
                <div class="rp-st-icon gold"><i class="fas fa-desktop"></i></div>
                <div class="rp-st-body">
                    <div class="rp-st-key">Platform License</div>
                    <div class="rp-st-val">MT4 / MT5 Full</div>
                    <div class="rp-st-sub">Commercial license holder</div>
                </div>
                <div class="rp-st-right">
                    <div class="rp-flag">
                        <img src="https://flagcdn.com/w40/gb.png" alt="UK">
                        <span>UK</span>
                    </div>
                    <span class="rp-pill rp-pill-amber">MT4</span>
                </div>
            </div>
            <div class="rp-stat-tile">
                <div class="rp-st-icon green"><i class="fas fa-coins"></i></div>
                <div class="rp-st-body">
                    <div class="rp-st-key">Capital Adequacy</div>
                    <div class="rp-st-val">Perfect</div>
                    <div class="rp-st-sub">Verified regulatory capital</div>
                </div>
                <div class="rp-st-right">
                    <span style="font-family:'JetBrains Mono',monospace;font-size:13px;font-weight:700;color:var(--gray-900)">$775,194</span>
                    <span style="font-size:10px;color:var(--gray-400)">USD · Verified</span>
                </div>
            </div>
        </div>

        <!-- Metric row -->
        <div class="rp-metrics">
            <div class="rp-metric">
                <div class="rp-metric-label">Influence Score</div>
                <div class="rp-metric-grade">AA</div>
                <div class="rp-metric-rank">Influence index <strong>#1</strong></div>
                <div style="display:flex;align-items:center;gap:7px">
                    <img src="https://flagcdn.com/w40/co.png" style="width:17px;border-radius:3px" alt="CO">
                    <span style="font-size:12px;color:var(--gray-600)">Colombia</span>
                    <span class="rp-score-tag">7.98</span>
                </div>
            </div>
            <div class="rp-metric">
                <div class="rp-metric-label">Execution Speed</div>
                <div class="rp-metric-grade">A<sup style="font-size:14px">+</sup></div>
                <div class="rp-metric-rank">Fastest execution <strong>#3</strong></div>
                <div style="display:flex;align-items:center;gap:7px">
                    <img src="https://flagcdn.com/w40/us.png" style="width:17px;border-radius:3px" alt="US">
                    <span style="font-size:12px;color:var(--gray-600)">United States</span>
                    <span class="rp-score-tag">6.43</span>
                </div>
            </div>
            <div class="rp-metric">
                <div class="rp-metric-label">Regulatory Score</div>
                <div class="rp-metric-grade">AAA</div>
                <div class="rp-metric-rank">Top regulator <strong>#2</strong></div>
                <div style="display:flex;align-items:center;gap:7px">
                    <img src="https://flagcdn.com/w40/gb.png" style="width:17px;border-radius:3px" alt="GB">
                    <span style="font-size:12px;color:var(--gray-600)">United Kingdom</span>
                    <span class="rp-score-tag">8.55</span>
                </div>
            </div>
        </div>

        <!-- Website bar -->
        <a href="#" class="rp-site-bar">
            <div class="rp-site-bar-l">
                <div class="rp-site-icon"><i class="fas fa-globe"></i></div>
                <span class="rp-site-text">Official Website</span>
            </div>
            <span class="rp-site-link">
                <?= htmlspecialchars($row['regulator']) ?>
                <i class="fas fa-arrow-up-right-from-square" style="font-size:10px"></i>
            </span>
        </a>
    </div><!-- /rp-hero-inner -->
</div><!-- /hero -->

<!-- ══════════════════════════════════════════════════════════
     INFO CARDS
══════════════════════════════════════════════════════════ -->
<div class="rp-info-grid">
    <div class="rp-info-card">
        <div class="rp-info-img">
            <img src="<?= $base_url ?>/uploads/research1.jpg"
                 onerror="this.src='https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?auto=format&fit=crop&q=80&w=400'" alt="">
        </div>
        <div class="rp-info-body">
            <div class="rp-info-title">IOSCO Membership</div>
            <div class="rp-info-hr"></div>
            <p style="font-size:12px;color:var(--gray-500);line-height:1.6">Full cooperative member of the International Organisation of Securities Commissions.</p>
        </div>
    </div>
    <div class="rp-info-card">
        <div class="rp-info-img">
            <img src="<?= $base_url ?>/uploads/research2.jpg"
                 onerror="this.src='https://images.unsplash.com/photo-1611974789855-9c2a0a7236a3?auto=format&fit=crop&q=80&w=400'" alt="">
        </div>
        <div class="rp-info-body">
            <div class="rp-info-title">Regulated Instruments</div>
            <div class="rp-info-hr"></div>
            <div class="rp-info-label">Asset Classes</div>
            <div class="rp-info-val">Forex · Stocks · Derivatives · Securities · Futures</div>
        </div>
    </div>
    <div class="rp-info-card">
        <div class="rp-info-img">
            <img src="<?= $base_url ?>/uploads/research3.jpg"
                 onerror="this.src='https://images.unsplash.com/photo-1556742049-0cfed4f6a45d?auto=format&fit=crop&q=80&w=400'" alt="">
        </div>
        <div class="rp-info-body">
            <div class="rp-info-title">Customer Service</div>
            <div class="rp-info-hr"></div>
            <div class="rp-info-label">Hotline</div>
            <div class="rp-info-gold">+852 2231 1222</div>
        </div>
    </div>
    <div class="rp-info-card">
        <div class="rp-info-img">
            <img src="<?= $base_url ?>/uploads/research4.jpg"
                 onerror="this.src='https://images.unsplash.com/photo-1450101499163-c8848c66ca85?auto=format&fit=crop&q=80&w=400'" alt="">
        </div>
        <div class="rp-info-body">
            <div class="rp-info-title">Complaint Channels</div>
            <div class="rp-info-hr"></div>
            <div class="rp-info-label">Live Chat &amp; Email</div>
            <div class="rp-info-gold" style="font-size:12px;word-break:break-all">complaint@sfc.hk</div>
        </div>
    </div>
</div>

<!-- ══════════════════════════════════════════════════════════
     INTRO
══════════════════════════════════════════════════════════ -->
<div class="rp-card rp-intro">
    <div class="rp-intro-badge"><i class="fas fa-landmark"></i> Organization Overview</div>
    <h2 class="rp-intro-title">About <?= htmlspecialchars($row['regulator']) ?></h2>
    <p class="rp-intro-text">
        The Securities and Futures Commission (SFC) is an independent statutory body established in 1989 to regulate
        Hong Kong's securities and futures markets. Operating under the Securities and Futures Ordinance, it is funded
        by transaction levies and licensing fees. As a core financial regulator in Hong Kong, it upholds market
        integrity and investor protection.
    </p>
</div>

<!-- ══════════════════════════════════════════════════════════
     MEMBERS
══════════════════════════════════════════════════════════ -->
<div style="margin-bottom:24px">
    <div class="rp-sec-header">
        <h2 class="rp-sec-title">Regulated Members</h2>
        <div class="rp-sec-line"></div>
        <span class="rp-sec-badge"><?= count($results) ?> brokers</span>
    </div>
    <div class="rp-members-grid">
        <?php foreach ($results as $brow): ?>
        <a href="/en/dealer/<?= htmlspecialchars($brow['id']) ?>.html" target="_blank" class="rp-broker">
            <div class="rp-broker-logo">
                <img src="<?= $base_url ?>/uploads/<?= htmlspecialchars($brow['logo'] ?? '') ?>"
                     alt="<?= htmlspecialchars($brow['name'] ?? '') ?>">
            </div>
            <div class="rp-broker-body">
                <div class="rp-broker-name"><?= htmlspecialchars($brow['name'] ?? '') ?></div>
                <div class="rp-broker-score-row">
                    <span class="rp-broker-score"><?= number_format($brow['score'] ?? 0, 2) ?></span>
                    <span class="rp-broker-score-lbl">Score</span>
                </div>
                <div class="rp-broker-tags">
                    <span class="rp-btag">15–20 years</span>
                    <span class="rp-btag"><?= htmlspecialchars($brow['regulator']) ?></span>
                    <span class="rp-btag"><?= htmlspecialchars($brow['broker_type'] ?? '') ?></span>
                    <span class="rp-btag">MT4 Full</span>
                    <span class="rp-btag">Global</span>
                </div>
            </div>
            <i class="fas fa-arrow-up-right-from-square rp-broker-arrow"></i>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- ══════════════════════════════════════════════════════════
     DISCLOSURES
══════════════════════════════════════════════════════════ -->
<div class="rp-card rp-disclosures" style="padding:32px 36px">
    <div class="rp-sec-header" style="margin-bottom:20px">
        <h2 class="rp-sec-title">Regulatory Disclosures</h2>
        <div class="rp-sec-line"></div>
    </div>

    <div class="rp-tab-nav">
        <button class="rp-tab active" data-idx="all">All <span class="rp-tab-count">3</span></button>
        <button class="rp-tab" data-idx="warning">Warnings <span class="rp-tab-count">2</span></button>
        <button class="rp-tab" data-idx="punish">Sanctions <span class="rp-tab-count">1</span></button>
    </div>

    <div class="rp-disc-grid" id="disc-grid">
        <a href="#" class="rp-disc-item" data-type="warning">
            <div class="rp-disc-meta">
                <span class="rp-disc-type rp-type-warn"><i class="fas fa-triangle-exclamation"></i> Warning</span>
                <span class="rp-disc-date">2024-08-19</span>
            </div>
            <p class="rp-disc-title">Complete list of unlicensed companies and suspicious websites</p>
            <div class="rp-disc-footer">
                <div class="rp-disc-avatar"><img src="https://eimgjys.fxeyee.com/ico/2350656405/FXT2350656405_600037.png_wiki-template-global" alt=""></div>
                <span class="rp-disc-org">Arrexpro</span>
            </div>
        </a>
        <a href="#" class="rp-disc-item" data-type="punish">
            <div class="rp-disc-meta">
                <span class="rp-disc-type rp-type-punish"><i class="fas fa-gavel"></i> Sanction</span>
                <span class="rp-disc-date">2024-08-15</span>
            </div>
            <p class="rp-disc-title">Fraudulent license document discovered during regulatory audit</p>
            <div class="rp-disc-footer">
                <div class="rp-disc-avatar"><img src="https://eimgjys.fxeyee.com/ico/3730866846/FXT3730866846_837129.png_wiki-template-global" alt=""></div>
                <span class="rp-disc-org">OnePro</span>
            </div>
        </a>
        <a href="#" class="rp-disc-item" data-type="warning">
            <div class="rp-disc-meta">
                <span class="rp-disc-type rp-type-warn"><i class="fas fa-triangle-exclamation"></i> Warning</span>
                <span class="rp-disc-date">2024-07-25</span>
            </div>
            <p class="rp-disc-title">Suspicious offshore firm using forged regulatory license</p>
            <div class="rp-disc-footer">
                <div class="rp-disc-avatar"><img src="https://eimgjys.fxeyee.com/ico/3843629634/FXT3843629634_572495.png_wiki-template-global" alt=""></div>
                <span class="rp-disc-org">CoinUnited.io</span>
            </div>
        </a>
    </div>
</div>

<?php else: ?>
<div class="rp-card rp-empty">
    <i class="fas fa-search"></i>
    <div class="rp-empty-title">No Brokers Found</div>
    <p class="rp-empty-text">We couldn't find any brokers for the specified regulator.</p>
</div>
<?php endif; ?>

</div><!-- /rp-wrap -->
</div><!-- /reg-page -->

<script>
document.addEventListener("DOMContentLoaded", function () {

    /* ── Radar Chart ─────────────────────────────────────── */
    <?php if (!empty($results)): ?>
    const el = document.getElementById('ratingChart');
    if (el && typeof echarts !== 'undefined') {
        const chart = echarts.init(el, null, { renderer: 'svg' });
        chart.setOption({
            backgroundColor: 'transparent',
            tooltip: {
                backgroundColor: '#0d1f3c',
                borderColor: 'rgba(212,160,23,0.4)',
                textStyle: { color: '#f5c842', fontFamily: 'Sora', fontSize: 11 }
            },
            radar: {
                radius: '65%',
                center: ['50%', '50%'],
                indicator: [
                    { name: 'License',    max: 10 },
                    { name: 'Business',   max: 10 },
                    { name: 'Risk',       max: 10 },
                    { name: 'Software',   max: 10 },
                    { name: 'Regulation', max: 10 }
                ],
                axisName: { color: 'rgba(255,255,255,0.55)', fontSize: 9, fontWeight: 600 },
                splitArea: { areaStyle: { color: ['rgba(255,255,255,0.03)', 'rgba(255,255,255,0.07)'] } },
                axisLine:  { lineStyle: { color: 'rgba(255,255,255,0.1)' } },
                splitLine: { lineStyle: { color: 'rgba(255,255,255,0.08)' } }
            },
            series: [{
                type: 'radar',
                data: [{
                    value: [9.43, 8.89, 9.89, 9.99, 9.42],
                    name: 'Rating',
                    areaStyle: { color: 'rgba(212,160,23,0.2)' },
                    lineStyle: { color: '#f5c842', width: 2 },
                    itemStyle: { color: '#d4a017' },
                    symbolSize: 5
                }]
            }]
        });
        window.addEventListener('resize', () => chart.resize());
    }
    <?php endif; ?>

    /* ── Disclosure Tabs ──────────────────────────────────── */
    document.querySelectorAll('.rp-tab').forEach(btn => {
        btn.addEventListener('click', () => {
            document.querySelectorAll('.rp-tab').forEach(b => b.classList.remove('active'));
            btn.classList.add('active');
            const sel = btn.getAttribute('data-idx');
            document.querySelectorAll('.rp-disc-item').forEach(card => {
                card.style.display = (sel === 'all' || card.dataset.type === sel) ? 'block' : 'none';
            });
        });
    });
});
</script>

<?php include __DIR__ . '/../includes/footer.php'; ?>