<?php
require_once __DIR__ . '/auth.php';
dm_require_login();
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');

// CSP — FASE 1 (híbrido): 'unsafe-inline' temporal mientras se migran los ~77
// handlers inline restantes en HTML generado por JS (asistente campañas, tablas
// TikTok, historial AVI, facturas). FASE 2: restaurar nonce + strict-dynamic
// tras migrar esos handlers a data-click/data-input. Ver migración pendiente.
// Se mantienen el resto de protecciones: object-src none, base-uri, frame-ancestors, etc.
$csp_nonce = base64_encode(random_bytes(16)); // reservado para FASE 2 (inyección al final)
header("Content-Security-Policy: default-src 'self'; "
    . "script-src 'self' 'unsafe-inline' "
    . "https://cdnjs.cloudflare.com https://fonts.googleapis.com "
    . "https://www.googletagmanager.com https://www.clarity.ms "
    . "https://app.posthog.com https://client.crisp.chat "
    . "https://translate.google.com https://translate.googleapis.com; "
    . "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "
    . "font-src 'self' https://fonts.gstatic.com; "
    . "img-src 'self' data: https:; "
    . "connect-src 'self' https://api.stripe.com https://app.posthog.com "
    . "https://api.brevo.com https://client.crisp.chat https://www.clarity.ms; "
    . "frame-src 'self' https://app.crisp.chat https://js.stripe.com; "
    . "frame-ancestors 'none'; object-src 'none'; base-uri 'self'; "
    . "form-action 'self' https://checkout.stripe.com; upgrade-insecure-requests");

ob_start();
$user       = dm_user();
$clients    = dm_get_clients();
$isAdmin    = dm_is_admin();
$defaultClient = (!$isAdmin && count($clients)===1) ? $clients[0]['id'] : 'all';
// SaaS helpers
require_once __DIR__ . '/helpers/analytics_snippets.php';
require_once __DIR__ . '/helpers/crisp_widget.php';
require_once __DIR__ . '/helpers/trial_banner.php';
require_once __DIR__ . '/helpers/upgrade_modal.php';
$_sub = function_exists('dm_get_subscription') ? (dm_get_subscription((int)$user['id']) ?? []) : [];
// Nav modules: admin=all, Pro/Agency/Enterprise=all, Starter=modules_chosen
$_sub_plan   = $_sub['plan'] ?? '';
$_sub_addons = $_sub['addons'] ?? [];
$_sub_mods   = $_sub['modules_chosen'] ?? [];
function _nav(string $k): bool {
    global $isAdmin, $_sub_plan, $_sub_addons, $_sub_mods;
    if ($isAdmin) return true;
    if (!$_sub_plan) return false;
    if ($_sub_plan !== 'starter') return true; // Pro/Agency/Enterprise: all
    // Starter: check modules_chosen
    $map = ['meta'=>'meta_ads','google'=>'google_ads','seo'=>'seo',
            'videos'=>'videos','imagenes'=>'images','tiktok'=>'tiktok_ads','email'=>'email',
            'reportes'=>'reports','marca'=>'brand','automatizaciones'=>'automations'];
    if ($k === 'web') return !empty($_sub_addons['sitios_web']) || in_array('web', $_sub_mods);
    return in_array($map[$k] ?? $k, $_sub_mods);
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<script>try{var _dm_t=localStorage.getItem('dm_theme');if(_dm_t)document.documentElement.setAttribute('data-theme',_dm_t)}catch(e){}</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="google" content="notranslate">
<meta name="dm-csrf" content="<?= dm_csrf_token() ?>">
<title>DIMARK Brain · Super Cerebro</title>
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;0,900;1,700&family=Inter:wght@300;400;500&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.4.1/chart.umd.js"></script>
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
.goog-te-banner-frame,.goog-te-balloon-frame,.goog-te-gadget,#goog-gt-tt,.skiptranslate{display:none!important}
body{top:0!important}
:root{--red:#9333EA;--rl:rgba(147,51,234,.12);--rb:rgba(147,51,234,.25);--teal:#0DCFCF;--danger:#E8272A;--purple-l:#A855F7;--bg:#0f0f0f;--bg2:#181818;--bg3:#222;--bg4:#2a2a2a;--br:rgba(255,255,255,.07);--br2:rgba(255,255,255,.13);--t:#f0f0f0;--m:rgba(240,240,240,.42);--g:#22c97a;--y:#f5c543;--b:#4a9ef8;--p:#A855F7;--fh:'Playfair Display',serif;--fb:'Inter',sans-serif;--row-div:rgba(255,255,255,.03);--sub-bg:rgba(255,255,255,.03)}
html[data-theme="light"]{--bg:#f4f5f7;--bg2:#ffffff;--bg3:#ecedef;--bg4:#e2e3e6;--br:rgba(0,0,0,.08);--br2:rgba(0,0,0,.16);--t:#0d0d10;--m:rgba(13,13,16,.5);--rl:rgba(147,51,234,.08);--rb:rgba(147,51,234,.2);--row-div:rgba(0,0,0,.05);--sub-bg:rgba(0,0,0,.03)}
html,body{height:100%;background:var(--bg);color:var(--t);font-family:var(--fb);font-size:14px}

/* NAV */
nav{display:flex;align-items:center;justify-content:space-between;padding:0 20px;height:54px;background:var(--bg);border-bottom:1px solid var(--br);position:sticky;top:0;z-index:200}
.logo{display:flex;align-items:center;gap:9px}
.lm{width:30px;height:30px;background:linear-gradient(135deg,#7C3AED,#9333EA);border-radius:8px;display:flex;align-items:center;justify-content:center;font-family:var(--fh);font-weight:900;font-size:15px;color:#fff}
.ln{font-family:var(--fh);font-weight:900;font-size:15px}
.ls{font-size:10px;letter-spacing:1.5px;text-transform:uppercase;color:var(--teal);margin-top:1px}
.logo-b{color:var(--purple-l)}
.nav-c{display:flex;gap:2px}
.nt{padding:5px 11px;border-radius:6px;font-size:13px;color:var(--m);cursor:pointer;border:1px solid transparent;background:none;font-family:var(--fb);transition:all .15s}
.nt:hover{color:var(--t);background:var(--bg3)}
.nt.active{color:var(--t);background:var(--bg3);border-color:var(--br2)}
.nav-r{display:flex;align-items:center;gap:8px}
.live{display:flex;align-items:center;gap:5px;font-size:12px;color:var(--g)}
.dot{width:7px;height:7px;background:var(--g);border-radius:50%;animation:blink 2s ease-in-out infinite}
@keyframes blink{0%,100%{opacity:1}50%{opacity:.3}}

/* USER DROPDOWN */
.udrop{position:relative}
.uav{width:30px;height:30px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:500;cursor:pointer;border:1.5px solid transparent;transition:border-color .15s}
.uav:hover{border-color:var(--br2)}
.umenu{position:absolute;right:0;top:38px;background:var(--bg2);border:1px solid var(--br2);border-radius:10px;padding:6px;min-width:200px;display:none;z-index:300}
.umenu.open{display:block}
.uinfo{padding:8px 10px 8px}
.uname{font-size:13px;font-weight:500}
.urole{font-size:11px;color:var(--m);margin-top:1px}
.udiv{height:1px;background:var(--br);margin:4px 0}
.ulink{display:block;padding:7px 10px;border-radius:6px;font-size:13px;color:var(--m);text-decoration:none;transition:all .15s;cursor:pointer;background:none;border:none;width:100%;text-align:left;font-family:var(--fb)}
.ulink:hover{color:var(--t);background:var(--bg3)}
.ulink.danger{color:#f87171}
.ulink.danger:hover{background:rgba(232,39,42,.1)}
.rbadge{font-size:10px;padding:2px 6px;border-radius:6px;margin-left:6px}
.rb-admin{background:var(--rl);color:var(--red)}
.rb-client{background:rgba(74,158,248,.12);color:var(--b)}

/* CLIENT BAR */
.cbar{display:flex;align-items:center;gap:8px;padding:8px 20px;background:var(--bg2);border-bottom:1px solid var(--br);overflow-x:auto;white-space:nowrap}
.cbar::-webkit-scrollbar{height:3px}
.cbar::-webkit-scrollbar-thumb{background:var(--br2);border-radius:2px}
.cbl{font-size:10px;color:var(--m);text-transform:uppercase;letter-spacing:1px;flex-shrink:0}
.call{display:flex;align-items:center;gap:5px;padding:4px 11px;border-radius:6px;font-size:12px;font-weight:500;cursor:pointer;border:1px solid var(--br2);background:var(--rl);color:var(--red);flex-shrink:0;transition:all .15s}
.cchip{display:flex;align-items:center;gap:6px;padding:4px 11px;border-radius:6px;font-size:12px;cursor:pointer;border:1px solid transparent;background:var(--bg3);color:var(--t);flex-shrink:0;transition:all .15s}
.cchip:hover,.call.active{border-color:var(--rb);background:var(--rl)}
.cchip.active{border-color:var(--client-color,var(--rb));background:var(--client-color-bg,var(--rl));color:var(--client-color,var(--t))}
.cdot{width:7px;height:7px;border-radius:50%;flex-shrink:0}
.ctag{font-size:10px;padding:1px 5px;border-radius:3px;font-weight:500}
.tm{background:rgba(74,158,248,.15);color:var(--b)}.tg{background:rgba(232,39,42,.15);color:#f87171}.ts{background:rgba(34,201,122,.15);color:var(--g)}.tw{background:rgba(245,197,67,.15);color:var(--y)}.tsh{background:rgba(150,191,77,.15);color:#96bf4d}

/* LAYOUT */
.shell{display:flex;flex-direction:column;height:100vh}
.body{display:flex;flex:1;overflow:hidden}
aside{width:188px;min-width:188px;border-right:1px solid var(--br);padding:12px 9px;display:flex;flex-direction:column;gap:2px;overflow-y:auto;border-top:3px solid var(--client-color,transparent);transition:border-top-color .3s}
.sl{font-size:10px;font-weight:500;letter-spacing:1.2px;text-transform:uppercase;color:var(--m);padding:7px 8px 3px}
.si{display:flex;align-items:center;gap:7px;padding:7px 9px;border-radius:7px;font-size:13px;color:var(--m);cursor:pointer;border:1px solid transparent;background:none;width:100%;text-align:left;font-family:var(--fb);transition:all .15s}
.si:hover{color:var(--t);background:var(--bg3)}
.si.active{color:var(--t);background:var(--rl);border-color:var(--rb)}
.si.active .sic{color:var(--red)}
.sic{font-size:13px;width:16px;text-align:center}
.sbg{margin-left:auto;background:var(--red);color:#fff;font-size:10px;padding:1px 5px;border-radius:8px}
main{flex:1;overflow-y:auto;padding:20px;display:flex;flex-direction:column;gap:16px}
.page{display:none;flex-direction:column;gap:16px}
.page.active{display:flex}
.doc-help-btn{width:22px;height:22px;border-radius:50%;background:var(--bg3);border:1px solid var(--br2);color:var(--m);cursor:pointer;font-size:11px;font-weight:700;font-family:var(--fb);transition:all .15s;display:inline-flex;align-items:center;justify-content:center;flex-shrink:0;margin-right:4px}
.doc-help-btn:hover{background:var(--red);border-color:var(--red);color:#fff}

/* HELPERS */
.ph{display:flex;align-items:flex-end;justify-content:space-between}
.pt{font-family:var(--fh);font-size:20px;font-weight:700;letter-spacing:-.4px}
.ps{font-size:12px;color:var(--m);margin-top:2px}
.dbs{display:flex;gap:3px}
.db{padding:4px 9px;border-radius:5px;font-size:12px;color:var(--m);border:1px solid transparent;background:none;cursor:pointer;font-family:var(--fb);transition:all .15s}
.db:hover{color:var(--t)}.db.active{color:var(--t);border-color:var(--br2);background:var(--bg3)}
.metrics{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px}
#meta-metrics{grid-template-columns:repeat(4,minmax(0,1fr))}
.metric{background:var(--bg2);border:1px solid var(--br);border-radius:10px;padding:13px 15px;cursor:pointer;transition:all .15s}
.metric:hover{background:var(--bg3);border-color:var(--rb)}
.ml{font-size:11px;color:var(--m);margin-bottom:6px;display:flex;align-items:center;gap:5px}
.md{width:7px;height:7px;border-radius:50%}
.mv{font-family:var(--fh);font-size:23px;font-weight:700;letter-spacing:-1px}
.mdelta{font-size:11px;margin-top:2px}.up{color:var(--g)}.down{color:var(--danger)}
.g2{display:grid;grid-template-columns:1fr 1fr;gap:13px}
.g3{display:grid;grid-template-columns:2fr 1fr;gap:13px}
.card{background:var(--bg2);border:1px solid var(--br);border-radius:11px;padding:15px 17px}
.chd{display:flex;align-items:center;justify-content:space-between;margin-bottom:13px}
.ctit{font-family:var(--fh);font-size:13px;font-weight:600;display:flex;align-items:center;gap:6px}
.cact{font-size:12px;color:var(--red);cursor:pointer;background:none;border:none;font-family:var(--fb)}
.cw{position:relative;width:100%;height:160px}
.tbl{width:100%;border-collapse:collapse;font-size:12px}
.tbl th{text-align:left;font-size:10px;font-weight:500;color:var(--m);text-transform:uppercase;letter-spacing:.7px;padding:0 0 8px;border-bottom:1px solid var(--br)}
.tbl td{padding:7px 0;border-bottom:1px solid rgba(255,255,255,.03);vertical-align:middle}
.tbl tr:last-child td{border-bottom:none}
.badge{display:inline-flex;align-items:center;gap:4px;padding:2px 6px;border-radius:4px;font-size:11px;font-weight:500}
.pill{display:inline-block;padding:2px 8px;border-radius:14px;font-size:11px}
.pill-ok{background:rgba(34,201,122,.1);color:var(--g)}.pill-pause{background:rgba(245,197,67,.1);color:var(--y)}.pill-warn{background:rgba(232,39,42,.1);color:var(--danger)}
.srow{display:flex;align-items:center;gap:8px;padding:6px 0;border-bottom:1px solid rgba(255,255,255,.03);font-size:12px}
.srow:last-child{border-bottom:none}
.pos{width:25px;height:25px;border-radius:5px;display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:600;font-family:var(--fh);flex-shrink:0}
.pg{background:rgba(245,197,67,.15);color:var(--y)}.pgr{background:rgba(34,201,122,.15);color:var(--g)}.px{background:var(--bg3);color:var(--m)}
.skw{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.sstat{font-size:11px;color:var(--m);text-align:right;min-width:46px}

/* ——— CONTEXT BLOCK ——— */
.ctx-block{background:linear-gradient(135deg,rgba(155,127,244,.06) 0%,transparent 60%);border:1px solid rgba(155,127,244,.2);border-radius:11px;padding:14px 16px}
.ctx-hd{display:flex;align-items:center;gap:8px;margin-bottom:10px}
.ctx-icon{width:24px;height:24px;background:rgba(155,127,244,.15);border-radius:6px;display:flex;align-items:center;justify-content:center;font-size:12px;flex-shrink:0}
.ctx-title{font-family:var(--fh);font-size:13px;font-weight:600}
.ctx-sub{font-size:11px;color:var(--m)}
.ctx-ta{width:100%;background:rgba(255,255,255,.04);border:1px solid var(--br2);border-radius:8px;padding:10px 12px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;resize:vertical;min-height:80px;line-height:1.5}
.ctx-ta::placeholder{color:var(--m)}
.ctx-ta:focus{border-color:rgba(155,127,244,.4)}
.ctx-save{margin-top:8px;padding:6px 14px;border-radius:6px;font-size:12px;background:rgba(155,127,244,.15);color:var(--p);border:1px solid rgba(155,127,244,.3);cursor:pointer;font-family:var(--fb);transition:all .15s}
.ctx-save:hover{background:rgba(155,127,244,.25)}
.ctx-saved{font-size:11px;color:var(--g);margin-left:8px;display:none;transition:color .2s}
.btn-avi-analyze{display:inline-flex;align-items:center;gap:6px;padding:7px 16px;border-radius:7px;font-size:12px;font-weight:500;background:rgba(155,127,244,.12);border:1px solid rgba(155,127,244,.28);color:var(--p);cursor:pointer;font-family:var(--fb);transition:all .15s}
.btn-avi-analyze:hover{background:rgba(155,127,244,.22);border-color:rgba(155,127,244,.45)}

/* ——— META MODULE ——— */
.fmt-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:8px}
.fmt-card{background:var(--bg3);border:1px solid var(--br);border-radius:8px;padding:10px;cursor:pointer;transition:all .15s}
.fmt-card:hover,.fmt-card.active{border-color:rgba(74,158,248,.4);background:rgba(74,158,248,.07)}
.fmt-name{font-family:var(--fh);font-size:12px;font-weight:600;margin-bottom:3px}
.fmt-specs{font-size:11px;color:var(--m);line-height:1.5}
.fmt-detail{background:var(--bg3);border:1px solid var(--br2);border-radius:8px;padding:12px;margin-top:8px;display:none}
.fmt-detail.show{display:block}
.spec-row{display:flex;align-items:center;gap:8px;padding:5px 0;border-bottom:1px solid rgba(255,255,255,.04);font-size:12px}
.spec-row:last-child{border-bottom:none}
.spec-label{color:var(--m);width:110px;flex-shrink:0}
.spec-val{color:var(--t);font-weight:500}
.spec-ok{color:var(--g)}
.checklist{display:flex;flex-direction:column;gap:6px}
.cl-item{display:flex;align-items:center;gap:8px;padding:6px 10px;background:rgba(255,255,255,.02);border-radius:6px;font-size:12px;cursor:pointer;transition:background .15s}
.cl-item:hover{background:rgba(255,255,255,.05)}
.cl-check{width:16px;height:16px;border-radius:4px;border:1px solid var(--br2);display:flex;align-items:center;justify-content:center;flex-shrink:0;transition:all .15s}
.cl-item.done .cl-check{background:var(--g);border-color:var(--g)}
.cl-item.done .cl-check::after{content:'✓';color:#fff;font-size:10px}
.cl-item.done .cl-text{color:var(--m);text-decoration:line-through}

/* ——— SEGMENTATION BLOCK ——— */
.seg-grid{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.seg-field{display:flex;flex-direction:column;gap:5px}
.seg-label{font-size:11px;color:var(--m);text-transform:uppercase;letter-spacing:.7px}
.seg-input{background:var(--bg3);border:1px solid var(--br2);border-radius:7px;padding:8px 11px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;width:100%}
.seg-input:focus{border-color:rgba(147,51,234,.4)}
.seg-input::placeholder{color:var(--m)}
.seg-tags{display:flex;gap:6px;flex-wrap:wrap;margin-top:6px}
.stag{padding:4px 10px;border-radius:5px;font-size:12px;background:var(--bg3);border:1px solid var(--br2);cursor:pointer;transition:all .15s}
.stag:hover{border-color:var(--br)}.stag.active{background:var(--rl);border-color:var(--rb);color:var(--red)}
.budget-row{display:flex;align-items:center;gap:10px;margin-top:6px}
.budget-input{background:var(--bg3);border:1px solid var(--br2);border-radius:7px;padding:7px 11px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;flex:1}
.budget-input:focus{border-color:rgba(147,51,234,.4)}

/* ——— AVI PANEL ——— */
.avi{background:linear-gradient(135deg,rgba(147,51,234,.08) 0%,transparent 55%);border:1px solid var(--rb);border-radius:11px;padding:14px 16px;display:flex;flex-direction:column;gap:9px}
.avi-hd{display:flex;align-items:center;gap:8px}
.avi-orb{width:24px;height:24px;background:var(--red);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:11px;flex-shrink:0;animation:ap 3s ease-in-out infinite}
@keyframes ap{0%,100%{box-shadow:0 0 0 0 rgba(147,51,234,.4)}50%{box-shadow:0 0 0 7px rgba(147,51,234,0)}}
.avi-name{font-family:var(--fh);font-weight:700;font-size:13px}
.avi-sub{font-size:11px;color:var(--m)}
.ai{background:rgba(255,255,255,.03);border-radius:7px;padding:8px 11px;font-size:12px;color:rgba(240,240,240,.8);line-height:1.5;border-left:2px solid;cursor:pointer;transition:background .15s}
.ai:hover{background:rgba(255,255,255,.05)}
.ai-w{border-color:var(--y)}.ai-ok{border-color:var(--g)}.ai-r{border-color:var(--danger)}
.avi-row{display:flex;gap:7px}
.avi-in{flex:1;background:rgba(255,255,255,.05);border:1px solid var(--br2);border-radius:7px;padding:7px 10px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none}
.avi-in::placeholder{color:var(--m)}
.avi-in:focus{border-color:rgba(147,51,234,.5)}
.avi-send{background:var(--red);border:none;border-radius:7px;padding:7px 13px;color:#fff;font-size:13px;cursor:pointer;font-family:var(--fb);transition:opacity .15s}
.avi-send:hover{opacity:.85}
.avi-resp{background:rgba(255,255,255,.03);border-radius:7px;padding:9px 11px;font-size:12px;color:rgba(240,240,240,.85);line-height:1.6;border-left:2px solid var(--red);display:none;white-space:pre-wrap}
.avi-loading{display:flex;align-items:center;gap:8px;font-size:12px;color:var(--m)}
.avi-dots span{animation:dots .8s infinite;opacity:0}
.avi-dots span:nth-child(2){animation-delay:.2s}
.avi-dots span:nth-child(3){animation-delay:.4s}
@keyframes dots{0%,80%,100%{opacity:0}40%{opacity:1}}
@keyframes spin{to{transform:rotate(360deg)}}

/* ——— KEYWORDS MODULE ——— */
.kw-table{width:100%;border-collapse:collapse;font-size:12px;margin-top:10px}
.kw-table th{text-align:left;font-size:10px;color:var(--m);text-transform:uppercase;letter-spacing:.7px;padding:0 0 8px;border-bottom:1px solid var(--br)}
.kw-table td{padding:7px 0;border-bottom:1px solid rgba(255,255,255,.03)}
.kw-table tr:last-child td{border-bottom:none}
.kw-vol{font-size:11px;padding:2px 6px;border-radius:4px}
.kw-high{background:rgba(34,201,122,.12);color:var(--g)}
.kw-med{background:rgba(245,197,67,.12);color:var(--y)}
.kw-low{background:rgba(255,255,255,.06);color:var(--m)}
.kw-intent{font-size:10px;padding:2px 6px;border-radius:10px}
.ki-trans{background:rgba(74,158,248,.12);color:var(--b)}
.ki-info{background:rgba(155,127,244,.12);color:var(--p)}
.ki-nav{background:rgba(150,191,77,.12);color:#96bf4d}
.ad-group{background:var(--bg3);border:1px solid var(--br);border-radius:8px;padding:10px 12px;margin-bottom:8px}
.ag-title{font-family:var(--fh);font-size:12px;font-weight:600;margin-bottom:6px;color:var(--b)}
.ag-kws{display:flex;gap:5px;flex-wrap:wrap}
.ag-kw{font-size:11px;padding:2px 8px;border-radius:4px;background:rgba(74,158,248,.1);color:var(--b)}
.neg-kw{font-size:11px;padding:2px 8px;border-radius:4px;background:rgba(232,39,42,.1);color:var(--danger)}
.rsa-card{background:var(--bg3);border:1px solid var(--br2);border-radius:9px;padding:13px}
.rsa-url{font-size:12px;color:var(--g);margin-bottom:3px}
.rsa-title{font-size:14px;color:var(--b);font-weight:500;margin-bottom:4px;line-height:1.3}
.rsa-desc{font-size:12px;color:var(--m);line-height:1.5}

/* ——— AD PREVIEW ——— */
.ad-preview{background:var(--bg3);border:1px solid var(--br2);border-radius:10px;padding:14px;max-width:380px}
.ap-label{font-size:10px;color:var(--m);text-transform:uppercase;letter-spacing:1px;margin-bottom:10px}
.ap-img{height:180px;background:var(--bg4);border-radius:7px;display:flex;align-items:center;justify-content:center;color:var(--m);font-size:13px;margin-bottom:8px;border:1px dashed var(--br2)}
.ap-body{padding:4px 0}
.ap-name{font-size:11px;color:var(--m);display:flex;align-items:center;gap:5px;margin-bottom:4px}
.ap-sp{width:7px;height:7px;border-radius:50%;background:var(--m)}
.ap-head{font-size:14px;font-weight:500;margin-bottom:3px;line-height:1.3}
.ap-text{font-size:12px;color:var(--m);line-height:1.4;margin-bottom:8px}
.ap-cta{padding:7px 14px;border-radius:5px;background:var(--b);color:#fff;font-size:12px;font-weight:500;display:inline-block}

/* ——— SEO MODULE ——— */
.audit-row{display:flex;align-items:center;gap:10px;padding:7px 0;border-bottom:1px solid rgba(255,255,255,.03);font-size:12px}
.audit-row:last-child{border-bottom:none}
.audit-icon{width:22px;height:22px;border-radius:5px;display:flex;align-items:center;justify-content:center;font-size:11px;flex-shrink:0}
.a-ok{background:rgba(34,201,122,.15)}.a-warn{background:rgba(245,197,67,.15)}.a-err{background:rgba(232,39,42,.15)}

/* DONUT */
.dw{display:flex;align-items:center;gap:16px;margin-top:6px}
.dleg{display:flex;flex-direction:column;gap:6px;flex:1}
.di{display:flex;align-items:center;gap:7px;font-size:12px}
.dsq{width:8px;height:8px;border-radius:2px;flex-shrink:0}

/* CONFIG */
.cfg-card{background:var(--bg2);border:1px solid var(--br);border-radius:11px;padding:16px 18px;margin-bottom:11px}
.mf{display:flex;flex-direction:column;gap:5px;margin-bottom:12px}
.mf label{font-size:11px;color:var(--m);text-transform:uppercase;letter-spacing:.7px}
.mf input{background:var(--bg3);border:1px solid var(--br2);border-radius:7px;padding:8px 11px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;width:100%}
.mf input:focus{border-color:rgba(147,51,234,.5)}
.pr-btn{padding:8px 16px;border-radius:7px;font-size:13px;background:var(--red);border:none;color:#fff;cursor:pointer;font-family:var(--fb)}

/* CLIENT HEADER */
.chc{background:var(--bg2);border:1px solid var(--br);border-radius:11px;padding:14px 18px;display:flex;align-items:center;gap:14px}
.chc-av{width:40px;height:40px;border-radius:9px;display:flex;align-items:center;justify-content:center;font-family:var(--fh);font-weight:700;font-size:14px;flex-shrink:0}
.chc-info{flex:1}
.chc-name{font-family:var(--fh);font-size:15px;font-weight:700}
.chc-meta{font-size:12px;color:var(--m);margin-top:1px}
.chc-svcs{display:flex;gap:5px;margin-top:6px;flex-wrap:wrap}
.chc-acts{display:flex;gap:6px;flex-wrap:wrap}
.chc-btn{padding:5px 12px;border-radius:6px;font-size:12px;cursor:pointer;font-family:var(--fb);transition:all .15s;border:1px solid var(--br2);background:var(--bg3);color:var(--t)}
.chc-btn:hover{background:var(--bg4)}
.chc-btn.pr{background:var(--red);border-color:var(--red);color:#fff}
.chc-btn.pr:hover{opacity:.85}

/* CLIENT LIST */
.clist{display:flex;flex-direction:column;gap:7px}
.crow{display:flex;align-items:center;gap:11px;padding:10px 13px;background:var(--bg2);border:1px solid var(--br);border-radius:10px;cursor:pointer;transition:all .15s}
.crow:hover{border-color:var(--rb);background:var(--bg3)}
.crav{width:33px;height:33px;border-radius:7px;display:flex;align-items:center;justify-content:center;font-family:var(--fh);font-weight:700;font-size:13px;flex-shrink:0}
.crinfo{flex:1;min-width:0}
.crname{font-size:13px;font-weight:500}
.crdetail{font-size:11px;color:var(--m);margin-top:1px}
.crtags{display:flex;gap:4px;flex-wrap:wrap}
.crmetrics{display:flex;gap:12px;text-align:right}
.crmv{font-family:var(--fh);font-size:13px;font-weight:700}
.crml{font-size:10px;color:var(--m)}

::-webkit-scrollbar{width:4px;height:4px}
::-webkit-scrollbar-thumb{background:var(--br2);border-radius:2px}
@media(max-width:900px){.metrics{grid-template-columns:repeat(2,1fr)}.g2,.g3{grid-template-columns:1fr}aside{display:none}.fmt-grid{grid-template-columns:repeat(2,1fr)}.seg-grid{grid-template-columns:1fr}}
/* ── Mobile hamburger ── */
.mob-menu-btn{display:none;background:none;border:none;cursor:pointer;padding:6px;color:var(--t);font-size:22px;line-height:1;flex-shrink:0}
#mob-overlay{display:none;position:fixed;inset:0;background:rgba(0,0,0,.55);z-index:299}
#mob-overlay.open{display:block}
/* ── 768px breakpoint ── */
@media(max-width:768px){
  .mob-menu-btn{display:flex;align-items:center;justify-content:center}
  .nav-c{display:none}
  nav{padding:0 10px;gap:6px}
  .logo .ln{font-size:13px}
  aside{display:flex!important;position:fixed;left:0;top:0;bottom:0;width:230px;z-index:300;background:var(--bg2);border-right:1px solid var(--br2);padding:58px 9px 12px;transform:translateX(-100%);transition:transform .25s ease}
  aside.mob-open{transform:translateX(0)}
  main{padding:12px 10px}
  .metrics{grid-template-columns:repeat(2,minmax(0,1fr))}
  .g2,.g3{grid-template-columns:1fr}
  .fmt-grid{grid-template-columns:repeat(2,minmax(0,1fr))}
  .seg-grid{grid-template-columns:1fr}
  .card{overflow-x:auto}
  .tbl{min-width:480px}
  .ph{flex-direction:column;align-items:flex-start;gap:8px}
  .dbs{flex-wrap:wrap}
  #rpt-iframe{height:420px!important}
  .nav-r .live{display:none}
  /* Tab buttons — smaller + no-wrap so parent can scroll */
  .vid-tab,.img-tab,.tt-tab,.seo-tab,.g-tab{font-size:11px!important;padding:6px 10px!important;white-space:nowrap;flex-shrink:0}
  /* AVI drawer full-width on small tablets */
  #avi-fab{bottom:16px;right:16px}
  /* Ctx textarea more compact */
  .ctx-ta{min-height:72px}
  /* Avi-row: avoid wrapping the send button off-screen */
  .avi-row{min-width:0}
  /* Onboarding cards */
  .ob-services{grid-template-columns:1fr 1fr!important}
}
@media(max-width:480px){
  .metrics{grid-template-columns:1fr}
  .fmt-grid{grid-template-columns:1fr}
  /* Single column for onboarding services */
  .ob-services{grid-template-columns:1fr!important}
  /* Tab buttons even smaller */
  .vid-tab,.img-tab,.tt-tab{font-size:10px!important;padding:5px 8px!important}
}
/* ── Tooltips ── */
[data-tip]{cursor:help}
th[data-tip],div[data-tip],span[data-tip]{text-decoration:underline dotted rgba(255,255,255,.25);text-underline-offset:3px}
html[data-theme="light"] th[data-tip],html[data-theme="light"] div[data-tip],html[data-theme="light"] span[data-tip]{text-decoration-color:rgba(0,0,0,.25)}
.dm-tip{position:fixed;z-index:99999;background:var(--bg2);color:var(--t);padding:8px 12px;border-radius:8px;font-size:12px;line-height:1.5;max-width:240px;pointer-events:none;opacity:0;transition:opacity .12s;border:1px solid var(--br2);box-shadow:0 8px 24px rgba(0,0,0,.5);font-family:var(--fb);font-weight:400;display:none;word-wrap:break-word}
html[data-theme="light"] .dm-tip{box-shadow:0 4px 20px rgba(0,0,0,.15)}
/* ── Theme button ── */
.theme-btn{display:flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:7px;border:1px solid var(--br2);background:none;cursor:pointer;font-size:15px;color:var(--m);transition:all .15s;line-height:1;flex-shrink:0}
.theme-btn:hover{background:var(--bg3);color:var(--t)}
/* ── Light mode: hardcoded semi-transparent whites → variables ── */
html[data-theme="light"] .tbl td{border-bottom-color:var(--row-div)}
html[data-theme="light"] .kw-table td{border-bottom-color:var(--row-div)}
html[data-theme="light"] .srow{border-bottom-color:var(--row-div)}
html[data-theme="light"] .ai{background:var(--sub-bg);color:var(--t)}
html[data-theme="light"] .ctx-ta{background:var(--sub-bg)}
html[data-theme="light"] .avi-in{background:var(--sub-bg)}
html[data-theme="light"] .avi-resp{background:var(--sub-bg);color:var(--t)}
html[data-theme="light"] .audit-row{border-bottom-color:var(--row-div)}
html[data-theme="light"] .spec-row{border-bottom-color:var(--row-div)}
html[data-theme="light"] .cl-item{background:var(--sub-bg)}
html[data-theme="light"] .avi{background:linear-gradient(135deg,rgba(147,51,234,.05) 0%,transparent 55%)}
html[data-theme="light"] .ctx-block{background:linear-gradient(135deg,rgba(155,127,244,.05) 0%,transparent 60%)}
html[data-theme="light"] .fmt-detail{background:var(--bg3)}
html[data-theme="light"] .ap-img{border-style:dashed}
/* ══ WIZARD NUEVA CAMPAÑA ══ */
.gcw-overlay{position:fixed;inset:0;z-index:3000;background:rgba(0,0,0,.78);display:flex;align-items:flex-start;justify-content:center;overflow-y:auto;padding:24px 16px}
.gcw-box{background:var(--bg2);border-radius:16px;border:1px solid var(--br2);width:100%;max-width:800px;overflow:hidden;margin:auto;box-shadow:0 24px 60px rgba(0,0,0,.5)}
.gcw-head{padding:18px 24px;border-bottom:1px solid var(--br);display:flex;align-items:center;gap:14px}
.gcw-title{font-family:var(--fh);font-size:15px;font-weight:700;flex:1}
.gcw-close{background:none;border:none;font-size:20px;color:var(--m);cursor:pointer;line-height:1;padding:2px}
.gcw-close:hover{color:var(--t)}
.gcw-progress{display:flex;align-items:center;gap:0;flex:1}
.gcw-prog-step{display:flex;flex-direction:column;align-items:center;flex:1;position:relative}
.gcw-prog-step+.gcw-prog-step::before{content:'';position:absolute;top:12px;right:50%;width:100%;height:1px;background:var(--br);z-index:0}
.gcw-prog-step.done+.gcw-prog-step::before{background:var(--g)}
.gcw-ps-num{width:24px;height:24px;border-radius:50%;border:2px solid var(--br2);background:var(--bg3);display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:700;font-family:var(--fh);z-index:1;transition:all .2s;flex-shrink:0}
.gcw-prog-step.done .gcw-ps-num{background:var(--g);border-color:var(--g);color:#000}
.gcw-prog-step.active .gcw-ps-num{background:var(--red);border-color:var(--red);color:#fff}
.gcw-ps-lbl{font-size:10px;color:var(--m);margin-top:3px;text-align:center;white-space:nowrap}
.gcw-prog-step.active .gcw-ps-lbl{color:var(--t)}
.gcw-body{padding:26px 24px;min-height:320px}
.gcw-foot{padding:14px 24px;border-top:1px solid var(--br);display:flex;align-items:center;justify-content:space-between;gap:8px}
.gcw-btn{padding:10px 22px;border-radius:8px;font-size:13px;font-weight:600;cursor:pointer;font-family:var(--fh);border:none;transition:all .15s}
.gcw-btn-sec{background:var(--bg3);border:1px solid var(--br2)!important;color:var(--t)}
.gcw-btn-sec:hover{background:var(--bg4)}
.gcw-btn-pri{background:var(--red);color:#fff}
.gcw-btn-pri:hover{opacity:.87}
.gcw-btn-pri:disabled{opacity:.4;cursor:not-allowed}
.gcw-btn-grn{background:var(--g);color:#000}
.gcw-btn-grn:hover{opacity:.87}
/* Step labels */
.gcw-stit{font-family:var(--fh);font-size:19px;font-weight:700;margin-bottom:5px}
.gcw-ssub{font-size:13px;color:var(--m);margin-bottom:20px;line-height:1.5}
.gcw-slbl{font-size:11px;color:var(--m);text-transform:uppercase;letter-spacing:.7px;margin-bottom:9px;font-weight:500}
/* Type cards */
.gcw-type-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:10px;margin-bottom:22px}
.gcw-tc{background:var(--bg3);border:2px solid var(--br2);border-radius:11px;padding:16px 12px;cursor:pointer;transition:all .15s;text-align:center}
.gcw-tc:hover{border-color:rgba(147,51,234,.4)}
.gcw-tc.sel{border-color:var(--red);background:rgba(147,51,234,.07)}
.gcw-tc-icon{font-size:28px;margin-bottom:8px}
.gcw-tc-name{font-family:var(--fh);font-size:13px;font-weight:700;margin-bottom:4px}
.gcw-tc-desc{font-size:11px;color:var(--m);line-height:1.4}
/* Goals */
.gcw-goals{display:flex;gap:7px;flex-wrap:wrap;margin-bottom:4px}
.gcw-goal{padding:7px 15px;border-radius:20px;border:1.5px solid var(--br2);background:var(--bg3);cursor:pointer;font-size:12px;transition:all .15s;display:flex;align-items:center;gap:6px}
.gcw-goal:hover{border-color:rgba(147,51,234,.4)}
.gcw-goal.sel{border-color:var(--red);background:rgba(147,51,234,.08)}
/* Ad group cards */
.gcw-ag{background:var(--bg3);border:1px solid var(--br2);border-radius:10px;padding:14px 16px;margin-bottom:10px}
.gcw-ag-hd{display:flex;align-items:center;gap:9px;margin-bottom:11px}
.gcw-ag-n{width:22px;height:22px;border-radius:50%;background:var(--red);color:#fff;font-size:11px;font-weight:700;display:flex;align-items:center;justify-content:center;flex-shrink:0}
.gcw-kw-tags{display:flex;flex-wrap:wrap;gap:5px;min-height:28px}
.gcw-kw{display:inline-flex;align-items:center;gap:4px;padding:3px 8px;border-radius:4px;font-size:11px;font-weight:500}
.gcw-kw-B{background:rgba(74,158,248,.12);color:var(--b)}.gcw-kw-P{background:rgba(34,201,122,.12);color:var(--g)}.gcw-kw-E{background:rgba(155,127,244,.12);color:var(--p)}
.gcw-kw-x{cursor:pointer;opacity:.55;font-size:10px;margin-left:1px}.gcw-kw-x:hover{opacity:1}
/* Ad preview */
.gcw-prev{background:var(--bg2);border:1px solid var(--br2);border-radius:8px;padding:13px;margin-top:10px}
.gcw-prev-lbl{font-size:10px;color:var(--m);letter-spacing:.5px;text-transform:uppercase;margin-bottom:8px}
.gcw-prev-sp{font-size:11px;color:var(--m);margin-bottom:2px}
.gcw-prev-url{font-size:12px;color:var(--g);margin-bottom:3px}
.gcw-prev-ttl{font-size:15px;color:#4a9ef8;margin-bottom:3px;line-height:1.3}
.gcw-prev-dsc{font-size:12px;color:var(--t);line-height:1.4}
/* Input fields */
.gcw-inp{background:var(--bg3);border:1px solid var(--br2);border-radius:7px;padding:8px 11px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;width:100%;box-sizing:border-box}
.gcw-inp:focus{border-color:rgba(147,51,234,.5)}
.gcw-ta{background:var(--bg3);border:1px solid var(--br2);border-radius:7px;padding:9px 11px;font-size:13px;color:var(--t);font-family:var(--fb);outline:none;width:100%;box-sizing:border-box;resize:vertical}
.gcw-ta:focus{border-color:rgba(147,51,234,.5)}
/* Generating state */
.gcw-gen{display:flex;flex-direction:column;align-items:center;justify-content:center;padding:50px 20px;gap:14px;text-align:center}
.gcw-gen-orb{width:52px;height:52px;background:var(--red);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:22px;animation:gcwPulse 1.5s ease-in-out infinite}
@keyframes gcwPulse{0%,100%{box-shadow:0 0 0 0 rgba(147,51,234,.4)}50%{box-shadow:0 0 0 14px rgba(147,51,234,0)}}
/* Success */
.gcw-ok{background:rgba(34,201,122,.1);border:1px solid rgba(34,201,122,.3);border-radius:8px;padding:10px 14px;font-size:12px;color:var(--g);display:flex;align-items:center;gap:8px}
.gcw-err{background:rgba(232,39,42,.08);border:1px solid rgba(232,39,42,.25);border-radius:8px;padding:10px 14px;font-size:12px;color:#f87171;display:flex;align-items:center;gap:8px}
/* Location pills */
.gcw-loc{display:inline-flex;align-items:center;gap:5px;padding:4px 10px;border-radius:5px;font-size:12px;background:rgba(34,201,122,.1);color:var(--g);border:1px solid rgba(34,201,122,.25);margin:3px}
/* Smooth transition on theme change */
html,body,nav,.cbar,aside,main,.card,.metric,.cfg-card,.tbl,.umenu,.cchip,.crow,.si,.nt,.ctx-ta,.avi-in,.seg-input,.mf input{transition:background-color .2s,border-color .2s,color .15s}
</style>
<?php analytics_head(true); ?>
</head>
<body>
<?php if (!empty($_sub)) trial_banner($_sub); ?>
<div class="shell">

<!-- NAV -->
<nav>
  <div style="display:flex;align-items:center;gap:8px">
    <button class="mob-menu-btn" data-click="toggleMobSidebar" aria-label="Menú">☰</button>
    <div class="logo"><div class="lm">B</div><div><div class="ln"><span class="logo-b">B</span>rain</div><div class="ls">Super Cerebro</div></div></div>
  </div>
  <div class="nav-c">
    <button class="nt active" data-click="showPage|overview" data-page="overview" data-i18n="nav.overview">Resumen</button>
    <?php if(_nav('marca')): ?><button class="nt" data-click="showPage|marca" data-page="marca" data-i18n="nav.brand">Activos de Marca</button><?php endif ?>
    <?php if(_nav('reportes')): ?><button class="nt" data-click="showPage|reportes" data-page="reportes" data-i18n="nav.reports">Reportes</button><?php endif ?>
    <?php if(_nav('automatizaciones')): ?><button class="nt" data-click="showPage|automatizaciones" data-page="automatizaciones">⚡ Automatizaciones</button><?php endif ?>
    <button class="nt" data-click="showPage|config" data-page="config" data-i18n="nav.apis">⚙ APIs</button>
    <button class="nt" data-click="showPage|docs" data-page="docs">📚 Ayuda</button>
  </div>
  <div class="nav-r">
    <div class="live"><div class="dot"></div><span data-i18n="status.online">En línea</span></div>
    <div style="display:flex;gap:2px;align-items:center">
      <button data-click="gtLang|es" id="gl-es" title="Español"   style="font-size:16px;background:none;border:none;cursor:pointer;opacity:.45;transition:opacity .2s;padding:0 2px;line-height:1">🇪🇸</button>
      <button data-click="gtLang|en" id="gl-en" title="English"   style="font-size:16px;background:none;border:none;cursor:pointer;opacity:.45;transition:opacity .2s;padding:0 2px;line-height:1">🇺🇸</button>
      <button data-click="gtLang|pt" id="gl-pt" title="Português" style="font-size:16px;background:none;border:none;cursor:pointer;opacity:.45;transition:opacity .2s;padding:0 2px;line-height:1">🇧🇷</button>
      <button data-click="gtLang|fr" id="gl-fr" title="Français"  style="font-size:16px;background:none;border:none;cursor:pointer;opacity:.45;transition:opacity .2s;padding:0 2px;line-height:1">🇫🇷</button>
      <button data-click="gtLang|de" id="gl-de" title="Deutsch"   style="font-size:16px;background:none;border:none;cursor:pointer;opacity:.45;transition:opacity .2s;padding:0 2px;line-height:1">🇩🇪</button>
    </div>
    <button id="dm-theme-btn" class="theme-btn" data-click="toggleTheme" title="Cambiar tema claro/oscuro">🌙</button>
    <div class="udrop">
      <div class="uav" style="background:<?= htmlspecialchars($user['color']) ?>" data-click="_scToggleClassSib|__el|open"><?= strtoupper(substr($user['name'],0,2)) ?></div>
      <div class="umenu">
        <div class="uinfo">
          <div class="uname"><?= htmlspecialchars($user['name']) ?><span class="rbadge rb-<?= $user['role'] ?>"><?= $user['role']==='admin'?'Admin':'Cliente' ?></span></div>
          <div class="urole"><?= htmlspecialchars($user['email']) ?></div>
        </div>
        <div class="udiv"></div>
        <?php if($isAdmin): ?>
        <a href="users.php"   class="ulink">👥 Gestionar usuarios</a>
        <a href="clients.php" class="ulink">🏢 Gestionar clientes</a>
        <div class="udiv"></div>
        <?php endif ?>
        <button class="ulink" data-click="showPage|profile">👤 Mi perfil</button>
        <a href="logout.php" class="ulink danger">→ Cerrar sesión</a>
      </div>
    </div>
  </div>
</nav>

<!-- CLIENT BAR -->
<div class="cbar">
  <span class="cbl">Cliente:</span>
  <?php if($isAdmin): ?>
  <div class="call active" id="chip-all" data-click="selectClient|all">◈ Todos</div>
  <?php endif ?>
  <?php foreach($clients as $c):
    $svcs=array_filter(explode(',',$c['services']));
  ?>
  <div class="cchip" id="chip-<?= $c['id'] ?>" data-click="selectClient|<?= $c['id'] ?>">
    <div class="cdot" style="background:<?= htmlspecialchars($c['color']) ?>"></div>
    <?= htmlspecialchars($c['name']) ?>
    <div style="display:flex;gap:2px"><?php foreach($svcs as $s) echo '<span class="ctag t'.$s[0].'">'.strtoupper($s).'</span>'; ?></div>
  </div>
  <?php endforeach ?>
  <?php $max_cl = $_sub['max_clients'] ?? ($isAdmin ? 999 : 0); if($isAdmin || ($max_cl > 0 && count($clients) < $max_cl)): ?><button data-click="openNewClientModal" style="padding:4px 9px;border-radius:5px;font-size:12px;cursor:pointer;border:1px dashed var(--br2);background:none;color:var(--m);flex-shrink:0;font-family:var(--fb)">+ Agregar</button><?php endif ?>
</div>

<div id="mob-overlay" data-click="toggleMobSidebar"></div>
<div class="body">
<aside>
  <?php if(_nav('meta')): ?><button class="si" data-click="showPage|meta" data-page="meta"><span class="sic">◉</span><span data-i18n="nav.meta">Meta Ads</span><span class="sbg" style="display:none">!</span></button><?php endif ?>
  <?php if(_nav('google')): ?><button class="si" data-click="showPage|google" data-page="google"><span class="sic">◎</span><span data-i18n="nav.google">Google Ads</span></button><?php endif ?>
  <?php if(_nav('seo')): ?><button class="si" data-click="showPage|seo" data-page="seo"><span class="sic">◐</span><span data-i18n="nav.seo">SEO</span></button><?php endif ?>
  <?php if(_nav('analytics')): ?><button class="si" data-click="showPage|analytics" data-page="analytics"><span class="sic">📊</span><span>Google Analytics</span></button><?php endif ?>
  <?php if(_nav('web')): ?><button class="si" data-click="showPage|web" data-page="web"><span class="sic">▣</span><span data-i18n="nav.web">Sitios Web</span></button><?php endif ?>
  <?php if(_nav('videos')): ?><button class="si" data-click="showPage|videos" data-page="videos"><span class="sic">🎬</span><span data-i18n="nav.videos">Videos</span></button><?php endif ?>
  <?php if(_nav('imagenes')): ?><button class="si" data-click="showPage|imagenes" data-page="imagenes"><span class="sic">🖼</span><span data-i18n="nav.images">Imágenes IA</span></button><?php endif ?>
  <?php if(_nav('tiktok')): ?><button class="si" data-click="showPage|tiktok" data-page="tiktok"><span class="sic">🎵</span><span data-i18n="nav.tiktok">TikTok Ads</span></button><?php endif ?>
  <?php if(_nav('email')): ?><button class="si" data-click="showPage|email" data-page="email"><span class="sic">📧</span><span data-i18n="nav.email">Email Marketing</span></button><?php endif ?>
  <div class="sl" style="margin-top:auto;padding-top:12px" data-i18n="nav.section.account">Cuenta</div>
  <button class="si" data-click="showPage|profile" data-page="profile"><span class="sic">👤</span><span data-i18n="nav.profile">Mi perfil</span></button>
  <?php if($isAdmin): ?>
  <a href="users.php" style="text-decoration:none"><button class="si"><span class="sic">👥</span><span data-i18n="nav.users">Usuarios</span></button></a>
  <?php endif ?>
  <button class="si" data-click="showPage|config" data-page="config"><span class="sic">⚙</span><span data-i18n="nav.apis">APIs</span></button>
  <button class="si" data-click="showPage|docs" data-page="docs"><span class="sic">📚</span><span>Ayuda</span></button>
  <a href="logout.php" style="text-decoration:none"><button class="si" style="color:#f87171"><span class="sic">→</span><span data-i18n="nav.logout">Cerrar sesión</span></button></a>
</aside>

<main>


<?php include __DIR__ . '/sections/resumen.php'; ?>
<?php include __DIR__ . '/sections/meta.php'; ?>
<?php include __DIR__ . '/sections/google.php'; ?>
<?php include __DIR__ . '/sections/seo.php'; ?>
<?php include __DIR__ . '/sections/analytics.php'; ?>
<?php include __DIR__ . '/sections/web.php'; ?>
<?php include __DIR__ . '/sections/videos.php'; ?>
<?php include __DIR__ . '/sections/imagenes.php'; ?>
<?php include __DIR__ . '/sections/marcas.php'; ?>
<?php include __DIR__ . '/sections/profile.php'; ?>
<?php include __DIR__ . '/sections/apis.php'; ?>
<?php include __DIR__ . '/sections/reportes.php'; ?>
<?php include __DIR__ . '/sections/tiktok.php'; ?>
<?php include __DIR__ . '/sections/email.php'; ?>
<?php include __DIR__ . '/sections/automatizaciones.php'; ?>
<?php if(file_exists(__DIR__.'/sections/onboarding.php')) include __DIR__.'/sections/onboarding.php'; ?>
<?php include __DIR__ . '/sections/docs.php'; ?>

</main>
</div>
</div>

<?php include __DIR__ . '/sections/modals.php'; ?>

<script>
function toggleMobSidebar(){
  document.querySelector('aside').classList.toggle('mob-open');
  document.getElementById('mob-overlay').classList.toggle('open');
}
document.addEventListener('click',function(e){
  if(e.target.closest('aside .si')||e.target.closest('aside a')){
    document.querySelector('aside').classList.remove('mob-open');
    document.getElementById('mob-overlay').classList.remove('open');
  }
});
</script>
<script>
const MONTHS=['Oct','Nov','Dic','Ene','Feb','Mar','Abr'];
const isAdmin=<?= $isAdmin?'true':'false' ?>;
const CLIENTS=<?= json_encode(array_values($clients)) ?>;
let currentClient='<?= $defaultClient ?>';
</script>
<script src="js/app.js?v=<?= filemtime(__DIR__.'/js/app.js') ?>"></script>
<script src="js/docs-content.js?v=<?= filemtime(__DIR__.'/js/docs-content.js') ?>"></script>
<script src="js/sc-dispatch.js?v=<?= filemtime(__DIR__.'/js/sc-dispatch.js') ?>"></script>
<?php upgrade_modal(); ?>
<?php crisp_widget($user); ?>
<?php if (!empty($user)) echo analytics_identify($user + ['plan' => $_sub['plan'] ?? 'trial']); ?>
</body>
</html>
<?php
// Inyectar nonce en todos los <script> blocks del buffer de salida
$_html = ob_get_clean();
$_n    = htmlspecialchars($csp_nonce, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$_html = preg_replace(
    '/<script\b(?![^>]*\bnonce=)([^>]*)>/i',
    '<script$1 nonce="' . $_n . '">',
    $_html
);
echo $_html;
?>
